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

Annotation of /sml/branches/primop-branch/src/system/Basis/Implementation/Unix/posix-text-prim-io.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1372 - (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 :     fun readA {buf, i, sz} = let
86 :     val p = !pos
87 :     val m = (case sz
88 :     of NONE => Int.min(CharArray.length buf-i, len-p)
89 :     | (SOME n) => Int.min(n, len-p)
90 :     (* end case *))
91 :     in
92 :     checkClosed ();
93 :     pos := p+m;
94 : mblume 1350 CharArraySlice.copyVec
95 :     { src = CharVectorSlice.slice (src, p, SOME m),
96 :     dst = buf, di = i };
97 : monnier 416 m
98 :     end
99 :     fun getPos () = (checkClosed(); !pos)
100 :     in
101 :     PrimIO.RD{
102 :     name = "<string>",
103 :     chunkSize = len,
104 :     readVec = SOME(readV),
105 :     readArr = SOME(readA),
106 :     readVecNB = SOME(SOME o readV),
107 :     readArrNB = SOME(SOME o readA),
108 :     block = SOME(checkClosed),
109 :     canInput = SOME(fn () => (checkClosed(); true)),
110 :     avail = SOME o avail,
111 :     getPos = SOME getPos,
112 :     setPos = SOME(fn i => (
113 :     checkClosed();
114 :     if (i < 0) orelse (len < i)
115 :     then raise Subscript
116 :     else ();
117 :     pos := i)),
118 :     endPos = SOME(fn () => (checkClosed(); len)),
119 :     verifyPos = SOME getPos,
120 :     close = fn () => closed := true,
121 :     ioDesc = NONE
122 :     }
123 :     end
124 :    
125 :     end (* PosixTextPrimIO *)
126 :     end
127 :    

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