1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-04-30 03:40:34 +02:00

Refactor to rw_random / rw_active port flags

* libguile/fports.c (fport_flush, fport_end_input): Move rw_active
  handling to ports.c.
* libguile/ioext.c (scm_redirect_port): Use scm_flush_unlocked instead
  of calling the flush function directly.
* libguile/ports.c (scm_c_make_port_with_encoding): Ports default to
  "rw_random" mode when they have a seek function.
  (scm_c_read_unlocked, scm_i_unget_bytes_unlocked)
  (scm_slow_get_byte_or_eof_unlocked)
  (scm_slow_peek_byte_or_eof_unlocked): Flush write buffer and set
  rw_active always in the same way, and only if rw_random is true.
  (scm_end_input_unlocked, scm_flush_unlocked): Clear rw_active here
  unconditionally.
  (scm_c_write_unlocked): Flush read buffer and set rw_active always in
  the same way, but only if rw_random is true.
  (scm_c_write, scm_lfwrite): Whitespace fixes.
  (scm_lfwrite_substr): Don't flush read buffer; lower-level code will
  do this.
  (scm_truncate_file): Use scm_flush_unlocked instead of calling the
  flush function directly.
* libguile/r6rs-ports.c (transcoded_port_flush): Don't muck with
  rw_active.
* libguile/read.c (scm_i_scan_for_encoding): Flush write buffer if
  needed in same way as ports.c.
* libguile/strports.c (st_end_input): Don't muck with rw_active.
  (scm_mkstrport): rw_random defaults to 1 now.
This commit is contained in:
Andy Wingo 2016-04-04 11:03:52 +02:00
parent b538a96f92
commit 2caae477c5
6 changed files with 40 additions and 44 deletions

View file

@ -801,7 +801,6 @@ fport_flush (SCM port)
scm_syserror ("scm_flush");
pt->write_pos = pt->write_buf;
pt->rw_active = SCM_PORT_NEITHER;
}
/* clear the read buffer and adjust the file position for unread bytes. */
@ -821,7 +820,6 @@ fport_end_input (SCM port, int offset)
if (lseek (fp->fdes, -offset, SEEK_CUR) == -1)
scm_syserror ("fport_end_input");
}
pt->rw_active = SCM_PORT_NEITHER;
}
static void

View file

