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
: AnsiChar): AnsiChar; inline;
87 function locase1251 (ch
: AnsiChar): AnsiChar; inline;
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;
103 // return fixed AnsiString or empty AnsiString
104 function findDiskWad (fname
: AnsiString): AnsiString;
107 function openDiskFileRO (pathname
: AnsiString): TStream
;
108 function createDiskFile (pathname
: AnsiString): TStream
;
111 procedure writeSign (st
: TStream
; const sign
: AnsiString);
112 function checkSign (st
: TStream
; const sign
: AnsiString): Boolean;
114 procedure writeBool (st
: TStream
; b
: Boolean);
115 function readBool (st
: TStream
): Boolean;
117 procedure writeStr (st
: TStream
; const str
: AnsiString; maxlen
: LongWord=65535);
118 function readStr (st
: TStream
; maxlen
: LongWord=65535): AnsiString;
120 procedure writeInt (st
: TStream
; v
: Byte); overload
;
121 procedure writeInt (st
: TStream
; v
: ShortInt); overload
;
122 procedure writeInt (st
: TStream
; v
: Word); overload
;
123 procedure writeInt (st
: TStream
; v
: SmallInt); overload
;
124 procedure writeInt (st
: TStream
; v
: LongWord); overload
;
125 procedure writeInt (st
: TStream
; v
: LongInt); overload
;
126 procedure writeInt (st
: TStream
; v
: Int64); overload
;
127 procedure writeInt (st
: TStream
; v
: UInt64); overload
;
129 function readByte (st
: TStream
): Byte;
130 function readShortInt (st
: TStream
): ShortInt;
131 function readWord (st
: TStream
): Word;
132 function readSmallInt (st
: TStream
): SmallInt;
133 function readLongWord (st
: TStream
): LongWord;
134 function readLongInt (st
: TStream
): LongInt;
135 function readInt64 (st
: TStream
): Int64;
136 function readUInt64 (st
: TStream
): UInt64;
139 procedure writeIntBE (st
: TStream
; v
: Byte); overload
;
140 procedure writeIntBE (st
: TStream
; v
: ShortInt); overload
;
141 procedure writeIntBE (st
: TStream
; v
: Word); overload
;
142 procedure writeIntBE (st
: TStream
; v
: SmallInt); overload
;
143 procedure writeIntBE (st
: TStream
; v
: LongWord); overload
;
144 procedure writeIntBE (st
: TStream
; v
: LongInt); overload
;
145 procedure writeIntBE (st
: TStream
; v
: Int64); overload
;
146 procedure writeIntBE (st
: TStream
; v
: UInt64); overload
;
148 function readByteBE (st
: TStream
): Byte;
149 function readShortIntBE (st
: TStream
): ShortInt;
150 function readWordBE (st
: TStream
): Word;
151 function readSmallIntBE (st
: TStream
): SmallInt;
152 function readLongWordBE (st
: TStream
): LongWord;
153 function readLongIntBE (st
: TStream
): LongInt;
154 function readInt64BE (st
: TStream
): Int64;
155 function readUInt64BE (st
: TStream
): UInt64;
158 function nmin (a
, b
: Byte): Byte; inline; overload
;
159 function nmin (a
, b
: ShortInt): ShortInt; inline; overload
;
160 function nmin (a
, b
: Word): Word; inline; overload
;
161 function nmin (a
, b
: SmallInt): SmallInt; inline; overload
;
162 function nmin (a
, b
: LongWord): LongWord; inline; overload
;
163 function nmin (a
, b
: LongInt): LongInt; inline; overload
;
164 function nmin (a
, b
: Int64): Int64; inline; overload
;
165 function nmin (a
, b
: UInt64): UInt64; inline; overload
;
166 function nmin (a
, b
: Single): Single; inline; overload
;
167 function nmin (a
, b
: Double): Double; inline; overload
;
168 function nmin (a
, b
: Extended
): Extended
; inline; overload
;
170 function nmax (a
, b
: Byte): Byte; inline; overload
;
171 function nmax (a
, b
: ShortInt): ShortInt; inline; overload
;
172 function nmax (a
, b
: Word): Word; inline; overload
;
173 function nmax (a
, b
: SmallInt): SmallInt; inline; overload
;
174 function nmax (a
, b
: LongWord): LongWord; inline; overload
;
175 function nmax (a
, b
: LongInt): LongInt; inline; overload
;
176 function nmax (a
, b
: Int64): Int64; inline; overload
;
177 function nmax (a
, b
: UInt64): UInt64; inline; overload
;
178 function nmax (a
, b
: Single): Single; inline; overload
;
179 function nmax (a
, b
: Double): Double; inline; overload
;
180 function nmax (a
, b
: Extended
): Extended
; inline; overload
;
182 function nclamp (v
, a
, b
: Byte): Byte; inline; overload
;
183 function nclamp (v
, a
, b
: ShortInt): ShortInt; inline; overload
;
184 function nclamp (v
, a
, b
: Word): Word; inline; overload
;
185 function nclamp (v
, a
, b
: SmallInt): SmallInt; inline; overload
;
186 function nclamp (v
, a
, b
: LongWord): LongWord; inline; overload
;
187 function nclamp (v
, a
, b
: LongInt): LongInt; inline; overload
;
188 function nclamp (v
, a
, b
: Int64): Int64; inline; overload
;
189 function nclamp (v
, a
, b
: UInt64): UInt64; inline; overload
;
190 function nclamp (v
, a
, b
: Single): Single; inline; overload
;
191 function nclamp (v
, a
, b
: Double): Double; inline; overload
;
192 function nclamp (v
, a
, b
: Extended
): Extended
; inline; overload
;
196 TFormatStrFCallback
= procedure (constref buf
; len
: SizeUInt
);
198 // returns formatted string if `writerCB` is `nil`, empty string otherwise
199 function formatstrf (const fmt
: AnsiString; const args
: array of const; writerCB
: TFormatStrFCallback
=nil): AnsiString;
201 function wchar2win (wc
: WideChar): AnsiChar; inline;
202 function utf2win (const s
: AnsiString): AnsiString;
203 function win2utf (const s
: AnsiString): AnsiString;
204 function digitInBase (ch
: AnsiChar; base
: Integer): Integer;
206 // returns string in single or double quotes
207 // single quotes supports only pascal-style '' for single quote char
208 // double quotes supports c-style escapes
209 // function will select quote mode automatically
210 function quoteStr (const s
: AnsiString): AnsiString;
214 generic TSimpleList
<ItemT
> = class
216 //type PItemT = ^ItemT;
217 type TItemArr
= array of ItemT
;
227 constructor Create (const aitems
: TItemArr
; acount
: Integer);
228 function MoveNext
: Boolean;
229 function getCurrent (): ItemT
;
230 property Current
: ItemT read getCurrent
;
235 mCount
: Integer; // can be less than `mItems` size
238 function getAt (idx
: Integer): ItemT
; inline;
239 procedure setAt (idx
: Integer; const it
: ItemT
); inline;
241 function getCapacity (): Integer; inline;
242 procedure setCapacity (v
: Integer); inline;
245 constructor Create (acapacity
: Integer=-1);
246 destructor Destroy (); override;
248 //WARNING! don't change list contents in `for ... in`!
249 function GetEnumerator (): TEnumerator
;
251 procedure reset (); inline; // won't resize `mItems`
252 procedure clear (); inline;
254 procedure append (constref it
: ItemT
); inline;
255 procedure delete (idx
: Integer); inline;
256 function remove (idx
: Integer): ItemT
; inline;
259 property count
: Integer read mCount
;
260 property capacity
: Integer read getCapacity write setCapacity
;
261 property at
[idx
: Integer]: ItemT read getAt write setAt
; default
;
265 procedure FillMemory (Dest
: Pointer; Len
: LongWord; Ch
: Byte); inline;
266 procedure CopyMemory (Dest
: Pointer; Src
: Pointer; Len
: LongWord); inline;
267 procedure ZeroMemory (Dest
: Pointer; Len
: LongWord); inline;
276 // ////////////////////////////////////////////////////////////////////////// //
277 procedure CopyMemory (Dest
: Pointer; Src
: Pointer; Len
: LongWord); inline;
279 Move(Src
^, Dest
^, Len
);
282 procedure FillMemory (Dest
: Pointer; Len
: LongWord; Ch
: Byte); inline;
284 FillChar(Dest
^, Len
, Ch
);
287 procedure ZeroMemory (Dest
: Pointer; Len
: LongWord); inline;
289 FillChar(Dest
^, Len
, 0);
293 // ////////////////////////////////////////////////////////////////////////// //
294 constructor TSimpleList
.TEnumerator
.Create (const aitems
: TItemArr
; acount
: Integer);
301 function TSimpleList
.TEnumerator
.MoveNext
: Boolean;
304 result
:= (mCurrent
< mCount
);
307 function TSimpleList
.TEnumerator
.getCurrent (): ItemT
;
309 result
:= mItems
[mCurrent
];
313 // ////////////////////////////////////////////////////////////////////////// //
314 constructor TSimpleList
.Create (acapacity
: Integer=-1);
317 if (acapacity
> 0) then SetLength(mItems
, acapacity
);
322 destructor TSimpleList
.Destroy ();
329 function TSimpleList
.getCapacity (): Integer; inline;
331 result
:= Length(mItems
);
335 procedure TSimpleList
.setCapacity (v
: Integer); inline;
337 if (v
< mCount
) then v
:= mCount
;
338 if (v
<> Length(mItems
)) then SetLength(mItems
, v
);
342 function TSimpleList
.GetEnumerator (): TEnumerator
;
344 if (Length(mItems
) > 0) then result
:= TEnumerator
.Create(mItems
, mCount
)
345 else result
:= TEnumerator
.Create(nil, -1);
349 procedure TSimpleList
.reset (); inline;
355 procedure TSimpleList
.clear (); inline;
362 function TSimpleList
.getAt (idx
: Integer): ItemT
; inline;
364 if (idx
>= 0) and (idx
< mCount
) then result
:= mItems
[idx
] else result
:= Default(ItemT
);
368 procedure TSimpleList
.setAt (idx
: Integer; const it
: ItemT
); inline;
370 if (idx
>= 0) and (idx
< mCount
) then mItems
[idx
] := it
;
374 procedure TSimpleList
.append (constref it
: ItemT
); inline;
378 if (mCount
>= Length(mItems
)) then
380 newsz
:= mCount
+(mCount
div 3)+128;
381 SetLength(mItems
, newsz
);
383 mItems
[mCount
] := it
;
388 procedure TSimpleList
.delete (idx
: Integer); inline;
392 if (idx
>= 0) and (idx
< mCount
) then
394 for f
:= idx
+1 to mCount
-1 do mItems
[f
-1] := mItems
[f
];
399 function TSimpleList
.remove (idx
: Integer): ItemT
; inline;
403 if (idx
>= 0) and (idx
< mCount
) then
405 result
:= mItems
[idx
];
406 for f
:= idx
+1 to mCount
-1 do mItems
[f
-1] := mItems
[f
];
410 result
:= Default(ItemT
);
415 // ////////////////////////////////////////////////////////////////////////// //
417 wc2shitmap
: array[0..65535] of AnsiChar;
418 wc2shitmapInited
: Boolean = false;
421 // ////////////////////////////////////////////////////////////////////////// //
423 cp1251
: array[0..127] of Word = (
424 $0402,$0403,$201A,$0453,$201E,$2026,$2020,$2021,$20AC,$2030,$0409,$2039,$040A,$040C,$040B,$040F,
425 $0452,$2018,$2019,$201C,$201D,$2022,$2013,$2014,$003F,$2122,$0459,$203A,$045A,$045C,$045B,$045F,
426 $00A0,$040E,$045E,$0408,$00A4,$0490,$00A6,$00A7,$0401,$00A9,$0404,$00AB,$00AC,$00AD,$00AE,$0407,
427 $00B0,$00B1,$0406,$0456,$0491,$00B5,$00B6,$00B7,$0451,$2116,$0454,$00BB,$0458,$0405,$0455,$0457,
428 $0410,$0411,$0412,$0413,$0414,$0415,$0416,$0417,$0418,$0419,$041A,$041B,$041C,$041D,$041E,$041F,
429 $0420,$0421,$0422,$0423,$0424,$0425,$0426,$0427,$0428,$0429,$042A,$042B,$042C,$042D,$042E,$042F,
430 $0430,$0431,$0432,$0433,$0434,$0435,$0436,$0437,$0438,$0439,$043A,$043B,$043C,$043D,$043E,$043F,
431 $0440,$0441,$0442,$0443,$0444,$0445,$0446,$0447,$0448,$0449,$044A,$044B,$044C,$044D,$044E,$044F
435 procedure initShitMap ();
439 for f
:= 0 to High(wc2shitmap
) do wc2shitmap
[f
] := '?';
440 for f
:= 0 to 127 do wc2shitmap
[f
] := AnsiChar(f
);
441 for f
:= 0 to 127 do wc2shitmap
[cp1251
[f
]] := AnsiChar(f
+128);
442 wc2shitmapInited
:= true;
446 // ////////////////////////////////////////////////////////////////////////// //
447 // fast state-machine based UTF-8 decoder; using 8 bytes of memory
448 // code points from invalid range will never be valid, this is the property of the state machine
450 // see http://bjoern.hoehrmann.de/utf-8/decoder/dfa/
451 utf8dfa
: array[0..$16c-1] of Byte = (
452 // maps bytes to character classes
453 $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00, // 00-0f
454 $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00, // 10-1f
455 $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00, // 20-2f
456 $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00, // 30-3f
457 $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00, // 40-4f
458 $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00, // 50-5f
459 $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00, // 60-6f
460 $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00, // 70-7f
461 $01,$01,$01,$01,$01,$01,$01,$01,$01,$01,$01,$01,$01,$01,$01,$01, // 80-8f
462 $09,$09,$09,$09,$09,$09,$09,$09,$09,$09,$09,$09,$09,$09,$09,$09, // 90-9f
463 $07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07, // a0-af
464 $07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07, // b0-bf
465 $08,$08,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02, // c0-cf
466 $02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02, // d0-df
467 $0a,$03,$03,$03,$03,$03,$03,$03,$03,$03,$03,$03,$03,$04,$03,$03, // e0-ef
468 $0b,$06,$06,$06,$05,$08,$08,$08,$08,$08,$08,$08,$08,$08,$08,$08, // f0-ff
469 // maps a combination of a state of the automaton and a character class to a state
470 $00,$0c,$18,$24,$3c,$60,$54,$0c,$0c,$0c,$30,$48,$0c,$0c,$0c,$0c, // 100-10f
471 $0c,$0c,$0c,$0c,$0c,$0c,$0c,$0c,$0c,$00,$0c,$0c,$0c,$0c,$0c,$00, // 110-11f
472 $0c,$00,$0c,$0c,$0c,$18,$0c,$0c,$0c,$0c,$0c,$18,$0c,$18,$0c,$0c, // 120-12f
473 $0c,$0c,$0c,$0c,$0c,$0c,$0c,$18,$0c,$0c,$0c,$0c,$0c,$18,$0c,$0c, // 130-13f
474 $0c,$0c,$0c,$0c,$0c,$18,$0c,$0c,$0c,$0c,$0c,$0c,$0c,$0c,$0c,$24, // 140-14f
475 $0c,$24,$0c,$0c,$0c,$24,$0c,$0c,$0c,$0c,$0c,$24,$0c,$24,$0c,$0c, // 150-15f
476 $0c,$24,$0c,$0c,$0c,$0c,$0c,$0c,$0c,$0c,$0c,$0c);
479 // ////////////////////////////////////////////////////////////////////////// //
480 constructor TUtf8DecoderFast
.Create (v
: Boolean{fuck you, fpc}); begin state
:= Accept
; codepoint
:= 0; end;
482 procedure TUtf8DecoderFast
.reset (); inline; begin state
:= Accept
; codepoint
:= 0; end;
484 function TUtf8DecoderFast
.complete (): Boolean; inline; begin result
:= (state
= Accept
); end;
485 function TUtf8DecoderFast
.invalid (): Boolean; inline; begin result
:= (state
= Reject
); end;
486 function TUtf8DecoderFast
.completeOrInvalid (): Boolean; inline; begin result
:= (state
= Accept
) or (state
= Reject
); end;
488 function TUtf8DecoderFast
.decode (c
: AnsiChar): Boolean; inline; overload
; begin result
:= decode(Byte(c
)); end;
490 function TUtf8DecoderFast
.decode (b
: Byte): Boolean; inline; overload
;
494 if (state
= Reject
) then begin state
:= Accept
; codepoint
:= 0; end;
496 if (state
<> Accept
) then codepoint
:= (b
and $3f) or (codepoint
shl 6) else codepoint
:= ($ff shr tp
) and b
;
497 state
:= utf8dfa
[256+state
+tp
];
498 if (state
= Reject
) then begin codepoint
:= Replacement
; state
:= Accept
; end;
499 result
:= (state
= Accept
);
503 // ////////////////////////////////////////////////////////////////////////// //
504 function wchar2win (wc
: WideChar): AnsiChar; inline;
506 if not wc2shitmapInited
then initShitMap();
507 if (LongWord(wc
) > 65535) then result
:= '?' else result
:= wc2shitmap
[LongWord(wc
)];
511 // ////////////////////////////////////////////////////////////////////////// //
512 function utf2win (const s
: AnsiString): AnsiString;
515 ud
: TUtf8DecoderFast
;
517 for f
:= 1 to Length(s
) do
519 if (Byte(s
[f
]) > 127) then
521 ud
:= TUtf8DecoderFast
.Create(true);
523 for c
:= 1 to Length(s
) do
525 if ud
.decode(s
[c
]) then result
+= wchar2win(WideChar(ud
.codepoint
));
534 function win2utf (const s
: AnsiString): AnsiString;
538 function utf8Encode (code
: Integer): AnsiString;
540 if (code
< 0) or (code
> $10FFFF) then begin result
:= '?'; exit
; end;
541 if (code
<= $7f) then
543 result
:= AnsiChar(code
and $ff);
545 else if (code
<= $7FF) then
547 result
:= AnsiChar($C0 or (code
shr 6));
548 result
+= AnsiChar($80 or (code
and $3F));
550 else if (code
<= $FFFF) then
552 result
:= AnsiChar($E0 or (code
shr 12));
553 result
+= AnsiChar($80 or ((code
shr 6) and $3F));
554 result
+= AnsiChar($80 or (code
and $3F));
556 else if (code
<= $10FFFF) then
558 result
:= AnsiChar($F0 or (code
shr 18));
559 result
+= AnsiChar($80 or ((code
shr 12) and $3F));
560 result
+= AnsiChar($80 or ((code
shr 6) and $3F));
561 result
+= AnsiChar($80 or (code
and $3F));
570 for f
:= 1 to Length(s
) do
572 if (Byte(s
[f
]) > 127) then
575 for c
:= 1 to Length(s
) do
577 if (Byte(s
[c
]) < 128) then
583 result
+= utf8Encode(cp1251
[Byte(s
[c
])-128])
593 // ////////////////////////////////////////////////////////////////////////// //
594 function digitInBase (ch
: AnsiChar; base
: Integer): Integer;
597 if (base
< 1) or (base
> 36) then exit
;
598 if (ch
< '0') then exit
;
601 if (Integer(ch
) >= 48+base
) then exit
;
602 result
:= Integer(ch
)-48;
606 if (ch
>= '0') and (ch
<= '9') then begin result
:= Integer(ch
)-48; exit
; end;
607 if (ch
>= 'a') and (ch
<= 'z') then Dec(ch
, 32); // poor man's tolower()
608 if (ch
< 'A') or (Integer(ch
) >= 65+(base
-10)) then exit
;
609 result
:= Integer(ch
)-65+10;
614 // ////////////////////////////////////////////////////////////////////////// //
615 function quoteStr (const s
: AnsiString): AnsiString;
617 function squote (const s
: AnsiString): AnsiString;
622 for f
:= 1 to Length(s
) do
624 if (s
[f
] = '''') then result
+= '''';
630 function dquote (const s
: AnsiString): AnsiString;
636 for f
:= 1 to Length(s
) do
639 if (ch
= #0) then result
+= '\z'
640 else if (ch
= #9) then result
+= '\t'
641 else if (ch
= #10) then result
+= '\n'
642 else if (ch
= #13) then result
+= '\r'
643 else if (ch
= #27) then result
+= '\e'
644 else if (ch
< ' ') or (ch
= #127) then
647 result
+= LowerCase(IntToHex(Integer(ch
), 2));
649 else if (ch
= '"') or (ch
= '\') then
663 needSingle
: Boolean = false;
666 for f
:= 1 to Length(s
) do
668 if (s
[f
] = '''') then begin needSingle
:= true; continue
; end;
669 if (s
[f
] < ' ') or (s
[f
] = #127) then begin result
:= dquote(s
); exit
; end;
671 if needSingle
then result
:= squote(s
) else result
:= ''''+s
+'''';
675 // ////////////////////////////////////////////////////////////////////////// //
676 function getFilenameExt (const fn
: AnsiString): AnsiString;
687 if (pos
= Length(fn
)) then result
:= '' else result
:= Copy(fn
, pos
, Length(fn
)-pos
+1);
690 if (ch
= '/') or (ch
= '\') then break
;
693 result
:= ''; // no extension
697 function setFilenameExt (const fn
, ext
: AnsiString): AnsiString;
703 if (Length(ext
) = 0) or (ext
= '.') then exit
;
708 if (ch
= '.') then exit
;
709 if (ch
= '/') or (ch
= '\') then break
;
712 if (ext
[1] <> '.') then result
+= '.';
717 function forceFilenameExt (const fn
, ext
: AnsiString): AnsiString;
729 if (Length(ext
) = 0) or (ext
= '.') then
731 result
:= Copy(fn
, 1, pos
-1);
735 if (ext
[1] = '.') then result
:= Copy(fn
, 1, pos
-1) else result
:= Copy(fn
, 1, pos
);
740 if (ch
= '/') or (ch
= '\') then break
;
743 if (Length(ext
) > 0) then
745 if (ext
[1] <> '.') then result
+= '.';
751 // strips out name from `fn`, leaving trailing slash
752 function getFilenamePath (const fn
: AnsiString): AnsiString;
757 if (Length(fn
) = 0) then begin result
:= './'; exit
; end;
758 if (fn
[Length(fn
)] = '/') or (fn
[Length(fn
)] = '\') then begin result
:= fn
; exit
; end;
763 if (ch
= '/') or (ch
= '\') then begin result
:= Copy(fn
, 1, pos
); exit
; end;
766 result
:= './'; // no path -> current dir
770 // ends with '/' or '\'?
771 function isFilenamePath (const fn
: AnsiString): Boolean;
773 if (Length(fn
) = 0) then
779 result
:= (fn
[Length(fn
)] = '/') or (fn
[Length(fn
)] = '\');
784 // strips extra trailing slashes in `path, and extra leading slashes in `fn`
785 // will add slash to `path`, even if `fn` is empty!
786 function filenameConcat (const path
, fn
: AnsiString): AnsiString;
791 while (pos
<= Length(fn
)) and ((fn
[pos
] = '/') or (fn
[pos
] = '\')) do Inc(pos
);
793 if (Length(result
) > 0) and ((result
[Length(result
)] <> '/') and (result
[Length(result
)] <> '\')) then result
+= '/';
794 if (pos
<= Length(fn
)) then
796 result
+= Copy(fn
, pos
, Length(fn
)-pos
+1);
797 //FIXME: make this faster!
798 while (Length(result
) > 0) and ((result
[Length(result
)] = '/') or (result
[Length(result
)] = '\')) do
800 Delete(result
, Length(result
), 1);
802 if (fn
[Length(fn
)] = '/') or (fn
[Length(fn
)] = '\') then result
+= '/';
807 function hasWadExtension (const fn
: AnsiString): Boolean;
811 ext
:= getFilenameExt(fn
);
812 result
:= StrEquCI1251(ext
, '.wad') or StrEquCI1251(ext
, '.pk3') or StrEquCI1251(ext
, '.zip');
816 function addWadExtension (const fn
: AnsiString): AnsiString;
819 if not hasWadExtension(result
) then result
:= result
+'.wad';
823 function isWadPath (const fn
: AnsiString): Boolean;
830 while (pos
<= Length(fn
)) do
832 if (fn
[pos
] = ':') then
834 if (Length(fn
)-pos
< 1) then break
;
835 if (pos
-4 > 1) and (fn
[pos
-4] = '.') and ((fn
[pos
+1] = '\') or (fn
[pos
+1] = '/')) then
837 s
:= Copy(fn
, pos
-4, 4);
838 if StrEquCI1251(s
, '.wad') or StrEquCI1251(s
, '.pk3') or StrEquCI1251(s
, '.zip') then
850 function int64ToStrComma (i
: Int64): AnsiString;
855 f
:= Length(result
)+1;
858 Dec(f
, 3); Insert(',', result
, f
);
863 function upcase1251 (ch
: AnsiChar): AnsiChar; inline;
867 if (ch
>= 'a') and (ch
<= 'z') then Dec(ch
, 32);
871 if (ch
>= #224) and (ch
<= #255) then
878 #184, #186, #191: Dec(ch
, 16);
887 function locase1251 (ch
: AnsiChar): AnsiChar; inline;
891 if (ch
>= 'A') and (ch
<= 'Z') then Inc(ch
, 32);
895 if (ch
>= #192) and (ch
<= #223) then
902 #168, #170, #175: Inc(ch
, 16);
911 function strEquCI1251 (const s0
, s1
: AnsiString): Boolean;
916 if length(s0
) <> length(s1
) then exit
;
917 for i
:= 1 to length(s0
) do if UpCase1251(s0
[i
]) <> UpCase1251(s1
[i
]) then exit
;
922 function toLowerCase1251 (const s
: AnsiString): AnsiString;
929 if (ch
<> LoCase1251(ch
)) then
932 SetLength(result
, Length(s
));
933 for f
:= 1 to Length(s
) do result
[f
] := LoCase1251(s
[f
]);
942 // ////////////////////////////////////////////////////////////////////////// //
946 function utf8CodeLen (ch
: Word): Integer;
948 if ch
< $80 then begin result
:= 1; exit
; end;
949 if (ch
and $FE) = $FC then begin result
:= 6; exit
; end;
950 if (ch
and $FC) = $F8 then begin result
:= 5; exit
; end;
951 if (ch
and $F8) = $F0 then begin result
:= 4; exit
; end;
952 if (ch
and $F0) = $E0 then begin result
:= 3; exit
; end;
953 if (ch
and $E0) = $C0 then begin result
:= 2; exit
; end;
954 result
:= -1; // invalid
958 function utf8Valid (const s
: AnsiString): Boolean;
964 while pos
<= length(s
) do
966 len
:= utf8CodeLen(Byte(s
[pos
]));
967 if len
< 1 then exit
; // invalid sequence start
968 if pos
+len
-1 > length(s
) then exit
; // out of chars in string
971 // check other sequence bytes
974 if (Byte(s
[pos
]) and $C0) <> $80 then exit
;
983 // ////////////////////////////////////////////////////////////////////////// //
985 uni2wint
: array [128..255] of Word = (
986 $0402,$0403,$201A,$0453,$201E,$2026,$2020,$2021,$20AC,$2030,$0409,$2039,$040A,$040C,$040B,$040F,
987 $0452,$2018,$2019,$201C,$201D,$2022,$2013,$2014,$003F,$2122,$0459,$203A,$045A,$045C,$045B,$045F,
988 $00A0,$040E,$045E,$0408,$00A4,$0490,$00A6,$00A7,$0401,$00A9,$0404,$00AB,$00AC,$00AD,$00AE,$0407,
989 $00B0,$00B1,$0406,$0456,$0491,$00B5,$00B6,$00B7,$0451,$2116,$0454,$00BB,$0458,$0405,$0455,$0457,
990 $0410,$0411,$0412,$0413,$0414,$0415,$0416,$0417,$0418,$0419,$041A,$041B,$041C,$041D,$041E,$041F,
991 $0420,$0421,$0422,$0423,$0424,$0425,$0426,$0427,$0428,$0429,$042A,$042B,$042C,$042D,$042E,$042F,
992 $0430,$0431,$0432,$0433,$0434,$0435,$0436,$0437,$0438,$0439,$043A,$043B,$043C,$043D,$043E,$043F,
993 $0440,$0441,$0442,$0443,$0444,$0445,$0446,$0447,$0448,$0449,$044A,$044B,$044C,$044D,$044E,$044F
997 function decodeUtf8Char (s
: AnsiString; var pos
: Integer): AnsiChar;
1001 (* The following encodings are valid, except for the 5 and 6 byte
1005 * 1110xxxx 10xxxxxx 10xxxxxx
1006 * 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
1007 * 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
1008 * 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
1011 if pos
> length(s
) then exit
;
1015 if b
< $80 then begin result
:= AnsiChar(b
); exit
; end;
1017 // mask out unused bits
1018 if (b
and $FE) = $FC then b
:= b
and $01
1019 else if (b
and $FC) = $F8 then b
:= b
and $03
1020 else if (b
and $F8) = $F0 then b
:= b
and $07
1021 else if (b
and $F0) = $E0 then b
:= b
and $0F
1022 else if (b
and $E0) = $C0 then b
:= b
and $1F
1023 else exit
; // invalid utf8
1026 while pos
<= length(s
) do
1029 if (c
and $C0) <> $80 then break
; // no more
1031 b
:= b
or (c
and $3F);
1036 for c
:= 128 to 255 do if uni2wint
[c
] = b
then begin result
:= AnsiChar(c
and $FF); exit
; end;
1041 function utf8to1251 (s
: AnsiString): AnsiString;
1045 if not utf8Valid(s
) then begin result
:= s
; exit
; end;
1047 while pos
<= length(s
) do
1049 if Byte(s
[pos
]) >= $80 then break
;
1052 if pos
> length(s
) then begin result
:= s
; exit
; end; // nothing to do here
1055 while pos
<= length(s
) do result
:= result
+decodeUtf8Char(s
, pos
);
1059 // ////////////////////////////////////////////////////////////////////////// //
1060 // `pathname` will be modified if path is valid
1061 // `lastIsDir` should be `true` if we are searching for directory
1062 // nobody cares about shitdoze, so i'll use the same code path for it
1063 function findFileCI (var pathname
: AnsiString; lastIsDir
: Boolean=false): Boolean;
1067 newname
: AnsiString = '';
1068 curname
: AnsiString;
1074 result
:= (length(npt
) > 0);
1075 if (length(npt
) > 0) and ((npt
[1] = '/') or (npt
[1] = '\')) then newname
:= '/';
1076 while length(npt
) > 0 do
1078 // remove trailing slashes
1079 while (length(npt
) > 0) and ((npt
[1] = '/') or (npt
[1] = '\')) do Delete(npt
, 1, 1);
1080 if length(npt
) = 0 then break
;
1083 while (length(npt
) > 0) and (npt
[1] <> '/') and (npt
[1] <> '\') do
1085 curname
:= curname
+npt
[1];
1088 // remove trailing slashes again
1089 while (length(npt
) > 0) and ((npt
[1] = '/') or (npt
[1] = '\')) do Delete(npt
, 1, 1);
1090 wantdir
:= lastIsDir
or (length(npt
) > 0); // do we want directory here?
1091 //writeln(Format('npt=[%s]; newname=[%s]; curname=[%s]; wantdir=%d', [npt, newname, curname, Integer(wantdir)]));
1092 // try the easiest case first
1093 attr
:= FileGetAttr(newname
+curname
);
1096 if wantdir
= ((attr
and faDirectory
) <> 0) then
1099 newname
:= newname
+curname
;
1100 if wantdir
then newname
:= newname
+'/';
1104 //writeln(Format('npt=[%s]; newname=[%s]; curname=[%s]; wantdir=%d', [npt, newname, curname, Integer(wantdir)]));
1105 // alas, either not found, or invalid attributes
1108 if FindFirst(newname
+'*', faAnyFile
, sr
) = 0 then
1110 if (wantdir
= ((sr
.attr
and faDirectory
) <> 0)) and StrEquCI1251(sr
.name
, curname
) then
1113 newname
:= newname
+sr
.name
;
1114 if wantdir
then newname
:= newname
+'/';
1118 until FindNext(sr
) <> 0;
1122 if not foundher
then begin newname
:= ''; result
:= false; break
; end;
1124 if result
then pathname
:= newname
;
1128 const fileExtensions
: array [0..5] of AnsiString = ('.wad', '.dfzip', '.dfwad', '.pk3', '.pak', '.zip');
1130 function findDiskWad (fname
: AnsiString): AnsiString;
1132 origExt
: AnsiString = '';
1133 newExt
: AnsiString = '';
1136 //writeln('findDiskWad00: fname=<', fname, '>');
1137 if (findFileCI(fname
)) then begin result
:= fname
; exit
; end;
1138 origExt
:= getFilenameExt(fname
);
1139 fname
:= forceFilenameExt(fname
, '');
1140 //writeln(' findDiskWad01: fname=<', fname, '>; origExt=<', origExt, '>');
1141 for newExt
in fileExtensions
do
1143 //writeln(' findDiskWad02: fname=<', fname, '>; origExt=<', origExt, '>; newExt=<', newExt, '>');
1144 if (StrEquCI1251(newExt
, origExt
)) then
1149 result
:= fname
+newExt
;
1150 if (findFileCI(result
)) then exit
;
1156 function openDiskFileRO (pathname
: AnsiString): TStream
;
1158 if not findFileCI(pathname
) then raise Exception
.Create('can''t open file "'+pathname
+'"');
1159 result
:= TFileStream
.Create(pathname
, fmOpenRead
or {fmShareDenyWrite}fmShareDenyNone
);
1162 function createDiskFile (pathname
: AnsiString): TStream
;
1166 path
:= ExtractFilePath(pathname
);
1167 if length(path
) > 0 then
1169 if not findFileCI(path
, true) then raise Exception
.Create('can''t create file "'+pathname
+'"');
1171 result
:= TFileStream
.Create(path
+ExtractFileName(pathname
), fmCreate
);
1175 procedure writeIntegerLE (st
: TStream
; vp
: Pointer; size
: Integer);
1176 {$IFDEF ENDIAN_LITTLE}
1178 st
.writeBuffer(vp
^, size
);
1184 p
:= PByte(vp
)+size
-1;
1187 st
.writeBuffer(p
^, 1);
1194 procedure writeIntegerBE (st
: TStream
; vp
: Pointer; size
: Integer);
1195 {$IFDEF ENDIAN_LITTLE}
1199 p
:= PByte(vp
)+size
-1;
1202 st
.writeBuffer(p
^, 1);
1209 st
.writeBuffer(vp
^, size
);
1213 procedure writeSign (st
: TStream
; const sign
: AnsiString);
1215 if (Length(sign
) > 0) then st
.WriteBuffer(sign
[1], Length(sign
));
1218 function checkSign (st
: TStream
; const sign
: AnsiString): Boolean;
1220 buf
: packed array[0..7] of AnsiChar;
1224 if (Length(sign
) > 0) then
1226 if (Length(sign
) <= 8) then
1228 st
.ReadBuffer(buf
[0], Length(sign
));
1229 for f
:= 1 to Length(sign
) do if (buf
[f
-1] <> sign
[f
]) then exit
;
1233 for f
:= 1 to Length(sign
) do
1235 st
.ReadBuffer(buf
[0], 1);
1236 if (buf
[0] <> sign
[f
]) then exit
;
1243 procedure writeInt (st
: TStream
; v
: Byte); overload
; begin writeIntegerLE(st
, @v
, 1); end;
1244 procedure writeInt (st
: TStream
; v
: ShortInt); overload
; begin writeIntegerLE(st
, @v
, 1); end;
1245 procedure writeInt (st
: TStream
; v
: Word); overload
; begin writeIntegerLE(st
, @v
, 2); end;
1246 procedure writeInt (st
: TStream
; v
: SmallInt); overload
; begin writeIntegerLE(st
, @v
, 2); end;
1247 procedure writeInt (st
: TStream
; v
: LongWord); overload
; begin writeIntegerLE(st
, @v
, 4); end;
1248 procedure writeInt (st
: TStream
; v
: LongInt); overload
; begin writeIntegerLE(st
, @v
, 4); end;
1249 procedure writeInt (st
: TStream
; v
: Int64); overload
; begin writeIntegerLE(st
, @v
, 8); end;
1250 procedure writeInt (st
: TStream
; v
: UInt64); overload
; begin writeIntegerLE(st
, @v
, 8); end;
1252 procedure writeIntBE (st
: TStream
; v
: Byte); overload
; begin writeIntegerBE(st
, @v
, 1); end;
1253 procedure writeIntBE (st
: TStream
; v
: ShortInt); overload
; begin writeIntegerBE(st
, @v
, 1); end;
1254 procedure writeIntBE (st
: TStream
; v
: Word); overload
; begin writeIntegerBE(st
, @v
, 2); end;
1255 procedure writeIntBE (st
: TStream
; v
: SmallInt); overload
; begin writeIntegerBE(st
, @v
, 2); end;
1256 procedure writeIntBE (st
: TStream
; v
: LongWord); overload
; begin writeIntegerBE(st
, @v
, 4); end;
1257 procedure writeIntBE (st
: TStream
; v
: LongInt); overload
; begin writeIntegerBE(st
, @v
, 4); end;
1258 procedure writeIntBE (st
: TStream
; v
: Int64); overload
; begin writeIntegerBE(st
, @v
, 8); end;
1259 procedure writeIntBE (st
: TStream
; v
: UInt64); overload
; begin writeIntegerBE(st
, @v
, 8); end;
1261 procedure writeBool (st
: TStream
; b
: Boolean); begin writeInt(st
, Byte(b
)); end;
1262 function readBool (st
: TStream
): Boolean; begin result
:= (readByte(st
) <> 0); end;
1265 procedure writeStr (st
: TStream
; const str
: AnsiString; maxlen
: LongWord=65535);
1267 if (Length(str
) > maxlen
) then raise XStreamError
.Create('string too long');
1268 if (maxlen
<= 65535) then writeInt(st
, Word(Length(str
))) else writeInt(st
, LongWord(Length(str
)));
1269 if (Length(str
) > 0) then st
.WriteBuffer(str
[1], Length(str
));
1272 function readStr (st
: TStream
; maxlen
: LongWord=65535): AnsiString;
1277 if (maxlen
<= 65535) then len
:= readWord(st
) else len
:= Integer(readLongWord(st
));
1278 if (len
< 0) or (len
> maxlen
) then raise XStreamError
.Create('string too long');
1281 SetLength(result
, len
);
1282 st
.ReadBuffer(result
[1], len
);
1287 procedure readIntegerLE (st
: TStream
; vp
: Pointer; size
: Integer);
1288 {$IFDEF ENDIAN_LITTLE}
1290 st
.readBuffer(vp
^, size
);
1296 p
:= PByte(vp
)+size
-1;
1299 st
.readBuffer(p
^, 1);
1306 procedure readIntegerBE (st
: TStream
; vp
: Pointer; size
: Integer);
1307 {$IFDEF ENDIAN_LITTLE}
1311 p
:= PByte(vp
)+size
-1;
1314 st
.readBuffer(p
^, 1);
1321 st
.readBuffer(vp
^, size
);
1325 function readByte (st
: TStream
): Byte; begin readIntegerLE(st
, @result
, 1); end;
1326 function readShortInt (st
: TStream
): ShortInt; begin readIntegerLE(st
, @result
, 1); end;
1327 function readWord (st
: TStream
): Word; begin readIntegerLE(st
, @result
, 2); end;
1328 function readSmallInt (st
: TStream
): SmallInt; begin readIntegerLE(st
, @result
, 2); end;
1329 function readLongWord (st
: TStream
): LongWord; begin readIntegerLE(st
, @result
, 4); end;
1330 function readLongInt (st
: TStream
): LongInt; begin readIntegerLE(st
, @result
, 4); end;
1331 function readInt64 (st
: TStream
): Int64; begin readIntegerLE(st
, @result
, 8); end;
1332 function readUInt64 (st
: TStream
): UInt64; begin readIntegerLE(st
, @result
, 8); end;
1334 function readByteBE (st
: TStream
): Byte; begin readIntegerBE(st
, @result
, 1); end;
1335 function readShortIntBE (st
: TStream
): ShortInt; begin readIntegerBE(st
, @result
, 1); end;
1336 function readWordBE (st
: TStream
): Word; begin readIntegerBE(st
, @result
, 2); end;
1337 function readSmallIntBE (st
: TStream
): SmallInt; begin readIntegerBE(st
, @result
, 2); end;
1338 function readLongWordBE (st
: TStream
): LongWord; begin readIntegerBE(st
, @result
, 4); end;
1339 function readLongIntBE (st
: TStream
): LongInt; begin readIntegerBE(st
, @result
, 4); end;
1340 function readInt64BE (st
: TStream
): Int64; begin readIntegerBE(st
, @result
, 8); end;
1341 function readUInt64BE (st
: TStream
): UInt64; begin readIntegerBE(st
, @result
, 8); end;
1344 // ////////////////////////////////////////////////////////////////////////// //
1345 function nmin (a
, b
: Byte): Byte; inline; overload
; begin if (a
< b
) then result
:= a
else result
:= b
; end;
1346 function nmin (a
, b
: ShortInt): ShortInt; inline; overload
; begin if (a
< b
) then result
:= a
else result
:= b
; end;
1347 function nmin (a
, b
: Word): Word; inline; overload
; begin if (a
< b
) then result
:= a
else result
:= b
; end;
1348 function nmin (a
, b
: SmallInt): SmallInt; inline; overload
; begin if (a
< b
) then result
:= a
else result
:= b
; end;
1349 function nmin (a
, b
: LongWord): LongWord; inline; overload
; begin if (a
< b
) then result
:= a
else result
:= b
; end;
1350 function nmin (a
, b
: LongInt): LongInt; inline; overload
; begin if (a
< b
) then result
:= a
else result
:= b
; end;
1351 function nmin (a
, b
: Int64): Int64; inline; overload
; begin if (a
< b
) then result
:= a
else result
:= b
; end;
1352 function nmin (a
, b
: UInt64): UInt64; inline; overload
; begin if (a
< b
) then result
:= a
else result
:= b
; end;
1353 function nmin (a
, b
: Single): Single; inline; overload
; begin if (a
< b
) then result
:= a
else result
:= b
; end;
1354 function nmin (a
, b
: Double): Double; inline; overload
; begin if (a
< b
) then result
:= a
else result
:= b
; end;
1355 function nmin (a
, b
: Extended
): Extended
; inline; overload
; begin if (a
< b
) then result
:= a
else result
:= b
; end;
1357 function nmax (a
, b
: Byte): Byte; inline; overload
; begin if (a
> b
) then result
:= a
else result
:= b
; end;
1358 function nmax (a
, b
: ShortInt): ShortInt; inline; overload
; begin if (a
> b
) then result
:= a
else result
:= b
; end;
1359 function nmax (a
, b
: Word): Word; inline; overload
; begin if (a
> b
) then result
:= a
else result
:= b
; end;
1360 function nmax (a
, b
: SmallInt): SmallInt; inline; overload
; begin if (a
> b
) then result
:= a
else result
:= b
; end;
1361 function nmax (a
, b
: LongWord): LongWord; inline; overload
; begin if (a
> b
) then result
:= a
else result
:= b
; end;
1362 function nmax (a
, b
: LongInt): LongInt; inline; overload
; begin if (a
> b
) then result
:= a
else result
:= b
; end;
1363 function nmax (a
, b
: Int64): Int64; inline; overload
; begin if (a
> b
) then result
:= a
else result
:= b
; end;
1364 function nmax (a
, b
: UInt64): UInt64; inline; overload
; begin if (a
> b
) then result
:= a
else result
:= b
; end;
1365 function nmax (a
, b
: Single): Single; inline; overload
; begin if (a
> b
) then result
:= a
else result
:= b
; end;
1366 function nmax (a
, b
: Double): Double; inline; overload
; begin if (a
> b
) then result
:= a
else result
:= b
; end;
1367 function nmax (a
, b
: Extended
): Extended
; inline; overload
; begin if (a
> b
) then result
:= a
else result
:= b
; end;
1369 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;
1370 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;
1371 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;
1372 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;
1373 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;
1374 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;
1375 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;
1376 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;
1377 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;
1378 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;
1379 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;
1382 // ////////////////////////////////////////////////////////////////////////// //
1384 function snprintf (buf
: PAnsiChar; bufsize
: SizeUInt
; const fmt
: PAnsiChar): SizeUInt
; cdecl; varargs
; external 'msvcrt.dll' name
'_snprintf';
1386 function snprintf (buf
: PAnsiChar; bufsize
: SizeUInt
; const fmt
: PAnsiChar): SizeUInt
; cdecl; varargs
; external 'libc' name
'snprintf';
1391 procedure conwriter (constref buf; len: SizeUInt);
1397 if (len < 1) then exit;
1401 if (len > 255) then slen := 255 else slen := Integer(len);
1402 Move(b^, ss[1], len);
1403 ss[0] := AnsiChar(slen);
1412 function formatstrf (const fmt
: AnsiString; const args
: array of const; writerCB
: TFormatStrFCallback
=nil): AnsiString;
1414 PadSpaces
: AnsiString = ' ';
1415 PadZeroes
: AnsiString = '00000000000000000000000000000000000000000000000000000000000000000000000';
1417 curarg
: Integer = 0; // current arg in `args`
1418 sign
, fmtch
: AnsiChar;
1420 width
, prec
: Integer; // width and precision
1421 spos
, epos
: Integer;
1423 strbuf
: array[0..256] of AnsiChar;
1425 fmtbuf
: array[0..256] of AnsiChar;
1429 ccname
: ShortString;
1431 procedure writer (constref buf
; len
: SizeUInt
);
1437 if (len
< 1) then exit
;
1439 if assigned(writerCB
) then
1447 if (len
> 255) then slen
:= 255 else slen
:= Integer(len
);
1448 Move(b
^, ss
[1], slen
);
1449 ss
[0] := AnsiChar(slen
);
1457 procedure xwrite (const s
: AnsiString);
1459 if (Length(s
) > 0) then writer(PAnsiChar(s
)^, Length(s
));
1462 procedure putFmtChar (ch
: AnsiChar);
1464 fmtbuf
[fmtblen
] := ch
;
1468 procedure putFmtInt (n
: Integer);
1472 len
:= snprintf(@fmtbuf
[fmtblen
], Length(fmtbuf
)-fmtblen
, '%d', n
);
1473 if (len
> 0) then Inc(fmtblen
, len
);
1476 procedure buildCFormat (const pfx
: AnsiString='');
1481 for f
:= 1 to Length(pfx
) do putFmtChar(pfx
[f
]);
1483 if (sign
<> ' ') then putFmtChar(sign
);
1484 if (width
>= 0) then
1486 if (zeropad
) then putFmtChar('0');
1495 fmtbuf
[fmtblen
] := #0;
1498 procedure writeStrBuf ();
1500 if (strblen
> 0) then writer(strbuf
, strblen
);
1503 function i642str (n
: Int64; hex
: Boolean; hexup
: Boolean): PAnsiChar;
1508 if (n
= $8000000000000000) then
1510 if hex
then snprintf(@strbuf
[0], Length(strbuf
), '-8000000000000000')
1511 else snprintf(@strbuf
[0], Length(strbuf
), '-9223372036854775808');
1512 result
:= @strbuf
[0];
1517 if neg
then n
:= -n
;
1518 xpos
:= High(strbuf
);
1519 strbuf
[xpos
] := #0; Dec(xpos
);
1523 strbuf
[xpos
] := AnsiChar((n
mod 10)+48);
1529 if (n
mod 16 > 9) then
1531 strbuf
[xpos
] := AnsiChar((n
mod 16)+48+7);
1532 if not hexup
then Inc(strbuf
[xpos
], 32);
1534 else strbuf
[xpos
] := AnsiChar((n
mod 16)+48);
1539 if neg
then begin strbuf
[xpos
] := '-'; Dec(xpos
); end;
1540 result
:= @strbuf
[xpos
+1];
1544 function ui642str (n
: UInt64; hex
: Boolean; hexup
: Boolean): PAnsiChar;
1548 xpos
:= High(strbuf
);
1549 strbuf
[xpos
] := #0; Dec(xpos
);
1553 strbuf
[xpos
] := AnsiChar((n
mod 10)+48);
1559 if (n
mod 16 > 9) then
1561 strbuf
[xpos
] := AnsiChar((n
mod 16)+48+7);
1562 if not hexup
then Inc(strbuf
[xpos
], 32);
1564 else strbuf
[xpos
] := AnsiChar((n
mod 16)+48);
1569 result
:= @strbuf
[xpos
+1];
1572 procedure indent (len
: Integer);
1578 if (len
> Length(PadSpaces
)) then ilen
:= Length(PadSpaces
) else ilen
:= len
;
1579 writer(PAnsiChar(PadSpaces
)^, ilen
);
1584 procedure indent0 (len
: Integer);
1590 if (len
> Length(PadZeroes
)) then ilen
:= Length(PadZeroes
) else ilen
:= len
;
1591 writer(PAnsiChar(PadZeroes
)^, ilen
);
1599 while (spos
<= Length(fmt
)) do
1601 // print literal part
1603 while (epos
<= Length(fmt
)) and (fmt
[epos
] <> '%') do Inc(epos
);
1604 // output literal part
1605 if (epos
> spos
) then
1607 if (epos
> Length(fmt
)) then
1609 writer((PAnsiChar(fmt
)+spos
-1)^, epos
-spos
);
1612 if (epos
+1 > Length(fmt
)) then Inc(epos
) // last percent, output literally
1613 else if (fmt
[epos
+1] = '%') then // special case
1616 writer((PAnsiChar(fmt
)+spos
-1)^, epos
-spos
);
1621 writer((PAnsiChar(fmt
)+spos
-1)^, epos
-spos
);
1626 // check if we have argument for this format string
1627 if (curarg
> High(args
)) then
1629 xwrite('<OUT OF ARGS>');
1630 writer((PAnsiChar(fmt
)+spos
-1)^, Length(fmt
)-spos
+1);
1634 if (spos
+1 > Length(fmt
)) then break
; // oops
1635 assert(fmt
[spos
] = '%');
1637 // parse format; check for sign
1638 if (fmt
[spos
] = '-') then begin sign
:= '-'; Inc(spos
); end
1639 else if (fmt
[spos
] = '+') then begin sign
:= '+'; Inc(spos
); end
1642 if (spos
> Length(fmt
)) then begin xwrite('<INVALID FORMAT>'); break
; end;
1643 if (sign
<> ' ') or ((fmt
[spos
] >= '0') and (fmt
[spos
] <= '9')) then
1645 if (fmt
[spos
] < '0') or (fmt
[spos
] > '9') then begin xwrite('<INVALID FORMAT>'); writer((PAnsiChar(fmt
)+spos
-1)^, Length(fmt
)-spos
+1); break
; end;
1646 zeropad
:= (fmt
[spos
] = '0');
1648 while (spos
<= Length(fmt
)) do
1651 if (ch
< '0') or (ch
> '9') then break
;
1652 width
:= width
*10+Integer(ch
)-48;
1663 if (spos
<= Length(fmt
)) and (fmt
[spos
] = '.') then
1666 if (spos
> Length(fmt
)) then begin xwrite('<INVALID FORMAT>'); break
; end;
1667 if (fmt
[spos
] < '0') or (fmt
[spos
] > '9') then begin xwrite('<INVALID FORMAT>'); writer((PAnsiChar(fmt
)+spos
-1)^, Length(fmt
)-spos
+1); break
; end;
1669 while (spos
<= Length(fmt
)) do
1672 if (ch
< '0') or (ch
> '9') then break
;
1673 prec
:= prec
*10+Integer(ch
)-48;
1678 if (spos
> Length(fmt
)) then begin xwrite('<INVALID FORMAT>'); break
; end;
1681 // done parsing format, check for valid format chars
1682 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;
1683 // now write formatted string
1684 case args
[curarg
].VType
of
1685 vtInteger
: // args[curarg].VInteger
1687 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;
1688 if (fmtch
= 's') then fmtch
:= 'd';
1690 strblen
:= snprintf(@strbuf
[0], Length(strbuf
), @fmtbuf
[0], args
[curarg
].VInteger
);
1693 vtBoolean
: // args[curarg].VBoolean
1698 if args
[curarg
].VBoolean
then strblen
:= snprintf(@strbuf
[0], Length(strbuf
), @fmtbuf
[0], 'true')
1699 else strblen
:= snprintf(@strbuf
[0], Length(strbuf
), @fmtbuf
[0], 'false');
1705 if args
[curarg
].VBoolean
then strblen
:= snprintf(@strbuf
[0], Length(strbuf
), @fmtbuf
[0], AnsiChar('t'))
1706 else strblen
:= snprintf(@strbuf
[0], Length(strbuf
), @fmtbuf
[0], AnsiChar('f'));
1712 strblen
:= snprintf(@strbuf
[0], Length(strbuf
), @fmtbuf
[0], Integer(args
[curarg
].VBoolean
));
1717 xwrite('<INVALID FORMAT CHAR>');
1718 writer((PAnsiChar(fmt
)+spos
-1)^, Length(fmt
)-spos
+1);
1722 vtChar
: // args[curarg].VChar
1728 strblen
:= snprintf(@strbuf
[0], Length(strbuf
), @fmtbuf
[0], args
[curarg
].VChar
);
1734 strblen
:= snprintf(@strbuf
[0], Length(strbuf
), @fmtbuf
[0], Integer(args
[curarg
].VChar
));
1739 xwrite('<INVALID FORMAT CHAR>');
1740 writer((PAnsiChar(fmt
)+spos
-1)^, Length(fmt
)-spos
+1);
1744 //vtWideChar: begin end; // args[curarg].VWideChar (WideChar)
1745 vtExtended
: // args[curarg].VExtended^
1751 strblen
:= snprintf(@strbuf
[0], Length(strbuf
), @fmtbuf
[0], Double(args
[curarg
].VExtended
^));
1757 strblen
:= snprintf(@strbuf
[0], Length(strbuf
), @fmtbuf
[0], Double(args
[curarg
].VExtended
^));
1763 strblen
:= snprintf(@strbuf
[0], Length(strbuf
), @fmtbuf
[0], Integer(trunc(args
[curarg
].VExtended
^)));
1769 strblen
:= snprintf(@strbuf
[0], Length(strbuf
), @fmtbuf
[0], LongWord(trunc(args
[curarg
].VExtended
^)));
1774 xwrite('<INVALID FORMAT CHAR>');
1775 writer((PAnsiChar(fmt
)+spos
-1)^, Length(fmt
)-spos
+1);
1779 vtString
: // args[curarg].VString^ (PShortString)
1781 if (sign
<> '-') then indent(width
-Length(args
[curarg
].VString
^));
1782 writer(args
[curarg
].VString
^[1], Length(args
[curarg
].VString
^));
1783 if (sign
= '-') then indent(width
-Length(args
[curarg
].VString
^));
1785 vtPointer
: // args[curarg].VPointer
1790 if (width
< 8) then width
:= 8;
1793 strblen
:= snprintf(@strbuf
[0], Length(strbuf
), @fmtbuf
[0], PtrUInt(args
[curarg
].VPointer
));
1796 'u', 'd', 'x', 'p', 'X':
1798 if (fmtch
= 'p') then fmtch
:= 'x';
1799 if (width
< 8) then width
:= 8;
1802 strblen
:= snprintf(@strbuf
[0], Length(strbuf
), @fmtbuf
[0], PtrUInt(args
[curarg
].VPointer
));
1807 xwrite('<INVALID FORMAT CHAR>');
1808 writer((PAnsiChar(fmt
)+spos
-1)^, Length(fmt
)-spos
+1);
1812 vtPChar
: // args[curarg].VPChar
1813 if (args
[curarg
].VPChar
= nil) then
1815 if (sign
<> '-') then indent(width
-3);
1817 if (sign
= '-') then indent(width
-3);
1822 while (args
[curarg
].VPChar
[pclen
] <> #0) do Inc(pclen
);
1823 if (sign
<> '-') then indent(width
-pclen
);
1824 writer(args
[curarg
].VPChar
^, pclen
);
1825 if (sign
= '-') then indent(width
-pclen
);
1827 vtObject
: // args[curarg].VObject.Classname (TObject)
1829 if (args
[curarg
].VObject
<> nil) then ccname
:= args
[curarg
].VObject
.Classname
else ccname
:= '<nil>';
1830 if (sign
<> '-') then indent(width
-Length(ccname
));
1832 if (sign
= '-') then indent(width
-Length(ccname
));
1834 vtClass
: // args[curarg].VClass.Classname (TClass)
1836 if (args
[curarg
].VClass
<> nil) then ccname
:= args
[curarg
].VClass
.Classname
else ccname
:= '<nil>';
1837 if (sign
<> '-') then indent(width
-Length(ccname
));
1839 if (sign
= '-') then indent(width
-Length(ccname
));
1841 //vtPWideChar: begin end; // args[curarg].VPWideChar (PWideChar)
1842 vtAnsiString
: // AnsiString(args[curarg].VAnsiString) (Pointer)
1844 if (sign
<> '-') then indent(width
-Length(AnsiString(args
[curarg
].VAnsiString
)));
1845 xwrite(AnsiString(args
[curarg
].VAnsiString
));
1846 if (sign
= '-') then indent(width
-Length(AnsiString(args
[curarg
].VAnsiString
)));
1848 //vtCurrency: begin end; // args[curarg].VCurrency (PCurrency)
1849 //vtVariant: begin end; // args[curarg].VVariant^ (PVariant)
1850 //vtInterface: begin end; // args[curarg].VInterface (Pointer);
1851 //vtWideString: begin end; // args[curarg].VWideString (Pointer);
1852 vtInt64
: // args[curarg].VInt64^ (PInt64)
1855 's','d','u': pc
:= i642str(args
[curarg
].VInt64
^, false, false);
1856 'x': pc
:= i642str(args
[curarg
].VInt64
^, true, false);
1857 'X': pc
:= i642str(args
[curarg
].VInt64
^, true, true);
1858 else begin xwrite('<INVALID FORMAT CHAR>'); writer((PAnsiChar(fmt
)+spos
-1)^, Length(fmt
)-spos
+1); break
; end;
1861 while (pc
[pclen
] <> #0) do Inc(pclen
);
1862 if (sign
<> '-') and (width
> pclen
) then
1866 if (pc
[0] = '-') or (pc
[0] = '+') then
1869 indent0(width
-pclen
-1);
1875 indent0(width
-pclen
);
1880 indent(width
-pclen
);
1884 if (sign
= '-') then indent(width
-pclen
);
1886 vtQWord
: // args[curarg].VQWord^ (PQWord)
1889 's','d','u': pc
:= ui642str(args
[curarg
].VInt64
^, false, false);
1890 'x': pc
:= ui642str(args
[curarg
].VInt64
^, true, false);
1891 'X': pc
:= ui642str(args
[curarg
].VInt64
^, true, true);
1892 else begin xwrite('<INVALID FORMAT CHAR>'); writer((PAnsiChar(fmt
)+spos
-1)^, Length(fmt
)-spos
+1); break
; end;
1895 while (pc
[pclen
] <> #0) do Inc(pclen
);
1896 if (sign
<> '-') then begin if zeropad
then indent0(width
-pclen
) else indent(width
-pclen
); end;
1898 if (sign
= '-') then indent(width
-pclen
);
1902 xwrite('<INVALID TYPE>');
1903 writer((PAnsiChar(fmt
)+spos
-1)^, Length(fmt
)-spos
+1);
1916 i64: Int64 = -$A000000000;
1917 ui64: UInt64 = $A000000000;
1919 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']);
1920 writef(conwriter, 'test float:<%s;%u;%f;%g>'#10, [666.6942, 666.6942, 666.6942, 666.6942]);
1923 writef(conwriter, 'test ss:<%5s;%040s>'#10, [ss, ss]);
1924 writef(conwriter, 'test ls:<%5s;%040s>'#10, [ls, ls]);
1925 writef(conwriter, 'test pointer:<%s;%x;%p>'#10, [@ss, @ss, @ss]);
1926 writef(conwriter, 'test i64:<%s;%x;%015d;%u;%X>'#10, [i64, i64, i64, i64, i64]);
1927 writef(conwriter, 'test ui64:<%s;%x;%15d;%015u;%X>'#10, [ui64, ui64, ui64, ui64, ui64]);