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/cml/src/Win32/win32-text-prim-io.sml
ViewVC logotype

Annotation of /sml/trunk/src/cml/src/Win32/win32-text-prim-io.sml

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1354 - (view) (download)

1 : monnier 137 (* win32-text-prim-io.sml
2 :     *
3 :     * COPYRIGHT (c) 1998 Bell Labs, Lucent Technologies.
4 :     * COPYRIGHT (c) 1995 AT&T Bell Laboratories.
5 :     *
6 :     * This implements the Win32 version of the OS specific text primitive
7 :     * IO structure. It is implemented by a trivial translation of the
8 :     * binary operations (see win32-bin-prim-io.sml).
9 :     *)
10 :    
11 :     structure Win32TextPrimIO : sig
12 :    
13 :     include OS_PRIM_IO
14 :    
15 :     val stdIn : unit -> PrimIO.reader
16 :     val stdOut : unit -> PrimIO.writer
17 :     val stdErr : unit -> PrimIO.writer
18 :    
19 :     val strReader : string -> PrimIO.reader
20 :    
21 :     end = struct
22 :    
23 :     structure SV = SyncVar
24 :     structure BinPrimIO = Win32BinPrimIO
25 :     structure PrimIO = TextPrimIO
26 :    
27 :     structure W32FS = Win32.FileSys
28 :     structure W32IO = Win32.IO
29 :     structure W32G = Win32.General
30 :    
31 :     structure V = Word8Vector
32 :    
33 :     type file_desc = W32G.hndl
34 :    
35 :     val bufferSzB = 4096
36 :    
37 :     (* If Char.char is really Word8.word, then very efficient versions of
38 :     * translateIn and translateOut are possible:
39 :     *)
40 :     val translateIn : BinPrimIO.PrimIO.reader -> PrimIO.reader = Unsafe.cast
41 :     val translateOut : BinPrimIO.PrimIO.writer -> PrimIO.writer = Unsafe.cast
42 :    
43 :     fun openRd fname = translateIn(BinPrimIO.openRd fname)
44 :     fun openWr fname = translateOut(BinPrimIO.openWr fname)
45 :     fun openApp fname = translateOut(BinPrimIO.openApp fname)
46 :    
47 :     fun mkReader args = translateIn(BinPrimIO.mkReader args)
48 :     fun mkWriter args = translateOut(BinPrimIO.mkWriter args)
49 :    
50 :     fun stdIn () = let
51 :     val h = W32IO.getStdHandle(W32IO.STD_INPUT_HANDLE)
52 :     in
53 :     if W32G.isValidHandle h
54 :     then mkReader{fd = h, name = "<stdIn>"}
55 :     else raise OS.SysErr("Win32TextPrimIO: can't get stdin",NONE)
56 :     end
57 :    
58 :     fun stdOut () = let
59 :     val h = W32IO.getStdHandle(W32IO.STD_OUTPUT_HANDLE)
60 :     in
61 :     if W32G.isValidHandle h
62 :     then mkWriter{
63 :     fd = h,
64 :     name = "<stdOut>",
65 :     appendMode = true,
66 :     chunkSize = bufferSzB
67 :     }
68 :     else raise OS.SysErr("Win32TextPrimIO: can't get stdout",NONE)
69 :     end
70 :    
71 :     fun stdErr () = let
72 :     val h = W32IO.getStdHandle(W32IO.STD_ERROR_HANDLE)
73 :     in
74 :     if W32G.isValidHandle h
75 :     then mkWriter{
76 :     fd = h,
77 :     name = "<stdErr>",
78 :     appendMode = true,
79 :     chunkSize = bufferSzB
80 :     }
81 :     else raise OS.SysErr("Win32TextPrimIO: can't get stderr",NONE)
82 :     end
83 :    
84 :     fun strReader src = let
85 :     val lockMV = SV.mVarInit()
86 :     fun withLock f x = (
87 :     SV.mTake lockMV;
88 :     f x before SV.mPut(lockMV, ()))
89 :     handle ex => (SV.mPut(lockMV, ()); raise ex)
90 :     val pos = ref 0
91 :     val closed = ref false
92 :     fun checkClosed () = if !closed then raise IO.ClosedStream else ()
93 :     val len = String.size src
94 :     fun avail () = (len - !pos)
95 :     fun readV n = let
96 :     val p = !pos
97 :     val m = Int.min(n, len-p)
98 :     in
99 :     checkClosed ();
100 :     pos := p+m;
101 :     (** NOTE: could use unchecked operations here **)
102 :     String.substring (src, p, m)
103 :     end
104 :     fun readA {buf, i, sz} = let
105 :     val p = !pos
106 :     val m = (case sz
107 :     of NONE => Int.min(CharArray.length buf-i, len-p)
108 :     | (SOME n) => Int.min(n, len-p)
109 :     (* end case *))
110 :     in
111 :     checkClosed ();
112 :     pos := p+m;
113 : mblume 1354 CharArraySlice.copyVec { src = CharVectorSlice.slice
114 :     (src,p,SOME len),
115 :     dst = buf, di = i };
116 : monnier 137 m
117 :     end
118 :     fun getPos () = (checkClosed(); !pos)
119 :     in
120 :     PrimIO.RD{
121 :     name = "<string>",
122 :     chunkSize = len,
123 :     readVec = withLock readV,
124 :     readArr = withLock readA,
125 :     readVecEvt = withLock(CML.alwaysEvt o readV),
126 :     readArrEvt = withLock(CML.alwaysEvt o readA),
127 :     avail = SOME o avail,
128 :     getPos = SOME(withLock getPos),
129 :     setPos = SOME(withLock(fn i => (
130 :     checkClosed();
131 :     if (i < 0) orelse (len < i)
132 :     then raise Subscript
133 :     else ();
134 :     pos := i))),
135 :     endPos = SOME(withLock(fn () => (checkClosed(); len))),
136 :     verifyPos = SOME(withLock getPos),
137 :     close = withLock(fn () => closed := true),
138 :     ioDesc = NONE
139 :     }
140 :     end
141 :    
142 :     end; (* Win32TextPrimIO *)
143 :    

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