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 SSArray
= array of ShortString;
30 // ////////////////////////////////////////////////////////////////////////// //
32 TUtf8DecoderFast
= packed record
34 const Replacement
= $FFFD; // replacement char for invalid unicode
42 codepoint
: LongWord; // decoded codepoint (valid only when decoder is in "complete" state)
45 constructor Create (v
: Boolean{fuck you, fpc});
47 procedure reset (); inline;
49 function complete (): Boolean; inline; // is current character complete? take `codepoint` then
50 function invalid (): Boolean; inline;
51 function completeOrInvalid (): Boolean; inline;
53 // process one byte, return `true` if codepoint is ready
54 function decode (b
: Byte): Boolean; inline; overload
;
55 function decode (c
: AnsiChar): Boolean; inline; overload
;
59 // ////////////////////////////////////////////////////////////////////////// //
60 function getFilenameExt (const fn
: AnsiString): AnsiString;
61 function setFilenameExt (const fn
, ext
: AnsiString): AnsiString;
62 function forceFilenameExt (const fn
, ext
: AnsiString): AnsiString;
64 // strips out name from `fn`, leaving trailing slash
65 function getFilenamePath (const fn
: AnsiString): AnsiString;
67 // ends with '/' or '\'?
68 function isFilenamePath (const fn
: AnsiString): Boolean;
70 // strips extra trailing slashes in `path, and extra leading slashes in `fn`
71 // will add slash to `path`, even if `fn` is empty!
72 function filenameConcat (const path
, fn
: AnsiString): AnsiString;
74 // does filename have one of ".wad", ".pk3", ".zip" extensions?
75 function hasWadExtension (const fn
: AnsiString): Boolean;
77 // does filepath have ".XXX:\" in it?
78 function isWadPath (const fn
: AnsiString): Boolean;
80 // adds ".wad" extension if filename doesn't have one of ".wad", ".pk3", ".zip"
81 function addWadExtension (const fn
: AnsiString): AnsiString;
83 // convert number to strig with nice commas
84 function Int64ToStrComma (i
: Int64): AnsiString;
86 function UpCase1251 (ch
: Char): Char;
87 function LoCase1251 (ch
: Char): Char;
89 function toLowerCase1251 (const s
: AnsiString): AnsiString;
91 // `true` if strings are equal; ignoring case for cp1251
92 function StrEquCI1251 (const s0
, s1
: AnsiString): Boolean;
94 function utf8Valid (const s
: AnsiString): Boolean;
96 function utf8to1251 (s
: AnsiString): AnsiString;
98 // `pathname` will be modified if path is valid
99 // `lastIsDir` should be `true` if we are searching for directory
100 // nobody cares about shitdoze, so i'll use the same code path for it
101 function findFileCI (var pathname
: AnsiString; lastIsDir
: Boolean=false): Boolean;
104 function openDiskFileRO (pathname
: AnsiString): TStream
;
105 function createDiskFile (pathname
: AnsiString): TStream
;
108 procedure writeSign (st
: TStream
; const sign
: AnsiString);
109 function checkSign (st
: TStream
; const sign
: AnsiString): Boolean;
111 procedure writeBool (st
: TStream
; b
: Boolean);
112 function readBool (st
: TStream
): Boolean;
114 procedure writeStr (st
: TStream
; const str
: AnsiString; maxlen
: LongWord=65535);
115 function readStr (st
: TStream
; maxlen
: LongWord=65535): AnsiString;
117 procedure writeInt (st
: TStream
; v
: Byte); overload
;
118 procedure writeInt (st
: TStream
; v
: ShortInt); overload
;
119 procedure writeInt (st
: TStream
; v
: Word); overload
;
120 procedure writeInt (st
: TStream
; v
: SmallInt); overload
;
121 procedure writeInt (st
: TStream
; v
: LongWord); overload
;
122 procedure writeInt (st
: TStream
; v
: LongInt); overload
;
123 procedure writeInt (st
: TStream
; v
: Int64); overload
;
124 procedure writeInt (st
: TStream
; v
: UInt64); overload
;
126 function readByte (st
: TStream
): Byte;
127 function readShortInt (st
: TStream
): ShortInt;
128 function readWord (st
: TStream
): Word;
129 function readSmallInt (st
: TStream
): SmallInt;
130 function readLongWord (st
: TStream
): LongWord;
131 function readLongInt (st
: TStream
): LongInt;
132 function readInt64 (st
: TStream
): Int64;
133 function readUInt64 (st
: TStream
): UInt64;
136 procedure writeIntBE (st
: TStream
; v
: Byte); overload
;
137 procedure writeIntBE (st
: TStream
; v
: ShortInt); overload
;
138 procedure writeIntBE (st
: TStream
; v
: Word); overload
;
139 procedure writeIntBE (st
: TStream
; v
: SmallInt); overload
;
140 procedure writeIntBE (st
: TStream
; v
: LongWord); overload
;
141 procedure writeIntBE (st
: TStream
; v
: LongInt); overload
;
142 procedure writeIntBE (st
: TStream
; v
: Int64); overload
;
143 procedure writeIntBE (st
: TStream
; v
: UInt64); overload
;
145 function readByteBE (st
: TStream
): Byte;
146 function readShortIntBE (st
: TStream
): ShortInt;
147 function readWordBE (st
: TStream
): Word;
148 function readSmallIntBE (st
: TStream
): SmallInt;
149 function readLongWordBE (st
: TStream
): LongWord;
150 function readLongIntBE (st
: TStream
): LongInt;
151 function readInt64BE (st
: TStream
): Int64;
152 function readUInt64BE (st
: TStream
): UInt64;
155 function nmin (a
, b
: Byte): Byte; inline; overload
;
156 function nmin (a
, b
: ShortInt): ShortInt; inline; overload
;
157 function nmin (a
, b
: Word): Word; inline; overload
;
158 function nmin (a
, b
: SmallInt): SmallInt; inline; overload
;
159 function nmin (a
, b
: LongWord): LongWord; inline; overload
;
160 function nmin (a
, b
: LongInt): LongInt; inline; overload
;
161 function nmin (a
, b
: Int64): Int64; inline; overload
;
162 function nmin (a
, b
: UInt64): UInt64; inline; overload
;
163 function nmin (a
, b
: Single): Single; inline; overload
;
164 function nmin (a
, b
: Double): Double; inline; overload
;
165 function nmin (a
, b
: Extended
): Extended
; inline; overload
;
167 function nmax (a
, b
: Byte): Byte; inline; overload
;
168 function nmax (a
, b
: ShortInt): ShortInt; inline; overload
;
169 function nmax (a
, b
: Word): Word; inline; overload
;
170 function nmax (a
, b
: SmallInt): SmallInt; inline; overload
;
171 function nmax (a
, b
: LongWord): LongWord; inline; overload
;
172 function nmax (a
, b
: LongInt): LongInt; inline; overload
;
173 function nmax (a
, b
: Int64): Int64; inline; overload
;
174 function nmax (a
, b
: UInt64): UInt64; inline; overload
;
175 function nmax (a
, b
: Single): Single; inline; overload
;
176 function nmax (a
, b
: Double): Double; inline; overload
;
177 function nmax (a
, b
: Extended
): Extended
; inline; overload
;
179 function nclamp (v
, a
, b
: Byte): Byte; inline; overload
;
180 function nclamp (v
, a
, b
: ShortInt): ShortInt; inline; overload
;
181 function nclamp (v
, a
, b
: Word): Word; inline; overload
;
182 function nclamp (v
, a
, b
: SmallInt): SmallInt; inline; overload
;
183 function nclamp (v
, a
, b
: LongWord): LongWord; inline; overload
;
184 function nclamp (v
, a
, b
: LongInt): LongInt; inline; overload
;
185 function nclamp (v
, a
, b
: Int64): Int64; inline; overload
;
186 function nclamp (v
, a
, b
: UInt64): UInt64; inline; overload
;
187 function nclamp (v
, a
, b
: Single): Single; inline; overload
;
188 function nclamp (v
, a
, b
: Double): Double; inline; overload
;
189 function nclamp (v
, a
, b
: Extended
): Extended
; inline; overload
;
193 TFormatStrFCallback
= procedure (constref buf
; len
: SizeUInt
);
195 // returns formatted string if `writerCB` is `nil`, empty string otherwise
196 function formatstrf (const fmt
: AnsiString; const args
: array of const; writerCB
: TFormatStrFCallback
=nil): AnsiString;
198 function wchar2win (wc
: WideChar): AnsiChar; inline;
199 function utf2win (const s
: AnsiString): AnsiString;
200 function win2utf (const s
: AnsiString): AnsiString;
201 function digitInBase (ch
: AnsiChar; base
: Integer): Integer;
203 // returns string in single or double quotes
204 // single quotes supports only pascal-style '' for single quote char
205 // double quotes supports c-style escapes
206 // function will select quote mode automatically
207 function quoteStr (const s
: AnsiString): AnsiString;
211 generic TSimpleList
<ItemT
> = class
213 //type PItemT = ^ItemT;
214 type TItemArr
= array of ItemT
;
224 constructor Create (const aitems
: TItemArr
; acount
: Integer);
225 function MoveNext
: Boolean;
226 function getCurrent (): ItemT
;
227 property Current
: ItemT read getCurrent
;
232 mCount
: Integer; // can be less than `mItems` size
235 function getAt (idx
: Integer): ItemT
; inline;
236 procedure setAt (idx
: Integer; const it
: ItemT
); inline;
238 function getCapacity (): Integer; inline;
239 procedure setCapacity (v
: Integer); inline;
242 constructor Create (acapacity
: Integer=-1);
243 destructor Destroy (); override;
245 //WARNING! don't change list contents in `for ... in`!
246 function GetEnumerator (): TEnumerator
;
248 procedure reset (); inline; // won't resize `mItems`
249 procedure clear (); inline;
251 procedure append (constref it
: ItemT
); inline;
252 procedure delete (idx
: Integer); inline;
253 function remove (idx
: Integer): ItemT
; inline;
256 property count
: Integer read mCount
;
257 property capacity
: Integer read getCapacity write setCapacity
;
258 property at
[idx
: Integer]: ItemT read getAt write setAt
; default
;
262 procedure FillMemory (Dest
: Pointer; Len
: LongWord; Ch
: Byte); inline;
263 procedure CopyMemory (Dest
: Pointer; Src
: Pointer; Len
: LongWord); inline;
264 procedure ZeroMemory (Dest
: Pointer; Len
: LongWord); inline;
273 // ////////////////////////////////////////////////////////////////////////// //
274 procedure CopyMemory (Dest
: Pointer; Src
: Pointer; Len
: LongWord); inline;
276 Move(Src
^, Dest
^, Len
);
279 procedure FillMemory (Dest
: Pointer; Len
: LongWord; Ch
: Byte); inline;
281 FillChar(Dest
^, Len
, Ch
);
284 procedure ZeroMemory (Dest
: Pointer; Len
: LongWord); inline;
286 FillChar(Dest
^, Len
, 0);
290 // ////////////////////////////////////////////////////////////////////////// //
291 constructor TSimpleList
.TEnumerator
.Create (const aitems
: TItemArr
; acount
: Integer);
298 function TSimpleList
.TEnumerator
.MoveNext
: Boolean;
301 result
:= (mCurrent
< mCount
);
304 function TSimpleList
.TEnumerator
.getCurrent (): ItemT
;
306 result
:= mItems
[mCurrent
];
310 // ////////////////////////////////////////////////////////////////////////// //
311 constructor TSimpleList
.Create (acapacity
: Integer=-1);
314 if (acapacity
> 0) then SetLength(mItems
, acapacity
);
319 destructor TSimpleList
.Destroy ();
326 function TSimpleList
.getCapacity (): Integer; inline;
328 result
:= Length(mItems
);
332 procedure TSimpleList
.setCapacity (v
: Integer); inline;
334 if (v
< mCount
) then v
:= mCount
;
335 if (v
<> Length(mItems
)) then SetLength(mItems
, v
);
339 function TSimpleList
.GetEnumerator (): TEnumerator
;
341 if (Length(mItems
) > 0) then result
:= TEnumerator
.Create(mItems
, mCount
)
342 else result
:= TEnumerator
.Create(nil, -1);
346 procedure TSimpleList
.reset (); inline;
352 procedure TSimpleList
.clear (); inline;
359 function TSimpleList
.getAt (idx
: Integer): ItemT
; inline;
361 if (idx
>= 0) and (idx
< mCount
) then result
:= mItems
[idx
] else result
:= Default(ItemT
);
365 procedure TSimpleList
.setAt (idx
: Integer; const it
: ItemT
); inline;
367 if (idx
>= 0) and (idx
< mCount
) then mItems
[idx
] := it
;
371 procedure TSimpleList
.append (constref it
: ItemT
); inline;
375 if (mCount
>= Length(mItems
)) then
377 newsz
:= mCount
+(mCount
div 3)+128;
378 SetLength(mItems
, newsz
);
380 mItems
[mCount
] := it
;
385 procedure TSimpleList
.delete (idx
: Integer); inline;
389 if (idx
>= 0) and (idx
< mCount
) then
391 for f
:= idx
+1 to mCount
-1 do mItems
[f
-1] := mItems
[f
];
396 function TSimpleList
.remove (idx
: Integer): ItemT
; inline;
400 if (idx
>= 0) and (idx
< mCount
) then
402 result
:= mItems
[idx
];
403 for f
:= idx
+1 to mCount
-1 do mItems
[f
-1] := mItems
[f
];
407 result
:= Default(ItemT
);
412 // ////////////////////////////////////////////////////////////////////////// //
414 wc2shitmap
: array[0..65535] of AnsiChar;
415 wc2shitmapInited
: Boolean = false;
418 // ////////////////////////////////////////////////////////////////////////// //
420 cp1251
: array[0..127] of Word = (
421 $0402,$0403,$201A,$0453,$201E,$2026,$2020,$2021,$20AC,$2030,$0409,$2039,$040A,$040C,$040B,$040F,
422 $0452,$2018,$2019,$201C,$201D,$2022,$2013,$2014,$003F,$2122,$0459,$203A,$045A,$045C,$045B,$045F,
423 $00A0,$040E,$045E,$0408,$00A4,$0490,$00A6,$00A7,$0401,$00A9,$0404,$00AB,$00AC,$00AD,$00AE,$0407,
424 $00B0,$00B1,$0406,$0456,$0491,$00B5,$00B6,$00B7,$0451,$2116,$0454,$00BB,$0458,$0405,$0455,$0457,
425 $0410,$0411,$0412,$0413,$0414,$0415,$0416,$0417,$0418,$0419,$041A,$041B,$041C,$041D,$041E,$041F,
426 $0420,$0421,$0422,$0423,$0424,$0425,$0426,$0427,$0428,$0429,$042A,$042B,$042C,$042D,$042E,$042F,
427 $0430,$0431,$0432,$0433,$0434,$0435,$0436,$0437,$0438,$0439,$043A,$043B,$043C,$043D,$043E,$043F,
428 $0440,$0441,$0442,$0443,$0444,$0445,$0446,$0447,$0448,$0449,$044A,$044B,$044C,$044D,$044E,$044F
432 procedure initShitMap ();
436 for f
:= 0 to High(wc2shitmap
) do wc2shitmap
[f
] := '?';
437 for f
:= 0 to 127 do wc2shitmap
[f
] := AnsiChar(f
);
438 for f
:= 0 to 127 do wc2shitmap
[cp1251
[f
]] := AnsiChar(f
+128);
439 wc2shitmapInited
:= true;
443 // ////////////////////////////////////////////////////////////////////////// //
444 // fast state-machine based UTF-8 decoder; using 8 bytes of memory
445 // code points from invalid range will never be valid, this is the property of the state machine
447 // see http://bjoern.hoehrmann.de/utf-8/decoder/dfa/
448 utf8dfa
: array[0..$16c-1] of Byte = (
449 // maps bytes to character classes
450 $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00, // 00-0f
451 $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00, // 10-1f
452 $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00, // 20-2f
453 $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00, // 30-3f
454 $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00, // 40-4f
455 $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00, // 50-5f
456 $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00, // 60-6f
457 $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00, // 70-7f
458 $01,$01,$01,$01,$01,$01,$01,$01,$01,$01,$01,$01,$01,$01,$01,$01, // 80-8f
459 $09,$09,$09,$09,$09,$09,$09,$09,$09,$09,$09,$09,$09,$09,$09,$09, // 90-9f
460 $07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07, // a0-af
461 $07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07, // b0-bf
462 $08,$08,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02, // c0-cf
463 $02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02, // d0-df
464 $0a,$03,$03,$03,$03,$03,$03,$03,$03,$03,$03,$03,$03,$04,$03,$03, // e0-ef
465 $0b,$06,$06,$06,$05,$08,$08,$08,$08,$08,$08,$08,$08,$08,$08,$08, // f0-ff
466 // maps a combination of a state of the automaton and a character class to a state
467 $00,$0c,$18,$24,$3c,$60,$54,$0c,$0c,$0c,$30,$48,$0c,$0c,$0c,$0c, // 100-10f
468 $0c,$0c,$0c,$0c,$0c,$0c,$0c,$0c,$0c,$00,$0c,$0c,$0c,$0c,$0c,$00, // 110-11f
469 $0c,$00,$0c,$0c,$0c,$18,$0c,$0c,$0c,$0c,$0c,$18,$0c,$18,$0c,$0c, // 120-12f
470 $0c,$0c,$0c,$0c,$0c,$0c,$0c,$18,$0c,$0c,$0c,$0c,$0c,$18,$0c,$0c, // 130-13f
471 $0c,$0c,$0c,$0c,$0c,$18,$0c,$0c,$0c,$0c,$0c,$0c,$0c,$0c,$0c,$24, // 140-14f
472 $0c,$24,$0c,$0c,$0c,$24,$0c,$0c,$0c,$0c,$0c,$24,$0c,$24,$0c,$0c, // 150-15f
473 $0c,$24,$0c,$0c,$0c,$0c,$0c,$0c,$0c,$0c,$0c,$0c);
476 // ////////////////////////////////////////////////////////////////////////// //
477 constructor TUtf8DecoderFast
.Create (v
: Boolean{fuck you, fpc}); begin state
:= Accept
; codepoint
:= 0; end;
479 procedure TUtf8DecoderFast
.reset (); inline; begin state
:= Accept
; codepoint
:= 0; end;
481 function TUtf8DecoderFast
.complete (): Boolean; inline; begin result
:= (state
= Accept
); end;
482 function TUtf8DecoderFast
.invalid (): Boolean; inline; begin result
:= (state
= Reject
); end;
483 function TUtf8DecoderFast
.completeOrInvalid (): Boolean; inline; begin result
:= (state
= Accept
) or (state
= Reject
); end;
485 function TUtf8DecoderFast
.decode (c
: AnsiChar): Boolean; inline; overload
; begin result
:= decode(Byte(c
)); end;
487 function TUtf8DecoderFast
.decode (b
: Byte): Boolean; inline; overload
;
491 if (state
= Reject
) then begin state
:= Accept
; codepoint
:= 0; end;
493 if (state
<> Accept
) then codepoint
:= (b
and $3f) or (codepoint
shl 6) else codepoint
:= ($ff shr tp
) and b
;
494 state
:= utf8dfa
[256+state
+tp
];
495 if (state
= Reject
) then begin codepoint
:= Replacement
; state
:= Accept
; end;
496 result
:= (state
= Accept
);
500 // ////////////////////////////////////////////////////////////////////////// //
501 function wchar2win (wc
: WideChar): AnsiChar; inline;
503 if not wc2shitmapInited
then initShitMap();
504 if (LongWord(wc
) > 65535) then result
:= '?' else result
:= wc2shitmap
[LongWord(wc
)];
508 // ////////////////////////////////////////////////////////////////////////// //
509 function utf2win (const s
: AnsiString): AnsiString;
512 ud
: TUtf8DecoderFast
;
514 for f
:= 1 to Length(s
) do
516 if (Byte(s
[f
]) > 127) then
518 ud
:= TUtf8DecoderFast
.Create(true);
520 for c
:= 1 to Length(s
) do
522 if ud
.decode(s
[c
]) then result
+= wchar2win(WideChar(ud
.codepoint
));
531 function win2utf (const s
: AnsiString): AnsiString;
535 function utf8Encode (code
: Integer): AnsiString;
537 if (code
< 0) or (code
> $10FFFF) then begin result
:= '?'; exit
; end;
538 if (code
<= $7f) then
540 result
:= Char(code
and $ff);
542 else if (code
<= $7FF) then
544 result
:= Char($C0 or (code
shr 6));
545 result
+= Char($80 or (code
and $3F));
547 else if (code
<= $FFFF) then
549 result
:= Char($E0 or (code
shr 12));
550 result
+= Char($80 or ((code
shr 6) and $3F));
551 result
+= Char($80 or (code
and $3F));
553 else if (code
<= $10FFFF) then
555 result
:= Char($F0 or (code
shr 18));
556 result
+= Char($80 or ((code
shr 12) and $3F));
557 result
+= Char($80 or ((code
shr 6) and $3F));
558 result
+= Char($80 or (code
and $3F));
567 for f
:= 1 to Length(s
) do
569 if (Byte(s
[f
]) > 127) then
572 for c
:= 1 to Length(s
) do
574 if (Byte(s
[c
]) < 128) then
580 result
+= utf8Encode(cp1251
[Byte(s
[c
])-128])
590 // ////////////////////////////////////////////////////////////////////////// //
591 function digitInBase (ch
: AnsiChar; base
: Integer): Integer;
594 if (base
< 1) or (base
> 36) then exit
;
595 if (ch
< '0') then exit
;
598 if (Integer(ch
) >= 48+base
) then exit
;
599 result
:= Integer(ch
)-48;
603 if (ch
>= '0') and (ch
<= '9') then begin result
:= Integer(ch
)-48; exit
; end;
604 if (ch
>= 'a') and (ch
<= 'z') then Dec(ch
, 32); // poor man's tolower()
605 if (ch
< 'A') or (Integer(ch
) >= 65+(base
-10)) then exit
;
606 result
:= Integer(ch
)-65+10;
611 // ////////////////////////////////////////////////////////////////////////// //
612 function quoteStr (const s
: AnsiString): AnsiString;
614 function squote (const s
: AnsiString): AnsiString;
619 for f
:= 1 to Length(s
) do
621 if (s
[f
] = '''') then result
+= '''';
627 function dquote (const s
: AnsiString): AnsiString;
633 for f
:= 1 to Length(s
) do
636 if (ch
= #0) then result
+= '\z'
637 else if (ch
= #9) then result
+= '\t'
638 else if (ch
= #10) then result
+= '\n'
639 else if (ch
= #13) then result
+= '\r'
640 else if (ch
= #27) then result
+= '\e'
641 else if (ch
< ' ') or (ch
= #127) then
644 result
+= LowerCase(IntToHex(Integer(ch
), 2));
646 else if (ch
= '"') or (ch
= '\') then
660 needSingle
: Boolean = false;
663 for f
:= 1 to Length(s
) do
665 if (s
[f
] = '''') then begin needSingle
:= true; continue
; end;
666 if (s
[f
] < ' ') or (s
[f
] = #127) then begin result
:= dquote(s
); exit
; end;
668 if needSingle
then result
:= squote(s
) else result
:= ''''+s
+'''';
672 // ////////////////////////////////////////////////////////////////////////// //
673 function getFilenameExt (const fn
: AnsiString): AnsiString;
684 if (pos
= Length(fn
)) then result
:= '' else result
:= Copy(fn
, pos
, Length(fn
)-pos
+1);
687 if (ch
= '/') or (ch
= '\') then break
;
690 result
:= ''; // no extension
694 function setFilenameExt (const fn
, ext
: AnsiString): AnsiString;
700 if (Length(ext
) = 0) or (ext
= '.') then exit
;
705 if (ch
= '.') then exit
;
706 if (ch
= '/') or (ch
= '\') then break
;
709 if (ext
[1] <> '.') then result
+= '.';
714 function forceFilenameExt (const fn
, ext
: AnsiString): AnsiString;
726 if (Length(ext
) = 0) or (ext
= '.') then
728 result
:= Copy(fn
, 1, pos
-1);
732 if (ext
[1] = '.') then result
:= Copy(fn
, 1, pos
-1) else result
:= Copy(fn
, 1, pos
);
737 if (ch
= '/') or (ch
= '\') then break
;
740 if (Length(ext
) > 0) then
742 if (ext
[1] <> '.') then result
+= '.';
748 // strips out name from `fn`, leaving trailing slash
749 function getFilenamePath (const fn
: AnsiString): AnsiString;
754 if (Length(fn
) = 0) then begin result
:= './'; exit
; end;
755 if (fn
[Length(fn
)] = '/') or (fn
[Length(fn
)] = '\') then begin result
:= fn
; exit
; end;
760 if (ch
= '/') or (ch
= '\') then begin result
:= Copy(fn
, 1, pos
); exit
; end;
763 result
:= './'; // no path -> current dir
767 // ends with '/' or '\'?
768 function isFilenamePath (const fn
: AnsiString): Boolean;
770 if (Length(fn
) = 0) then
776 result
:= (fn
[Length(fn
)] = '/') or (fn
[Length(fn
)] = '\');
781 // strips extra trailing slashes in `path, and extra leading slashes in `fn`
782 // will add slash to `path`, even if `fn` is empty!
783 function filenameConcat (const path
, fn
: AnsiString): AnsiString;
788 while (pos
<= Length(fn
)) and ((fn
[pos
] = '/') or (fn
[pos
] = '\')) do Inc(pos
);
790 if (Length(result
) > 0) and ((result
[Length(result
)] <> '/') and (result
[Length(result
)] <> '\')) then result
+= '/';
791 if (pos
<= Length(fn
)) then
793 result
+= Copy(fn
, pos
, Length(fn
)-pos
+1);
794 //FIXME: make this faster!
795 while (Length(result
) > 0) and ((result
[Length(result
)] = '/') or (result
[Length(result
)] = '\')) do
797 Delete(result
, Length(result
), 1);
799 if (fn
[Length(fn
)] = '/') or (fn
[Length(fn
)] = '\') then result
+= '/';
804 function hasWadExtension (const fn
: AnsiString): Boolean;
808 ext
:= getFilenameExt(fn
);
809 result
:= StrEquCI1251(ext
, '.wad') or StrEquCI1251(ext
, '.pk3') or StrEquCI1251(ext
, '.zip');
813 function addWadExtension (const fn
: AnsiString): AnsiString;
816 if not hasWadExtension(result
) then result
:= result
+'.wad';
820 function isWadPath (const fn
: AnsiString): Boolean;
827 while (pos
<= Length(fn
)) do
829 if (fn
[pos
] = ':') then
831 if (Length(fn
)-pos
< 1) then break
;
832 if (pos
-4 > 1) and (fn
[pos
-4] = '.') and ((fn
[pos
+1] = '\') or (fn
[pos
+1] = '/')) then
834 s
:= Copy(fn
, pos
-4, 4);
835 if StrEquCI1251(s
, '.wad') or StrEquCI1251(s
, '.pk3') or StrEquCI1251(s
, '.zip') then
847 function Int64ToStrComma (i
: Int64): AnsiString;
852 f
:= Length(result
)+1;
855 Dec(f
, 3); Insert(',', result
, f
);
860 function UpCase1251 (ch
: Char): Char;
864 if (ch
>= 'a') and (ch
<= 'z') then Dec(ch
, 32);
868 if (ch
>= #224) and (ch
<= #255) then
875 #184, #186, #191: Dec(ch
, 16);
884 function LoCase1251 (ch
: Char): Char;
888 if (ch
>= 'A') and (ch
<= 'Z') then Inc(ch
, 32);
892 if (ch
>= #192) and (ch
<= #223) then
899 #168, #170, #175: Inc(ch
, 16);
908 function StrEquCI1251 (const s0
, s1
: AnsiString): Boolean;
913 if length(s0
) <> length(s1
) then exit
;
914 for i
:= 1 to length(s0
) do if UpCase1251(s0
[i
]) <> UpCase1251(s1
[i
]) then exit
;
919 function toLowerCase1251 (const s
: AnsiString): AnsiString;
926 if (ch
<> LoCase1251(ch
)) then
929 SetLength(result
, Length(s
));
930 for f
:= 1 to Length(s
) do result
[f
] := LoCase1251(s
[f
]);
939 // ////////////////////////////////////////////////////////////////////////// //
943 function utf8CodeLen (ch
: Word): Integer;
945 if ch
< $80 then begin result
:= 1; exit
; end;
946 if (ch
and $FE) = $FC then begin result
:= 6; exit
; end;
947 if (ch
and $FC) = $F8 then begin result
:= 5; exit
; end;
948 if (ch
and $F8) = $F0 then begin result
:= 4; exit
; end;
949 if (ch
and $F0) = $E0 then begin result
:= 3; exit
; end;
950 if (ch
and $E0) = $C0 then begin result
:= 2; exit
; end;
951 result
:= -1; // invalid
955 function utf8Valid (const s
: AnsiString): Boolean;
961 while pos
<= length(s
) do
963 len
:= utf8CodeLen(Byte(s
[pos
]));
964 if len
< 1 then exit
; // invalid sequence start
965 if pos
+len
-1 > length(s
) then exit
; // out of chars in string
968 // check other sequence bytes
971 if (Byte(s
[pos
]) and $C0) <> $80 then exit
;
980 // ////////////////////////////////////////////////////////////////////////// //
982 uni2wint
: array [128..255] of Word = (
983 $0402,$0403,$201A,$0453,$201E,$2026,$2020,$2021,$20AC,$2030,$0409,$2039,$040A,$040C,$040B,$040F,
984 $0452,$2018,$2019,$201C,$201D,$2022,$2013,$2014,$003F,$2122,$0459,$203A,$045A,$045C,$045B,$045F,
985 $00A0,$040E,$045E,$0408,$00A4,$0490,$00A6,$00A7,$0401,$00A9,$0404,$00AB,$00AC,$00AD,$00AE,$0407,
986 $00B0,$00B1,$0406,$0456,$0491,$00B5,$00B6,$00B7,$0451,$2116,$0454,$00BB,$0458,$0405,$0455,$0457,
987 $0410,$0411,$0412,$0413,$0414,$0415,$0416,$0417,$0418,$0419,$041A,$041B,$041C,$041D,$041E,$041F,
988 $0420,$0421,$0422,$0423,$0424,$0425,$0426,$0427,$0428,$0429,$042A,$042B,$042C,$042D,$042E,$042F,
989 $0430,$0431,$0432,$0433,$0434,$0435,$0436,$0437,$0438,$0439,$043A,$043B,$043C,$043D,$043E,$043F,
990 $0440,$0441,$0442,$0443,$0444,$0445,$0446,$0447,$0448,$0449,$044A,$044B,$044C,$044D,$044E,$044F
994 function decodeUtf8Char (s
: AnsiString; var pos
: Integer): char;
998 (* The following encodings are valid, except for the 5 and 6 byte
1002 * 1110xxxx 10xxxxxx 10xxxxxx
1003 * 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
1004 * 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
1005 * 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
1008 if pos
> length(s
) then exit
;
1012 if b
< $80 then begin result
:= char(b
); exit
; end;
1014 // mask out unused bits
1015 if (b
and $FE) = $FC then b
:= b
and $01
1016 else if (b
and $FC) = $F8 then b
:= b
and $03
1017 else if (b
and $F8) = $F0 then b
:= b
and $07
1018 else if (b
and $F0) = $E0 then b
:= b
and $0F
1019 else if (b
and $E0) = $C0 then b
:= b
and $1F
1020 else exit
; // invalid utf8
1023 while pos
<= length(s
) do
1026 if (c
and $C0) <> $80 then break
; // no more
1028 b
:= b
or (c
and $3F);
1033 for c
:= 128 to 255 do if uni2wint
[c
] = b
then begin result
:= char(c
and $FF); exit
; end;
1038 function utf8to1251 (s
: AnsiString): AnsiString;
1042 if not utf8Valid(s
) then begin result
:= s
; exit
; end;
1044 while pos
<= length(s
) do
1046 if Byte(s
[pos
]) >= $80 then break
;
1049 if pos
> length(s
) then begin result
:= s
; exit
; end; // nothing to do here
1052 while pos
<= length(s
) do result
:= result
+decodeUtf8Char(s
, pos
);
1056 // ////////////////////////////////////////////////////////////////////////// //
1057 // `pathname` will be modified if path is valid
1058 // `lastIsDir` should be `true` if we are searching for directory
1059 // nobody cares about shitdoze, so i'll use the same code path for it
1060 function findFileCI (var pathname
: AnsiString; lastIsDir
: Boolean=false): Boolean;
1064 newname
: AnsiString = '';
1065 curname
: AnsiString;
1071 result
:= (length(npt
) > 0);
1072 if (length(npt
) > 0) and ((npt
[1] = '/') or (npt
[1] = '\')) then newname
:= '/';
1073 while length(npt
) > 0 do
1075 // remove trailing slashes
1076 while (length(npt
) > 0) and ((npt
[1] = '/') or (npt
[1] = '\')) do Delete(npt
, 1, 1);
1077 if length(npt
) = 0 then break
;
1080 while (length(npt
) > 0) and (npt
[1] <> '/') and (npt
[1] <> '\') do
1082 curname
:= curname
+npt
[1];
1085 // remove trailing slashes again
1086 while (length(npt
) > 0) and ((npt
[1] = '/') or (npt
[1] = '\')) do Delete(npt
, 1, 1);
1087 wantdir
:= lastIsDir
or (length(npt
) > 0); // do we want directory here?
1088 //writeln(Format('npt=[%s]; newname=[%s]; curname=[%s]; wantdir=%d', [npt, newname, curname, Integer(wantdir)]));
1089 // try the easiest case first
1090 attr
:= FileGetAttr(newname
+curname
);
1093 if wantdir
= ((attr
and faDirectory
) <> 0) then
1096 newname
:= newname
+curname
;
1097 if wantdir
then newname
:= newname
+'/';
1101 //writeln(Format('npt=[%s]; newname=[%s]; curname=[%s]; wantdir=%d', [npt, newname, curname, Integer(wantdir)]));
1102 // alas, either not found, or invalid attributes
1105 if FindFirst(newname
+'*', faAnyFile
, sr
) = 0 then
1107 if (wantdir
= ((sr
.attr
and faDirectory
) <> 0)) and StrEquCI1251(sr
.name
, curname
) then
1110 newname
:= newname
+sr
.name
;
1111 if wantdir
then newname
:= newname
+'/';
1115 until FindNext(sr
) <> 0;
1119 if not foundher
then begin newname
:= ''; result
:= false; break
; end;
1121 if result
then pathname
:= newname
;
1125 function openDiskFileRO (pathname
: AnsiString): TStream
;
1127 if not findFileCI(pathname
) then raise Exception
.Create('can''t open file "'+pathname
+'"');
1128 result
:= TFileStream
.Create(pathname
, fmOpenRead
or {fmShareDenyWrite}fmShareDenyNone
);
1131 function createDiskFile (pathname
: AnsiString): TStream
;
1135 path
:= ExtractFilePath(pathname
);
1136 if length(path
) > 0 then
1138 if not findFileCI(path
, true) then raise Exception
.Create('can''t create file "'+pathname
+'"');
1140 result
:= TFileStream
.Create(path
+ExtractFileName(pathname
), fmCreate
);
1144 procedure writeIntegerLE (st
: TStream
; vp
: Pointer; size
: Integer);
1145 {$IFDEF ENDIAN_LITTLE}
1147 st
.writeBuffer(vp
^, size
);
1153 p
:= PByte(vp
)+size
-1;
1156 st
.writeBuffer(p
^, 1);
1163 procedure writeIntegerBE (st
: TStream
; vp
: Pointer; size
: Integer);
1164 {$IFDEF ENDIAN_LITTLE}
1168 p
:= PByte(vp
)+size
-1;
1171 st
.writeBuffer(p
^, 1);
1178 st
.writeBuffer(vp
^, size
);
1182 procedure writeSign (st
: TStream
; const sign
: AnsiString);
1184 if (Length(sign
) > 0) then st
.WriteBuffer(sign
[1], Length(sign
));
1187 function checkSign (st
: TStream
; const sign
: AnsiString): Boolean;
1189 buf
: packed array[0..7] of Char;
1193 if (Length(sign
) > 0) then
1195 if (Length(sign
) <= 8) then
1197 st
.ReadBuffer(buf
[0], Length(sign
));
1198 for f
:= 1 to Length(sign
) do if (buf
[f
-1] <> sign
[f
]) then exit
;
1202 for f
:= 1 to Length(sign
) do
1204 st
.ReadBuffer(buf
[0], 1);
1205 if (buf
[0] <> sign
[f
]) then exit
;
1212 procedure writeInt (st
: TStream
; v
: Byte); overload
; begin writeIntegerLE(st
, @v
, 1); end;
1213 procedure writeInt (st
: TStream
; v
: ShortInt); overload
; begin writeIntegerLE(st
, @v
, 1); end;
1214 procedure writeInt (st
: TStream
; v
: Word); overload
; begin writeIntegerLE(st
, @v
, 2); end;
1215 procedure writeInt (st
: TStream
; v
: SmallInt); overload
; begin writeIntegerLE(st
, @v
, 2); end;
1216 procedure writeInt (st
: TStream
; v
: LongWord); overload
; begin writeIntegerLE(st
, @v
, 4); end;
1217 procedure writeInt (st
: TStream
; v
: LongInt); overload
; begin writeIntegerLE(st
, @v
, 4); end;
1218 procedure writeInt (st
: TStream
; v
: Int64); overload
; begin writeIntegerLE(st
, @v
, 8); end;
1219 procedure writeInt (st
: TStream
; v
: UInt64); overload
; begin writeIntegerLE(st
, @v
, 8); end;
1221 procedure writeIntBE (st
: TStream
; v
: Byte); overload
; begin writeIntegerBE(st
, @v
, 1); end;
1222 procedure writeIntBE (st
: TStream
; v
: ShortInt); overload
; begin writeIntegerBE(st
, @v
, 1); end;
1223 procedure writeIntBE (st
: TStream
; v
: Word); overload
; begin writeIntegerBE(st
, @v
, 2); end;
1224 procedure writeIntBE (st
: TStream
; v
: SmallInt); overload
; begin writeIntegerBE(st
, @v
, 2); end;
1225 procedure writeIntBE (st
: TStream
; v
: LongWord); overload
; begin writeIntegerBE(st
, @v
, 4); end;
1226 procedure writeIntBE (st
: TStream
; v
: LongInt); overload
; begin writeIntegerBE(st
, @v
, 4); end;
1227 procedure writeIntBE (st
: TStream
; v
: Int64); overload
; begin writeIntegerBE(st
, @v
, 8); end;
1228 procedure writeIntBE (st
: TStream
; v
: UInt64); overload
; begin writeIntegerBE(st
, @v
, 8); end;
1230 procedure writeBool (st
: TStream
; b
: Boolean); begin writeInt(st
, Byte(b
)); end;
1231 function readBool (st
: TStream
): Boolean; begin result
:= (readByte(st
) <> 0); end;
1234 procedure writeStr (st
: TStream
; const str
: AnsiString; maxlen
: LongWord=65535);
1236 if (Length(str
) > maxlen
) then raise XStreamError
.Create('string too long');
1237 if (maxlen
<= 65535) then writeInt(st
, Word(Length(str
))) else writeInt(st
, LongWord(Length(str
)));
1238 if (Length(str
) > 0) then st
.WriteBuffer(str
[1], Length(str
));
1241 function readStr (st
: TStream
; maxlen
: LongWord=65535): AnsiString;
1246 if (maxlen
<= 65535) then len
:= readWord(st
) else len
:= Integer(readLongWord(st
));
1247 if (len
< 0) or (len
> maxlen
) then raise XStreamError
.Create('string too long');
1250 SetLength(result
, len
);
1251 st
.ReadBuffer(result
[1], len
);
1256 procedure readIntegerLE (st
: TStream
; vp
: Pointer; size
: Integer);
1257 {$IFDEF ENDIAN_LITTLE}
1259 st
.readBuffer(vp
^, size
);
1265 p
:= PByte(vp
)+size
-1;
1268 st
.readBuffer(p
^, 1);
1275 procedure readIntegerBE (st
: TStream
; vp
: Pointer; size
: Integer);
1276 {$IFDEF ENDIAN_LITTLE}
1280 p
:= PByte(vp
)+size
-1;
1283 st
.readBuffer(p
^, 1);
1290 st
.readBuffer(vp
^, size
);
1294 function readByte (st
: TStream
): Byte; begin readIntegerLE(st
, @result
, 1); end;
1295 function readShortInt (st
: TStream
): ShortInt; begin readIntegerLE(st
, @result
, 1); end;
1296 function readWord (st
: TStream
): Word; begin readIntegerLE(st
, @result
, 2); end;
1297 function readSmallInt (st
: TStream
): SmallInt; begin readIntegerLE(st
, @result
, 2); end;
1298 function readLongWord (st
: TStream
): LongWord; begin readIntegerLE(st
, @result
, 4); end;
1299 function readLongInt (st
: TStream
): LongInt; begin readIntegerLE(st
, @result
, 4); end;
1300 function readInt64 (st
: TStream
): Int64; begin readIntegerLE(st
, @result
, 8); end;
1301 function readUInt64 (st
: TStream
): UInt64; begin readIntegerLE(st
, @result
, 8); end;
1303 function readByteBE (st
: TStream
): Byte; begin readIntegerBE(st
, @result
, 1); end;
1304 function readShortIntBE (st
: TStream
): ShortInt; begin readIntegerBE(st
, @result
, 1); end;
1305 function readWordBE (st
: TStream
): Word; begin readIntegerBE(st
, @result
, 2); end;
1306 function readSmallIntBE (st
: TStream
): SmallInt; begin readIntegerBE(st
, @result
, 2); end;
1307 function readLongWordBE (st
: TStream
): LongWord; begin readIntegerBE(st
, @result
, 4); end;
1308 function readLongIntBE (st
: TStream
): LongInt; begin readIntegerBE(st
, @result
, 4); end;
1309 function readInt64BE (st
: TStream
): Int64; begin readIntegerBE(st
, @result
, 8); end;
1310 function readUInt64BE (st
: TStream
): UInt64; begin readIntegerBE(st
, @result
, 8); end;
1313 // ////////////////////////////////////////////////////////////////////////// //
1314 function nmin (a
, b
: Byte): Byte; inline; overload
; begin if (a
< b
) then result
:= a
else result
:= b
; end;
1315 function nmin (a
, b
: ShortInt): ShortInt; inline; overload
; begin if (a
< b
) then result
:= a
else result
:= b
; end;
1316 function nmin (a
, b
: Word): Word; inline; overload
; begin if (a
< b
) then result
:= a
else result
:= b
; end;
1317 function nmin (a
, b
: SmallInt): SmallInt; inline; overload
; begin if (a
< b
) then result
:= a
else result
:= b
; end;
1318 function nmin (a
, b
: LongWord): LongWord; inline; overload
; begin if (a
< b
) then result
:= a
else result
:= b
; end;
1319 function nmin (a
, b
: LongInt): LongInt; inline; overload
; begin if (a
< b
) then result
:= a
else result
:= b
; end;
1320 function nmin (a
, b
: Int64): Int64; inline; overload
; begin if (a
< b
) then result
:= a
else result
:= b
; end;
1321 function nmin (a
, b
: UInt64): UInt64; inline; overload
; begin if (a
< b
) then result
:= a
else result
:= b
; end;
1322 function nmin (a
, b
: Single): Single; inline; overload
; begin if (a
< b
) then result
:= a
else result
:= b
; end;
1323 function nmin (a
, b
: Double): Double; inline; overload
; begin if (a
< b
) then result
:= a
else result
:= b
; end;
1324 function nmin (a
, b
: Extended
): Extended
; inline; overload
; begin if (a
< b
) then result
:= a
else result
:= b
; end;
1326 function nmax (a
, b
: Byte): Byte; inline; overload
; begin if (a
> b
) then result
:= a
else result
:= b
; end;
1327 function nmax (a
, b
: ShortInt): ShortInt; inline; overload
; begin if (a
> b
) then result
:= a
else result
:= b
; end;
1328 function nmax (a
, b
: Word): Word; inline; overload
; begin if (a
> b
) then result
:= a
else result
:= b
; end;
1329 function nmax (a
, b
: SmallInt): SmallInt; inline; overload
; begin if (a
> b
) then result
:= a
else result
:= b
; end;
1330 function nmax (a
, b
: LongWord): LongWord; inline; overload
; begin if (a
> b
) then result
:= a
else result
:= b
; end;
1331 function nmax (a
, b
: LongInt): LongInt; inline; overload
; begin if (a
> b
) then result
:= a
else result
:= b
; end;
1332 function nmax (a
, b
: Int64): Int64; inline; overload
; begin if (a
> b
) then result
:= a
else result
:= b
; end;
1333 function nmax (a
, b
: UInt64): UInt64; inline; overload
; begin if (a
> b
) then result
:= a
else result
:= b
; end;
1334 function nmax (a
, b
: Single): Single; inline; overload
; begin if (a
> b
) then result
:= a
else result
:= b
; end;
1335 function nmax (a
, b
: Double): Double; inline; overload
; begin if (a
> b
) then result
:= a
else result
:= b
; end;
1336 function nmax (a
, b
: Extended
): Extended
; inline; overload
; begin if (a
> b
) then result
:= a
else result
:= b
; end;
1338 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;
1339 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;
1340 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;
1341 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;
1342 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;
1343 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;
1344 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;
1345 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;
1346 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;
1347 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;
1348 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;
1351 // ////////////////////////////////////////////////////////////////////////// //
1353 function snprintf (buf
: PAnsiChar; bufsize
: SizeUInt
; const fmt
: PAnsiChar): SizeUInt
; cdecl; varargs
; external 'msvcrt.dll' name
'_snprintf';
1355 function snprintf (buf
: PAnsiChar; bufsize
: SizeUInt
; const fmt
: PAnsiChar): SizeUInt
; cdecl; varargs
; external 'libc' name
'snprintf';
1360 procedure conwriter (constref buf; len: SizeUInt);
1366 if (len < 1) then exit;
1370 if (len > 255) then slen := 255 else slen := Integer(len);
1371 Move(b^, ss[1], len);
1372 ss[0] := AnsiChar(slen);
1381 function formatstrf (const fmt
: AnsiString; const args
: array of const; writerCB
: TFormatStrFCallback
=nil): AnsiString;
1383 PadSpaces
: AnsiString = ' ';
1384 PadZeroes
: AnsiString = '00000000000000000000000000000000000000000000000000000000000000000000000';
1386 curarg
: Integer = 0; // current arg in `args`
1387 sign
, fmtch
: AnsiChar;
1389 width
, prec
: Integer; // width and precision
1390 spos
, epos
: Integer;
1392 strbuf
: array[0..256] of AnsiChar;
1394 fmtbuf
: array[0..256] of AnsiChar;
1398 ccname
: ShortString;
1400 procedure writer (constref buf
; len
: SizeUInt
);
1406 if (len
< 1) then exit
;
1408 if assigned(writerCB
) then
1416 if (len
> 255) then slen
:= 255 else slen
:= Integer(len
);
1417 Move(b
^, ss
[1], slen
);
1418 ss
[0] := AnsiChar(slen
);
1426 procedure xwrite (const s
: AnsiString);
1428 if (Length(s
) > 0) then writer(PAnsiChar(s
)^, Length(s
));
1431 procedure putFmtChar (ch
: AnsiChar);
1433 fmtbuf
[fmtblen
] := ch
;
1437 procedure putFmtInt (n
: Integer);
1441 len
:= snprintf(@fmtbuf
[fmtblen
], Length(fmtbuf
)-fmtblen
, '%d', n
);
1442 if (len
> 0) then Inc(fmtblen
, len
);
1445 procedure buildCFormat (const pfx
: AnsiString='');
1450 for f
:= 1 to Length(pfx
) do putFmtChar(pfx
[f
]);
1452 if (sign
<> ' ') then putFmtChar(sign
);
1453 if (width
>= 0) then
1455 if (zeropad
) then putFmtChar('0');
1464 fmtbuf
[fmtblen
] := #0;
1467 procedure writeStrBuf ();
1469 if (strblen
> 0) then writer(strbuf
, strblen
);
1472 function i642str (n
: Int64; hex
: Boolean; hexup
: Boolean): PAnsiChar;
1477 if (n
= $8000000000000000) then
1479 if hex
then snprintf(@strbuf
[0], Length(strbuf
), '-8000000000000000')
1480 else snprintf(@strbuf
[0], Length(strbuf
), '-9223372036854775808');
1481 result
:= @strbuf
[0];
1486 if neg
then n
:= -n
;
1487 xpos
:= High(strbuf
);
1488 strbuf
[xpos
] := #0; Dec(xpos
);
1492 strbuf
[xpos
] := AnsiChar((n
mod 10)+48);
1498 if (n
mod 16 > 9) then
1500 strbuf
[xpos
] := AnsiChar((n
mod 16)+48+7);
1501 if not hexup
then Inc(strbuf
[xpos
], 32);
1503 else strbuf
[xpos
] := AnsiChar((n
mod 16)+48);
1508 if neg
then begin strbuf
[xpos
] := '-'; Dec(xpos
); end;
1509 result
:= @strbuf
[xpos
+1];
1513 function ui642str (n
: UInt64; hex
: Boolean; hexup
: Boolean): PAnsiChar;
1517 xpos
:= High(strbuf
);
1518 strbuf
[xpos
] := #0; Dec(xpos
);
1522 strbuf
[xpos
] := AnsiChar((n
mod 10)+48);
1528 if (n
mod 16 > 9) then
1530 strbuf
[xpos
] := AnsiChar((n
mod 16)+48+7);
1531 if not hexup
then Inc(strbuf
[xpos
], 32);
1533 else strbuf
[xpos
] := AnsiChar((n
mod 16)+48);
1538 result
:= @strbuf
[xpos
+1];
1541 procedure indent (len
: Integer);
1547 if (len
> Length(PadSpaces
)) then ilen
:= Length(PadSpaces
) else ilen
:= len
;
1548 writer(PAnsiChar(PadSpaces
)^, ilen
);
1553 procedure indent0 (len
: Integer);
1559 if (len
> Length(PadZeroes
)) then ilen
:= Length(PadZeroes
) else ilen
:= len
;
1560 writer(PAnsiChar(PadZeroes
)^, ilen
);
1568 while (spos
<= Length(fmt
)) do
1570 // print literal part
1572 while (epos
<= Length(fmt
)) and (fmt
[epos
] <> '%') do Inc(epos
);
1573 // output literal part
1574 if (epos
> spos
) then
1576 if (epos
> Length(fmt
)) then
1578 writer((PAnsiChar(fmt
)+spos
-1)^, epos
-spos
);
1581 if (epos
+1 > Length(fmt
)) then Inc(epos
) // last percent, output literally
1582 else if (fmt
[epos
+1] = '%') then // special case
1585 writer((PAnsiChar(fmt
)+spos
-1)^, epos
-spos
);
1590 writer((PAnsiChar(fmt
)+spos
-1)^, epos
-spos
);
1595 // check if we have argument for this format string
1596 if (curarg
> High(args
)) then
1598 xwrite('<OUT OF ARGS>');
1599 writer((PAnsiChar(fmt
)+spos
-1)^, Length(fmt
)-spos
+1);
1603 if (spos
+1 > Length(fmt
)) then break
; // oops
1604 assert(fmt
[spos
] = '%');
1606 // parse format; check for sign
1607 if (fmt
[spos
] = '-') then begin sign
:= '-'; Inc(spos
); end
1608 else if (fmt
[spos
] = '+') then begin sign
:= '+'; Inc(spos
); end
1611 if (spos
> Length(fmt
)) then begin xwrite('<INVALID FORMAT>'); break
; end;
1612 if (sign
<> ' ') or ((fmt
[spos
] >= '0') and (fmt
[spos
] <= '9')) then
1614 if (fmt
[spos
] < '0') or (fmt
[spos
] > '9') then begin xwrite('<INVALID FORMAT>'); writer((PAnsiChar(fmt
)+spos
-1)^, Length(fmt
)-spos
+1); break
; end;
1615 zeropad
:= (fmt
[spos
] = '0');
1617 while (spos
<= Length(fmt
)) do
1620 if (ch
< '0') or (ch
> '9') then break
;
1621 width
:= width
*10+Integer(ch
)-48;
1632 if (spos
<= Length(fmt
)) and (fmt
[spos
] = '.') then
1635 if (spos
> Length(fmt
)) then begin xwrite('<INVALID FORMAT>'); break
; end;
1636 if (fmt
[spos
] < '0') or (fmt
[spos
] > '9') then begin xwrite('<INVALID FORMAT>'); writer((PAnsiChar(fmt
)+spos
-1)^, Length(fmt
)-spos
+1); break
; end;
1638 while (spos
<= Length(fmt
)) do
1641 if (ch
< '0') or (ch
> '9') then break
;
1642 prec
:= prec
*10+Integer(ch
)-48;
1647 if (spos
> Length(fmt
)) then begin xwrite('<INVALID FORMAT>'); break
; end;
1650 // done parsing format, check for valid format chars
1651 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;
1652 // now write formatted string
1653 case args
[curarg
].VType
of
1654 vtInteger
: // args[curarg].VInteger
1656 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;
1657 if (fmtch
= 's') then fmtch
:= 'd';
1659 strblen
:= snprintf(@strbuf
[0], Length(strbuf
), @fmtbuf
[0], args
[curarg
].VInteger
);
1662 vtBoolean
: // args[curarg].VBoolean
1667 if args
[curarg
].VBoolean
then strblen
:= snprintf(@strbuf
[0], Length(strbuf
), @fmtbuf
[0], 'true')
1668 else strblen
:= snprintf(@strbuf
[0], Length(strbuf
), @fmtbuf
[0], 'false');
1674 if args
[curarg
].VBoolean
then strblen
:= snprintf(@strbuf
[0], Length(strbuf
), @fmtbuf
[0], AnsiChar('t'))
1675 else strblen
:= snprintf(@strbuf
[0], Length(strbuf
), @fmtbuf
[0], AnsiChar('f'));
1681 strblen
:= snprintf(@strbuf
[0], Length(strbuf
), @fmtbuf
[0], Integer(args
[curarg
].VBoolean
));
1686 xwrite('<INVALID FORMAT CHAR>');
1687 writer((PAnsiChar(fmt
)+spos
-1)^, Length(fmt
)-spos
+1);
1691 vtChar
: // args[curarg].VChar
1697 strblen
:= snprintf(@strbuf
[0], Length(strbuf
), @fmtbuf
[0], args
[curarg
].VChar
);
1703 strblen
:= snprintf(@strbuf
[0], Length(strbuf
), @fmtbuf
[0], Integer(args
[curarg
].VChar
));
1708 xwrite('<INVALID FORMAT CHAR>');
1709 writer((PAnsiChar(fmt
)+spos
-1)^, Length(fmt
)-spos
+1);
1713 //vtWideChar: begin end; // args[curarg].VWideChar (WideChar)
1714 vtExtended
: // args[curarg].VExtended^
1720 strblen
:= snprintf(@strbuf
[0], Length(strbuf
), @fmtbuf
[0], Double(args
[curarg
].VExtended
^));
1726 strblen
:= snprintf(@strbuf
[0], Length(strbuf
), @fmtbuf
[0], Double(args
[curarg
].VExtended
^));
1732 strblen
:= snprintf(@strbuf
[0], Length(strbuf
), @fmtbuf
[0], Integer(trunc(args
[curarg
].VExtended
^)));
1738 strblen
:= snprintf(@strbuf
[0], Length(strbuf
), @fmtbuf
[0], LongWord(trunc(args
[curarg
].VExtended
^)));
1743 xwrite('<INVALID FORMAT CHAR>');
1744 writer((PAnsiChar(fmt
)+spos
-1)^, Length(fmt
)-spos
+1);
1748 vtString
: // args[curarg].VString^ (PShortString)
1750 if (sign
<> '-') then indent(width
-Length(args
[curarg
].VString
^));
1751 writer(args
[curarg
].VString
^[1], Length(args
[curarg
].VString
^));
1752 if (sign
= '-') then indent(width
-Length(args
[curarg
].VString
^));
1754 vtPointer
: // args[curarg].VPointer
1759 if (width
< 8) then width
:= 8;
1762 strblen
:= snprintf(@strbuf
[0], Length(strbuf
), @fmtbuf
[0], PtrUInt(args
[curarg
].VPointer
));
1765 'u', 'd', 'x', 'p', 'X':
1767 if (fmtch
= 'p') then fmtch
:= 'x';
1768 if (width
< 8) then width
:= 8;
1771 strblen
:= snprintf(@strbuf
[0], Length(strbuf
), @fmtbuf
[0], PtrUInt(args
[curarg
].VPointer
));
1776 xwrite('<INVALID FORMAT CHAR>');
1777 writer((PAnsiChar(fmt
)+spos
-1)^, Length(fmt
)-spos
+1);
1781 vtPChar
: // args[curarg].VPChar
1782 if (args
[curarg
].VPChar
= nil) then
1784 if (sign
<> '-') then indent(width
-3);
1786 if (sign
= '-') then indent(width
-3);
1791 while (args
[curarg
].VPChar
[pclen
] <> #0) do Inc(pclen
);
1792 if (sign
<> '-') then indent(width
-pclen
);
1793 writer(args
[curarg
].VPChar
^, pclen
);
1794 if (sign
= '-') then indent(width
-pclen
);
1796 vtObject
: // args[curarg].VObject.Classname (TObject)
1798 if (args
[curarg
].VObject
<> nil) then ccname
:= args
[curarg
].VObject
.Classname
else ccname
:= '<nil>';
1799 if (sign
<> '-') then indent(width
-Length(ccname
));
1801 if (sign
= '-') then indent(width
-Length(ccname
));
1803 vtClass
: // args[curarg].VClass.Classname (TClass)
1805 if (args
[curarg
].VClass
<> nil) then ccname
:= args
[curarg
].VClass
.Classname
else ccname
:= '<nil>';
1806 if (sign
<> '-') then indent(width
-Length(ccname
));
1808 if (sign
= '-') then indent(width
-Length(ccname
));
1810 //vtPWideChar: begin end; // args[curarg].VPWideChar (PWideChar)
1811 vtAnsiString
: // AnsiString(args[curarg].VAnsiString) (Pointer)
1813 if (sign
<> '-') then indent(width
-Length(AnsiString(args
[curarg
].VAnsiString
)));
1814 xwrite(AnsiString(args
[curarg
].VAnsiString
));
1815 if (sign
= '-') then indent(width
-Length(AnsiString(args
[curarg
].VAnsiString
)));
1817 //vtCurrency: begin end; // args[curarg].VCurrency (PCurrency)
1818 //vtVariant: begin end; // args[curarg].VVariant^ (PVariant)
1819 //vtInterface: begin end; // args[curarg].VInterface (Pointer);
1820 //vtWideString: begin end; // args[curarg].VWideString (Pointer);
1821 vtInt64
: // args[curarg].VInt64^ (PInt64)
1824 's','d','u': pc
:= i642str(args
[curarg
].VInt64
^, false, false);
1825 'x': pc
:= i642str(args
[curarg
].VInt64
^, true, false);
1826 'X': pc
:= i642str(args
[curarg
].VInt64
^, true, true);
1827 else begin xwrite('<INVALID FORMAT CHAR>'); writer((PAnsiChar(fmt
)+spos
-1)^, Length(fmt
)-spos
+1); break
; end;
1830 while (pc
[pclen
] <> #0) do Inc(pclen
);
1831 if (sign
<> '-') and (width
> pclen
) then
1835 if (pc
[0] = '-') or (pc
[0] = '+') then
1838 indent0(width
-pclen
-1);
1844 indent0(width
-pclen
);
1849 indent(width
-pclen
);
1853 if (sign
= '-') then indent(width
-pclen
);
1855 vtQWord
: // args[curarg].VQWord^ (PQWord)
1858 's','d','u': pc
:= ui642str(args
[curarg
].VInt64
^, false, false);
1859 'x': pc
:= ui642str(args
[curarg
].VInt64
^, true, false);
1860 'X': pc
:= ui642str(args
[curarg
].VInt64
^, true, true);
1861 else begin xwrite('<INVALID FORMAT CHAR>'); writer((PAnsiChar(fmt
)+spos
-1)^, Length(fmt
)-spos
+1); break
; end;
1864 while (pc
[pclen
] <> #0) do Inc(pclen
);
1865 if (sign
<> '-') then begin if zeropad
then indent0(width
-pclen
) else indent(width
-pclen
); end;
1867 if (sign
= '-') then indent(width
-pclen
);
1871 xwrite('<INVALID TYPE>');
1872 writer((PAnsiChar(fmt
)+spos
-1)^, Length(fmt
)-spos
+1);
1885 i64: Int64 = -$A000000000;
1886 ui64: UInt64 = $A000000000;
1888 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']);
1889 writef(conwriter, 'test float:<%s;%u;%f;%g>'#10, [666.6942, 666.6942, 666.6942, 666.6942]);
1892 writef(conwriter, 'test ss:<%5s;%040s>'#10, [ss, ss]);
1893 writef(conwriter, 'test ls:<%5s;%040s>'#10, [ls, ls]);
1894 writef(conwriter, 'test pointer:<%s;%x;%p>'#10, [@ss, @ss, @ss]);
1895 writef(conwriter, 'test i64:<%s;%x;%015d;%u;%X>'#10, [i64, i64, i64, i64, i64]);
1896 writef(conwriter, 'test ui64:<%s;%x;%15d;%015u;%X>'#10, [ui64, ui64, ui64, ui64, ui64]);