summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndy Wingo <wingo@pobox.com>2010-12-06 19:59:15 +0100
committerAndy Wingo <wingo@pobox.com>2010-12-06 19:59:15 +0100
commit67a72dc13c9d058730361449416ba88c4a6d5dcb (patch)
treeb53f36ff13bf4a0fd9ef5881a2e02befe0c464ad
parent4595600a08b7f01b9a4eb8f9d1732f1728989d81 (diff)
downloadguile-67a72dc13c9d058730361449416ba88c4a6d5dcb.tar.gz
scm_setvbuf doesn't throw away current buffers
* libguile/ports.c (scm_drain_input): Slight optimization. * libguile/fports.c (scm_setvbuf): If there is buffered output, flush it. If there is input, drain it, and then unread it after updating the buffers. Much more sensible than dropping it silently...
-rw-r--r--libguile/fports.c14
-rw-r--r--libguile/ports.c10
2 files changed, 21 insertions, 3 deletions
diff --git a/libguile/fports.c b/libguile/fports.c
index 4b190e15f..fdc8f467b 100644
--- a/libguile/fports.c
+++ b/libguile/fports.c
@@ -170,6 +170,7 @@ SCM_DEFINE (scm_setvbuf, "setvbuf", 2, 1, 0,
{
int cmode;
long csize;
+ SCM drained;
scm_t_port *pt;
port = SCM_COERCE_OUTPORT (port);
@@ -205,7 +206,14 @@ SCM_DEFINE (scm_setvbuf, "setvbuf", 2, 1, 0,
pt = SCM_PTAB_ENTRY (port);
- /* silently discards buffered and put-back chars. */
+ if (SCM_INPUT_PORT_P (port))
+ drained = scm_drain_input (port);
+ else
+ drained = scm_nullstr;
+
+ if (SCM_OUTPUT_PORT_P (port))
+ scm_flush (port);
+
if (pt->read_buf == pt->putback_buf)
{
pt->read_buf = pt->saved_read_buf;
@@ -219,6 +227,10 @@ SCM_DEFINE (scm_setvbuf, "setvbuf", 2, 1, 0,
scm_gc_free (pt->write_buf, pt->write_buf_size, "port buffer");
scm_fport_buffer_add (port, csize, csize);
+
+ if (scm_is_true (drained) && scm_c_string_length (drained))
+ scm_unread_string (drained, port);
+
return SCM_UNSPECIFIED;
}
#undef FUNC_NAME
diff --git a/libguile/ports.c b/libguile/ports.c
index ff40a3359..a9ba08e2e 100644
--- a/libguile/ports.c
+++ b/libguile/ports.c
@@ -347,8 +347,14 @@ SCM_DEFINE (scm_drain_input, "drain-input", 1, 0, 0,
if (pt->read_buf == pt->putback_buf)
count += pt->saved_read_end - pt->saved_read_pos;
- result = scm_i_make_string (count, &data);
- scm_take_from_input_buffers (port, data, count);
+ if (count)
+ {
+ result = scm_i_make_string (count, &data);
+ scm_take_from_input_buffers (port, data, count);
+ }
+ else
+ result = scm_nullstr;
+
return result;
}
#undef FUNC_NAME