DEADSOFTWARE

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