DEADSOFTWARE

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