X-Git-Url: http://deadsoftware.ru/gitweb?a=blobdiff_plain;f=src%2Fshared%2Futils.pas;h=0ac5caa00f15004d8f7901fa99eadf36f966712c;hb=6356457c45922035e2040452cef266c2fc628ece;hp=20706c661dae397f8a0b4914ab00e9caa765a252;hpb=6fc3ad181843ea8ee8c1be9060fda475b8d906b4;p=d2df-sdl.git diff --git a/src/shared/utils.pas b/src/shared/utils.pas index 20706c6..0ac5caa 100644 --- a/src/shared/utils.pas +++ b/src/shared/utils.pas @@ -26,6 +26,17 @@ type SSArray = array of ShortString; +const wadExtensions: array [0..6] of AnsiString = ( + '.dfz', + '.wad', + '.dfwad', + '.pk3', + '.pak', + '.zip', + '.dfzip' +); + + // ////////////////////////////////////////////////////////////////////////// // type TUtf8DecoderFast = packed record @@ -60,6 +71,9 @@ function getFilenameExt (const fn: AnsiString): AnsiString; function setFilenameExt (const fn, ext: AnsiString): AnsiString; function forceFilenameExt (const fn, ext: AnsiString): AnsiString; +// rewrites slashes to '/' +function fixSlashes (s: AnsiString): AnsiString; + // strips out name from `fn`, leaving trailing slash function getFilenamePath (const fn: AnsiString): AnsiString; @@ -99,12 +113,18 @@ function utf8Valid (const s: AnsiString): Boolean; function utf8to1251 (s: AnsiString): AnsiString; -// `pathname` will be modified if path is valid -// `lastIsDir` should be `true` if we are searching for directory -// nobody cares about shitdoze, so i'll use the same code path for it +// findFileCI takes case-insensitive path, traverses it, and rewrites it to +// a case-sensetive one (using real on-disk names). return value means 'success'. +// if some dir or file wasn't found, pathname is undefined (destroyed, but not +// necessarily cleared). +// last name assumed to be a file, not directory (unless `lastIsDir` flag is set). function findFileCI (var pathname: AnsiString; lastIsDir: Boolean=false): Boolean; -// return fixed AnsiString or empty AnsiString +// findDiskWad tries to find the wad file using common wad extensions +// (see `wadExtensions` array). +// returns real on-disk filename, or empty string. +// original wad extension is used as a hint for the first try. +// also, this automatically performs `findFileCI()`. function findDiskWad (fname: AnsiString): AnsiString; // slashes must be normalized! function isWadNamesEqu (wna, wnb: AnsiString): Boolean; @@ -112,7 +132,7 @@ function isWadNamesEqu (wna, wnb: AnsiString): Boolean; // they throws function openDiskFileRO (pathname: AnsiString): TStream; function createDiskFile (pathname: AnsiString): TStream; -// creates file if necessary +// create file if necessary, but don't truncate the existing one function openDiskFileRW (pathname: AnsiString): TStream; // little endian @@ -299,17 +319,6 @@ implementation uses xstreams; -const wadExtensions: array [0..6] of AnsiString = ( - '.dfz', - '.wad', - '.dfwad', - '.pk3', - '.pak', - '.zip', - '.dfzip' -); - - // ////////////////////////////////////////////////////////////////////////// // procedure CopyMemory (Dest: Pointer; Src: Pointer; Len: LongWord); inline; begin @@ -327,6 +336,17 @@ begin end; +// ////////////////////////////////////////////////////////////////////////// // +// rewrites slashes to '/' +function fixSlashes (s: AnsiString): AnsiString; +var + f: Integer; +begin + result := s; + for f := 1 to length(result) do if (result[f] = '\') then result[f] := '/'; +end; + + // ////////////////////////////////////////////////////////////////////////// // constructor TSimpleList.TEnumerator.Create (const aitems: TItemArr; acount: Integer); begin @@ -879,7 +899,7 @@ end; function isWadPath (const fn: AnsiString): Boolean; var pos: Integer; - s: AnsiString; + s, wext: AnsiString; begin result := false; pos := 1; @@ -891,10 +911,13 @@ begin if (pos-4 > 1) and (fn[pos-4] = '.') and ((fn[pos+1] = '\') or (fn[pos+1] = '/')) then begin s := Copy(fn, pos-4, 4); - if StrEquCI1251(s, '.wad') or StrEquCI1251(s, '.pk3') or StrEquCI1251(s, '.zip') or StrEquCI1251(s, '.dfz') then + for wext in wadExtensions do begin - result := true; - exit; + if strEquCI1251(s, wext) then + begin + result := true; + exit; + end; end; end; end; @@ -1123,9 +1146,9 @@ end; // ////////////////////////////////////////////////////////////////////////// // -// `pathname` will be modified if path is valid -// `lastIsDir` should be `true` if we are searching for directory -// nobody cares about shitdoze, so i'll use the same code path for it +// findFileCI eats case-insensitive path, traverses it and rewrites it to a +// case-sensetive. result value means success. +// if file/dir not founded than pathname is in undefined state! function findFileCI (var pathname: AnsiString; lastIsDir: Boolean=false): Boolean; var sr: TSearchRec;