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 1441 - (view) (download)

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

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