DEADSOFTWARE

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