guile-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

char-ready? for soft ports


From: Neil Jerram
Subject: char-ready? for soft ports
Date: 03 Oct 2002 23:33:05 +0100
User-agent: Gnus/5.0808 (Gnus v5.8.8) Emacs/20.7

Currently, char-ready? always returns #t for a soft port.

The patch below extends make-soft-port so that its vector argument can have 6
as well as 5 elements.  If present and not #f, the 6th element is used
as the input-waiting thunk for the soft port - i.e. it should return
the number of characters available for reading without blocking.  The
patch to (ice-9 buffered-input) gives an example of how to define such
a thunk.

Thoughts?  Is there a reason I've missed why soft ports should not
support char-ready? completely?  Should I commit this?  (Is the use of
SCM_INUM in sf_input_waiting safe?)

Regards,
        Neil

Index: libguile/vports.c
===================================================================
RCS file: /cvsroot/guile/guile/guile-core/libguile/vports.c,v
retrieving revision 1.57
diff -u -r1.57 vports.c
--- libguile/vports.c   15 Aug 2002 21:17:21 -0000      1.57
+++ libguile/vports.c   3 Oct 2002 22:27:21 -0000
@@ -139,6 +139,22 @@
 }
 
 
+static int 
+sf_input_waiting (SCM port)
+{
+  SCM p = SCM_PACK (SCM_STREAM (port));
+  if (SCM_VECTOR_LENGTH (p) >= 6)
+    {
+      SCM f = SCM_VELTS (p)[5];
+      if (SCM_NFALSEP (f))
+       return SCM_INUM (scm_call_0 (f));
+    }
+  /* Default is such that char-ready? for soft ports returns #t, as it
+     did before this extension was implemented. */
+  return 1;
+}
+
+
 
 SCM_DEFINE (scm_make_soft_port, "make-soft-port", 2, 0, 0,
            (SCM pv, SCM modes),
@@ -185,9 +201,13 @@
            "@end lisp")
 #define FUNC_NAME s_scm_make_soft_port
 {
+  int vlen;
   scm_t_port *pt;
   SCM z;
-  SCM_VALIDATE_VECTOR_LEN (1, pv,5);
+
+  SCM_VALIDATE_VECTOR (1, pv);
+  vlen = SCM_VECTOR_LENGTH (pv);
+  SCM_ASSERT ((vlen == 5) || (vlen == 6), pv, 1, FUNC_NAME);
   SCM_VALIDATE_STRING (2, modes);
   
   SCM_DEFER_INTS;
@@ -211,6 +231,7 @@
   scm_set_port_mark (tc, scm_markstream);
   scm_set_port_flush (tc, sf_flush);
   scm_set_port_close (tc, sf_close);
+  scm_set_port_input_waiting (tc, sf_input_waiting);
 
   return tc;
 }

Index: ice-9/buffered-input.scm
===================================================================
RCS file: /cvsroot/guile/guile/guile-core/ice-9/buffered-input.scm,v
retrieving revision 1.4
diff -u -r1.4 buffered-input.scm
--- ice-9/buffered-input.scm    3 Jun 2001 23:29:45 -0000       1.4
+++ ice-9/buffered-input.scm    3 Oct 2002 22:27:10 -0000
@@ -105,8 +105,15 @@
                     (if (not (char-whitespace? res))
                         (set! (buffered-input-continuation? port) #t))
                    res)))))
+            (input-waiting
+             (lambda ()
+               ;(write read-string)
+               ;(newline)
+               (if (eof-object? read-string)
+                   1
+                   (- (string-length read-string) string-index))))
              (port #f))
-      (set! port (make-soft-port (vector #f #f #f get-character #f) "r"))
+      (set! port (make-soft-port (vector #f #f #f get-character #f 
input-waiting) "r"))
       (set! (buffered-input-continuation? port) #f)
       port)))





reply via email to

[Prev in Thread] Current Thread [Next in Thread]