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/trunk/src/system/Basis/Implementation/Unix/posix-text-prim-io.sml
ViewVC logotype

Annotation of /sml/trunk/src/system/Basis/Implementation/Unix/posix-text-prim-io.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1381 - (view) (download)

1 : monnier 416 (* posix-text-prim-io.sml
2 :     *
3 :     * COPYRIGHT (c) 1995 AT&T Bell Laboratories.
4 :     *
5 :     * This implements the UNIX version of the OS specific text primitive
6 :     * IO structure. It is implemented by a trivial translation of the
7 :     * binary operations (see posix-bin-prim-io.sml).
8 :     *
9 :     *)
10 :    
11 :     local
12 :     structure String = StringImp
13 :     structure Int = IntImp
14 :     in
15 :     structure PosixTextPrimIO : sig
16 :    
17 :     include OS_PRIM_IO
18 :    
19 :     val stdIn : unit -> PrimIO.reader
20 :     val stdOut : unit -> PrimIO.writer
21 :     val stdErr : unit -> PrimIO.writer
22 :    
23 :     val strReader : string -> PrimIO.reader
24 :    
25 :     end = struct
26 :    
27 :     structure PF = Posix.FileSys
28 :     structure BinPrimIO = PosixBinPrimIO
29 :     structure PrimIO = TextPrimIO
30 :    
31 :     type file_desc = PF.file_desc
32 :    
33 :     val bufferSzB = 4096
34 :    
35 :     (* If Char.char is really Word8.word, then very efficient versions of
36 :     * translateIn and translateOut are possible:
37 :     *)
38 :     val translateIn : BinPrimIO.PrimIO.reader -> PrimIO.reader = InlineT.cast
39 :     val translateOut : BinPrimIO.PrimIO.writer -> PrimIO.writer = InlineT.cast
40 :    
41 :     fun openRd fname = translateIn(BinPrimIO.openRd fname)
42 :     fun openWr fname = translateOut(BinPrimIO.openWr fname)
43 :     fun openApp fname = translateOut(BinPrimIO.openApp fname)
44 :    
45 :     fun mkReader args = translateIn(BinPrimIO.mkReader args)
46 :     fun mkWriter args = translateOut(BinPrimIO.mkWriter args)
47 :    
48 :     fun stdIn () = mkReader{
49 :     fd = PF.stdin,
50 :     name = "<stdIn>",
51 :     initBlkMode = true (* Bug! Should check! *)
52 :     }
53 :    
54 :     fun stdOut () = mkWriter{
55 :     fd = PF.stdout,
56 :     name = "<stdOut>",
57 :     initBlkMode = true (* Bug! Should check! *),
58 :     appendMode = false (* Bug! Should check! *),
59 :     chunkSize = bufferSzB
60 :     }
61 :    
62 :     fun stdErr () = mkWriter{
63 :     fd = PF.stderr,
64 :     name = "<stdErr>",
65 :     initBlkMode = true, (* Bug! Should check! *)
66 :     appendMode = false, (* Bug! Should check! *)
67 :     chunkSize = bufferSzB
68 :     }
69 :    
70 :     fun strReader src = let
71 :     val pos = ref 0
72 :     val closed = ref false
73 :     fun checkClosed () = if !closed then raise IO.ClosedStream else ()
74 :     val len = String.size src
75 :     fun avail () = (len - !pos)
76 :     fun readV n = let
77 :     val p = !pos
78 :     val m = Int.min(n, len-p)
79 :     in
80 :     checkClosed ();
81 :     pos := p+m;
82 :     (** NOTE: could use unchecked operations here **)
83 :     String.substring (src, p, m)
84 :     end
85 : mblume 1381 fun readA asl = let
86 : monnier 416 val p = !pos
87 : mblume 1381 val (buf, i, n) = CharArraySlice.base asl
88 :     val m = Int.min(n, len-p)
89 :     in
90 :     checkClosed ();
91 :     pos := p+m;
92 :     CharArraySlice.copyVec
93 :     { src = CharVectorSlice.slice (src, p, SOME m),
94 :     dst = buf, di = i };
95 :     m
96 :     end
97 : monnier 416 fun getPos () = (checkClosed(); !pos)
98 :     in
99 :     PrimIO.RD{
100 :     name = "<string>",
101 :     chunkSize = len,
102 :     readVec = SOME(readV),
103 :     readArr = SOME(readA),
104 :     readVecNB = SOME(SOME o readV),
105 :     readArrNB = SOME(SOME o readA),
106 :     block = SOME(checkClosed),
107 :     canInput = SOME(fn () => (checkClosed(); true)),
108 :     avail = SOME o avail,
109 :     getPos = SOME getPos,
110 :     setPos = SOME(fn i => (
111 :     checkClosed();
112 :     if (i < 0) orelse (len < i)
113 :     then raise Subscript
114 :     else ();
115 :     pos := i)),
116 :     endPos = SOME(fn () => (checkClosed(); len)),
117 :     verifyPos = SOME getPos,
118 :     close = fn () => closed := true,
119 :     ioDesc = NONE
120 :     }
121 :     end
122 :    
123 :     end (* PosixTextPrimIO *)
124 :     end
125 :    

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