unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
* char-ready? for soft ports
@ 2002-10-03 22:33 Neil Jerram
  2002-10-08 20:58 ` Marius Vollmer
  0 siblings, 1 reply; 5+ messages in thread
From: Neil Jerram @ 2002-10-03 22:33 UTC (permalink / raw)


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)))



_______________________________________________
Guile-devel mailing list
Guile-devel@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-devel


^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2002-10-09 19:35 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2002-10-03 22:33 char-ready? for soft ports Neil Jerram
2002-10-08 20:58 ` Marius Vollmer
2002-10-09 18:21   ` Neil Jerram
2002-10-09 19:23     ` Neil Jerram
2002-10-09 19:35     ` Marius Vollmer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).