diff --git a/src/shared/utils.pas b/src/shared/utils.pas
index 641cae7ddf44bc4d36a1360e0d3e6bd46394ace1..89ecb9097fa605ac105f6b1e9cb4b5d6409f39ce 100644 (file)
--- a/src/shared/utils.pas
+++ b/src/shared/utils.pas
implementation
-uses
- xstreams;
+ uses
+ xstreams, StrUtils, e_Log;
+ const
+ {$IFDEF GO32V2}
+ DirSep = '\';
+ {$ELSE}
+ DirSep = '/';
+ {$ENDIF}
// ////////////////////////////////////////////////////////////////////////// //
procedure CopyMemory (Dest: Pointer; Src: Pointer; Len: LongWord); inline;
pos: Integer;
ch: AnsiChar;
begin
- if (Length(fn) = 0) then begin result := './'; exit; end;
+ if (Length(fn) = 0) then begin result := '.' + DirSep; exit; end;
if (fn[Length(fn)] = '/') or (fn[Length(fn)] = '\') then begin result := fn; exit; end;
pos := Length(fn);
while (pos > 0) do
if (ch = '/') or (ch = '\') then begin result := Copy(fn, 1, pos); exit; end;
Dec(pos);
end;
- result := './'; // no path -> current dir
+ result := '.' + DirSep; // no path -> current dir
end;
pos := 1;
while (pos <= Length(fn)) and ((fn[pos] = '/') or (fn[pos] = '\')) do Inc(pos);
result := path;
- if (Length(result) > 0) and ((result[Length(result)] <> '/') and (result[Length(result)] <> '\')) then result += '/';
+ if (Length(result) > 0) and ((result[Length(result)] <> '/') and (result[Length(result)] <> '\')) then result += DirSep;
if (pos <= Length(fn)) then
begin
result += Copy(fn, pos, Length(fn)-pos+1);
begin
Delete(result, Length(result), 1);
end;
- if (fn[Length(fn)] = '/') or (fn[Length(fn)] = '\') then result += '/';
+ if (fn[Length(fn)] = '/') or (fn[Length(fn)] = '\') then result += DirSep;
end;
end;
begin
npt := pathname;
result := (length(npt) > 0);
- if (length(npt) > 0) and ((npt[1] = '/') or (npt[1] = '\')) then newname := '/';
+ if (length(npt) > 0) and ((npt[1] = '/') or (npt[1] = '\')) then newname := DirSep;
while length(npt) > 0 do
begin
// remove trailing slashes
// remove trailing slashes again
while (length(npt) > 0) and ((npt[1] = '/') or (npt[1] = '\')) do Delete(npt, 1, 1);
wantdir := lastIsDir or (length(npt) > 0); // do we want directory here?
- //writeln(Format('npt=[%s]; newname=[%s]; curname=[%s]; wantdir=%d', [npt, newname, curname, Integer(wantdir)]));
+ //e_LogWritefln('npt=[%s]; newname=[%s]; curname=[%s]; wantdir=%d', [npt, newname, curname, Integer(wantdir)]);
// try the easiest case first
attr := FileGetAttr(newname+curname);
if attr <> -1 then
begin
// i found her!
newname := newname+curname;
- if wantdir then newname := newname+'/';
+ if wantdir then newname := newname + DirSep;
continue;
end;
end;
- //writeln(Format('npt=[%s]; newname=[%s]; curname=[%s]; wantdir=%d', [npt, newname, curname, Integer(wantdir)]));
+ //e_LogWritefLn('npt=[%s]; newname=[%s]; curname=[%s]; wantdir=%d', [npt, newname, curname, Integer(wantdir)]);
// alas, either not found, or invalid attributes
foundher := false;
try
begin
// i found her!
newname := newname+sr.name;
- if wantdir then newname := newname+'/';
+ if wantdir then newname := newname + DirSep;
foundher := true;
break;
end;
end;
+(** Replace slashes to backslashes for DOS **)
+function FixFileName (filename: AnsiString): AnsiString;
+begin
+ {$IFDEF GO32V2}
+ Result := StringReplace(filename, '/', '\', [rfReplaceAll, rfIgnoreCase])
+ {$ELSE}
+ Result := filename
+ {$ENDIF}
+end;
+
+
const fileExtensions: array [0..6] of AnsiString = ('.wad', '.dfzip', '.dfwad', '.pk3', '.pak', '.zip', '.dfz');
function findDiskWad (fname: AnsiString): AnsiString;
newExt: AnsiString = '';
begin
result := '';
- //writeln('findDiskWad00: fname=<', fname, '>');
+{$IFDEF GO32V2}
+ // FIXIT: it didn't work under MSDOS for some reason, so i just cut extension replacement
+ result := FixFileName(fname);
+{$ELSE}
+ //e_LogWriteLn('findDiskWad00: fname=<' + fname + '>');
if (findFileCI(fname)) then begin result := fname; exit; end;
origExt := getFilenameExt(fname);
fname := forceFilenameExt(fname, '');
- //writeln(' findDiskWad01: fname=<', fname, '>; origExt=<', origExt, '>');
+ //e_LogWriteLn(' findDiskWad01: fname=<' + fname + '>; origExt=<' + origExt + '>');
for newExt in fileExtensions do
begin
- //writeln(' findDiskWad02: fname=<', fname, '>; origExt=<', origExt, '>; newExt=<', newExt, '>');
+ //e_LogWriteLn(' findDiskWad02: fname=<' + fname + '>; origExt=<' + origExt + '>; newExt=<' + newExt + '>');
if (StrEquCI1251(newExt, origExt)) then
begin
- //writeln(' SKIP');
+ //e_LogWriteLn(' SKIP');
continue;
end;
result := fname+newExt;
if (findFileCI(result)) then exit;
end;
result := '';
+{$ENDIF}
end;
-
function openDiskFileRO (pathname: AnsiString): TStream;
begin
+ pathname := FixFileName(pathname);
if not findFileCI(pathname) then raise Exception.Create('can''t open file "'+pathname+'"');
result := TFileStream.Create(pathname, fmOpenRead or {fmShareDenyWrite}fmShareDenyNone);
end;
var
path: AnsiString;
begin
+ pathname := FixFileName(pathname);
path := ExtractFilePath(pathname);
if length(path) > 0 then
begin
@@ -1393,42 +1416,7 @@ function nclamp (v, a, b: Extended): Extended; inline; overload; begin if (v < a
function snprintf (buf: PAnsiChar; bufsize: SizeUInt; const fmt: PAnsiChar): SizeUInt; cdecl; varargs; external 'msvcrt.dll' name '_snprintf';
{$ELSE}
{$IFDEF GO32V2}
-
-function snprintf (buf: PAnsiChar; bufsize: SizeUInt; const fmt: PAnsiChar): SizeUInt;
-begin
- {$WARNING snprintf not implemented!}
- buf[0] := #0;
- result := 0
-end;
-
-function snprintf (buf: PAnsiChar; bufsize: SizeUInt; const fmt: PAnsiChar; x: LongInt): SizeUInt; overload;
-begin
- {$WARNING snprintf+longint not implemented!}
- buf[0] := #0;
- result := 0
-end;
-
-function snprintf (buf: PAnsiChar; bufsize: SizeUInt; const fmt: PAnsiChar; x: PAnsiChar): SizeUInt; overload;
-begin
- {$WARNING snprintf+string not implemented!}
- buf[0] := #0;
- result := 0
-end;
-
-function snprintf (buf: PAnsiChar; bufsize: SizeUInt; const fmt: PAnsiChar; x: Char): SizeUInt; overload;
-begin
- {$WARNING snprintf+char not implemented!}
- buf[0] := #0;
- result := 0
-end;
-
-function snprintf (buf: PAnsiChar; bufsize: SizeUInt; const fmt: PAnsiChar; x: Double): SizeUInt; overload;
-begin
- {$WARNING snprintf+double not implemented!}
- buf[0] := #0;
- result := 0
-end;
-
+function snprintf (buf: PAnsiChar; bufsize: SizeUInt; const fmt: PAnsiChar): SizeUInt; cdecl; varargs; external;
{$ELSE}
function snprintf (buf: PAnsiChar; bufsize: SizeUInt; const fmt: PAnsiChar): SizeUInt; cdecl; varargs; external 'libc' name 'snprintf';
{$ENDIF}