1 (* Copyright (C) DooM 2D:Forever Developers
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, either version 3 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 {$INCLUDE a_modes.inc}
25 // ////////////////////////////////////////////////////////////////////////// //
27 TUtf8DecoderFast
= packed record
29 const Replacement
= $FFFD; // replacement char for invalid unicode
37 codepoint
: LongWord; // decoded codepoint (valid only when decoder is in "complete" state)
40 constructor Create (v
: Boolean{fuck you, fpc});
42 procedure reset (); inline;
44 function complete (): Boolean; inline; // is current character complete? take `codepoint` then
45 function invalid (): Boolean; inline;
46 function completeOrInvalid (): Boolean; inline;
48 // process one byte, return `true` if codepoint is ready
49 function decode (b
: Byte): Boolean; inline; overload
;
50 function decode (c
: AnsiChar): Boolean; inline; overload
;
54 // ////////////////////////////////////////////////////////////////////////// //
55 // does filename have one of ".wad", ".pk3", ".zip" extensions?
56 function hasWadExtension (fn
: AnsiString): Boolean;
58 // does filepath have ".XXX:\" in it?
59 function isWadPath (fn
: AnsiString): Boolean;
61 // adds ".wad" extension if filename doesn't have one of ".wad", ".pk3", ".zip"
62 function addWadExtension (fn
: AnsiString): AnsiString;
64 // convert number to strig with nice commas
65 function Int64ToStrComma (i
: Int64): AnsiString;
67 function UpCase1251 (ch
: Char): Char;
68 function LoCase1251 (ch
: Char): Char;
70 // `true` if strings are equal; ignoring case for cp1251
71 function StrEquCI1251 (const s0
, s1
: AnsiString): Boolean;
73 function utf8Valid (const s
: AnsiString): Boolean;
75 function utf8to1251 (s
: AnsiString): AnsiString;
77 // `pathname` will be modified if path is valid
78 // `lastIsDir` should be `true` if we are searching for directory
79 // nobody cares about shitdoze, so i'll use the same code path for it
80 function findFileCI (var pathname
: AnsiString; lastIsDir
: Boolean=false): Boolean;
83 function openDiskFileRO (pathname
: AnsiString): TStream
;
84 function createDiskFile (pathname
: AnsiString): TStream
;
87 procedure writeInt (st
: TStream
; v
: Byte); overload
;
88 procedure writeInt (st
: TStream
; v
: ShortInt); overload
;
89 procedure writeInt (st
: TStream
; v
: Word); overload
;
90 procedure writeInt (st
: TStream
; v
: SmallInt); overload
;
91 procedure writeInt (st
: TStream
; v
: LongWord); overload
;
92 procedure writeInt (st
: TStream
; v
: LongInt); overload
;
93 procedure writeInt (st
: TStream
; v
: Int64); overload
;
94 procedure writeInt (st
: TStream
; v
: UInt64); overload
;
96 function readByte (st
: TStream
): Byte;
97 function readShortInt (st
: TStream
): ShortInt;
98 function readWord (st
: TStream
): Word;
99 function readSmallInt (st
: TStream
): SmallInt;
100 function readLongWord (st
: TStream
): LongWord;
101 function readLongInt (st
: TStream
): LongInt;
102 function readInt64 (st
: TStream
): Int64;
103 function readUInt64 (st
: TStream
): UInt64;
106 procedure writeIntBE (st
: TStream
; v
: Byte); overload
;
107 procedure writeIntBE (st
: TStream
; v
: ShortInt); overload
;
108 procedure writeIntBE (st
: TStream
; v
: Word); overload
;
109 procedure writeIntBE (st
: TStream
; v
: SmallInt); overload
;
110 procedure writeIntBE (st
: TStream
; v
: LongWord); overload
;
111 procedure writeIntBE (st
: TStream
; v
: LongInt); overload
;
112 procedure writeIntBE (st
: TStream
; v
: Int64); overload
;
113 procedure writeIntBE (st
: TStream
; v
: UInt64); overload
;
115 function readByteBE (st
: TStream
): Byte;
116 function readShortIntBE (st
: TStream
): ShortInt;
117 function readWordBE (st
: TStream
): Word;
118 function readSmallIntBE (st
: TStream
): SmallInt;
119 function readLongWordBE (st
: TStream
): LongWord;
120 function readLongIntBE (st
: TStream
): LongInt;
121 function readInt64BE (st
: TStream
): Int64;
122 function readUInt64BE (st
: TStream
): UInt64;
126 TFormatStrFCallback
= procedure (constref buf
; len
: SizeUInt
);
128 // returns formatted string if `writerCB` is `nil`, empty string otherwise
129 function formatstrf (const fmt
: AnsiString; args
: array of const; writerCB
: TFormatStrFCallback
=nil): AnsiString;
131 function wchar2win (wc
: WideChar): AnsiChar; inline;
132 function utf2win (const s
: AnsiString): AnsiString;
133 function digitInBase (ch
: AnsiChar; base
: Integer): Integer;
135 // returns string in single or double quotes
136 // single quotes supports only pascal-style '' for single quote char
137 // double quotes supports c-style escapes
138 // function will select quote mode automatically
139 function quoteStr (const s
: AnsiString): AnsiString;
143 generic TSimpleList
<ItemT
> = class
145 type PItemT
= ^ItemT
;
155 constructor Create (aitems
: PItemT
; acount
: Integer);
156 function MoveNext
: Boolean;
157 function getCurrent (): ItemT
;
158 property Current
: ItemT read getCurrent
;
162 mItems
: array of ItemT
;
163 mCount
: Integer; // can be less than `mItems` size
166 function getAt (idx
: Integer): ItemT
; inline;
169 constructor Create ();
170 destructor Destroy (); override;
172 function GetEnumerator (): TEnumerator
;
174 procedure reset (); inline; // won't resize `mItems`
175 procedure clear (); inline;
177 procedure append (constref it
: ItemT
); inline;
180 property count
: Integer read mCount
;
181 property at
[idx
: Integer]: ItemT read getAt
; default
;
188 // ////////////////////////////////////////////////////////////////////////// //
189 constructor TSimpleList
.TEnumerator
.Create (aitems
: PItemT
; acount
: Integer);
196 function TSimpleList
.TEnumerator
.MoveNext
: Boolean;
199 result
:= (mCurrent
< mCount
);
202 function TSimpleList
.TEnumerator
.getCurrent (): ItemT
;
204 result
:= mItems
[mCurrent
];
208 // ////////////////////////////////////////////////////////////////////////// //
209 constructor TSimpleList
.Create ();
216 destructor TSimpleList
.Destroy ();
223 function TSimpleList
.GetEnumerator (): TEnumerator
;
225 if (Length(mItems
) > 0) then result
:= TEnumerator
.Create(@mItems
[0], mCount
)
226 else result
:= TEnumerator
.Create(nil, -1);
230 procedure TSimpleList
.reset (); inline;
236 procedure TSimpleList
.clear (); inline;
243 function TSimpleList
.getAt (idx
: Integer): ItemT
; inline;
245 if (idx
>= 0) and (idx
< mCount
) then result
:= mItems
[idx
] else result
:= Default(ItemT
);
249 procedure TSimpleList
.append (constref it
: ItemT
); inline;
251 if (mCount
= Length(mItems
)) then
253 if (mCount
= 0) then SetLength(mItems
, 128) else SetLength(mItems
, mCount
*2);
255 mItems
[mCount
] := it
;
260 // ////////////////////////////////////////////////////////////////////////// //
262 wc2shitmap
: array[0..65535] of AnsiChar;
263 wc2shitmapInited
: Boolean = false;
266 // ////////////////////////////////////////////////////////////////////////// //
267 procedure initShitMap ();
269 cp1251
: array[0..127] of Word = (
270 $0402,$0403,$201A,$0453,$201E,$2026,$2020,$2021,$20AC,$2030,$0409,$2039,$040A,$040C,$040B,$040F,
271 $0452,$2018,$2019,$201C,$201D,$2022,$2013,$2014,$003F,$2122,$0459,$203A,$045A,$045C,$045B,$045F,
272 $00A0,$040E,$045E,$0408,$00A4,$0490,$00A6,$00A7,$0401,$00A9,$0404,$00AB,$00AC,$00AD,$00AE,$0407,
273 $00B0,$00B1,$0406,$0456,$0491,$00B5,$00B6,$00B7,$0451,$2116,$0454,$00BB,$0458,$0405,$0455,$0457,
274 $0410,$0411,$0412,$0413,$0414,$0415,$0416,$0417,$0418,$0419,$041A,$041B,$041C,$041D,$041E,$041F,
275 $0420,$0421,$0422,$0423,$0424,$0425,$0426,$0427,$0428,$0429,$042A,$042B,$042C,$042D,$042E,$042F,
276 $0430,$0431,$0432,$0433,$0434,$0435,$0436,$0437,$0438,$0439,$043A,$043B,$043C,$043D,$043E,$043F,
277 $0440,$0441,$0442,$0443,$0444,$0445,$0446,$0447,$0448,$0449,$044A,$044B,$044C,$044D,$044E,$044F
282 for f
:= 0 to High(wc2shitmap
) do wc2shitmap
[f
] := '?';
283 for f
:= 0 to 127 do wc2shitmap
[f
] := AnsiChar(f
);
284 for f
:= 0 to 127 do wc2shitmap
[cp1251
[f
]] := AnsiChar(f
+128);
285 wc2shitmapInited
:= true;
289 // ////////////////////////////////////////////////////////////////////////// //
290 // fast state-machine based UTF-8 decoder; using 8 bytes of memory
291 // code points from invalid range will never be valid, this is the property of the state machine
293 // see http://bjoern.hoehrmann.de/utf-8/decoder/dfa/
294 utf8dfa
: array[0..$16c-1] of Byte = (
295 // maps bytes to character classes
296 $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00, // 00-0f
297 $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00, // 10-1f
298 $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00, // 20-2f
299 $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00, // 30-3f
300 $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00, // 40-4f
301 $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00, // 50-5f
302 $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00, // 60-6f
303 $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00, // 70-7f
304 $01,$01,$01,$01,$01,$01,$01,$01,$01,$01,$01,$01,$01,$01,$01,$01, // 80-8f
305 $09,$09,$09,$09,$09,$09,$09,$09,$09,$09,$09,$09,$09,$09,$09,$09, // 90-9f
306 $07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07, // a0-af
307 $07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07, // b0-bf
308 $08,$08,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02, // c0-cf
309 $02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02, // d0-df
310 $0a,$03,$03,$03,$03,$03,$03,$03,$03,$03,$03,$03,$03,$04,$03,$03, // e0-ef
311 $0b,$06,$06,$06,$05,$08,$08,$08,$08,$08,$08,$08,$08,$08,$08,$08, // f0-ff
312 // maps a combination of a state of the automaton and a character class to a state
313 $00,$0c,$18,$24,$3c,$60,$54,$0c,$0c,$0c,$30,$48,$0c,$0c,$0c,$0c, // 100-10f
314 $0c,$0c,$0c,$0c,$0c,$0c,$0c,$0c,$0c,$00,$0c,$0c,$0c,$0c,$0c,$00, // 110-11f
315 $0c,$00,$0c,$0c,$0c,$18,$0c,$0c,$0c,$0c,$0c,$18,$0c,$18,$0c,$0c, // 120-12f
316 $0c,$0c,$0c,$0c,$0c,$0c,$0c,$18,$0c,$0c,$0c,$0c,$0c,$18,$0c,$0c, // 130-13f
317 $0c,$0c,$0c,$0c,$0c,$18,$0c,$0c,$0c,$0c,$0c,$0c,$0c,$0c,$0c,$24, // 140-14f
318 $0c,$24,$0c,$0c,$0c,$24,$0c,$0c,$0c,$0c,$0c,$24,$0c,$24,$0c,$0c, // 150-15f
319 $0c,$24,$0c,$0c,$0c,$0c,$0c,$0c,$0c,$0c,$0c,$0c);
322 // ////////////////////////////////////////////////////////////////////////// //
323 constructor TUtf8DecoderFast
.Create (v
: Boolean{fuck you, fpc}); begin state
:= Accept
; codepoint
:= 0; end;
325 procedure TUtf8DecoderFast
.reset (); inline; begin state
:= Accept
; codepoint
:= 0; end;
327 function TUtf8DecoderFast
.complete (): Boolean; inline; begin result
:= (state
= Accept
); end;
328 function TUtf8DecoderFast
.invalid (): Boolean; inline; begin result
:= (state
= Reject
); end;
329 function TUtf8DecoderFast
.completeOrInvalid (): Boolean; inline; begin result
:= (state
= Accept
) or (state
= Reject
); end;
331 function TUtf8DecoderFast
.decode (c
: AnsiChar): Boolean; inline; overload
; begin result
:= decode(Byte(c
)); end;
333 function TUtf8DecoderFast
.decode (b
: Byte): Boolean; inline; overload
;
337 if (state
= Reject
) then begin state
:= Accept
; codepoint
:= 0; end;
339 if (state
<> Accept
) then codepoint
:= (b
and $3f) or (codepoint
shl 6) else codepoint
:= ($ff shr tp
) and b
;
340 state
:= utf8dfa
[256+state
+tp
];
341 if (state
= Reject
) then begin codepoint
:= Replacement
; state
:= Accept
; end;
342 result
:= (state
= Accept
);
346 // ////////////////////////////////////////////////////////////////////////// //
347 function wchar2win (wc
: WideChar): AnsiChar; inline;
349 if not wc2shitmapInited
then initShitMap();
350 if (LongWord(wc
) > 65535) then result
:= '?' else result
:= wc2shitmap
[LongWord(wc
)];
354 // ////////////////////////////////////////////////////////////////////////// //
355 function utf2win (const s
: AnsiString): AnsiString;
358 ud
: TUtf8DecoderFast
;
360 for f
:= 1 to Length(s
) do
362 if (Byte(s
[f
]) > 127) then
364 ud
:= TUtf8DecoderFast
.Create(true);
366 for c
:= 1 to Length(s
) do
368 if ud
.decode(s
[c
]) then result
+= wchar2win(WideChar(ud
.codepoint
));
377 // ////////////////////////////////////////////////////////////////////////// //
378 function digitInBase (ch
: AnsiChar; base
: Integer): Integer;
381 if (base
< 1) or (base
> 36) then exit
;
382 if (ch
< '0') then exit
;
385 if (Integer(ch
) >= 48+base
) then exit
;
386 result
:= Integer(ch
)-48;
390 if (ch
>= '0') and (ch
<= '9') then begin result
:= Integer(ch
)-48; exit
; end;
391 if (ch
>= 'a') and (ch
<= 'z') then Dec(ch
, 32); // poor man's tolower()
392 if (ch
< 'A') or (Integer(ch
) >= 65+(base
-10)) then exit
;
393 result
:= Integer(ch
)-65+10;
398 // ////////////////////////////////////////////////////////////////////////// //
399 function quoteStr (const s
: AnsiString): AnsiString;
401 function squote (const s
: AnsiString): AnsiString;
406 for f
:= 1 to Length(s
) do
408 if (s
[f
] = '''') then result
+= '''';
414 function dquote (const s
: AnsiString): AnsiString;
420 for f
:= 1 to Length(s
) do
423 if (ch
= #0) then result
+= '\z'
424 else if (ch
= #9) then result
+= '\t'
425 else if (ch
= #10) then result
+= '\n'
426 else if (ch
= #13) then result
+= '\r'
427 else if (ch
= #27) then result
+= '\e'
428 else if (ch
< ' ') or (ch
= #127) then
431 result
+= LowerCase(IntToHex(Integer(ch
), 2));
433 else if (ch
= '"') or (ch
= '\') then
447 needSingle
: Boolean = false;
450 for f
:= 1 to Length(s
) do
452 if (s
[f
] = '''') then begin needSingle
:= true; continue
; end;
453 if (s
[f
] < ' ') or (s
[f
] = #127) then begin result
:= dquote(s
); exit
; end;
455 if needSingle
then result
:= squote(s
) else result
:= ''''+s
+'''';
459 // ////////////////////////////////////////////////////////////////////////// //
460 function hasWadExtension (fn
: AnsiString): Boolean;
462 fn
:= ExtractFileExt(fn
);
463 result
:= StrEquCI1251(fn
, '.wad') or StrEquCI1251(fn
, '.pk3') or StrEquCI1251(fn
, '.zip');
467 function addWadExtension (fn
: AnsiString): AnsiString;
470 if not hasWadExtension(result
) then result
:= result
+'.wad';
474 function isWadPath (fn
: AnsiString): Boolean;
483 if (p
= 0) or (length(fn
)-p
< 1) then break
;
484 if (p
-4 > 1) and (fn
[p
-4] = '.') and ((fn
[p
+1] = '\') or (fn
[p
+1] = '/')) then
486 s
:= Copy(fn
, p
-4, 4);
487 if StrEquCI1251(s
, '.wad') or StrEquCI1251(s
, '.pk3') or StrEquCI1251(s
, '.zip') then
498 function Int64ToStrComma (i
: Int64): AnsiString;
503 f
:= Length(result
)+1;
506 Dec(f
, 3); Insert(',', result
, f
);
511 function UpCase1251 (ch
: Char): Char;
515 if (ch
>= 'a') and (ch
<= 'z') then Dec(ch
, 32);
519 if (ch
>= #224) and (ch
<= #255) then
526 #184, #186, #191: Dec(ch
, 16);
535 function LoCase1251 (ch
: Char): Char;
539 if (ch
>= 'A') and (ch
<= 'Z') then Inc(ch
, 32);
543 if (ch
>= #192) and (ch
<= #223) then
550 #168, #170, #175: Inc(ch
, 16);
559 function StrEquCI1251 (const s0
, s1
: AnsiString): Boolean;
564 if length(s0
) <> length(s1
) then exit
;
565 for i
:= 1 to length(s0
) do if UpCase1251(s0
[i
]) <> UpCase1251(s1
[i
]) then exit
;
570 // ////////////////////////////////////////////////////////////////////////// //
574 function utf8CodeLen (ch
: Word): Integer;
576 if ch
< $80 then begin result
:= 1; exit
; end;
577 if (ch
and $FE) = $FC then begin result
:= 6; exit
; end;
578 if (ch
and $FC) = $F8 then begin result
:= 5; exit
; end;
579 if (ch
and $F8) = $F0 then begin result
:= 4; exit
; end;
580 if (ch
and $F0) = $E0 then begin result
:= 3; exit
; end;
581 if (ch
and $E0) = $C0 then begin result
:= 2; exit
; end;
582 result
:= -1; // invalid
586 function utf8Valid (const s
: AnsiString): Boolean;
592 while pos
<= length(s
) do
594 len
:= utf8CodeLen(Byte(s
[pos
]));
595 if len
< 1 then exit
; // invalid sequence start
596 if pos
+len
-1 > length(s
) then exit
; // out of chars in string
599 // check other sequence bytes
602 if (Byte(s
[pos
]) and $C0) <> $80 then exit
;
611 // ////////////////////////////////////////////////////////////////////////// //
613 uni2wint
: array [128..255] of Word = (
614 $0402,$0403,$201A,$0453,$201E,$2026,$2020,$2021,$20AC,$2030,$0409,$2039,$040A,$040C,$040B,$040F,
615 $0452,$2018,$2019,$201C,$201D,$2022,$2013,$2014,$003F,$2122,$0459,$203A,$045A,$045C,$045B,$045F,
616 $00A0,$040E,$045E,$0408,$00A4,$0490,$00A6,$00A7,$0401,$00A9,$0404,$00AB,$00AC,$00AD,$00AE,$0407,
617 $00B0,$00B1,$0406,$0456,$0491,$00B5,$00B6,$00B7,$0451,$2116,$0454,$00BB,$0458,$0405,$0455,$0457,
618 $0410,$0411,$0412,$0413,$0414,$0415,$0416,$0417,$0418,$0419,$041A,$041B,$041C,$041D,$041E,$041F,
619 $0420,$0421,$0422,$0423,$0424,$0425,$0426,$0427,$0428,$0429,$042A,$042B,$042C,$042D,$042E,$042F,
620 $0430,$0431,$0432,$0433,$0434,$0435,$0436,$0437,$0438,$0439,$043A,$043B,$043C,$043D,$043E,$043F,
621 $0440,$0441,$0442,$0443,$0444,$0445,$0446,$0447,$0448,$0449,$044A,$044B,$044C,$044D,$044E,$044F
625 function decodeUtf8Char (s
: AnsiString; var pos
: Integer): char;
629 (* The following encodings are valid, except for the 5 and 6 byte
633 * 1110xxxx 10xxxxxx 10xxxxxx
634 * 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
635 * 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
636 * 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
639 if pos
> length(s
) then exit
;
643 if b
< $80 then begin result
:= char(b
); exit
; end;
645 // mask out unused bits
646 if (b
and $FE) = $FC then b
:= b
and $01
647 else if (b
and $FC) = $F8 then b
:= b
and $03
648 else if (b
and $F8) = $F0 then b
:= b
and $07
649 else if (b
and $F0) = $E0 then b
:= b
and $0F
650 else if (b
and $E0) = $C0 then b
:= b
and $1F
651 else exit
; // invalid utf8
654 while pos
<= length(s
) do
657 if (c
and $C0) <> $80 then break
; // no more
659 b
:= b
or (c
and $3F);
664 for c
:= 128 to 255 do if uni2wint
[c
] = b
then begin result
:= char(c
and $FF); exit
; end;
669 function utf8to1251 (s
: AnsiString): AnsiString;
673 if not utf8Valid(s
) then begin result
:= s
; exit
; end;
675 while pos
<= length(s
) do
677 if Byte(s
[pos
]) >= $80 then break
;
680 if pos
> length(s
) then begin result
:= s
; exit
; end; // nothing to do here
683 while pos
<= length(s
) do result
:= result
+decodeUtf8Char(s
, pos
);
687 // ////////////////////////////////////////////////////////////////////////// //
688 // `pathname` will be modified if path is valid
689 // `lastIsDir` should be `true` if we are searching for directory
690 // nobody cares about shitdoze, so i'll use the same code path for it
691 function findFileCI (var pathname
: AnsiString; lastIsDir
: Boolean=false): Boolean;
695 newname
: AnsiString = '';
702 result
:= (length(npt
) > 0);
703 if (length(npt
) > 0) and ((npt
[1] = '/') or (npt
[1] = '\')) then newname
:= '/';
704 while length(npt
) > 0 do
706 // remove trailing slashes
707 while (length(npt
) > 0) and ((npt
[1] = '/') or (npt
[1] = '\')) do Delete(npt
, 1, 1);
708 if length(npt
) = 0 then break
;
711 while (length(npt
) > 0) and (npt
[1] <> '/') and (npt
[1] <> '\') do
713 curname
:= curname
+npt
[1];
716 // remove trailing slashes again
717 while (length(npt
) > 0) and ((npt
[1] = '/') or (npt
[1] = '\')) do Delete(npt
, 1, 1);
718 wantdir
:= lastIsDir
or (length(npt
) > 0); // do we want directory here?
719 //writeln(Format('npt=[%s]; newname=[%s]; curname=[%s]; wantdir=%d', [npt, newname, curname, Integer(wantdir)]));
720 // try the easiest case first
721 attr
:= FileGetAttr(newname
+curname
);
724 if wantdir
= ((attr
and faDirectory
) <> 0) then
727 newname
:= newname
+curname
;
728 if wantdir
then newname
:= newname
+'/';
732 //writeln(Format('npt=[%s]; newname=[%s]; curname=[%s]; wantdir=%d', [npt, newname, curname, Integer(wantdir)]));
733 // alas, either not found, or invalid attributes
736 if FindFirst(newname
+'*', faAnyFile
, sr
) = 0 then
738 if (wantdir
= ((sr
.attr
and faDirectory
) <> 0)) and StrEquCI1251(sr
.name
, curname
) then
741 newname
:= newname
+sr
.name
;
742 if wantdir
then newname
:= newname
+'/';
746 until FindNext(sr
) <> 0;
750 if not foundher
then begin newname
:= ''; result
:= false; break
; end;
752 if result
then pathname
:= newname
;
756 function openDiskFileRO (pathname
: AnsiString): TStream
;
758 if not findFileCI(pathname
) then raise Exception
.Create('can''t open file "'+pathname
+'"');
759 result
:= TFileStream
.Create(pathname
, fmOpenRead
or {fmShareDenyWrite}fmShareDenyNone
);
762 function createDiskFile (pathname
: AnsiString): TStream
;
766 path
:= ExtractFilePath(pathname
);
767 if length(path
) > 0 then
769 if not findFileCI(path
, true) then raise Exception
.Create('can''t create file "'+pathname
+'"');
771 result
:= TFileStream
.Create(path
+ExtractFileName(pathname
), fmCreate
);
775 procedure writeIntegerLE (st
: TStream
; vp
: Pointer; size
: Integer);
776 {$IFDEF ENDIAN_LITTLE}
778 st
.writeBuffer(vp
^, size
);
784 p
:= PByte(vp
)+size
-1;
787 st
.writeBuffer(p
^, 1);
794 procedure writeIntegerBE (st
: TStream
; vp
: Pointer; size
: Integer);
795 {$IFDEF ENDIAN_LITTLE}
799 p
:= PByte(vp
)+size
-1;
802 st
.writeBuffer(p
^, 1);
809 st
.writeBuffer(vp
^, size
);
813 procedure writeInt (st
: TStream
; v
: Byte); overload
; begin writeIntegerLE(st
, @v
, 1); end;
814 procedure writeInt (st
: TStream
; v
: ShortInt); overload
; begin writeIntegerLE(st
, @v
, 1); end;
815 procedure writeInt (st
: TStream
; v
: Word); overload
; begin writeIntegerLE(st
, @v
, 2); end;
816 procedure writeInt (st
: TStream
; v
: SmallInt); overload
; begin writeIntegerLE(st
, @v
, 2); end;
817 procedure writeInt (st
: TStream
; v
: LongWord); overload
; begin writeIntegerLE(st
, @v
, 4); end;
818 procedure writeInt (st
: TStream
; v
: LongInt); overload
; begin writeIntegerLE(st
, @v
, 4); end;
819 procedure writeInt (st
: TStream
; v
: Int64); overload
; begin writeIntegerLE(st
, @v
, 8); end;
820 procedure writeInt (st
: TStream
; v
: UInt64); overload
; begin writeIntegerLE(st
, @v
, 8); end;
822 procedure writeIntBE (st
: TStream
; v
: Byte); overload
; begin writeIntegerBE(st
, @v
, 1); end;
823 procedure writeIntBE (st
: TStream
; v
: ShortInt); overload
; begin writeIntegerBE(st
, @v
, 1); end;
824 procedure writeIntBE (st
: TStream
; v
: Word); overload
; begin writeIntegerBE(st
, @v
, 2); end;
825 procedure writeIntBE (st
: TStream
; v
: SmallInt); overload
; begin writeIntegerBE(st
, @v
, 2); end;
826 procedure writeIntBE (st
: TStream
; v
: LongWord); overload
; begin writeIntegerBE(st
, @v
, 4); end;
827 procedure writeIntBE (st
: TStream
; v
: LongInt); overload
; begin writeIntegerBE(st
, @v
, 4); end;
828 procedure writeIntBE (st
: TStream
; v
: Int64); overload
; begin writeIntegerBE(st
, @v
, 8); end;
829 procedure writeIntBE (st
: TStream
; v
: UInt64); overload
; begin writeIntegerBE(st
, @v
, 8); end;
832 procedure readIntegerLE (st
: TStream
; vp
: Pointer; size
: Integer);
833 {$IFDEF ENDIAN_LITTLE}
835 st
.readBuffer(vp
^, size
);
841 p
:= PByte(vp
)+size
-1;
844 st
.readBuffer(p
^, 1);
851 procedure readIntegerBE (st
: TStream
; vp
: Pointer; size
: Integer);
852 {$IFDEF ENDIAN_LITTLE}
856 p
:= PByte(vp
)+size
-1;
859 st
.readBuffer(p
^, 1);
866 st
.readBuffer(vp
^, size
);
870 function readByte (st
: TStream
): Byte; begin readIntegerLE(st
, @result
, 1); end;
871 function readShortInt (st
: TStream
): ShortInt; begin readIntegerLE(st
, @result
, 1); end;
872 function readWord (st
: TStream
): Word; begin readIntegerLE(st
, @result
, 2); end;
873 function readSmallInt (st
: TStream
): SmallInt; begin readIntegerLE(st
, @result
, 2); end;
874 function readLongWord (st
: TStream
): LongWord; begin readIntegerLE(st
, @result
, 4); end;
875 function readLongInt (st
: TStream
): LongInt; begin readIntegerLE(st
, @result
, 4); end;
876 function readInt64 (st
: TStream
): Int64; begin readIntegerLE(st
, @result
, 8); end;
877 function readUInt64 (st
: TStream
): UInt64; begin readIntegerLE(st
, @result
, 8); end;
879 function readByteBE (st
: TStream
): Byte; begin readIntegerBE(st
, @result
, 1); end;
880 function readShortIntBE (st
: TStream
): ShortInt; begin readIntegerBE(st
, @result
, 1); end;
881 function readWordBE (st
: TStream
): Word; begin readIntegerBE(st
, @result
, 2); end;
882 function readSmallIntBE (st
: TStream
): SmallInt; begin readIntegerBE(st
, @result
, 2); end;
883 function readLongWordBE (st
: TStream
): LongWord; begin readIntegerBE(st
, @result
, 4); end;
884 function readLongIntBE (st
: TStream
): LongInt; begin readIntegerBE(st
, @result
, 4); end;
885 function readInt64BE (st
: TStream
): Int64; begin readIntegerBE(st
, @result
, 8); end;
886 function readUInt64BE (st
: TStream
): UInt64; begin readIntegerBE(st
, @result
, 8); end;
889 // ////////////////////////////////////////////////////////////////////////// //
891 function snprintf (buf
: PAnsiChar; bufsize
: SizeUInt
; const fmt
: PAnsiChar): SizeUInt
; cdecl; varargs
; external 'msvcrt.dll' name
'_snprintf';
893 function snprintf (buf
: PAnsiChar; bufsize
: SizeUInt
; const fmt
: PAnsiChar): SizeUInt
; cdecl; varargs
; external 'libc' name
'snprintf';
898 procedure conwriter (constref buf; len: SizeUInt);
904 if (len < 1) then exit;
908 if (len > 255) then slen := 255 else slen := Integer(len);
909 Move(b^, ss[1], len);
910 ss[0] := AnsiChar(slen);
919 function formatstrf (const fmt
: AnsiString; args
: array of const; writerCB
: TFormatStrFCallback
=nil): AnsiString;
921 PadSpaces
: AnsiString = ' ';
922 PadZeroes
: AnsiString = '00000000000000000000000000000000000000000000000000000000000000000000000';
924 curarg
: Integer = 0; // current arg in `args`
925 sign
, fmtch
: AnsiChar;
927 width
, prec
: Integer; // width and precision
930 strbuf
: array[0..256] of AnsiChar;
932 fmtbuf
: array[0..256] of AnsiChar;
937 procedure writer (constref buf
; len
: SizeUInt
);
943 if (len
< 1) then exit
;
945 if assigned(writerCB
) then
953 if (len
> 255) then slen
:= 255 else slen
:= Integer(len
);
954 Move(b
^, ss
[1], len
);
955 ss
[0] := AnsiChar(slen
);
963 procedure xwrite (const s
: AnsiString);
965 if (Length(s
) > 0) then writer(PAnsiChar(s
)^, Length(s
));
968 procedure putFmtChar (ch
: AnsiChar);
970 fmtbuf
[fmtblen
] := ch
;
974 procedure putFmtInt (n
: Integer);
978 len
:= snprintf(@fmtbuf
[fmtblen
], Length(fmtbuf
)-fmtblen
, '%d', n
);
979 if (len
> 0) then Inc(fmtblen
, len
);
982 procedure buildCFormat (const pfx
: AnsiString='');
987 for f
:= 1 to Length(pfx
) do putFmtChar(pfx
[f
]);
989 if (sign
<> ' ') then putFmtChar(sign
);
992 if (zeropad
) then putFmtChar('0');
1001 fmtbuf
[fmtblen
] := #0;
1004 procedure writeStrBuf ();
1006 if (strblen
> 0) then writer(strbuf
, strblen
);
1009 function i642str (n
: Int64; hex
: Boolean; hexup
: Boolean): PAnsiChar;
1014 if (n
= $8000000000000000) then
1016 if hex
then snprintf(@strbuf
[0], Length(strbuf
), '-8000000000000000')
1017 else snprintf(@strbuf
[0], Length(strbuf
), '-9223372036854775808');
1018 result
:= @strbuf
[0];
1023 if neg
then n
:= -n
;
1024 xpos
:= High(strbuf
);
1025 strbuf
[xpos
] := #0; Dec(xpos
);
1029 strbuf
[xpos
] := AnsiChar((n
mod 10)+48);
1035 if (n
mod 16 > 9) then
1037 strbuf
[xpos
] := AnsiChar((n
mod 16)+48+7);
1038 if not hexup
then Inc(strbuf
[xpos
], 32);
1040 else strbuf
[xpos
] := AnsiChar((n
mod 16)+48);
1045 if neg
then begin strbuf
[xpos
] := '-'; Dec(xpos
); end;
1046 result
:= @strbuf
[xpos
+1];
1050 function ui642str (n
: UInt64; hex
: Boolean; hexup
: Boolean): PAnsiChar;
1054 xpos
:= High(strbuf
);
1055 strbuf
[xpos
] := #0; Dec(xpos
);
1059 strbuf
[xpos
] := AnsiChar((n
mod 10)+48);
1065 if (n
mod 16 > 9) then
1067 strbuf
[xpos
] := AnsiChar((n
mod 16)+48+7);
1068 if not hexup
then Inc(strbuf
[xpos
], 32);
1070 else strbuf
[xpos
] := AnsiChar((n
mod 16)+48);
1075 result
:= @strbuf
[xpos
+1];
1078 procedure indent (len
: Integer);
1084 if (len
> Length(PadSpaces
)) then ilen
:= Length(PadSpaces
) else ilen
:= len
;
1085 writer(PAnsiChar(PadSpaces
)^, ilen
);
1090 procedure indent0 (len
: Integer);
1096 if (len
> Length(PadZeroes
)) then ilen
:= Length(PadZeroes
) else ilen
:= len
;
1097 writer(PAnsiChar(PadZeroes
)^, ilen
);
1105 while (spos
<= Length(fmt
)) do
1107 // print literal part
1109 while (epos
<= Length(fmt
)) and (fmt
[epos
] <> '%') do Inc(epos
);
1110 // output literal part
1111 if (epos
> spos
) then
1113 if (epos
> Length(fmt
)) then
1115 writer((PAnsiChar(fmt
)+spos
-1)^, epos
-spos
);
1118 if (epos
+1 > Length(fmt
)) then Inc(epos
) // last percent, output literally
1119 else if (fmt
[epos
+1] = '%') then // special case
1122 writer((PAnsiChar(fmt
)+spos
-1)^, epos
-spos
);
1127 writer((PAnsiChar(fmt
)+spos
-1)^, epos
-spos
);
1132 // check if we have argument for this format string
1133 if (curarg
> High(args
)) then
1135 xwrite('<OUT OF ARGS>');
1136 writer((PAnsiChar(fmt
)+spos
-1)^, Length(fmt
)-spos
+1);
1140 if (spos
+1 > Length(fmt
)) then break
; // oops
1141 assert(fmt
[spos
] = '%');
1143 // parse format; check for sign
1144 if (fmt
[spos
] = '-') then begin sign
:= '-'; Inc(spos
); end
1145 else if (fmt
[spos
] = '+') then begin sign
:= '+'; Inc(spos
); end
1148 if (spos
> Length(fmt
)) then begin xwrite('<INVALID FORMAT>'); break
; end;
1149 if (sign
<> ' ') or ((fmt
[spos
] >= '0') and (fmt
[spos
] <= '9')) then
1151 if (fmt
[spos
] < '0') or (fmt
[spos
] > '9') then begin xwrite('<INVALID FORMAT>'); writer((PAnsiChar(fmt
)+spos
-1)^, Length(fmt
)-spos
+1); break
; end;
1152 zeropad
:= (fmt
[spos
] = '0');
1154 while (spos
<= Length(fmt
)) do
1157 if (ch
< '0') or (ch
> '9') then break
;
1158 width
:= width
*10+Integer(ch
)-48;
1169 if (spos
<= Length(fmt
)) and (fmt
[spos
] = '.') then
1172 if (spos
> Length(fmt
)) then begin xwrite('<INVALID FORMAT>'); break
; end;
1173 if (fmt
[spos
] < '0') or (fmt
[spos
] > '9') then begin xwrite('<INVALID FORMAT>'); writer((PAnsiChar(fmt
)+spos
-1)^, Length(fmt
)-spos
+1); break
; end;
1175 while (spos
<= Length(fmt
)) do
1178 if (ch
< '0') or (ch
> '9') then break
;
1179 prec
:= prec
*10+Integer(ch
)-48;
1184 if (spos
> Length(fmt
)) then begin xwrite('<INVALID FORMAT>'); break
; end;
1187 // done parsing format, check for valid format chars
1188 if not (fmtch
in ['s','u','d','x','X','p','f','g','c']) then begin xwrite('<INVALID FORMAT CHAR>'); writer((PAnsiChar(fmt
)+spos
-1)^, Length(fmt
)-spos
+1); break
; end;
1189 // now write formatted string
1190 case args
[curarg
].VType
of
1191 vtInteger
: // args[curarg].VInteger
1193 if not (fmtch
in ['s','u','d','x','X']) then begin xwrite('<INVALID FORMAT CHAR>'); writer((PAnsiChar(fmt
)+spos
-1)^, Length(fmt
)-spos
+1); break
; end;
1194 if (fmtch
= 's') then fmtch
:= 'd';
1196 strblen
:= snprintf(@strbuf
[0], Length(strbuf
), @fmtbuf
[0], args
[curarg
].VInteger
);
1199 vtBoolean
: // args[curarg].VBoolean
1204 if args
[curarg
].VBoolean
then strblen
:= snprintf(@strbuf
[0], Length(strbuf
), @fmtbuf
[0], 'true')
1205 else strblen
:= snprintf(@strbuf
[0], Length(strbuf
), @fmtbuf
[0], 'false');
1211 if args
[curarg
].VBoolean
then strblen
:= snprintf(@strbuf
[0], Length(strbuf
), @fmtbuf
[0], AnsiChar('t'))
1212 else strblen
:= snprintf(@strbuf
[0], Length(strbuf
), @fmtbuf
[0], AnsiChar('f'));
1218 strblen
:= snprintf(@strbuf
[0], Length(strbuf
), @fmtbuf
[0], Integer(args
[curarg
].VBoolean
));
1223 xwrite('<INVALID FORMAT CHAR>');
1224 writer((PAnsiChar(fmt
)+spos
-1)^, Length(fmt
)-spos
+1);
1228 vtChar
: // args[curarg].VChar
1234 strblen
:= snprintf(@strbuf
[0], Length(strbuf
), @fmtbuf
[0], args
[curarg
].VChar
);
1240 strblen
:= snprintf(@strbuf
[0], Length(strbuf
), @fmtbuf
[0], Integer(args
[curarg
].VChar
));
1245 xwrite('<INVALID FORMAT CHAR>');
1246 writer((PAnsiChar(fmt
)+spos
-1)^, Length(fmt
)-spos
+1);
1250 //vtWideChar: begin end; // args[curarg].VWideChar (WideChar)
1251 vtExtended
: // args[curarg].VExtended^
1257 strblen
:= snprintf(@strbuf
[0], Length(strbuf
), @fmtbuf
[0], Double(args
[curarg
].VExtended
^));
1263 strblen
:= snprintf(@strbuf
[0], Length(strbuf
), @fmtbuf
[0], Double(args
[curarg
].VExtended
^));
1269 strblen
:= snprintf(@strbuf
[0], Length(strbuf
), @fmtbuf
[0], Integer(trunc(args
[curarg
].VExtended
^)));
1275 strblen
:= snprintf(@strbuf
[0], Length(strbuf
), @fmtbuf
[0], LongWord(trunc(args
[curarg
].VExtended
^)));
1280 xwrite('<INVALID FORMAT CHAR>');
1281 writer((PAnsiChar(fmt
)+spos
-1)^, Length(fmt
)-spos
+1);
1285 vtString
: // args[curarg].VString^ (PShortString)
1287 if (sign
<> '-') then indent(width
-Length(args
[curarg
].VString
^));
1288 writer(args
[curarg
].VString
^[1], Length(args
[curarg
].VString
^));
1289 if (sign
= '-') then indent(width
-Length(args
[curarg
].VString
^));
1291 vtPointer
: // args[curarg].VPointer
1296 if (width
< 8) then width
:= 8;
1299 strblen
:= snprintf(@strbuf
[0], Length(strbuf
), @fmtbuf
[0], PtrUInt(args
[curarg
].VPointer
));
1302 'u', 'd', 'x', 'p', 'X':
1304 if (fmtch
= 'p') then fmtch
:= 'x';
1305 if (width
< 8) then width
:= 8;
1308 strblen
:= snprintf(@strbuf
[0], Length(strbuf
), @fmtbuf
[0], PtrUInt(args
[curarg
].VPointer
));
1313 xwrite('<INVALID FORMAT CHAR>');
1314 writer((PAnsiChar(fmt
)+spos
-1)^, Length(fmt
)-spos
+1);
1318 vtPChar
: // args[curarg].VPChar
1319 if (args
[curarg
].VPChar
= nil) then
1321 if (sign
<> '-') then indent(width
-3);
1323 if (sign
= '-') then indent(width
-3);
1328 while (args
[curarg
].VPChar
[pclen
] <> #0) do Inc(pclen
);
1329 if (sign
<> '-') then indent(width
-pclen
);
1330 writer(args
[curarg
].VPChar
^, pclen
);
1331 if (sign
= '-') then indent(width
-pclen
);
1333 vtObject
: // args[curarg].VObject.Classname (TObject)
1335 if (sign
<> '-') then indent(width
-Length(args
[curarg
].VObject
.Classname
));
1336 xwrite(args
[curarg
].VObject
.Classname
);
1337 if (sign
= '-') then indent(width
-Length(args
[curarg
].VObject
.Classname
));
1339 vtClass
: // args[curarg].VClass.Classname (TClass)
1341 if (sign
<> '-') then indent(width
-Length(args
[curarg
].VClass
.Classname
));
1342 xwrite(args
[curarg
].VClass
.Classname
);
1343 if (sign
= '-') then indent(width
-Length(args
[curarg
].VClass
.Classname
));
1345 //vtPWideChar: begin end; // args[curarg].VPWideChar (PWideChar)
1346 vtAnsiString
: // AnsiString(args[curarg].VAnsiString) (Pointer)
1348 if (sign
<> '-') then indent(width
-Length(AnsiString(args
[curarg
].VAnsiString
)));
1349 xwrite(AnsiString(args
[curarg
].VAnsiString
));
1350 if (sign
= '-') then indent(width
-Length(AnsiString(args
[curarg
].VAnsiString
)));
1352 //vtCurrency: begin end; // args[curarg].VCurrency (PCurrency)
1353 //vtVariant: begin end; // args[curarg].VVariant^ (PVariant)
1354 //vtInterface: begin end; // args[curarg].VInterface (Pointer);
1355 //vtWideString: begin end; // args[curarg].VWideString (Pointer);
1356 vtInt64
: // args[curarg].VInt64^ (PInt64)
1359 's','d','u': pc
:= i642str(args
[curarg
].VInt64
^, false, false);
1360 'x': pc
:= i642str(args
[curarg
].VInt64
^, true, false);
1361 'X': pc
:= i642str(args
[curarg
].VInt64
^, true, true);
1362 else begin xwrite('<INVALID FORMAT CHAR>'); writer((PAnsiChar(fmt
)+spos
-1)^, Length(fmt
)-spos
+1); break
; end;
1365 while (pc
[pclen
] <> #0) do Inc(pclen
);
1366 if (sign
<> '-') and (width
> pclen
) then
1370 if (pc
[0] = '-') or (pc
[0] = '+') then
1373 indent0(width
-pclen
-1);
1379 indent0(width
-pclen
);
1384 indent(width
-pclen
);
1388 if (sign
= '-') then indent(width
-pclen
);
1390 vtQWord
: // args[curarg].VQWord^ (PQWord)
1393 's','d','u': pc
:= ui642str(args
[curarg
].VInt64
^, false, false);
1394 'x': pc
:= ui642str(args
[curarg
].VInt64
^, true, false);
1395 'X': pc
:= ui642str(args
[curarg
].VInt64
^, true, true);
1396 else begin xwrite('<INVALID FORMAT CHAR>'); writer((PAnsiChar(fmt
)+spos
-1)^, Length(fmt
)-spos
+1); break
; end;
1399 while (pc
[pclen
] <> #0) do Inc(pclen
);
1400 if (sign
<> '-') then begin if zeropad
then indent0(width
-pclen
) else indent(width
-pclen
); end;
1402 if (sign
= '-') then indent(width
-pclen
);
1406 xwrite('<INVALID TYPE>');
1407 writer((PAnsiChar(fmt
)+spos
-1)^, Length(fmt
)-spos
+1);
1420 i64: Int64 = -$A000000000;
1421 ui64: UInt64 = $A000000000;
1423 writef(conwriter, 'test int:<%s> bool:<%s:%02d:%c> bool:<%s:%02d:%c>; char:<%2s;%c;%d>!'#10, [42, true, true, true, false, false, false, 'A', 'A', 'A']);
1424 writef(conwriter, 'test float:<%s;%u;%f;%g>'#10, [666.6942, 666.6942, 666.6942, 666.6942]);
1427 writef(conwriter, 'test ss:<%5s;%040s>'#10, [ss, ss]);
1428 writef(conwriter, 'test ls:<%5s;%040s>'#10, [ls, ls]);
1429 writef(conwriter, 'test pointer:<%s;%x;%p>'#10, [@ss, @ss, @ss]);
1430 writef(conwriter, 'test i64:<%s;%x;%015d;%u;%X>'#10, [i64, i64, i64, i64, i64]);
1431 writef(conwriter, 'test ui64:<%s;%x;%15d;%015u;%X>'#10, [ui64, ui64, ui64, ui64, ui64]);