1(*
2    Title:      Rebuild the basis library: IO
3    Copyright   David C.J. Matthews 2016
4
5    This library is free software; you can redistribute it and/or
6    modify it under the terms of the GNU Lesser General Public
7    License version 2.1 as published by the Free Software Foundation.
8    
9    This library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13    
14    You should have received a copy of the GNU Lesser General Public
15    License along with this library; if not, write to the Free Software
16    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17*)
18
19(* IO *)
20(* This is much simpler if we ignore PrimIO. *)
21
22useBasis "STREAM_IO.sml";
23useBasis "IMPERATIVE_IO.sml";
24
25signature TEXT_STREAM_IO =
26sig
27    include STREAM_IO
28    where type vector = CharVector.vector
29    where type elem = Char.char
30
31    val inputLine : instream -> (string * instream) option
32    val outputSubstr : outstream * Substring.substring -> unit
33end;
34
35signature TEXT_IO =
36sig
37    (* include IMPERATIVE_IO *)
38    structure StreamIO : TEXT_STREAM_IO
39        where type reader = TextPrimIO.reader
40        where type writer = TextPrimIO.writer
41        where type pos = TextPrimIO.pos
42
43    type vector = StreamIO.vector
44    type elem = StreamIO.elem
45
46    type instream
47    type outstream
48
49    val input : instream -> vector
50    val input1 : instream -> elem option
51    val inputN : instream * int -> vector
52    val inputAll : instream -> vector
53    val canInput : instream * int -> int option
54    val lookahead : instream -> elem option
55    val closeIn : instream -> unit
56    val endOfStream : instream -> bool
57    val output : outstream * vector -> unit
58    val output1 : outstream * elem -> unit
59    val flushOut : outstream -> unit
60    val closeOut : outstream -> unit
61    val mkInstream : StreamIO.instream -> instream
62    val getInstream : instream -> StreamIO.instream
63    val setInstream : instream * StreamIO.instream -> unit
64    val mkOutstream : StreamIO.outstream -> outstream
65    val getOutstream : outstream -> StreamIO.outstream
66    val setOutstream : outstream * StreamIO.outstream -> unit
67    val getPosOut : outstream -> StreamIO.out_pos
68    val setPosOut : outstream * StreamIO.out_pos -> unit
69    (* End of include IMPERATIVE_IO *)
70
71    val inputLine : instream -> string option
72    val outputSubstr : outstream * Substring.substring -> unit
73    val openIn  : string -> instream
74    val openOut : string -> outstream
75    val openAppend : string -> outstream
76    val openString : string -> instream
77
78    val stdIn  : instream
79    val stdOut : outstream
80    val stdErr : outstream
81
82    val print : string -> unit
83    val scanStream : ((Char.char, StreamIO.instream) StringCvt.reader
84                      -> ('a, StreamIO.instream) StringCvt.reader)
85                      -> instream -> 'a option
86end;
87
88structure TextIO: TEXT_IO =
89struct
90    open TextIO
91    val canInput =
92        fn (s, n) => Option.map FixedInt.toLarge (canInput(s, FixedInt.fromLarge n))
93    val inputN =
94        fn (s, n) => inputN(s, FixedInt.fromLarge n)
95    structure StreamIO =
96    struct
97        open StreamIO
98        val canInput =
99            fn (s, n) => Option.map FixedInt.toLarge (canInput(s, FixedInt.fromLarge n))
100        val inputN =
101            fn (s, n) => inputN(s, FixedInt.fromLarge n)
102    end
103end;
104
105signature BIN_IO =
106sig
107    include IMPERATIVE_IO
108       where type StreamIO.vector = Word8Vector.vector
109       where type StreamIO.elem = Word8.word
110       where type StreamIO.reader = BinPrimIO.reader
111       where type StreamIO.writer = BinPrimIO.writer
112       where type StreamIO.pos = BinPrimIO.pos
113
114    val openIn  : string -> instream
115    val openOut : string -> outstream
116    val openAppend : string -> outstream
117end;
118
119structure BinIO: BIN_IO =
120struct
121    open BinIO
122    val canInput =
123        fn (s, n) => Option.map FixedInt.toLarge (canInput(s, FixedInt.fromLarge n))
124    val inputN =
125        fn (s, n) => inputN(s, FixedInt.fromLarge n)
126    structure StreamIO =
127    struct
128        open StreamIO
129        val canInput =
130            fn (s, n) => Option.map FixedInt.toLarge (canInput(s, FixedInt.fromLarge n))
131        val inputN =
132            fn (s, n) => inputN(s, FixedInt.fromLarge n)
133    end
134end;
135
136