-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlist.i
More file actions
447 lines (400 loc) · 14.1 KB
/
list.i
File metadata and controls
447 lines (400 loc) · 14.1 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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
% unit
% module List
type vote_record :
record
name : string
number : int
end record
type shortList : array 1 .. shortListMax of str
const INDENT := repeat (" ", 4)
% Kludge cause "star" won't take SIL names that start with % or $
const dummyFileName := "$$$$"
var lastFileName : string := dummyFileName
const tracing := true
const notTracing := not tracing
const flagDirectories := "-p" % Parameter for dirToList
proc openFailMessage (fileNo : int, fileName : string, mode : string)
put "*** Open '", mode, "' fails for '", fileName, "'"
end openFailMessage
proc openForGet (var fileNo : int, fileName : string)
if lastFileName not= dummyFileName then
put "***Double open: ", lastFileName, " then ", fileName
end if
open : fileNo, fileName, get
if fileNo > 0 then
lastFileName := fileName
else
openFailMessage (fileNo, fileName, "get")
end if
end openForGet
proc openForRead (var fileNo : int, fileName : string)
if lastFileName not= dummyFileName then
put "***Double open: ", lastFileName, " then ", fileName
end if
open : fileNo, fileName, read
if fileNo > 0 then
lastFileName := fileName
else
openFailMessage (fileNo, fileName, "read")
end if
end openForRead
proc openForPut (var fileNo : int, fileName : string)
if lastFileName not= dummyFileName then
put "***Double open: ", lastFileName, " then ", fileName
end if
open : fileNo, fileName, put
if fileNo > 0 then
lastFileName := fileName
else
openFailMessage (fileNo, fileName, "put")
end if
end openForPut
proc openForWrite (var fileNo : int, fileName : string)
if lastFileName not= dummyFileName then
put "***Double open: ", lastFileName, " then ", fileName
end if
open : fileNo, fileName, write
if fileNo > 0 then
lastFileName := fileName
else
openFailMessage (fileNo, fileName, "write")
end if
end openForWrite
proc openForAppend (var fileNo : int, fileName : string)
if lastFileName not= dummyFileName then
put "***Double open: ", lastFileName, " then ", fileName
end if
open : fileNo, fileName, seek, mod, put
if fileNo > 0 then
lastFileName := fileName
seek : fileNo, *
else
openFailMessage (fileNo, fileName, "append")
end if
end openForAppend
proc closeFile (fileNo : int, fileName : string)
if fileName not= lastFileName then
put "*** Warning: last opened ", lastFileName, " but closing ",
fileName
end if
close : fileNo
% if Error.Last not= 0 then
% put "***Close fails: ", Error.Last
% end if
lastFileName := dummyFileName
end closeFile
% Use LSFlags = flagDirectories to get "/" to mark directories
proc dirToList (dirName : string, LSFlags : string,
var fileList : array 1 .. * of string (*), var n : int,
trace : boolean)
var tempFileName : string
var fileNo : int
loop
var i : int
randint (i, 0, 999999)
tempFileName := "/tmp/xxx.HOLT.xxx" + intstr (i)
open : fileNo, tempFileName, get
exit when fileNo <= 0 % See if file exists
close : fileNo
end loop
var success : int
const LSCommand := "ls " + LSFlags + " " + dirName + " > " +
tempFileName
system (LSCommand, success)
if success not= 0 then
put "### ls was not successful: ", success
put " command was: ", LSCommand
n := 0
return
end if
var fileName : string := tempFileName
open : fileNo, fileName, get
if fileNo <= 0 then
put "### Sorry, can't open: ", tempFileName, " fileNo: ", fileNo
n := 0
return
end if
n := 0
loop
exit when eof (fileNo)
n += 1
var entryName : string
get : fileNo, entryName : *
fileList (n) := entryName
if trace then
put n : 3, " ", fileList (n)
end if
end loop
close : fileNo
system ("rm " + tempFileName, success)
if success not= 0 then
put "Removal of temp file failed"
end if
end dirToList
proc fileToList (fileName : string, var words : array 1 .. * of str,
var wordsSize : int)
wordsSize := 0
var fileNo : int
openForGet (fileNo, fileName)
if fileNo <= 0 then
return
end if
loop
exit when eof (fileNo)
wordsSize += 1
var name : charstr
get : fileNo, name : *
words (wordsSize) := nameNum (name)
end loop
closeFile (fileNo, fileName)
end fileToList
proc putList (names : array 1 .. * of string (*), n : int)
for i : 1 .. n
put i : 3, " ", names (i)
end for
end putList
proc putNamList (names : array 1 .. * of str, n : int)
for i : 1 .. n
put i : 3, " ", numName (names (i))
end for
end putNamList
proc testDirToList (dirName : string)
put "Test listing directory: ", dirName
var names : array 1 .. 1500 of string (25)
var n : int
dirToList (dirName, flagDirectories, names, n, false)
put " ", n, " names"
putList (names, n)
end testDirToList
proc testFileToList (fileName : string)
put "Test reading file: ", fileName
var names : array 1 .. 500 of str
var n : int
fileToList (fileName, names, n)
put " ", n, " names"
putNamList (names, n)
end testFileToList
proc putDirRecursive (dirName : string, indent : string,
showContents : boolean)
var names : array 1 .. 1500 of string (25)
var n : int
dirToList (dirName, flagDirectories, names, n, false)
for i : 1 .. n
if showContents & names (i) = "contents" then
var contents : array 1 .. 500 of str
var m : int
fileToList (dirName + "/contents", contents, m)
for j : 1 .. m
const contentsJ := numName (contents (j))
put indent, contentsJ
if contentsJ = "" or index (contentsJ, " ") not= 0
then
put "#####", "Funny file name: '", dirName + "/" +
contentsJ, "'"
end if
end for
else
put indent, names (i)
if names (i) (*) = "/" then
putDirRecursive (dirName + "/" + names (i) (1 .. * - 1),
indent + " ", showContents)
end if
end if
end for
end putDirRecursive
fcn findCurrentDir (dirName : string) : string
var currentDir := ""
var i := length (dirName)
loop
exit when i = 0 or dirName (i) = "/"
currentDir := dirName (i) + currentDir
i -= 1
end loop
result currentDir
end findCurrentDir
proc listToFile (words : array 1 .. * of str, wordsSize : int,
fileName : string)
var fileNo : int
openForPut (fileNo, fileName)
if fileNo <= 0 then
return
end if
for i : 1 .. wordsSize
put : fileNo, numName(words (i))
end for
closeFile (fileNo, fileName)
end listToFile
fcn fixName (charName : string) : string
if length (charName) > 0 then
if charName (1) = "%" then
result "P_" + charName (2 .. *)
elsif charName (1) = "$" then
result "D_" + charName (2 .. *)
else
result charName
end if
end if
result charName
end fixName
fcn fixNameNumber (nameNumber : str) : string
const name := numName (nameNumber)
result fixName (name)
end fixNameNumber
fcn memberOfList (words : array 1 .. * of str, noWords : int,
word : str) : boolean
for i : 1 .. noWords
if words (i) = word then
result true
end if
end for
result false
end memberOfList
fcn isSubset (leftSet : shortList, leftSetSize : int,
rightSet : shortList, rightSetSize : int) : boolean
for i : 1 .. leftSetSize
if not memberOfList (rightSet, rightSetSize, leftSet (i)) then
result false
end if
end for
result true
end isSubset
proc insertIntoList (var words : array 1 .. * of str, var noWords : int,
word : str)
% Does not maintain alphabetic order
noWords += 1
if noWords > upper (words) then
put "#####Short list not long enough for insertion of ",
numName(word)
end if
words (noWords) := word
end insertIntoList
proc insertIntoListUnique (var words : array 1 .. * of str,
var noWords : int, word : str)
% Does not maintain alphabetic order
if not memberOfList (words, noWords, word) then
noWords += 1
if noWords > upper (words) then
put "***Sorry, list max size (", upper(words), " limits set. " +
" Can't add word '", word, "'"
quit
end if
words (noWords) := word
end if
end insertIntoListUnique
proc listAdd (var list1 : array 1 .. * of str, var list1Size : int,
list2 : array 1 .. * of str, list2Size : int)
for i : 1 .. list2Size
insertIntoListUnique (list1, list1Size, list2 (i))
end for
end listAdd
proc deleteFromList (var list : array 1 .. * of str,
var listSize : int, item : str)
var noDeleted := 0
for i : 1 .. listSize
if list (i) = item then
noDeleted += 1
else
if noDeleted > 0 then
list (i - noDeleted) := list (i)
end if
end if
end for
listSize -= noDeleted
end deleteFromList
proc listSubtract (var list1 : array 1 .. * of str, var list1Size : int,
list2 : array 1 .. * of str, list2Size : int)
for i : 1 .. list2Size
deleteFromList (list1, list1Size, list2 (i))
end for
end listSubtract
proc listIntersect (var list1 : shortList, %array 1 .. * of str,
var list1Size : int,
list2 : array 1 .. * of str, list2Size : int)
var tempList : shortList
var tempListSize := 0
for i : 1 .. list2Size
const member2 := list2 (i)
for j : 1 .. list1Size
if memberOfList (list1, list1Size, member2) then
insertIntoListUnique (tempList, tempListSize, member2)
end if
end for
end for
list1 := tempList
list1Size := tempListSize
end listIntersect
proc putListOnLine (list : array 1 .. * of string (*), listSize : int,
fixNames : boolean)
for i : 1 .. listSize
var name := list (i)
if fixNames then
name := fixNameNumber (nameNum(name))
end if
put name, " " ..
end for
put ""
end putListOnLine
proc putListWithCommas (fileNo : int,
list : array 1 .. * of str, listSize : int,
indent : string, fixNames : boolean)
for i : 1 .. listSize
var nameNumber : str := list (i)
var name := numName (nameNumber)
if fixNames then
name := fixNameNumber (nameNumber)
end if
if i < listSize then
put : fileNo, indent, name, ","
else
put : fileNo, indent, name
end if
end for
end putListWithCommas
proc putLabelledList (fileNo : int,
list : array 1 .. * of str, listSize : int,
listLabel : string, fixNames : boolean)
if listSize > 0 then
put : fileNo, INDENT, listLabel
putListWithCommas (fileNo, list, listSize, INDENT + INDENT,
fixNames)
end if
end putLabelledList
proc putLabelledListNoCommas (fileNo : int,
list : array 1 .. * of str, listSize : int,
listLabel : string, fixNames : boolean)
put : fileNo, listLabel
if listSize > 0 then
for i : 1 .. listSize
var nameNumber : str := list (i)
var name := numName (nameNumber)
if fixNames then
name := fixNameNumber (nameNumber)
end if
put : fileNo, INDENT, INDENT, name
end for
else
put INDENT, INDENT, "empty"
end if
end putLabelledListNoCommas
proc putCommentList (fileNo : int,
list : array 1 .. * of str, listSize : int,
fixNames : boolean)
for i : 1 .. listSize
var nameNumber := list (i)
var name := numName (nameNumber)
if fixNames then
name := fixName (name)
end if
put : fileNo, INDENT, "% ", name
end for
end putCommentList
fcn isPrefix (prefix, name : string) : boolean
const lenPrefix := length (prefix)
result length (name) >= lenPrefix and name (1 .. lenPrefix) = prefix
end isPrefix
fcn isSuffix (suffix, name : string) : boolean
const lenSuffix := length (suffix)
result length (name) >= lenSuffix and
name (* - lenSuffix + 1 .. *) = suffix
end isSuffix
% end List