[go: up one dir, main page]

Menu

[cb92cd]: / cc / ccfileut.pas  Maximize  Restore  History

Download this file

275 lines (252 with data), 7.2 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
{---------------------------------------------------------------------------}
{ }
{ P l a n e t s C o m m a n d C e n t e r }
{ }
{ 1995-2002 by Streu }
{ }
{---------------------------------------------------------------------------}
{ }
{ File-related Functions For PCC (and VPA) }
{ (This is a subset of another unit "FileUtil" I wrote) }
{ }
{---------------------------------------------------------------------------}
{$I switches.inc}
{$S-,D-}
UNIT ccFileUtil;
INTERFACE
{$IFDEF VPA}
USES VPACC;
{$ELSE}
USES Objects;
{$ENDIF}
{=== Filenames ===}
TYPE TFileName=STRING[79];
TExtension=STRING[3];
PROCEDURE AppendExt(VAR N:TFileName; X:TExtension; Force:BYTE);
FUNCTION IsPathSep(c:CHAR):BOOLEAN;
{=== File access ===}
TYPE TBuf=RECORD { Buffer for ReadStr & friends }
Len,Ptr:WORD;
B:ARRAY[0..127] OF BYTE;
END;
PROCEDURE ReadStr(H:PStream; VAR S:STRING; VAR Buf:TBuf);
FUNCTION ReadStrD(H:PStream; VAR S:STRING; VAR Buf:TBuf; delim:CHAR) : CHAR;
FUNCTION ReadBuf(H:PStream; VAR D; count:WORD; VAR Buf:TBuf) : WORD;
PROCEDURE InitBuf(H:PStream; VAR B:TBuf);
FUNCTION GetFAttr(Name:TFileName):WORD;
FUNCTION EraseFile(Name:TFileName):WORD;
FUNCTION RenameFile(old,new:TFileName):WORD;
{=== Constants for AppendExt ===}
CONST ae_Check = 0; { Check whether extension is needed }
ae_Force = 1; { Always replace }
{=== File attributes ===}
CONST fa_ReadOnly = 1; { read-only file }
fa_Hidden = 2; { hidden file }
fa_System = 4; { system file }
fa_VolumeID = 8; { Volume Label }
fa_Directory = 16; { Directory }
fa_Archive = 32; { File was modified }
fa_Sharable = 128; { <NetWare> }
{=== Seek directions [not used here] ===}
sd_FileStart = 0; { relative to file start }
sd_CurPos = 1; { relative to current position }
sd_FileEnd = 2; { relative to file end }
{=== File Status ===}
CONST FileError:WORD=0; { Last I/O error }
IMPLEMENTATION
{ true iff c is a path separator }
FUNCTION IsPathSep(c:CHAR):BOOLEAN;
BEGIN
IsPathSep:=(c='\') OR (c='/') OR (c=':');
END;
{
Append extension to file name.
N file name
X extension (without period)
Force replace existing extension if =ae_Force
}
PROCEDURE AppendExt(VAR N:TFileName; X:TExtension; Force:BYTE);
VAR i:INTEGER;
BEGIN
i:=Length(N);
WHILE (i<>0) AND (N[i]<>'.') AND NOT IsPathSep(N[i]) DO Dec(i);
IF (N[i]='.') AND (Force=ae_Force) THEN BEGIN
N[0]:=CHAR(i);
N:=N+X;
END ELSE
IF (i=0) OR (N[i]<>'.') THEN N:=N+'.'+X;
END;
{
Read a line from file.
H file
S output string
buf buffer to use (-> InitBuf)
delim delimiter. Reading stops either at LF or delimiter
Returns delimiter that caused end of read, or 0 (and FileError set to -1)
on EOF/error.
}
FUNCTION ReadStrD(H:PStream; VAR S:STRING; VAR Buf:TBuf; delim:CHAR):CHAR;
VAR c : CHAR;
amount : WORD;
BEGIN
S:='';
REPEAT
IF Buf.Ptr>=Buf.Len THEN BEGIN
amount := H^.PRead(Buf.B, Sizeof(Buf.B));
Buf.Len:=amount;
Buf.Ptr:=0;
IF (amount=0) OR (H^.Status<>0) THEN BEGIN
FileError:=$FFFF;
ReadStrD := #0;
Exit;
END;
END;
c := Chr(Buf.B[Buf.Ptr]);
Inc(Buf.Ptr);
IF (c = #10) OR (c = delim) THEN BEGIN
ReadStrD := c;
Exit;
END;
CASE c OF
#13:;
#26:BEGIN
FileError:=$FFFF;
ReadStrD := #0;
Exit;
END;
ELSE
{ avoid dependency to LowLevel: CatChar(S, c); }
IF Length(S)<>255 THEN BEGIN
Inc(S[0]);
S[Ord(S[0])] := c;
END;
END;
UNTIL FALSE;
END;
{
Read a line from file (normal reading with LF delimiter)
}
PROCEDURE ReadStr(H:PStream; VAR S:STRING; VAR Buf:TBuf);
BEGIN
ReadStrD(H, S, Buf, #10);
END;
{
Read a block from file, using the buffer (the buffer is not filled,
but its contents is used when possible).
H file
D data buffer
count amount to read
buf buffer
Returns number of bytes read.
}
FUNCTION ReadBuf(H:PStream; VAR D; count:WORD; VAR Buf:TBuf) : WORD;
VAR read : WORD;
n : WORD;
BEGIN
read := 0;
IF Buf.Ptr < Buf.Len THEN BEGIN
n := Buf.Len - Buf.Ptr;
IF n > count THEN n := count;
Move(Buf.B[Buf.Ptr], D, n);
Dec(count, n);
Inc(read, n);
Inc(Buf.Ptr, n);
ASM
mov ax, n
add d.word, ax
END;
END;
IF count > 0 THEN BEGIN
Inc(read, H^.PRead(D, count));
END;
ReadBuf := read;
END;
{
Initialize buffer before it can be used.
}
PROCEDURE InitBuf(H:PStream; VAR B:TBuf);
BEGIN
B.Ptr:=1;
B.Len:=0;
FileError:=0;
END;
{
Convert Pascal string into zero-terminated string. In-place operation,
string is not usable as pascal string afterwards.
}
PROCEDURE FrobFN(VAR fn:TFileName);
VAR len:BYTE;
BEGIN
len := Length(fn);
Move(fn[1], fn[0], len);
fn[len] := #0;
END;
{
Fetch attributes of specified file. Returns attributes, or zero and
FileError set on error
}
FUNCTION GetFAttr(Name:TFileName):WORD;
BEGIN
FrobFN(name);
ASM
push ds
push ss
pop ds
lea dx,Name
mov ax,4300h
int 21h
pop ds
jc @@Error
xor ax,ax
jmp @@OK
@@Error: xor cx,cx
@@OK: mov @Result,cx
mov FileError,ax
END;
END;
{
Erase a file. Returns zero on success, or error code and FileError set
on error.
}
FUNCTION EraseFile(Name:TFileName):WORD;
BEGIN
FrobFN(name);
ASM
push ds
push ss
pop ds
lea dx,Name
mov ah,41h
int 21h
pop ds
jc @@Error
xor ax,ax
@@Error: mov @Result,ax
mov FileError,ax
END;
END;
{
Rename a file. Returns zero on success or error code and FileError set
on error.
}
FUNCTION RenameFile(old,new:TFileName):WORD;
BEGIN
FrobFN(old);
FrobFN(new);
ASM
push ds
mov ax, ss
mov ds, ax
mov es, ax
lea dx, old
lea di, new
mov ah, 56h
int 21h
pop ds
jc @error
xor ax, ax
@error: mov @Result, ax
mov FileError, ax
END;
END;
END.