DEADSOFTWARE

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