More `.gitignore'.
[guile-r6rs-libs.git] / src / ports.c
blob8d5c016a410ba4d19f5976d05b6db89f7eeb31ec
1 /* Guile-R6RS-Libs --- Implementation of R6RS standard libraries.
2 Copyright (C) 2007 Ludovic Courtès <ludovic.courtes@laas.fr>
4 Guile-R6RS-Libs is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 Guile-R6RS-Libs is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with Guile-R6RS-Libs; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
18 #include "config.h"
20 #include <libguile.h>
21 #include <string.h>
22 #include <stdio.h>
24 #include "ports.h"
25 #include "bytevector.h"
26 #include "utils.h"
29 /* Unimplemented features. */
32 /* Transoders are currently not implemented since Guile 1.8 is not
33 Unicode-capable. Thus, most of the code here assumes the use of the
34 binary transcoder. */
35 static inline void
36 transcoders_not_implemented (void)
38 fprintf (stderr, "%s: warning: transcoders not implemented\n",
39 PACKAGE_NAME);
43 /* End-of-file object. */
45 SCM_DEFINE (scm_r6rs_eof_object, "eof-object", 0, 0, 0,
46 (void),
47 "Return the end-of-file object.")
48 #define FUNC_NAME s_scm_r6rs_eof_object
50 return (SCM_EOF_VAL);
52 #undef FUNC_NAME
55 /* Input ports. */
57 /* Bytevector input ports or "bip" for short. */
58 static scm_t_bits bytevector_input_port_type = 0;
60 static inline SCM
61 make_bip (SCM bv)
63 SCM port;
64 char *c_bv;
65 unsigned c_len;
66 scm_t_port *c_port;
67 const unsigned long mode_bits = SCM_OPN | SCM_RDNG | SCM_BUF0;
69 port = scm_new_port_table_entry (bytevector_input_port_type);
71 /* Prevent BV from being GC'd. */
72 SCM_SETSTREAM (port, SCM_UNPACK (bv));
74 /* Have the port directly access the bytevector. */
75 c_bv = (char *) SCM_R6RS_BYTEVECTOR_CONTENTS (bv);
76 c_len = SCM_R6RS_BYTEVECTOR_LENGTH (bv);
78 c_port = SCM_PTAB_ENTRY (port);
79 c_port->read_pos = c_port->read_buf = (unsigned char *) c_bv;
80 c_port->read_end = (unsigned char *) c_bv + c_len;
82 /* Mark PORT as open, readable and unbuffered (hmm, how elegant...). */
83 SCM_SET_CELL_TYPE (port, bytevector_input_port_type | mode_bits);
85 return port;
88 static SCM
89 bip_mark (SCM port)
91 /* Mark the underlying bytevector. */
92 return (SCM_PACK (SCM_STREAM (port)));
95 static int
96 bip_fill_input (SCM port)
98 int result;
99 scm_t_port *c_port = SCM_PTAB_ENTRY (port);
101 if (c_port->read_pos >= c_port->read_end)
102 result = EOF;
103 else
104 result = (int) *c_port->read_pos;
106 return result;
109 static off_t
110 bip_seek (SCM port, off_t offset, int whence)
111 #define FUNC_NAME "bip_seek"
113 off_t c_result = 0;
114 scm_t_port *c_port = SCM_PTAB_ENTRY (port);
116 switch (whence)
118 case SEEK_CUR:
119 offset += c_port->read_pos - c_port->read_buf;
120 /* Fall through. */
122 case SEEK_SET:
123 if (c_port->read_buf + offset < c_port->read_end)
125 c_port->read_pos = c_port->read_buf + offset;
126 c_result = offset;
128 else
129 scm_out_of_range (FUNC_NAME, scm_from_int (offset));
130 break;
132 case SEEK_END:
133 if (c_port->read_end - offset >= c_port->read_buf)
135 c_port->read_pos = c_port->read_end - offset;
136 c_result = c_port->read_pos - c_port->read_buf;
138 else
139 scm_out_of_range (FUNC_NAME, scm_from_int (offset));
140 break;
142 default:
143 scm_wrong_type_arg_msg (FUNC_NAME, 0, port,
144 "invalid `seek' parameter");
147 return c_result;
149 #undef FUNC_NAME
152 /* Instantiate the bytevector input port type. */
153 static inline void
154 initialize_bytevector_input_ports (void)
156 bytevector_input_port_type =
157 scm_make_port_type ("r6rs-bytevector-input-port",
158 bip_fill_input, NULL);
160 scm_set_port_mark (bytevector_input_port_type, bip_mark);
161 scm_set_port_seek (bytevector_input_port_type, bip_seek);
165 SCM_DEFINE (scm_r6rs_open_bytevector_input_port,
166 "open-bytevector-input-port", 1, 1, 0,
167 (SCM bv, SCM transcoder),
168 "Return an input port whose contents are drawn from "
169 "bytevector @var{bv}.")
170 #define FUNC_NAME s_scm_r6rs_open_bytevector_input_port
172 SCM_VALIDATE_R6RS_BYTEVECTOR (1, bv);
173 if (transcoder != SCM_UNDEFINED)
174 transcoders_not_implemented ();
176 return (make_bip (bv));
178 #undef FUNC_NAME
182 /* Custom binary input port ("cbip" for short). */
184 static scm_t_bits custom_binary_input_port_type = 0;
186 /* Size of the buffer embedded in custom binary input ports. */
187 #define CBIP_BUFFER_SIZE 4096
189 /* Return the bytevector associated to PORT. */
190 #define SCM_R6RS_CBIP_BYTEVECTOR(_port) \
191 SCM_SIMPLE_VECTOR_REF (SCM_PACK (SCM_STREAM (_port)), 0)
193 /* Return the various procedures of PORT. */
194 #define SCM_R6RS_CBIP_READ_PROC(_port) \
195 SCM_SIMPLE_VECTOR_REF (SCM_PACK (SCM_STREAM (_port)), 1)
196 #define SCM_R6RS_CBIP_GET_POSITION_PROC(_port) \
197 SCM_SIMPLE_VECTOR_REF (SCM_PACK (SCM_STREAM (_port)), 2)
198 #define SCM_R6RS_CBIP_SET_POSITION_PROC(_port) \
199 SCM_SIMPLE_VECTOR_REF (SCM_PACK (SCM_STREAM (_port)), 3)
200 #define SCM_R6RS_CBIP_CLOSE_PROC(_port) \
201 SCM_SIMPLE_VECTOR_REF (SCM_PACK (SCM_STREAM (_port)), 4)
204 static inline SCM
205 make_cbip (SCM read_proc, SCM get_position_proc,
206 SCM set_position_proc, SCM close_proc)
208 SCM port, bv, method_vector;
209 char *c_bv;
210 unsigned c_len;
211 scm_t_port *c_port;
212 const unsigned long mode_bits = SCM_OPN | SCM_RDNG | SCM_BUF0;
214 /* Use a bytevector as the underlying buffer. */
215 c_len = CBIP_BUFFER_SIZE;
216 bv = scm_r6rs_c_make_bytevector (c_len);
217 c_bv = (char *) SCM_R6RS_BYTEVECTOR_CONTENTS (bv);
219 /* Store the various methods and bytevector in a vector. */
220 method_vector = scm_c_make_vector (5, SCM_BOOL_F);
221 SCM_SIMPLE_VECTOR_SET (method_vector, 0, bv);
222 SCM_SIMPLE_VECTOR_SET (method_vector, 1, read_proc);
223 SCM_SIMPLE_VECTOR_SET (method_vector, 2, get_position_proc);
224 SCM_SIMPLE_VECTOR_SET (method_vector, 3, set_position_proc);
225 SCM_SIMPLE_VECTOR_SET (method_vector, 4, close_proc);
227 port = scm_new_port_table_entry (custom_binary_input_port_type);
229 /* Attach it the method vector. */
230 SCM_SETSTREAM (port, SCM_UNPACK (method_vector));
232 /* Have the port directly access the buffer (bytevector). */
233 c_port = SCM_PTAB_ENTRY (port);
234 c_port->read_pos = c_port->read_buf = (unsigned char *) c_bv;
235 c_port->read_end = (unsigned char *) c_bv;
237 /* Mark PORT as open, readable and unbuffered (hmm, how elegant...). */
238 SCM_SET_CELL_TYPE (port, custom_binary_input_port_type | mode_bits);
240 return port;
243 static SCM
244 cbip_mark (SCM port)
246 /* Mark the underlying bytevector and methods. */
247 return (SCM_PACK (SCM_STREAM (port)));
250 static int
251 cbip_fill_input (SCM port)
252 #define FUNC_NAME "cbip_fill_input"
254 int result;
255 scm_t_port *c_port = SCM_PTAB_ENTRY (port);
257 again:
258 if (c_port->read_pos >= c_port->read_end)
260 /* Invoke the user's `read!' procedure. */
261 unsigned c_octets;
262 SCM bv, read_proc, octets;
264 bv = SCM_R6RS_CBIP_BYTEVECTOR (port);
265 read_proc = SCM_R6RS_CBIP_READ_PROC (port);
267 octets = scm_call_3 (read_proc, bv, SCM_INUM0,
268 SCM_I_MAKINUM (CBIP_BUFFER_SIZE));
269 c_octets = scm_to_uint (octets);
271 c_port->read_pos = (unsigned char *) SCM_R6RS_BYTEVECTOR_CONTENTS (bv);
272 c_port->read_end = (unsigned char *) c_port->read_pos + c_octets;
274 if (c_octets > 0)
275 goto again;
276 else
277 result = EOF;
279 else
280 result = (int) *c_port->read_pos;
282 return result;
284 #undef FUNC_NAME
286 static off_t
287 cbip_seek (SCM port, off_t offset, int whence)
288 #define FUNC_NAME "cbip_seek"
290 SCM result;
291 off_t c_result = 0;
293 switch (whence)
295 case SEEK_CUR:
297 SCM get_position_proc;
299 get_position_proc = SCM_R6RS_CBIP_GET_POSITION_PROC (port);
300 if (EXPECT_TRUE (scm_is_true (get_position_proc)))
301 result = scm_call_0 (get_position_proc);
302 else
303 scm_wrong_type_arg_msg (FUNC_NAME, 0, port,
304 "R6RS custom binary input port does not "
305 "support `port-position'");
307 offset += scm_to_int (result);
308 /* Fall through. */
311 case SEEK_SET:
313 SCM set_position_proc;
315 set_position_proc = SCM_R6RS_CBIP_SET_POSITION_PROC (port);
316 if (EXPECT_TRUE (scm_is_true (set_position_proc)))
317 result = scm_call_1 (set_position_proc, scm_from_int (offset));
318 else
319 scm_wrong_type_arg_msg (FUNC_NAME, 0, port,
320 "R6RS custom binary input port does not "
321 "support `set-port-position!'");
323 /* Assuming setting the position succeeded. */
324 c_result = offset;
325 break;
328 default:
329 /* `SEEK_END' cannot be supported. */
330 scm_wrong_type_arg_msg (FUNC_NAME, 0, port,
331 "R6RS custom binary input ports do not "
332 "support `SEEK_END'");
335 return c_result;
337 #undef FUNC_NAME
339 static int
340 cbip_close (SCM port)
342 SCM close_proc;
344 close_proc = SCM_R6RS_CBIP_CLOSE_PROC (port);
345 if (scm_is_true (close_proc))
346 /* Invoke the `close' thunk. */
347 scm_call_0 (close_proc);
349 return 1;
353 SCM_DEFINE (scm_r6rs_make_custom_binary_input_port,
354 "make-custom-binary-input-port", 2, 3, 0,
355 (SCM id, SCM read_proc, SCM get_position_proc,
356 SCM set_position_proc, SCM close_proc),
357 "Return a new custom binary input port whose input is drained "
358 "by invoking @var{read_proc} and passing it a bytevector, an "
359 "index where octets should be written, and an octet count.")
360 #define FUNC_NAME s_scm_r6rs_make_custom_binary_input_port
362 SCM_VALIDATE_STRING (1, id);
363 SCM_VALIDATE_PROC (2, read_proc);
365 if (get_position_proc == SCM_UNDEFINED)
366 get_position_proc = SCM_BOOL_F;
367 else
368 SCM_VALIDATE_PROC (3, get_position_proc);
370 if (set_position_proc == SCM_UNDEFINED)
371 set_position_proc = SCM_BOOL_F;
372 else
373 SCM_VALIDATE_PROC (4, set_position_proc);
375 if (close_proc == SCM_UNDEFINED)
376 close_proc = SCM_BOOL_F;
377 else
378 SCM_VALIDATE_PROC (5, close_proc);
380 return (make_cbip (read_proc, get_position_proc, set_position_proc,
381 close_proc));
383 #undef FUNC_NAME
386 /* Instantiate the custom binary input port type. */
387 static inline void
388 initialize_custom_binary_input_ports (void)
390 custom_binary_input_port_type =
391 scm_make_port_type ("r6rs-custom-binary-input-port",
392 cbip_fill_input, NULL);
394 scm_set_port_mark (custom_binary_input_port_type, cbip_mark);
395 scm_set_port_seek (custom_binary_input_port_type, cbip_seek);
396 scm_set_port_close (custom_binary_input_port_type, cbip_close);
401 /* Binary input. */
403 /* We currently don't support specific binary input ports. */
404 #define SCM_VALIDATE_R6RS_BINARY_INPUT_PORT SCM_VALIDATE_OPINPORT
406 SCM_DEFINE (scm_r6rs_get_u8, "get-u8", 1, 0, 0,
407 (SCM port),
408 "Read an octet from @var{port}, a binary input port, "
409 "blocking as necessary.")
410 #define FUNC_NAME s_scm_r6rs_get_u8
412 SCM result;
413 int c_result;
415 SCM_VALIDATE_R6RS_BINARY_INPUT_PORT (1, port);
417 c_result = scm_getc (port);
418 if (c_result == EOF)
419 result = SCM_EOF_VAL;
420 else
421 result = SCM_I_MAKINUM ((unsigned char) c_result);
423 return result;
425 #undef FUNC_NAME
427 SCM_DEFINE (scm_r6rs_lookahead_u8, "lookahead-u8", 1, 0, 0,
428 (SCM port),
429 "Like @code{get-u8} but does not update @var{port} to "
430 "point past the octet.")
431 #define FUNC_NAME s_scm_r6rs_lookahead_u8
433 SCM result;
435 SCM_VALIDATE_R6RS_BINARY_INPUT_PORT (1, port);
437 result = scm_peek_char (port);
438 if (SCM_CHARP (result))
439 result = SCM_I_MAKINUM ((signed char) SCM_CHAR (result));
440 else
441 result = SCM_EOF_VAL;
443 return result;
445 #undef FUNC_NAME
447 SCM_DEFINE (scm_r6rs_get_bytevector_n, "get-bytevector-n", 2, 0, 0,
448 (SCM port, SCM count),
449 "Read @var{count} octets from @var{port}, blocking as "
450 "necessary and return a bytevector containing the octets "
451 "read. If fewer bytes are available, a bytevector smaller "
452 "than @var{count} is returned.")
453 #define FUNC_NAME s_scm_r6rs_get_bytevector_n
455 SCM result;
456 char *c_bv;
457 unsigned c_count;
458 size_t c_read;
460 SCM_VALIDATE_R6RS_BINARY_INPUT_PORT (1, port);
461 c_count = scm_to_uint (count);
463 result = scm_r6rs_c_make_bytevector (c_count);
464 c_bv = (char *) SCM_R6RS_BYTEVECTOR_CONTENTS (result);
466 if (EXPECT_TRUE (c_count > 0))
467 /* XXX: `scm_c_read ()' does not update the port position. */
468 c_read = scm_c_read (port, c_bv, c_count);
469 else
470 /* Don't invoke `scm_c_read ()' since it may block. */
471 c_read = 0;
473 if ((c_read == 0) && (c_count > 0))
475 if (SCM_EOF_OBJECT_P (scm_peek_char (port)))
476 result = SCM_EOF_VAL;
477 else
478 result = scm_r6rs_null_bytevector;
480 else
482 if (c_read < c_count)
483 result = scm_r6rs_c_shrink_bytevector (result, c_read);
486 return result;
488 #undef FUNC_NAME
490 SCM_DEFINE (scm_r6rs_get_bytevector_n_x, "get-bytevector-n!", 4, 0, 0,
491 (SCM port, SCM bv, SCM start, SCM count),
492 "Read @var{count} bytes from @var{port} and store them "
493 "in @var{bv} starting at index @var{start}. Return either "
494 "the number of bytes actually read or the end-of-file "
495 "object.")
496 #define FUNC_NAME s_scm_r6rs_get_bytevector_n_x
498 SCM result;
499 char *c_bv;
500 unsigned c_start, c_count, c_len;
501 size_t c_read;
503 SCM_VALIDATE_R6RS_BINARY_INPUT_PORT (1, port);
504 SCM_VALIDATE_R6RS_BYTEVECTOR (2, bv);
505 c_start = scm_to_uint (start);
506 c_count = scm_to_uint (count);
508 c_bv = (char *) SCM_R6RS_BYTEVECTOR_CONTENTS (bv);
509 c_len = SCM_R6RS_BYTEVECTOR_LENGTH (bv);
511 if (EXPECT_FALSE (c_start + c_count > c_len))
512 scm_out_of_range (FUNC_NAME, count);
514 if (EXPECT_TRUE (c_count > 0))
515 c_read = scm_c_read (port, c_bv + c_start, c_count);
516 else
517 /* Don't invoke `scm_c_read ()' since it may block. */
518 c_read = 0;
520 if ((c_read == 0) && (c_count > 0))
522 if (SCM_EOF_OBJECT_P (scm_peek_char (port)))
523 result = SCM_EOF_VAL;
524 else
525 result = SCM_I_MAKINUM (0);
527 else
528 result = scm_from_size_t (c_read);
530 return result;
532 #undef FUNC_NAME
535 SCM_DEFINE (scm_r6rs_get_bytevector_some, "get-bytevector-some", 1, 0, 0,
536 (SCM port),
537 "Read from @var{port}, blocking as necessary, until data "
538 "are available or and end-of-file is reached. Return either "
539 "a new bytevector containing the data read or the "
540 "end-of-file object.")
541 #define FUNC_NAME s_scm_r6rs_get_bytevector_some
543 /* Read at least one byte, unless the end-of-file is already reached, and
544 read while characters are available (buffered). */
546 SCM result;
547 char *c_bv;
548 unsigned c_len;
549 size_t c_total;
551 SCM_VALIDATE_R6RS_BINARY_INPUT_PORT (1, port);
553 c_len = 4096;
554 c_bv = (char *) scm_gc_malloc (c_len, SCM_GC_BYTEVECTOR);
555 c_total = 0;
559 int c_chr;
561 if (c_total + 1 > c_len)
563 /* Grow the bytevector. */
564 c_bv = (char *) scm_gc_realloc (c_bv, c_len, c_len * 2,
565 SCM_GC_BYTEVECTOR);
566 c_len *= 2;
569 /* We can't use `scm_c_read ()' since it blocks. */
570 c_chr = scm_getc (port);
571 if (c_chr != EOF)
573 c_bv[c_total] = (char) c_chr;
574 c_total++;
577 while ((scm_is_true (scm_char_ready_p (port)))
578 && (!SCM_EOF_OBJECT_P (scm_peek_char (port))));
580 if (c_total == 0)
582 result = SCM_EOF_VAL;
583 scm_gc_free (c_bv, c_len, SCM_GC_BYTEVECTOR);
585 else
587 if (c_len > c_total)
589 /* Shrink the bytevector. */
590 c_bv = (char *) scm_gc_realloc (c_bv, c_len, c_total,
591 SCM_GC_BYTEVECTOR);
592 c_len = (unsigned) c_total;
595 result = scm_r6rs_c_take_bytevector ((signed char *) c_bv, c_len);
598 return result;
600 #undef FUNC_NAME
602 SCM_DEFINE (scm_r6rs_get_bytevector_all, "get-bytevector-all", 1, 0, 0,
603 (SCM port),
604 "Read from @var{port}, blocking as necessary, until "
605 "the end-of-file is reached. Return either "
606 "a new bytevector containing the data read or the "
607 "end-of-file object (if no data were available).")
608 #define FUNC_NAME s_scm_r6rs_get_bytevector_all
610 SCM result;
611 char *c_bv;
612 unsigned c_len, c_count;
613 size_t c_read, c_total;
615 SCM_VALIDATE_R6RS_BINARY_INPUT_PORT (1, port);
617 c_len = c_count = 4096;
618 c_bv = (char *) scm_gc_malloc (c_len, SCM_GC_BYTEVECTOR);
619 c_total = c_read = 0;
623 if (c_total + c_read > c_len)
625 /* Grow the bytevector. */
626 c_bv = (char *) scm_gc_realloc (c_bv, c_len, c_len * 2,
627 SCM_GC_BYTEVECTOR);
628 c_count = c_len;
629 c_len *= 2;
632 /* `scm_c_read ()' blocks until C_COUNT bytes are available or EOF is
633 reached. */
634 c_read = scm_c_read (port, c_bv + c_total, c_count);
635 c_total += c_read, c_count -= c_read;
637 while (!SCM_EOF_OBJECT_P (scm_peek_char (port)));
639 if (c_total == 0)
641 result = SCM_EOF_VAL;
642 scm_gc_free (c_bv, c_len, SCM_GC_BYTEVECTOR);
644 else
646 if (c_len > c_total)
648 /* Shrink the bytevector. */
649 c_bv = (char *) scm_gc_realloc (c_bv, c_len, c_total,
650 SCM_GC_BYTEVECTOR);
651 c_len = (unsigned) c_total;
654 result = scm_r6rs_c_take_bytevector ((signed char *) c_bv, c_len);
657 return result;
659 #undef FUNC_NAME
663 /* Binary output. */
665 /* We currently don't support specific binary input ports. */
666 #define SCM_VALIDATE_R6RS_BINARY_OUTPUT_PORT SCM_VALIDATE_OPOUTPORT
669 SCM_DEFINE (scm_r6rs_put_u8, "put-u8", 2, 0, 0,
670 (SCM port, SCM octet),
671 "Write @var{octet} to binary port @var{port}.")
672 #define FUNC_NAME s_scm_r6rs_put_u8
674 scm_t_uint8 c_octet;
676 SCM_VALIDATE_R6RS_BINARY_OUTPUT_PORT (1, port);
677 c_octet = scm_to_uint8 (octet);
679 scm_putc ((char) c_octet, port);
681 return SCM_UNSPECIFIED;
683 #undef FUNC_NAME
685 SCM_DEFINE (scm_r6rs_put_bytevector, "put-bytevector", 2, 2, 0,
686 (SCM port, SCM bv, SCM start, SCM count),
687 "Write the contents of @var{bv} to @var{port}, optionally "
688 "starting at index @var{start} and limiting to @var{count} "
689 "octets.")
690 #define FUNC_NAME s_scm_r6rs_put_bytevector
692 char *c_bv;
693 unsigned c_start, c_count, c_len;
695 SCM_VALIDATE_R6RS_BINARY_OUTPUT_PORT (1, port);
696 SCM_VALIDATE_R6RS_BYTEVECTOR (2, bv);
698 c_len = SCM_R6RS_BYTEVECTOR_LENGTH (bv);
699 c_bv = (char *) SCM_R6RS_BYTEVECTOR_CONTENTS (bv);
701 if (start != SCM_UNDEFINED)
703 c_start = scm_to_uint (start);
705 if (count != SCM_UNDEFINED)
707 c_count = scm_to_uint (count);
708 if (EXPECT_FALSE (c_start + c_count > c_len))
709 scm_out_of_range (FUNC_NAME, count);
711 else
713 if (EXPECT_FALSE (c_start >= c_len))
714 scm_out_of_range (FUNC_NAME, start);
715 else
716 c_count = c_len - c_start;
719 else
720 c_start = 0, c_count = c_len;
722 scm_c_write (port, c_bv + c_start, c_count);
724 return SCM_UNSPECIFIED;
726 #undef FUNC_NAME
730 /* Bytevector output port ("bop" for short). */
732 /* Implementation of "bops".
734 Each bop has an internal buffer, of type `scm_t_bop_buffer', attached to
735 it. The procedure returned along with the output port is actually an
736 applicable SMOB. The SMOB holds a reference to the port. When applied,
737 the SMOB swallows the port's internal buffer, turning it into a
738 bytevector, and resets it.
740 XXX: Access to a bop's internal buffer is not thread-safe. */
742 static scm_t_bits bytevector_output_port_type = 0;
744 SCM_SMOB (bytevector_output_port_procedure,
745 "r6rs-bytevector-output-port-procedure",
748 #define SCM_GC_BOP "r6rs-bytevector-output-port"
749 #define SCM_BOP_BUFFER_INITIAL_SIZE 4096
751 /* Representation of a bop's internal buffer. */
752 typedef struct
754 size_t total_len;
755 size_t len;
756 size_t pos;
757 char *buffer;
758 } scm_t_bop_buffer;
761 /* Accessing a bop's buffer. */
762 #define SCM_R6RS_BOP_BUFFER(_port) \
763 ((scm_t_bop_buffer *) SCM_STREAM (_port))
764 #define SCM_R6RS_SET_BOP_BUFFER(_port, _buf) \
765 (SCM_SETSTREAM ((_port), (scm_t_bits) (_buf)))
768 static inline void
769 bop_buffer_init (scm_t_bop_buffer *buf)
771 buf->total_len = buf->len = buf->pos = 0;
772 buf->buffer = NULL;
775 static inline void
776 bop_buffer_grow (scm_t_bop_buffer *buf, size_t min_size)
778 char *new_buf;
779 size_t new_size;
781 for (new_size = buf->total_len
782 ? buf->total_len : SCM_BOP_BUFFER_INITIAL_SIZE;
783 new_size < min_size;
784 new_size *= 2);
786 if (buf->buffer)
787 new_buf = scm_gc_realloc ((void *) buf->buffer, buf->total_len,
788 new_size, SCM_GC_BOP);
789 else
790 new_buf = scm_gc_malloc (new_size, SCM_GC_BOP);
792 buf->buffer = new_buf;
793 buf->total_len = new_size;
796 static inline SCM
797 make_bop (void)
799 SCM port, bop_proc;
800 scm_t_port *c_port;
801 scm_t_bop_buffer *buf;
802 const unsigned long mode_bits = SCM_OPN | SCM_WRTNG | SCM_BUF0;
804 port = scm_new_port_table_entry (bytevector_output_port_type);
806 buf = (scm_t_bop_buffer *) scm_gc_malloc (sizeof (* buf), SCM_GC_BOP);
807 bop_buffer_init (buf);
809 c_port = SCM_PTAB_ENTRY (port);
810 c_port->write_buf = c_port->write_pos = c_port->write_end = NULL;
812 SCM_R6RS_SET_BOP_BUFFER (port, buf);
814 /* Mark PORT as open and writable. */
815 SCM_SET_CELL_TYPE (port, bytevector_output_port_type | mode_bits);
817 /* Make the bop procedure. */
818 SCM_NEWSMOB (bop_proc, bytevector_output_port_procedure,
819 SCM_PACK (port));
821 return (scm_values (scm_list_2 (port, bop_proc)));
824 static size_t
825 bop_free (SCM port)
827 /* The port itself is necessarily freed _after_ the bop proc, since the bop
828 proc holds a reference to it. Thus we can safely free the internal
829 buffer when the bop becomes unreferenced. */
830 scm_t_bop_buffer *buf;
832 buf = SCM_R6RS_BOP_BUFFER (port);
833 if (buf->buffer)
834 scm_gc_free (buf->buffer, buf->total_len, SCM_GC_BOP);
836 scm_gc_free (buf, sizeof (* buf), SCM_GC_BOP);
838 return 0;
841 /* Write SIZE octets from DATA to PORT. */
842 static void
843 bop_write (SCM port, const void *data, size_t size)
845 scm_t_bop_buffer *buf;
847 buf = SCM_R6RS_BOP_BUFFER (port);
849 if (buf->pos + size > buf->total_len)
850 bop_buffer_grow (buf, buf->pos + size);
852 memcpy (buf->buffer + buf->pos, data, size);
853 buf->pos += size;
854 buf->len = (buf->len > buf->pos) ? buf->len : buf->pos;
857 static off_t
858 bop_seek (SCM port, off_t offset, int whence)
859 #define FUNC_NAME "bop_seek"
861 scm_t_bop_buffer *buf;
863 buf = SCM_R6RS_BOP_BUFFER (port);
864 switch (whence)
866 case SEEK_CUR:
867 offset += (off_t) buf->pos;
868 /* Fall through. */
870 case SEEK_SET:
871 if (offset > buf->len)
872 scm_out_of_range (FUNC_NAME, scm_from_int (offset));
873 else
874 buf->pos = offset;
875 break;
877 case SEEK_END:
878 if (offset >= buf->len)
879 scm_out_of_range (FUNC_NAME, scm_from_int (offset));
880 else
881 buf->pos = buf->len - offset;
882 break;
884 default:
885 scm_wrong_type_arg_msg (FUNC_NAME, 0, port,
886 "invalid `seek' parameter");
889 return buf->pos;
891 #undef FUNC_NAME
893 /* Fetch data from a bop. */
894 SCM_SMOB_APPLY (bytevector_output_port_procedure,
895 bop_proc_apply, 0, 0, 0, (SCM bop_proc))
897 SCM port, bv;
898 scm_t_bop_buffer *buf, result_buf;
900 port = SCM_PACK (SCM_SMOB_DATA (bop_proc));
901 buf = SCM_R6RS_BOP_BUFFER (port);
903 result_buf = *buf;
904 bop_buffer_init (buf);
906 if (result_buf.len == 0)
907 bv = scm_r6rs_c_take_bytevector (NULL, 0);
908 else
910 if (result_buf.total_len > result_buf.len)
911 /* Shrink the buffer. */
912 result_buf.buffer = scm_gc_realloc ((void *) result_buf.buffer,
913 result_buf.total_len,
914 result_buf.len,
915 SCM_GC_BOP);
917 bv = scm_r6rs_c_take_bytevector ((signed char *) result_buf.buffer,
918 result_buf.len);
921 return bv;
924 SCM_SMOB_MARK (bytevector_output_port_procedure, bop_proc_mark,
925 bop_proc)
927 /* Mark the port associated to BOP_PROC. */
928 return (SCM_PACK (SCM_SMOB_DATA (bop_proc)));
932 SCM_DEFINE (scm_r6rs_open_bytevector_output_port,
933 "open-bytevector-output-port", 0, 1, 0,
934 (SCM transcoder),
935 "Return two values: an output port and a procedure. The latter "
936 "should be called with zero arguments to obtain a bytevector "
937 "containing the data accumulated by the port.")
938 #define FUNC_NAME scm_r6rs_open_bytevector_output_port
940 if (transcoder != SCM_UNDEFINED)
941 transcoders_not_implemented ();
943 return (make_bop ());
945 #undef FUNC_NAME
947 static inline void
948 initialize_bytevector_output_ports (void)
950 bytevector_output_port_type =
951 scm_make_port_type ("r6rs-bytevector-output-port",
952 NULL, bop_write);
954 scm_set_port_seek (bytevector_output_port_type, bop_seek);
955 scm_set_port_free (bytevector_output_port_type, bop_free);
960 /* Initialization. */
962 void
963 scm_init_r6rs_ports (void)
965 #include "ports.x"
967 initialize_bytevector_input_ports ();
968 initialize_custom_binary_input_ports ();
969 initialize_bytevector_output_ports ();
972 /* arch-tag: c041ffd6-8871-40e8-a25a-d0f8768a71dc