Debian Codegraph @

utils/m-tx/mtx-src/multfile.pas

raw
1 unit multfile; (* DPL 2004-03-06, 2015-01-16
2 (* Support for including files into an input stream.
3 (* Intended for an application that does not require Pascal formatting,
4 (* but reads complete lines one at a time.
5 (* You never actually work with any files, except by supplying the
6 ( filename when the file is opened.
7 (* At any stage, you can switch the input to a new file.
8 (* When the new file is at EOF, and a "read" is issued, the file is
9 (* closed and the previous one resumed transparently. This inclusion
10 (* can be nested to any level, memory permitting.
11 (* --- Normal mode of operation, replacing the usual Pascal style ---
12 Instead of: assign(textfile,filename); reset(textfile)
13 use: pushFile(filename)
14 When another file should be included before the current file is done,
15 use: pushFile(newfilename)
16 Instead of: readln(textfile,line)
17 use: line:=readLine
18 Instead of: eof(textfilen)
19 or: eofAll; {Are all files at EOF?}
20 (* --- Abnormal mode of operation ---
21 To abort a file before EOF is reached:
22 use: popFile;
23 To abort all files:
24 use: closeAll;
25 To test whether only the current file is at EOF:
26 use: eofCurrent;
27 (* Additional features:
28 fileError: boolean function, was there an error during file open or read?
29 currentFilename: string function, name of current file
30 currentLineNo: integer function, number of line just read from current file
31 isEmpty(var s: string): boolean function, is s empty?
32 readData: string function, like readLine, but continue reading until
33 a non-blank line is found, return blank only at EOF
34 skipBlanks: skip blank lines in input: next call to readLine will be
35 non-blank unless EOF is encountered
36 report(items): procedure to control which messages are printed,
37 "items" is the sum of the following options
38 (constants with the appropriate values are defined in the interface)
39 1: reportnewfile - file is opened
40 2: reportoldfile - file is resumed
41 4: reportclose - file is closed
42 8: reporterror - a file error is encountered
43 16: reportrecursive - there is a recursive include
44 The default value is items=27 (all the above except reportclose)
45 At present you cannot turn reportrecursive off.
46 *)
47 interface
48
49 procedure pushFile(filename: string);
50 procedure popFile;
51 procedure closeAll;
52 procedure report(items: integer);
53 function currentFilename: string;
54 function eofAll: boolean;
55 function eofCurrent: boolean;
56 function fileError: boolean;
57 function readLine: string;
58 function readData: string;
59 function isEmpty(var s: string): boolean;
60 function currentLineNo: integer;
61 procedure skipBlanks;
62 const nextData: string = '';
63
64 const
65 reportnewfile = 1;
66 reportoldfile = 2;
67 reportclose = 4;
68 reporterror = 8;
69 reportrecursive = 16;
70
71 implementation
72
73 type
74 pfilenode = ^filenode;
75 filenode = record
76 name: string;
77 actualfile: text;
78 prev: pfilenode;
79 lineno: integer;
80 end;
81
82 const current: pfilenode = NIL;
83 last_valid_line_no: integer = 0;
84 inputerror: boolean = false;
85 reportitem: integer = reportnewfile + reportoldfile
86 + reporterror + reportrecursive;
87
88 procedure report(items: integer); begin reportitem := items end;
89
90 function recursive (filename: string): boolean;
91 var previous: pfilenode;
92 begin if current=NIL then begin recursive:=false; exit; end;
93 previous := current; recursive:=true;
94 while previous <> NIL do
95 begin
96 if filename=previous^.name then exit;
97 previous := previous^.prev;
98 end;
99 recursive:=false
100 end;
101
102 procedure pushFile(filename: string);
103 var newnode: pfilenode;
104 begin
105 if recursive(filename) then
106 begin writeln('===! Ignoring recursive include of file ',filename); exit;
107 end;
108 new(newnode); newnode^.name := filename; newnode^.prev := current;
109 newnode^.lineno := 0;
110 {$I-}
111 assign(newnode^.actualfile,filename); reset(newnode^.actualfile);
112 {$I+}
113 inputerror := ioresult<>0;
114 if inputerror then dispose(newnode) else current := newnode;
115 if not inputerror and ((reportitem and reportnewfile)>0) then writeln
116 ('==>> Input from file ',currentFilename);
117 if inputerror and ((reportitem and reporterror)>0) then writeln
118 ('==!! Could not open file ',filename);
119 end;
120
121 procedure popFile;
122 var previous: pfilenode;
123 begin if current=NIL then exit;
124 if (reportitem and reportclose)>0 then writeln
125 ('==>> Closing file ',currentFilename,' at line number ', currentLineNo:1);
126 close(current^.actualfile); previous := current^.prev; dispose(current);
127 current := previous;
128 if (current<>NIL) and ((reportitem and reportoldfile)>0) then writeln
129 ('==>> Resuming input from file ',currentFilename,' at line number ',
130 currentLineNo:1);
131 end;
132
133 procedure closeAll; begin repeat popFile until current=NIL; end;
134
135 function eofCurrent: boolean;
136 begin eofCurrent := eof(current^.actualfile);
137 end;
138
139 function readLine: string;
140 var s: string;
141 begin if nextData<>'' then
142 begin readLine:=nextData; nextData:=''; exit end;
143 if eofAll then begin readLine:=''; exit end;
144 {$I-}
145 readln(current^.actualfile,s); readLine:=s;
146 {$I+}
147 inputerror := ioresult<>0;
148 if not inputerror then
149 begin inc(current^.lineno);
150 last_valid_line_no := current^.lineno
151 end;
152 if inputerror and ((reportitem and reporterror)>0) then writeln
153 ('==!! Could not read from file ',currentFilename);
154 end;
155
156 function isEmpty(var s: string): boolean;
157 var i: integer;
158 begin if length(s)=0 then begin isEmpty:=true; exit; end;
159 for i:=1 to length(s) do if s[i]<>' ' then
160 begin isEmpty:=false; exit; end;
161 isEmpty:=true
162 end;
163
164 function readData: string;
165 var s: string;
166 begin if not isEmpty(nextData) then
167 begin readData:=nextData; nextData:=''; exit end;
168 while not eofAll do
169 begin s:=readLine;
170 if not isEmpty(s) then begin readData:=s; exit end;
171 end;
172 readData:='';
173 end;
174
175 procedure skipBlanks;
176 begin while nextData='' do
177 begin nextData:=readData; if eofAll then exit
178 end
179 end;
180
181 function eofAll: boolean;
182 begin eofAll := true;
183 if current=NIL then exit else
184 if eofCurrent then begin popFile; eofAll:=eofAll; exit end;
185 eofAll:=false
186 end;
187
188 function currentLineNo: integer;
189 begin currentLineNo := last_valid_line_no
190 end;
191
192 function currentFilename: string;
193 begin if current = NIL then currentFilename := 'No file open yet'
194 else currentFilename := current^.name;
195 end;
196
197 function fileError: boolean; begin fileError := inputerror; end;
198
199 end.