DEADSOFTWARE

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