1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-06-12 14:50:19 +02:00
guile/test-suite/standalone/test-scm-c-read.c
Andy Wingo f7027a8b88 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.
2016-04-11 22:23:47 +02:00

122 lines
2.9 KiB
C
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/* Copyright (C) 2008, 2014 Free Software Foundation, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 3 of
* the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
/* Exercise `scm_c_read ()' and the port type API. Verify assumptions that
can be made by port type implementations. */
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#undef NDEBUG
#include <libguile.h>
#include <assert.h>
/* Size of our port's internal buffer. */
#define PORT_BUFFER_SIZE 1024
struct custom_port
{
size_t pos;
size_t len;
char *buf;
};
/* Return a new port of type PORT_TYPE. */
static inline SCM
make_port (scm_t_bits port_type)
{
struct custom_port *stream = scm_gc_typed_calloc (struct custom_port);
stream->pos = 0;
stream->len = PORT_BUFFER_SIZE;
stream->buf = scm_gc_calloc (stream->len, "custom-port-buffer");
return scm_c_make_port (port_type, SCM_RDNG, (scm_t_bits) stream);
}
static size_t
custom_port_read (SCM port, SCM dst, size_t start, size_t count)
{
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 (SCM_BYTEVECTOR_CONTENTS (dst) + start,
stream->buf + stream->pos, to_copy);
stream->pos += to_copy;
return to_copy;
}
/* Return true (non-zero) if BUF contains only zeros. */
static inline int
zeroed_buffer_p (const char *buf, size_t len)
{
size_t i;
for (i = 0; i < len; i++)
if (buf[i] != 0)
return 0;
return 1;
}
/* Run the test. */
static void *
do_start (void *arg)
{
SCM port;
scm_t_bits port_type;
char buffer[PORT_BUFFER_SIZE + (PORT_BUFFER_SIZE / 2)];
size_t read, last_read;
port_type = scm_make_port_type ("custom-input-port", custom_port_read, NULL);
port = make_port (port_type);
read = 0;
do
{
last_read = scm_c_read (port, &buffer[read], 123);
assert (last_read <= 123);
assert (zeroed_buffer_p (&buffer[read], last_read));
read += last_read;
}
while (last_read > 0 && read < sizeof (buffer));
/* We shouldn't be able to read more than what's in PORT's buffer. */
assert (read == PORT_BUFFER_SIZE);
return NULL;
}
int
main (int argc, char *argv[])
{
scm_with_guile (do_start, NULL);
return 0;
}