-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfilemgr.i
More file actions
168 lines (136 loc) · 5.35 KB
/
filemgr.i
File metadata and controls
168 lines (136 loc) · 5.35 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
% This file manager module was built to allow Grok to support
% loops. Each command file is "cached" into this module. A loop
% returns to its beginning by "seeking" back there... which is
% accomplished by resetting a "nextLineNo" pointer back to the
% beginning.
% When a file is opened, it is cached here. In case of a future
% open of the same file, the same cached lines are used. The
% cache is never flushed.
% Ric Holt Jan 99
module fileMgr
import var commandLinesMax
export Open, Close, Get, Seek, Tell, EOF
const EOFString := "### EOF ###"
const commandLineSize := 200
type commandLineType : string (commandLineSize)
const nullFileLocator := - 1
const maxCommandFiles := 50
var commandLines :
% flexible
array 1 .. commandLinesMax of commandLineType
var noCommandLines := 0
% File record statuses:
const freeRecord := 0
const openFile := 1
const closedFile := 2
var fileRecord : array 1 .. maxCommandFiles of
record
fileName : string
status : int
firstLineNo : int
lastLineNo : int
nextLineNo : int
end record
for i : 1 .. maxCommandFiles
fileRecord (i).status := freeRecord
end for
procedure increaseCommandLines
put "Grok increases max command lines from ", commandLinesMax,
" to " ..
const linesAddOn := 2000
commandLinesMax += linesAddOn
put commandLinesMax
% new commandLines, commandLinesMax
end increaseCommandLines
fcn findFileRecord (fileName : string) : int
for i : 1 .. maxCommandFiles
if fileRecord (i).status = closedFile &
fileRecord (i).fileName = fileName then
result i
end if
end for
result nullFileLocator
end findFileRecord
fcn findFreeFileRecord : int
for i : 1 .. maxCommandFiles
if fileRecord (i).status = freeRecord then
result i
end if
end for
result nullFileLocator
end findFreeFileRecord
procedure Open (var commandFileNo : int, commandFileName : string)
var oldFileIndex := findFileRecord (commandFileName)
var fileIndex : int
if oldFileIndex = nullFileLocator then
var fileNo : int
open : fileNo, commandFileName, get
if fileNo <= 0 then
commandFileNo := - 1
return
end if
fileIndex := findFreeFileRecord
if fileIndex < 0 then
put "***Sorry, no room for command file: ", commandFileName
commandFileNo := - 1
return
end if
fileRecord (fileIndex).fileName := commandFileName
fileRecord (fileIndex).status := openFile
fileRecord (fileIndex).firstLineNo := noCommandLines + 1
fileRecord (fileIndex).nextLineNo := noCommandLines + 1
loop
exit when eof (fileNo)
if noCommandLines = commandLinesMax then
increaseCommandLines
end if
noCommandLines += 1
get : fileNo, commandLines (noCommandLines) : *
end loop
close : fileNo
fileRecord (fileIndex).lastLineNo := noCommandLines
else
fileIndex := oldFileIndex
if fileRecord (fileIndex).status = openFile then
put "***Error: Reopened file: ",
fileRecord (fileIndex).fileName
return
end if
assert fileRecord (fileIndex).status = closedFile
fileRecord (fileIndex).status := openFile
% Rewind file to beginning
fileRecord (fileIndex).nextLineNo :=
fileRecord (fileIndex).firstLineNo
end if
commandFileNo := fileIndex
end Open
procedure Close (commandFileLocator : int)
pre commandFileLocator >= 1 & commandFileLocator <= maxCommandFiles
assert fileRecord (commandFileLocator).status = openFile
% Closed during "Open" --- close : commandFileLocator
fileRecord (commandFileLocator).status := closedFile
end Close
procedure Get (commandFileLocator : int, var line : string)
pre commandFileLocator >= 1 & commandFileLocator <= maxCommandFiles
const nextLineNo := fileRecord (commandFileLocator).nextLineNo
if nextLineNo <= fileRecord (commandFileLocator).lastLineNo then
line := commandLines (nextLineNo)
fileRecord (commandFileLocator).nextLineNo := nextLineNo + 1
else
line := EOFString
end if
end Get
procedure Tell (commandFileLocator : int, var lineNo : int)
pre commandFileLocator >= 1 & commandFileLocator <= maxCommandFiles
lineNo := fileRecord (commandFileLocator).nextLineNo
end Tell
procedure Seek (commandFileLocator : int, lineNo : int)
pre commandFileLocator >= 1 & commandFileLocator <= maxCommandFiles
fileRecord (commandFileLocator).nextLineNo := lineNo
end Seek
fcn EOF (commandFileLocator : int) : boolean
pre commandFileLocator >= 1 & commandFileLocator <= maxCommandFiles
result fileRecord (commandFileLocator).nextLineNo >
fileRecord (commandFileLocator).lastLineNo
end EOF
end fileMgr