DEADSOFTWARE

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