shithub: femtolisp

Download patch

ref: 88d08edecc4c24d4ad0cca3d15ab01090a559de9
parent: 66c671bfeeb94ffaf3f475f30f6dd2c522d3fd2e
author: JeffBezanson <jeff.bezanson@gmail.com>
date: Tue Jul 21 23:50:57 EDT 2009

adding io.pos, io.readlines, read-all
fixing a small ios bug


--- a/femtolisp/iostream.c
+++ b/femtolisp/iostream.c
@@ -169,6 +169,16 @@
     return FL_T;
 }
 
+value_t fl_iopos(value_t *args, u_int32_t nargs)
+{
+    argcount("io.pos", nargs, 1);
+    ios_t *s = toiostream(args[0], "io.pos");
+    off_t res = ios_pos(s);
+    if (res == -1)
+        return FL_F;
+    return size_wrap((size_t)res);
+}
+
 static void do_ioprint(value_t *args, u_int32_t nargs, char *fname)
 {
     if (nargs < 2)
@@ -340,6 +350,7 @@
     { "io.close", fl_ioclose },
     { "io.eof?" , fl_ioeof },
     { "io.seek" , fl_ioseek },
+    { "io.pos",   fl_iopos },
     { "io.getc" , fl_iogetc },
     { "io.putc" , fl_ioputc },
     { "io.discardbuffer", fl_iopurge },
--- a/femtolisp/system.lsp
+++ b/femtolisp/system.lsp
@@ -503,6 +503,17 @@
 
 (define (io.readline s) (io.readuntil s #\x0a))
 
+; call f on a stream until the stream runs out of data
+(define (read-all-of f s)
+  (let loop ((lines ())
+	     (curr  (f s)))
+    (if (io.eof? s)
+	(reverse! lines)
+	(loop (cons curr lines) (f s)))))
+
+(define (io.readlines s) (read-all-of io.readline s))
+(define (read-all s) (read-all-of read s))
+
 ; vector functions ------------------------------------------------------------
 
 (define (list->vector l) (apply vector l))
--- a/femtolisp/todo
+++ b/femtolisp/todo
@@ -878,10 +878,10 @@
  io.trunc
  io.read!     - destructively take data
 *io.tostring!
- io.readlines
+*io.readlines
  io.readall
- print-to-string
- princ-to-string
+*print-to-string
+*princ-to-string
 
 
  path.combine
--- a/llt/ios.c
+++ b/llt/ios.c
@@ -306,6 +306,7 @@
     size_t space = s->size - s->bpos;
     if (s->state == bst_wr)
         return space;
+    s->state = bst_rd;
     if (space >= n || s->bm == bm_mem || s->fd == -1)
         return space;
     if (s->maxsize < s->bpos+n) {