123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379 |
- typedef enum scm_t_port_type_flags {
-
- SCM_PORT_TYPE_NEEDS_CLOSE_ON_GC = 1 << 0
- } scm_t_port_type_flags;
- struct scm_t_port_type
- {
- char *name
- int (*print) (SCM exp, SCM port, scm_print_state *pstate)
- size_t (*c_read) (SCM port, SCM dst, size_t start, size_t count)
- size_t (*c_write) (SCM port, SCM src, size_t start, size_t count)
- SCM scm_read;
- SCM scm_write;
- int (*read_wait_fd) (SCM port)
- int (*write_wait_fd) (SCM port)
- scm_t_off (*seek) (SCM port, scm_t_off OFFSET, int WHENCE)
- void (*close) (SCM port)
- void (*get_natural_buffer_sizes) (SCM port, size_t *read_size,
- size_t *write_size)
- int (*random_access_p) (SCM port)
- int (*input_waiting) (SCM port)
- void (*truncate) (SCM port, scm_t_off length)
- unsigned flags
-
- SCM input_class, output_class, input_output_class
- }
- enum scm_port_buffer_field {
- SCM_PORT_BUFFER_FIELD_BYTEVECTOR,
- SCM_PORT_BUFFER_FIELD_CUR,
- SCM_PORT_BUFFER_FIELD_END,
- SCM_PORT_BUFFER_FIELD_HAS_EOF_P,
- SCM_PORT_BUFFER_FIELD_POSITION,
- SCM_PORT_BUFFER_FIELD_COUNT
- }
- static inline SCM
- scm_port_buffer_bytevector (SCM buf)
- {
- return SCM_SIMPLE_VECTOR_REF (buf, SCM_PORT_BUFFER_FIELD_BYTEVECTOR);
- }
- static inline SCM
- scm_port_buffer_cur (SCM buf)
- {
- return SCM_SIMPLE_VECTOR_REF (buf, SCM_PORT_BUFFER_FIELD_CUR);
- }
- static inline void
- scm_port_buffer_set_cur (SCM buf, SCM cur)
- {
- SCM_SIMPLE_VECTOR_SET (buf, SCM_PORT_BUFFER_FIELD_CUR, cur)
- }
- static inline SCM
- scm_port_buffer_end (SCM buf)
- {
- return SCM_SIMPLE_VECTOR_REF (buf, SCM_PORT_BUFFER_FIELD_END);
- }
- static inline void
- scm_port_buffer_set_end (SCM buf, SCM end)
- {
- SCM_SIMPLE_VECTOR_SET (buf, SCM_PORT_BUFFER_FIELD_END, end)
- }
- static inline SCM
- scm_port_buffer_has_eof_p (SCM buf)
- {
- return SCM_SIMPLE_VECTOR_REF (buf, SCM_PORT_BUFFER_FIELD_HAS_EOF_P);
- }
- static inline void
- scm_port_buffer_set_has_eof_p (SCM buf, SCM has_eof_p)
- {
- SCM_SIMPLE_VECTOR_SET (buf, SCM_PORT_BUFFER_FIELD_HAS_EOF_P,
- has_eof_p)
- }
- static inline SCM
- scm_port_buffer_position (SCM buf)
- {
- return SCM_SIMPLE_VECTOR_REF (buf, SCM_PORT_BUFFER_FIELD_POSITION);
- }
- static inline SCM
- scm_port_position_line (SCM position)
- {
- return scm_car (position)
- }
- static inline void
- scm_port_position_set_line (SCM position, SCM line)
- {
- scm_set_car_x (position, line)
- }
- static inline SCM
- scm_port_position_column (SCM position)
- {
- return scm_cdr (position)
- }
- static inline void
- scm_port_position_set_column (SCM position, SCM column)
- {
- scm_set_cdr_x (position, column)
- }
- static inline size_t
- scm_port_buffer_size (SCM buf)
- {
- SCM bv = scm_port_buffer_bytevector (buf);
- if (SCM_LIKELY (SCM_BYTEVECTOR_P (bv)))
- return SCM_BYTEVECTOR_LENGTH (bv);
- scm_misc_error (NULL, "invalid port buffer ~a", scm_list_1 (bv));
- return -1
- }
- static inline void
- scm_port_buffer_reset (SCM buf)
- {
- scm_port_buffer_set_cur (buf, SCM_INUM0);
- scm_port_buffer_set_end (buf, SCM_INUM0);
- }
- static inline void
- scm_port_buffer_reset_end (SCM buf)
- {
- scm_port_buffer_set_cur (buf, scm_from_size_t (scm_port_buffer_size (buf)));
- scm_port_buffer_set_end (buf, scm_from_size_t (scm_port_buffer_size (buf)));
- }
- static inline size_t
- scm_port_buffer_can_take (SCM buf, size_t *cur_out)
- {
- size_t cur, end
- cur = scm_to_size_t (scm_port_buffer_cur (buf));
- end = scm_to_size_t (scm_port_buffer_end (buf));
- if (end > scm_port_buffer_size (buf))
- scm_misc_error (NULL, "invalid port buffer ~a", scm_list_1 (buf));
-
- *cur_out = cur
- return end < cur ? 0 : end - cur
- }
- static inline size_t
- scm_port_buffer_can_put (SCM buf, size_t *end_out)
- {
- size_t end = scm_to_size_t (scm_port_buffer_end (buf));
- if (end > scm_port_buffer_size (buf))
- scm_misc_error (NULL, "invalid port buffer ~a", scm_list_1 (buf));
- *end_out = end
- return scm_port_buffer_size (buf) - end
- }
- static inline size_t
- scm_port_buffer_can_putback (SCM buf)
- {
- size_t cur = scm_to_size_t (scm_port_buffer_cur (buf));
- if (cur > scm_port_buffer_size (buf))
- scm_misc_error (NULL, "invalid port buffer ~a", scm_list_1 (buf));
- return cur
- }
- static inline void
- scm_port_buffer_did_take (SCM buf, size_t prev_cur, size_t count)
- {
- scm_port_buffer_set_cur (buf, SCM_I_MAKINUM (prev_cur + count))
- }
- static inline void
- scm_port_buffer_did_put (SCM buf, size_t prev_end, size_t count)
- {
- scm_port_buffer_set_end (buf, SCM_I_MAKINUM (prev_end + count))
- }
- static inline const uint8_t *
- scm_port_buffer_take_pointer (SCM buf, size_t cur)
- {
- signed char *ret = SCM_BYTEVECTOR_CONTENTS (scm_port_buffer_bytevector (buf));
- return ((uint8_t *) ret) + cur
- }
- static inline uint8_t *
- scm_port_buffer_put_pointer (SCM buf, size_t end)
- {
- signed char *ret = SCM_BYTEVECTOR_CONTENTS (scm_port_buffer_bytevector (buf));
- return ((uint8_t *) ret) + end
- }
- static inline size_t
- scm_port_buffer_take (SCM buf, uint8_t *dst, size_t count,
- size_t cur, size_t avail)
- {
- if (avail < count)
- count = avail
- if (dst)
- memcpy (dst, scm_port_buffer_take_pointer (buf, cur), count)
- scm_port_buffer_did_take (buf, cur, count)
- return count
- }
- static inline size_t
- scm_port_buffer_put (SCM buf, const uint8_t *src, size_t count,
- size_t end, size_t avail)
- {
- if (avail < count)
- count = avail
- if (src)
- memcpy (scm_port_buffer_put_pointer (buf, end), src, count)
- scm_port_buffer_did_put (buf, end, count)
- return count
- }
- static inline void
- scm_port_buffer_putback (SCM buf, const uint8_t *src, size_t count,
- size_t cur)
- {
- assert (count <= cur)
-
- cur -= count
- scm_port_buffer_set_cur (buf, scm_from_size_t (cur))
- memmove (SCM_BYTEVECTOR_CONTENTS (scm_port_buffer_bytevector (buf)) + cur,
- src, count)
- }
- struct scm_t_port
- {
-
- SCM file_name
- SCM position
-
- SCM read_buf
- SCM write_buf
- SCM write_buf_aux
-
- size_t read_buffering
-
- uint32_t refcount
-
- uint32_t rw_random : 1
- uint32_t at_stream_start_for_bom_read : 1
- uint32_t at_stream_start_for_bom_write : 1
-
- SCM encoding
- SCM conversion_strategy
-
- SCM precise_encoding
- iconv_t input_cd
- iconv_t output_cd
-
- SCM alist
- }
- SCM_INTERNAL void scm_port_acquire_iconv_descriptors (SCM port,
- iconv_t *input_cd,
- iconv_t *output_cd)
- SCM_INTERNAL void scm_port_release_iconv_descriptors (SCM port)
|