DEADSOFTWARE

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