-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSTM.PAS
272 lines (217 loc) · 8.86 KB
/
STM.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
{$A+,B-,D+,E+,F-,G-,I+,L+,N-,O-,P-,Q-,R-,S-,T-,V-,X+,Y+}
UNIT STM;
{=============================================================================
Unit STreaMs
Provides buffered input/output for untyped files.
Purpose :
Accessing files with small records can slow down a program dramatically
due to overhead when chaining to the DOS INT 21h handler. By buffering
all reads and writes, thereby reducing the number of calls to the INT 21h
handler, we can gain a significant amount of speed increase at an
acceptable cost of increased memory use.
Files are split up into blocks of STM_BufSize bytes, these blocks are
called 'pages'. As needs predict, the stream will read the appropriate
page in memory, and copy the required data out of the buffer.
The STM unit strongly resembles the C 'FILE' type. STM is however
somewhat simpler since it provides no way for altering the buffer size
(other than changing the STM_BufSize constant and recompiling), and always
works by reading data on STM_BufSize boundaries.
=============================================================================}
{ßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßß}
INTERFACE
{ÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜ}
USES DOS;
CONST STM_BufSize = 4096; { Multiples of 2 are best }
TYPE OpenFlag = (NOCREATE, CREATE, CREATENEW);
TYPE STM_Buffer = ARRAY [0..STM_BufSize-1] OF BYTE;
STM_BufPtr = ^STM_Buffer;
STM_Error = (STM_OK, { No error }
STM_NOMEM, { Insufficient memory for buffer}
STM_OPENERROR, { Open/create did not succeed }
STM_SEEKERROR, { Error seeking to location }
STM_READERROR, { Error when reading }
STM_WRITEERROR); { Error when writing }
STREAM = RECORD
F : File; { FILE associated with stream }
BufPtr : STM_BufPtr; { Pointer to buffer for stream }
PageNr : LONGINT; { Page number we're working at }
PageNdx : LONGINT; { offset within page of current }
{ file pointer }
PageLen : LONGINT; { Actual length of buffer read }
{ from disk. will be equal to }
{ STM_BufSize except for the }
{ last page }
LastErr : STM_Error; { Last error reported on this }
{ stream }
Modified: BOOLEAN; { Has the page been written to? }
END;
PROCEDURE STM_Open (VAR S : STREAM; CONST FName : NameStr; CONST Flag : OpenFlag);
PROCEDURE STM_Create (VAR S : STREAM; CONST FName : NameStr);
PROCEDURE STM_Close (VAR S : STREAM);
PROCEDURE STM_Write (VAR S : STREAM; VAR DataBuf; DataLen : Word);
PROCEDURE STM_Read (VAR S : STREAM; VAR DataBuf; DataLen : Word);
PROCEDURE STM_Flush (VAR S : STREAM);
PROCEDURE STM_Goto (VAR S : STREAM; Location : Longint);
FUNCTION STM_GetPos (VAR S : STREAM) : LongInt;
FUNCTION STM_GetSize(VAR S : STREAM) : LongInt;
{ßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßßß}
IMPLEMENTATION
{ÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜÜ}
{ Open a stream.
{ Flag: NOCREATE : Open a stream but do not create a new file }
{ CREATE : Open a stream if possible, create a new file if opening }
{ did not succeed. }
{ CREATENEW: Create a new file }
PROCEDURE STM_Open (VAR S : STREAM; CONST FName : NameStr; CONST Flag : OpenFlag);
VAR IsOpen : BOOLEAN;
BEGIN
IsOpen:=FALSE;
FILLCHAR(S,sizeof(S),#0); { Initialize the stream record }
IF (MaxAvail<STM_BufSize) THEN BEGIN { Enough memory is available ? }
S.LastErr:=STM_NOMEM;
Exit;
END;
ASSIGN (S.F,FName); { Assign filename to file }
IF (Flag = NOCREATE) OR (Flag = CREATE) THEN BEGIN
{$i-}
Reset(S.F,1);
{$i+}
IsOpen:=IOResult=0;
END;
IF (NOT IsOpen) AND ((Flag = CREATE) OR (Flag = CREATENEW)) THEN BEGIN
{$i-}
Rewrite(S.F,1);
{$i+}
IsOpen:=IOResult=0;
END;
IF NOT IsOpen THEN BEGIN
S.LastErr:=STM_OPENERROR;
END
ELSE BEGIN
New(S.BufPtr);
S.PageNr:=-1; { Make sure we'll be actually reading }
{ a new page }
STM_Goto(S,0); { and position at start of file }
END;
END;
{ Create a stream }
PROCEDURE STM_Create (VAR S : STREAM; CONST FName : NameStr);
BEGIN
STM_Open(S,FName,CREATENEW);
END;
{ Close a stream }
PROCEDURE STM_Close (VAR S : STREAM);
BEGIN
STM_Flush(S); { Force any modified data to disk }
Close(S.F); { Close the file }
Dispose(S.BufPtr); { Free the memory }
END;
{ Write the buffered data to disk }
PROCEDURE STM_Flush (VAR S : STREAM);
VAR BytesWritten : Word;
BEGIN
IF (S.Modified) THEN BEGIN { Is there any modified data ? }
{$I-}
Seek(S.F, S.PageNr*STM_BufSize); { Seek to start of current page }
{$I+}
IF (IOResult<>0) THEN BEGIN
S.LastErr:=STM_SEEKERROR;
Exit;
END;
{ Write page }
BlockWrite(S.F, S.BufPtr^, S.PageLen, BytesWritten);
IF (BytesWritten<>S.PageLen) THEN BEGIN
S.LastErr:=STM_WRITEERROR;
Exit;
END;
S.Modified:=FALSE;
END;
S.LastErr:=STM_OK;
END;
{ Alter the current file pointer (similar to Pascal's Seek) }
PROCEDURE STM_Goto (VAR S : STREAM; Location : LONGINT);
VAR BytesRead : Word;
WantedPageNr : LongInt;
WantedPageNdx : LongInt;
BEGIN
WantedPageNr :=Location DIV STM_BufSize;
WantedPageNdx:=Location MOD STM_BufSize;
IF (WantedPageNr<>S.PageNr) THEN BEGIN
STM_Flush(S); { Force any modified data to disk }
IF (S.LastErr<>STM_OK) THEN Exit;
{$I-} { Seek to start of current page }
Seek(S.F, WantedPageNr*STM_BufSize);
{$I+}
IF (IOResult<>0) THEN BEGIN
S.LastErr:=STM_SEEKERROR;
Exit;
END;
{ Read page }
BlockRead(S.F, S.BufPtr^, STM_BufSize, BytesRead);
S.PageLen:=BytesRead;
IF (WantedPageNdx>S.PageLen) THEN BEGIN
S.LastErr:=STM_SEEKERROR;
Exit;
END;
S.PageNdx:=WantedPageNdx;
S.PageNr :=WantedPageNr;
END
ELSE
S.PageNdx:=WantedPageNdx;
S.LastErr:=STM_OK;
END;
{ Write some data to the stream, not necessarily writing to disk }
PROCEDURE STM_Write (VAR S : STREAM; VAR DataBuf; DataLen : Word);
VAR BytePtr : ^Byte;
Len : Word;
BEGIN
IF (S.LastErr<>STM_OK) THEN RunError(5);
BytePtr:=@DataBuf;
WHILE (DataLen>0) AND (S.LastErr=STM_OK) DO BEGIN
IF (S.PageNdx+DataLen>STM_BufSize) THEN
Len:=STM_BufSize-S.PageNdx
ELSE
Len:=DataLen;
Move(BytePtr^,S.BufPtr^[S.PageNdx],Len);
S.Modified:=TRUE;
Dec(DataLen,Len);
Inc(BytePtr,Len);
Inc(S.PageNdx,Len);
IF (S.PageLen<S.PageNdx) THEN
S.PageLen:=S.PageNdx;
IF (DataLen>0) OR (S.PageNdx=STM_BufSize) THEN
STM_Goto(S,(S.PageNr+1)*STM_BufSize);
END;
END;
{ Read some data from the stream }
PROCEDURE STM_Read (VAR S : STREAM; VAR DataBuf; DataLen : Word);
VAR BytePtr : ^Byte;
Len : Word;
BEGIN
IF (S.LastErr<>STM_OK) THEN RunError(5);
BytePtr:=@DataBuf;
WHILE (DataLen>0) AND (S.LastErr=STM_OK) DO BEGIN
IF (S.PageNdx+DataLen>STM_BufSize) THEN
Len:=STM_BufSize-S.PageNdx
ELSE
Len:=DataLen;
Move(S.BufPtr^[S.PageNdx],BytePtr^,Len);
Dec(DataLen,Len);
Inc(BytePtr,Len);
Inc(S.PageNdx,Len);
IF (DataLen>0) OR (S.PageNdx=STM_BufSize) THEN
STM_Goto(S,(S.PageNr+1)*STM_BufSize);
END;
END;
{ Return the current file pointer (similar to Pascal's GetPos) }
FUNCTION STM_GetPos (VAR S : STREAM) : LongInt;
BEGIN
STM_GetPos := S.PageNr*STM_BufSize + S.PageNdx;
END;
{ Return the current size of the file }
FUNCTION STM_GetSize(VAR S : STREAM) : LongInt;
BEGIN
STM_Flush(S); { Force data to disk first }
STM_GetSize:= FileSize(S.F);
END;
END.