DEADSOFTWARE

fix work under msdos
[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, either version 3 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 *)
16 {$INCLUDE a_modes.inc}
17 unit utils;
19 interface
21 uses
22 SysUtils, Classes;
25 // ////////////////////////////////////////////////////////////////////////// //
26 type
27 SSArray = array of ShortString;
30 // ////////////////////////////////////////////////////////////////////////// //
31 type
32 TUtf8DecoderFast = packed record
33 public
34 const Replacement = $FFFD; // replacement char for invalid unicode
35 const Accept = 0;
36 const Reject = 12;
38 private
39 state: LongWord;
41 public
42 codepoint: LongWord; // decoded codepoint (valid only when decoder is in "complete" state)
44 public
45 constructor Create (v: Boolean{fuck you, fpc});
47 procedure reset (); inline;
49 function complete (): Boolean; inline; // is current character complete? take `codepoint` then
50 function invalid (): Boolean; inline;
51 function completeOrInvalid (): Boolean; inline;
53 // process one byte, return `true` if codepoint is ready
54 function decode (b: Byte): Boolean; inline; overload;
55 function decode (c: AnsiChar): Boolean; inline; overload;
56 end;
59 // ////////////////////////////////////////////////////////////////////////// //
60 function getFilenameExt (const fn: AnsiString): AnsiString;
61 function setFilenameExt (const fn, ext: AnsiString): AnsiString;
62 function forceFilenameExt (const fn, ext: AnsiString): AnsiString;
64 // strips out name from `fn`, leaving trailing slash
65 function getFilenamePath (const fn: AnsiString): AnsiString;
67 // ends with '/' or '\'?
68 function isFilenamePath (const fn: AnsiString): Boolean;
70 // strips extra trailing slashes in `path, and extra leading slashes in `fn`
71 // will add slash to `path`, even if `fn` is empty!
72 function filenameConcat (const path, fn: AnsiString): AnsiString;
74 // does filename have one of ".wad", ".pk3", ".zip" extensions?
75 function hasWadExtension (const fn: AnsiString): Boolean;
77 // does filepath have ".XXX:\" in it?
78 function isWadPath (const fn: AnsiString): Boolean;
80 // adds ".wad" extension if filename doesn't have one of ".wad", ".pk3", ".zip"
81 function addWadExtension (const fn: AnsiString): AnsiString;
83 // convert number to strig with nice commas
84 function int64ToStrComma (i: Int64): AnsiString;
86 function upcase1251 (ch: AnsiChar): AnsiChar; inline;
87 function locase1251 (ch: AnsiChar): AnsiChar; inline;
89 function toLowerCase1251 (const s: AnsiString): AnsiString;
91 // `true` if strings are equal; ignoring case for cp1251
92 function strEquCI1251 (const s0, s1: AnsiString): Boolean;
94 function utf8Valid (const s: AnsiString): Boolean;
96 function utf8to1251 (s: AnsiString): AnsiString;
98 // `pathname` will be modified if path is valid
99 // `lastIsDir` should be `true` if we are searching for directory
100 // nobody cares about shitdoze, so i'll use the same code path for it
101 function findFileCI (var pathname: AnsiString; lastIsDir: Boolean=false): Boolean;
103 // return fixed AnsiString or empty AnsiString
104 function findDiskWad (fname: AnsiString): AnsiString;
106 // they throws
107 function openDiskFileRO (pathname: AnsiString): TStream;
108 function createDiskFile (pathname: AnsiString): TStream;
110 // little endian
111 procedure writeSign (st: TStream; const sign: AnsiString);
112 function checkSign (st: TStream; const sign: AnsiString): Boolean;
114 procedure writeBool (st: TStream; b: Boolean);
115 function readBool (st: TStream): Boolean;
117 procedure writeStr (st: TStream; const str: AnsiString; maxlen: LongWord=65535);
118 function readStr (st: TStream; maxlen: LongWord=65535): AnsiString;
120 procedure writeInt (st: TStream; v: Byte); overload;
121 procedure writeInt (st: TStream; v: ShortInt); overload;
122 procedure writeInt (st: TStream; v: Word); overload;
123 procedure writeInt (st: TStream; v: SmallInt); overload;
124 procedure writeInt (st: TStream; v: LongWord); overload;
125 procedure writeInt (st: TStream; v: LongInt); overload;
126 procedure writeInt (st: TStream; v: Int64); overload;
127 procedure writeInt (st: TStream; v: UInt64); overload;
129 function readByte (st: TStream): Byte;
130 function readShortInt (st: TStream): ShortInt;
131 function readWord (st: TStream): Word;
132 function readSmallInt (st: TStream): SmallInt;
133 function readLongWord (st: TStream): LongWord;
134 function readLongInt (st: TStream): LongInt;
135 function readInt64 (st: TStream): Int64;
136 function readUInt64 (st: TStream): UInt64;
138 // big endian
139 procedure writeIntBE (st: TStream; v: Byte); overload;
140 procedure writeIntBE (st: TStream; v: ShortInt); overload;
141 procedure writeIntBE (st: TStream; v: Word); overload;
142 procedure writeIntBE (st: TStream; v: SmallInt); overload;
143 procedure writeIntBE (st: TStream; v: LongWord); overload;
144 procedure writeIntBE (st: TStream; v: LongInt); overload;
145 procedure writeIntBE (st: TStream; v: Int64); overload;
146 procedure writeIntBE (st: TStream; v: UInt64); overload;
148 function readByteBE (st: TStream): Byte;
149 function readShortIntBE (st: TStream): ShortInt;
150 function readWordBE (st: TStream): Word;
151 function readSmallIntBE (st: TStream): SmallInt;
152 function readLongWordBE (st: TStream): LongWord;
153 function readLongIntBE (st: TStream): LongInt;
154 function readInt64BE (st: TStream): Int64;
155 function readUInt64BE (st: TStream): UInt64;
158 function nmin (a, b: Byte): Byte; inline; overload;
159 function nmin (a, b: ShortInt): ShortInt; inline; overload;
160 function nmin (a, b: Word): Word; inline; overload;
161 function nmin (a, b: SmallInt): SmallInt; inline; overload;
162 function nmin (a, b: LongWord): LongWord; inline; overload;
163 function nmin (a, b: LongInt): LongInt; inline; overload;
164 function nmin (a, b: Int64): Int64; inline; overload;
165 function nmin (a, b: UInt64): UInt64; inline; overload;
166 function nmin (a, b: Single): Single; inline; overload;
167 function nmin (a, b: Double): Double; inline; overload;
168 {$IF DEFINED(CPU386) OR DEFINED(CPUAMD64)}
169 function nmin (a, b: Extended): Extended; inline; overload;
170 {$ENDIF}
172 function nmax (a, b: Byte): Byte; inline; overload;
173 function nmax (a, b: ShortInt): ShortInt; inline; overload;
174 function nmax (a, b: Word): Word; inline; overload;
175 function nmax (a, b: SmallInt): SmallInt; inline; overload;
176 function nmax (a, b: LongWord): LongWord; inline; overload;
177 function nmax (a, b: LongInt): LongInt; inline; overload;
178 function nmax (a, b: Int64): Int64; inline; overload;
179 function nmax (a, b: UInt64): UInt64; inline; overload;
180 function nmax (a, b: Single): Single; inline; overload;
181 function nmax (a, b: Double): Double; inline; overload;
182 {$IF DEFINED(CPU386) OR DEFINED(CPUAMD64)}
183 function nmax (a, b: Extended): Extended; inline; overload;
184 {$ENDIF}
185 function nclamp (v, a, b: Byte): Byte; inline; overload;
186 function nclamp (v, a, b: ShortInt): ShortInt; inline; overload;
187 function nclamp (v, a, b: Word): Word; inline; overload;
188 function nclamp (v, a, b: SmallInt): SmallInt; inline; overload;
189 function nclamp (v, a, b: LongWord): LongWord; inline; overload;
190 function nclamp (v, a, b: LongInt): LongInt; inline; overload;
191 function nclamp (v, a, b: Int64): Int64; inline; overload;
192 function nclamp (v, a, b: UInt64): UInt64; inline; overload;
193 function nclamp (v, a, b: Single): Single; inline; overload;
194 function nclamp (v, a, b: Double): Double; inline; overload;
195 {$IF DEFINED(CPU386) OR DEFINED(CPUAMD64)}
196 function nclamp (v, a, b: Extended): Extended; inline; overload;
197 {$ENDIF}
199 type
200 TFormatStrFCallback = procedure (constref buf; len: SizeUInt);
202 // returns formatted string if `writerCB` is `nil`, empty string otherwise
203 function formatstrf (const fmt: AnsiString; const args: array of const; writerCB: TFormatStrFCallback=nil): AnsiString;
205 function wchar2win (wc: WideChar): AnsiChar; inline;
206 function utf2win (const s: AnsiString): AnsiString;
207 function win2utf (const s: AnsiString): AnsiString;
208 function digitInBase (ch: AnsiChar; base: Integer): Integer;
210 // returns string in single or double quotes
211 // single quotes supports only pascal-style '' for single quote char
212 // double quotes supports c-style escapes
213 // function will select quote mode automatically
214 function quoteStr (const s: AnsiString): AnsiString;
217 type
218 generic TSimpleList<ItemT> = class
219 private
220 //type PItemT = ^ItemT;
221 type TItemArr = array of ItemT;
223 public
224 type
225 TEnumerator = record
226 private
227 mItems: TItemArr;
228 mCount: Integer;
229 mCurrent: Integer;
230 public
231 constructor Create (const aitems: TItemArr; acount: Integer);
232 function MoveNext: Boolean;
233 function getCurrent (): ItemT;
234 property Current: ItemT read getCurrent;
235 end;
237 private
238 mItems: TItemArr;
239 mCount: Integer; // can be less than `mItems` size
241 private
242 function getAt (idx: Integer): ItemT; inline;
243 procedure setAt (idx: Integer; const it: ItemT); inline;
245 function getCapacity (): Integer; inline;
246 procedure setCapacity (v: Integer); inline;
248 public
249 constructor Create (acapacity: Integer=-1);
250 destructor Destroy (); override;
252 //WARNING! don't change list contents in `for ... in`!
253 function GetEnumerator (): TEnumerator;
255 procedure reset (); inline; // won't resize `mItems`
256 procedure clear (); inline;
258 procedure append (constref it: ItemT); inline;
259 procedure delete (idx: Integer); inline;
260 function remove (idx: Integer): ItemT; inline;
262 public
263 property count: Integer read mCount;
264 property capacity: Integer read getCapacity write setCapacity;
265 property at[idx: Integer]: ItemT read getAt write setAt; default;
266 end;
269 procedure FillMemory (Dest: Pointer; Len: LongWord; Ch: Byte); inline;
270 procedure CopyMemory (Dest: Pointer; Src: Pointer; Len: LongWord); inline;
271 procedure ZeroMemory (Dest: Pointer; Len: LongWord); inline;
274 implementation
276 uses
277 xstreams, StrUtils, e_Log;
279 const
280 {$IFDEF GO32V2}
281 DirSep = '\';
282 {$ELSE}
283 DirSep = '/';
284 {$ENDIF}
286 // ////////////////////////////////////////////////////////////////////////// //
287 procedure CopyMemory (Dest: Pointer; Src: Pointer; Len: LongWord); inline;
288 begin
289 Move(Src^, Dest^, Len);
290 end;
292 procedure FillMemory (Dest: Pointer; Len: LongWord; Ch: Byte); inline;
293 begin
294 FillChar(Dest^, Len, Ch);
295 end;
297 procedure ZeroMemory (Dest: Pointer; Len: LongWord); inline;
298 begin
299 FillChar(Dest^, Len, 0);
300 end;
303 // ////////////////////////////////////////////////////////////////////////// //
304 constructor TSimpleList.TEnumerator.Create (const aitems: TItemArr; acount: Integer);
305 begin
306 mItems := aitems;
307 mCurrent := -1;
308 mCount := acount;
309 end;
311 function TSimpleList.TEnumerator.MoveNext: Boolean;
312 begin
313 Inc(mCurrent);
314 result := (mCurrent < mCount);
315 end;
317 function TSimpleList.TEnumerator.getCurrent (): ItemT;
318 begin
319 result := mItems[mCurrent];
320 end;
323 // ////////////////////////////////////////////////////////////////////////// //
324 constructor TSimpleList.Create (acapacity: Integer=-1);
325 begin
326 mItems := nil;
327 if (acapacity > 0) then SetLength(mItems, acapacity);
328 mCount := 0;
329 end;
332 destructor TSimpleList.Destroy ();
333 begin
334 mItems := nil;
335 inherited;
336 end;
339 function TSimpleList.getCapacity (): Integer; inline;
340 begin
341 result := Length(mItems);
342 end;
345 procedure TSimpleList.setCapacity (v: Integer); inline;
346 begin
347 if (v < mCount) then v := mCount;
348 if (v <> Length(mItems)) then SetLength(mItems, v);
349 end;
352 function TSimpleList.GetEnumerator (): TEnumerator;
353 begin
354 if (Length(mItems) > 0) then result := TEnumerator.Create(mItems, mCount)
355 else result := TEnumerator.Create(nil, -1);
356 end;
359 procedure TSimpleList.reset (); inline;
360 begin
361 mCount := 0;
362 end;
365 procedure TSimpleList.clear (); inline;
366 begin
367 mItems := nil;
368 mCount := 0;
369 end;
372 function TSimpleList.getAt (idx: Integer): ItemT; inline;
373 begin
374 if (idx >= 0) and (idx < mCount) then result := mItems[idx] else result := Default(ItemT);
375 end;
378 procedure TSimpleList.setAt (idx: Integer; const it: ItemT); inline;
379 begin
380 if (idx >= 0) and (idx < mCount) then mItems[idx] := it;
381 end;
384 procedure TSimpleList.append (constref it: ItemT); inline;
385 var
386 newsz: Integer;
387 begin
388 if (mCount >= Length(mItems)) then
389 begin
390 newsz := mCount+(mCount div 3)+128;
391 SetLength(mItems, newsz);
392 end;
393 mItems[mCount] := it;
394 Inc(mCount);
395 end;
398 procedure TSimpleList.delete (idx: Integer); inline;
399 var
400 f: Integer;
401 begin
402 if (idx >= 0) and (idx < mCount) then
403 begin
404 for f := idx+1 to mCount-1 do mItems[f-1] := mItems[f];
405 end;
406 end;
409 function TSimpleList.remove (idx: Integer): ItemT; inline;
410 var
411 f: Integer;
412 begin
413 if (idx >= 0) and (idx < mCount) then
414 begin
415 result := mItems[idx];
416 for f := idx+1 to mCount-1 do mItems[f-1] := mItems[f];
417 end
418 else
419 begin
420 result := Default(ItemT);
421 end;
422 end;
425 // ////////////////////////////////////////////////////////////////////////// //
426 var
427 wc2shitmap: array[0..65535] of AnsiChar;
428 wc2shitmapInited: Boolean = false;
431 // ////////////////////////////////////////////////////////////////////////// //
432 const
433 cp1251: array[0..127] of Word = (
434 $0402,$0403,$201A,$0453,$201E,$2026,$2020,$2021,$20AC,$2030,$0409,$2039,$040A,$040C,$040B,$040F,
435 $0452,$2018,$2019,$201C,$201D,$2022,$2013,$2014,$003F,$2122,$0459,$203A,$045A,$045C,$045B,$045F,
436 $00A0,$040E,$045E,$0408,$00A4,$0490,$00A6,$00A7,$0401,$00A9,$0404,$00AB,$00AC,$00AD,$00AE,$0407,
437 $00B0,$00B1,$0406,$0456,$0491,$00B5,$00B6,$00B7,$0451,$2116,$0454,$00BB,$0458,$0405,$0455,$0457,
438 $0410,$0411,$0412,$0413,$0414,$0415,$0416,$0417,$0418,$0419,$041A,$041B,$041C,$041D,$041E,$041F,
439 $0420,$0421,$0422,$0423,$0424,$0425,$0426,$0427,$0428,$0429,$042A,$042B,$042C,$042D,$042E,$042F,
440 $0430,$0431,$0432,$0433,$0434,$0435,$0436,$0437,$0438,$0439,$043A,$043B,$043C,$043D,$043E,$043F,
441 $0440,$0441,$0442,$0443,$0444,$0445,$0446,$0447,$0448,$0449,$044A,$044B,$044C,$044D,$044E,$044F
442 );
445 procedure initShitMap ();
446 var
447 f: Integer;
448 begin
449 for f := 0 to High(wc2shitmap) do wc2shitmap[f] := '?';
450 for f := 0 to 127 do wc2shitmap[f] := AnsiChar(f);
451 for f := 0 to 127 do wc2shitmap[cp1251[f]] := AnsiChar(f+128);
452 wc2shitmapInited := true;
453 end;
456 // ////////////////////////////////////////////////////////////////////////// //
457 // fast state-machine based UTF-8 decoder; using 8 bytes of memory
458 // code points from invalid range will never be valid, this is the property of the state machine
459 const
460 // see http://bjoern.hoehrmann.de/utf-8/decoder/dfa/
461 utf8dfa: array[0..$16c-1] of Byte = (
462 // maps bytes to character classes
463 $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00, // 00-0f
464 $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00, // 10-1f
465 $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00, // 20-2f
466 $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00, // 30-3f
467 $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00, // 40-4f
468 $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00, // 50-5f
469 $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00, // 60-6f
470 $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00, // 70-7f
471 $01,$01,$01,$01,$01,$01,$01,$01,$01,$01,$01,$01,$01,$01,$01,$01, // 80-8f
472 $09,$09,$09,$09,$09,$09,$09,$09,$09,$09,$09,$09,$09,$09,$09,$09, // 90-9f
473 $07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07, // a0-af
474 $07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07,$07, // b0-bf
475 $08,$08,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02, // c0-cf
476 $02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02,$02, // d0-df
477 $0a,$03,$03,$03,$03,$03,$03,$03,$03,$03,$03,$03,$03,$04,$03,$03, // e0-ef
478 $0b,$06,$06,$06,$05,$08,$08,$08,$08,$08,$08,$08,$08,$08,$08,$08, // f0-ff
479 // maps a combination of a state of the automaton and a character class to a state
480 $00,$0c,$18,$24,$3c,$60,$54,$0c,$0c,$0c,$30,$48,$0c,$0c,$0c,$0c, // 100-10f
481 $0c,$0c,$0c,$0c,$0c,$0c,$0c,$0c,$0c,$00,$0c,$0c,$0c,$0c,$0c,$00, // 110-11f
482 $0c,$00,$0c,$0c,$0c,$18,$0c,$0c,$0c,$0c,$0c,$18,$0c,$18,$0c,$0c, // 120-12f
483 $0c,$0c,$0c,$0c,$0c,$0c,$0c,$18,$0c,$0c,$0c,$0c,$0c,$18,$0c,$0c, // 130-13f
484 $0c,$0c,$0c,$0c,$0c,$18,$0c,$0c,$0c,$0c,$0c,$0c,$0c,$0c,$0c,$24, // 140-14f
485 $0c,$24,$0c,$0c,$0c,$24,$0c,$0c,$0c,$0c,$0c,$24,$0c,$24,$0c,$0c, // 150-15f
486 $0c,$24,$0c,$0c,$0c,$0c,$0c,$0c,$0c,$0c,$0c,$0c);
489 // ////////////////////////////////////////////////////////////////////////// //
490 constructor TUtf8DecoderFast.Create (v: Boolean{fuck you, fpc}); begin state := Accept; codepoint := 0; end;
492 procedure TUtf8DecoderFast.reset (); inline; begin state := Accept; codepoint := 0; end;
494 function TUtf8DecoderFast.complete (): Boolean; inline; begin result := (state = Accept); end;
495 function TUtf8DecoderFast.invalid (): Boolean; inline; begin result := (state = Reject); end;
496 function TUtf8DecoderFast.completeOrInvalid (): Boolean; inline; begin result := (state = Accept) or (state = Reject); end;
498 function TUtf8DecoderFast.decode (c: AnsiChar): Boolean; inline; overload; begin result := decode(Byte(c)); end;
500 function TUtf8DecoderFast.decode (b: Byte): Boolean; inline; overload;
501 var
502 tp: LongWord;
503 begin
504 if (state = Reject) then begin state := Accept; codepoint := 0; end;
505 tp := utf8dfa[b];
506 if (state <> Accept) then codepoint := (b and $3f) or (codepoint shl 6) else codepoint := ($ff shr tp) and b;
507 state := utf8dfa[256+state+tp];
508 if (state = Reject) then begin codepoint := Replacement; state := Accept; end;
509 result := (state = Accept);
510 end;
513 // ////////////////////////////////////////////////////////////////////////// //
514 function wchar2win (wc: WideChar): AnsiChar; inline;
515 begin
516 if not wc2shitmapInited then initShitMap();
517 if (LongWord(wc) > 65535) then result := '?' else result := wc2shitmap[LongWord(wc)];
518 end;
521 // ////////////////////////////////////////////////////////////////////////// //
522 function utf2win (const s: AnsiString): AnsiString;
523 var
524 f, c: Integer;
525 ud: TUtf8DecoderFast;
526 begin
527 for f := 1 to Length(s) do
528 begin
529 if (Byte(s[f]) > 127) then
530 begin
531 ud := TUtf8DecoderFast.Create(true);
532 result := '';
533 for c := 1 to Length(s) do
534 begin
535 if ud.decode(s[c]) then result += wchar2win(WideChar(ud.codepoint));
536 end;
537 exit;
538 end;
539 end;
540 result := s;
541 end;
544 function win2utf (const s: AnsiString): AnsiString;
545 var
546 f, c: Integer;
548 function utf8Encode (code: Integer): AnsiString;
549 begin
550 if (code < 0) or (code > $10FFFF) then begin result := '?'; exit; end;
551 if (code <= $7f) then
552 begin
553 result := AnsiChar(code and $ff);
554 end
555 else if (code <= $7FF) then
556 begin
557 result := AnsiChar($C0 or (code shr 6));
558 result += AnsiChar($80 or (code and $3F));
559 end
560 else if (code <= $FFFF) then
561 begin
562 result := AnsiChar($E0 or (code shr 12));
563 result += AnsiChar($80 or ((code shr 6) and $3F));
564 result += AnsiChar($80 or (code and $3F));
565 end
566 else if (code <= $10FFFF) then
567 begin
568 result := AnsiChar($F0 or (code shr 18));
569 result += AnsiChar($80 or ((code shr 12) and $3F));
570 result += AnsiChar($80 or ((code shr 6) and $3F));
571 result += AnsiChar($80 or (code and $3F));
572 end
573 else
574 begin
575 result := '?';
576 end;
577 end;
579 begin
580 for f := 1 to Length(s) do
581 begin
582 if (Byte(s[f]) > 127) then
583 begin
584 result := '';
585 for c := 1 to Length(s) do
586 begin
587 if (Byte(s[c]) < 128) then
588 begin
589 result += s[c];
590 end
591 else
592 begin
593 result += utf8Encode(cp1251[Byte(s[c])-128])
594 end;
595 end;
596 exit;
597 end;
598 end;
599 result := s;
600 end;
603 // ////////////////////////////////////////////////////////////////////////// //
604 function digitInBase (ch: AnsiChar; base: Integer): Integer;
605 begin
606 result := -1;
607 if (base < 1) or (base > 36) then exit;
608 if (ch < '0') then exit;
609 if (base <= 10) then
610 begin
611 if (Integer(ch) >= 48+base) then exit;
612 result := Integer(ch)-48;
613 end
614 else
615 begin
616 if (ch >= '0') and (ch <= '9') then begin result := Integer(ch)-48; exit; end;
617 if (ch >= 'a') and (ch <= 'z') then Dec(ch, 32); // poor man's tolower()
618 if (ch < 'A') or (Integer(ch) >= 65+(base-10)) then exit;
619 result := Integer(ch)-65+10;
620 end;
621 end;
624 // ////////////////////////////////////////////////////////////////////////// //
625 function quoteStr (const s: AnsiString): AnsiString;
627 function squote (const s: AnsiString): AnsiString;
628 var
629 f: Integer;
630 begin
631 result := '''';
632 for f := 1 to Length(s) do
633 begin
634 if (s[f] = '''') then result += '''';
635 result += s[f];
636 end;
637 result += '''';
638 end;
640 function dquote (const s: AnsiString): AnsiString;
641 var
642 f: Integer;
643 ch: AnsiChar;
644 begin
645 result := '"';
646 for f := 1 to Length(s) do
647 begin
648 ch := s[f];
649 if (ch = #0) then result += '\z'
650 else if (ch = #9) then result += '\t'
651 else if (ch = #10) then result += '\n'
652 else if (ch = #13) then result += '\r'
653 else if (ch = #27) then result += '\e'
654 else if (ch < ' ') or (ch = #127) then
655 begin
656 result += '\x';
657 result += LowerCase(IntToHex(Integer(ch), 2));
658 end
659 else if (ch = '"') or (ch = '\') then
660 begin
661 result += '\';
662 result += ch;
663 end
664 else
665 begin
666 result += ch;
667 end;
668 end;
669 result += '"';
670 end;
672 var
673 needSingle: Boolean = false;
674 f: Integer;
675 begin
676 for f := 1 to Length(s) do
677 begin
678 if (s[f] = '''') then begin needSingle := true; continue; end;
679 if (s[f] < ' ') or (s[f] = #127) then begin result := dquote(s); exit; end;
680 end;
681 if needSingle then result := squote(s) else result := ''''+s+'''';
682 end;
685 // ////////////////////////////////////////////////////////////////////////// //
686 function getFilenameExt (const fn: AnsiString): AnsiString;
687 var
688 pos: Integer;
689 ch: AnsiChar;
690 begin
691 pos := Length(fn);
692 while (pos > 0) do
693 begin
694 ch := fn[pos];
695 if (ch = '.') then
696 begin
697 if (pos = Length(fn)) then result := '' else result := Copy(fn, pos, Length(fn)-pos+1);
698 exit;
699 end;
700 if (ch = '/') or (ch = '\') then break;
701 Dec(pos);
702 end;
703 result := ''; // no extension
704 end;
707 function setFilenameExt (const fn, ext: AnsiString): AnsiString;
708 var
709 pos: Integer;
710 ch: AnsiChar;
711 begin
712 result := fn;
713 if (Length(ext) = 0) or (ext = '.') then exit;
714 pos := Length(fn);
715 while (pos > 0) do
716 begin
717 ch := fn[pos];
718 if (ch = '.') then exit;
719 if (ch = '/') or (ch = '\') then break;
720 Dec(pos);
721 end;
722 if (ext[1] <> '.') then result += '.';
723 result += ext;
724 end;
727 function forceFilenameExt (const fn, ext: AnsiString): AnsiString;
728 var
729 pos: Integer;
730 ch: AnsiChar;
731 begin
732 result := fn;
733 pos := Length(fn);
734 while (pos > 0) do
735 begin
736 ch := fn[pos];
737 if (ch = '.') then
738 begin
739 if (Length(ext) = 0) or (ext = '.') then
740 begin
741 result := Copy(fn, 1, pos-1);
742 end
743 else
744 begin
745 if (ext[1] = '.') then result := Copy(fn, 1, pos-1) else result := Copy(fn, 1, pos);
746 result += ext;
747 exit;
748 end;
749 end;
750 if (ch = '/') or (ch = '\') then break;
751 Dec(pos);
752 end;
753 if (Length(ext) > 0) then
754 begin
755 if (ext[1] <> '.') then result += '.';
756 result += ext;
757 end;
758 end;
761 // strips out name from `fn`, leaving trailing slash
762 function getFilenamePath (const fn: AnsiString): AnsiString;
763 var
764 pos: Integer;
765 ch: AnsiChar;
766 begin
767 if (Length(fn) = 0) then begin result := '.' + DirSep; exit; end;
768 if (fn[Length(fn)] = '/') or (fn[Length(fn)] = '\') then begin result := fn; exit; end;
769 pos := Length(fn);
770 while (pos > 0) do
771 begin
772 ch := fn[pos];
773 if (ch = '/') or (ch = '\') then begin result := Copy(fn, 1, pos); exit; end;
774 Dec(pos);
775 end;
776 result := '.' + DirSep; // no path -> current dir
777 end;
780 // ends with '/' or '\'?
781 function isFilenamePath (const fn: AnsiString): Boolean;
782 begin
783 if (Length(fn) = 0) then
784 begin
785 result := false;
786 end
787 else
788 begin
789 result := (fn[Length(fn)] = '/') or (fn[Length(fn)] = '\');
790 end;
791 end;
794 // strips extra trailing slashes in `path, and extra leading slashes in `fn`
795 // will add slash to `path`, even if `fn` is empty!
796 function filenameConcat (const path, fn: AnsiString): AnsiString;
797 var
798 pos: Integer;
799 begin
800 pos := 1;
801 while (pos <= Length(fn)) and ((fn[pos] = '/') or (fn[pos] = '\')) do Inc(pos);
802 result := path;
803 if (Length(result) > 0) and ((result[Length(result)] <> '/') and (result[Length(result)] <> '\')) then result += DirSep;
804 if (pos <= Length(fn)) then
805 begin
806 result += Copy(fn, pos, Length(fn)-pos+1);
807 //FIXME: make this faster!
808 while (Length(result) > 0) and ((result[Length(result)] = '/') or (result[Length(result)] = '\')) do
809 begin
810 Delete(result, Length(result), 1);
811 end;
812 if (fn[Length(fn)] = '/') or (fn[Length(fn)] = '\') then result += DirSep;
813 end;
814 end;
817 function hasWadExtension (const fn: AnsiString): Boolean;
818 var
819 ext: AnsiString;
820 begin
821 ext := getFilenameExt(fn);
822 result := StrEquCI1251(ext, '.wad') or StrEquCI1251(ext, '.pk3') or StrEquCI1251(ext, '.zip') or StrEquCI1251(ext, '.dfz');
823 end;
826 function addWadExtension (const fn: AnsiString): AnsiString;
827 begin
828 result := fn;
829 if not hasWadExtension(result) then result := result+'.wad';
830 end;
833 function isWadPath (const fn: AnsiString): Boolean;
834 var
835 pos: Integer;
836 s: AnsiString;
837 begin
838 result := false;
839 pos := 1;
840 while (pos <= Length(fn)) do
841 begin
842 if (fn[pos] = ':') then
843 begin
844 if (Length(fn)-pos < 1) then break;
845 if (pos-4 > 1) and (fn[pos-4] = '.') and ((fn[pos+1] = '\') or (fn[pos+1] = '/')) then
846 begin
847 s := Copy(fn, pos-4, 4);
848 if StrEquCI1251(s, '.wad') or StrEquCI1251(s, '.pk3') or StrEquCI1251(s, '.zip') or StrEquCI1251(s, '.dfz') then
849 begin
850 result := true;
851 exit;
852 end;
853 end;
854 end;
855 Inc(pos);
856 end;
857 end;
860 function int64ToStrComma (i: Int64): AnsiString;
861 var
862 f: Integer;
863 begin
864 Str(i, result);
865 f := Length(result)+1;
866 while f > 4 do
867 begin
868 Dec(f, 3); Insert(',', result, f);
869 end;
870 end;
873 function upcase1251 (ch: AnsiChar): AnsiChar; inline;
874 begin
875 if ch < #128 then
876 begin
877 if (ch >= 'a') and (ch <= 'z') then Dec(ch, 32);
878 end
879 else
880 begin
881 if (ch >= #224) and (ch <= #255) then
882 begin
883 Dec(ch, 32);
884 end
885 else
886 begin
887 case ch of
888 #184, #186, #191: Dec(ch, 16);
889 #162, #179: Dec(ch);
890 end;
891 end;
892 end;
893 result := ch;
894 end;
897 function locase1251 (ch: AnsiChar): AnsiChar; inline;
898 begin
899 if ch < #128 then
900 begin
901 if (ch >= 'A') and (ch <= 'Z') then Inc(ch, 32);
902 end
903 else
904 begin
905 if (ch >= #192) and (ch <= #223) then
906 begin
907 Inc(ch, 32);
908 end
909 else
910 begin
911 case ch of
912 #168, #170, #175: Inc(ch, 16);
913 #161, #178: Inc(ch);
914 end;
915 end;
916 end;
917 result := ch;
918 end;
921 function strEquCI1251 (const s0, s1: AnsiString): Boolean;
922 var
923 i: Integer;
924 begin
925 result := false;
926 if length(s0) <> length(s1) then exit;
927 for i := 1 to length(s0) do if UpCase1251(s0[i]) <> UpCase1251(s1[i]) then exit;
928 result := true;
929 end;
932 function toLowerCase1251 (const s: AnsiString): AnsiString;
933 var
934 f: Integer;
935 ch: AnsiChar;
936 begin
937 for ch in s do
938 begin
939 if (ch <> LoCase1251(ch)) then
940 begin
941 result := '';
942 SetLength(result, Length(s));
943 for f := 1 to Length(s) do result[f] := LoCase1251(s[f]);
944 exit;
945 end;
946 end;
947 // nothing to do
948 result := s;
949 end;
952 // ////////////////////////////////////////////////////////////////////////// //
953 // utils
954 // `ch`: utf8 start
955 // -1: invalid utf8
956 function utf8CodeLen (ch: Word): Integer;
957 begin
958 if ch < $80 then begin result := 1; exit; end;
959 if (ch and $FE) = $FC then begin result := 6; exit; end;
960 if (ch and $FC) = $F8 then begin result := 5; exit; end;
961 if (ch and $F8) = $F0 then begin result := 4; exit; end;
962 if (ch and $F0) = $E0 then begin result := 3; exit; end;
963 if (ch and $E0) = $C0 then begin result := 2; exit; end;
964 result := -1; // invalid
965 end;
968 function utf8Valid (const s: AnsiString): Boolean;
969 var
970 pos, len: Integer;
971 begin
972 result := false;
973 pos := 1;
974 while pos <= length(s) do
975 begin
976 len := utf8CodeLen(Byte(s[pos]));
977 if len < 1 then exit; // invalid sequence start
978 if pos+len-1 > length(s) then exit; // out of chars in string
979 Dec(len);
980 Inc(pos);
981 // check other sequence bytes
982 while len > 0 do
983 begin
984 if (Byte(s[pos]) and $C0) <> $80 then exit;
985 Dec(len);
986 Inc(pos);
987 end;
988 end;
989 result := true;
990 end;
993 // ////////////////////////////////////////////////////////////////////////// //
994 const
995 uni2wint: array [128..255] of Word = (
996 $0402,$0403,$201A,$0453,$201E,$2026,$2020,$2021,$20AC,$2030,$0409,$2039,$040A,$040C,$040B,$040F,
997 $0452,$2018,$2019,$201C,$201D,$2022,$2013,$2014,$003F,$2122,$0459,$203A,$045A,$045C,$045B,$045F,
998 $00A0,$040E,$045E,$0408,$00A4,$0490,$00A6,$00A7,$0401,$00A9,$0404,$00AB,$00AC,$00AD,$00AE,$0407,
999 $00B0,$00B1,$0406,$0456,$0491,$00B5,$00B6,$00B7,$0451,$2116,$0454,$00BB,$0458,$0405,$0455,$0457,
1000 $0410,$0411,$0412,$0413,$0414,$0415,$0416,$0417,$0418,$0419,$041A,$041B,$041C,$041D,$041E,$041F,
1001 $0420,$0421,$0422,$0423,$0424,$0425,$0426,$0427,$0428,$0429,$042A,$042B,$042C,$042D,$042E,$042F,
1002 $0430,$0431,$0432,$0433,$0434,$0435,$0436,$0437,$0438,$0439,$043A,$043B,$043C,$043D,$043E,$043F,
1003 $0440,$0441,$0442,$0443,$0444,$0445,$0446,$0447,$0448,$0449,$044A,$044B,$044C,$044D,$044E,$044F
1004 );
1007 function decodeUtf8Char (s: AnsiString; var pos: Integer): AnsiChar;
1008 var
1009 b, c: Integer;
1010 begin
1011 (* The following encodings are valid, except for the 5 and 6 byte
1012 * combinations:
1013 * 0xxxxxxx
1014 * 110xxxxx 10xxxxxx
1015 * 1110xxxx 10xxxxxx 10xxxxxx
1016 * 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
1017 * 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
1018 * 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
1019 *)
1020 result := '?';
1021 if pos > length(s) then exit;
1023 b := Byte(s[pos]);
1024 Inc(pos);
1025 if b < $80 then begin result := AnsiChar(b); exit; end;
1027 // mask out unused bits
1028 if (b and $FE) = $FC then b := b and $01
1029 else if (b and $FC) = $F8 then b := b and $03
1030 else if (b and $F8) = $F0 then b := b and $07
1031 else if (b and $F0) = $E0 then b := b and $0F
1032 else if (b and $E0) = $C0 then b := b and $1F
1033 else exit; // invalid utf8
1035 // now continue
1036 while pos <= length(s) do
1037 begin
1038 c := Byte(s[pos]);
1039 if (c and $C0) <> $80 then break; // no more
1040 b := b shl 6;
1041 b := b or (c and $3F);
1042 Inc(pos);
1043 end;
1045 // done, try 1251
1046 for c := 128 to 255 do if uni2wint[c] = b then begin result := AnsiChar(c and $FF); exit; end;
1047 // alas
1048 end;
1051 function utf8to1251 (s: AnsiString): AnsiString;
1052 var
1053 pos: Integer;
1054 begin
1055 if not utf8Valid(s) then begin result := s; exit; end;
1056 pos := 1;
1057 while pos <= length(s) do
1058 begin
1059 if Byte(s[pos]) >= $80 then break;
1060 Inc(pos);
1061 end;
1062 if pos > length(s) then begin result := s; exit; end; // nothing to do here
1063 result := '';
1064 pos := 1;
1065 while pos <= length(s) do result := result+decodeUtf8Char(s, pos);
1066 end;
1069 // ////////////////////////////////////////////////////////////////////////// //
1070 // `pathname` will be modified if path is valid
1071 // `lastIsDir` should be `true` if we are searching for directory
1072 // nobody cares about shitdoze, so i'll use the same code path for it
1073 function findFileCI (var pathname: AnsiString; lastIsDir: Boolean=false): Boolean;
1074 var
1075 sr: TSearchRec;
1076 npt: AnsiString;
1077 newname: AnsiString = '';
1078 curname: AnsiString;
1079 wantdir: Boolean;
1080 attr: LongInt;
1081 foundher: Boolean;
1082 begin
1083 npt := pathname;
1084 result := (length(npt) > 0);
1085 if (length(npt) > 0) and ((npt[1] = '/') or (npt[1] = '\')) then newname := DirSep;
1086 while length(npt) > 0 do
1087 begin
1088 // remove trailing slashes
1089 while (length(npt) > 0) and ((npt[1] = '/') or (npt[1] = '\')) do Delete(npt, 1, 1);
1090 if length(npt) = 0 then break;
1091 // extract name
1092 curname := '';
1093 while (length(npt) > 0) and (npt[1] <> '/') and (npt[1] <> '\') do
1094 begin
1095 curname := curname+npt[1];
1096 Delete(npt, 1, 1);
1097 end;
1098 // remove trailing slashes again
1099 while (length(npt) > 0) and ((npt[1] = '/') or (npt[1] = '\')) do Delete(npt, 1, 1);
1100 wantdir := lastIsDir or (length(npt) > 0); // do we want directory here?
1101 //e_LogWritefln('npt=[%s]; newname=[%s]; curname=[%s]; wantdir=%d', [npt, newname, curname, Integer(wantdir)]);
1102 // try the easiest case first
1103 attr := FileGetAttr(newname+curname);
1104 if attr <> -1 then
1105 begin
1106 if wantdir = ((attr and faDirectory) <> 0) then
1107 begin
1108 // i found her!
1109 newname := newname+curname;
1110 if wantdir then newname := newname + DirSep;
1111 continue;
1112 end;
1113 end;
1114 //e_LogWritefLn('npt=[%s]; newname=[%s]; curname=[%s]; wantdir=%d', [npt, newname, curname, Integer(wantdir)]);
1115 // alas, either not found, or invalid attributes
1116 foundher := false;
1117 try
1118 if FindFirst(newname+'*', faAnyFile, sr) = 0 then
1119 repeat
1120 if (wantdir = ((sr.attr and faDirectory) <> 0)) and StrEquCI1251(sr.name, curname) then
1121 begin
1122 // i found her!
1123 newname := newname+sr.name;
1124 if wantdir then newname := newname + DirSep;
1125 foundher := true;
1126 break;
1127 end;
1128 until FindNext(sr) <> 0;
1129 finally
1130 FindClose(sr);
1131 end;
1132 if not foundher then begin newname := ''; result := false; break; end;
1133 end;
1134 if result then pathname := newname;
1135 end;
1138 (** Replace slashes to backslashes for DOS **)
1139 function FixFileName (filename: AnsiString): AnsiString;
1140 begin
1141 {$IFDEF GO32V2}
1142 Result := StringReplace(filename, '/', '\', [rfReplaceAll, rfIgnoreCase])
1143 {$ELSE}
1144 Result := filename
1145 {$ENDIF}
1146 end;
1149 const fileExtensions: array [0..6] of AnsiString = ('.wad', '.dfzip', '.dfwad', '.pk3', '.pak', '.zip', '.dfz');
1151 function findDiskWad (fname: AnsiString): AnsiString;
1152 var
1153 origExt: AnsiString = '';
1154 newExt: AnsiString = '';
1155 begin
1156 result := '';
1157 {$IFDEF GO32V2}
1158 // FIXIT: it didn't work under MSDOS for some reason, so i just cut extension replacement
1159 result := FixFileName(fname);
1160 {$ELSE}
1161 //e_LogWriteLn('findDiskWad00: fname=<' + fname + '>');
1162 if (findFileCI(fname)) then begin result := fname; exit; end;
1163 origExt := getFilenameExt(fname);
1164 fname := forceFilenameExt(fname, '');
1165 //e_LogWriteLn(' findDiskWad01: fname=<' + fname + '>; origExt=<' + origExt + '>');
1166 for newExt in fileExtensions do
1167 begin
1168 //e_LogWriteLn(' findDiskWad02: fname=<' + fname + '>; origExt=<' + origExt + '>; newExt=<' + newExt + '>');
1169 if (StrEquCI1251(newExt, origExt)) then
1170 begin
1171 //e_LogWriteLn(' SKIP');
1172 continue;
1173 end;
1174 result := fname+newExt;
1175 if (findFileCI(result)) then exit;
1176 end;
1177 result := '';
1178 {$ENDIF}
1179 end;
1181 function openDiskFileRO (pathname: AnsiString): TStream;
1182 begin
1183 pathname := FixFileName(pathname);
1184 if not findFileCI(pathname) then raise Exception.Create('can''t open file "'+pathname+'"');
1185 result := TFileStream.Create(pathname, fmOpenRead or {fmShareDenyWrite}fmShareDenyNone);
1186 end;
1188 function createDiskFile (pathname: AnsiString): TStream;
1189 var
1190 path: AnsiString;
1191 begin
1192 pathname := FixFileName(pathname);
1193 path := ExtractFilePath(pathname);
1194 if length(path) > 0 then
1195 begin
1196 if not findFileCI(path, true) then raise Exception.Create('can''t create file "'+pathname+'"');
1197 end;
1198 result := TFileStream.Create(path+ExtractFileName(pathname), fmCreate);
1199 end;
1202 procedure writeIntegerLE (st: TStream; vp: Pointer; size: Integer);
1203 {$IFDEF ENDIAN_LITTLE}
1204 begin
1205 st.writeBuffer(vp^, size);
1206 end;
1207 {$ELSE}
1208 var
1209 p: PByte;
1210 begin
1211 p := PByte(vp)+size-1;
1212 while size > 0 do
1213 begin
1214 st.writeBuffer(p^, 1);
1215 Dec(size);
1216 Dec(p);
1217 end;
1218 end;
1219 {$ENDIF}
1221 procedure writeIntegerBE (st: TStream; vp: Pointer; size: Integer);
1222 {$IFDEF ENDIAN_LITTLE}
1223 var
1224 p: PByte;
1225 begin
1226 p := PByte(vp)+size-1;
1227 while size > 0 do
1228 begin
1229 st.writeBuffer(p^, 1);
1230 Dec(size);
1231 Dec(p);
1232 end;
1233 end;
1234 {$ELSE}
1235 begin
1236 st.writeBuffer(vp^, size);
1237 end;
1238 {$ENDIF}
1240 procedure writeSign (st: TStream; const sign: AnsiString);
1241 begin
1242 if (Length(sign) > 0) then st.WriteBuffer(sign[1], Length(sign));
1243 end;
1245 function checkSign (st: TStream; const sign: AnsiString): Boolean;
1246 var
1247 buf: packed array[0..7] of AnsiChar;
1248 f: Integer;
1249 begin
1250 result := false;
1251 if (Length(sign) > 0) then
1252 begin
1253 if (Length(sign) <= 8) then
1254 begin
1255 st.ReadBuffer(buf[0], Length(sign));
1256 for f := 1 to Length(sign) do if (buf[f-1] <> sign[f]) then exit;
1257 end
1258 else
1259 begin
1260 for f := 1 to Length(sign) do
1261 begin
1262 st.ReadBuffer(buf[0], 1);
1263 if (buf[0] <> sign[f]) then exit;
1264 end;
1265 end;
1266 end;
1267 result := true;
1268 end;
1270 procedure writeInt (st: TStream; v: Byte); overload; begin writeIntegerLE(st, @v, 1); end;
1271 procedure writeInt (st: TStream; v: ShortInt); overload; begin writeIntegerLE(st, @v, 1); end;
1272 procedure writeInt (st: TStream; v: Word); overload; begin writeIntegerLE(st, @v, 2); end;
1273 procedure writeInt (st: TStream; v: SmallInt); overload; begin writeIntegerLE(st, @v, 2); end;
1274 procedure writeInt (st: TStream; v: LongWord); overload; begin writeIntegerLE(st, @v, 4); end;
1275 procedure writeInt (st: TStream; v: LongInt); overload; begin writeIntegerLE(st, @v, 4); end;
1276 procedure writeInt (st: TStream; v: Int64); overload; begin writeIntegerLE(st, @v, 8); end;
1277 procedure writeInt (st: TStream; v: UInt64); overload; begin writeIntegerLE(st, @v, 8); end;
1279 procedure writeIntBE (st: TStream; v: Byte); overload; begin writeIntegerBE(st, @v, 1); end;
1280 procedure writeIntBE (st: TStream; v: ShortInt); overload; begin writeIntegerBE(st, @v, 1); end;
1281 procedure writeIntBE (st: TStream; v: Word); overload; begin writeIntegerBE(st, @v, 2); end;
1282 procedure writeIntBE (st: TStream; v: SmallInt); overload; begin writeIntegerBE(st, @v, 2); end;
1283 procedure writeIntBE (st: TStream; v: LongWord); overload; begin writeIntegerBE(st, @v, 4); end;
1284 procedure writeIntBE (st: TStream; v: LongInt); overload; begin writeIntegerBE(st, @v, 4); end;
1285 procedure writeIntBE (st: TStream; v: Int64); overload; begin writeIntegerBE(st, @v, 8); end;
1286 procedure writeIntBE (st: TStream; v: UInt64); overload; begin writeIntegerBE(st, @v, 8); end;
1288 procedure writeBool (st: TStream; b: Boolean); begin writeInt(st, Byte(b)); end;
1289 function readBool (st: TStream): Boolean; begin result := (readByte(st) <> 0); end;
1292 procedure writeStr (st: TStream; const str: AnsiString; maxlen: LongWord=65535);
1293 begin
1294 if (Length(str) > maxlen) then raise XStreamError.Create('string too long');
1295 if (maxlen <= 65535) then writeInt(st, Word(Length(str))) else writeInt(st, LongWord(Length(str)));
1296 if (Length(str) > 0) then st.WriteBuffer(str[1], Length(str));
1297 end;
1299 function readStr (st: TStream; maxlen: LongWord=65535): AnsiString;
1300 var
1301 len: Integer;
1302 begin
1303 result := '';
1304 if (maxlen <= 65535) then len := readWord(st) else len := Integer(readLongWord(st));
1305 if (len < 0) or (len > maxlen) then raise XStreamError.Create('string too long');
1306 if (len > 0) then
1307 begin
1308 SetLength(result, len);
1309 st.ReadBuffer(result[1], len);
1310 end;
1311 end;
1314 procedure readIntegerLE (st: TStream; vp: Pointer; size: Integer);
1315 {$IFDEF ENDIAN_LITTLE}
1316 begin
1317 st.readBuffer(vp^, size);
1318 end;
1319 {$ELSE}
1320 var
1321 p: PByte;
1322 begin
1323 p := PByte(vp)+size-1;
1324 while size > 0 do
1325 begin
1326 st.readBuffer(p^, 1);
1327 Dec(size);
1328 Dec(p);
1329 end;
1330 end;
1331 {$ENDIF}
1333 procedure readIntegerBE (st: TStream; vp: Pointer; size: Integer);
1334 {$IFDEF ENDIAN_LITTLE}
1335 var
1336 p: PByte;
1337 begin
1338 p := PByte(vp)+size-1;
1339 while size > 0 do
1340 begin
1341 st.readBuffer(p^, 1);
1342 Dec(size);
1343 Dec(p);
1344 end;
1345 end;
1346 {$ELSE}
1347 begin
1348 st.readBuffer(vp^, size);
1349 end;
1350 {$ENDIF}
1352 function readByte (st: TStream): Byte; begin readIntegerLE(st, @result, 1); end;
1353 function readShortInt (st: TStream): ShortInt; begin readIntegerLE(st, @result, 1); end;
1354 function readWord (st: TStream): Word; begin readIntegerLE(st, @result, 2); end;
1355 function readSmallInt (st: TStream): SmallInt; begin readIntegerLE(st, @result, 2); end;
1356 function readLongWord (st: TStream): LongWord; begin readIntegerLE(st, @result, 4); end;
1357 function readLongInt (st: TStream): LongInt; begin readIntegerLE(st, @result, 4); end;
1358 function readInt64 (st: TStream): Int64; begin readIntegerLE(st, @result, 8); end;
1359 function readUInt64 (st: TStream): UInt64; begin readIntegerLE(st, @result, 8); end;
1361 function readByteBE (st: TStream): Byte; begin readIntegerBE(st, @result, 1); end;
1362 function readShortIntBE (st: TStream): ShortInt; begin readIntegerBE(st, @result, 1); end;
1363 function readWordBE (st: TStream): Word; begin readIntegerBE(st, @result, 2); end;
1364 function readSmallIntBE (st: TStream): SmallInt; begin readIntegerBE(st, @result, 2); end;
1365 function readLongWordBE (st: TStream): LongWord; begin readIntegerBE(st, @result, 4); end;
1366 function readLongIntBE (st: TStream): LongInt; begin readIntegerBE(st, @result, 4); end;
1367 function readInt64BE (st: TStream): Int64; begin readIntegerBE(st, @result, 8); end;
1368 function readUInt64BE (st: TStream): UInt64; begin readIntegerBE(st, @result, 8); end;
1371 // ////////////////////////////////////////////////////////////////////////// //
1372 function nmin (a, b: Byte): Byte; inline; overload; begin if (a < b) then result := a else result := b; end;
1373 function nmin (a, b: ShortInt): ShortInt; inline; overload; begin if (a < b) then result := a else result := b; end;
1374 function nmin (a, b: Word): Word; inline; overload; begin if (a < b) then result := a else result := b; end;
1375 function nmin (a, b: SmallInt): SmallInt; inline; overload; begin if (a < b) then result := a else result := b; end;
1376 function nmin (a, b: LongWord): LongWord; inline; overload; begin if (a < b) then result := a else result := b; end;
1377 function nmin (a, b: LongInt): LongInt; inline; overload; begin if (a < b) then result := a else result := b; end;
1378 function nmin (a, b: Int64): Int64; inline; overload; begin if (a < b) then result := a else result := b; end;
1379 function nmin (a, b: UInt64): UInt64; inline; overload; begin if (a < b) then result := a else result := b; end;
1380 function nmin (a, b: Single): Single; inline; overload; begin if (a < b) then result := a else result := b; end;
1381 function nmin (a, b: Double): Double; inline; overload; begin if (a < b) then result := a else result := b; end;
1382 {$IF DEFINED(CPU386) OR DEFINED(CPUAMD64)}
1383 function nmin (a, b: Extended): Extended; inline; overload; begin if (a < b) then result := a else result := b; end;
1384 {$ENDIF}
1386 function nmax (a, b: Byte): Byte; inline; overload; begin if (a > b) then result := a else result := b; end;
1387 function nmax (a, b: ShortInt): ShortInt; inline; overload; begin if (a > b) then result := a else result := b; end;
1388 function nmax (a, b: Word): Word; inline; overload; begin if (a > b) then result := a else result := b; end;
1389 function nmax (a, b: SmallInt): SmallInt; inline; overload; begin if (a > b) then result := a else result := b; end;
1390 function nmax (a, b: LongWord): LongWord; inline; overload; begin if (a > b) then result := a else result := b; end;
1391 function nmax (a, b: LongInt): LongInt; inline; overload; begin if (a > b) then result := a else result := b; end;
1392 function nmax (a, b: Int64): Int64; inline; overload; begin if (a > b) then result := a else result := b; end;
1393 function nmax (a, b: UInt64): UInt64; inline; overload; begin if (a > b) then result := a else result := b; end;
1394 function nmax (a, b: Single): Single; inline; overload; begin if (a > b) then result := a else result := b; end;
1395 function nmax (a, b: Double): Double; inline; overload; begin if (a > b) then result := a else result := b; end;
1396 {$IF DEFINED(CPU386) OR DEFINED(CPUAMD64)}
1397 function nmax (a, b: Extended): Extended; inline; overload; begin if (a > b) then result := a else result := b; end;
1398 {$ENDIF}
1400 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;
1401 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;
1402 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;
1403 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;
1404 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;
1405 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;
1406 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;
1407 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;
1408 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;
1409 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;
1410 {$IF DEFINED(CPU386) OR DEFINED(CPUAMD64)}
1411 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;
1412 {$ENDIF}
1414 // ////////////////////////////////////////////////////////////////////////// //
1415 {$IFDEF WINDOWS}
1416 function snprintf (buf: PAnsiChar; bufsize: SizeUInt; const fmt: PAnsiChar): SizeUInt; cdecl; varargs; external 'msvcrt.dll' name '_snprintf';
1417 {$ELSE}
1418 {$IFDEF GO32V2}
1419 function snprintf (buf: PAnsiChar; bufsize: SizeUInt; const fmt: PAnsiChar): SizeUInt; cdecl; varargs; external;
1420 {$ELSE}
1421 function snprintf (buf: PAnsiChar; bufsize: SizeUInt; const fmt: PAnsiChar): SizeUInt; cdecl; varargs; external 'libc' name 'snprintf';
1422 {$ENDIF}
1423 {$ENDIF}
1426 (*
1427 procedure conwriter (constref buf; len: SizeUInt);
1428 var
1429 ss: ShortString;
1430 slen: Integer;
1431 b: PByte;
1432 begin
1433 if (len < 1) then exit;
1434 b := PByte(@buf);
1435 while (len > 0) do
1436 begin
1437 if (len > 255) then slen := 255 else slen := Integer(len);
1438 Move(b^, ss[1], len);
1439 ss[0] := AnsiChar(slen);
1440 write(ss);
1441 b += slen;
1442 len -= slen;
1443 end;
1444 end;
1445 *)
1448 function formatstrf (const fmt: AnsiString; const args: array of const; writerCB: TFormatStrFCallback=nil): AnsiString;
1449 const
1450 PadSpaces: AnsiString = ' ';
1451 PadZeroes: AnsiString = '00000000000000000000000000000000000000000000000000000000000000000000000';
1452 var
1453 curarg: Integer = 0; // current arg in `args`
1454 sign, fmtch: AnsiChar;
1455 zeropad: Boolean;
1456 width, prec: Integer; // width and precision
1457 spos, epos: Integer;
1458 ch: AnsiChar;
1459 strbuf: array[0..256] of AnsiChar;
1460 strblen: SizeUInt;
1461 fmtbuf: array[0..256] of AnsiChar;
1462 fmtblen: Integer;
1463 pclen: Integer;
1464 pc: PAnsiChar;
1465 ccname: ShortString;
1467 procedure writer (constref buf; len: SizeUInt);
1468 var
1469 ss: ShortString;
1470 slen: Integer;
1471 b: PByte;
1472 begin
1473 if (len < 1) then exit;
1474 b := PByte(@buf);
1475 if assigned(writerCB) then
1476 begin
1477 writerCB(b^, len);
1478 end
1479 else
1480 begin
1481 while (len > 0) do
1482 begin
1483 if (len > 255) then slen := 255 else slen := Integer(len);
1484 Move(b^, ss[1], slen);
1485 ss[0] := AnsiChar(slen);
1486 result += ss;
1487 b += slen;
1488 len -= slen;
1489 end;
1490 end;
1491 end;
1493 procedure xwrite (const s: AnsiString);
1494 begin
1495 if (Length(s) > 0) then writer(PAnsiChar(s)^, Length(s));
1496 end;
1498 procedure putFmtChar (ch: AnsiChar);
1499 begin
1500 fmtbuf[fmtblen] := ch;
1501 Inc(fmtblen);
1502 end;
1504 procedure putFmtInt (n: Integer);
1505 var
1506 len: SizeUInt;
1507 begin
1508 len := snprintf(@fmtbuf[fmtblen], Length(fmtbuf)-fmtblen, '%d', n);
1509 if (len > 0) then Inc(fmtblen, len);
1510 end;
1512 procedure buildCFormat (const pfx: AnsiString='');
1513 var
1514 f: Integer;
1515 begin
1516 fmtblen := 0;
1517 for f := 1 to Length(pfx) do putFmtChar(pfx[f]);
1518 putFmtChar('%');
1519 if (sign <> ' ') then putFmtChar(sign);
1520 if (width >= 0) then
1521 begin
1522 if (zeropad) then putFmtChar('0');
1523 putFmtInt(width);
1524 if (prec >= 0) then
1525 begin
1526 putFmtChar('.');
1527 putFmtInt(prec);
1528 end;
1529 end;
1530 putFmtChar(fmtch);
1531 fmtbuf[fmtblen] := #0;
1532 end;
1534 procedure writeStrBuf ();
1535 begin
1536 if (strblen > 0) then writer(strbuf, strblen);
1537 end;
1539 function i642str (n: Int64; hex: Boolean; hexup: Boolean): PAnsiChar;
1540 var
1541 neg: Boolean;
1542 xpos: Integer;
1543 begin
1544 if (n = $8000000000000000) then
1545 begin
1546 if hex then snprintf(@strbuf[0], Length(strbuf), '-8000000000000000')
1547 else snprintf(@strbuf[0], Length(strbuf), '-9223372036854775808');
1548 result := @strbuf[0];
1549 end
1550 else
1551 begin
1552 neg := (n < 0);
1553 if neg then n := -n;
1554 xpos := High(strbuf);
1555 strbuf[xpos] := #0; Dec(xpos);
1556 repeat
1557 if not hex then
1558 begin
1559 strbuf[xpos] := AnsiChar((n mod 10)+48);
1560 Dec(xpos);
1561 n := n div 10;
1562 end
1563 else
1564 begin
1565 if (n mod 16 > 9) then
1566 begin
1567 strbuf[xpos] := AnsiChar((n mod 16)+48+7);
1568 if not hexup then Inc(strbuf[xpos], 32);
1569 end
1570 else strbuf[xpos] := AnsiChar((n mod 16)+48);
1571 Dec(xpos);
1572 n := n div 16;
1573 end;
1574 until (n = 0);
1575 if neg then begin strbuf[xpos] := '-'; Dec(xpos); end;
1576 result := @strbuf[xpos+1];
1577 end;
1578 end;
1580 function ui642str (n: UInt64; hex: Boolean; hexup: Boolean): PAnsiChar;
1581 var
1582 xpos: Integer;
1583 begin
1584 xpos := High(strbuf);
1585 strbuf[xpos] := #0; Dec(xpos);
1586 repeat
1587 if not hex then
1588 begin
1589 strbuf[xpos] := AnsiChar((n mod 10)+48);
1590 Dec(xpos);
1591 n := n div 10;
1592 end
1593 else
1594 begin
1595 if (n mod 16 > 9) then
1596 begin
1597 strbuf[xpos] := AnsiChar((n mod 16)+48+7);
1598 if not hexup then Inc(strbuf[xpos], 32);
1599 end
1600 else strbuf[xpos] := AnsiChar((n mod 16)+48);
1601 Dec(xpos);
1602 n := n div 16;
1603 end;
1604 until (n = 0);
1605 result := @strbuf[xpos+1];
1606 end;
1608 procedure indent (len: Integer);
1609 var
1610 ilen: Integer;
1611 begin
1612 while (len > 0) do
1613 begin
1614 if (len > Length(PadSpaces)) then ilen := Length(PadSpaces) else ilen := len;
1615 writer(PAnsiChar(PadSpaces)^, ilen);
1616 Dec(len, ilen);
1617 end;
1618 end;
1620 procedure indent0 (len: Integer);
1621 var
1622 ilen: Integer;
1623 begin
1624 while (len > 0) do
1625 begin
1626 if (len > Length(PadZeroes)) then ilen := Length(PadZeroes) else ilen := len;
1627 writer(PAnsiChar(PadZeroes)^, ilen);
1628 Dec(len, ilen);
1629 end;
1630 end;
1632 begin
1633 result := '';
1634 spos := 1;
1635 while (spos <= Length(fmt)) do
1636 begin
1637 // print literal part
1638 epos := spos;
1639 while (epos <= Length(fmt)) and (fmt[epos] <> '%') do Inc(epos);
1640 // output literal part
1641 if (epos > spos) then
1642 begin
1643 if (epos > Length(fmt)) then
1644 begin
1645 writer((PAnsiChar(fmt)+spos-1)^, epos-spos);
1646 break;
1647 end;
1648 if (epos+1 > Length(fmt)) then Inc(epos) // last percent, output literally
1649 else if (fmt[epos+1] = '%') then // special case
1650 begin
1651 Inc(epos);
1652 writer((PAnsiChar(fmt)+spos-1)^, epos-spos);
1653 spos := epos+1;
1654 end
1655 else
1656 begin
1657 writer((PAnsiChar(fmt)+spos-1)^, epos-spos);
1658 spos := epos;
1659 end;
1660 continue;
1661 end;
1662 // check if we have argument for this format string
1663 if (curarg > High(args)) then
1664 begin
1665 xwrite('<OUT OF ARGS>');
1666 writer((PAnsiChar(fmt)+spos-1)^, Length(fmt)-spos+1);
1667 break;
1668 end;
1669 // skip percent
1670 if (spos+1 > Length(fmt)) then break; // oops
1671 assert(fmt[spos] = '%');
1672 Inc(spos);
1673 // parse format; check for sign
1674 if (fmt[spos] = '-') then begin sign := '-'; Inc(spos); end
1675 else if (fmt[spos] = '+') then begin sign := '+'; Inc(spos); end
1676 else sign := ' ';
1677 // parse width
1678 if (spos > Length(fmt)) then begin xwrite('<INVALID FORMAT>'); break; end;
1679 if (sign <> ' ') or ((fmt[spos] >= '0') and (fmt[spos] <= '9')) then
1680 begin
1681 if (fmt[spos] < '0') or (fmt[spos] > '9') then begin xwrite('<INVALID FORMAT>'); writer((PAnsiChar(fmt)+spos-1)^, Length(fmt)-spos+1); break; end;
1682 zeropad := (fmt[spos] = '0');
1683 width := 0;
1684 while (spos <= Length(fmt)) do
1685 begin
1686 ch := fmt[spos];
1687 if (ch < '0') or (ch > '9') then break;
1688 width := width*10+Integer(ch)-48;
1689 Inc(spos);
1690 end;
1691 end
1692 else
1693 begin
1694 width := -1;
1695 zeropad := false;
1696 end;
1697 // parse precision
1698 prec := -1;
1699 if (spos <= Length(fmt)) and (fmt[spos] = '.') then
1700 begin
1701 Inc(spos);
1702 if (spos > Length(fmt)) then begin xwrite('<INVALID FORMAT>'); break; end;
1703 if (fmt[spos] < '0') or (fmt[spos] > '9') then begin xwrite('<INVALID FORMAT>'); writer((PAnsiChar(fmt)+spos-1)^, Length(fmt)-spos+1); break; end;
1704 prec := 0;
1705 while (spos <= Length(fmt)) do
1706 begin
1707 ch := fmt[spos];
1708 if (ch < '0') or (ch > '9') then break;
1709 prec := prec*10+Integer(ch)-48;
1710 Inc(spos);
1711 end;
1712 end;
1713 // get format char
1714 if (spos > Length(fmt)) then begin xwrite('<INVALID FORMAT>'); break; end;
1715 fmtch := fmt[spos];
1716 Inc(spos);
1717 // done parsing format, check for valid format chars
1718 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;
1719 // now write formatted string
1720 case args[curarg].VType of
1721 vtInteger: // args[curarg].VInteger
1722 begin
1723 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;
1724 if (fmtch = 's') then fmtch := 'd';
1725 buildCFormat();
1726 strblen := snprintf(@strbuf[0], Length(strbuf), @fmtbuf[0], args[curarg].VInteger);
1727 writeStrBuf();
1728 end;
1729 vtBoolean: // args[curarg].VBoolean
1730 case fmtch of
1731 's':
1732 begin
1733 buildCFormat();
1734 if args[curarg].VBoolean then strblen := snprintf(@strbuf[0], Length(strbuf), @fmtbuf[0], 'true')
1735 else strblen := snprintf(@strbuf[0], Length(strbuf), @fmtbuf[0], 'false');
1736 writeStrBuf();
1737 end;
1738 'c':
1739 begin
1740 buildCFormat();
1741 if args[curarg].VBoolean then strblen := snprintf(@strbuf[0], Length(strbuf), @fmtbuf[0], AnsiChar('t'))
1742 else strblen := snprintf(@strbuf[0], Length(strbuf), @fmtbuf[0], AnsiChar('f'));
1743 writeStrBuf();
1744 end;
1745 'u', 'd', 'x', 'X':
1746 begin
1747 buildCFormat();
1748 strblen := snprintf(@strbuf[0], Length(strbuf), @fmtbuf[0], Integer(args[curarg].VBoolean));
1749 writeStrBuf();
1750 end;
1751 else
1752 begin
1753 xwrite('<INVALID FORMAT CHAR>');
1754 writer((PAnsiChar(fmt)+spos-1)^, Length(fmt)-spos+1);
1755 break;
1756 end;
1757 end;
1758 vtChar: // args[curarg].VChar
1759 case fmtch of
1760 's', 'c':
1761 begin
1762 fmtch := 'c';
1763 buildCFormat();
1764 strblen := snprintf(@strbuf[0], Length(strbuf), @fmtbuf[0], args[curarg].VChar);
1765 writeStrBuf();
1766 end;
1767 'u', 'd', 'x', 'X':
1768 begin
1769 buildCFormat();
1770 strblen := snprintf(@strbuf[0], Length(strbuf), @fmtbuf[0], Integer(args[curarg].VChar));
1771 writeStrBuf();
1772 end;
1773 else
1774 begin
1775 xwrite('<INVALID FORMAT CHAR>');
1776 writer((PAnsiChar(fmt)+spos-1)^, Length(fmt)-spos+1);
1777 break;
1778 end;
1779 end;
1780 //vtWideChar: begin end; // args[curarg].VWideChar (WideChar)
1781 vtExtended: // args[curarg].VExtended^
1782 case fmtch of
1783 's', 'g':
1784 begin
1785 fmtch := 'g';
1786 buildCFormat();
1787 strblen := snprintf(@strbuf[0], Length(strbuf), @fmtbuf[0], Double(args[curarg].VExtended^));
1788 writeStrBuf();
1789 end;
1790 'f':
1791 begin
1792 buildCFormat();
1793 strblen := snprintf(@strbuf[0], Length(strbuf), @fmtbuf[0], Double(args[curarg].VExtended^));
1794 writeStrBuf();
1795 end;
1796 'd':
1797 begin
1798 buildCFormat();
1799 strblen := snprintf(@strbuf[0], Length(strbuf), @fmtbuf[0], Integer(trunc(args[curarg].VExtended^)));
1800 writeStrBuf();
1801 end;
1802 'u', 'x', 'X':
1803 begin
1804 buildCFormat();
1805 strblen := snprintf(@strbuf[0], Length(strbuf), @fmtbuf[0], LongWord(trunc(args[curarg].VExtended^)));
1806 writeStrBuf();
1807 end;
1808 else
1809 begin
1810 xwrite('<INVALID FORMAT CHAR>');
1811 writer((PAnsiChar(fmt)+spos-1)^, Length(fmt)-spos+1);
1812 break;
1813 end;
1814 end;
1815 vtString: // args[curarg].VString^ (PShortString)
1816 begin
1817 if (sign <> '-') then indent(width-Length(args[curarg].VString^));
1818 writer(args[curarg].VString^[1], Length(args[curarg].VString^));
1819 if (sign = '-') then indent(width-Length(args[curarg].VString^));
1820 end;
1821 vtPointer: // args[curarg].VPointer
1822 case fmtch of
1823 's':
1824 begin
1825 fmtch := 'x';
1826 if (width < 8) then width := 8;
1827 zeropad := true;
1828 buildCFormat('0x');
1829 strblen := snprintf(@strbuf[0], Length(strbuf), @fmtbuf[0], PtrUInt(args[curarg].VPointer));
1830 writeStrBuf();
1831 end;
1832 'u', 'd', 'x', 'p', 'X':
1833 begin
1834 if (fmtch = 'p') then fmtch := 'x';
1835 if (width < 8) then width := 8;
1836 zeropad := true;
1837 buildCFormat('0x');
1838 strblen := snprintf(@strbuf[0], Length(strbuf), @fmtbuf[0], PtrUInt(args[curarg].VPointer));
1839 writeStrBuf();
1840 end;
1841 else
1842 begin
1843 xwrite('<INVALID FORMAT CHAR>');
1844 writer((PAnsiChar(fmt)+spos-1)^, Length(fmt)-spos+1);
1845 break;
1846 end;
1847 end;
1848 vtPChar: // args[curarg].VPChar
1849 if (args[curarg].VPChar = nil) then
1850 begin
1851 if (sign <> '-') then indent(width-3);
1852 xwrite('nil');
1853 if (sign = '-') then indent(width-3);
1854 end
1855 else
1856 begin
1857 pclen := 0;
1858 while (args[curarg].VPChar[pclen] <> #0) do Inc(pclen);
1859 if (sign <> '-') then indent(width-pclen);
1860 writer(args[curarg].VPChar^, pclen);
1861 if (sign = '-') then indent(width-pclen);
1862 end;
1863 vtObject: // args[curarg].VObject.Classname (TObject)
1864 begin
1865 if (args[curarg].VObject <> nil) then ccname := args[curarg].VObject.Classname else ccname := '<nil>';
1866 if (sign <> '-') then indent(width-Length(ccname));
1867 xwrite(ccname);
1868 if (sign = '-') then indent(width-Length(ccname));
1869 end;
1870 vtClass: // args[curarg].VClass.Classname (TClass)
1871 begin
1872 if (args[curarg].VClass <> nil) then ccname := args[curarg].VClass.Classname else ccname := '<nil>';
1873 if (sign <> '-') then indent(width-Length(ccname));
1874 xwrite(ccname);
1875 if (sign = '-') then indent(width-Length(ccname));
1876 end;
1877 //vtPWideChar: begin end; // args[curarg].VPWideChar (PWideChar)
1878 vtAnsiString: // AnsiString(args[curarg].VAnsiString) (Pointer)
1879 begin
1880 if (sign <> '-') then indent(width-Length(AnsiString(args[curarg].VAnsiString)));
1881 xwrite(AnsiString(args[curarg].VAnsiString));
1882 if (sign = '-') then indent(width-Length(AnsiString(args[curarg].VAnsiString)));
1883 end;
1884 //vtCurrency: begin end; // args[curarg].VCurrency (PCurrency)
1885 //vtVariant: begin end; // args[curarg].VVariant^ (PVariant)
1886 //vtInterface: begin end; // args[curarg].VInterface (Pointer);
1887 //vtWideString: begin end; // args[curarg].VWideString (Pointer);
1888 vtInt64: // args[curarg].VInt64^ (PInt64)
1889 begin
1890 case fmtch of
1891 's','d','u': pc := i642str(args[curarg].VInt64^, false, false);
1892 'x': pc := i642str(args[curarg].VInt64^, true, false);
1893 'X': pc := i642str(args[curarg].VInt64^, true, true);
1894 else begin xwrite('<INVALID FORMAT CHAR>'); writer((PAnsiChar(fmt)+spos-1)^, Length(fmt)-spos+1); break; end;
1895 end;
1896 pclen := 0;
1897 while (pc[pclen] <> #0) do Inc(pclen);
1898 if (sign <> '-') and (width > pclen) then
1899 begin
1900 if zeropad then
1901 begin
1902 if (pc[0] = '-') or (pc[0] = '+') then
1903 begin
1904 writer(pc^, 1);
1905 indent0(width-pclen-1);
1906 Inc(pc);
1907 Dec(pclen);
1908 end
1909 else
1910 begin
1911 indent0(width-pclen);
1912 end;
1913 end
1914 else
1915 begin
1916 indent(width-pclen);
1917 end;
1918 end;
1919 writer(pc^, pclen);
1920 if (sign = '-') then indent(width-pclen);
1921 end;
1922 vtQWord: // args[curarg].VQWord^ (PQWord)
1923 begin
1924 case fmtch of
1925 's','d','u': pc := ui642str(args[curarg].VInt64^, false, false);
1926 'x': pc := ui642str(args[curarg].VInt64^, true, false);
1927 'X': pc := ui642str(args[curarg].VInt64^, true, true);
1928 else begin xwrite('<INVALID FORMAT CHAR>'); writer((PAnsiChar(fmt)+spos-1)^, Length(fmt)-spos+1); break; end;
1929 end;
1930 pclen := 0;
1931 while (pc[pclen] <> #0) do Inc(pclen);
1932 if (sign <> '-') then begin if zeropad then indent0(width-pclen) else indent(width-pclen); end;
1933 writer(pc^, pclen);
1934 if (sign = '-') then indent(width-pclen);
1935 end;
1936 else
1937 begin
1938 xwrite('<INVALID TYPE>');
1939 writer((PAnsiChar(fmt)+spos-1)^, Length(fmt)-spos+1);
1940 break;
1941 end;
1942 end;
1943 Inc(curarg);
1944 end;
1945 end;
1948 (*
1949 var
1950 ss: ShortString;
1951 ls: AnsiString;
1952 i64: Int64 = -$A000000000;
1953 ui64: UInt64 = $A000000000;
1954 begin
1955 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']);
1956 writef(conwriter, 'test float:<%s;%u;%f;%g>'#10, [666.6942, 666.6942, 666.6942, 666.6942]);
1957 ss := 'fuckit';
1958 ls := 'FUCKIT';
1959 writef(conwriter, 'test ss:<%5s;%040s>'#10, [ss, ss]);
1960 writef(conwriter, 'test ls:<%5s;%040s>'#10, [ls, ls]);
1961 writef(conwriter, 'test pointer:<%s;%x;%p>'#10, [@ss, @ss, @ss]);
1962 writef(conwriter, 'test i64:<%s;%x;%015d;%u;%X>'#10, [i64, i64, i64, i64, i64]);
1963 writef(conwriter, 'test ui64:<%s;%x;%15d;%015u;%X>'#10, [ui64, ui64, ui64, ui64, ui64]);
1964 *)
1965 end.