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 function getFilenameExt (const fn
: AnsiString): AnsiString;
56 function setFilenameExt (const fn
, ext
: AnsiString): AnsiString;
57 function forceFilenameExt (const fn
, ext
: AnsiString): AnsiString;
59 // strips out name from `fn`, leaving trailing slash
60 function getFilenamePath (const fn
: AnsiString): AnsiString;
62 // ends with '/' or '\'?
63 function isFilenamePath (const fn
: AnsiString): Boolean;
65 // strips extra trailing slashes in `path, and extra leading slashes in `fn`
66 // will add slash to `path`, even if `fn` is empty!
67 function filenameConcat (const path
, fn
: AnsiString): AnsiString;
69 // does filename have one of ".wad", ".pk3", ".zip" extensions?
70 function hasWadExtension (const fn
: AnsiString): Boolean;
72 // does filepath have ".XXX:\" in it?
73 function isWadPath (const fn
: AnsiString): Boolean;
75 // adds ".wad" extension if filename doesn't have one of ".wad", ".pk3", ".zip"
76 function addWadExtension (const fn
: AnsiString): AnsiString;
78 // convert number to strig with nice commas
79 function Int64ToStrComma (i
: Int64): AnsiString;
81 function UpCase1251 (ch
: Char): Char;
82 function LoCase1251 (ch
: Char): Char;
84 // `true` if strings are equal; ignoring case for cp1251
85 function StrEquCI1251 (const s0
, s1
: AnsiString): Boolean;
87 function utf8Valid (const s
: AnsiString): Boolean;
89 function utf8to1251 (s
: AnsiString): AnsiString;
91 // `pathname` will be modified if path is valid
92 // `lastIsDir` should be `true` if we are searching for directory
93 // nobody cares about shitdoze, so i'll use the same code path for it
94 function findFileCI (var pathname
: AnsiString; lastIsDir
: Boolean=false): Boolean;
97 function openDiskFileRO (pathname
: AnsiString): TStream
;
98 function createDiskFile (pathname
: AnsiString): TStream
;
101 procedure writeInt (st
: TStream
; v
: Byte); overload
;
102 procedure writeInt (st
: TStream
; v
: ShortInt); overload
;
103 procedure writeInt (st
: TStream
; v
: Word); overload
;
104 procedure writeInt (st
: TStream
; v
: SmallInt); overload
;
105 procedure writeInt (st
: TStream
; v
: LongWord); overload
;
106 procedure writeInt (st
: TStream
; v
: LongInt); overload
;
107 procedure writeInt (st
: TStream
; v
: Int64); overload
;
108 procedure writeInt (st
: TStream
; v
: UInt64); overload
;
110 function readByte (st
: TStream
): Byte;
111 function readShortInt (st
: TStream
): ShortInt;
112 function readWord (st
: TStream
): Word;
113 function readSmallInt (st
: TStream
): SmallInt;
114 function readLongWord (st
: TStream
): LongWord;
115 function readLongInt (st
: TStream
): LongInt;
116 function readInt64 (st
: TStream
): Int64;
117 function readUInt64 (st
: TStream
): UInt64;
120 procedure writeIntBE (st
: TStream
; v
: Byte); overload
;
121 procedure writeIntBE (st
: TStream
; v
: ShortInt); overload
;
122 procedure writeIntBE (st
: TStream
; v
: Word); overload
;
123 procedure writeIntBE (st
: TStream
; v
: SmallInt); overload
;
124 procedure writeIntBE (st
: TStream
; v
: LongWord); overload
;
125 procedure writeIntBE (st
: TStream
; v
: LongInt); overload
;
126 procedure writeIntBE (st
: TStream
; v
: Int64); overload
;
127 procedure writeIntBE (st
: TStream
; v
: UInt64); overload
;
129 function readByteBE (st
: TStream
): Byte;
130 function readShortIntBE (st
: TStream
): ShortInt;
131 function readWordBE (st
: TStream
): Word;
132 function readSmallIntBE (st
: TStream
): SmallInt;
133 function readLongWordBE (st
: TStream
): LongWord;
134 function readLongIntBE (st
: TStream
): LongInt;
135 function readInt64BE (st
: TStream
): Int64;
136 function readUInt64BE (st
: TStream
): UInt64;
139 function nmin (a
, b
: Byte): Byte; inline; overload
;
140 function nmin (a
, b
: ShortInt): ShortInt; inline; overload
;
141 function nmin (a
, b
: Word): Word; inline; overload
;
142 function nmin (a
, b
: SmallInt): SmallInt; inline; overload
;
143 function nmin (a
, b
: LongWord): LongWord; inline; overload
;
144 function nmin (a
, b
: LongInt): LongInt; inline; overload
;
145 function nmin (a
, b
: Int64): Int64; inline; overload
;
146 function nmin (a
, b
: UInt64): UInt64; inline; overload
;
147 function nmin (a
, b
: Single): Single; inline; overload
;
148 function nmin (a
, b
: Double): Double; inline; overload
;
149 function nmin (a
, b
: Extended
): Extended
; inline; overload
;
151 function nmax (a
, b
: Byte): Byte; inline; overload
;
152 function nmax (a
, b
: ShortInt): ShortInt; inline; overload
;
153 function nmax (a
, b
: Word): Word; inline; overload
;
154 function nmax (a
, b
: SmallInt): SmallInt; inline; overload
;
155 function nmax (a
, b
: LongWord): LongWord; inline; overload
;
156 function nmax (a
, b
: LongInt): LongInt; inline; overload
;
157 function nmax (a
, b
: Int64): Int64; inline; overload
;
158 function nmax (a
, b
: UInt64): UInt64; inline; overload
;
159 function nmax (a
, b
: Single): Single; inline; overload
;
160 function nmax (a
, b
: Double): Double; inline; overload
;
161 function nmax (a
, b
: Extended
): Extended
; inline; overload
;
163 function nclamp (v
, a
, b
: Byte): Byte; inline; overload
;
164 function nclamp (v
, a
, b
: ShortInt): ShortInt; inline; overload
;
165 function nclamp (v
, a
, b
: Word): Word; inline; overload
;
166 function nclamp (v
, a
, b
: SmallInt): SmallInt; inline; overload
;
167 function nclamp (v
, a
, b
: LongWord): LongWord; inline; overload
;
168 function nclamp (v
, a
, b
: LongInt): LongInt; inline; overload
;
169 function nclamp (v
, a
, b
: Int64): Int64; inline; overload
;
170 function nclamp (v
, a
, b
: UInt64): UInt64; inline; overload
;
171 function nclamp (v
, a
, b
: Single): Single; inline; overload
;
172 function nclamp (v
, a
, b
: Double): Double; inline; overload
;
173 function nclamp (v
, a
, b
: Extended
): Extended
; inline; overload
;
177 TFormatStrFCallback
= procedure (constref buf
; len
: SizeUInt
);
179 // returns formatted string if `writerCB` is `nil`, empty string otherwise
180 function formatstrf (const fmt
: AnsiString; args
: array of const; writerCB
: TFormatStrFCallback
=nil): AnsiString;
182 function wchar2win (wc
: WideChar): AnsiChar; inline;
183 function utf2win (const s
: AnsiString): AnsiString;
184 function win2utf (const s
: AnsiString): AnsiString;
185 function digitInBase (ch
: AnsiChar; base
: Integer): Integer;
187 // returns string in single or double quotes
188 // single quotes supports only pascal-style '' for single quote char
189 // double quotes supports c-style escapes
190 // function will select quote mode automatically
191 function quoteStr (const s
: AnsiString): AnsiString;
195 generic TSimpleList
<ItemT
> = class
197 //type PItemT = ^ItemT;
198 type TItemArr
= array of ItemT
;
208 constructor Create (const aitems
: TItemArr
; acount
: Integer);
209 function MoveNext
: Boolean;
210 function getCurrent (): ItemT
;
211 property Current
: ItemT read getCurrent
;
216 mCount
: Integer; // can be less than `mItems` size
219 function getAt (idx
: Integer): ItemT
; inline;
220 procedure setAt (idx
: Integer; const it
: ItemT
); inline;
222 function getCapacity (): Integer; inline;
223 procedure setCapacity (v
: Integer); inline;
226 constructor Create (acapacity
: Integer=-1);
227 destructor Destroy (); override;
229 //WARNING! don't change list contents in `for ... in`!
230 function GetEnumerator (): TEnumerator
;
232 procedure reset (); inline; // won't resize `mItems`
233 procedure clear (); inline;
235 procedure append (constref it
: ItemT
); inline;
238 property count
: Integer read mCount
;
239 property capacity
: Integer read getCapacity write setCapacity
;
240 property at
[idx
: Integer]: ItemT read getAt write setAt
; default
;
247 // ////////////////////////////////////////////////////////////////////////// //
248 constructor TSimpleList
.TEnumerator
.Create (const aitems
: TItemArr
; acount
: Integer);
255 function TSimpleList
.TEnumerator
.MoveNext
: Boolean;
258 result
:= (mCurrent
< mCount
);
261 function TSimpleList
.TEnumerator
.getCurrent (): ItemT
;
263 result
:= mItems
[mCurrent
];
267 // ////////////////////////////////////////////////////////////////////////// //
268 constructor TSimpleList
.Create (acapacity
: Integer=-1);
271 if (acapacity
> 0) then SetLength(mItems
, acapacity
);
276 destructor TSimpleList
.Destroy ();
283 function TSimpleList
.getCapacity (): Integer; inline;
285 result
:= Length(mItems
);
289 procedure TSimpleList
.setCapacity (v
: Integer); inline;
291 if (v
< mCount
) then v
:= mCount
;
292 if (v
<> Length(mItems
)) then SetLength(mItems
, v
);
296 function TSimpleList
.GetEnumerator (): TEnumerator
;
298 if (Length(mItems
) > 0) then result
:= TEnumerator
.Create(mItems
, mCount
)
299 else result
:= TEnumerator
.Create(nil, -1);
303 procedure TSimpleList
.reset (); inline;
309 procedure TSimpleList
.clear (); inline;
316 function TSimpleList
.getAt (idx
: Integer): ItemT
; inline;
318 if (idx
>= 0) and (idx
< mCount
) then result
:= mItems
[idx
] else result
:= Default(ItemT
);
322 procedure TSimpleList
.setAt (idx
: Integer; const it
: ItemT
); inline;
324 if (idx
>= 0) and (idx
< mCount
) then mItems
[idx
] := it
;
328 procedure TSimpleList
.append (constref it
: ItemT
); inline;
330 if (mCount
= Length(mItems
)) then
332 if (mCount
= 0) then SetLength(mItems
, 128) else SetLength(mItems
, mCount
*2);
334 mItems
[mCount
] := it
;
339 // ////////////////////////////////////////////////////////////////////////// //
341 wc2shitmap
: array[0..65535] of AnsiChar;
342 wc2shitmapInited
: Boolean = false;
345 // ////////////////////////////////////////////////////////////////////////// //
347 cp1251
: array[0..127] of Word = (
348 $0402,$0403,$201A,$0453,$201E,$2026,$2020,$2021,$20AC,$2030,$0409,$2039,$040A,$040C,$040B,$040F,
349 $0452,$2018,$2019,$201C,$201D,$2022,$2013,$2014,$003F,$2122,$0459,$203A,$045A,$045C,$045B,$045F,
350 $00A0,$040E,$045E,$0408,$00A4,$0490,$00A6,$00A7,$0401,$00A9,$0404,$00AB,$00AC,$00AD,$00AE,$0407,
351 $00B0,$00B1,$0406,$0456,$0491,$00B5,$00B6,$00B7,$0451,$2116,$0454,$00BB,$0458,$0405,$0455,$0457,
352 $0410,$0411,$0412,$0413,$0414,$0415,$0416,$0417,$0418,$0419,$041A,$041B,$041C,$041D,$041E,$041F,
353 $0420,$0421,$0422,$0423,$0424,$0425,$0426,$0427,$0428,$0429,$042A,$042B,$042C,$042D,$042E,$042F,
354 $0430,$0431,$0432,$0433,$0434,$0435,$0436,$0437,$0438,$0439,$043A,$043B,$043C,$043D,$043E,$043F,
355 $0440,$0441,$0442,$0443,$0444,$0445,$0446,$0447,$0448,$0449,$044A,$044B,$044C,$044D,$044E,$044F
359 procedure initShitMap ();
363 for f
:= 0 to High(wc2shitmap
) do wc2shitmap
[f
] := '?';
364 for f
:= 0 to 127 do wc2shitmap
[f
] := AnsiChar(f
);
365 for f
:= 0 to 127 do wc2shitmap
[cp1251
[f
]] := AnsiChar(f
+128);
366 wc2shitmapInited
:= true;
370 // ////////////////////////////////////////////////////////////////////////// //
371 // fast state-machine based UTF-8 decoder; using 8 bytes of memory
372 // code points from invalid range will never be valid, this is the property of the state machine
374 // see http://bjoern.hoehrmann.de/utf-8/decoder/dfa/
375 utf8dfa
: array[0..$16c-1] of Byte = (
376 // maps bytes to character classes
377 $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00, // 00-0f
378 $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00, // 10-1f
379 $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00, // 20-2f
380 $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00, // 30-3f
381 $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00, // 40-4f
382 $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00, // 50-5f
383 $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00, // 60-6f
384 $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00, // 70-7f
385 $01,$01,$01,$01,$01,$01,$01,$01,$01,$01,$01,$01,$01,$01,$01,$01, // 80-8f
386 $09,$09,$09,$09,$09,$09,$09,$09,$09,$09,$09,$09,$09,$09,$09,$09, // 90-9f
387 $07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07, // a0-af
388 $07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07, // b0-bf
389 $08,$08,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02, // c0-cf
390 $02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02, // d0-df
391 $0a,$03,$03,$03,$03,$03,$03,$03,$03,$03,$03,$03,$03,$04,$03,$03, // e0-ef
392 $0b,$06,$06,$06,$05,$08,$08,$08,$08,$08,$08,$08,$08,$08,$08,$08, // f0-ff
393 // maps a combination of a state of the automaton and a character class to a state
394 $00,$0c,$18,$24,$3c,$60,$54,$0c,$0c,$0c,$30,$48,$0c,$0c,$0c,$0c, // 100-10f
395 $0c,$0c,$0c,$0c,$0c,$0c,$0c,$0c,$0c,$00,$0c,$0c,$0c,$0c,$0c,$00, // 110-11f
396 $0c,$00,$0c,$0c,$0c,$18,$0c,$0c,$0c,$0c,$0c,$18,$0c,$18,$0c,$0c, // 120-12f
397 $0c,$0c,$0c,$0c,$0c,$0c,$0c,$18,$0c,$0c,$0c,$0c,$0c,$18,$0c,$0c, // 130-13f
398 $0c,$0c,$0c,$0c,$0c,$18,$0c,$0c,$0c,$0c,$0c,$0c,$0c,$0c,$0c,$24, // 140-14f
399 $0c,$24,$0c,$0c,$0c,$24,$0c,$0c,$0c,$0c,$0c,$24,$0c,$24,$0c,$0c, // 150-15f
400 $0c,$24,$0c,$0c,$0c,$0c,$0c,$0c,$0c,$0c,$0c,$0c);
403 // ////////////////////////////////////////////////////////////////////////// //
404 constructor TUtf8DecoderFast
.Create (v
: Boolean{fuck you, fpc}); begin state
:= Accept
; codepoint
:= 0; end;
406 procedure TUtf8DecoderFast
.reset (); inline; begin state
:= Accept
; codepoint
:= 0; end;
408 function TUtf8DecoderFast
.complete (): Boolean; inline; begin result
:= (state
= Accept
); end;
409 function TUtf8DecoderFast
.invalid (): Boolean; inline; begin result
:= (state
= Reject
); end;
410 function TUtf8DecoderFast
.completeOrInvalid (): Boolean; inline; begin result
:= (state
= Accept
) or (state
= Reject
); end;
412 function TUtf8DecoderFast
.decode (c
: AnsiChar): Boolean; inline; overload
; begin result
:= decode(Byte(c
)); end;
414 function TUtf8DecoderFast
.decode (b
: Byte): Boolean; inline; overload
;
418 if (state
= Reject
) then begin state
:= Accept
; codepoint
:= 0; end;
420 if (state
<> Accept
) then codepoint
:= (b
and $3f) or (codepoint
shl 6) else codepoint
:= ($ff shr tp
) and b
;
421 state
:= utf8dfa
[256+state
+tp
];
422 if (state
= Reject
) then begin codepoint
:= Replacement
; state
:= Accept
; end;
423 result
:= (state
= Accept
);
427 // ////////////////////////////////////////////////////////////////////////// //
428 function wchar2win (wc
: WideChar): AnsiChar; inline;
430 if not wc2shitmapInited
then initShitMap();
431 if (LongWord(wc
) > 65535) then result
:= '?' else result
:= wc2shitmap
[LongWord(wc
)];
435 // ////////////////////////////////////////////////////////////////////////// //
436 function utf2win (const s
: AnsiString): AnsiString;
439 ud
: TUtf8DecoderFast
;
441 for f
:= 1 to Length(s
) do
443 if (Byte(s
[f
]) > 127) then
445 ud
:= TUtf8DecoderFast
.Create(true);
447 for c
:= 1 to Length(s
) do
449 if ud
.decode(s
[c
]) then result
+= wchar2win(WideChar(ud
.codepoint
));
458 function win2utf (const s
: AnsiString): AnsiString;
462 function utf8Encode (code
: Integer): AnsiString;
464 if (code
< 0) or (code
> $10FFFF) then begin result
:= '?'; exit
; end;
465 if (code
<= $7f) then
467 result
:= Char(code
and $ff);
469 else if (code
<= $7FF) then
471 result
:= Char($C0 or (code
shr 6));
472 result
+= Char($80 or (code
and $3F));
474 else if (code
<= $FFFF) then
476 result
:= Char($E0 or (code
shr 12));
477 result
+= Char($80 or ((code
shr 6) and $3F));
478 result
+= Char($80 or (code
and $3F));
480 else if (code
<= $10FFFF) then
482 result
:= Char($F0 or (code
shr 18));
483 result
+= Char($80 or ((code
shr 12) and $3F));
484 result
+= Char($80 or ((code
shr 6) and $3F));
485 result
+= Char($80 or (code
and $3F));
494 for f
:= 1 to Length(s
) do
496 if (Byte(s
[f
]) > 127) then
499 for c
:= 1 to Length(s
) do
501 if (Byte(s
[c
]) < 128) then
507 result
+= utf8Encode(cp1251
[Byte(s
[c
])-128])
517 // ////////////////////////////////////////////////////////////////////////// //
518 function digitInBase (ch
: AnsiChar; base
: Integer): Integer;
521 if (base
< 1) or (base
> 36) then exit
;
522 if (ch
< '0') then exit
;
525 if (Integer(ch
) >= 48+base
) then exit
;
526 result
:= Integer(ch
)-48;
530 if (ch
>= '0') and (ch
<= '9') then begin result
:= Integer(ch
)-48; exit
; end;
531 if (ch
>= 'a') and (ch
<= 'z') then Dec(ch
, 32); // poor man's tolower()
532 if (ch
< 'A') or (Integer(ch
) >= 65+(base
-10)) then exit
;
533 result
:= Integer(ch
)-65+10;
538 // ////////////////////////////////////////////////////////////////////////// //
539 function quoteStr (const s
: AnsiString): AnsiString;
541 function squote (const s
: AnsiString): AnsiString;
546 for f
:= 1 to Length(s
) do
548 if (s
[f
] = '''') then result
+= '''';
554 function dquote (const s
: AnsiString): AnsiString;
560 for f
:= 1 to Length(s
) do
563 if (ch
= #0) then result
+= '\z'
564 else if (ch
= #9) then result
+= '\t'
565 else if (ch
= #10) then result
+= '\n'
566 else if (ch
= #13) then result
+= '\r'
567 else if (ch
= #27) then result
+= '\e'
568 else if (ch
< ' ') or (ch
= #127) then
571 result
+= LowerCase(IntToHex(Integer(ch
), 2));
573 else if (ch
= '"') or (ch
= '\') then
587 needSingle
: Boolean = false;
590 for f
:= 1 to Length(s
) do
592 if (s
[f
] = '''') then begin needSingle
:= true; continue
; end;
593 if (s
[f
] < ' ') or (s
[f
] = #127) then begin result
:= dquote(s
); exit
; end;
595 if needSingle
then result
:= squote(s
) else result
:= ''''+s
+'''';
599 // ////////////////////////////////////////////////////////////////////////// //
600 function getFilenameExt (const fn
: AnsiString): AnsiString;
611 if (pos
= Length(fn
)) then result
:= '' else result
:= Copy(fn
, pos
, Length(fn
)-pos
+1);
614 if (ch
= '/') or (ch
= '\') then break
;
617 result
:= ''; // no extension
621 function setFilenameExt (const fn
, ext
: AnsiString): AnsiString;
627 if (Length(ext
) = 0) or (ext
= '.') then exit
;
632 if (ch
= '.') then exit
;
633 if (ch
= '/') or (ch
= '\') then break
;
636 if (ext
[1] <> '.') then result
+= '.';
641 function forceFilenameExt (const fn
, ext
: AnsiString): AnsiString;
653 if (Length(ext
) = 0) or (ext
= '.') then
655 result
:= Copy(fn
, 1, pos
-1);
659 if (ext
[1] = '.') then result
:= Copy(fn
, 1, pos
-1) else result
:= Copy(fn
, 1, pos
);
664 if (ch
= '/') or (ch
= '\') then break
;
667 if (Length(ext
) > 0) then
669 if (ext
[1] <> '.') then result
+= '.';
675 // strips out name from `fn`, leaving trailing slash
676 function getFilenamePath (const fn
: AnsiString): AnsiString;
681 if (Length(fn
) = 0) then begin result
:= './'; exit
; end;
682 if (fn
[Length(fn
)] = '/') or (fn
[Length(fn
)] = '\') then begin result
:= fn
; exit
; end;
687 if (ch
= '/') or (ch
= '\') then begin result
:= Copy(fn
, 1, pos
); exit
; end;
690 result
:= './'; // no path -> current dir
694 // ends with '/' or '\'?
695 function isFilenamePath (const fn
: AnsiString): Boolean;
697 if (Length(fn
) = 0) then
703 result
:= (fn
[Length(fn
)] = '/') or (fn
[Length(fn
)] = '\');
708 // strips extra trailing slashes in `path, and extra leading slashes in `fn`
709 // will add slash to `path`, even if `fn` is empty!
710 function filenameConcat (const path
, fn
: AnsiString): AnsiString;
715 while (pos
<= Length(fn
)) and ((fn
[pos
] = '/') or (fn
[pos
] = '\')) do Inc(pos
);
717 if (Length(result
) > 0) and ((result
[Length(result
)] <> '/') and (result
[Length(result
)] <> '\')) then result
+= '/';
718 if (pos
<= Length(fn
)) then
720 result
+= Copy(fn
, pos
, Length(fn
)-pos
+1);
721 //FIXME: make this faster!
722 while (Length(result
) > 0) and ((result
[Length(result
)] = '/') or (result
[Length(result
)] = '\')) do
724 Delete(result
, Length(result
), 1);
726 if (fn
[Length(fn
)] = '/') or (fn
[Length(fn
)] = '\') then result
+= '/';
731 function hasWadExtension (const fn
: AnsiString): Boolean;
735 ext
:= getFilenameExt(fn
);
736 result
:= StrEquCI1251(ext
, '.wad') or StrEquCI1251(ext
, '.pk3') or StrEquCI1251(ext
, '.zip');
740 function addWadExtension (const fn
: AnsiString): AnsiString;
743 if not hasWadExtension(result
) then result
:= result
+'.wad';
747 function isWadPath (const fn
: AnsiString): Boolean;
754 while (pos
<= Length(fn
)) do
756 if (fn
[pos
] = ':') then
758 if (Length(fn
)-pos
< 1) then break
;
759 if (pos
-4 > 1) and (fn
[pos
-4] = '.') and ((fn
[pos
+1] = '\') or (fn
[pos
+1] = '/')) then
761 s
:= Copy(fn
, pos
-4, 4);
762 if StrEquCI1251(s
, '.wad') or StrEquCI1251(s
, '.pk3') or StrEquCI1251(s
, '.zip') then
774 function Int64ToStrComma (i
: Int64): AnsiString;
779 f
:= Length(result
)+1;
782 Dec(f
, 3); Insert(',', result
, f
);
787 function UpCase1251 (ch
: Char): Char;
791 if (ch
>= 'a') and (ch
<= 'z') then Dec(ch
, 32);
795 if (ch
>= #224) and (ch
<= #255) then
802 #184, #186, #191: Dec(ch
, 16);
811 function LoCase1251 (ch
: Char): Char;
815 if (ch
>= 'A') and (ch
<= 'Z') then Inc(ch
, 32);
819 if (ch
>= #192) and (ch
<= #223) then
826 #168, #170, #175: Inc(ch
, 16);
835 function StrEquCI1251 (const s0
, s1
: AnsiString): Boolean;
840 if length(s0
) <> length(s1
) then exit
;
841 for i
:= 1 to length(s0
) do if UpCase1251(s0
[i
]) <> UpCase1251(s1
[i
]) then exit
;
846 // ////////////////////////////////////////////////////////////////////////// //
850 function utf8CodeLen (ch
: Word): Integer;
852 if ch
< $80 then begin result
:= 1; exit
; end;
853 if (ch
and $FE) = $FC then begin result
:= 6; exit
; end;
854 if (ch
and $FC) = $F8 then begin result
:= 5; exit
; end;
855 if (ch
and $F8) = $F0 then begin result
:= 4; exit
; end;
856 if (ch
and $F0) = $E0 then begin result
:= 3; exit
; end;
857 if (ch
and $E0) = $C0 then begin result
:= 2; exit
; end;
858 result
:= -1; // invalid
862 function utf8Valid (const s
: AnsiString): Boolean;
868 while pos
<= length(s
) do
870 len
:= utf8CodeLen(Byte(s
[pos
]));
871 if len
< 1 then exit
; // invalid sequence start
872 if pos
+len
-1 > length(s
) then exit
; // out of chars in string
875 // check other sequence bytes
878 if (Byte(s
[pos
]) and $C0) <> $80 then exit
;
887 // ////////////////////////////////////////////////////////////////////////// //
889 uni2wint
: array [128..255] of Word = (
890 $0402,$0403,$201A,$0453,$201E,$2026,$2020,$2021,$20AC,$2030,$0409,$2039,$040A,$040C,$040B,$040F,
891 $0452,$2018,$2019,$201C,$201D,$2022,$2013,$2014,$003F,$2122,$0459,$203A,$045A,$045C,$045B,$045F,
892 $00A0,$040E,$045E,$0408,$00A4,$0490,$00A6,$00A7,$0401,$00A9,$0404,$00AB,$00AC,$00AD,$00AE,$0407,
893 $00B0,$00B1,$0406,$0456,$0491,$00B5,$00B6,$00B7,$0451,$2116,$0454,$00BB,$0458,$0405,$0455,$0457,
894 $0410,$0411,$0412,$0413,$0414,$0415,$0416,$0417,$0418,$0419,$041A,$041B,$041C,$041D,$041E,$041F,
895 $0420,$0421,$0422,$0423,$0424,$0425,$0426,$0427,$0428,$0429,$042A,$042B,$042C,$042D,$042E,$042F,
896 $0430,$0431,$0432,$0433,$0434,$0435,$0436,$0437,$0438,$0439,$043A,$043B,$043C,$043D,$043E,$043F,
897 $0440,$0441,$0442,$0443,$0444,$0445,$0446,$0447,$0448,$0449,$044A,$044B,$044C,$044D,$044E,$044F
901 function decodeUtf8Char (s
: AnsiString; var pos
: Integer): char;
905 (* The following encodings are valid, except for the 5 and 6 byte
909 * 1110xxxx 10xxxxxx 10xxxxxx
910 * 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
911 * 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
912 * 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
915 if pos
> length(s
) then exit
;
919 if b
< $80 then begin result
:= char(b
); exit
; end;
921 // mask out unused bits
922 if (b
and $FE) = $FC then b
:= b
and $01
923 else if (b
and $FC) = $F8 then b
:= b
and $03
924 else if (b
and $F8) = $F0 then b
:= b
and $07
925 else if (b
and $F0) = $E0 then b
:= b
and $0F
926 else if (b
and $E0) = $C0 then b
:= b
and $1F
927 else exit
; // invalid utf8
930 while pos
<= length(s
) do
933 if (c
and $C0) <> $80 then break
; // no more
935 b
:= b
or (c
and $3F);
940 for c
:= 128 to 255 do if uni2wint
[c
] = b
then begin result
:= char(c
and $FF); exit
; end;
945 function utf8to1251 (s
: AnsiString): AnsiString;
949 if not utf8Valid(s
) then begin result
:= s
; exit
; end;
951 while pos
<= length(s
) do
953 if Byte(s
[pos
]) >= $80 then break
;
956 if pos
> length(s
) then begin result
:= s
; exit
; end; // nothing to do here
959 while pos
<= length(s
) do result
:= result
+decodeUtf8Char(s
, pos
);
963 // ////////////////////////////////////////////////////////////////////////// //
964 // `pathname` will be modified if path is valid
965 // `lastIsDir` should be `true` if we are searching for directory
966 // nobody cares about shitdoze, so i'll use the same code path for it
967 function findFileCI (var pathname
: AnsiString; lastIsDir
: Boolean=false): Boolean;
971 newname
: AnsiString = '';
978 result
:= (length(npt
) > 0);
979 if (length(npt
) > 0) and ((npt
[1] = '/') or (npt
[1] = '\')) then newname
:= '/';
980 while length(npt
) > 0 do
982 // remove trailing slashes
983 while (length(npt
) > 0) and ((npt
[1] = '/') or (npt
[1] = '\')) do Delete(npt
, 1, 1);
984 if length(npt
) = 0 then break
;
987 while (length(npt
) > 0) and (npt
[1] <> '/') and (npt
[1] <> '\') do
989 curname
:= curname
+npt
[1];
992 // remove trailing slashes again
993 while (length(npt
) > 0) and ((npt
[1] = '/') or (npt
[1] = '\')) do Delete(npt
, 1, 1);
994 wantdir
:= lastIsDir
or (length(npt
) > 0); // do we want directory here?
995 //writeln(Format('npt=[%s]; newname=[%s]; curname=[%s]; wantdir=%d', [npt, newname, curname, Integer(wantdir)]));
996 // try the easiest case first
997 attr
:= FileGetAttr(newname
+curname
);
1000 if wantdir
= ((attr
and faDirectory
) <> 0) then
1003 newname
:= newname
+curname
;
1004 if wantdir
then newname
:= newname
+'/';
1008 //writeln(Format('npt=[%s]; newname=[%s]; curname=[%s]; wantdir=%d', [npt, newname, curname, Integer(wantdir)]));
1009 // alas, either not found, or invalid attributes
1012 if FindFirst(newname
+'*', faAnyFile
, sr
) = 0 then
1014 if (wantdir
= ((sr
.attr
and faDirectory
) <> 0)) and StrEquCI1251(sr
.name
, curname
) then
1017 newname
:= newname
+sr
.name
;
1018 if wantdir
then newname
:= newname
+'/';
1022 until FindNext(sr
) <> 0;
1026 if not foundher
then begin newname
:= ''; result
:= false; break
; end;
1028 if result
then pathname
:= newname
;
1032 function openDiskFileRO (pathname
: AnsiString): TStream
;
1034 if not findFileCI(pathname
) then raise Exception
.Create('can''t open file "'+pathname
+'"');
1035 result
:= TFileStream
.Create(pathname
, fmOpenRead
or {fmShareDenyWrite}fmShareDenyNone
);
1038 function createDiskFile (pathname
: AnsiString): TStream
;
1042 path
:= ExtractFilePath(pathname
);
1043 if length(path
) > 0 then
1045 if not findFileCI(path
, true) then raise Exception
.Create('can''t create file "'+pathname
+'"');
1047 result
:= TFileStream
.Create(path
+ExtractFileName(pathname
), fmCreate
);
1051 procedure writeIntegerLE (st
: TStream
; vp
: Pointer; size
: Integer);
1052 {$IFDEF ENDIAN_LITTLE}
1054 st
.writeBuffer(vp
^, size
);
1060 p
:= PByte(vp
)+size
-1;
1063 st
.writeBuffer(p
^, 1);
1070 procedure writeIntegerBE (st
: TStream
; vp
: Pointer; size
: Integer);
1071 {$IFDEF ENDIAN_LITTLE}
1075 p
:= PByte(vp
)+size
-1;
1078 st
.writeBuffer(p
^, 1);
1085 st
.writeBuffer(vp
^, size
);
1089 procedure writeInt (st
: TStream
; v
: Byte); overload
; begin writeIntegerLE(st
, @v
, 1); end;
1090 procedure writeInt (st
: TStream
; v
: ShortInt); overload
; begin writeIntegerLE(st
, @v
, 1); end;
1091 procedure writeInt (st
: TStream
; v
: Word); overload
; begin writeIntegerLE(st
, @v
, 2); end;
1092 procedure writeInt (st
: TStream
; v
: SmallInt); overload
; begin writeIntegerLE(st
, @v
, 2); end;
1093 procedure writeInt (st
: TStream
; v
: LongWord); overload
; begin writeIntegerLE(st
, @v
, 4); end;
1094 procedure writeInt (st
: TStream
; v
: LongInt); overload
; begin writeIntegerLE(st
, @v
, 4); end;
1095 procedure writeInt (st
: TStream
; v
: Int64); overload
; begin writeIntegerLE(st
, @v
, 8); end;
1096 procedure writeInt (st
: TStream
; v
: UInt64); overload
; begin writeIntegerLE(st
, @v
, 8); end;
1098 procedure writeIntBE (st
: TStream
; v
: Byte); overload
; begin writeIntegerBE(st
, @v
, 1); end;
1099 procedure writeIntBE (st
: TStream
; v
: ShortInt); overload
; begin writeIntegerBE(st
, @v
, 1); end;
1100 procedure writeIntBE (st
: TStream
; v
: Word); overload
; begin writeIntegerBE(st
, @v
, 2); end;
1101 procedure writeIntBE (st
: TStream
; v
: SmallInt); overload
; begin writeIntegerBE(st
, @v
, 2); end;
1102 procedure writeIntBE (st
: TStream
; v
: LongWord); overload
; begin writeIntegerBE(st
, @v
, 4); end;
1103 procedure writeIntBE (st
: TStream
; v
: LongInt); overload
; begin writeIntegerBE(st
, @v
, 4); end;
1104 procedure writeIntBE (st
: TStream
; v
: Int64); overload
; begin writeIntegerBE(st
, @v
, 8); end;
1105 procedure writeIntBE (st
: TStream
; v
: UInt64); overload
; begin writeIntegerBE(st
, @v
, 8); end;
1108 procedure readIntegerLE (st
: TStream
; vp
: Pointer; size
: Integer);
1109 {$IFDEF ENDIAN_LITTLE}
1111 st
.readBuffer(vp
^, size
);
1117 p
:= PByte(vp
)+size
-1;
1120 st
.readBuffer(p
^, 1);
1127 procedure readIntegerBE (st
: TStream
; vp
: Pointer; size
: Integer);
1128 {$IFDEF ENDIAN_LITTLE}
1132 p
:= PByte(vp
)+size
-1;
1135 st
.readBuffer(p
^, 1);
1142 st
.readBuffer(vp
^, size
);
1146 function readByte (st
: TStream
): Byte; begin readIntegerLE(st
, @result
, 1); end;
1147 function readShortInt (st
: TStream
): ShortInt; begin readIntegerLE(st
, @result
, 1); end;
1148 function readWord (st
: TStream
): Word; begin readIntegerLE(st
, @result
, 2); end;
1149 function readSmallInt (st
: TStream
): SmallInt; begin readIntegerLE(st
, @result
, 2); end;
1150 function readLongWord (st
: TStream
): LongWord; begin readIntegerLE(st
, @result
, 4); end;
1151 function readLongInt (st
: TStream
): LongInt; begin readIntegerLE(st
, @result
, 4); end;
1152 function readInt64 (st
: TStream
): Int64; begin readIntegerLE(st
, @result
, 8); end;
1153 function readUInt64 (st
: TStream
): UInt64; begin readIntegerLE(st
, @result
, 8); end;
1155 function readByteBE (st
: TStream
): Byte; begin readIntegerBE(st
, @result
, 1); end;
1156 function readShortIntBE (st
: TStream
): ShortInt; begin readIntegerBE(st
, @result
, 1); end;
1157 function readWordBE (st
: TStream
): Word; begin readIntegerBE(st
, @result
, 2); end;
1158 function readSmallIntBE (st
: TStream
): SmallInt; begin readIntegerBE(st
, @result
, 2); end;
1159 function readLongWordBE (st
: TStream
): LongWord; begin readIntegerBE(st
, @result
, 4); end;
1160 function readLongIntBE (st
: TStream
): LongInt; begin readIntegerBE(st
, @result
, 4); end;
1161 function readInt64BE (st
: TStream
): Int64; begin readIntegerBE(st
, @result
, 8); end;
1162 function readUInt64BE (st
: TStream
): UInt64; begin readIntegerBE(st
, @result
, 8); end;
1165 // ////////////////////////////////////////////////////////////////////////// //
1166 function nmin (a
, b
: Byte): Byte; inline; overload
; begin if (a
< b
) then result
:= a
else result
:= b
; end;
1167 function nmin (a
, b
: ShortInt): ShortInt; inline; overload
; begin if (a
< b
) then result
:= a
else result
:= b
; end;
1168 function nmin (a
, b
: Word): Word; inline; overload
; begin if (a
< b
) then result
:= a
else result
:= b
; end;
1169 function nmin (a
, b
: SmallInt): SmallInt; inline; overload
; begin if (a
< b
) then result
:= a
else result
:= b
; end;
1170 function nmin (a
, b
: LongWord): LongWord; inline; overload
; begin if (a
< b
) then result
:= a
else result
:= b
; end;
1171 function nmin (a
, b
: LongInt): LongInt; inline; overload
; begin if (a
< b
) then result
:= a
else result
:= b
; end;
1172 function nmin (a
, b
: Int64): Int64; inline; overload
; begin if (a
< b
) then result
:= a
else result
:= b
; end;
1173 function nmin (a
, b
: UInt64): UInt64; inline; overload
; begin if (a
< b
) then result
:= a
else result
:= b
; end;
1174 function nmin (a
, b
: Single): Single; inline; overload
; begin if (a
< b
) then result
:= a
else result
:= b
; end;
1175 function nmin (a
, b
: Double): Double; inline; overload
; begin if (a
< b
) then result
:= a
else result
:= b
; end;
1176 function nmin (a
, b
: Extended
): Extended
; inline; overload
; begin if (a
< b
) then result
:= a
else result
:= b
; end;
1178 function nmax (a
, b
: Byte): Byte; inline; overload
; begin if (a
> b
) then result
:= a
else result
:= b
; end;
1179 function nmax (a
, b
: ShortInt): ShortInt; inline; overload
; begin if (a
> b
) then result
:= a
else result
:= b
; end;
1180 function nmax (a
, b
: Word): Word; inline; overload
; begin if (a
> b
) then result
:= a
else result
:= b
; end;
1181 function nmax (a
, b
: SmallInt): SmallInt; inline; overload
; begin if (a
> b
) then result
:= a
else result
:= b
; end;
1182 function nmax (a
, b
: LongWord): LongWord; inline; overload
; begin if (a
> b
) then result
:= a
else result
:= b
; end;
1183 function nmax (a
, b
: LongInt): LongInt; inline; overload
; begin if (a
> b
) then result
:= a
else result
:= b
; end;
1184 function nmax (a
, b
: Int64): Int64; inline; overload
; begin if (a
> b
) then result
:= a
else result
:= b
; end;
1185 function nmax (a
, b
: UInt64): UInt64; inline; overload
; begin if (a
> b
) then result
:= a
else result
:= b
; end;
1186 function nmax (a
, b
: Single): Single; inline; overload
; begin if (a
> b
) then result
:= a
else result
:= b
; end;
1187 function nmax (a
, b
: Double): Double; inline; overload
; begin if (a
> b
) then result
:= a
else result
:= b
; end;
1188 function nmax (a
, b
: Extended
): Extended
; inline; overload
; begin if (a
> b
) then result
:= a
else result
:= b
; end;
1190 function nclamp (v
, a
, b
: Byte): Byte; inline; overload
; begin if (v
< a
) then result
:= a
else if (v
> b
) then result
:= b
else result
:= v
; end;
1191 function nclamp (v
, a
, b
: ShortInt): ShortInt; inline; overload
; begin if (v
< a
) then result
:= a
else if (v
> b
) then result
:= b
else result
:= v
; end;
1192 function nclamp (v
, a
, b
: Word): Word; inline; overload
; begin if (v
< a
) then result
:= a
else if (v
> b
) then result
:= b
else result
:= v
; end;
1193 function nclamp (v
, a
, b
: SmallInt): SmallInt; inline; overload
; begin if (v
< a
) then result
:= a
else if (v
> b
) then result
:= b
else result
:= v
; end;
1194 function nclamp (v
, a
, b
: LongWord): LongWord; inline; overload
; begin if (v
< a
) then result
:= a
else if (v
> b
) then result
:= b
else result
:= v
; end;
1195 function nclamp (v
, a
, b
: LongInt): LongInt; inline; overload
; begin if (v
< a
) then result
:= a
else if (v
> b
) then result
:= b
else result
:= v
; end;
1196 function nclamp (v
, a
, b
: Int64): Int64; inline; overload
; begin if (v
< a
) then result
:= a
else if (v
> b
) then result
:= b
else result
:= v
; end;
1197 function nclamp (v
, a
, b
: UInt64): UInt64; inline; overload
; begin if (v
< a
) then result
:= a
else if (v
> b
) then result
:= b
else result
:= v
; end;
1198 function nclamp (v
, a
, b
: Single): Single; inline; overload
; begin if (v
< a
) then result
:= a
else if (v
> b
) then result
:= b
else result
:= v
; end;
1199 function nclamp (v
, a
, b
: Double): Double; inline; overload
; begin if (v
< a
) then result
:= a
else if (v
> b
) then result
:= b
else result
:= v
; end;
1200 function nclamp (v
, a
, b
: Extended
): Extended
; inline; overload
; begin if (v
< a
) then result
:= a
else if (v
> b
) then result
:= b
else result
:= v
; end;
1203 // ////////////////////////////////////////////////////////////////////////// //
1205 function snprintf (buf
: PAnsiChar; bufsize
: SizeUInt
; const fmt
: PAnsiChar): SizeUInt
; cdecl; varargs
; external 'msvcrt.dll' name
'_snprintf';
1207 function snprintf (buf
: PAnsiChar; bufsize
: SizeUInt
; const fmt
: PAnsiChar): SizeUInt
; cdecl; varargs
; external 'libc' name
'snprintf';
1212 procedure conwriter (constref buf; len: SizeUInt);
1218 if (len < 1) then exit;
1222 if (len > 255) then slen := 255 else slen := Integer(len);
1223 Move(b^, ss[1], len);
1224 ss[0] := AnsiChar(slen);
1233 function formatstrf (const fmt
: AnsiString; args
: array of const; writerCB
: TFormatStrFCallback
=nil): AnsiString;
1235 PadSpaces
: AnsiString = ' ';
1236 PadZeroes
: AnsiString = '00000000000000000000000000000000000000000000000000000000000000000000000';
1238 curarg
: Integer = 0; // current arg in `args`
1239 sign
, fmtch
: AnsiChar;
1241 width
, prec
: Integer; // width and precision
1242 spos
, epos
: Integer;
1244 strbuf
: array[0..256] of AnsiChar;
1246 fmtbuf
: array[0..256] of AnsiChar;
1250 ccname
: ShortString;
1252 procedure writer (constref buf
; len
: SizeUInt
);
1258 if (len
< 1) then exit
;
1260 if assigned(writerCB
) then
1268 if (len
> 255) then slen
:= 255 else slen
:= Integer(len
);
1269 Move(b
^, ss
[1], slen
);
1270 ss
[0] := AnsiChar(slen
);
1278 procedure xwrite (const s
: AnsiString);
1280 if (Length(s
) > 0) then writer(PAnsiChar(s
)^, Length(s
));
1283 procedure putFmtChar (ch
: AnsiChar);
1285 fmtbuf
[fmtblen
] := ch
;
1289 procedure putFmtInt (n
: Integer);
1293 len
:= snprintf(@fmtbuf
[fmtblen
], Length(fmtbuf
)-fmtblen
, '%d', n
);
1294 if (len
> 0) then Inc(fmtblen
, len
);
1297 procedure buildCFormat (const pfx
: AnsiString='');
1302 for f
:= 1 to Length(pfx
) do putFmtChar(pfx
[f
]);
1304 if (sign
<> ' ') then putFmtChar(sign
);
1305 if (width
>= 0) then
1307 if (zeropad
) then putFmtChar('0');
1316 fmtbuf
[fmtblen
] := #0;
1319 procedure writeStrBuf ();
1321 if (strblen
> 0) then writer(strbuf
, strblen
);
1324 function i642str (n
: Int64; hex
: Boolean; hexup
: Boolean): PAnsiChar;
1329 if (n
= $8000000000000000) then
1331 if hex
then snprintf(@strbuf
[0], Length(strbuf
), '-8000000000000000')
1332 else snprintf(@strbuf
[0], Length(strbuf
), '-9223372036854775808');
1333 result
:= @strbuf
[0];
1338 if neg
then n
:= -n
;
1339 xpos
:= High(strbuf
);
1340 strbuf
[xpos
] := #0; Dec(xpos
);
1344 strbuf
[xpos
] := AnsiChar((n
mod 10)+48);
1350 if (n
mod 16 > 9) then
1352 strbuf
[xpos
] := AnsiChar((n
mod 16)+48+7);
1353 if not hexup
then Inc(strbuf
[xpos
], 32);
1355 else strbuf
[xpos
] := AnsiChar((n
mod 16)+48);
1360 if neg
then begin strbuf
[xpos
] := '-'; Dec(xpos
); end;
1361 result
:= @strbuf
[xpos
+1];
1365 function ui642str (n
: UInt64; hex
: Boolean; hexup
: Boolean): PAnsiChar;
1369 xpos
:= High(strbuf
);
1370 strbuf
[xpos
] := #0; Dec(xpos
);
1374 strbuf
[xpos
] := AnsiChar((n
mod 10)+48);
1380 if (n
mod 16 > 9) then
1382 strbuf
[xpos
] := AnsiChar((n
mod 16)+48+7);
1383 if not hexup
then Inc(strbuf
[xpos
], 32);
1385 else strbuf
[xpos
] := AnsiChar((n
mod 16)+48);
1390 result
:= @strbuf
[xpos
+1];
1393 procedure indent (len
: Integer);
1399 if (len
> Length(PadSpaces
)) then ilen
:= Length(PadSpaces
) else ilen
:= len
;
1400 writer(PAnsiChar(PadSpaces
)^, ilen
);
1405 procedure indent0 (len
: Integer);
1411 if (len
> Length(PadZeroes
)) then ilen
:= Length(PadZeroes
) else ilen
:= len
;
1412 writer(PAnsiChar(PadZeroes
)^, ilen
);
1420 while (spos
<= Length(fmt
)) do
1422 // print literal part
1424 while (epos
<= Length(fmt
)) and (fmt
[epos
] <> '%') do Inc(epos
);
1425 // output literal part
1426 if (epos
> spos
) then
1428 if (epos
> Length(fmt
)) then
1430 writer((PAnsiChar(fmt
)+spos
-1)^, epos
-spos
);
1433 if (epos
+1 > Length(fmt
)) then Inc(epos
) // last percent, output literally
1434 else if (fmt
[epos
+1] = '%') then // special case
1437 writer((PAnsiChar(fmt
)+spos
-1)^, epos
-spos
);
1442 writer((PAnsiChar(fmt
)+spos
-1)^, epos
-spos
);
1447 // check if we have argument for this format string
1448 if (curarg
> High(args
)) then
1450 xwrite('<OUT OF ARGS>');
1451 writer((PAnsiChar(fmt
)+spos
-1)^, Length(fmt
)-spos
+1);
1455 if (spos
+1 > Length(fmt
)) then break
; // oops
1456 assert(fmt
[spos
] = '%');
1458 // parse format; check for sign
1459 if (fmt
[spos
] = '-') then begin sign
:= '-'; Inc(spos
); end
1460 else if (fmt
[spos
] = '+') then begin sign
:= '+'; Inc(spos
); end
1463 if (spos
> Length(fmt
)) then begin xwrite('<INVALID FORMAT>'); break
; end;
1464 if (sign
<> ' ') or ((fmt
[spos
] >= '0') and (fmt
[spos
] <= '9')) then
1466 if (fmt
[spos
] < '0') or (fmt
[spos
] > '9') then begin xwrite('<INVALID FORMAT>'); writer((PAnsiChar(fmt
)+spos
-1)^, Length(fmt
)-spos
+1); break
; end;
1467 zeropad
:= (fmt
[spos
] = '0');
1469 while (spos
<= Length(fmt
)) do
1472 if (ch
< '0') or (ch
> '9') then break
;
1473 width
:= width
*10+Integer(ch
)-48;
1484 if (spos
<= Length(fmt
)) and (fmt
[spos
] = '.') then
1487 if (spos
> Length(fmt
)) then begin xwrite('<INVALID FORMAT>'); break
; end;
1488 if (fmt
[spos
] < '0') or (fmt
[spos
] > '9') then begin xwrite('<INVALID FORMAT>'); writer((PAnsiChar(fmt
)+spos
-1)^, Length(fmt
)-spos
+1); break
; end;
1490 while (spos
<= Length(fmt
)) do
1493 if (ch
< '0') or (ch
> '9') then break
;
1494 prec
:= prec
*10+Integer(ch
)-48;
1499 if (spos
> Length(fmt
)) then begin xwrite('<INVALID FORMAT>'); break
; end;
1502 // done parsing format, check for valid format chars
1503 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;
1504 // now write formatted string
1505 case args
[curarg
].VType
of
1506 vtInteger
: // args[curarg].VInteger
1508 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;
1509 if (fmtch
= 's') then fmtch
:= 'd';
1511 strblen
:= snprintf(@strbuf
[0], Length(strbuf
), @fmtbuf
[0], args
[curarg
].VInteger
);
1514 vtBoolean
: // args[curarg].VBoolean
1519 if args
[curarg
].VBoolean
then strblen
:= snprintf(@strbuf
[0], Length(strbuf
), @fmtbuf
[0], 'true')
1520 else strblen
:= snprintf(@strbuf
[0], Length(strbuf
), @fmtbuf
[0], 'false');
1526 if args
[curarg
].VBoolean
then strblen
:= snprintf(@strbuf
[0], Length(strbuf
), @fmtbuf
[0], AnsiChar('t'))
1527 else strblen
:= snprintf(@strbuf
[0], Length(strbuf
), @fmtbuf
[0], AnsiChar('f'));
1533 strblen
:= snprintf(@strbuf
[0], Length(strbuf
), @fmtbuf
[0], Integer(args
[curarg
].VBoolean
));
1538 xwrite('<INVALID FORMAT CHAR>');
1539 writer((PAnsiChar(fmt
)+spos
-1)^, Length(fmt
)-spos
+1);
1543 vtChar
: // args[curarg].VChar
1549 strblen
:= snprintf(@strbuf
[0], Length(strbuf
), @fmtbuf
[0], args
[curarg
].VChar
);
1555 strblen
:= snprintf(@strbuf
[0], Length(strbuf
), @fmtbuf
[0], Integer(args
[curarg
].VChar
));
1560 xwrite('<INVALID FORMAT CHAR>');
1561 writer((PAnsiChar(fmt
)+spos
-1)^, Length(fmt
)-spos
+1);
1565 //vtWideChar: begin end; // args[curarg].VWideChar (WideChar)
1566 vtExtended
: // args[curarg].VExtended^
1572 strblen
:= snprintf(@strbuf
[0], Length(strbuf
), @fmtbuf
[0], Double(args
[curarg
].VExtended
^));
1578 strblen
:= snprintf(@strbuf
[0], Length(strbuf
), @fmtbuf
[0], Double(args
[curarg
].VExtended
^));
1584 strblen
:= snprintf(@strbuf
[0], Length(strbuf
), @fmtbuf
[0], Integer(trunc(args
[curarg
].VExtended
^)));
1590 strblen
:= snprintf(@strbuf
[0], Length(strbuf
), @fmtbuf
[0], LongWord(trunc(args
[curarg
].VExtended
^)));
1595 xwrite('<INVALID FORMAT CHAR>');
1596 writer((PAnsiChar(fmt
)+spos
-1)^, Length(fmt
)-spos
+1);
1600 vtString
: // args[curarg].VString^ (PShortString)
1602 if (sign
<> '-') then indent(width
-Length(args
[curarg
].VString
^));
1603 writer(args
[curarg
].VString
^[1], Length(args
[curarg
].VString
^));
1604 if (sign
= '-') then indent(width
-Length(args
[curarg
].VString
^));
1606 vtPointer
: // args[curarg].VPointer
1611 if (width
< 8) then width
:= 8;
1614 strblen
:= snprintf(@strbuf
[0], Length(strbuf
), @fmtbuf
[0], PtrUInt(args
[curarg
].VPointer
));
1617 'u', 'd', 'x', 'p', 'X':
1619 if (fmtch
= 'p') then fmtch
:= 'x';
1620 if (width
< 8) then width
:= 8;
1623 strblen
:= snprintf(@strbuf
[0], Length(strbuf
), @fmtbuf
[0], PtrUInt(args
[curarg
].VPointer
));
1628 xwrite('<INVALID FORMAT CHAR>');
1629 writer((PAnsiChar(fmt
)+spos
-1)^, Length(fmt
)-spos
+1);
1633 vtPChar
: // args[curarg].VPChar
1634 if (args
[curarg
].VPChar
= nil) then
1636 if (sign
<> '-') then indent(width
-3);
1638 if (sign
= '-') then indent(width
-3);
1643 while (args
[curarg
].VPChar
[pclen
] <> #0) do Inc(pclen
);
1644 if (sign
<> '-') then indent(width
-pclen
);
1645 writer(args
[curarg
].VPChar
^, pclen
);
1646 if (sign
= '-') then indent(width
-pclen
);
1648 vtObject
: // args[curarg].VObject.Classname (TObject)
1650 if (args
[curarg
].VObject
<> nil) then ccname
:= args
[curarg
].VObject
.Classname
else ccname
:= '<nil>';
1651 if (sign
<> '-') then indent(width
-Length(ccname
));
1653 if (sign
= '-') then indent(width
-Length(ccname
));
1655 vtClass
: // args[curarg].VClass.Classname (TClass)
1657 if (args
[curarg
].VClass
<> nil) then ccname
:= args
[curarg
].VClass
.Classname
else ccname
:= '<nil>';
1658 if (sign
<> '-') then indent(width
-Length(ccname
));
1660 if (sign
= '-') then indent(width
-Length(ccname
));
1662 //vtPWideChar: begin end; // args[curarg].VPWideChar (PWideChar)
1663 vtAnsiString
: // AnsiString(args[curarg].VAnsiString) (Pointer)
1665 if (sign
<> '-') then indent(width
-Length(AnsiString(args
[curarg
].VAnsiString
)));
1666 xwrite(AnsiString(args
[curarg
].VAnsiString
));
1667 if (sign
= '-') then indent(width
-Length(AnsiString(args
[curarg
].VAnsiString
)));
1669 //vtCurrency: begin end; // args[curarg].VCurrency (PCurrency)
1670 //vtVariant: begin end; // args[curarg].VVariant^ (PVariant)
1671 //vtInterface: begin end; // args[curarg].VInterface (Pointer);
1672 //vtWideString: begin end; // args[curarg].VWideString (Pointer);
1673 vtInt64
: // args[curarg].VInt64^ (PInt64)
1676 's','d','u': pc
:= i642str(args
[curarg
].VInt64
^, false, false);
1677 'x': pc
:= i642str(args
[curarg
].VInt64
^, true, false);
1678 'X': pc
:= i642str(args
[curarg
].VInt64
^, true, true);
1679 else begin xwrite('<INVALID FORMAT CHAR>'); writer((PAnsiChar(fmt
)+spos
-1)^, Length(fmt
)-spos
+1); break
; end;
1682 while (pc
[pclen
] <> #0) do Inc(pclen
);
1683 if (sign
<> '-') and (width
> pclen
) then
1687 if (pc
[0] = '-') or (pc
[0] = '+') then
1690 indent0(width
-pclen
-1);
1696 indent0(width
-pclen
);
1701 indent(width
-pclen
);
1705 if (sign
= '-') then indent(width
-pclen
);
1707 vtQWord
: // args[curarg].VQWord^ (PQWord)
1710 's','d','u': pc
:= ui642str(args
[curarg
].VInt64
^, false, false);
1711 'x': pc
:= ui642str(args
[curarg
].VInt64
^, true, false);
1712 'X': pc
:= ui642str(args
[curarg
].VInt64
^, true, true);
1713 else begin xwrite('<INVALID FORMAT CHAR>'); writer((PAnsiChar(fmt
)+spos
-1)^, Length(fmt
)-spos
+1); break
; end;
1716 while (pc
[pclen
] <> #0) do Inc(pclen
);
1717 if (sign
<> '-') then begin if zeropad
then indent0(width
-pclen
) else indent(width
-pclen
); end;
1719 if (sign
= '-') then indent(width
-pclen
);
1723 xwrite('<INVALID TYPE>');
1724 writer((PAnsiChar(fmt
)+spos
-1)^, Length(fmt
)-spos
+1);
1737 i64: Int64 = -$A000000000;
1738 ui64: UInt64 = $A000000000;
1740 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']);
1741 writef(conwriter, 'test float:<%s;%u;%f;%g>'#10, [666.6942, 666.6942, 666.6942, 666.6942]);
1744 writef(conwriter, 'test ss:<%5s;%040s>'#10, [ss, ss]);
1745 writef(conwriter, 'test ls:<%5s;%040s>'#10, [ls, ls]);
1746 writef(conwriter, 'test pointer:<%s;%x;%p>'#10, [@ss, @ss, @ss]);
1747 writef(conwriter, 'test i64:<%s;%x;%015d;%u;%X>'#10, [i64, i64, i64, i64, i64]);
1748 writef(conwriter, 'test ui64:<%s;%x;%15d;%015u;%X>'#10, [ui64, ui64, ui64, ui64, ui64]);