]>
Commit | Line | Data |
---|---|---|
1eee94d3 GM |
1 | (* TextIO.mod implement the ISO TextIO specification. |
2 | ||
a945c346 | 3 | Copyright (C) 2008-2024 Free Software Foundation, Inc. |
1eee94d3 GM |
4 | Contributed by Gaius Mulley <gaius.mulley@southwales.ac.uk>. |
5 | ||
6 | This file is part of GNU Modula-2. | |
7 | ||
8 | GNU Modula-2 is free software; you can redistribute it and/or modify | |
9 | it under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 3, or (at your option) | |
11 | any later version. | |
12 | ||
13 | GNU Modula-2 is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | General Public License for more details. | |
17 | ||
18 | Under Section 7 of GPL version 3, you are granted additional | |
19 | permissions described in the GCC Runtime Library Exception, version | |
20 | 3.1, as published by the Free Software Foundation. | |
21 | ||
22 | You should have received a copy of the GNU General Public License and | |
23 | a copy of the GCC Runtime Library Exception along with this program; | |
24 | see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
25 | <http://www.gnu.org/licenses/>. *) | |
26 | ||
27 | IMPLEMENTATION MODULE TextIO ; | |
28 | ||
29 | ||
01cca857 | 30 | IMPORT IOConsts, CharClass, ASCII ; |
1eee94d3 GM |
31 | FROM SYSTEM IMPORT ADR ; |
32 | FROM FIO IMPORT FlushOutErr ; | |
434dade5 | 33 | FROM libc IMPORT printf ; |
509eef93 | 34 | FROM TextUtil IMPORT SkipSpaces, EofOrEoln, CharAvailable ; |
434dade5 GM |
35 | |
36 | ||
37 | CONST | |
38 | DebugState = FALSE ; | |
39 | ||
1eee94d3 | 40 | |
434dade5 GM |
41 | (* |
42 | DumpState | |
43 | *) | |
44 | ||
45 | PROCEDURE DumpState (cid: IOChan.ChanId) ; | |
46 | BEGIN | |
47 | printf ("cid = %d, ", cid) ; | |
48 | CASE IOChan.ReadResult (cid) OF | |
49 | ||
50 | IOConsts.notKnown: printf ('notKnown') | | |
51 | IOConsts.allRight: printf ('allRight') | | |
52 | IOConsts.outOfRange: printf ('outOfRange') | | |
53 | IOConsts.wrongFormat: printf ('wrongFormat') | | |
54 | IOConsts.endOfLine: printf ('endOfLine') | | |
55 | IOConsts.endOfInput: printf ('endOfInput') | |
56 | ||
57 | END ; | |
58 | printf ("\n") | |
59 | END DumpState ; | |
1eee94d3 GM |
60 | |
61 | ||
62 | (* | |
434dade5 | 63 | SetNul - assigns the result in cid. |
1eee94d3 GM |
64 | If s is empty then leave as endOfInput |
65 | or endOfLine | |
66 | If s is not empty then assign allRight | |
67 | If range and i exceeds, h, then assign outOfRange | |
68 | *) | |
69 | ||
434dade5 | 70 | PROCEDURE SetNul (cid: IOChan.ChanId; i: CARDINAL; |
3af2af15 | 71 | VAR s: ARRAY OF CHAR; range: BOOLEAN) ; |
1eee94d3 | 72 | BEGIN |
434dade5 GM |
73 | IF DebugState |
74 | THEN | |
75 | DumpState (cid) | |
76 | END ; | |
1eee94d3 GM |
77 | IF i<=HIGH(s) |
78 | THEN | |
434dade5 | 79 | s[i] := ASCII.nul |
1eee94d3 GM |
80 | ELSIF range |
81 | THEN | |
434dade5 | 82 | IOChan.SetReadResult (cid, IOConsts.outOfRange) |
1eee94d3 | 83 | END |
434dade5 | 84 | END SetNul ; |
1eee94d3 GM |
85 | |
86 | ||
87 | PROCEDURE ReadChar (cid: IOChan.ChanId; VAR ch: CHAR); | |
88 | (* If possible, removes a character from the input stream | |
89 | cid and assigns the corresponding value to ch. The | |
90 | read result is set to the value allRight, endOfLine, or | |
91 | endOfInput. | |
92 | *) | |
93 | VAR | |
94 | res: IOConsts.ReadResults ; | |
95 | BEGIN | |
96 | FlushOutErr ; | |
434dade5 | 97 | IF CharAvailable (cid) |
1eee94d3 | 98 | THEN |
434dade5 GM |
99 | IOChan.Look (cid, ch, res) ; |
100 | IF res = IOConsts.allRight | |
1eee94d3 | 101 | THEN |
434dade5 | 102 | IOChan.Skip (cid) |
1eee94d3 GM |
103 | END |
104 | END | |
105 | END ReadChar ; | |
106 | ||
434dade5 | 107 | |
1eee94d3 GM |
108 | PROCEDURE ReadRestLine (cid: IOChan.ChanId; VAR s: ARRAY OF CHAR); |
109 | (* Removes any remaining characters from the input stream | |
110 | cid before the next line mark, copying to s as many as | |
111 | can be accommodated as a string value. The read result is | |
112 | set to the value allRight, outOfRange, endOfLine, or | |
113 | endOfInput. | |
114 | *) | |
115 | VAR | |
116 | i, h : CARDINAL ; | |
117 | finished: BOOLEAN ; | |
118 | BEGIN | |
119 | h := HIGH(s) ; | |
120 | i := 0 ; | |
121 | finished := FALSE ; | |
434dade5 GM |
122 | WHILE (i<=h) AND CharAvailable (cid) AND (NOT finished) DO |
123 | ReadChar (cid, s[i]) ; | |
124 | IF EofOrEoln (cid) | |
1eee94d3 | 125 | THEN |
1eee94d3 | 126 | finished := TRUE |
434dade5 GM |
127 | ELSE |
128 | INC (i) | |
1eee94d3 GM |
129 | END |
130 | END ; | |
434dade5 GM |
131 | WHILE CharAvailable (cid) DO |
132 | IOChan.Skip (cid) | |
1eee94d3 | 133 | END ; |
434dade5 | 134 | SetNul (cid, i, s, TRUE) |
1eee94d3 GM |
135 | END ReadRestLine ; |
136 | ||
434dade5 | 137 | |
1eee94d3 GM |
138 | PROCEDURE ReadString (cid: IOChan.ChanId; VAR s: ARRAY OF CHAR); |
139 | (* Removes only those characters from the input stream cid | |
140 | before the next line mark that can be accommodated in s | |
141 | as a string value, and copies them to s. The read result | |
142 | is set to the value allRight, endOfLine, or endOfInput. | |
143 | *) | |
144 | VAR | |
145 | i, h : CARDINAL ; | |
146 | finished: BOOLEAN ; | |
147 | BEGIN | |
434dade5 | 148 | h := HIGH (s) ; |
1eee94d3 GM |
149 | i := 0 ; |
150 | finished := FALSE ; | |
434dade5 GM |
151 | WHILE (i<=h) AND CharAvailable (cid) AND (NOT finished) DO |
152 | ReadChar (cid, s[i]) ; | |
153 | IF EofOrEoln (cid) | |
1eee94d3 | 154 | THEN |
1eee94d3 | 155 | finished := TRUE |
434dade5 GM |
156 | ELSE |
157 | INC (i) | |
1eee94d3 GM |
158 | END |
159 | END ; | |
434dade5 | 160 | SetNul (cid, i, s, FALSE) |
1eee94d3 GM |
161 | END ReadString ; |
162 | ||
163 | ||
1eee94d3 GM |
164 | PROCEDURE ReadToken (cid: IOChan.ChanId; VAR s: ARRAY OF CHAR); |
165 | (* Skips leading spaces, and then removes characters from | |
166 | the input stream cid before the next space or line mark, | |
167 | copying to s as many as can be accommodated as a string | |
168 | value. The read result is set to the value allRight, | |
169 | outOfRange, endOfLine, or endOfInput. | |
170 | *) | |
171 | VAR | |
172 | i, h: CARDINAL ; | |
173 | BEGIN | |
434dade5 GM |
174 | SkipSpaces (cid) ; |
175 | h := HIGH (s) ; | |
1eee94d3 | 176 | i := 0 ; |
434dade5 GM |
177 | WHILE (i<=h) AND CharAvailable (cid) DO |
178 | ReadChar (cid, s[i]) ; | |
179 | IF (s[i]=ASCII.nul) OR CharClass.IsWhiteSpace (s[i]) | |
1eee94d3 | 180 | THEN |
434dade5 | 181 | SetNul (cid, i, s, TRUE) ; |
1eee94d3 GM |
182 | RETURN |
183 | END ; | |
434dade5 | 184 | INC (i) |
1eee94d3 | 185 | END ; |
434dade5 | 186 | SetNul (cid, i, s, TRUE) |
1eee94d3 GM |
187 | END ReadToken ; |
188 | ||
189 | (* The following procedure reads past the next line mark *) | |
190 | ||
191 | PROCEDURE SkipLine (cid: IOChan.ChanId); | |
192 | (* Removes successive items from the input stream cid up | |
193 | to and including the next line mark, or until the end | |
194 | of input is reached. The read result is set to the | |
195 | value allRight, or endOfInput. | |
196 | *) | |
197 | VAR | |
198 | ch : CHAR ; | |
199 | res: IOConsts.ReadResults ; | |
200 | BEGIN | |
434dade5 GM |
201 | IOChan.Look (cid, ch, res) ; |
202 | WHILE res = IOConsts.allRight DO | |
203 | IOChan.SkipLook (cid, ch, res) | |
1eee94d3 | 204 | END ; |
434dade5 | 205 | IF res = IOConsts.endOfLine |
1eee94d3 | 206 | THEN |
434dade5 GM |
207 | IOChan.Skip (cid) ; |
208 | IOChan.SetReadResult (cid, IOConsts.allRight) | |
1eee94d3 GM |
209 | END |
210 | END SkipLine ; | |
211 | ||
212 | (* Output procedures *) | |
213 | ||
214 | PROCEDURE WriteChar (cid: IOChan.ChanId; ch: CHAR); | |
215 | (* Writes the value of ch to the output stream cid. *) | |
216 | BEGIN | |
434dade5 | 217 | IOChan.TextWrite (cid, ADR (ch), SIZE (ch)) |
1eee94d3 GM |
218 | END WriteChar ; |
219 | ||
220 | PROCEDURE WriteLn (cid: IOChan.ChanId); | |
221 | (* Writes a line mark to the output stream cid. *) | |
222 | BEGIN | |
434dade5 | 223 | IOChan.WriteLn (cid) |
1eee94d3 GM |
224 | END WriteLn ; |
225 | ||
226 | PROCEDURE WriteString (cid: IOChan.ChanId; s: ARRAY OF CHAR); | |
227 | (* Writes the string value in s to the output stream cid. *) | |
228 | BEGIN | |
434dade5 | 229 | IOChan.TextWrite (cid, ADR (s), LENGTH (s)) |
1eee94d3 GM |
230 | END WriteString ; |
231 | ||
232 | ||
233 | END TextIO. |