DEADSOFTWARE

a854ca45bc922f155465ed98a32daad0710ced8c
[d2df-sdl.git] / src / shared / utils.pas
1 (* Copyright (C) Doom 2D: Forever Developers
2 *
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, version 3 of the License ONLY.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 *)
15 {$INCLUDE a_modes.inc}
16 unit utils;
18 interface
20 uses
21 SysUtils, Classes;
24 // ////////////////////////////////////////////////////////////////////////// //
25 type
26 SSArray = array of ShortString;
29 // ////////////////////////////////////////////////////////////////////////// //
30 type
31 TUtf8DecoderFast = packed record
32 public
33 const Replacement = $FFFD; // replacement char for invalid unicode
34 const Accept = 0;
35 const Reject = 12;
37 private
38 state: LongWord;
40 public
41 codepoint: LongWord; // decoded codepoint (valid only when decoder is in "complete" state)
43 public
44 constructor Create (v: Boolean{fuck you, fpc});
46 procedure reset (); inline;
48 function complete (): Boolean; inline; // is current character complete? take `codepoint` then
49 function invalid (): Boolean; inline;
50 function completeOrInvalid (): Boolean; inline;
52 // process one byte, return `true` if codepoint is ready
53 function decode (b: Byte): Boolean; inline; overload;
54 function decode (c: AnsiChar): Boolean; inline; overload;
55 end;
58 // ////////////////////////////////////////////////////////////////////////// //
59 function getFilenameExt (const fn: AnsiString): AnsiString;
60 function setFilenameExt (const fn, ext: AnsiString): AnsiString;
61 function forceFilenameExt (const fn, ext: AnsiString): AnsiString;
63 // strips out name from `fn`, leaving trailing slash
64 function getFilenamePath (const fn: AnsiString): AnsiString;
66 // ends with '/' or '\'?
67 function isFilenamePath (const fn: AnsiString): Boolean;
69 // strips extra trailing slashes in `path, and extra leading slashes in `fn`
70 // will add slash to `path`, even if `fn` is empty!
71 function filenameConcat (const path, fn: AnsiString): AnsiString;
73 // does filename have one of ".wad", ".pk3", ".zip" extensions?
74 function hasWadExtension (const fn: AnsiString): Boolean;
76 // does filepath have ".XXX:\" in it?
77 function isWadPath (const fn: AnsiString): Boolean;
79 // adds ".wad" extension if filename doesn't have one of ".wad", ".pk3", ".zip"
80 function addWadExtension (const fn: AnsiString): AnsiString;
82 // check wad signature
83 function isWadData (data: Pointer; len: LongWord): Boolean;
85 // convert number to strig with nice commas
86 function int64ToStrComma (i: Int64): AnsiString;
88 function upcase1251 (ch: AnsiChar): AnsiChar; inline;
89 function locase1251 (ch: AnsiChar): AnsiChar; inline;
91 function toLowerCase1251 (const s: AnsiString): AnsiString;
93 // `true` if strings are equal; ignoring case for cp1251
94 function strEquCI1251 (const s0, s1: AnsiString): Boolean;
96 function utf8Valid (const s: AnsiString): Boolean;
98 function utf8to1251 (s: AnsiString): AnsiString;
100 // `pathname` will be modified if path is valid
101 // `lastIsDir` should be `true` if we are searching for directory
102 // nobody cares about shitdoze, so i'll use the same code path for it
103 function findFileCI (var pathname: AnsiString; lastIsDir: Boolean=false): Boolean;
105 // return fixed AnsiString or empty AnsiString
106 function findDiskWad (fname: AnsiString): AnsiString;
107 // slashes must be normalized!
108 function isWadNamesEqu (wna, wnb: AnsiString): Boolean;
110 // they throws
111 function openDiskFileRO (pathname: AnsiString): TStream;
112 function createDiskFile (pathname: AnsiString): TStream;
114 // little endian
115 procedure writeSign (st: TStream; const sign: AnsiString);
116 function checkSign (st: TStream; const sign: AnsiString): Boolean;
118 procedure writeBool (st: TStream; b: Boolean);
119 function readBool (st: TStream): Boolean;
121 procedure writeStr (st: TStream; const str: AnsiString; maxlen: LongWord=65535);
122 function readStr (st: TStream; maxlen: LongWord=65535): AnsiString;
124 procedure writeInt (st: TStream; v: Byte); overload;
125 procedure writeInt (st: TStream; v: ShortInt); overload;
126 procedure writeInt (st: TStream; v: Word); overload;
127 procedure writeInt (st: TStream; v: SmallInt); overload;
128 procedure writeInt (st: TStream; v: LongWord); overload;
129 procedure writeInt (st: TStream; v: LongInt); overload;
130 procedure writeInt (st: TStream; v: Int64); overload;
131 procedure writeInt (st: TStream; v: UInt64); overload;
133 function readByte (st: TStream): Byte;
134 function readShortInt (st: TStream): ShortInt;
135 function readWord (st: TStream): Word;
136 function readSmallInt (st: TStream): SmallInt;
137 function readLongWord (st: TStream): LongWord;
138 function readLongInt (st: TStream): LongInt;
139 function readInt64 (st: TStream): Int64;
140 function readUInt64 (st: TStream): UInt64;
142 // big endian
143 procedure writeIntBE (st: TStream; v: Byte); overload;
144 procedure writeIntBE (st: TStream; v: ShortInt); overload;
145 procedure writeIntBE (st: TStream; v: Word); overload;
146 procedure writeIntBE (st: TStream; v: SmallInt); overload;
147 procedure writeIntBE (st: TStream; v: LongWord); overload;
148 procedure writeIntBE (st: TStream; v: LongInt); overload;
149 procedure writeIntBE (st: TStream; v: Int64); overload;
150 procedure writeIntBE (st: TStream; v: UInt64); overload;
152 function readByteBE (st: TStream): Byte;
153 function readShortIntBE (st: TStream): ShortInt;
154 function readWordBE (st: TStream): Word;
155 function readSmallIntBE (st: TStream): SmallInt;
156 function readLongWordBE (st: TStream): LongWord;
157 function readLongIntBE (st: TStream): LongInt;
158 function readInt64BE (st: TStream): Int64;
159 function readUInt64BE (st: TStream): UInt64;
162 function nmin (a, b: Byte): Byte; inline; overload;
163 function nmin (a, b: ShortInt): ShortInt; inline; overload;
164 function nmin (a, b: Word): Word; inline; overload;
165 function nmin (a, b: SmallInt): SmallInt; inline; overload;
166 function nmin (a, b: LongWord): LongWord; inline; overload;
167 function nmin (a, b: LongInt): LongInt; inline; overload;
168 function nmin (a, b: Int64): Int64; inline; overload;
169 function nmin (a, b: UInt64): UInt64; inline; overload;
170 function nmin (a, b: Single): Single; inline; overload;
171 function nmin (a, b: Double): Double; inline; overload;
172 {$IF DEFINED(CPU386) OR DEFINED(CPUAMD64)}
173 function nmin (a, b: Extended): Extended; inline; overload;
174 {$ENDIF}
176 function nmax (a, b: Byte): Byte; inline; overload;
177 function nmax (a, b: ShortInt): ShortInt; inline; overload;
178 function nmax (a, b: Word): Word; inline; overload;
179 function nmax (a, b: SmallInt): SmallInt; inline; overload;
180 function nmax (a, b: LongWord): LongWord; inline; overload;
181 function nmax (a, b: LongInt): LongInt; inline; overload;
182 function nmax (a, b: Int64): Int64; inline; overload;
183 function nmax (a, b: UInt64): UInt64; inline; overload;
184 function nmax (a, b: Single): Single; inline; overload;
185 function nmax (a, b: Double): Double; inline; overload;
186 {$IF DEFINED(CPU386) OR DEFINED(CPUAMD64)}
187 function nmax (a, b: Extended): Extended; inline; overload;
188 {$ENDIF}
189 function nclamp (v, a, b: Byte): Byte; inline; overload;
190 function nclamp (v, a, b: ShortInt): ShortInt; inline; overload;
191 function nclamp (v, a, b: Word): Word; inline; overload;
192 function nclamp (v, a, b: SmallInt): SmallInt; inline; overload;
193 function nclamp (v, a, b: LongWord): LongWord; inline; overload;
194 function nclamp (v, a, b: LongInt): LongInt; inline; overload;
195 function nclamp (v, a, b: Int64): Int64; inline; overload;
196 function nclamp (v, a, b: UInt64): UInt64; inline; overload;
197 function nclamp (v, a, b: Single): Single; inline; overload;
198 function nclamp (v, a, b: Double): Double; inline; overload;
199 {$IF DEFINED(CPU386) OR DEFINED(CPUAMD64)}
200 function nclamp (v, a, b: Extended): Extended; inline; overload;
201 {$ENDIF}
203 type
204 TFormatStrFCallback = procedure (constref buf; len: SizeUInt);
206 // returns formatted string if `writerCB` is `nil`, empty string otherwise
207 function formatstrf (const fmt: AnsiString; const args: array of const; writerCB: TFormatStrFCallback=nil): AnsiString;
209 function wchar2win (wc: WideChar): AnsiChar; inline;
210 function utf2win (const s: AnsiString): AnsiString;
211 function win2utf (const s: AnsiString): AnsiString;
212 function digitInBase (ch: AnsiChar; base: Integer): Integer;
214 // returns string in single or double quotes
215 // single quotes supports only pascal-style '' for single quote char
216 // double quotes supports c-style escapes
217 // function will select quote mode automatically
218 function quoteStr (const s: AnsiString): AnsiString;
221 type
222 generic TSimpleList<ItemT> = class
223 private
224 //type PItemT = ^ItemT;
225 type TItemArr = array of ItemT;
227 public
228 type
229 TEnumerator = record
230 private
231 mItems: TItemArr;
232 mCount: Integer;
233 mCurrent: Integer;
234 public
235 constructor Create (const aitems: TItemArr; acount: Integer);
236 function MoveNext: Boolean;
237 function getCurrent (): ItemT;
238 property Current: ItemT read getCurrent;
239 end;
241 private
242 mItems: TItemArr;
243 mCount: Integer; // can be less than `mItems` size
245 private
246 function getAt (idx: Integer): ItemT; inline;
247 procedure setAt (idx: Integer; const it: ItemT); inline;
249 function getCapacity (): Integer; inline;
250 procedure setCapacity (v: Integer); inline;
252 public
253 constructor Create (acapacity: Integer=-1);
254 destructor Destroy (); override;
256 //WARNING! don't change list contents in `for ... in`!
257 function GetEnumerator (): TEnumerator;
259 procedure reset (); inline; // won't resize `mItems`
260 procedure clear (); inline;
262 procedure append (constref it: ItemT); inline;
263 procedure delete (idx: Integer); inline;
264 function remove (idx: Integer): ItemT; inline;
266 public
267 property count: Integer read mCount;
268 property capacity: Integer read getCapacity write setCapacity;
269 property at[idx: Integer]: ItemT read getAt write setAt; default;
270 end;
273 procedure FillMemory (Dest: Pointer; Len: LongWord; Ch: Byte); inline;
274 procedure CopyMemory (Dest: Pointer; Src: Pointer; Len: LongWord); inline;
275 procedure ZeroMemory (Dest: Pointer; Len: LongWord); inline;
278 implementation
280 uses
281 xstreams;
284 // ////////////////////////////////////////////////////////////////////////// //
285 procedure CopyMemory (Dest: Pointer; Src: Pointer; Len: LongWord); inline;
286 begin
287 Move(Src^, Dest^, Len);
288 end;
290 procedure FillMemory (Dest: Pointer; Len: LongWord; Ch: Byte); inline;
291 begin
292 FillChar(Dest^, Len, Ch);
293 end;
295 procedure ZeroMemory (Dest: Pointer; Len: LongWord); inline;
296 begin
297 FillChar(Dest^, Len, 0);
298 end;
301 // ////////////////////////////////////////////////////////////////////////// //
302 constructor TSimpleList.TEnumerator.Create (const aitems: TItemArr; acount: Integer);
303 begin
304 mItems := aitems;
305 mCurrent := -1;
306 mCount := acount;
307 end;
309 function TSimpleList.TEnumerator.MoveNext: Boolean;
310 begin
311 Inc(mCurrent);
312 result := (mCurrent < mCount);
313 end;
315 function TSimpleList.TEnumerator.getCurrent (): ItemT;
316 begin
317 result := mItems[mCurrent];
318 end;
321 // ////////////////////////////////////////////////////////////////////////// //
322 constructor TSimpleList.Create (acapacity: Integer=-1);
323 begin
324 mItems := nil;
325 if (acapacity > 0) then SetLength(mItems, acapacity);
326 mCount := 0;
327 end;
330 destructor TSimpleList.Destroy ();
331 begin
332 mItems := nil;
333 inherited;
334 end;
337 function TSimpleList.getCapacity (): Integer; inline;
338 begin
339 result := Length(mItems);
340 end;
343 procedure TSimpleList.setCapacity (v: Integer); inline;
344 begin
345 if (v < mCount) then v := mCount;
346 if (v <> Length(mItems)) then SetLength(mItems, v);
347 end;
350 function TSimpleList.GetEnumerator (): TEnumerator;
351 begin
352 if (Length(mItems) > 0) then result := TEnumerator.Create(mItems, mCount)
353 else result := TEnumerator.Create(nil, -1);
354 end;
357 procedure TSimpleList.reset (); inline;
358 begin
359 mCount := 0;
360 end;
363 procedure TSimpleList.clear (); inline;
364 begin
365 mItems := nil;
366 mCount := 0;
367 end;
370 function TSimpleList.getAt (idx: Integer): ItemT; inline;
371 begin
372 if (idx >= 0) and (idx < mCount) then result := mItems[idx] else result := Default(ItemT);
373 end;
376 procedure TSimpleList.setAt (idx: Integer; const it: ItemT); inline;
377 begin
378 if (idx >= 0) and (idx < mCount) then mItems[idx] := it;
379 end;
382 procedure TSimpleList.append (constref it: ItemT); inline;
383 var
384 newsz: Integer;
385 begin
386 if (mCount >= Length(mItems)) then
387 begin
388 newsz := mCount+(mCount div 3)+128;
389 SetLength(mItems, newsz);
390 end;
391 mItems[mCount] := it;
392 Inc(mCount);
393 end;
396 procedure TSimpleList.delete (idx: Integer); inline;
397 var
398 f: Integer;
399 begin
400 if (idx >= 0) and (idx < mCount) then
401 begin
402 for f := idx+1 to mCount-1 do mItems[f-1] := mItems[f];
403 end;
404 end;
407 function TSimpleList.remove (idx: Integer): ItemT; inline;
408 var
409 f: Integer;
410 begin
411 if (idx >= 0) and (idx < mCount) then
412 begin
413 result := mItems[idx];
414 for f := idx+1 to mCount-1 do mItems[f-1] := mItems[f];
415 end
416 else
417 begin
418 result := Default(ItemT);
419 end;
420 end;
423 // ////////////////////////////////////////////////////////////////////////// //
424 var
425 wc2shitmap: array[0..65535] of AnsiChar;
426 wc2shitmapInited: Boolean = false;
429 // ////////////////////////////////////////////////////////////////////////// //
430 const
431 cp1251: array[0..127] of Word = (
432 $0402,$0403,$201A,$0453,$201E,$2026,$2020,$2021,$20AC,$2030,$0409,$2039,$040A,$040C,$040B,$040F,
433 $0452,$2018,$2019,$201C,$201D,$2022,$2013,$2014,$003F,$2122,$0459,$203A,$045A,$045C,$045B,$045F,
434 $00A0,$040E,$045E,$0408,$00A4,$0490,$00A6,$00A7,$0401,$00A9,$0404,$00AB,$00AC,$00AD,$00AE,$0407,
435 $00B0,$00B1,$0406,$0456,$0491,$00B5,$00B6,$00B7,$0451,$2116,$0454,$00BB,$0458,$0405,$0455,$0457,
436 $0410,$0411,$0412,$0413,$0414,$0415,$0416,$0417,$0418,$0419,$041A,$041B,$041C,$041D,$041E,$041F,
437 $0420,$0421,$0422,$0423,$0424,$0425,$0426,$0427,$0428,$0429,$042A,$042B,$042C,$042D,$042E,$042F,
438 $0430,$0431,$0432,$0433,$0434,$0435,$0436,$0437,$0438,$0439,$043A,$043B,$043C,$043D,$043E,$043F,
439 $0440,$0441,$0442,$0443,$0444,$0445,$0446,$0447,$0448,$0449,$044A,$044B,$044C,$044D,$044E,$044F
440 );
443 procedure initShitMap ();
444 var
445 f: Integer;
446 begin
447 for f := 0 to High(wc2shitmap) do wc2shitmap[f] := '?';
448 for f := 0 to 127 do wc2shitmap[f] := AnsiChar(f);
449 for f := 0 to 127 do wc2shitmap[cp1251[f]] := AnsiChar(f+128);
450 wc2shitmapInited := true;
451 end;
454 // ////////////////////////////////////////////////////////////////////////// //
455 // fast state-machine based UTF-8 decoder; using 8 bytes of memory
456 // code points from invalid range will never be valid, this is the property of the state machine
457 const
458 // see http://bjoern.hoehrmann.de/utf-8/decoder/dfa/
459 utf8dfa: array[0..$16c-1] of Byte = (
460 // maps bytes to character classes
461 $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00, // 00-0f
462 $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00, // 10-1f
463 $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00, // 20-2f
464 $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00, // 30-3f
465 $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00, // 40-4f
466 $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00, // 50-5f
467 $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00, // 60-6f
468 $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00, // 70-7f
469 $01,$01,$01,$01,$01,$01,$01,$01,$01,$01,$01,$01,$01,$01,$01,$01, // 80-8f
470 $09,$09,$09,$09,$09,$09,$09,$09,$09,$09,$09,$09,$09,$09,$09,$09, // 90-9f
471 $07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07, // a0-af
472 $07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07, // b0-bf
473 $08,$08,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02, // c0-cf
474 $02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02, // d0-df
475 $0a,$03,$03,$03,$03,$03,$03,$03,$03,$03,$03,$03,$03,$04,$03,$03, // e0-ef
476 $0b,$06,$06,$06,$05,$08,$08,$08,$08,$08,$08,$08,$08,$08,$08,$08, // f0-ff
477 // maps a combination of a state of the automaton and a character class to a state
478 $00,$0c,$18,$24,$3c,$60,$54,$0c,$0c,$0c,$30,$48,$0c,$0c,$0c,$0c, // 100-10f
479 $0c,$0c,$0c,$0c,$0c,$0c,$0c,$0c,$0c,$00,$0c,$0c,$0c,$0c,$0c,$00, // 110-11f
480 $0c,$00,$0c,$0c,$0c,$18,$0c,$0c,$0c,$0c,$0c,$18,$0c,$18,$0c,$0c, // 120-12f
481 $0c,$0c,$0c,$0c,$0c,$0c,$0c,$18,$0c,$0c,$0c,$0c,$0c,$18,$0c,$0c, // 130-13f
482 $0c,$0c,$0c,$0c,$0c,$18,$0c,$0c,$0c,$0c,$0c,$0c,$0c,$0c,$0c,$24, // 140-14f
483 $0c,$24,$0c,$0c,$0c,$24,$0c,$0c,$0c,$0c,$0c,$24,$0c,$24,$0c,$0c, // 150-15f
484 $0c,$24,$0c,$0c,$0c,$0c,$0c,$0c,$0c,$0c,$0c,$0c);
487 // ////////////////////////////////////////////////////////////////////////// //
488 constructor TUtf8DecoderFast.Create (v: Boolean{fuck you, fpc}); begin state := Accept; codepoint := 0; end;
490 procedure TUtf8DecoderFast.reset (); inline; begin state := Accept; codepoint := 0; end;
492 function TUtf8DecoderFast.complete (): Boolean; inline; begin result := (state = Accept); end;
493 function TUtf8DecoderFast.invalid (): Boolean; inline; begin result := (state = Reject); end;
494 function TUtf8DecoderFast.completeOrInvalid (): Boolean; inline; begin result := (state = Accept) or (state = Reject); end;
496 function TUtf8DecoderFast.decode (c: AnsiChar): Boolean; inline; overload; begin result := decode(Byte(c)); end;
498 function TUtf8DecoderFast.decode (b: Byte): Boolean; inline; overload;
499 var
500 tp: LongWord;
501 begin
502 if (state = Reject) then begin state := Accept; codepoint := 0; end;
503 tp := utf8dfa[b];
504 if (state <> Accept) then codepoint := (b and $3f) or (codepoint shl 6) else codepoint := ($ff shr tp) and b;
505 state := utf8dfa[256+state+tp];
506 if (state = Reject) then begin codepoint := Replacement; state := Accept; end;
507 result := (state = Accept);
508 end;
511 // ////////////////////////////////////////////////////////////////////////// //
512 function wchar2win (wc: WideChar): AnsiChar; inline;
513 begin
514 if not wc2shitmapInited then initShitMap();
515 if (LongWord(wc) > 65535) then result := '?' else result := wc2shitmap[LongWord(wc)];
516 end;
519 // ////////////////////////////////////////////////////////////////////////// //
520 function utf2win (const s: AnsiString): AnsiString;
521 var
522 f, c: Integer;
523 ud: TUtf8DecoderFast;
524 begin
525 for f := 1 to Length(s) do
526 begin
527 if (Byte(s[f]) > 127) then
528 begin
529 ud := TUtf8DecoderFast.Create(true);
530 result := '';
531 for c := 1 to Length(s) do
532 begin
533 if ud.decode(s[c]) then result += wchar2win(WideChar(ud.codepoint));
534 end;
535 exit;
536 end;
537 end;
538 result := s;
539 end;
542 function win2utf (const s: AnsiString): AnsiString;
543 var
544 f, c: Integer;
546 function utf8Encode (code: Integer): AnsiString;
547 begin
548 if (code < 0) or (code > $10FFFF) then begin result := '?'; exit; end;
549 if (code <= $7f) then
550 begin
551 result := AnsiChar(code and $ff);
552 end
553 else if (code <= $7FF) then
554 begin
555 result := AnsiChar($C0 or (code shr 6));
556 result += AnsiChar($80 or (code and $3F));
557 end
558 else if (code <= $FFFF) then
559 begin
560 result := AnsiChar($E0 or (code shr 12));
561 result += AnsiChar($80 or ((code shr 6) and $3F));
562 result += AnsiChar($80 or (code and $3F));
563 end
564 else if (code <= $10FFFF) then
565 begin
566 result := AnsiChar($F0 or (code shr 18));
567 result += AnsiChar($80 or ((code shr 12) and $3F));
568 result += AnsiChar($80 or ((code shr 6) and $3F));
569 result += AnsiChar($80 or (code and $3F));
570 end
571 else
572 begin
573 result := '?';
574 end;
575 end;
577 begin
578 for f := 1 to Length(s) do
579 begin
580 if (Byte(s[f]) > 127) then
581 begin
582 result := '';
583 for c := 1 to Length(s) do
584 begin
585 if (Byte(s[c]) < 128) then
586 begin
587 result += s[c];
588 end
589 else
590 begin
591 result += utf8Encode(cp1251[Byte(s[c])-128])
592 end;
593 end;
594 exit;
595 end;
596 end;
597 result := s;
598 end;
601 // ////////////////////////////////////////////////////////////////////////// //
602 function digitInBase (ch: AnsiChar; base: Integer): Integer;
603 begin
604 result := -1;
605 if (base < 1) or (base > 36) then exit;
606 if (ch < '0') then exit;
607 if (base <= 10) then
608 begin
609 if (Integer(ch) >= 48+base) then exit;
610 result := Integer(ch)-48;
611 end
612 else
613 begin
614 if (ch >= '0') and (ch <= '9') then begin result := Integer(ch)-48; exit; end;
615 if (ch >= 'a') and (ch <= 'z') then Dec(ch, 32); // poor man's tolower()
616 if (ch < 'A') or (Integer(ch) >= 65+(base-10)) then exit;
617 result := Integer(ch)-65+10;
618 end;
619 end;
622 // ////////////////////////////////////////////////////////////////////////// //
623 function quoteStr (const s: AnsiString): AnsiString;
625 function squote (const s: AnsiString): AnsiString;
626 var
627 f: Integer;
628 begin
629 result := '''';
630 for f := 1 to Length(s) do
631 begin
632 if (s[f] = '''') then result += '''';
633 result += s[f];
634 end;
635 result += '''';
636 end;
638 function dquote (const s: AnsiString): AnsiString;
639 var
640 f: Integer;
641 ch: AnsiChar;
642 begin
643 result := '"';
644 for f := 1 to Length(s) do
645 begin
646 ch := s[f];
647 if (ch = #0) then result += '\z'
648 else if (ch = #9) then result += '\t'
649 else if (ch = #10) then result += '\n'
650 else if (ch = #13) then result += '\r'
651 else if (ch = #27) then result += '\e'
652 else if (ch < ' ') or (ch = #127) then
653 begin
654 result += '\x';
655 result += LowerCase(IntToHex(Integer(ch), 2));
656 end
657 else if (ch = '"') or (ch = '\') then
658 begin
659 result += '\';
660 result += ch;
661 end
662 else
663 begin
664 result += ch;
665 end;
666 end;
667 result += '"';
668 end;
670 var
671 needSingle: Boolean = false;
672 f: Integer;
673 begin
674 for f := 1 to Length(s) do
675 begin
676 if (s[f] = '''') then begin needSingle := true; continue; end;
677 if (s[f] < ' ') or (s[f] = #127) then begin result := dquote(s); exit; end;
678 end;
679 if needSingle then result := squote(s) else result := ''''+s+'''';
680 end;
683 // ////////////////////////////////////////////////////////////////////////// //
684 function getFilenameExt (const fn: AnsiString): AnsiString;
685 var
686 pos: Integer;
687 ch: AnsiChar;
688 begin
689 pos := Length(fn);
690 while (pos > 0) do
691 begin
692 ch := fn[pos];
693 if (ch = '.') then
694 begin
695 if (pos = Length(fn)) then result := '' else result := Copy(fn, pos, Length(fn)-pos+1);
696 exit;
697 end;
698 if (ch = '/') or (ch = '\') then break;
699 Dec(pos);
700 end;
701 result := ''; // no extension
702 end;
705 function setFilenameExt (const fn, ext: AnsiString): AnsiString;
706 var
707 pos: Integer;
708 ch: AnsiChar;
709 begin
710 result := fn;
711 if (Length(ext) = 0) or (ext = '.') then exit;
712 pos := Length(fn);
713 while (pos > 0) do
714 begin
715 ch := fn[pos];
716 if (ch = '.') then exit;
717 if (ch = '/') or (ch = '\') then break;
718 Dec(pos);
719 end;
720 if (ext[1] <> '.') then result += '.';
721 result += ext;
722 end;
725 function forceFilenameExt (const fn, ext: AnsiString): AnsiString;
726 var
727 pos: Integer;
728 ch: AnsiChar;
729 begin
730 result := fn;
731 pos := Length(fn);
732 while (pos > 0) do
733 begin
734 ch := fn[pos];
735 if (ch = '.') then
736 begin
737 if (Length(ext) = 0) or (ext = '.') then
738 begin
739 result := Copy(fn, 1, pos-1);
740 end
741 else
742 begin
743 if (ext[1] = '.') then result := Copy(fn, 1, pos-1) else result := Copy(fn, 1, pos);
744 result += ext;
745 exit;
746 end;
747 end;
748 if (ch = '/') or (ch = '\') then break;
749 Dec(pos);
750 end;
751 if (Length(ext) > 0) then
752 begin
753 if (ext[1] <> '.') then result += '.';
754 result += ext;
755 end;
756 end;
759 // strips out name from `fn`, leaving trailing slash
760 function getFilenamePath (const fn: AnsiString): AnsiString;
761 var
762 pos: Integer;
763 ch: AnsiChar;
764 begin
765 if (Length(fn) = 0) then begin result := './'; exit; end;
766 if (fn[Length(fn)] = '/') or (fn[Length(fn)] = '\') then begin result := fn; exit; end;
767 pos := Length(fn);
768 while (pos > 0) do
769 begin
770 ch := fn[pos];
771 if (ch = '/') or (ch = '\') then begin result := Copy(fn, 1, pos); exit; end;
772 Dec(pos);
773 end;
774 result := './'; // no path -> current dir
775 end;
778 // ends with '/' or '\'?
779 function isFilenamePath (const fn: AnsiString): Boolean;
780 begin
781 if (Length(fn) = 0) then
782 begin
783 result := false;
784 end
785 else
786 begin
787 result := (fn[Length(fn)] = '/') or (fn[Length(fn)] = '\');
788 end;
789 end;
792 // strips extra trailing slashes in `path, and extra leading slashes in `fn`
793 // will add slash to `path`, even if `fn` is empty!
794 function filenameConcat (const path, fn: AnsiString): AnsiString;
795 var
796 pos: Integer;
797 begin
798 pos := 1;
799 while (pos <= Length(fn)) and ((fn[pos] = '/') or (fn[pos] = '\')) do Inc(pos);
800 result := path;
801 if (Length(result) > 0) and ((result[Length(result)] <> '/') and (result[Length(result)] <> '\')) then result += '/';
802 if (pos <= Length(fn)) then
803 begin
804 result += Copy(fn, pos, Length(fn)-pos+1);
805 //FIXME: make this faster!
806 while (Length(result) > 0) and ((result[Length(result)] = '/') or (result[Length(result)] = '\')) do
807 begin
808 Delete(result, Length(result), 1);
809 end;
810 if (fn[Length(fn)] = '/') or (fn[Length(fn)] = '\') then result += '/';
811 end;
812 end;
815 function hasWadExtension (const fn: AnsiString): Boolean;
816 var
817 ext: AnsiString;
818 begin
819 ext := getFilenameExt(fn);
820 result := StrEquCI1251(ext, '.wad') or StrEquCI1251(ext, '.pk3') or StrEquCI1251(ext, '.zip') or StrEquCI1251(ext, '.dfz');
821 end;
824 function addWadExtension (const fn: AnsiString): AnsiString;
825 begin
826 result := fn;
827 if not hasWadExtension(result) then result := result+'.wad';
828 end;
830 function isWadData (data: Pointer; len: LongWord): Boolean;
831 var p: PChar;
832 begin
833 p := PChar(data);
834 Result :=
835 (* ZIP *)
836 ((len > 3) and (p[0] = 'P') and (p[1] = 'K') and (p[2] = #03) and (p[3] = #04)) or
837 ((len > 3) and (p[0] = 'P') and (p[1] = 'K') and (p[2] = #05) and (p[3] = #06)) or
838 (* PACK *)
839 ((len > 3) and (p[0] = 'P') and (p[1] = 'A') and (p[2] = 'C') and (p[3] = 'K')) or
840 ((len > 3) and (p[0] = 'S') and (p[1] = 'P') and (p[2] = 'A') and (p[3] = 'K')) or
841 (* DFWAD *)
842 ((len > 5) and (p[0] = 'D') and (p[1] = 'F') and (p[2] = 'W') and (p[3] = 'A') and (p[4] = 'D') and (p[5] = #01))
843 end;
846 function isWadPath (const fn: AnsiString): Boolean;
847 var
848 pos: Integer;
849 s: AnsiString;
850 begin
851 result := false;
852 pos := 1;
853 while (pos <= Length(fn)) do
854 begin
855 if (fn[pos] = ':') then
856 begin
857 if (Length(fn)-pos < 1) then break;
858 if (pos-4 > 1) and (fn[pos-4] = '.') and ((fn[pos+1] = '\') or (fn[pos+1] = '/')) then
859 begin
860 s := Copy(fn, pos-4, 4);
861 if StrEquCI1251(s, '.wad') or StrEquCI1251(s, '.pk3') or StrEquCI1251(s, '.zip') or StrEquCI1251(s, '.dfz') then
862 begin
863 result := true;
864 exit;
865 end;
866 end;
867 end;
868 Inc(pos);
869 end;
870 end;
873 function int64ToStrComma (i: Int64): AnsiString;
874 var
875 f: Integer;
876 begin
877 Str(i, result);
878 f := Length(result)+1;
879 while f > 4 do
880 begin
881 Dec(f, 3); Insert(',', result, f);
882 end;
883 end;
886 function upcase1251 (ch: AnsiChar): AnsiChar; inline;
887 begin
888 if ch < #128 then
889 begin
890 if (ch >= 'a') and (ch <= 'z') then Dec(ch, 32);
891 end
892 else
893 begin
894 if (ch >= #224) and (ch <= #255) then
895 begin
896 Dec(ch, 32);
897 end
898 else
899 begin
900 case ch of
901 #184, #186, #191: Dec(ch, 16);
902 #162, #179: Dec(ch);
903 end;
904 end;
905 end;
906 result := ch;
907 end;
910 function locase1251 (ch: AnsiChar): AnsiChar; inline;
911 begin
912 if ch < #128 then
913 begin
914 if (ch >= 'A') and (ch <= 'Z') then Inc(ch, 32);
915 end
916 else
917 begin
918 if (ch >= #192) and (ch <= #223) then
919 begin
920 Inc(ch, 32);
921 end
922 else
923 begin
924 case ch of
925 #168, #170, #175: Inc(ch, 16);
926 #161, #178: Inc(ch);
927 end;
928 end;
929 end;
930 result := ch;
931 end;
934 function strEquCI1251 (const s0, s1: AnsiString): Boolean;
935 var
936 i: Integer;
937 begin
938 result := false;
939 if length(s0) <> length(s1) then exit;
940 for i := 1 to length(s0) do if UpCase1251(s0[i]) <> UpCase1251(s1[i]) then exit;
941 result := true;
942 end;
945 function toLowerCase1251 (const s: AnsiString): AnsiString;
946 var
947 f: Integer;
948 ch: AnsiChar;
949 begin
950 for ch in s do
951 begin
952 if (ch <> LoCase1251(ch)) then
953 begin
954 result := '';
955 SetLength(result, Length(s));
956 for f := 1 to Length(s) do result[f] := LoCase1251(s[f]);
957 exit;
958 end;
959 end;
960 // nothing to do
961 result := s;
962 end;
965 // ////////////////////////////////////////////////////////////////////////// //
966 // utils
967 // `ch`: utf8 start
968 // -1: invalid utf8
969 function utf8CodeLen (ch: Word): Integer;
970 begin
971 if ch < $80 then begin result := 1; exit; end;
972 if (ch and $FE) = $FC then begin result := 6; exit; end;
973 if (ch and $FC) = $F8 then begin result := 5; exit; end;
974 if (ch and $F8) = $F0 then begin result := 4; exit; end;
975 if (ch and $F0) = $E0 then begin result := 3; exit; end;
976 if (ch and $E0) = $C0 then begin result := 2; exit; end;
977 result := -1; // invalid
978 end;
981 function utf8Valid (const s: AnsiString): Boolean;
982 var
983 pos, len: Integer;
984 begin
985 result := false;
986 pos := 1;
987 while pos <= length(s) do
988 begin
989 len := utf8CodeLen(Byte(s[pos]));
990 if len < 1 then exit; // invalid sequence start
991 if pos+len-1 > length(s) then exit; // out of chars in string
992 Dec(len);
993 Inc(pos);
994 // check other sequence bytes
995 while len > 0 do
996 begin
997 if (Byte(s[pos]) and $C0) <> $80 then exit;
998 Dec(len);
999 Inc(pos);
1000 end;
1001 end;
1002 result := true;
1003 end;
1006 // ////////////////////////////////////////////////////////////////////////// //
1007 const
1008 uni2wint: array [128..255] of Word = (
1009 $0402,$0403,$201A,$0453,$201E,$2026,$2020,$2021,$20AC,$2030,$0409,$2039,$040A,$040C,$040B,$040F,
1010 $0452,$2018,$2019,$201C,$201D,$2022,$2013,$2014,$003F,$2122,$0459,$203A,$045A,$045C,$045B,$045F,
1011 $00A0,$040E,$045E,$0408,$00A4,$0490,$00A6,$00A7,$0401,$00A9,$0404,$00AB,$00AC,$00AD,$00AE,$0407,
1012 $00B0,$00B1,$0406,$0456,$0491,$00B5,$00B6,$00B7,$0451,$2116,$0454,$00BB,$0458,$0405,$0455,$0457,
1013 $0410,$0411,$0412,$0413,$0414,$0415,$0416,$0417,$0418,$0419,$041A,$041B,$041C,$041D,$041E,$041F,
1014 $0420,$0421,$0422,$0423,$0424,$0425,$0426,$0427,$0428,$0429,$042A,$042B,$042C,$042D,$042E,$042F,
1015 $0430,$0431,$0432,$0433,$0434,$0435,$0436,$0437,$0438,$0439,$043A,$043B,$043C,$043D,$043E,$043F,
1016 $0440,$0441,$0442,$0443,$0444,$0445,$0446,$0447,$0448,$0449,$044A,$044B,$044C,$044D,$044E,$044F
1017 );
1020 function decodeUtf8Char (s: AnsiString; var pos: Integer): AnsiChar;
1021 var
1022 b, c: Integer;
1023 begin
1024 (* The following encodings are valid, except for the 5 and 6 byte
1025 * combinations:
1026 * 0xxxxxxx
1027 * 110xxxxx 10xxxxxx
1028 * 1110xxxx 10xxxxxx 10xxxxxx
1029 * 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
1030 * 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
1031 * 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
1032 *)
1033 result := '?';
1034 if pos > length(s) then exit;
1036 b := Byte(s[pos]);
1037 Inc(pos);
1038 if b < $80 then begin result := AnsiChar(b); exit; end;
1040 // mask out unused bits
1041 if (b and $FE) = $FC then b := b and $01
1042 else if (b and $FC) = $F8 then b := b and $03
1043 else if (b and $F8) = $F0 then b := b and $07
1044 else if (b and $F0) = $E0 then b := b and $0F
1045 else if (b and $E0) = $C0 then b := b and $1F
1046 else exit; // invalid utf8
1048 // now continue
1049 while pos <= length(s) do
1050 begin
1051 c := Byte(s[pos]);
1052 if (c and $C0) <> $80 then break; // no more
1053 b := b shl 6;
1054 b := b or (c and $3F);
1055 Inc(pos);
1056 end;
1058 // done, try 1251
1059 for c := 128 to 255 do if uni2wint[c] = b then begin result := AnsiChar(c and $FF); exit; end;
1060 // alas
1061 end;
1064 function utf8to1251 (s: AnsiString): AnsiString;
1065 var
1066 pos: Integer;
1067 begin
1068 if not utf8Valid(s) then begin result := s; exit; end;
1069 pos := 1;
1070 while pos <= length(s) do
1071 begin
1072 if Byte(s[pos]) >= $80 then break;
1073 Inc(pos);
1074 end;
1075 if pos > length(s) then begin result := s; exit; end; // nothing to do here
1076 result := '';
1077 pos := 1;
1078 while pos <= length(s) do result := result+decodeUtf8Char(s, pos);
1079 end;
1082 // ////////////////////////////////////////////////////////////////////////// //
1083 // `pathname` will be modified if path is valid
1084 // `lastIsDir` should be `true` if we are searching for directory
1085 // nobody cares about shitdoze, so i'll use the same code path for it
1086 function findFileCI (var pathname: AnsiString; lastIsDir: Boolean=false): Boolean;
1087 var
1088 sr: TSearchRec;
1089 npt: AnsiString;
1090 newname: AnsiString = '';
1091 curname: AnsiString;
1092 wantdir: Boolean;
1093 attr: LongInt;
1094 foundher: Boolean;
1095 begin
1096 npt := pathname;
1097 result := (length(npt) > 0);
1098 if (length(npt) > 0) and ((npt[1] = '/') or (npt[1] = '\')) then newname := '/';
1099 while length(npt) > 0 do
1100 begin
1101 // remove trailing slashes
1102 while (length(npt) > 0) and ((npt[1] = '/') or (npt[1] = '\')) do Delete(npt, 1, 1);
1103 if length(npt) = 0 then break;
1104 // extract name
1105 curname := '';
1106 while (length(npt) > 0) and (npt[1] <> '/') and (npt[1] <> '\') do
1107 begin
1108 curname := curname+npt[1];
1109 Delete(npt, 1, 1);
1110 end;
1111 // remove trailing slashes again
1112 while (length(npt) > 0) and ((npt[1] = '/') or (npt[1] = '\')) do Delete(npt, 1, 1);
1113 wantdir := lastIsDir or (length(npt) > 0); // do we want directory here?
1114 //writeln(Format('npt=[%s]; newname=[%s]; curname=[%s]; wantdir=%d', [npt, newname, curname, Integer(wantdir)]));
1115 // try the easiest case first
1116 attr := FileGetAttr(newname+curname);
1117 if attr <> -1 then
1118 begin
1119 if wantdir = ((attr and faDirectory) <> 0) then
1120 begin
1121 // i found her!
1122 newname := newname+curname;
1123 if wantdir then newname := newname+'/';
1124 continue;
1125 end;
1126 end;
1127 //writeln(Format('npt=[%s]; newname=[%s]; curname=[%s]; wantdir=%d', [npt, newname, curname, Integer(wantdir)]));
1128 // alas, either not found, or invalid attributes
1129 foundher := false;
1130 try
1131 if FindFirst(newname+'*', faAnyFile, sr) = 0 then
1132 repeat
1133 if (wantdir = ((sr.attr and faDirectory) <> 0)) and StrEquCI1251(sr.name, curname) then
1134 begin
1135 // i found her!
1136 newname := newname+sr.name;
1137 if wantdir then newname := newname+'/';
1138 foundher := true;
1139 break;
1140 end;
1141 until FindNext(sr) <> 0;
1142 finally
1143 FindClose(sr);
1144 end;
1145 if not foundher then begin newname := ''; result := false; break; end;
1146 end;
1147 if result then pathname := newname;
1148 end;
1151 const fileExtensions: array [0..6] of AnsiString = ('.dfz', '.wad', '.dfwad', '.pk3', '.pak', '.zip', '.dfzip');
1153 function isWadNamesEqu (wna, wnb: AnsiString): Boolean;
1154 var
1155 ext, newExt: AnsiString;
1156 found: Boolean;
1157 begin
1158 result := StrEquCI1251(wna, wnb);
1159 if result then exit;
1160 // check first ext
1161 ext := getFilenameExt(wna);
1162 found := false;
1163 for newExt in fileExtensions do if (StrEquCI1251(ext, newExt)) then begin found := true; break; end;
1164 if not found then exit;
1165 // check second ext
1166 ext := getFilenameExt(wnb);
1167 found := false;
1168 for newExt in fileExtensions do if (StrEquCI1251(ext, newExt)) then begin found := true; break; end;
1169 if not found then exit;
1170 wna := forceFilenameExt(wna, '');
1171 wnb := forceFilenameExt(wnb, '');
1172 result := StrEquCI1251(wna, wnb);
1173 end;
1175 function findDiskWad (fname: AnsiString): AnsiString;
1176 var
1177 origExt: AnsiString = '';
1178 newExt: AnsiString = '';
1179 begin
1180 result := '';
1181 //writeln('findDiskWad00: fname=<', fname, '>');
1182 if (findFileCI(fname)) then begin result := fname; exit; end;
1183 origExt := getFilenameExt(fname);
1184 fname := forceFilenameExt(fname, '');
1185 //writeln(' findDiskWad01: fname=<', fname, '>; origExt=<', origExt, '>');
1186 for newExt in fileExtensions do
1187 begin
1188 //writeln(' findDiskWad02: fname=<', fname, '>; origExt=<', origExt, '>; newExt=<', newExt, '>');
1189 if (StrEquCI1251(newExt, origExt)) then
1190 begin
1191 //writeln(' SKIP');
1192 continue;
1193 end;
1194 result := fname+newExt;
1195 if (findFileCI(result)) then exit;
1196 end;
1197 result := '';
1198 end;
1201 function openDiskFileRO (pathname: AnsiString): TStream;
1202 begin
1203 if not findFileCI(pathname) then raise EFileNotFoundException.Create('can''t open file "'+pathname+'"');
1204 result := TFileStream.Create(pathname, fmOpenRead or {fmShareDenyWrite}fmShareDenyNone);
1205 end;
1207 function createDiskFile (pathname: AnsiString): TStream;
1208 var
1209 path: AnsiString;
1210 begin
1211 path := ExtractFilePath(pathname);
1212 if length(path) > 0 then
1213 begin
1214 if not findFileCI(path, true) then raise Exception.Create('can''t create file "'+pathname+'"');
1215 end;
1216 result := TFileStream.Create(path+ExtractFileName(pathname), fmCreate);
1217 end;
1220 procedure writeIntegerLE (st: TStream; vp: Pointer; size: Integer);
1221 {$IFDEF ENDIAN_LITTLE}
1222 begin
1223 st.writeBuffer(vp^, size);
1224 end;
1225 {$ELSE}
1226 var
1227 p: PByte;
1228 begin
1229 p := PByte(vp)+size-1;
1230 while size > 0 do
1231 begin
1232 st.writeBuffer(p^, 1);
1233 Dec(size);
1234 Dec(p);
1235 end;
1236 end;
1237 {$ENDIF}
1239 procedure writeIntegerBE (st: TStream; vp: Pointer; size: Integer);
1240 {$IFDEF ENDIAN_LITTLE}
1241 var
1242 p: PByte;
1243 begin
1244 p := PByte(vp)+size-1;
1245 while size > 0 do
1246 begin
1247 st.writeBuffer(p^, 1);
1248 Dec(size);
1249 Dec(p);
1250 end;
1251 end;
1252 {$ELSE}
1253 begin
1254 st.writeBuffer(vp^, size);
1255 end;
1256 {$ENDIF}
1258 procedure writeSign (st: TStream; const sign: AnsiString);
1259 begin
1260 if (Length(sign) > 0) then st.WriteBuffer(sign[1], Length(sign));
1261 end;
1263 function checkSign (st: TStream; const sign: AnsiString): Boolean;
1264 var
1265 buf: packed array[0..7] of AnsiChar;
1266 f: Integer;
1267 begin
1268 result := false;
1269 if (Length(sign) > 0) then
1270 begin
1271 if (Length(sign) <= 8) then
1272 begin
1273 st.ReadBuffer(buf[0], Length(sign));
1274 for f := 1 to Length(sign) do if (buf[f-1] <> sign[f]) then exit;
1275 end
1276 else
1277 begin
1278 for f := 1 to Length(sign) do
1279 begin
1280 st.ReadBuffer(buf[0], 1);
1281 if (buf[0] <> sign[f]) then exit;
1282 end;
1283 end;
1284 end;
1285 result := true;
1286 end;
1288 procedure writeInt (st: TStream; v: Byte); overload; begin writeIntegerLE(st, @v, 1); end;
1289 procedure writeInt (st: TStream; v: ShortInt); overload; begin writeIntegerLE(st, @v, 1); end;
1290 procedure writeInt (st: TStream; v: Word); overload; begin writeIntegerLE(st, @v, 2); end;
1291 procedure writeInt (st: TStream; v: SmallInt); overload; begin writeIntegerLE(st, @v, 2); end;
1292 procedure writeInt (st: TStream; v: LongWord); overload; begin writeIntegerLE(st, @v, 4); end;
1293 procedure writeInt (st: TStream; v: LongInt); overload; begin writeIntegerLE(st, @v, 4); end;
1294 procedure writeInt (st: TStream; v: Int64); overload; begin writeIntegerLE(st, @v, 8); end;
1295 procedure writeInt (st: TStream; v: UInt64); overload; begin writeIntegerLE(st, @v, 8); end;
1297 procedure writeIntBE (st: TStream; v: Byte); overload; begin writeIntegerBE(st, @v, 1); end;
1298 procedure writeIntBE (st: TStream; v: ShortInt); overload; begin writeIntegerBE(st, @v, 1); end;
1299 procedure writeIntBE (st: TStream; v: Word); overload; begin writeIntegerBE(st, @v, 2); end;
1300 procedure writeIntBE (st: TStream; v: SmallInt); overload; begin writeIntegerBE(st, @v, 2); end;
1301 procedure writeIntBE (st: TStream; v: LongWord); overload; begin writeIntegerBE(st, @v, 4); end;
1302 procedure writeIntBE (st: TStream; v: LongInt); overload; begin writeIntegerBE(st, @v, 4); end;
1303 procedure writeIntBE (st: TStream; v: Int64); overload; begin writeIntegerBE(st, @v, 8); end;
1304 procedure writeIntBE (st: TStream; v: UInt64); overload; begin writeIntegerBE(st, @v, 8); end;
1306 procedure writeBool (st: TStream; b: Boolean); begin writeInt(st, Byte(b)); end;
1307 function readBool (st: TStream): Boolean; begin result := (readByte(st) <> 0); end;
1310 procedure writeStr (st: TStream; const str: AnsiString; maxlen: LongWord=65535);
1311 begin
1312 if (Length(str) > maxlen) then raise XStreamError.Create('string too long');
1313 if (maxlen <= 65535) then writeInt(st, Word(Length(str))) else writeInt(st, LongWord(Length(str)));
1314 if (Length(str) > 0) then st.WriteBuffer(str[1], Length(str));
1315 end;
1317 function readStr (st: TStream; maxlen: LongWord=65535): AnsiString;
1318 var
1319 len: Integer;
1320 begin
1321 result := '';
1322 if (maxlen <= 65535) then len := readWord(st) else len := Integer(readLongWord(st));
1323 if (len < 0) or (len > maxlen) then raise XStreamError.Create('string too long');
1324 if (len > 0) then
1325 begin
1326 SetLength(result, len);
1327 st.ReadBuffer(result[1], len);
1328 end;
1329 end;
1332 procedure readIntegerLE (st: TStream; vp: Pointer; size: Integer);
1333 {$IFDEF ENDIAN_LITTLE}
1334 begin
1335 st.readBuffer(vp^, size);
1336 end;
1337 {$ELSE}
1338 var
1339 p: PByte;
1340 begin
1341 p := PByte(vp)+size-1;
1342 while size > 0 do
1343 begin
1344 st.readBuffer(p^, 1);
1345 Dec(size);
1346 Dec(p);
1347 end;
1348 end;
1349 {$ENDIF}
1351 procedure readIntegerBE (st: TStream; vp: Pointer; size: Integer);
1352 {$IFDEF ENDIAN_LITTLE}
1353 var
1354 p: PByte;
1355 begin
1356 p := PByte(vp)+size-1;
1357 while size > 0 do
1358 begin
1359 st.readBuffer(p^, 1);
1360 Dec(size);
1361 Dec(p);
1362 end;
1363 end;
1364 {$ELSE}
1365 begin
1366 st.readBuffer(vp^, size);
1367 end;
1368 {$ENDIF}
1370 function readByte (st: TStream): Byte; begin readIntegerLE(st, @result, 1); end;
1371 function readShortInt (st: TStream): ShortInt; begin readIntegerLE(st, @result, 1); end;
1372 function readWord (st: TStream): Word; begin readIntegerLE(st, @result, 2); end;
1373 function readSmallInt (st: TStream): SmallInt; begin readIntegerLE(st, @result, 2); end;
1374 function readLongWord (st: TStream): LongWord; begin readIntegerLE(st, @result, 4); end;
1375 function readLongInt (st: TStream): LongInt; begin readIntegerLE(st, @result, 4); end;
1376 function readInt64 (st: TStream): Int64; begin readIntegerLE(st, @result, 8); end;
1377 function readUInt64 (st: TStream): UInt64; begin readIntegerLE(st, @result, 8); end;
1379 function readByteBE (st: TStream): Byte; begin readIntegerBE(st, @result, 1); end;
1380 function readShortIntBE (st: TStream): ShortInt; begin readIntegerBE(st, @result, 1); end;
1381 function readWordBE (st: TStream): Word; begin readIntegerBE(st, @result, 2); end;
1382 function readSmallIntBE (st: TStream): SmallInt; begin readIntegerBE(st, @result, 2); end;
1383 function readLongWordBE (st: TStream): LongWord; begin readIntegerBE(st, @result, 4); end;
1384 function readLongIntBE (st: TStream): LongInt; begin readIntegerBE(st, @result, 4); end;
1385 function readInt64BE (st: TStream): Int64; begin readIntegerBE(st, @result, 8); end;
1386 function readUInt64BE (st: TStream): UInt64; begin readIntegerBE(st, @result, 8); end;
1389 // ////////////////////////////////////////////////////////////////////////// //
1390 function nmin (a, b: Byte): Byte; inline; overload; begin if (a < b) then result := a else result := b; end;
1391 function nmin (a, b: ShortInt): ShortInt; inline; overload; begin if (a < b) then result := a else result := b; end;
1392 function nmin (a, b: Word): Word; inline; overload; begin if (a < b) then result := a else result := b; end;
1393 function nmin (a, b: SmallInt): SmallInt; inline; overload; begin if (a < b) then result := a else result := b; end;
1394 function nmin (a, b: LongWord): LongWord; inline; overload; begin if (a < b) then result := a else result := b; end;
1395 function nmin (a, b: LongInt): LongInt; inline; overload; begin if (a < b) then result := a else result := b; end;
1396 function nmin (a, b: Int64): Int64; inline; overload; begin if (a < b) then result := a else result := b; end;
1397 function nmin (a, b: UInt64): UInt64; inline; overload; begin if (a < b) then result := a else result := b; end;
1398 function nmin (a, b: Single): Single; inline; overload; begin if (a < b) then result := a else result := b; end;
1399 function nmin (a, b: Double): Double; inline; overload; begin if (a < b) then result := a else result := b; end;
1400 {$IF DEFINED(CPU386) OR DEFINED(CPUAMD64)}
1401 function nmin (a, b: Extended): Extended; inline; overload; begin if (a < b) then result := a else result := b; end;
1402 {$ENDIF}
1404 function nmax (a, b: Byte): Byte; inline; overload; begin if (a > b) then result := a else result := b; end;
1405 function nmax (a, b: ShortInt): ShortInt; inline; overload; begin if (a > b) then result := a else result := b; end;
1406 function nmax (a, b: Word): Word; inline; overload; begin if (a > b) then result := a else result := b; end;
1407 function nmax (a, b: SmallInt): SmallInt; inline; overload; begin if (a > b) then result := a else result := b; end;
1408 function nmax (a, b: LongWord): LongWord; inline; overload; begin if (a > b) then result := a else result := b; end;
1409 function nmax (a, b: LongInt): LongInt; inline; overload; begin if (a > b) then result := a else result := b; end;
1410 function nmax (a, b: Int64): Int64; inline; overload; begin if (a > b) then result := a else result := b; end;
1411 function nmax (a, b: UInt64): UInt64; inline; overload; begin if (a > b) then result := a else result := b; end;
1412 function nmax (a, b: Single): Single; inline; overload; begin if (a > b) then result := a else result := b; end;
1413 function nmax (a, b: Double): Double; inline; overload; begin if (a > b) then result := a else result := b; end;
1414 {$IF DEFINED(CPU386) OR DEFINED(CPUAMD64)}
1415 function nmax (a, b: Extended): Extended; inline; overload; begin if (a > b) then result := a else result := b; end;
1416 {$ENDIF}
1418 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;
1419 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;
1420 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;
1421 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;
1422 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;
1423 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;
1424 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;
1425 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;
1426 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;
1427 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;
1428 {$IF DEFINED(CPU386) OR DEFINED(CPUAMD64)}
1429 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;
1430 {$ENDIF}
1432 // ////////////////////////////////////////////////////////////////////////// //
1433 {$IFDEF WINDOWS}
1434 function snprintf (buf: PAnsiChar; bufsize: SizeUInt; const fmt: PAnsiChar): SizeUInt; cdecl; varargs; external 'msvcrt.dll' name '_snprintf';
1435 {$ELSE}
1436 function snprintf (buf: PAnsiChar; bufsize: SizeUInt; const fmt: PAnsiChar): SizeUInt; cdecl; varargs; external 'libc' name 'snprintf';
1437 {$ENDIF}
1440 (*
1441 procedure conwriter (constref buf; len: SizeUInt);
1442 var
1443 ss: ShortString;
1444 slen: Integer;
1445 b: PByte;
1446 begin
1447 if (len < 1) then exit;
1448 b := PByte(@buf);
1449 while (len > 0) do
1450 begin
1451 if (len > 255) then slen := 255 else slen := Integer(len);
1452 Move(b^, ss[1], len);
1453 ss[0] := AnsiChar(slen);
1454 write(ss);
1455 b += slen;
1456 len -= slen;
1457 end;
1458 end;
1459 *)
1462 function formatstrf (const fmt: AnsiString; const args: array of const; writerCB: TFormatStrFCallback=nil): AnsiString;
1463 const
1464 PadSpaces: AnsiString = ' ';
1465 PadZeroes: AnsiString = '00000000000000000000000000000000000000000000000000000000000000000000000';
1466 var
1467 curarg: Integer = 0; // current arg in `args`
1468 sign, fmtch: AnsiChar;
1469 zeropad: Boolean;
1470 width, prec: Integer; // width and precision
1471 spos, epos: Integer;
1472 ch: AnsiChar;
1473 strbuf: array[0..256] of AnsiChar;
1474 strblen: SizeUInt;
1475 fmtbuf: array[0..256] of AnsiChar;
1476 fmtblen: Integer;
1477 pclen: Integer;
1478 pc: PAnsiChar;
1479 ccname: ShortString;
1481 procedure writer (constref buf; len: SizeUInt);
1482 var
1483 ss: ShortString;
1484 slen: Integer;
1485 b: PByte;
1486 begin
1487 if (len < 1) then exit;
1488 b := PByte(@buf);
1489 if assigned(writerCB) then
1490 begin
1491 writerCB(b^, len);
1492 end
1493 else
1494 begin
1495 while (len > 0) do
1496 begin
1497 if (len > 255) then slen := 255 else slen := Integer(len);
1498 Move(b^, ss[1], slen);
1499 ss[0] := AnsiChar(slen);
1500 result += ss;
1501 b += slen;
1502 len -= slen;
1503 end;
1504 end;
1505 end;
1507 procedure xwrite (const s: AnsiString);
1508 begin
1509 if (Length(s) > 0) then writer(PAnsiChar(s)^, Length(s));
1510 end;
1512 procedure putFmtChar (ch: AnsiChar);
1513 begin
1514 fmtbuf[fmtblen] := ch;
1515 Inc(fmtblen);
1516 end;
1518 procedure putFmtInt (n: Integer);
1519 var
1520 len: SizeUInt;
1521 begin
1522 len := snprintf(@fmtbuf[fmtblen], Length(fmtbuf)-fmtblen, '%d', n);
1523 if (len > 0) then Inc(fmtblen, len);
1524 end;
1526 procedure buildCFormat (const pfx: AnsiString='');
1527 var
1528 f: Integer;
1529 begin
1530 fmtblen := 0;
1531 for f := 1 to Length(pfx) do putFmtChar(pfx[f]);
1532 putFmtChar('%');
1533 if (sign <> ' ') then putFmtChar(sign);
1534 if (width >= 0) then
1535 begin
1536 if (zeropad) then putFmtChar('0');
1537 putFmtInt(width);
1538 if (prec >= 0) then
1539 begin
1540 putFmtChar('.');
1541 putFmtInt(prec);
1542 end;
1543 end;
1544 putFmtChar(fmtch);
1545 fmtbuf[fmtblen] := #0;
1546 end;
1548 procedure writeStrBuf ();
1549 begin
1550 if (strblen > 0) then writer(strbuf, strblen);
1551 end;
1553 function i642str (n: Int64; hex: Boolean; hexup: Boolean): PAnsiChar;
1554 var
1555 neg: Boolean;
1556 xpos: Integer;
1557 begin
1558 if (n = $8000000000000000) then
1559 begin
1560 if hex then snprintf(@strbuf[0], Length(strbuf), '-8000000000000000')
1561 else snprintf(@strbuf[0], Length(strbuf), '-9223372036854775808');
1562 result := @strbuf[0];
1563 end
1564 else
1565 begin
1566 neg := (n < 0);
1567 if neg then n := -n;
1568 xpos := High(strbuf);
1569 strbuf[xpos] := #0; Dec(xpos);
1570 repeat
1571 if not hex then
1572 begin
1573 strbuf[xpos] := AnsiChar((n mod 10)+48);
1574 Dec(xpos);
1575 n := n div 10;
1576 end
1577 else
1578 begin
1579 if (n mod 16 > 9) then
1580 begin
1581 strbuf[xpos] := AnsiChar((n mod 16)+48+7);
1582 if not hexup then Inc(strbuf[xpos], 32);
1583 end
1584 else strbuf[xpos] := AnsiChar((n mod 16)+48);
1585 Dec(xpos);
1586 n := n div 16;
1587 end;
1588 until (n = 0);
1589 if neg then begin strbuf[xpos] := '-'; Dec(xpos); end;
1590 result := @strbuf[xpos+1];
1591 end;
1592 end;
1594 function ui642str (n: UInt64; hex: Boolean; hexup: Boolean): PAnsiChar;
1595 var
1596 xpos: Integer;
1597 begin
1598 xpos := High(strbuf);
1599 strbuf[xpos] := #0; Dec(xpos);
1600 repeat
1601 if not hex then
1602 begin
1603 strbuf[xpos] := AnsiChar((n mod 10)+48);
1604 Dec(xpos);
1605 n := n div 10;
1606 end
1607 else
1608 begin
1609 if (n mod 16 > 9) then
1610 begin
1611 strbuf[xpos] := AnsiChar((n mod 16)+48+7);
1612 if not hexup then Inc(strbuf[xpos], 32);
1613 end
1614 else strbuf[xpos] := AnsiChar((n mod 16)+48);
1615 Dec(xpos);
1616 n := n div 16;
1617 end;
1618 until (n = 0);
1619 result := @strbuf[xpos+1];
1620 end;
1622 procedure indent (len: Integer);
1623 var
1624 ilen: Integer;
1625 begin
1626 while (len > 0) do
1627 begin
1628 if (len > Length(PadSpaces)) then ilen := Length(PadSpaces) else ilen := len;
1629 writer(PAnsiChar(PadSpaces)^, ilen);
1630 Dec(len, ilen);
1631 end;
1632 end;
1634 procedure indent0 (len: Integer);
1635 var
1636 ilen: Integer;
1637 begin
1638 while (len > 0) do
1639 begin
1640 if (len > Length(PadZeroes)) then ilen := Length(PadZeroes) else ilen := len;
1641 writer(PAnsiChar(PadZeroes)^, ilen);
1642 Dec(len, ilen);
1643 end;
1644 end;
1646 begin
1647 result := '';
1648 spos := 1;
1649 while (spos <= Length(fmt)) do
1650 begin
1651 // print literal part
1652 epos := spos;
1653 while (epos <= Length(fmt)) and (fmt[epos] <> '%') do Inc(epos);
1654 // output literal part
1655 if (epos > spos) then
1656 begin
1657 if (epos > Length(fmt)) then
1658 begin
1659 writer((PAnsiChar(fmt)+spos-1)^, epos-spos);
1660 break;
1661 end;
1662 if (epos+1 > Length(fmt)) then Inc(epos) // last percent, output literally
1663 else if (fmt[epos+1] = '%') then // special case
1664 begin
1665 Inc(epos);
1666 writer((PAnsiChar(fmt)+spos-1)^, epos-spos);
1667 spos := epos+1;
1668 end
1669 else
1670 begin
1671 writer((PAnsiChar(fmt)+spos-1)^, epos-spos);
1672 spos := epos;
1673 end;
1674 continue;
1675 end;
1676 // check if we have argument for this format string
1677 if (curarg > High(args)) then
1678 begin
1679 xwrite('<OUT OF ARGS>');
1680 writer((PAnsiChar(fmt)+spos-1)^, Length(fmt)-spos+1);
1681 break;
1682 end;
1683 // skip percent
1684 if (spos+1 > Length(fmt)) then break; // oops
1685 assert(fmt[spos] = '%');
1686 Inc(spos);
1687 // parse format; check for sign
1688 if (fmt[spos] = '-') then begin sign := '-'; Inc(spos); end
1689 else if (fmt[spos] = '+') then begin sign := '+'; Inc(spos); end
1690 else sign := ' ';
1691 // parse width
1692 if (spos > Length(fmt)) then begin xwrite('<INVALID FORMAT>'); break; end;
1693 if (sign <> ' ') or ((fmt[spos] >= '0') and (fmt[spos] <= '9')) then
1694 begin
1695 if (fmt[spos] < '0') or (fmt[spos] > '9') then begin xwrite('<INVALID FORMAT>'); writer((PAnsiChar(fmt)+spos-1)^, Length(fmt)-spos+1); break; end;
1696 zeropad := (fmt[spos] = '0');
1697 width := 0;
1698 while (spos <= Length(fmt)) do
1699 begin
1700 ch := fmt[spos];
1701 if (ch < '0') or (ch > '9') then break;
1702 width := width*10+Integer(ch)-48;
1703 Inc(spos);
1704 end;
1705 end
1706 else
1707 begin
1708 width := -1;
1709 zeropad := false;
1710 end;
1711 // parse precision
1712 prec := -1;
1713 if (spos <= Length(fmt)) and (fmt[spos] = '.') then
1714 begin
1715 Inc(spos);
1716 if (spos > Length(fmt)) then begin xwrite('<INVALID FORMAT>'); break; end;
1717 if (fmt[spos] < '0') or (fmt[spos] > '9') then begin xwrite('<INVALID FORMAT>'); writer((PAnsiChar(fmt)+spos-1)^, Length(fmt)-spos+1); break; end;
1718 prec := 0;
1719 while (spos <= Length(fmt)) do
1720 begin
1721 ch := fmt[spos];
1722 if (ch < '0') or (ch > '9') then break;
1723 prec := prec*10+Integer(ch)-48;
1724 Inc(spos);
1725 end;
1726 end;
1727 // get format char
1728 if (spos > Length(fmt)) then begin xwrite('<INVALID FORMAT>'); break; end;
1729 fmtch := fmt[spos];
1730 Inc(spos);
1731 // done parsing format, check for valid format chars
1732 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;
1733 // now write formatted string
1734 case args[curarg].VType of
1735 vtInteger: // args[curarg].VInteger
1736 begin
1737 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;
1738 if (fmtch = 's') then fmtch := 'd';
1739 buildCFormat();
1740 strblen := snprintf(@strbuf[0], Length(strbuf), @fmtbuf[0], args[curarg].VInteger);
1741 writeStrBuf();
1742 end;
1743 vtBoolean: // args[curarg].VBoolean
1744 case fmtch of
1745 's':
1746 begin
1747 buildCFormat();
1748 if args[curarg].VBoolean then strblen := snprintf(@strbuf[0], Length(strbuf), @fmtbuf[0], 'true')
1749 else strblen := snprintf(@strbuf[0], Length(strbuf), @fmtbuf[0], 'false');
1750 writeStrBuf();
1751 end;
1752 'c':
1753 begin
1754 buildCFormat();
1755 if args[curarg].VBoolean then strblen := snprintf(@strbuf[0], Length(strbuf), @fmtbuf[0], AnsiChar('t'))
1756 else strblen := snprintf(@strbuf[0], Length(strbuf), @fmtbuf[0], AnsiChar('f'));
1757 writeStrBuf();
1758 end;
1759 'u', 'd', 'x', 'X':
1760 begin
1761 buildCFormat();
1762 strblen := snprintf(@strbuf[0], Length(strbuf), @fmtbuf[0], Integer(args[curarg].VBoolean));
1763 writeStrBuf();
1764 end;
1765 else
1766 begin
1767 xwrite('<INVALID FORMAT CHAR>');
1768 writer((PAnsiChar(fmt)+spos-1)^, Length(fmt)-spos+1);
1769 break;
1770 end;
1771 end;
1772 vtChar: // args[curarg].VChar
1773 case fmtch of
1774 's', 'c':
1775 begin
1776 fmtch := 'c';
1777 buildCFormat();
1778 strblen := snprintf(@strbuf[0], Length(strbuf), @fmtbuf[0], args[curarg].VChar);
1779 writeStrBuf();
1780 end;
1781 'u', 'd', 'x', 'X':
1782 begin
1783 buildCFormat();
1784 strblen := snprintf(@strbuf[0], Length(strbuf), @fmtbuf[0], Integer(args[curarg].VChar));
1785 writeStrBuf();
1786 end;
1787 else
1788 begin
1789 xwrite('<INVALID FORMAT CHAR>');
1790 writer((PAnsiChar(fmt)+spos-1)^, Length(fmt)-spos+1);
1791 break;
1792 end;
1793 end;
1794 //vtWideChar: begin end; // args[curarg].VWideChar (WideChar)
1795 vtExtended: // args[curarg].VExtended^
1796 case fmtch of
1797 's', 'g':
1798 begin
1799 fmtch := 'g';
1800 buildCFormat();
1801 strblen := snprintf(@strbuf[0], Length(strbuf), @fmtbuf[0], Double(args[curarg].VExtended^));
1802 writeStrBuf();
1803 end;
1804 'f':
1805 begin
1806 buildCFormat();
1807 strblen := snprintf(@strbuf[0], Length(strbuf), @fmtbuf[0], Double(args[curarg].VExtended^));
1808 writeStrBuf();
1809 end;
1810 'd':
1811 begin
1812 buildCFormat();
1813 strblen := snprintf(@strbuf[0], Length(strbuf), @fmtbuf[0], Integer(trunc(args[curarg].VExtended^)));
1814 writeStrBuf();
1815 end;
1816 'u', 'x', 'X':
1817 begin
1818 buildCFormat();
1819 strblen := snprintf(@strbuf[0], Length(strbuf), @fmtbuf[0], LongWord(trunc(args[curarg].VExtended^)));
1820 writeStrBuf();
1821 end;
1822 else
1823 begin
1824 xwrite('<INVALID FORMAT CHAR>');
1825 writer((PAnsiChar(fmt)+spos-1)^, Length(fmt)-spos+1);
1826 break;
1827 end;
1828 end;
1829 vtString: // args[curarg].VString^ (PShortString)
1830 begin
1831 if (sign <> '-') then indent(width-Length(args[curarg].VString^));
1832 writer(args[curarg].VString^[1], Length(args[curarg].VString^));
1833 if (sign = '-') then indent(width-Length(args[curarg].VString^));
1834 end;
1835 vtPointer: // args[curarg].VPointer
1836 case fmtch of
1837 's':
1838 begin
1839 fmtch := 'x';
1840 if (width < 8) then width := 8;
1841 zeropad := true;
1842 buildCFormat('0x');
1843 strblen := snprintf(@strbuf[0], Length(strbuf), @fmtbuf[0], PtrUInt(args[curarg].VPointer));
1844 writeStrBuf();
1845 end;
1846 'u', 'd', 'x', 'p', 'X':
1847 begin
1848 if (fmtch = 'p') then fmtch := 'x';
1849 if (width < 8) then width := 8;
1850 zeropad := true;
1851 buildCFormat('0x');
1852 strblen := snprintf(@strbuf[0], Length(strbuf), @fmtbuf[0], PtrUInt(args[curarg].VPointer));
1853 writeStrBuf();
1854 end;
1855 else
1856 begin
1857 xwrite('<INVALID FORMAT CHAR>');
1858 writer((PAnsiChar(fmt)+spos-1)^, Length(fmt)-spos+1);
1859 break;
1860 end;
1861 end;
1862 vtPChar: // args[curarg].VPChar
1863 if (args[curarg].VPChar = nil) then
1864 begin
1865 if (sign <> '-') then indent(width-3);
1866 xwrite('nil');
1867 if (sign = '-') then indent(width-3);
1868 end
1869 else
1870 begin
1871 pclen := 0;
1872 while (args[curarg].VPChar[pclen] <> #0) do Inc(pclen);
1873 if (sign <> '-') then indent(width-pclen);
1874 writer(args[curarg].VPChar^, pclen);
1875 if (sign = '-') then indent(width-pclen);
1876 end;
1877 vtObject: // args[curarg].VObject.Classname (TObject)
1878 begin
1879 if (args[curarg].VObject <> nil) then ccname := args[curarg].VObject.Classname else ccname := '<nil>';
1880 if (sign <> '-') then indent(width-Length(ccname));
1881 xwrite(ccname);
1882 if (sign = '-') then indent(width-Length(ccname));
1883 end;
1884 vtClass: // args[curarg].VClass.Classname (TClass)
1885 begin
1886 if (args[curarg].VClass <> nil) then ccname := args[curarg].VClass.Classname else ccname := '<nil>';
1887 if (sign <> '-') then indent(width-Length(ccname));
1888 xwrite(ccname);
1889 if (sign = '-') then indent(width-Length(ccname));
1890 end;
1891 //vtPWideChar: begin end; // args[curarg].VPWideChar (PWideChar)
1892 vtAnsiString: // AnsiString(args[curarg].VAnsiString) (Pointer)
1893 begin
1894 if (sign <> '-') then indent(width-Length(AnsiString(args[curarg].VAnsiString)));
1895 xwrite(AnsiString(args[curarg].VAnsiString));
1896 if (sign = '-') then indent(width-Length(AnsiString(args[curarg].VAnsiString)));
1897 end;
1898 //vtCurrency: begin end; // args[curarg].VCurrency (PCurrency)
1899 //vtVariant: begin end; // args[curarg].VVariant^ (PVariant)
1900 //vtInterface: begin end; // args[curarg].VInterface (Pointer);
1901 //vtWideString: begin end; // args[curarg].VWideString (Pointer);
1902 vtInt64: // args[curarg].VInt64^ (PInt64)
1903 begin
1904 case fmtch of
1905 's','d','u': pc := i642str(args[curarg].VInt64^, false, false);
1906 'x': pc := i642str(args[curarg].VInt64^, true, false);
1907 'X': pc := i642str(args[curarg].VInt64^, true, true);
1908 else begin xwrite('<INVALID FORMAT CHAR>'); writer((PAnsiChar(fmt)+spos-1)^, Length(fmt)-spos+1); break; end;
1909 end;
1910 pclen := 0;
1911 while (pc[pclen] <> #0) do Inc(pclen);
1912 if (sign <> '-') and (width > pclen) then
1913 begin
1914 if zeropad then
1915 begin
1916 if (pc[0] = '-') or (pc[0] = '+') then
1917 begin
1918 writer(pc^, 1);
1919 indent0(width-pclen-1);
1920 Inc(pc);
1921 Dec(pclen);
1922 end
1923 else
1924 begin
1925 indent0(width-pclen);
1926 end;
1927 end
1928 else
1929 begin
1930 indent(width-pclen);
1931 end;
1932 end;
1933 writer(pc^, pclen);
1934 if (sign = '-') then indent(width-pclen);
1935 end;
1936 vtQWord: // args[curarg].VQWord^ (PQWord)
1937 begin
1938 case fmtch of
1939 's','d','u': pc := ui642str(args[curarg].VInt64^, false, false);
1940 'x': pc := ui642str(args[curarg].VInt64^, true, false);
1941 'X': pc := ui642str(args[curarg].VInt64^, true, true);
1942 else begin xwrite('<INVALID FORMAT CHAR>'); writer((PAnsiChar(fmt)+spos-1)^, Length(fmt)-spos+1); break; end;
1943 end;
1944 pclen := 0;
1945 while (pc[pclen] <> #0) do Inc(pclen);
1946 if (sign <> '-') then begin if zeropad then indent0(width-pclen) else indent(width-pclen); end;
1947 writer(pc^, pclen);
1948 if (sign = '-') then indent(width-pclen);
1949 end;
1950 else
1951 begin
1952 xwrite('<INVALID TYPE>');
1953 writer((PAnsiChar(fmt)+spos-1)^, Length(fmt)-spos+1);
1954 break;
1955 end;
1956 end;
1957 Inc(curarg);
1958 end;
1959 end;
1962 (*
1963 var
1964 ss: ShortString;
1965 ls: AnsiString;
1966 i64: Int64 = -$A000000000;
1967 ui64: UInt64 = $A000000000;
1968 begin
1969 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']);
1970 writef(conwriter, 'test float:<%s;%u;%f;%g>'#10, [666.6942, 666.6942, 666.6942, 666.6942]);
1971 ss := 'fuckit';
1972 ls := 'FUCKIT';
1973 writef(conwriter, 'test ss:<%5s;%040s>'#10, [ss, ss]);
1974 writef(conwriter, 'test ls:<%5s;%040s>'#10, [ls, ls]);
1975 writef(conwriter, 'test pointer:<%s;%x;%p>'#10, [@ss, @ss, @ss]);
1976 writef(conwriter, 'test i64:<%s;%x;%015d;%u;%X>'#10, [i64, i64, i64, i64, i64]);
1977 writef(conwriter, 'test ui64:<%s;%x;%15d;%015u;%X>'#10, [ui64, ui64, ui64, ui64, ui64]);
1978 *)
1979 end.