1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-06-26 05:00:28 +02:00

Port read/write functions take bytevectors

This will allow better Scheme integration for ports.

* libguile/ports.h (scm_t_port_buffer): Change "holder" member to be a
  bytevector defined to have "buf" as its starting point.
  (scm_t_ptob_descriptor): Change read and write functions to take
  bytevectors as arguments and to return the number of octets read or
  written.
  (scm_make_port_type): Adapt accordingly.
  (scm_c_read_bytes, scm_c_write_bytes): New functions that take
  bytevectors.
* libguile/ports.c (scm_make_port_type): Adapt to read/write function
  prototype change.
  (scm_c_make_port_buffer): Arrange to populate the "bytevector" field.
  (scm_i_read_bytes_unlocked): New function.
  (scm_i_read_unlocked): Use scm_i_read_bytes_unlocked.
  (scm_c_read_bytes_unlocked): New function.
  (scm_c_read_unlocked): Update comment, and always go through the
  buffer.
  (scm_c_read_bytes): New function.
  (scm_flush_unlocked): Use scm_i_write_unlocked instead of the port's
  write function.
  (scm_i_write_bytes_unlocked): New function.
  (scm_i_write_unlocked): Use scm_i_write_bytes_unlocked.
  (scm_c_write_bytes_unlocked): New function.
  (scm_c_write_unlocked): Always write through the buffer.
  (scm_c_write_bytes): New function.
  (scm_truncate_file): Remove unused variable.
  (void_port_read, void_port_write): Adapt to read/write prototype
  change.
* libguile/fports.c (fport_read, fport_write):
* libguile/r6rs-ports.c (bytevector_input_port_read)
  (custom_binary_input_port_read, bytevector_output_port_write)
  (custom_binary_output_port_write, transcoded_port_write)
  (transcoded_port_read): Adapt to read/write prototype
  change.
  (scm_get_bytevector_n, scm_get_bytevector_n_x)
  (scm_get_bytevector_all): Use scm_c_read_bytes.
  (scm_put_bytevector): Use scm_c_write_bytes.
* libguile/strports.c (string_port_read, string_port_write):
* libguile/vports.c (soft_port_write, soft_port_read): Adapt to
  read/write prototype change.
* test-suite/standalone/test-scm-c-read.c (custom_port_read): Fix for
  read API change.
This commit is contained in:
Andy Wingo 2016-04-11 18:40:03 +02:00
parent 55fb8f4e7e
commit f7027a8b88
7 changed files with 372 additions and 240 deletions

View file

@ -54,18 +54,20 @@ make_port (scm_t_bits port_type)
return scm_c_make_port (port_type, SCM_RDNG, (scm_t_bits) stream);
}
static void
custom_port_read (SCM port, scm_t_port_buffer *dst)
static size_t
custom_port_read (SCM port, SCM dst, size_t start, size_t count)
{
size_t to_copy = dst->size - dst->end;
size_t to_copy = count;
struct custom_port *stream = (void *) SCM_STREAM (port);
if (stream->pos + to_copy > stream->len)
to_copy = stream->len - stream->pos;
memcpy (dst->buf + dst->end, stream->buf + stream->pos, to_copy);
memcpy (SCM_BYTEVECTOR_CONTENTS (dst) + start,
stream->buf + stream->pos, to_copy);
stream->pos += to_copy;
dst->end += to_copy;
return to_copy;
}
/* Return true (non-zero) if BUF contains only zeros. */