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

* Re: char-ready? for soft ports
  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
  0 siblings, 1 reply; 5+ messages in thread
From: Marius Vollmer @ 2002-10-08 20:58 UTC (permalink / raw)
  Cc: Guile Development

Neil Jerram <neil@ossau.uklinux.net> writes:

> Thoughts?  Is there a reason I've missed why soft ports should not
> support char-ready? completely?

I can't think of any.

> Should I commit this?

Yes, please.

> (Is the use of SCM_INUM in sf_input_waiting safe?)

I don't think so.  Using scm_uint2num or scm_int2num should not be a
significant overhead.

-- 
GPG: D5D4E405 - 2F9B BCCC 8527 692A 04E3  331E FAF8 226A D5D4 E405


_______________________________________________
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

* Re: char-ready? for soft ports
  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
  0 siblings, 2 replies; 5+ messages in thread
From: Neil Jerram @ 2002-10-09 18:21 UTC (permalink / raw)
  Cc: Guile Development

>>>>> "Marius" == Marius Vollmer <mvo@zagadka.ping.de> writes:

    Marius> Neil Jerram <neil@ossau.uklinux.net> writes:

    >> (Is the use of SCM_INUM in sf_input_waiting safe?)

    Marius> I don't think so.  Using scm_uint2num or scm_int2num
    Marius> should not be a significant overhead.

That's the wrong way round, though.  I need to convert a Scheme value,
which is expected but not guaranteed to be an integer >=0, to a C int.

        Neil



_______________________________________________
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

* Re: char-ready? for soft ports
  2002-10-09 18:21   ` Neil Jerram
@ 2002-10-09 19:23     ` Neil Jerram
  2002-10-09 19:35     ` Marius Vollmer
  1 sibling, 0 replies; 5+ messages in thread
From: Neil Jerram @ 2002-10-09 19:23 UTC (permalink / raw)
  Cc: Guile Development

>>>>> "Neil" == Neil Jerram <neil@ossau.uklinux.net> writes:

>>>>> "Marius" == Marius Vollmer <mvo@zagadka.ping.de> writes:
    Marius> Neil Jerram <neil@ossau.uklinux.net> writes:

    >>> (Is the use of SCM_INUM in sf_input_waiting safe?)

    Marius> I don't think so.  Using scm_uint2num or scm_int2num
    Marius> should not be a significant overhead.

    Neil> That's the wrong way round, though.  I need to convert a
    Neil> Scheme value, which is expected but not guaranteed to be an
    Neil> integer >=0, to a C int.

OK, I see now that I need scm_num2int here.

        Neil






_______________________________________________
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

* Re: char-ready? for soft ports
  2002-10-09 18:21   ` Neil Jerram
  2002-10-09 19:23     ` Neil Jerram
@ 2002-10-09 19:35     ` Marius Vollmer
  1 sibling, 0 replies; 5+ messages in thread
From: Marius Vollmer @ 2002-10-09 19:35 UTC (permalink / raw)
  Cc: Guile Development

Neil Jerram <neil@ossau.uklinux.net> writes:

> That's the wrong way round, though.  I need to convert a Scheme value,
> which is expected but not guaranteed to be an integer >=0, to a C int.

Right.  It's scm_num2int, then. :-)

-- 
GPG: D5D4E405 - 2F9B BCCC 8527 692A 04E3  331E FAF8 226A D5D4 E405


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