DEADSOFTWARE

utils: added endian-neutral integer i/o functions
[d2df-sdl.git] / src / shared / utils.pas
1 {$MODE DELPHI}
2 unit utils;
4 interface
6 uses
7 SysUtils, Classes;
10 // does filename have one of ".wad", ".pk3", ".zip" extensions?
11 function hasWadExtension (fn: AnsiString): Boolean;
13 // does filepath have ".XXX:\" in it?
14 function isWadPath (fn: AnsiString): Boolean;
16 // adds ".wad" extension if filename doesn't have one of ".wad", ".pk3", ".zip"
17 function addWadExtension (fn: AnsiString): AnsiString;
19 // convert number to strig with nice commas
20 function Int64ToStrComma (i: Int64): AnsiString;
22 function UpCase1251 (ch: Char): Char;
24 // `true` if strings are equal; ignoring case for cp1251
25 function StrEquCI1251 (const s0, s1: AnsiString): Boolean;
27 function utf8Valid (const s: AnsiString): Boolean;
29 function utf8to1251 (s: AnsiString): AnsiString;
31 // `pathname` will be modified if path is valid
32 // `lastIsDir` should be `true` if we are searching for directory
33 // nobody cares about shitdoze, so i'll use the same code path for it
34 function findFileCI (var pathname: AnsiString; lastIsDir: Boolean=false): Boolean;
36 // they throws
37 function openDiskFileRO (pathname: AnsiString): TStream;
38 function createDiskFile (pathname: AnsiString): TStream;
40 // little endian
41 procedure writeInt (st: TStream; v: Byte); overload;
42 procedure writeInt (st: TStream; v: ShortInt); overload;
43 procedure writeInt (st: TStream; v: Word); overload;
44 procedure writeInt (st: TStream; v: SmallInt); overload;
45 procedure writeInt (st: TStream; v: LongWord); overload;
46 procedure writeInt (st: TStream; v: LongInt); overload;
47 procedure writeInt (st: TStream; v: Int64); overload;
48 procedure writeInt (st: TStream; v: UInt64); overload;
50 function readByte (st: TStream): Byte;
51 function readShortInt (st: TStream): ShortInt;
52 function readWord (st: TStream): Word;
53 function readSmallInt (st: TStream): SmallInt;
54 function readLongWord (st: TStream): LongWord;
55 function readLongInt (st: TStream): LongInt;
56 function readInt64 (st: TStream): Int64;
57 function readUInt64 (st: TStream): UInt64;
59 // big endian
60 procedure writeIntBE (st: TStream; v: Byte); overload;
61 procedure writeIntBE (st: TStream; v: ShortInt); overload;
62 procedure writeIntBE (st: TStream; v: Word); overload;
63 procedure writeIntBE (st: TStream; v: SmallInt); overload;
64 procedure writeIntBE (st: TStream; v: LongWord); overload;
65 procedure writeIntBE (st: TStream; v: LongInt); overload;
66 procedure writeIntBE (st: TStream; v: Int64); overload;
67 procedure writeIntBE (st: TStream; v: UInt64); overload;
69 function readByteBE (st: TStream): Byte;
70 function readShortIntBE (st: TStream): ShortInt;
71 function readWordBE (st: TStream): Word;
72 function readSmallIntBE (st: TStream): SmallInt;
73 function readLongWordBE (st: TStream): LongWord;
74 function readLongIntBE (st: TStream): LongInt;
75 function readInt64BE (st: TStream): Int64;
76 function readUInt64BE (st: TStream): UInt64;
79 implementation
82 function hasWadExtension (fn: AnsiString): Boolean;
83 begin
84 fn := ExtractFileExt(fn);
85 result := StrEquCI1251(fn, '.wad') or StrEquCI1251(fn, '.pk3') or StrEquCI1251(fn, '.zip');
86 end;
89 function addWadExtension (fn: AnsiString): AnsiString;
90 begin
91 result := fn;
92 if not hasWadExtension(result) then result := result+'.wad';
93 end;
96 function isWadPath (fn: AnsiString): Boolean;
97 var
98 p: Integer;
99 s: AnsiString;
100 begin
101 result := false;
102 while true do
103 begin
104 p := Pos(':', fn);
105 if (p = 0) or (length(fn)-p < 1) then break;
106 if (p-4 > 1) and (fn[p-4] = '.') and ((fn[p+1] = '\') or (fn[p+1] = '/')) then
107 begin
108 s := Copy(fn, p-4, 4);
109 if StrEquCI1251(s, '.wad') or StrEquCI1251(s, '.pk3') or StrEquCI1251(s, '.zip') then
110 begin
111 result := true;
112 exit;
113 end;
114 end;
115 Delete(fn, 1, p);
116 end;
117 end;
120 function Int64ToStrComma (i: Int64): AnsiString;
121 var
122 f: Integer;
123 begin
124 Str(i, result);
125 f := Length(result)+1;
126 while f > 4 do
127 begin
128 Dec(f, 3); Insert(',', result, f);
129 end;
130 end;
133 function UpCase1251 (ch: Char): Char;
134 begin
135 if ch < #128 then
136 begin
137 if (ch >= 'a') and (ch <= 'z') then Dec(ch, 32);
138 end
139 else
140 begin
141 if (ch >= #224) and (ch <= #255) then
142 begin
143 Dec(ch, 32);
144 end
145 else
146 begin
147 case ch of
148 #184, #186, #191: Dec(ch, 16);
149 #162, #179: Dec(ch);
150 end;
151 end;
152 end;
153 result := ch;
154 end;
157 function StrEquCI1251 (const s0, s1: AnsiString): Boolean;
158 var
159 i: Integer;
160 begin
161 result := false;
162 if length(s0) <> length(s1) then exit;
163 for i := 1 to length(s0) do if UpCase1251(s0[i]) <> UpCase1251(s1[i]) then exit;
164 result := true;
165 end;
168 // ////////////////////////////////////////////////////////////////////////// //
169 // utils
170 // `ch`: utf8 start
171 // -1: invalid utf8
172 function utf8CodeLen (ch: Word): Integer;
173 begin
174 if ch < $80 then begin result := 1; exit; end;
175 if (ch and $FE) = $FC then begin result := 6; exit; end;
176 if (ch and $FC) = $F8 then begin result := 5; exit; end;
177 if (ch and $F8) = $F0 then begin result := 4; exit; end;
178 if (ch and $F0) = $E0 then begin result := 3; exit; end;
179 if (ch and $E0) = $C0 then begin result := 2; exit; end;
180 result := -1; // invalid
181 end;
184 function utf8Valid (const s: AnsiString): Boolean;
185 var
186 pos, len: Integer;
187 begin
188 result := false;
189 pos := 1;
190 while pos <= length(s) do
191 begin
192 len := utf8CodeLen(Byte(s[pos]));
193 if len < 1 then exit; // invalid sequence start
194 if pos+len-1 > length(s) then exit; // out of chars in string
195 Dec(len);
196 Inc(pos);
197 // check other sequence bytes
198 while len > 0 do
199 begin
200 if (Byte(s[pos]) and $C0) <> $80 then exit;
201 Dec(len);
202 Inc(pos);
203 end;
204 end;
205 result := true;
206 end;
209 // ////////////////////////////////////////////////////////////////////////// //
210 const
211 uni2wint: array [128..255] of Word = (
212 $0402,$0403,$201A,$0453,$201E,$2026,$2020,$2021,$20AC,$2030,$0409,$2039,$040A,$040C,$040B,$040F,
213 $0452,$2018,$2019,$201C,$201D,$2022,$2013,$2014,$003F,$2122,$0459,$203A,$045A,$045C,$045B,$045F,
214 $00A0,$040E,$045E,$0408,$00A4,$0490,$00A6,$00A7,$0401,$00A9,$0404,$00AB,$00AC,$00AD,$00AE,$0407,
215 $00B0,$00B1,$0406,$0456,$0491,$00B5,$00B6,$00B7,$0451,$2116,$0454,$00BB,$0458,$0405,$0455,$0457,
216 $0410,$0411,$0412,$0413,$0414,$0415,$0416,$0417,$0418,$0419,$041A,$041B,$041C,$041D,$041E,$041F,
217 $0420,$0421,$0422,$0423,$0424,$0425,$0426,$0427,$0428,$0429,$042A,$042B,$042C,$042D,$042E,$042F,
218 $0430,$0431,$0432,$0433,$0434,$0435,$0436,$0437,$0438,$0439,$043A,$043B,$043C,$043D,$043E,$043F,
219 $0440,$0441,$0442,$0443,$0444,$0445,$0446,$0447,$0448,$0449,$044A,$044B,$044C,$044D,$044E,$044F
220 );
223 function decodeUtf8Char (s: AnsiString; var pos: Integer): char;
224 var
225 b, c: Integer;
226 begin
227 (* The following encodings are valid, except for the 5 and 6 byte
228 * combinations:
229 * 0xxxxxxx
230 * 110xxxxx 10xxxxxx
231 * 1110xxxx 10xxxxxx 10xxxxxx
232 * 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
233 * 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
234 * 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
235 *)
236 result := '?';
237 if pos > length(s) then exit;
239 b := Byte(s[pos]);
240 Inc(pos);
241 if b < $80 then begin result := char(b); exit; end;
243 // mask out unused bits
244 if (b and $FE) = $FC then b := b and $01
245 else if (b and $FC) = $F8 then b := b and $03
246 else if (b and $F8) = $F0 then b := b and $07
247 else if (b and $F0) = $E0 then b := b and $0F
248 else if (b and $E0) = $C0 then b := b and $1F
249 else exit; // invalid utf8
251 // now continue
252 while pos <= length(s) do
253 begin
254 c := Byte(s[pos]);
255 if (c and $C0) <> $80 then break; // no more
256 b := b shl 6;
257 b := b or (c and $3F);
258 Inc(pos);
259 end;
261 // done, try 1251
262 for c := 128 to 255 do if uni2wint[c] = b then begin result := char(c and $FF); exit; end;
263 // alas
264 end;
267 function utf8to1251 (s: AnsiString): AnsiString;
268 var
269 pos: Integer;
270 begin
271 if not utf8Valid(s) then begin result := s; exit; end;
272 pos := 1;
273 while pos <= length(s) do
274 begin
275 if Byte(s[pos]) >= $80 then break;
276 Inc(pos);
277 end;
278 if pos > length(s) then begin result := s; exit; end; // nothing to do here
279 result := '';
280 pos := 1;
281 while pos <= length(s) do result := result+decodeUtf8Char(s, pos);
282 end;
285 // ////////////////////////////////////////////////////////////////////////// //
286 // `pathname` will be modified if path is valid
287 // `lastIsDir` should be `true` if we are searching for directory
288 // nobody cares about shitdoze, so i'll use the same code path for it
289 function findFileCI (var pathname: AnsiString; lastIsDir: Boolean=false): Boolean;
290 var
291 sr: TSearchRec;
292 npt: AnsiString;
293 newname: AnsiString = '';
294 curname: AnsiString;
295 wantdir: Boolean;
296 attr: LongInt;
297 foundher: Boolean;
298 begin
299 npt := pathname;
300 result := (length(npt) > 0);
301 if (length(npt) > 0) and ((npt[1] = '/') or (npt[1] = '\')) then newname := '/';
302 while length(npt) > 0 do
303 begin
304 // remove trailing slashes
305 while (length(npt) > 0) and ((npt[1] = '/') or (npt[1] = '\')) do Delete(npt, 1, 1);
306 if length(npt) = 0 then break;
307 // extract name
308 curname := '';
309 while (length(npt) > 0) and (npt[1] <> '/') and (npt[1] <> '\') do
310 begin
311 curname := curname+npt[1];
312 Delete(npt, 1, 1);
313 end;
314 // remove trailing slashes again
315 while (length(npt) > 0) and ((npt[1] = '/') or (npt[1] = '\')) do Delete(npt, 1, 1);
316 wantdir := lastIsDir or (length(npt) > 0); // do we want directory here?
317 //writeln(Format('npt=[%s]; newname=[%s]; curname=[%s]; wantdir=%d', [npt, newname, curname, Integer(wantdir)]));
318 // try the easiest case first
319 attr := FileGetAttr(newname+curname);
320 if attr <> -1 then
321 begin
322 if wantdir = ((attr and faDirectory) <> 0) then
323 begin
324 // i found her!
325 newname := newname+curname;
326 if wantdir then newname := newname+'/';
327 continue;
328 end;
329 end;
330 //writeln(Format('npt=[%s]; newname=[%s]; curname=[%s]; wantdir=%d', [npt, newname, curname, Integer(wantdir)]));
331 // alas, either not found, or invalid attributes
332 foundher := false;
333 try
334 if FindFirst(newname+'*', faAnyFile, sr) = 0 then
335 repeat
336 if (wantdir = ((sr.attr and faDirectory) <> 0)) and StrEquCI1251(sr.name, curname) then
337 begin
338 // i found her!
339 newname := newname+sr.name;
340 if wantdir then newname := newname+'/';
341 foundher := true;
342 break;
343 end;
344 until FindNext(sr) <> 0;
345 finally
346 FindClose(sr);
347 end;
348 if not foundher then begin newname := ''; result := false; break; end;
349 end;
350 if result then pathname := newname;
351 end;
354 function openDiskFileRO (pathname: AnsiString): TStream;
355 begin
356 if not findFileCI(pathname) then raise Exception.Create('can''t open file "'+pathname+'"');
357 result := TFileStream.Create(pathname, fmOpenRead or {fmShareDenyWrite}fmShareDenyNone);
358 end;
360 function createDiskFile (pathname: AnsiString): TStream;
361 var
362 path: AnsiString;
363 begin
364 path := ExtractFilePath(pathname);
365 if length(path) > 0 then
366 begin
367 if not findFileCI(path, true) then raise Exception.Create('can''t create file "'+pathname+'"');
368 end;
369 result := TFileStream.Create(path+ExtractFileName(pathname), fmCreate);
370 end;
373 procedure writeIntegerLE (st: TStream; vp: Pointer; size: Integer);
374 {$IFDEF ENDIAN_LITTLE}
375 begin
376 st.writeBuffer(vp^, size);
377 end;
378 {$ELSE}
379 var
380 p: PByte;
381 begin
382 p := PByte(vp)+size-1;
383 while size > 0 do
384 begin
385 st.writeBuffer(p^, 1);
386 Dec(size);
387 Dec(p);
388 end;
389 end;
390 {$ENDIF}
392 procedure writeIntegerBE (st: TStream; vp: Pointer; size: Integer);
393 {$IFDEF ENDIAN_LITTLE}
394 var
395 p: PByte;
396 begin
397 p := PByte(vp)+size-1;
398 while size > 0 do
399 begin
400 st.writeBuffer(p^, 1);
401 Dec(size);
402 Dec(p);
403 end;
404 end;
405 {$ELSE}
406 begin
407 st.writeBuffer(vp^, size);
408 end;
409 {$ENDIF}
411 procedure writeInt (st: TStream; v: Byte); overload; begin writeIntegerLE(st, @v, 1); end;
412 procedure writeInt (st: TStream; v: ShortInt); overload; begin writeIntegerLE(st, @v, 1); end;
413 procedure writeInt (st: TStream; v: Word); overload; begin writeIntegerLE(st, @v, 2); end;
414 procedure writeInt (st: TStream; v: SmallInt); overload; begin writeIntegerLE(st, @v, 2); end;
415 procedure writeInt (st: TStream; v: LongWord); overload; begin writeIntegerLE(st, @v, 4); end;
416 procedure writeInt (st: TStream; v: LongInt); overload; begin writeIntegerLE(st, @v, 4); end;
417 procedure writeInt (st: TStream; v: Int64); overload; begin writeIntegerLE(st, @v, 8); end;
418 procedure writeInt (st: TStream; v: UInt64); overload; begin writeIntegerLE(st, @v, 8); end;
420 procedure writeIntBE (st: TStream; v: Byte); overload; begin writeIntegerBE(st, @v, 1); end;
421 procedure writeIntBE (st: TStream; v: ShortInt); overload; begin writeIntegerBE(st, @v, 1); end;
422 procedure writeIntBE (st: TStream; v: Word); overload; begin writeIntegerBE(st, @v, 2); end;
423 procedure writeIntBE (st: TStream; v: SmallInt); overload; begin writeIntegerBE(st, @v, 2); end;
424 procedure writeIntBE (st: TStream; v: LongWord); overload; begin writeIntegerBE(st, @v, 4); end;
425 procedure writeIntBE (st: TStream; v: LongInt); overload; begin writeIntegerBE(st, @v, 4); end;
426 procedure writeIntBE (st: TStream; v: Int64); overload; begin writeIntegerBE(st, @v, 8); end;
427 procedure writeIntBE (st: TStream; v: UInt64); overload; begin writeIntegerBE(st, @v, 8); end;
430 procedure readIntegerLE (st: TStream; vp: Pointer; size: Integer);
431 {$IFDEF ENDIAN_LITTLE}
432 begin
433 st.readBuffer(vp^, size);
434 end;
435 {$ELSE}
436 var
437 p: PByte;
438 begin
439 p := PByte(vp)+size-1;
440 while size > 0 do
441 begin
442 st.readBuffer(p^, 1);
443 Dec(size);
444 Dec(p);
445 end;
446 end;
447 {$ENDIF}
449 procedure readIntegerBE (st: TStream; vp: Pointer; size: Integer);
450 {$IFDEF ENDIAN_LITTLE}
451 var
452 p: PByte;
453 begin
454 p := PByte(vp)+size-1;
455 while size > 0 do
456 begin
457 st.readBuffer(p^, 1);
458 Dec(size);
459 Dec(p);
460 end;
461 end;
462 {$ELSE}
463 begin
464 st.readBuffer(vp^, size);
465 end;
466 {$ENDIF}
468 function readByte (st: TStream): Byte; begin readIntegerLE(st, @result, 1); end;
469 function readShortInt (st: TStream): ShortInt; begin readIntegerLE(st, @result, 1); end;
470 function readWord (st: TStream): Word; begin readIntegerLE(st, @result, 2); end;
471 function readSmallInt (st: TStream): SmallInt; begin readIntegerLE(st, @result, 2); end;
472 function readLongWord (st: TStream): LongWord; begin readIntegerLE(st, @result, 4); end;
473 function readLongInt (st: TStream): LongInt; begin readIntegerLE(st, @result, 4); end;
474 function readInt64 (st: TStream): Int64; begin readIntegerLE(st, @result, 8); end;
475 function readUInt64 (st: TStream): UInt64; begin readIntegerLE(st, @result, 8); end;
477 function readByteBE (st: TStream): Byte; begin readIntegerBE(st, @result, 1); end;
478 function readShortIntBE (st: TStream): ShortInt; begin readIntegerBE(st, @result, 1); end;
479 function readWordBE (st: TStream): Word; begin readIntegerBE(st, @result, 2); end;
480 function readSmallIntBE (st: TStream): SmallInt; begin readIntegerBE(st, @result, 2); end;
481 function readLongWordBE (st: TStream): LongWord; begin readIntegerBE(st, @result, 4); end;
482 function readLongIntBE (st: TStream): LongInt; begin readIntegerBE(st, @result, 4); end;
483 function readInt64BE (st: TStream): Int64; begin readIntegerBE(st, @result, 8); end;
484 function readUInt64BE (st: TStream): UInt64; begin readIntegerBE(st, @result, 8); end;
487 end.