Repository navigation
Expand file tree
/
Copy pathBFS.PAS
More file actions
340 lines (325 loc) · 8.91 KB
/
Copy pathBFS.PAS
File metadata and controls
340 lines (325 loc) · 8.91 KB
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
{ @author: Sylvain Maltais (support@gladir.com)
@created: 2026
@website(https://www.gladir.com/linux-0)
@abstract(Target: Turbo Pascal 7, Free Pascal 3.2)
}
Program BFS(Input,Output);
Uses DOS;
Const
MaxCommandLength=255;
Var
TextFile:Text;
BinaryFile:File;
FileName,CommandLine,OutputLine:String;
LineCount,CurrentLine,FileBytes:LongInt;
CommandPosition,ErrorCode:Integer;
SuppressSize,QuitRequested:Boolean;
Function TrimText(S:String):String;
Var
First,Last:Integer;
Begin
First:=1;
Last:=Length(S);
While(First<=Last)and(S[First]in[' ',#9])Do Inc(First);
While(Last>=First)and(S[Last]in[' ',#9])Do Dec(Last);
TrimText:=Copy(S,First,Last-First+1);
End;
Function IntString(Value:LongInt):String;
Var
S:String;
Begin
Str(Value,S);
IntString:=S;
End;
Procedure CountFile;
Begin
LineCount:=0;
{$I-}Assign(TextFile,FileName);
Reset(TextFile);{$I+}
If IOResult<>0 Then Begin
WriteLn('Erreur : impossible de lire ',FileName);
Halt(1);
End;
While Not EOF(TextFile)Do Begin
ReadLn(TextFile);
Inc(LineCount);
End;
Close(TextFile);
Assign(BinaryFile,FileName);
{$I-}Reset(BinaryFile,1);{$I+}
If IOResult<>0 Then Begin
WriteLn('Erreur : impossible de mesurer ',FileName);
Halt(1);
End;
FileBytes:=FileSize(BinaryFile);
Close(BinaryFile);
End;
Function ReadLineAt(Target:LongInt; Var S:String):Boolean;
Var
Source:Text;
Number:LongInt;
Begin
ReadLineAt:=False;
If(Target<1)or(Target>LineCount)Then Exit;
{$I-}Assign(Source,FileName);
Reset(Source);{$I+}
If IOResult<>0 Then Exit;
Number:=0;
While Not EOF(Source)Do Begin
ReadLn(Source,S);
Inc(Number);
If Number=Target Then Begin
ReadLineAt:=True;
Break;
End;
End;
Close(Source);
End;
Procedure PrintLines(FirstLine,LastLine:LongInt; Numbered:Boolean);
Var
Source:Text;
Number:LongInt;
S:String;
Begin
If FirstLine<1 Then FirstLine:=1;
If LastLine>LineCount Then LastLine:=LineCount;
If FirstLine>LastLine Then Exit;
{$I-}Assign(Source,FileName);
Reset(Source);{$I+}
If IOResult<>0 Then Begin
WriteLn('Erreur : impossible de relire ',FileName);
Exit;
End;
Number:=0;
While(Not EOF(Source))and(Number<LastLine)Do Begin
ReadLn(Source,S);
Inc(Number);
If Number>=FirstLine Then Begin
If Numbered Then Write(Number:6,' ');
WriteLn(S);
CurrentLine:=Number;
End;
End;
Close(Source);
End;
Function FindForward(Const Needle:String; StartLine:LongInt;
Var FoundLine:LongInt):Boolean;
Var
Source:Text;
S:String;
Number:LongInt;
Begin
FindForward:=False;
{$I-}Assign(Source,FileName);
Reset(Source);{$I+}
If IOResult<>0 Then Exit;
Number:=0;
While Not EOF(Source)Do Begin
ReadLn(Source,S);
Inc(Number);
If(Number>=StartLine)and(Pos(Needle,S)>0)Then Begin
FoundLine:=Number;
FindForward:=True;
Break;
End;
End;
Close(Source);
End;
Function FindBackward(Const Needle:String; StartLine:LongInt;
Var FoundLine:LongInt):Boolean;
Var
Source:Text;
S:String;
Number:LongInt;
Begin
FindBackward:=False;
If StartLine>LineCount Then StartLine:=LineCount;
{$I-}Assign(Source,FileName);
Reset(Source);{$I+}
If IOResult<>0 Then Exit;
Number:=0;
While(Not EOF(Source))and(Number<StartLine)Do Begin
ReadLn(Source,S);
Inc(Number);
If Pos(Needle,S)>0 Then Begin
FoundLine:=Number;
FindBackward:=True;
End;
End;
Close(Source);
End;
Function ReadAddress(Var Position:Integer; Var Value:LongInt):Boolean;
Var
Start:Integer;
S:String;
Begin
ReadAddress:=False;
While(Position<=Length(CommandLine))and(CommandLine[Position]=' ')Do Inc(Position);
If Position>Length(CommandLine)Then Exit;
If CommandLine[Position]='$'Then Begin
Value:=LineCount;
Inc(Position);
ReadAddress:=True;
Exit;
End;
Start:=Position;
While(Position<=Length(CommandLine))and
(CommandLine[Position]in['0'..'9'])Do Inc(Position);
If Position=Start Then Exit;
S:=Copy(CommandLine,Start,Position-Start);
Val(S,Value,ErrorCode);
ReadAddress:=ErrorCode=0;
End;
Procedure ShowHelp;
Begin
If Copy(GetEnv('LANGUAGE'),1,2)='EN'Then Begin
WriteLn('BFS : Browse a large text file interactively.');
WriteLn('Syntax: bfs [-] filename');
WriteLn('Commands: p print, n numbered print, = line number,');
WriteLn(' /text/ forward search, ?text? backward search,');
WriteLn(' g/text/p print every matching line, f filename, q quit.');
End Else Begin
WriteLn('BFS : Cette commande permet de parcourir interactivement un grand fichier texte.');
WriteLn;
WriteLn('Syntaxe : bfs [-] fichier');
WriteLn;
WriteLn('Commandes : p afficher, n afficher avec numero, = numero,');
WriteLn(' /texte/ chercher avant, ?texte? chercher arriere,');
WriteLn(' g/texte/p afficher les correspondances, f fichier, q quitter.');
End;
End;
Procedure ExecuteCommand;
Var
FirstAddress,SecondAddress,FoundLine,Start:LongInt;
HasAddress,HasSecondAddress:Boolean;
Operation,Delimiter:Char;
S:String;
Begin
CommandLine:=TrimText(CommandLine);
If CommandLine=''Then Begin
If CurrentLine<LineCount Then PrintLines(CurrentLine+1,CurrentLine+1,False);
Exit;
End;
CommandPosition:=1;
HasAddress:=ReadAddress(CommandPosition,FirstAddress);
HasSecondAddress:=False;
If HasAddress and(CommandPosition<=Length(CommandLine))and
(CommandLine[CommandPosition]=',')Then Begin
Inc(CommandPosition);
HasSecondAddress:=ReadAddress(CommandPosition,SecondAddress);
If Not HasSecondAddress Then SecondAddress:=LineCount;
End;
While(CommandPosition<=Length(CommandLine))and
(CommandLine[CommandPosition]=' ')Do Inc(CommandPosition);
If HasAddress Then Begin
If Not HasSecondAddress Then SecondAddress:=FirstAddress;
If CommandPosition>Length(CommandLine)Then Begin
PrintLines(FirstAddress,SecondAddress,False);
Exit;
End;
End Else Begin
FirstAddress:=CurrentLine;
SecondAddress:=CurrentLine;
End;
Operation:=CommandLine[CommandPosition];
Inc(CommandPosition);
Case Operation Of
'p':Begin
If Not HasAddress and(FirstAddress<1)Then Begin
FirstAddress:=1;
SecondAddress:=1;
End;
PrintLines(FirstAddress,SecondAddress,False);
End;
'n':Begin
If Not HasAddress and(FirstAddress<1)Then Begin
FirstAddress:=1;
SecondAddress:=1;
End;
PrintLines(FirstAddress,SecondAddress,True);
End;
'=':Begin
If Not HasAddress Then FirstAddress:=LineCount;
WriteLn(FirstAddress);
End;
'f':WriteLn(FileName);
'q','Q':QuitRequested:=True;
'h':ShowHelp;
'/','?':Begin
Delimiter:=Operation;
Start:=CommandPosition;
While(CommandPosition<=Length(CommandLine))and
(CommandLine[CommandPosition]<>Delimiter)Do Inc(CommandPosition);
S:=Copy(CommandLine,Start,CommandPosition-Start);
If S=''Then Begin
WriteLn('?');
Exit;
End;
If Operation='/'Then Begin
If Not FindForward(S,CurrentLine+1,FoundLine)Then
If Not FindForward(S,1,FoundLine)Then FoundLine:=0;
End Else Begin
If CurrentLine<=0 Then Start:=LineCount Else Start:=CurrentLine-1;
If Not FindBackward(S,Start,FoundLine)Then FoundLine:=0;
End;
If FoundLine=0 Then WriteLn('?')
Else PrintLines(FoundLine,FoundLine,False);
End;
'g':Begin
If(CommandPosition>Length(CommandLine))or
Not(CommandLine[CommandPosition]in['/','?'])Then Begin
WriteLn('?');
Exit;
End;
Delimiter:=CommandLine[CommandPosition];
Inc(CommandPosition);
Start:=CommandPosition;
While(CommandPosition<=Length(CommandLine))and
(CommandLine[CommandPosition]<>Delimiter)Do Inc(CommandPosition);
S:=Copy(CommandLine,Start,CommandPosition-Start);
If S=''Then Begin
WriteLn('?');
Exit;
End;
For FoundLine:=1 to LineCount Do
If ReadLineAt(FoundLine,OutputLine)and(Pos(S,OutputLine)>0)Then
PrintLines(FoundLine,FoundLine,False);
End;
Else WriteLn('?');
End;
End;
BEGIN
FileName:='';
SuppressSize:=False;
If(ParamCount=1)and((ParamStr(1)='--help')or(ParamStr(1)='-h')or
(ParamStr(1)='/?')or(ParamStr(1)='/h')or(ParamStr(1)='/H'))Then Begin
ShowHelp;
Halt(0);
End;
If(ParamCount=1)and((ParamStr(1)='--version')or(ParamStr(1)='-v'))Then Begin
WriteLn('bfs (LINUX-0) 1.0');
Halt(0);
End;
If ParamCount=1 Then FileName:=ParamStr(1) Else
If ParamCount=2 Then Begin
If ParamStr(1)='-'Then Begin
SuppressSize:=True;
FileName:=ParamStr(2);
End Else Begin
ShowHelp;
Halt(2);
End;
End Else Begin
ShowHelp;
Halt(2);
End;
CountFile;
If Not SuppressSize Then WriteLn(FileBytes,' bytes');
CurrentLine:=0;
QuitRequested:=False;
While Not QuitRequested Do Begin
Write('*');
ReadLn(Input,CommandLine);
ExecuteCommand;
End;
END.