DEADSOFTWARE

more sfs refactoring
[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;
41 implementation
43 function hasWadExtension (fn: AnsiString): Boolean;
44 begin
45 fn := ExtractFileExt(fn);
46 result := StrEquCI1251(fn, '.wad') or StrEquCI1251(fn, '.pk3') or StrEquCI1251(fn, '.zip');
47 end;
50 function addWadExtension (fn: AnsiString): AnsiString;
51 begin
52 result := fn;
53 if not hasWadExtension(result) then result := result+'.wad';
54 end;
57 function isWadPath (fn: AnsiString): Boolean;
58 var
59 p: Integer;
60 s: AnsiString;
61 begin
62 result := false;
63 while true do
64 begin
65 p := Pos(':', fn);
66 if (p = 0) or (length(fn)-p < 1) then break;
67 if (p-4 > 1) and (fn[p-4] = '.') and ((fn[p+1] = '\') or (fn[p+1] = '/')) then
68 begin
69 s := Copy(fn, p-4, 4);
70 if StrEquCI1251(s, '.wad') or StrEquCI1251(s, '.pk3') or StrEquCI1251(s, '.zip') then
71 begin
72 result := true;
73 exit;
74 end;
75 end;
76 Delete(fn, 1, p);
77 end;
78 end;
81 function Int64ToStrComma (i: Int64): AnsiString;
82 var
83 f: Integer;
84 begin
85 Str(i, result);
86 f := Length(result)+1;
87 while f > 4 do
88 begin
89 Dec(f, 3); Insert(',', result, f);
90 end;
91 end;
94 function UpCase1251 (ch: Char): Char;
95 begin
96 if ch < #128 then
97 begin
98 if (ch >= 'a') and (ch <= 'z') then Dec(ch, 32);
99 end
100 else
101 begin
102 if (ch >= #224) and (ch <= #255) then
103 begin
104 Dec(ch, 32);
105 end
106 else
107 begin
108 case ch of
109 #184, #186, #191: Dec(ch, 16);
110 #162, #179: Dec(ch);
111 end;
112 end;
113 end;
114 result := ch;
115 end;
118 function StrEquCI1251 (const s0, s1: AnsiString): Boolean;
119 var
120 i: Integer;
121 begin
122 result := false;
123 if length(s0) <> length(s1) then exit;
124 for i := 1 to length(s0) do if UpCase1251(s0[i]) <> UpCase1251(s1[i]) then exit;
125 result := true;
126 end;
129 // ////////////////////////////////////////////////////////////////////////// //
130 // utils
131 // `ch`: utf8 start
132 // -1: invalid utf8
133 function utf8CodeLen (ch: Word): Integer;
134 begin
135 if ch < $80 then begin result := 1; exit; end;
136 if (ch and $FE) = $FC then begin result := 6; exit; end;
137 if (ch and $FC) = $F8 then begin result := 5; exit; end;
138 if (ch and $F8) = $F0 then begin result := 4; exit; end;
139 if (ch and $F0) = $E0 then begin result := 3; exit; end;
140 if (ch and $E0) = $C0 then begin result := 2; exit; end;
141 result := -1; // invalid
142 end;
145 function utf8Valid (const s: AnsiString): Boolean;
146 var
147 pos, len: Integer;
148 begin
149 result := false;
150 pos := 1;
151 while pos <= length(s) do
152 begin
153 len := utf8CodeLen(Byte(s[pos]));
154 if len < 1 then exit; // invalid sequence start
155 if pos+len-1 > length(s) then exit; // out of chars in string
156 Dec(len);
157 Inc(pos);
158 // check other sequence bytes
159 while len > 0 do
160 begin
161 if (Byte(s[pos]) and $C0) <> $80 then exit;
162 Dec(len);
163 Inc(pos);
164 end;
165 end;
166 result := true;
167 end;
170 // ////////////////////////////////////////////////////////////////////////// //
171 const
172 uni2wint: array [128..255] of Word = (
173 $0402,$0403,$201A,$0453,$201E,$2026,$2020,$2021,$20AC,$2030,$0409,$2039,$040A,$040C,$040B,$040F,
174 $0452,$2018,$2019,$201C,$201D,$2022,$2013,$2014,$003F,$2122,$0459,$203A,$045A,$045C,$045B,$045F,
175 $00A0,$040E,$045E,$0408,$00A4,$0490,$00A6,$00A7,$0401,$00A9,$0404,$00AB,$00AC,$00AD,$00AE,$0407,
176 $00B0,$00B1,$0406,$0456,$0491,$00B5,$00B6,$00B7,$0451,$2116,$0454,$00BB,$0458,$0405,$0455,$0457,
177 $0410,$0411,$0412,$0413,$0414,$0415,$0416,$0417,$0418,$0419,$041A,$041B,$041C,$041D,$041E,$041F,
178 $0420,$0421,$0422,$0423,$0424,$0425,$0426,$0427,$0428,$0429,$042A,$042B,$042C,$042D,$042E,$042F,
179 $0430,$0431,$0432,$0433,$0434,$0435,$0436,$0437,$0438,$0439,$043A,$043B,$043C,$043D,$043E,$043F,
180 $0440,$0441,$0442,$0443,$0444,$0445,$0446,$0447,$0448,$0449,$044A,$044B,$044C,$044D,$044E,$044F
181 );
184 function decodeUtf8Char (s: AnsiString; var pos: Integer): char;
185 var
186 b, c: Integer;
187 begin
188 (* The following encodings are valid, except for the 5 and 6 byte
189 * combinations:
190 * 0xxxxxxx
191 * 110xxxxx 10xxxxxx
192 * 1110xxxx 10xxxxxx 10xxxxxx
193 * 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
194 * 111110xx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
195 * 1111110x 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx 10xxxxxx
196 *)
197 result := '?';
198 if pos > length(s) then exit;
200 b := Byte(s[pos]);
201 Inc(pos);
202 if b < $80 then begin result := char(b); exit; end;
204 // mask out unused bits
205 if (b and $FE) = $FC then b := b and $01
206 else if (b and $FC) = $F8 then b := b and $03
207 else if (b and $F8) = $F0 then b := b and $07
208 else if (b and $F0) = $E0 then b := b and $0F
209 else if (b and $E0) = $C0 then b := b and $1F
210 else exit; // invalid utf8
212 // now continue
213 while pos <= length(s) do
214 begin
215 c := Byte(s[pos]);
216 if (c and $C0) <> $80 then break; // no more
217 b := b shl 6;
218 b := b or (c and $3F);
219 Inc(pos);
220 end;
222 // done, try 1251
223 for c := 128 to 255 do if uni2wint[c] = b then begin result := char(c and $FF); exit; end;
224 // alas
225 end;
228 function utf8to1251 (s: AnsiString): AnsiString;
229 var
230 pos: Integer;
231 begin
232 if not utf8Valid(s) then begin result := s; exit; end;
233 pos := 1;
234 while pos <= length(s) do
235 begin
236 if Byte(s[pos]) >= $80 then break;
237 Inc(pos);
238 end;
239 if pos > length(s) then begin result := s; exit; end; // nothing to do here
240 result := '';
241 pos := 1;
242 while pos <= length(s) do result := result+decodeUtf8Char(s, pos);
243 end;
246 // ////////////////////////////////////////////////////////////////////////// //
247 // `pathname` will be modified if path is valid
248 // `lastIsDir` should be `true` if we are searching for directory
249 // nobody cares about shitdoze, so i'll use the same code path for it
250 function findFileCI (var pathname: AnsiString; lastIsDir: Boolean=false): Boolean;
251 var
252 sr: TSearchRec;
253 npt: AnsiString;
254 newname: AnsiString = '';
255 curname: AnsiString;
256 wantdir: Boolean;
257 attr: LongInt;
258 foundher: Boolean;
259 begin
260 npt := pathname;
261 result := (length(npt) > 0);
262 if (length(npt) > 0) and ((npt[1] = '/') or (npt[1] = '\')) then newname := '/';
263 while length(npt) > 0 do
264 begin
265 // remove trailing slashes
266 while (length(npt) > 0) and ((npt[1] = '/') or (npt[1] = '\')) do Delete(npt, 1, 1);
267 if length(npt) = 0 then break;
268 // extract name
269 curname := '';
270 while (length(npt) > 0) and (npt[1] <> '/') and (npt[1] <> '\') do
271 begin
272 curname := curname+npt[1];
273 Delete(npt, 1, 1);
274 end;
275 // remove trailing slashes again
276 while (length(npt) > 0) and ((npt[1] = '/') or (npt[1] = '\')) do Delete(npt, 1, 1);
277 wantdir := lastIsDir or (length(npt) > 0); // do we want directory here?
278 //writeln(Format('npt=[%s]; newname=[%s]; curname=[%s]; wantdir=%d', [npt, newname, curname, Integer(wantdir)]));
279 // try the easiest case first
280 attr := FileGetAttr(newname+curname);
281 if attr <> -1 then
282 begin
283 if wantdir = ((attr and faDirectory) <> 0) then
284 begin
285 // i found her!
286 newname := newname+curname;
287 if wantdir then newname := newname+'/';
288 continue;
289 end;
290 end;
291 //writeln(Format('npt=[%s]; newname=[%s]; curname=[%s]; wantdir=%d', [npt, newname, curname, Integer(wantdir)]));
292 // alas, either not found, or invalid attributes
293 foundher := false;
294 try
295 if FindFirst(newname+'*', faAnyFile, sr) = 0 then
296 repeat
297 if (wantdir = ((sr.attr and faDirectory) <> 0)) and StrEquCI1251(sr.name, curname) then
298 begin
299 // i found her!
300 newname := newname+sr.name;
301 if wantdir then newname := newname+'/';
302 foundher := true;
303 break;
304 end;
305 until FindNext(sr) <> 0;
306 finally
307 FindClose(sr);
308 end;
309 if not foundher then begin newname := ''; result := false; break; end;
310 end;
311 if result then pathname := newname;
312 end;
315 function openDiskFileRO (pathname: AnsiString): TStream;
316 begin
317 if not findFileCI(pathname) then raise Exception.Create('can''t open file "'+pathname+'"');
318 result := TFileStream.Create(pathname, fmOpenRead or {fmShareDenyWrite}fmShareDenyNone);
319 end;
321 function createDiskFile (pathname: AnsiString): TStream;
322 var
323 path: AnsiString;
324 begin
325 path := ExtractFilePath(pathname);
326 if length(path) > 0 then
327 begin
328 if not findFileCI(path, true) then raise Exception.Create('can''t create file "'+pathname+'"');
329 end;
330 result := TFileStream.Create(path+ExtractFileName(pathname), fmCreate);
331 end;
334 end.