implement ExtraftFileDir and ExtractFileName in uUtils
authorsheepluva
Tue, 17 May 2016 21:32:10 +0200
changeset 11831 7654e2357934
parent 11830 bb6503b9847e
child 11833 a69124eb7ce7
child 11834 ebdd7e6e6c49
implement ExtraftFileDir and ExtractFileName in uUtils
hedgewars/pas2cRedo.pas
hedgewars/uLand.pas
hedgewars/uPhysFSLayer.pas
hedgewars/uUtils.pas
--- a/hedgewars/pas2cRedo.pas	Mon May 16 00:15:32 2016 +0200
+++ b/hedgewars/pas2cRedo.pas	Tue May 17 21:32:10 2016 +0200
@@ -84,8 +84,6 @@
      min, max:function:integer;
     assign, rewrite, rewrite_2, reset, reset_2, flush, BlockWrite, BlockRead, close : procedure;
     FileExists, DirectoryExists, eof : function : boolean;
-    ExtractFileDir : function : string;
-    ExtractFileName : function : string;
 
     ParamCount : function : integer;
     ParamStr : function : string;
--- a/hedgewars/uLand.pas	Mon May 16 00:15:32 2016 +0200
+++ b/hedgewars/uLand.pas	Tue May 17 21:32:10 2016 +0200
@@ -30,7 +30,7 @@
 procedure GenPreviewAlpha(out Preview: TPreviewAlpha);
 
 implementation
-uses uConsole, uStore, uRandom, uLandObjects, uIO, uLandTexture, SysUtils,
+uses uConsole, uStore, uRandom, uLandObjects, uIO, uLandTexture,
      uVariables, uUtils, uCommands, adler32, uDebug, uLandPainted, uTextures,
      uLandGenMaze, uPhysFSLayer, uScript, uLandGenPerlin,
      uLandGenTemplateBased, uLandUtils, uRenderUtils;
--- a/hedgewars/uPhysFSLayer.pas	Mon May 16 00:15:32 2016 +0200
+++ b/hedgewars/uPhysFSLayer.pas	Tue May 17 21:32:10 2016 +0200
@@ -40,7 +40,7 @@
 procedure hedgewarsMountPackage(filename: PChar); cdecl; external PhyslayerLibName;
 
 implementation
-uses uConsts, uUtils, uVariables{$IFNDEF PAS2C}, sysutils{$ELSE}, physfs{$ENDIF};
+uses uConsts, uUtils, uVariables{$IFDEF PAS2C}, physfs{$ENDIF};
 
 function PHYSFSRWOPS_openRead(fname: PChar): PSDL_RWops; cdecl; external PhyslayerLibName;
 function PHYSFSRWOPS_openWrite(fname: PChar): PSDL_RWops; cdecl; external PhyslayerLibName;
--- a/hedgewars/uUtils.pas	Mon May 16 00:15:32 2016 +0200
+++ b/hedgewars/uUtils.pas	Tue May 17 21:32:10 2016 +0200
@@ -30,6 +30,9 @@
 procedure SplitByChar(var a, b: shortstring; c: char);
 procedure SplitByCharA(var a, b: ansistring; c: char);
 
+function ExtractFileDir(s: shortstring) : shortstring;
+function ExtractFileName(s: shortstring) : shortstring;
+
 function  EnumToStr(const en : TGearType) : shortstring; overload;
 function  EnumToStr(const en : TVisualGearType) : shortstring; overload;
 function  EnumToStr(const en : TSound) : shortstring; overload;
@@ -150,6 +153,61 @@
 
 end;
 
+function GetLastSlashPos(var s: shortString) : integer;
+var lslash: integer;
+    c: char;
+begin
+
+// find last slash
+lslash:= Length(s);
+while lslash >= 1 do
+    begin
+    c:= s[lslash];
+    if (c = #47) or (c = #92) then
+        break;
+    dec(lslash); end;
+
+GetLastSlashPos:= lslash;
+end;
+
+function ExtractFileDir(s: shortstring) : shortstring;
+var lslash: byte;
+begin
+
+if Length(s) = 0 then
+    exit(s);
+
+lslash:= GetLastSlashPos(s);
+
+if lslash <= 1 then
+    exit('');
+
+s[0]:= char(lslash - 1);
+
+ExtractFileDir:= s;
+end;
+
+function ExtractFileName(s: shortstring) : shortstring;
+var lslash, len: byte;
+begin
+
+len:= Length(s);
+
+if len = 0 then
+    exit(s);
+
+lslash:= GetLastSlashPos(s);
+
+if lslash < 1 then
+    exit(s);
+
+if lslash = len then
+    exit('');
+
+len:= len - lslash;
+ExtractFilename:= copy(s, lslash + 1, len);
+end;
+
 procedure SplitBySpace(var a,b: shortstring);
 begin
 SplitByChar(a,b,' ');