DEADSOFTWARE

dfzip: more checks and user visible errors
[d2df-editor.git] / src / shared / WADEDITOR.pas
index db62d0e767e99b57c1c00d06ff0023551e8b5d37..74a3bfccccd1bf28656cf1b45695e71702ad87cb 100644 (file)
@@ -35,6 +35,16 @@ interface
     DFWAD_ERROR_READWAD          = -7;
     DFWAD_ERROR_WRONGVERSION     = -8;
 
+  const
+    DFWAD_LOG_SILENT  = 0;
+    DFWAD_LOG_FATAL   = 1;
+    DFWAD_LOG_ERROR   = 2;
+    DFWAD_LOG_WARN    = 3;
+    DFWAD_LOG_INFO    = 4;
+    DFWAD_LOG_DEBUG   = 5;
+    DFWAD_LOG_TRACE   = 6;
+    DFWAD_LOG_DEFAULT = DFWAD_LOG_INFO;
+
   type
     SArray = array of ShortString;
 
@@ -126,57 +136,52 @@ interface
 
   function gWADEditorFactory: TWADEditorFactory;
 
+  var
+    gWADEditorLogLevel: Integer = DFWAD_LOG_INFO;
+
 implementation
 
-  uses SysUtils, utils;
+  uses SysUtils, Math, utils;
 
   var
     uWADEditorFactory: TWADEditorFactory;
 
+  // EBNF:
+  // Resource = [Archive ":"] [{slash} Section {slash}slash] Name.
+  // slash    = "/" | "\".
   procedure g_ProcessResourceStr(ResourceStr: String; var FileName, SectionName, ResourceName: String);
-    var a, i: Integer;
   begin
-    for i := Length(ResourceStr) downto 1 do
-      if ResourceStr[i] = ':' then
-        Break;
-
-    FileName := Copy(ResourceStr, 1, i-1);
-
-    for a := i+1 to Length(ResourceStr) do
-      if (ResourceStr[a] = '\') or (ResourceStr[a] = '/') then Break;
-
-    ResourceName := Copy(ResourceStr, a+1, Length(ResourceStr)-Abs(a));
-    SectionName := Copy(ResourceStr, i+1, Length(ResourceStr)-Length(ResourceName)-Length(FileName)-2);
+    g_ProcessResourceStr(ResourceStr, @FileName, @SectionName, @ResourceName);
   end;
 
   procedure g_ProcessResourceStr(ResourceStr: AnsiString; FileName, SectionName, ResourceName: PAnsiString);
-    var a, i, l1, l2: Integer;
+    var i, j: Integer; sn: AnsiString;
   begin
-    for i := Length(ResourceStr) downto 1 do
-      if ResourceStr[i] = ':' then
-        Break;
+    i := Max(1, LastDelimiter(':', ResourceStr));
 
     if FileName <> nil then
-      begin
-        FileName^ := Copy(ResourceStr, 1, i-1);
-        l1 := Length(FileName^);
-      end
-    else
-      l1 := 0;
+      FileName^ := LeftStr(ResourceStr, i - 1);
 
-    for a := i+1 to Length(ResourceStr) do
-      if (ResourceStr[a] = '\') or (ResourceStr[a] = '/') then Break;
+    if (SectionName <> nil) or (ResourceName <> nil) then
+    begin
+      for i := i to High(ResourceStr) do
+        if ResourceStr[i] in ['\', '/', ':'] = False then
+          break;
+      sn := Copy(ResourceStr, i);
 
-    if ResourceName <> nil then
-      begin
-        ResourceName^ := Copy(ResourceStr, a+1, Length(ResourceStr)-Abs(a));
-        l2 := Length(ResourceName^);
-      end
-    else
-      l2 := 0;
+      j := LastDelimiter('/\', sn);
+      if ResourceName <> nil then
+        ResourceName^ := Copy(sn, j + 1);
 
-    if SectionName <> nil then
-      SectionName^ := Copy(ResourceStr, i+1, Length(ResourceStr)-l2-l1-2);
+      if SectionName <> nil then
+      begin
+        for j := j downto 0 do
+          if (j > 0) and (sn[j] in ['\', '/'] = False) then
+            break;
+        if SectionName <> nil then
+          SectionName^ := LeftStr(sn, j);
+      end;
+    end;
   end;
 
 { TWADEditor }
@@ -264,7 +269,7 @@ implementation
       fname := findFileCIStr(FileName);
       for i := 0 to FMappings.Count - 1 do
       begin
-        tmp := gWADEditorFactory.CreateEditor(FMappings[i]);
+        tmp := TWADEditorMapping(FMappings.Objects[i]).WADEditorClass.Create();
         if tmp.ReadFile2(fname) then
         begin
           Result := tmp;
@@ -283,7 +288,7 @@ implementation
     begin
       for i := 0 to FMappings.Count - 1 do
       begin
-        tmp := gWADEditorFactory.CreateEditor(FMappings[i]);
+        tmp := TWADEditorMapping(FMappings.Objects[i]).WADEditorClass.Create();
         if tmp.ReadMemory(Data, Len) then
         begin
           Result := tmp;