Debian Codegraph @

gcc/m2/gm2-libs-iso/TextIO.mod

raw
1 (* TextIO.mod implement the ISO TextIO specification.
2
3 Copyright (C) 2008-2025 Free Software Foundation, Inc.
4 Contributed by Gaius Mulley <[email protected]>.
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
30 IMPORT IOConsts, CharClass, ASCII ;
31 FROM SYSTEM IMPORT ADR ;
32 FROM FIO IMPORT FlushOutErr ;
33 FROM libc IMPORT printf ;
34 FROM TextUtil IMPORT SkipSpaces, EofOrEoln, CharAvailable ;
35
36
37 CONST
38 DebugState = FALSE ;
39
40
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 ;
60
61
62 (*
63 SetNul - assigns the result in cid.
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
70 PROCEDURE SetNul (cid: IOChan.ChanId; i: CARDINAL;
71 VAR s: ARRAY OF CHAR; range: BOOLEAN) ;
72 BEGIN
73 IF DebugState
74 THEN
75 DumpState (cid)
76 END ;
77 IF i<=HIGH(s)
78 THEN
79 s[i] := ASCII.nul
80 ELSIF range
81 THEN
82 IOChan.SetReadResult (cid, IOConsts.outOfRange)
83 END
84 END SetNul ;
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 ;
97 IF CharAvailable (cid)
98 THEN
99 IOChan.Look (cid, ch, res) ;
100 IF res = IOConsts.allRight
101 THEN
102 IOChan.Skip (cid)
103 END
104 END
105 END ReadChar ;
106
107
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 ignore : CHAR ;
118 finished: BOOLEAN ;
119 BEGIN
120 h := HIGH(s) ;
121 i := 0 ;
122 finished := FALSE ;
123 WHILE CharAvailable (cid) AND (NOT finished) DO
124 IF i <= h
125 THEN
126 ReadChar (cid, s[i])
127 ELSE
128 ReadChar (cid, ignore)
129 END ;
130 IF EofOrEoln (cid)
131 THEN
132 finished := TRUE
133 ELSE
134 INC (i)
135 END
136 END ;
137 SetNul (cid, i, s, TRUE)
138 END ReadRestLine ;
139
140
141 PROCEDURE ReadString (cid: IOChan.ChanId; VAR s: ARRAY OF CHAR);
142 (* Removes only those characters from the input stream cid
143 before the next line mark that can be accommodated in s
144 as a string value, and copies them to s. The read result
145 is set to the value allRight, endOfLine, or endOfInput.
146 *)
147 VAR
148 i, h : CARDINAL ;
149 finished: BOOLEAN ;
150 BEGIN
151 h := HIGH (s) ;
152 i := 0 ;
153 finished := FALSE ;
154 WHILE (i<=h) AND CharAvailable (cid) AND (NOT finished) DO
155 ReadChar (cid, s[i]) ;
156 IF EofOrEoln (cid)
157 THEN
158 finished := TRUE
159 ELSE
160 INC (i)
161 END
162 END ;
163 SetNul (cid, i, s, FALSE)
164 END ReadString ;
165
166
167 PROCEDURE ReadToken (cid: IOChan.ChanId; VAR s: ARRAY OF CHAR);
168 (* Skips leading spaces, and then removes characters from
169 the input stream cid before the next space or line mark,
170 copying to s as many as can be accommodated as a string
171 value. The read result is set to the value allRight,
172 outOfRange, endOfLine, or endOfInput.
173 *)
174 VAR
175 i, h: CARDINAL ;
176 BEGIN
177 SkipSpaces (cid) ;
178 h := HIGH (s) ;
179 i := 0 ;
180 WHILE (i<=h) AND CharAvailable (cid) DO
181 ReadChar (cid, s[i]) ;
182 IF (s[i]=ASCII.nul) OR CharClass.IsWhiteSpace (s[i])
183 THEN
184 SetNul (cid, i, s, TRUE) ;
185 RETURN
186 END ;
187 INC (i)
188 END ;
189 SetNul (cid, i, s, TRUE)
190 END ReadToken ;
191
192 (* The following procedure reads past the next line mark *)
193
194 PROCEDURE SkipLine (cid: IOChan.ChanId);
195 (* Removes successive items from the input stream cid up
196 to and including the next line mark, or until the end
197 of input is reached. The read result is set to the
198 value allRight, or endOfInput.
199 *)
200 VAR
201 ch : CHAR ;
202 res: IOConsts.ReadResults ;
203 BEGIN
204 IOChan.Look (cid, ch, res) ;
205 WHILE res = IOConsts.allRight DO
206 IOChan.SkipLook (cid, ch, res)
207 END ;
208 IF res = IOConsts.endOfLine
209 THEN
210 IOChan.Skip (cid) ;
211 IOChan.SetReadResult (cid, IOConsts.allRight)
212 END
213 END SkipLine ;
214
215 (* Output procedures *)
216
217 PROCEDURE WriteChar (cid: IOChan.ChanId; ch: CHAR);
218 (* Writes the value of ch to the output stream cid. *)
219 BEGIN
220 IOChan.TextWrite (cid, ADR (ch), SIZE (ch))
221 END WriteChar ;
222
223 PROCEDURE WriteLn (cid: IOChan.ChanId);
224 (* Writes a line mark to the output stream cid. *)
225 BEGIN
226 IOChan.WriteLn (cid)
227 END WriteLn ;
228
229 PROCEDURE WriteString (cid: IOChan.ChanId; s: ARRAY OF CHAR);
230 (* Writes the string value in s to the output stream cid. *)
231 BEGIN
232 IOChan.TextWrite (cid, ADR (s), LENGTH (s))
233 END WriteString ;
234
235
236 END TextIO.