DEADSOFTWARE

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