]>
Commit | Line | Data |
---|---|---|
1 | (* CardinalIO.mod provides a PIM and Logitech compatible module. | |
2 | ||
3 | Copyright (C) 2004-2024 Free Software Foundation, Inc. | |
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 CardinalIO ; | |
28 | ||
29 | ||
30 | FROM DynamicStrings IMPORT String, InitString, KillString, RemoveWhitePrefix ; | |
31 | FROM SYSTEM IMPORT ADR, BYTE ; | |
32 | IMPORT InOut ; | |
33 | ||
34 | FROM StringConvert IMPORT StringToCardinal, CardinalToString, | |
35 | StringToLongCardinal, LongCardinalToString, | |
36 | StringToShortCardinal, ShortCardinalToString ; | |
37 | ||
38 | ||
39 | (* | |
40 | ReadCardinal - read an unsigned decimal number from the terminal. | |
41 | The read continues until a space, newline, esc or | |
42 | end of file is reached. | |
43 | *) | |
44 | ||
45 | PROCEDURE ReadCardinal (VAR c: CARDINAL) ; | |
46 | VAR | |
47 | s: String ; | |
48 | BEGIN | |
49 | s := RemoveWhitePrefix(InOut.ReadS()) ; | |
50 | IF InOut.Done | |
51 | THEN | |
52 | c := StringToCardinal(s, 10, Done) | |
53 | ELSE | |
54 | Done := FALSE | |
55 | END ; | |
56 | s := KillString(s) | |
57 | END ReadCardinal ; | |
58 | ||
59 | ||
60 | (* | |
61 | WriteCardinal - writes the value, c, to the terminal and ensures | |
62 | that at least, n, characters are written. The number | |
63 | will be padded out by preceeding spaces if necessary. | |
64 | *) | |
65 | ||
66 | PROCEDURE WriteCardinal (c: CARDINAL; n: CARDINAL) ; | |
67 | VAR | |
68 | s: String ; | |
69 | BEGIN | |
70 | s := KillString(InOut.WriteS(CardinalToString(c, n, ' ', 10, FALSE))) ; | |
71 | Done := TRUE | |
72 | END WriteCardinal ; | |
73 | ||
74 | ||
75 | (* | |
76 | ReadHex - reads in an unsigned hexadecimal number from the terminal. | |
77 | The read continues until a space, newline, esc or | |
78 | end of file is reached. | |
79 | *) | |
80 | ||
81 | PROCEDURE ReadHex (VAR c: CARDINAL) ; | |
82 | VAR | |
83 | s: String ; | |
84 | BEGIN | |
85 | s := RemoveWhitePrefix(InOut.ReadS()) ; | |
86 | IF InOut.Done | |
87 | THEN | |
88 | c := StringToCardinal(s, 16, Done) | |
89 | ELSE | |
90 | Done := FALSE | |
91 | END ; | |
92 | s := KillString(s) | |
93 | END ReadHex ; | |
94 | ||
95 | ||
96 | (* | |
97 | WriteHex - writes out a CARDINAL, c, in hexadecimal format padding | |
98 | with, n, characters (leading with '0') | |
99 | *) | |
100 | ||
101 | PROCEDURE WriteHex (c: CARDINAL; n: CARDINAL) ; | |
102 | VAR | |
103 | s: String ; | |
104 | BEGIN | |
105 | s := KillString(InOut.WriteS(CardinalToString(c, n, '0', 16, TRUE))) ; | |
106 | Done := TRUE | |
107 | END WriteHex ; | |
108 | ||
109 | ||
110 | (* | |
111 | ReadLongCardinal - read an unsigned decimal number from the terminal. | |
112 | The read continues until a space, newline, esc or | |
113 | end of file is reached. | |
114 | *) | |
115 | ||
116 | PROCEDURE ReadLongCardinal (VAR c: LONGCARD) ; | |
117 | VAR | |
118 | s: String ; | |
119 | BEGIN | |
120 | s := RemoveWhitePrefix(InOut.ReadS()) ; | |
121 | IF InOut.Done | |
122 | THEN | |
123 | c := StringToLongCardinal(s, 10, Done) | |
124 | ELSE | |
125 | Done := FALSE | |
126 | END ; | |
127 | s := KillString(s) | |
128 | END ReadLongCardinal ; | |
129 | ||
130 | ||
131 | (* | |
132 | WriteLongCardinal - writes the value, c, to the terminal and ensures | |
133 | that at least, n, characters are written. The number | |
134 | will be padded out by preceeding spaces if necessary. | |
135 | *) | |
136 | ||
137 | PROCEDURE WriteLongCardinal (c: LONGCARD; n: CARDINAL) ; | |
138 | VAR | |
139 | s: String ; | |
140 | BEGIN | |
141 | s := KillString(InOut.WriteS(LongCardinalToString(c, n, ' ', 10, FALSE))) ; | |
142 | Done := TRUE | |
143 | END WriteLongCardinal ; | |
144 | ||
145 | ||
146 | (* | |
147 | ReadLongHex - reads in an unsigned hexadecimal number from the terminal. | |
148 | The read continues until a space, newline, esc or | |
149 | end of file is reached. | |
150 | *) | |
151 | ||
152 | PROCEDURE ReadLongHex (VAR c: LONGCARD) ; | |
153 | VAR | |
154 | s: String ; | |
155 | BEGIN | |
156 | s := RemoveWhitePrefix(InOut.ReadS()) ; | |
157 | IF InOut.Done | |
158 | THEN | |
159 | c := StringToLongCardinal(s, 16, Done) | |
160 | ELSE | |
161 | Done := FALSE | |
162 | END ; | |
163 | s := KillString(s) | |
164 | END ReadLongHex ; | |
165 | ||
166 | ||
167 | (* | |
168 | WriteLongHex - writes out a LONGCARD, c, in hexadecimal format padding | |
169 | with, n, characters (leading with '0') | |
170 | *) | |
171 | ||
172 | PROCEDURE WriteLongHex (c: LONGCARD; n: CARDINAL) ; | |
173 | VAR | |
174 | s: String ; | |
175 | BEGIN | |
176 | s := KillString(InOut.WriteS(LongCardinalToString(c, n, '0', 16, TRUE))) ; | |
177 | Done := TRUE | |
178 | END WriteLongHex ; | |
179 | ||
180 | ||
181 | (* | |
182 | ReadShortCardinal - read an unsigned decimal number from the terminal. | |
183 | The read continues until a space, newline, esc or | |
184 | end of file is reached. | |
185 | *) | |
186 | ||
187 | PROCEDURE ReadShortCardinal (VAR c: SHORTCARD) ; | |
188 | VAR | |
189 | s: String ; | |
190 | BEGIN | |
191 | s := RemoveWhitePrefix(InOut.ReadS()) ; | |
192 | IF InOut.Done | |
193 | THEN | |
194 | c := StringToShortCardinal(s, 10, Done) | |
195 | ELSE | |
196 | Done := FALSE | |
197 | END ; | |
198 | s := KillString(s) | |
199 | END ReadShortCardinal ; | |
200 | ||
201 | ||
202 | (* | |
203 | WriteShortCardinal - writes the value, c, to the terminal and ensures | |
204 | that at least, n, characters are written. The number | |
205 | will be padded out by preceeding spaces if necessary. | |
206 | *) | |
207 | ||
208 | PROCEDURE WriteShortCardinal (c: SHORTCARD; n: CARDINAL) ; | |
209 | VAR | |
210 | s: String ; | |
211 | BEGIN | |
212 | s := KillString(InOut.WriteS(ShortCardinalToString(c, n, ' ', 10, FALSE))) ; | |
213 | Done := TRUE | |
214 | END WriteShortCardinal ; | |
215 | ||
216 | ||
217 | (* | |
218 | ReadShortHex - reads in an unsigned hexadecimal number from the terminal. | |
219 | The read continues until a space, newline, esc or | |
220 | end of file is reached. | |
221 | *) | |
222 | ||
223 | PROCEDURE ReadShortHex (VAR c: SHORTCARD) ; | |
224 | VAR | |
225 | s: String ; | |
226 | BEGIN | |
227 | s := RemoveWhitePrefix(InOut.ReadS()) ; | |
228 | IF InOut.Done | |
229 | THEN | |
230 | c := StringToShortCardinal(s, 16, Done) | |
231 | ELSE | |
232 | Done := FALSE | |
233 | END ; | |
234 | s := KillString(s) | |
235 | END ReadShortHex ; | |
236 | ||
237 | ||
238 | (* | |
239 | WriteShortHex - writes out a SHORTCARD, c, in hexadecimal format padding | |
240 | with, n, characters (leading with '0') | |
241 | *) | |
242 | ||
243 | PROCEDURE WriteShortHex (c: SHORTCARD; n: CARDINAL) ; | |
244 | VAR | |
245 | s: String ; | |
246 | BEGIN | |
247 | s := KillString(InOut.WriteS(ShortCardinalToString(c, n, '0', 16, TRUE))) ; | |
248 | Done := TRUE | |
249 | END WriteShortHex ; | |
250 | ||
251 | ||
252 | END CardinalIO. | |
253 | (* | |
254 | * Local variables: | |
255 | * compile-command: "gm2 -I.:../gm2-libs -g -c -Wsources CardinalIO.mod" | |
256 | * End: | |
257 | *) |