DEADSOFTWARE

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