-
Notifications
You must be signed in to change notification settings - Fork 0
/
strip.pas
executable file
·287 lines (271 loc) · 6.72 KB
/
strip.pas
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
PROGRAM Strip;
USES Crt, getopts;
TYPE
String80 = String[80];
String3 = String[3];
VAR
cchr : Char;
compare : Text;
compareopt : String80;
erroropt : String80;
errname : String80;
error : Boolean;
errorfile : Text;
i : Integer;
illegal : Set of Char;
line : String[255];
lineno : Integer;
master : Text;
masteropt : String80;
outcom : Text;
outmst : Text;
overwrite : Char;
temp : Integer;
tempcomp : String80;
tempmast : String80;
want_error : Boolean;
want_errorc : Char;
PROCEDURE Add_ext (VAR filename : String80; ext : String3);
VAR
dot : Boolean;
i : Integer;
Begin
dot := false;
For i := 1 to length(filename) do
dot := dot or (filename[i] = '.');
If not dot then
Begin
filename := Concat(filename,'.');
filename := Concat(filename,ext)
End
End;
PROCEDURE ParseCommandLine;
var
c : char;
optionindex : Longint;
theopts : array[1..3] of TOption;
begin
masteropt := '';
compareopt := '';
erroropt := '';
with theopts[1] do
begin
name:='master';
has_arg:=1;
flag:=nil;
value:='m';
end;
with theopts[2] do
begin
name:='compare';
has_arg:=1;
flag:=nil;
value:='c';
end;
with theopts[3] do
begin
name:='errors';
has_arg:=1;
flag:=nil;
value:='e';
end;
c:=#0;
repeat
c:=getlongopts('m:c:e:012',@theopts[1],optionindex);
case c of
'1','2','3','4','5','6','7','8','9' :
begin
writeln ('Got optind : ',c)
end;
#0 : begin
write ('Long option : ',theopts[optionindex].name);
if theopts[optionindex].has_arg>0 then
writeln (' With value : ',optarg)
else
writeln
end;
'a' : writeln ('Option a.');
'b' : writeln ('Option b.');
'c' : Begin
compareopt := optarg;
End;
'm' : Begin
masteropt := optarg;
End;
'e' : Begin
erroropt := optarg;
End;
'd' : writeln ('Option d : ', optarg);
'?',':' : writeln ('Error with opt : ',optopt);
end; { case }
until c=endofoptions;
if optind<=paramcount then
begin
write ('Non options : ');
while optind<=paramcount do
begin
write (paramstr(optind),' ');
inc(optind)
end;
writeln
end
end;
PROCEDURE Initialize;
Begin
If masteropt = '' then
Begin
Writeln('--master filename missing');
Halt(1);
End;
If compareopt = '' then
Begin
Writeln('--compare filename missing');
Halt(1);
End;
tempmast := masteropt;
Assign(master,tempmast);
{$I-}
Reset(master);
{$I+}
temp := ioresult;
If temp = 1 then
Begin
Writeln('File not on disk');
Halt(1)
End;
tempcomp := compareopt;
Assign(compare,tempcomp);
{$I-}
Reset(compare);
{I+}
temp := ioresult;
If temp = 1 then
Begin
Writeln('File not on disk');
Halt(2);
End;
want_error := erroropt <> '';
If want_error then
Begin
errname := erroropt;
Assign(errorfile,errname);
{$i-}
Reset(errorfile);
{$i+}
temp := ioresult;
If temp = 0 then
Begin
Writeln('File ',erroropt,' already on disk. Please delete it first.');
Halt(3)
End;
Rewrite(errorfile)
End;
Reset(master);
Reset(compare);
Assign(outmst,'master.tmp');
Assign(outcom,'compare.tmp');
Rewrite(outmst);
Rewrite(outcom);
error := false;
lineno := 0;
illegal := [];
For i := 1 to 9 do
illegal := illegal + [chr(i)];
illegal := illegal + [chr(11), chr(12)];
For i := 14 to 31 do
illegal := illegal + [chr(i)];
{
For i := 128 to 255 do
illegal := illegal + [chr(i)]
}
End;
PROCEDURE Strip_controls;
VAR
i : Integer;
j : Integer;
Begin
For i := 1 to length(line) do
If line[i] in illegal then
Begin
error := true;
If want_error then
Begin
Write(errorfile,'Line ',lineno,' of ');
If cchr = '@' then
Write(errorfile,'master')
Else
Write(errorfile,'compare');
Writeln(errorfile,' - Illegal character')
End;
Write('Line ',lineno,' of ');
If cchr = '@' then
Write('master')
Else
Write('compare');
Writeln(' - Illegal character');
line[i] := cchr
End
End;
FUNCTION empty_line : Boolean;
VAR
flag : Boolean;
i : Integer;
Begin
flag := true;
If length(line) > 0 then
For i := 1 to length(line) do
If line[i] <> ' ' then
flag := false;
empty_line := flag
End;
Begin { main program }
ParseCommandLine;
Initialize;
cchr := '@';
While not eof(master) do
Begin
Readln(master,line);
lineno := lineno + 1;
Strip_controls;
If not empty_line then
Writeln(outmst,line)
Else
Begin
error := true;
If want_error then
Writeln(errorfile,'Line ',lineno,' of master - Blank line');
Writeln('Line ',lineno,' of master - Blank line');
Writeln(outmst,'*** Empty line ***')
End
End;
cchr := '#';
lineno := 0;
While not eof(compare) do
Begin
Readln(compare,line);
lineno := lineno + 1;
Strip_controls;
If not empty_line then
Writeln(outcom,line)
Else
Begin
error := true;
If want_error then
Writeln(errorfile,'Line ',lineno,' of compare - Blank line');
Writeln('Line ',lineno,' of compare - Blank line');
Writeln(outcom,'*** Empty line ***')
End
End;
Close(master);
Close(compare);
Close(outmst);
Close(outcom);
If want_error then
Close(errorfile);
If error then
Begin
Writeln('Hit any key to continue.');
cchr := ReadKey()
End
End.