DEADSOFTWARE

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