DEADSOFTWARE

utils: made `wadExtensions` array public, because we may need it elsewhere
[d2df-sdl.git] / src / shared / utils.pas
index 5ef58845df0057b088866ef616bbf1e1f6b3ebcf..a3a650070a1ff1ef05791ee85676edd231259dd1 100644 (file)
@@ -18,7 +18,7 @@ unit utils;
 interface
 
 uses
-  SysUtils, Classes;
+  SysUtils, Classes, md5;
 
 
 // ////////////////////////////////////////////////////////////////////////// //
@@ -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
@@ -87,6 +98,8 @@ function int64ToStrComma (i: Int64): AnsiString;
 
 function upcase1251 (ch: AnsiChar): AnsiChar; inline;
 function locase1251 (ch: AnsiChar): AnsiChar; inline;
+function IsValid1251 (ch: Word): Boolean;
+function IsPrintable1251 (ch: AnsiChar): Boolean;
 
 function toLowerCase1251 (const s: AnsiString): AnsiString;
 
@@ -97,12 +110,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;
@@ -110,7 +129,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
@@ -277,22 +296,26 @@ procedure CopyMemory (Dest: Pointer; Src: Pointer; Len: LongWord); inline;
 procedure ZeroMemory (Dest: Pointer; Len: LongWord); inline;
 
 
+type
+  TDiskFileInfo = record
+    diskName: AnsiString;
+    size: LongInt;
+    age: LongInt;
+    // not changed by info getter; used in other parts of the code
+    userName: AnsiString;
+    tag: Integer;
+    hash: TMD5Digest;
+    udata: Pointer;
+  end;
+
+function GetDiskFileInfo (fname: AnsiString; var info: TDiskFileInfo): Boolean;
+
+
 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
@@ -946,6 +969,16 @@ begin
   result := ch;
 end;
 
+function IsValid1251 (ch: Word): Boolean;
+begin
+  result := (ch = Ord('?')) or (wc2shitmap[ch] <> '?')
+end;
+
+function IsPrintable1251 (ch: AnsiChar): Boolean;
+begin
+  result := (ch >= #32) and (ch <> #127)
+end;
+
 
 function strEquCI1251 (const s0, s1: AnsiString): Boolean;
 var
@@ -1096,9 +1129,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;
@@ -1997,6 +2030,32 @@ begin
 end;
 
 
+function GetDiskFileInfo (fname: AnsiString; var info: TDiskFileInfo): Boolean;
+var
+  age: LongInt;
+  size: LongInt;
+  handle: THandle;
+begin
+  result := false;
+  if (length(fname) = 0) then exit;
+  if not findFileCI(fname) then exit;
+  // get age
+  age := FileAge(fname);
+  if (age = -1) then exit;
+  // get size
+  handle := FileOpen(fname, fmOpenRead or fmShareDenyNone);
+  if (handle = THandle(-1)) then exit;
+  size := FileSeek(handle, 0, fsFromEnd);
+  FileClose(handle);
+  if (size = -1) then exit;
+  // fill info
+  info.diskName := fname;
+  info.size := size;
+  info.age := age;
+  result := true;
+end;
+
+
 (*
 var
   ss: ShortString;