@ -88,11 +88,10 @@ SCM_DEFINE (scm_redirect_port, "redirect-port", 2, 0, 0,
{
scm_t_port *pt = SCM_PTAB_ENTRY (new);
scm_t_port *old_pt = SCM_PTAB_ENTRY (old);
scm_t_ptob_descriptor *ptob = SCM_PORT_DESCRIPTOR (new);
/* must flush to old fdes. */
if (pt->rw_active == SCM_PORT_WRITE)
ptob->flush (new);
scm_flush_unlocked (new);
else if (pt->rw_active == SCM_PORT_READ)
scm_end_input_unlocked (new);
ans = dup2 (oldfd, newfd);

View file

@ -680,6 +680,9 @@ scm_c_make_port_with_encoding (scm_t_bits tag, unsigned long mode_bits,
entry->internal = pti;
entry->file_name = SCM_BOOL_F;
/* By default, any port type with a seek function has random-access
ports. */
entry->rw_random = ptob->seek != NULL;
entry->rw_active = SCM_PORT_NEITHER;
entry->port = ret;
entry->stream = stream;
@ -1455,11 +1458,13 @@ scm_c_read_unlocked (SCM port, void *buffer, size_t size)
pt = SCM_PTAB_ENTRY (port);
pti = SCM_PORT_GET_INTERNAL (port);
if (pt->rw_active == SCM_PORT_WRITE)
SCM_PORT_DESCRIPTOR (port)->flush (port);
if (pt->rw_random)
pt->rw_active = SCM_PORT_READ;
{
if (pt->rw_active == SCM_PORT_WRITE)
scm_flush_unlocked (port);
pt->rw_active = SCM_PORT_READ;
}
/* Take bytes first from the port's read buffer. */
if (pt->read_pos < pt->read_end)
@ -1984,6 +1989,13 @@ scm_i_unget_bytes_unlocked (const unsigned char *buf, size_t len, SCM port)
scm_t_port *pt = SCM_PTAB_ENTRY (port);
size_t old_len, new_len;
if (pt->rw_random)
{
if (pt->rw_active == SCM_PORT_WRITE)
scm_flush (port);
pt->rw_active = SCM_PORT_READ;
}
scm_i_clear_pending_eof (port);
if (pt->read_buf != pt->putback_buf)
@ -2053,12 +2065,6 @@ scm_i_unget_bytes_unlocked (const unsigned char *buf, size_t len, SCM port)
/* Move read_pos back and copy the bytes there. */
pt->read_pos -= len;
memcpy (pt->read_buf + (pt->read_pos - pt->read_buf), buf, len);
if (pt->rw_active == SCM_PORT_WRITE)
scm_flush (port);
if (pt->rw_random)
pt->rw_active = SCM_PORT_READ;
}
#undef FUNC_NAME
@ -2460,11 +2466,12 @@ scm_slow_get_byte_or_eof_unlocked (SCM port)
{
scm_t_port *pt = SCM_PTAB_ENTRY (port);
if (pt->rw_active == SCM_PORT_WRITE)
scm_flush_unlocked (port);
if (pt->rw_random)
pt->rw_active = SCM_PORT_READ;
{
if (pt->rw_active == SCM_PORT_WRITE)
scm_flush_unlocked (port);
pt->rw_active = SCM_PORT_READ;
}
if (pt->read_pos >= pt->read_end)
{
@ -2481,11 +2488,12 @@ scm_slow_peek_byte_or_eof_unlocked (SCM port)
{
scm_t_port *pt = SCM_PTAB_ENTRY (port);
if (pt->rw_active == SCM_PORT_WRITE)
scm_flush_unlocked (port);
if (pt->rw_random)
pt->rw_active = SCM_PORT_READ;
{
if (pt->rw_active == SCM_PORT_WRITE)
scm_flush_unlocked (port);
pt->rw_active = SCM_PORT_READ;
}
if (pt->read_pos >= pt->read_end)
{
@ -2594,6 +2602,7 @@ scm_end_input_unlocked (SCM port)
offset = 0;
SCM_PORT_DESCRIPTOR (port)->end_input (port, offset);
pt->rw_active = SCM_PORT_NEITHER;
}
void
@ -2633,6 +2642,7 @@ void
scm_flush_unlocked (SCM port)
{
SCM_PORT_DESCRIPTOR (port)->flush (port);
SCM_PTAB_ENTRY (port)->rw_active = SCM_PORT_NEITHER;
}
void
@ -2700,13 +2710,14 @@ scm_c_write_unlocked (SCM port, const void *ptr, size_t size)
pt = SCM_PTAB_ENTRY (port);
ptob = SCM_PORT_DESCRIPTOR (port);
if (pt->rw_active == SCM_PORT_READ)
scm_end_input_unlocked (port);
if (pt->rw_random)
{
if (pt->rw_active == SCM_PORT_READ)
scm_end_input_unlocked (port);
pt->rw_active = SCM_PORT_WRITE;
}
ptob->write (port, ptr, size);
if (pt->rw_random)
pt->rw_active = SCM_PORT_WRITE;
}
#undef FUNC_NAME
@ -2718,7 +2729,6 @@ scm_c_write (SCM port, const void *ptr, size_t size)
scm_c_write_unlocked (port, ptr, size);
if (lock)
scm_i_pthread_mutex_unlock (lock);
}
/* scm_lfwrite
@ -2749,25 +2759,16 @@ scm_lfwrite (const char *ptr, size_t size, SCM port)
scm_lfwrite_unlocked (ptr, size, port);
if (lock)
scm_i_pthread_mutex_unlock (lock);
}
/* Write STR to PORT from START inclusive to END exclusive. */
void
scm_lfwrite_substr (SCM str, size_t start, size_t end, SCM port)
{
scm_t_port *pt = SCM_PTAB_ENTRY (port);
if (pt->rw_active == SCM_PORT_READ)
scm_end_input_unlocked (port);
if (end == (size_t) -1)
end = scm_i_string_length (str);
scm_i_display_substring (str, start, end, port);
if (pt->rw_random)
pt->rw_active = SCM_PORT_WRITE;
}
@ -2972,7 +2973,7 @@ SCM_DEFINE (scm_truncate_file, "truncate-file", 1, 1, 0,
if (pt->rw_active == SCM_PORT_READ)
scm_end_input_unlocked (object);
else if (pt->rw_active == SCM_PORT_WRITE)
ptob->flush (object);
scm_flush_unlocked (object);
ptob->truncate (object, c_length);
rv = 0;

View file

@ -1233,7 +1233,6 @@ transcoded_port_flush (SCM port)
scm_c_write_unlocked (binary_port, c_port->write_buf, count);
c_port->write_pos = c_port->write_buf;
c_port->rw_active = SCM_PORT_NEITHER;
if (SCM_OPOUTPORTP (binary_port))
scm_force_output (binary_port);

View file

@ -2065,11 +2065,12 @@ scm_i_scan_for_encoding (SCM port)
pt = SCM_PTAB_ENTRY (port);
if (pt->rw_active == SCM_PORT_WRITE)
scm_flush_unlocked (port);
if (pt->rw_random)
pt->rw_active = SCM_PORT_READ;
{
if (pt->rw_active == SCM_PORT_WRITE)
scm_flush_unlocked (port);
pt->rw_active = SCM_PORT_READ;
}
if (pt->read_pos == pt->read_end)
{

View file

@ -154,7 +154,6 @@ st_end_input (SCM port, int offset)
scm_misc_error ("st_end_input", "negative position", SCM_EOL);
pt->write_pos = (unsigned char *) (pt->read_pos = pt->read_pos - offset);
pt->rw_active = SCM_PORT_NEITHER;
}
static scm_t_off
@ -304,7 +303,6 @@ scm_mkstrport (SCM pos, SCM str, long modes, const char *caller)
pt->read_buf_size = read_buf_size;
pt->write_buf_size = num_bytes;
pt->write_end = pt->read_end = pt->read_buf + pt->read_buf_size;
pt->rw_random = 1;
return z;
}