Home My Page Projects Code Snippets Project Openings SML/NJ
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files

SCM Repository

[smlnj] Annotation of /sml/branches/blume-private-devel/src/ml-nlffi-lib/Tests/readline/stream.sml
ViewVC logotype

Annotation of /sml/branches/blume-private-devel/src/ml-nlffi-lib/Tests/readline/stream.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1442 - (view) (download)

1 : mblume 1442 (* stream.sml
2 :     *
3 :     * Implementation of functional input stream (TextIO.StreamIO.instream)
4 :     * on top of readline library linked via NLFFI.
5 :     *
6 :     * Copyright (c) 2004 by Toyota Technological Institute at Chicago
7 :     *
8 :     * Author: Matthias Blume (blume@tti-c.org)
9 :     *)
10 : mblume 1441 structure ReadlineStream : sig
11 :    
12 :     val stream : TextIO.StreamIO.instream
13 :    
14 :     end = struct
15 :    
16 :     structure T = TextIO
17 :     structure S = T.StreamIO
18 :     structure P = TextPrimIO
19 :    
20 :     val prompt = RL.prompt "rl> "
21 :    
22 :     fun rl () = RL.rl prompt
23 :    
24 :     val closed = ref false
25 :    
26 :     fun check () =
27 :     if !closed then raise IO.ClosedStream
28 :     else ()
29 :    
30 :     val buffer = ref (CharVectorSlice.full "")
31 :    
32 :     fun getN n = let
33 :     val buf = !buffer
34 :     val sz = CharVectorSlice.length buf
35 :     in
36 :     if sz > 0 then
37 :     if sz > n then
38 :     (buffer := CharVectorSlice.subslice (buf, n, NONE);
39 :     CharVectorSlice.subslice (buf, 0, SOME n))
40 :     else
41 :     (buffer := CharVectorSlice.full "";
42 :     buf)
43 :     else
44 :     case rl () of
45 :     NONE => CharVectorSlice.full ""
46 :     | SOME buf => (buffer := CharVectorSlice.full (buf ^ "\n");
47 :     getN n)
48 :     end
49 :    
50 :     fun readVec n = (check (); CharVectorSlice.vector (getN n))
51 :     fun readArr asl = let
52 :     val _ = check ()
53 :     val (a, di, n) = CharArraySlice.base asl
54 :     val vsl = getN n
55 :     in
56 :     CharArraySlice.copyVec { src = vsl, dst = a, di = di };
57 :     CharVectorSlice.length vsl
58 :     end
59 :    
60 :     fun block () =
61 :     (check ();
62 :     if CharVectorSlice.length (!buffer) = 0 then
63 :     case rl () of
64 :     NONE => ()
65 :     | SOME buf => (buffer := CharVectorSlice.full (buf ^ "\n"))
66 :     else ())
67 :    
68 :     fun canInput () = (check (); CharVectorSlice.length (!buffer) > 0)
69 :    
70 :     val reader =
71 :     P.augmentReader
72 :     (P.RD { name = "<readlineStdIn>",
73 :     chunkSize = 4096,
74 :     readVec = SOME readVec,
75 :     readArr = SOME readArr,
76 :     readVecNB = NONE,
77 :     readArrNB = NONE,
78 :     block = SOME block,
79 :     canInput = SOME canInput,
80 :     avail = fn () => NONE,
81 :     getPos = NONE,
82 :     setPos = NONE,
83 :     endPos = NONE,
84 :     verifyPos = NONE,
85 :     close = fn () => closed := true,
86 :     ioDesc = NONE })
87 :    
88 :    
89 :     val stream = S.mkInstream (reader, "")
90 :     end

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0