forked from ThomasMertes/seed7
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenable_io.s7i
298 lines (267 loc) · 12.1 KB
/
enable_io.s7i
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
(********************************************************************)
(* *)
(* enable_io.s7i Templates to enable file I/O for a given type *)
(* Copyright (C) 1989 - 2018 Thomas Mertes *)
(* *)
(* This file is part of the Seed7 Runtime Library. *)
(* *)
(* The Seed7 Runtime Library is free software; you can *)
(* redistribute it and/or modify it under the terms of the GNU *)
(* Lesser General Public License as published by the Free Software *)
(* Foundation; either version 2.1 of the License, or (at your *)
(* option) any later version. *)
(* *)
(* The Seed7 Runtime Library is distributed in the hope that it *)
(* will be useful, but WITHOUT ANY WARRANTY; without even the *)
(* implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR *)
(* PURPOSE. See the GNU Lesser General Public License for more *)
(* details. *)
(* *)
(* You should have received a copy of the GNU Lesser General *)
(* Public License along with this program; if not, write to the *)
(* Free Software Foundation, Inc., 51 Franklin Street, *)
(* Fifth Floor, Boston, MA 02110-1301, USA. *)
(* *)
(********************************************************************)
include "file.s7i";
var file: IN is forward;
var file: OUT is forward;
(**
* Template function to define input functions for ''aType''.
* It defines the functions [[#read(inout_aType)|read]] and
* [[#readln(inout_aType)|readln]]. The functions read a
* whitespace terminated word respectively a line as
* [[string]]. Afterwards the ''parse'' operator is used to
* convert the ''string'' to an ''aType'' value. The functions
* [[#read(inout_file,inout_aType)|read]] and
* [[#readln(inout_file,inout_aType)|readln]] are defined with
* [[file]] argument and without [[file]] argument. The
* functions without [[file]] argument use the standard
* input file [[stdio#IN|IN]].
*)
const proc: enable_input (in type: aType) is func
begin
(**
* Read ''aVar'' from a word read from ''inFile''.
* Before reading the word it skips spaces and tabs. The function
* accepts words ending with " ", "\t", end-of-line or [[char#EOF|EOF]].
* The conversion to the type ''aType'' is done with the ''parse''
* operator. When the function is left inFile.bufferChar contains the
* word ending character (' ', '\t', '\n' or [[char#EOF|EOF]]).
* @exception RANGE_ERROR If the ''parse'' operator cannot convert
* the word to the type ''aType''.
*)
const proc: read (inout file: inFile, inout aType: aVar) is func
begin
aVar := aType parse getwd(inFile);
end func;
(**
* Read ''aVar'' from a word read from ''inFile'' or use ''defaultValue''.
* Before reading the word it skips spaces and tabs. The function
* accepts words ending with " ", "\t", end-of-line or [[char#EOF|EOF]].
* If the word is empty ''defaultValue'' is assigned to ''aVar''.
* The conversion to the type ''aType'' is done with the ''parse''
* operator. When the function is left inFile.bufferChar contains the
* word ending character (' ', '\t', '\n' or [[char#EOF|EOF]]).
* @exception RANGE_ERROR If the ''parse'' operator cannot convert
* the word to the type ''aType''.
*)
const proc: read (inout file: inFile, inout aType: aVar,
in aType: defaultValue) is func
local
var string: stri is "";
begin
stri := getwd(inFile);
if stri = "" then
aVar := defaultValue;
else
aVar := aType parse stri;
end if;
end func;
(**
* Read ''aVar'' from a line read from ''inFile''.
* The function reads a string up to end-of-line or [[char#EOF|EOF]].
* The conversion to the type ''aType'' is done with the ''parse''
* operator. When the function is left inFile.bufferChar contains the
* line ending character ('\n' or [[char#EOF|EOF]]).
* @exception RANGE_ERROR If the ''parse'' operator cannot convert
* the line to the type ''aType''.
*)
const proc: readln (inout file: inFile, inout aType: aVar) is func
begin
aVar := aType parse trimValue(aType, getln(inFile));
end func;
(**
* Read ''aVar'' from a line read from ''inFile'' or use ''defaultValue''.
* The function reads a string up to end-of-line or [[char#EOF|EOF]].
* If the line is empty ''defaultValue'' is assigned to ''aVar''.
* The conversion to the type ''aType'' is done with the ''parse''
* operator. When the function is left inFile.bufferChar contains the
* line ending character ('\n' or [[char#EOF|EOF]]).
* @exception RANGE_ERROR If the ''parse'' operator cannot convert
* the line to the type ''aType''.
*)
const proc: readln (inout file: inFile, inout aType: aVar,
in aType: defaultValue) is func
local
var string: stri is "";
begin
stri := getln(inFile);
if stri = "" then
aVar := defaultValue;
else
aVar := aType parse trimValue(aType, stri);
end if;
end func;
(**
* Read ''aVar'' from a word read from the standard input file [[stdio#IN|IN]].
* Before reading the word it skips spaces and tabs. The function
* accepts words ending with " ", "\t", end-of-line or [[char#EOF|EOF]].
* The conversion to the type ''aType'' is done with the ''parse''
* operator. When the function is left [[stdio#IN|IN]].bufferChar contains the
* word ending character (' ', '\t', '\n' or [[char#EOF|EOF]]).
* @exception RANGE_ERROR If the ''parse'' operator cannot convert
* the word to the type ''aType''.
*)
const proc: read (inout aType: aVar) is func
begin
read(IN, aVar);
end func;
(**
* Read ''aVar'' from a word read from standard input ([[stdio#IN|IN]]) or use ''defaultValue''.
* Before reading the word it skips spaces and tabs. The function
* accepts words ending with " ", "\t", end-of-line or [[char#EOF|EOF]].
* If the word is empty ''defaultValue'' is assigned to ''aVar''.
* The conversion to the type ''aType'' is done with the ''parse''
* operator. When the function is left [[stdio#IN|IN]].bufferChar contains the
* word ending character (' ', '\t', '\n' or [[char#EOF|EOF]]).
* @exception RANGE_ERROR If the ''parse'' operator cannot convert
* the word to the type ''aType''.
*)
const proc: read (inout aType: aVar, in aType: defaultValue) is func
begin
read(IN, aVar, defaultValue);
end func;
(**
* Read ''aVar'' from a line read from the standard input file [[stdio#IN|IN]].
* The function reads a string up to end-of-line or [[char#EOF|EOF]].
* The conversion to the type ''aType'' is done with the ''parse''
* operator. When the function is left [[stdio#IN|IN]].bufferChar contains the
* line ending character ('\n' or [[char#EOF|EOF]]).
* @exception RANGE_ERROR If the ''parse'' operator cannot convert
* the line to the type ''aType''.
*)
const proc: readln (inout aType: aVar) is func
begin
readln(IN, aVar);
end func;
(**
* Read ''aVar'' from a line read from standard input ([[stdio#IN|IN]]) or use ''defaultValue''.
* The function reads a string up to end-of-line or [[char#EOF|EOF]].
* If the line is empty ''defaultValue'' is assigned to ''aVar''.
* The conversion to the type ''aType'' is done with the ''parse''
* operator. When the function is left [[stdio#IN|IN]].bufferChar contains the
* line ending character ('\n' or [[char#EOF|EOF]]).
* @exception RANGE_ERROR If the ''parse'' operator cannot convert
* the line to the type ''aType''.
*)
const proc: readln (inout aType: aVar, in aType: defaultValue) is func
begin
readln(IN, aVar, defaultValue);
end func;
end func;
(**
* Template function to define output functions for ''aType''.
* It defines the functions [[#write(in_aType)|write]]
* and [[#writeln(in_aType)|writeln]] and the operators
* [[#(in_aType)lpad(in_integer)|lpad]], [[#(in_aType)rpad(in_integer)|rpad]]
* and [[#(in_string)<&(in_aType)|<&]]. The functions and operators use
* the ''str'' function to convert the ''aType'' value to a [[string]].
* Afterwards they call the corresponding function respectively operator
* for [[string]] values. The functions [[#write(inout_file,in_aType)|write]]
* and [[#writeln(inout_file,in_aType)|writeln]] are defined with [[file]]
* argument and without [[file]] argument. The functions without [[file]]
* argument write to the standard output file [[stdio#OUT|OUT]].
*)
const proc: enable_output (in type: aType) is func
begin
(**
* Write ''aValue'' to the [[file]] ''outFile''.
*)
const proc: write (inout file: outFile, in aType: aValue) is func
begin
write(outFile, str(aValue));
end func;
(**
* Write ''aValue'' followed by end-of-line to the [[file]] ''outFile''.
*)
const proc: writeln (inout file: outFile, in aType: aValue) is func
begin
writeln(outFile, str(aValue));
end func;
(**
* Write ''aValue'' to the standard output file [[stdio#OUT|OUT]].
*)
const proc: write (in aType: aValue) is func
begin
write(OUT, aValue);
end func;
(**
* Write ''aValue'' followed by end-of-line to the standard output file [[stdio#OUT|OUT]].
*)
const proc: writeln (in aType: aValue) is func
begin
writeln(OUT, aValue);
end func;
(**
* Convert ''aValue'' to [[string]] and pad it with spaces at the left side.
* The [[string]] is padded up to a given length.
* @return ''aValue'' converted to string and left padded with spaces.
*)
const func string: (in aType: aValue) lpad (in integer: leng) is
return str(aValue) lpad leng;
(**
* Convert ''aValue'' to [[string]] and pad it with spaces at the right side.
* The [[string]] is padded up to a given length.
* @return ''aValue'' converted to string and right padded with spaces.
*)
const func string: (in aType: aValue) rpad (in integer: leng) is
return str(aValue) rpad leng;
(**
* Convert ''aValue'' to [[string]] and append it to ''stri''.
* This operator is intended for write statements.
* @return the result of the concatenation.
*)
const func string: (in string: stri) <& (in aType: aValue) is
return stri & str(aValue);
(**
* Convert ''aValue'' to [[string]] and append ''stri'' to it.
* This operator is intended for write statements.
* @return the result of the concatenation.
*)
const func string: (in aType: aValue) <& (in string: stri) is
return str(aValue) & stri;
end func;
(**
* Template function to define I/O functions for ''aType''.
* It defines the functions [[#read(inout_aType)|read]],
* [[#readln(inout_aType)|readln]], [[#write(in_aType)|write]]
* and [[#writeln(in_aType)|writeln]] and the
* operators [[#(in_aType)lpad(in_integer)|lpad]],
* [[#(in_aType)rpad(in_integer)|rpad]]
* and [[#(in_string)<&(in_aType)|<&]]. To do this it
* calls the templates [[#enable_input(in_type)|enable_input]] and
* [[#enable_output(in_type)|enable_output]].
*)
const proc: enable_io (in type: aType) is func
begin
enable_input(aType);
enable_output(aType);
end func;
enable_io(char);
enable_io(integer);
enable_io(boolean);
enable_io(bitset);
enable_output(void);
# enable_output(type);
# enable_output(ACTION);