10 // does filename have one of ".wad", ".pk3", ".zip" extensions?
11 function hasWadExtension (fn
: AnsiString): Boolean;
13 // does filepath have ".XXX:\" in it?
14 function isWadPath (fn
: AnsiString): Boolean;
16 // adds ".wad" extension if filename doesn't have one of ".wad", ".pk3", ".zip"
17 function addWadExtension (fn
: AnsiString): AnsiString;
19 // convert number to strig with nice commas
20 function Int64ToStrComma (i
: Int64): AnsiString;
22 function UpCase1251 (ch
: Char): Char;
24 // `true` if strings are equal; ignoring case for cp1251
25 function StrEquCI1251 (const s0
, s1
: AnsiString): Boolean;
27 function utf8Valid (const s
: AnsiString): Boolean;
29 function utf8to1251 (s
: AnsiString): AnsiString;
31 // `pathname` will be modified if path is valid
32 // `lastIsDir` should be `true` if we are searching for directory
33 // nobody cares about shitdoze, so i'll use the same code path for it
34 function findFileCI (var pathname
: AnsiString; lastIsDir
: Boolean=false): Boolean;
37 function openDiskFileRO (pathname
: AnsiString): TStream
;
38 function createDiskFile (pathname
: AnsiString): TStream
;
41 procedure writeInt (st
: TStream
; v
: Byte); overload
;
42 procedure writeInt (st
: TStream
; v
: ShortInt); overload
;
43 procedure writeInt (st
: TStream
; v
: Word); overload
;
44 procedure writeInt (st
: TStream
; v
: SmallInt); overload
;
45 procedure writeInt (st
: TStream
; v
: LongWord); overload
;
46 procedure writeInt (st
: TStream
; v
: LongInt); overload
;
47 procedure writeInt (st
: TStream
; v
: Int64); overload
;
48 procedure writeInt (st
: TStream
; v
: UInt64); overload
;
50 function readByte (st
: TStream
): Byte;
51 function readShortInt (st
: TStream
): ShortInt;
52 function readWord (st
: TStream
): Word;
53 function readSmallInt (st
: TStream
): SmallInt;
54 function readLongWord (st
: TStream
): LongWord;
55 function readLongInt (st
: TStream
): LongInt;
56 function readInt64 (st
: TStream
): Int64;
57 function readUInt64 (st
: TStream
): UInt64;
60 procedure writeIntBE (st
: TStream
; v
: Byte); overload
;
61 procedure writeIntBE (st
: TStream
; v
: ShortInt); overload
;
62 procedure writeIntBE (st
: TStream
; v
: Word); overload
;
63 procedure writeIntBE (st
: TStream
; v
: SmallInt); overload
;
64 procedure writeIntBE (st
: TStream
; v
: LongWord); overload
;
65 procedure writeIntBE (st
: TStream
; v
: LongInt); overload
;
66 procedure writeIntBE (st
: TStream
; v
: Int64); overload
;
67 procedure writeIntBE (st
: TStream
; v
: UInt64); overload
;
69 function readByteBE (st
: TStream
): Byte;
70 function readShortIntBE (st
: TStream
): ShortInt;
71 function readWordBE (st
: TStream
): Word;
72 function readSmallIntBE (st
: TStream
): SmallInt;
73 function readLongWordBE (st
: TStream
): LongWord;
74 function readLongIntBE (st
: TStream
): LongInt;
75 function readInt64BE (st
: TStream
): Int64;
76 function readUInt64BE (st
: TStream
): UInt64;
82 function hasWadExtension (fn
: AnsiString): Boolean;
84 fn
:= ExtractFileExt(fn
);
85 result
:= StrEquCI1251(fn
, '.wad') or StrEquCI1251(fn
, '.pk3') or StrEquCI1251(fn
, '.zip');
89 function addWadExtension (fn
: AnsiString): AnsiString;
92 if not hasWadExtension(result
) then result
:= result
+'.wad';
96 function isWadPath (fn
: AnsiString): Boolean;
105 if (p
= 0) or (length(fn
)-p
< 1) then break
;
106 if (p
-4 > 1) and (fn
[p
-4] = '.') and ((fn
[p
+1] = '\') or (fn
[p
+1] = '/')) then
108 s
:= Copy(fn
, p
-4, 4);
109 if StrEquCI1251(s
, '.wad') or StrEquCI1251(s
, '.pk3') or StrEquCI1251(s
, '.zip') then
120 function Int64ToStrComma (i
: Int64): AnsiString;
125 f
:= Length(result
)+1;
128 Dec(f
, 3); Insert(',', result
, f
);
133 function UpCase1251 (ch
: Char): Char;
137 if (ch
>= 'a') and (ch
<= 'z') then Dec(ch
, 32);
141 if (ch
>= #224) and (ch
<= #255) then
148 #184, #186, #191: Dec(ch
, 16);
157 function StrEquCI1251 (const s0
, s1
: AnsiString): Boolean;
162 if length(s0
) <> length(s1
) then exit
;
163 for i
:= 1 to length(s0
) do if UpCase1251(s0
[i
]) <> UpCase1251(s1
[i
]) then exit
;
168 // ////////////////////////////////////////////////////////////////////////// //
172 function utf8CodeLen (ch
: Word): Integer;
174 if ch
< $80 then begin result
:= 1; exit
; end;
175 if (ch
and $FE) = $FC then begin result
:= 6; exit
; end;
176 if (ch
and $FC) = $F8 then begin result
:= 5; exit
; end;
177 if (ch
and $F8) = $F0 then begin result
:= 4; exit
; end;
178 if (ch
and $F0) = $E0 then begin result
:= 3; exit
; end;
179 if (ch
and $E0) = $C0 then begin result
:= 2; exit
; end;
180 result
:= -1; // invalid
184 function utf8Valid (const s
: AnsiString): Boolean;
190 while pos
<= length(s
) do
192 len
:= utf8CodeLen(Byte(s
[pos
]));
193 if len
< 1 then exit
; // invalid sequence start
194 if pos
+len
-1 > length(s
) then exit
; // out of chars in string
197 // check other sequence bytes
200 if (Byte(s
[pos
]) and $C0) <> $80 then exit
;
209 // ////////////////////////////////////////////////////////////////////////// //
211 uni2wint
: array [128..255] of Word = (
212 $0402,$0403,$201A,$0453,$201E,$2026,$2020,$2021,$20AC,$2030,$0409,$2039,$040A,$040C,$040B,$040F,
213 $0452,$2018,$2019,$201C,$201D,$2022,$2013,$2014,$003F,$2122,$0459,$203A,$045A,$045C,$045B,$045F,
214 $00A0,$040E,$045E,$0408,$00A4,$0490,$00A6,$00A7,$0401,$00A9,$0404,$00AB,$00AC,$00AD,$00AE,$0407,
215 $00B0,$00B1,$0406,$0456,$0491,$00B5,$00B6,$00B7,$0451,$2116,$0454,$00BB,$0458,$0405,$0455,$0457,
216 $0410,$0411,$0412,$0413,$0414,$0415,$0416,$0417,$0418,$0419,$041A,$041B,$041C,$041D,$041E,$041F,
217 $0420,$0421,$0422,$0423,$0424,$0425,$0426,$0427,$0428,$0429,$042A,$042B,$042C,$042D,$042E,$042F,
218 $0430,$0431,$0432,$0433,$0434,$0435,$0436,$0437,$0438,$0439,$043A,$043B,$043C,$043D,$043E,$043F,
219 $0440,$0441,$0442,$0443,$0444,$0445,$0446,$0447,$0448,$0449,$044A,$044B,$044C,$044D,$044E,$044F
223 function decodeUtf8Char (s
: AnsiString; var pos
: Integer): char;
227 (* The following encodings are valid, except for the 5 and 6 byte
231 * 1110xxxx 10xxxxxx 10xxxxxx
232 * 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
233 * 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
234 * 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
237 if pos
> length(s
) then exit
;
241 if b
< $80 then begin result
:= char(b
); exit
; end;
243 // mask out unused bits
244 if (b
and $FE) = $FC then b
:= b
and $01
245 else if (b
and $FC) = $F8 then b
:= b
and $03
246 else if (b
and $F8) = $F0 then b
:= b
and $07
247 else if (b
and $F0) = $E0 then b
:= b
and $0F
248 else if (b
and $E0) = $C0 then b
:= b
and $1F
249 else exit
; // invalid utf8
252 while pos
<= length(s
) do
255 if (c
and $C0) <> $80 then break
; // no more
257 b
:= b
or (c
and $3F);
262 for c
:= 128 to 255 do if uni2wint
[c
] = b
then begin result
:= char(c
and $FF); exit
; end;
267 function utf8to1251 (s
: AnsiString): AnsiString;
271 if not utf8Valid(s
) then begin result
:= s
; exit
; end;
273 while pos
<= length(s
) do
275 if Byte(s
[pos
]) >= $80 then break
;
278 if pos
> length(s
) then begin result
:= s
; exit
; end; // nothing to do here
281 while pos
<= length(s
) do result
:= result
+decodeUtf8Char(s
, pos
);
285 // ////////////////////////////////////////////////////////////////////////// //
286 // `pathname` will be modified if path is valid
287 // `lastIsDir` should be `true` if we are searching for directory
288 // nobody cares about shitdoze, so i'll use the same code path for it
289 function findFileCI (var pathname
: AnsiString; lastIsDir
: Boolean=false): Boolean;
293 newname
: AnsiString = '';
300 result
:= (length(npt
) > 0);
301 if (length(npt
) > 0) and ((npt
[1] = '/') or (npt
[1] = '\')) then newname
:= '/';
302 while length(npt
) > 0 do
304 // remove trailing slashes
305 while (length(npt
) > 0) and ((npt
[1] = '/') or (npt
[1] = '\')) do Delete(npt
, 1, 1);
306 if length(npt
) = 0 then break
;
309 while (length(npt
) > 0) and (npt
[1] <> '/') and (npt
[1] <> '\') do
311 curname
:= curname
+npt
[1];
314 // remove trailing slashes again
315 while (length(npt
) > 0) and ((npt
[1] = '/') or (npt
[1] = '\')) do Delete(npt
, 1, 1);
316 wantdir
:= lastIsDir
or (length(npt
) > 0); // do we want directory here?
317 //writeln(Format('npt=[%s]; newname=[%s]; curname=[%s]; wantdir=%d', [npt, newname, curname, Integer(wantdir)]));
318 // try the easiest case first
319 attr
:= FileGetAttr(newname
+curname
);
322 if wantdir
= ((attr
and faDirectory
) <> 0) then
325 newname
:= newname
+curname
;
326 if wantdir
then newname
:= newname
+'/';
330 //writeln(Format('npt=[%s]; newname=[%s]; curname=[%s]; wantdir=%d', [npt, newname, curname, Integer(wantdir)]));
331 // alas, either not found, or invalid attributes
334 if FindFirst(newname
+'*', faAnyFile
, sr
) = 0 then
336 if (wantdir
= ((sr
.attr
and faDirectory
) <> 0)) and StrEquCI1251(sr
.name
, curname
) then
339 newname
:= newname
+sr
.name
;
340 if wantdir
then newname
:= newname
+'/';
344 until FindNext(sr
) <> 0;
348 if not foundher
then begin newname
:= ''; result
:= false; break
; end;
350 if result
then pathname
:= newname
;
354 function openDiskFileRO (pathname
: AnsiString): TStream
;
356 if not findFileCI(pathname
) then raise Exception
.Create('can''t open file "'+pathname
+'"');
357 result
:= TFileStream
.Create(pathname
, fmOpenRead
or {fmShareDenyWrite}fmShareDenyNone
);
360 function createDiskFile (pathname
: AnsiString): TStream
;
364 path
:= ExtractFilePath(pathname
);
365 if length(path
) > 0 then
367 if not findFileCI(path
, true) then raise Exception
.Create('can''t create file "'+pathname
+'"');
369 result
:= TFileStream
.Create(path
+ExtractFileName(pathname
), fmCreate
);
373 procedure writeIntegerLE (st
: TStream
; vp
: Pointer; size
: Integer);
374 {$IFDEF ENDIAN_LITTLE}
376 st
.writeBuffer(vp
^, size
);
382 p
:= PByte(vp
)+size
-1;
385 st
.writeBuffer(p
^, 1);
392 procedure writeIntegerBE (st
: TStream
; vp
: Pointer; size
: Integer);
393 {$IFDEF ENDIAN_LITTLE}
397 p
:= PByte(vp
)+size
-1;
400 st
.writeBuffer(p
^, 1);
407 st
.writeBuffer(vp
^, size
);
411 procedure writeInt (st
: TStream
; v
: Byte); overload
; begin writeIntegerLE(st
, @v
, 1); end;
412 procedure writeInt (st
: TStream
; v
: ShortInt); overload
; begin writeIntegerLE(st
, @v
, 1); end;
413 procedure writeInt (st
: TStream
; v
: Word); overload
; begin writeIntegerLE(st
, @v
, 2); end;
414 procedure writeInt (st
: TStream
; v
: SmallInt); overload
; begin writeIntegerLE(st
, @v
, 2); end;
415 procedure writeInt (st
: TStream
; v
: LongWord); overload
; begin writeIntegerLE(st
, @v
, 4); end;
416 procedure writeInt (st
: TStream
; v
: LongInt); overload
; begin writeIntegerLE(st
, @v
, 4); end;
417 procedure writeInt (st
: TStream
; v
: Int64); overload
; begin writeIntegerLE(st
, @v
, 8); end;
418 procedure writeInt (st
: TStream
; v
: UInt64); overload
; begin writeIntegerLE(st
, @v
, 8); end;
420 procedure writeIntBE (st
: TStream
; v
: Byte); overload
; begin writeIntegerBE(st
, @v
, 1); end;
421 procedure writeIntBE (st
: TStream
; v
: ShortInt); overload
; begin writeIntegerBE(st
, @v
, 1); end;
422 procedure writeIntBE (st
: TStream
; v
: Word); overload
; begin writeIntegerBE(st
, @v
, 2); end;
423 procedure writeIntBE (st
: TStream
; v
: SmallInt); overload
; begin writeIntegerBE(st
, @v
, 2); end;
424 procedure writeIntBE (st
: TStream
; v
: LongWord); overload
; begin writeIntegerBE(st
, @v
, 4); end;
425 procedure writeIntBE (st
: TStream
; v
: LongInt); overload
; begin writeIntegerBE(st
, @v
, 4); end;
426 procedure writeIntBE (st
: TStream
; v
: Int64); overload
; begin writeIntegerBE(st
, @v
, 8); end;
427 procedure writeIntBE (st
: TStream
; v
: UInt64); overload
; begin writeIntegerBE(st
, @v
, 8); end;
430 procedure readIntegerLE (st
: TStream
; vp
: Pointer; size
: Integer);
431 {$IFDEF ENDIAN_LITTLE}
433 st
.readBuffer(vp
^, size
);
439 p
:= PByte(vp
)+size
-1;
442 st
.readBuffer(p
^, 1);
449 procedure readIntegerBE (st
: TStream
; vp
: Pointer; size
: Integer);
450 {$IFDEF ENDIAN_LITTLE}
454 p
:= PByte(vp
)+size
-1;
457 st
.readBuffer(p
^, 1);
464 st
.readBuffer(vp
^, size
);
468 function readByte (st
: TStream
): Byte; begin readIntegerLE(st
, @result
, 1); end;
469 function readShortInt (st
: TStream
): ShortInt; begin readIntegerLE(st
, @result
, 1); end;
470 function readWord (st
: TStream
): Word; begin readIntegerLE(st
, @result
, 2); end;
471 function readSmallInt (st
: TStream
): SmallInt; begin readIntegerLE(st
, @result
, 2); end;
472 function readLongWord (st
: TStream
): LongWord; begin readIntegerLE(st
, @result
, 4); end;
473 function readLongInt (st
: TStream
): LongInt; begin readIntegerLE(st
, @result
, 4); end;
474 function readInt64 (st
: TStream
): Int64; begin readIntegerLE(st
, @result
, 8); end;
475 function readUInt64 (st
: TStream
): UInt64; begin readIntegerLE(st
, @result
, 8); end;
477 function readByteBE (st
: TStream
): Byte; begin readIntegerBE(st
, @result
, 1); end;
478 function readShortIntBE (st
: TStream
): ShortInt; begin readIntegerBE(st
, @result
, 1); end;
479 function readWordBE (st
: TStream
): Word; begin readIntegerBE(st
, @result
, 2); end;
480 function readSmallIntBE (st
: TStream
): SmallInt; begin readIntegerBE(st
, @result
, 2); end;
481 function readLongWordBE (st
: TStream
): LongWord; begin readIntegerBE(st
, @result
, 4); end;
482 function readLongIntBE (st
: TStream
): LongInt; begin readIntegerBE(st
, @result
, 4); end;
483 function readInt64BE (st
: TStream
): Int64; begin readIntegerBE(st
, @result
, 8); end;
484 function readUInt64BE (st
: TStream
): UInt64; begin readIntegerBE(st
, @result
, 8); end;