diff mde/resource/paths.d @ 47:e0839643ff52

New mtcp utility and changes to paths. Changed some of mde.resource.paths and mde.mergetag.Reader's handling of resolving mergetag files, and allowed getting a list of files. Created a simple mergetag-file copy utility. Fixed mergetag's MTTReader getting array out of bounds exceptions (now throws a mergetag exception). committer: Diggory Hardy <diggory.hardy@gmail.com>
author Diggory Hardy <diggory.hardy@gmail.com>
date Fri, 23 May 2008 13:13:08 +0100
parents 07bd1a09e161
children f000d6cd0f74
line wrap: on
line diff
--- a/mde/resource/paths.d	Thu May 22 12:51:47 2008 +0100
+++ b/mde/resource/paths.d	Fri May 23 13:13:08 2008 +0100
@@ -72,16 +72,11 @@
     */
     IReader makeMTReader (char[] file, PRIORITY readOrder, DataSet ds = null, bool rdHeader = false)
     {
-        if (readOrder == PRIORITY.HIGH_ONLY) {
-            foreach_reverse (path; paths) {     // starting with highest-priority path...
-                try {
-                    return makeReader (path~file, ds, rdHeader);
-                }
-                catch (MTFileIOException) {}    // Ignore errors regarding no file for now.
-            }
+        PathView[] files = getFiles (file, readOrder);
+        if (files is null)
             throw new MTFileIOException ("Unable to find the file: "~file~"[.mtt|mtb]");
-        }
-        else return new mdeReader (file, readOrder, ds, rdHeader, paths);
+        
+        return new mdeReader (files, ds, rdHeader);
     }
     
     /** Creates an MT writer for file deciding on the best path to use.
@@ -97,6 +92,20 @@
         return makeWriter (paths[pathsLen-1] ~ file, ds, WriterMethod.Text);
     }
     
+    /** Returns a string listing the file name or names (if readOrder is not HIGH_ONLY and multiple
+      * matches are found), or "no file found". Intended for user output only. */
+    char[] getFileName (char[] file, PRIORITY readOrder)
+    {
+        PathView[] files = getFiles (file, readOrder);
+        if (files is null)
+            return "no file found";
+        
+        char[] ret = files[0].toString;
+        foreach (f; files[1..$])
+            ret ~= ", " ~ f.toString;
+        return ret;
+    }
+    
     /** Check whether the given file exists under any path with either .mtt or .mtb suffix. */
     bool exists (char[] file) {
         for (uint i = 0; i < pathsLen; ++i) {
@@ -107,6 +116,30 @@
     }
     
 private:
+    PathView[] getFiles (char[] filename, PRIORITY readOrder)
+    in {
+        assert (readOrder == PRIORITY.LOW_HIGH ||
+                readOrder == PRIORITY.HIGH_LOW ||
+                readOrder == PRIORITY.HIGH_ONLY );
+    } body {
+        PathView[] ret;
+        if (readOrder == PRIORITY.LOW_HIGH) {
+            for (size_t i = 0; i < pathsLen; ++i) {
+                PathView file = findFile (paths[i]~filename);
+                if (file !is null)
+                    ret ~= file;
+            }
+        } else {
+            for (int i = pathsLen - 1; i >= 0; --i) {
+                PathView file = findFile (paths[i]~filename);
+                if (file !is null) {
+                    ret ~= file;
+                    if (readOrder == PRIORITY.HIGH_ONLY) break;
+                }
+            }
+        }
+        return ret;
+    }
     
     // Unconditionally add a path
     void addPath (char[] path) {
@@ -238,27 +271,17 @@
 */
 class mdeReader : IReader
 {
-    private this (char[] file, PRIORITY readOrder, DataSet ds, bool rdHeader, char[][MAX_PATHS] paths)
-    in { assert (readOrder == PRIORITY.LOW_HIGH || readOrder == PRIORITY.HIGH_LOW); }
-    body {
-        rdOrder = readOrder;
+    private this (PathView[] files, DataSet ds, bool rdHeader)
+    in {
+        assert (files !is null, "mdeReader.this: files is null");
+    } body {
         if (ds is null) ds = new DataSet;
         
-        foreach (path; paths) {
-            try {
-                IReader r = makeReader (path~file, ds, rdHeader);
-                
-                readers[readersLen++] = r;
-            }
-            catch (MTFileIOException) {}    // Ignore errors regarding no file for now.
+        foreach (file; files) {
+            IReader r = makeReader (file, ds, rdHeader);
+            
+            readers[readersLen++] = r;
         }
-        
-        if (readersLen == 0) {          // totally failed to find any valid files
-            throw new MTFileIOException ("Unable to find the file: "~file~"[.mtt|mtb]");
-        }
-        
-        // This is simply the easiest way of adjusting the reading order:
-        if (readOrder == PRIORITY.HIGH_LOW) readers[0..readersLen].reverse;
     }
     
     DataSet dataset () {                /// Get the DataSet