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 */
25 #include "bytevector.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
36 transcoders_not_implemented (void)
38 fprintf (stderr
, "%s: warning: transcoders not implemented\n",
43 /* End-of-file object. */
45 SCM_DEFINE (scm_r6rs_eof_object
, "eof-object", 0, 0, 0,
47 "Return the end-of-file object.")
48 #define FUNC_NAME s_scm_r6rs_eof_object
57 /* Bytevector input ports or "bip" for short. */
58 static scm_t_bits bytevector_input_port_type
= 0;
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
);
91 /* Mark the underlying bytevector. */
92 return (SCM_PACK (SCM_STREAM (port
)));
96 bip_fill_input (SCM port
)
99 scm_t_port
*c_port
= SCM_PTAB_ENTRY (port
);
101 if (c_port
->read_pos
>= c_port
->read_end
)
104 result
= (int) *c_port
->read_pos
;
110 bip_seek (SCM port
, off_t offset
, int whence
)
111 #define FUNC_NAME "bip_seek"
114 scm_t_port
*c_port
= SCM_PTAB_ENTRY (port
);
119 offset
+= c_port
->read_pos
- c_port
->read_buf
;
123 if (c_port
->read_buf
+ offset
< c_port
->read_end
)
125 c_port
->read_pos
= c_port
->read_buf
+ offset
;
129 scm_out_of_range (FUNC_NAME
, scm_from_int (offset
));
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
;
139 scm_out_of_range (FUNC_NAME
, scm_from_int (offset
));
143 scm_wrong_type_arg_msg (FUNC_NAME
, 0, port
,
144 "invalid `seek' parameter");
152 /* Instantiate the bytevector input port type. */
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
));
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)
205 make_cbip (SCM read_proc
, SCM get_position_proc
,
206 SCM set_position_proc
, SCM close_proc
)
208 SCM port
, bv
, method_vector
;
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
);
246 /* Mark the underlying bytevector and methods. */
247 return (SCM_PACK (SCM_STREAM (port
)));
251 cbip_fill_input (SCM port
)
252 #define FUNC_NAME "cbip_fill_input"
255 scm_t_port
*c_port
= SCM_PTAB_ENTRY (port
);
258 if (c_port
->read_pos
>= c_port
->read_end
)
260 /* Invoke the user's `read!' procedure. */
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
;
280 result
= (int) *c_port
->read_pos
;
287 cbip_seek (SCM port
, off_t offset
, int whence
)
288 #define FUNC_NAME "cbip_seek"
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
);
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
);
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
));
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. */
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'");
340 cbip_close (SCM port
)
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
);
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
;
368 SCM_VALIDATE_PROC (3, get_position_proc
);
370 if (set_position_proc
== SCM_UNDEFINED
)
371 set_position_proc
= SCM_BOOL_F
;
373 SCM_VALIDATE_PROC (4, set_position_proc
);
375 if (close_proc
== SCM_UNDEFINED
)
376 close_proc
= SCM_BOOL_F
;
378 SCM_VALIDATE_PROC (5, close_proc
);
380 return (make_cbip (read_proc
, get_position_proc
, set_position_proc
,
386 /* Instantiate the custom binary input port type. */
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
);
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,
408 "Read an octet from @var{port}, a binary input port, "
409 "blocking as necessary.")
410 #define FUNC_NAME s_scm_r6rs_get_u8
415 SCM_VALIDATE_R6RS_BINARY_INPUT_PORT (1, port
);
417 c_result
= scm_getc (port
);
419 result
= SCM_EOF_VAL
;
421 result
= SCM_I_MAKINUM ((unsigned char) c_result
);
427 SCM_DEFINE (scm_r6rs_lookahead_u8
, "lookahead-u8", 1, 0, 0,
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
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
));
441 result
= SCM_EOF_VAL
;
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
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
);
470 /* Don't invoke `scm_c_read ()' since it may block. */
473 if ((c_read
== 0) && (c_count
> 0))
475 if (SCM_EOF_OBJECT_P (scm_peek_char (port
)))
476 result
= SCM_EOF_VAL
;
478 result
= scm_r6rs_null_bytevector
;
482 if (c_read
< c_count
)
483 result
= scm_r6rs_c_shrink_bytevector (result
, c_read
);
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 "
496 #define FUNC_NAME s_scm_r6rs_get_bytevector_n_x
500 unsigned c_start
, c_count
, c_len
;
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
);
517 /* Don't invoke `scm_c_read ()' since it may block. */
520 if ((c_read
== 0) && (c_count
> 0))
522 if (SCM_EOF_OBJECT_P (scm_peek_char (port
)))
523 result
= SCM_EOF_VAL
;
525 result
= SCM_I_MAKINUM (0);
528 result
= scm_from_size_t (c_read
);
535 SCM_DEFINE (scm_r6rs_get_bytevector_some
, "get-bytevector-some", 1, 0, 0,
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). */
551 SCM_VALIDATE_R6RS_BINARY_INPUT_PORT (1, port
);
554 c_bv
= (char *) scm_gc_malloc (c_len
, SCM_GC_BYTEVECTOR
);
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,
569 /* We can't use `scm_c_read ()' since it blocks. */
570 c_chr
= scm_getc (port
);
573 c_bv
[c_total
] = (char) c_chr
;
577 while ((scm_is_true (scm_char_ready_p (port
)))
578 && (!SCM_EOF_OBJECT_P (scm_peek_char (port
))));
582 result
= SCM_EOF_VAL
;
583 scm_gc_free (c_bv
, c_len
, SCM_GC_BYTEVECTOR
);
589 /* Shrink the bytevector. */
590 c_bv
= (char *) scm_gc_realloc (c_bv
, c_len
, c_total
,
592 c_len
= (unsigned) c_total
;
595 result
= scm_r6rs_c_take_bytevector ((signed char *) c_bv
, c_len
);
602 SCM_DEFINE (scm_r6rs_get_bytevector_all
, "get-bytevector-all", 1, 0, 0,
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
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,
632 /* `scm_c_read ()' blocks until C_COUNT bytes are available or EOF is
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
)));
641 result
= SCM_EOF_VAL
;
642 scm_gc_free (c_bv
, c_len
, SCM_GC_BYTEVECTOR
);
648 /* Shrink the bytevector. */
649 c_bv
= (char *) scm_gc_realloc (c_bv
, c_len
, c_total
,
651 c_len
= (unsigned) c_total
;
654 result
= scm_r6rs_c_take_bytevector ((signed char *) c_bv
, c_len
);
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
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
;
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} "
690 #define FUNC_NAME s_scm_r6rs_put_bytevector
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
);
713 if (EXPECT_FALSE (c_start
>= c_len
))
714 scm_out_of_range (FUNC_NAME
, start
);
716 c_count
= c_len
- c_start
;
720 c_start
= 0, c_count
= c_len
;
722 scm_c_write (port
, c_bv
+ c_start
, c_count
);
724 return SCM_UNSPECIFIED
;
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. */
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)))
769 bop_buffer_init (scm_t_bop_buffer
*buf
)
771 buf
->total_len
= buf
->len
= buf
->pos
= 0;
776 bop_buffer_grow (scm_t_bop_buffer
*buf
, size_t min_size
)
781 for (new_size
= buf
->total_len
782 ? buf
->total_len
: SCM_BOP_BUFFER_INITIAL_SIZE
;
787 new_buf
= scm_gc_realloc ((void *) buf
->buffer
, buf
->total_len
,
788 new_size
, SCM_GC_BOP
);
790 new_buf
= scm_gc_malloc (new_size
, SCM_GC_BOP
);
792 buf
->buffer
= new_buf
;
793 buf
->total_len
= new_size
;
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
,
821 return (scm_values (scm_list_2 (port
, bop_proc
)));
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
);
834 scm_gc_free (buf
->buffer
, buf
->total_len
, SCM_GC_BOP
);
836 scm_gc_free (buf
, sizeof (* buf
), SCM_GC_BOP
);
841 /* Write SIZE octets from DATA to PORT. */
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
);
854 buf
->len
= (buf
->len
> buf
->pos
) ? buf
->len
: buf
->pos
;
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
);
867 offset
+= (off_t
) buf
->pos
;
871 if (offset
> buf
->len
)
872 scm_out_of_range (FUNC_NAME
, scm_from_int (offset
));
878 if (offset
>= buf
->len
)
879 scm_out_of_range (FUNC_NAME
, scm_from_int (offset
));
881 buf
->pos
= buf
->len
- offset
;
885 scm_wrong_type_arg_msg (FUNC_NAME
, 0, port
,
886 "invalid `seek' parameter");
893 /* Fetch data from a bop. */
894 SCM_SMOB_APPLY (bytevector_output_port_procedure
,
895 bop_proc_apply
, 0, 0, 0, (SCM bop_proc
))
898 scm_t_bop_buffer
*buf
, result_buf
;
900 port
= SCM_PACK (SCM_SMOB_DATA (bop_proc
));
901 buf
= SCM_R6RS_BOP_BUFFER (port
);
904 bop_buffer_init (buf
);
906 if (result_buf
.len
== 0)
907 bv
= scm_r6rs_c_take_bytevector (NULL
, 0);
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
,
917 bv
= scm_r6rs_c_take_bytevector ((signed char *) result_buf
.buffer
,
924 SCM_SMOB_MARK (bytevector_output_port_procedure
, bop_proc_mark
,
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,
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 ());
948 initialize_bytevector_output_ports (void)
950 bytevector_output_port_type
=
951 scm_make_port_type ("r6rs-bytevector-output-port",
954 scm_set_port_seek (bytevector_output_port_type
, bop_seek
);
955 scm_set_port_free (bytevector_output_port_type
, bop_free
);
960 /* Initialization. */
963 scm_init_r6rs_ports (void)
967 initialize_bytevector_input_ports ();
968 initialize_custom_binary_input_ports ();
969 initialize_bytevector_output_ports ();
972 /* arch-tag: c041ffd6-8871-40e8-a25a-d0f8768a71dc