diff mde/file/paths.d @ 136:4084f07f2c7a

Added simpler mergetag readers and writers, with unittest.
author Diggory Hardy <diggory.hardy@gmail.com>
date Sun, 01 Feb 2009 12:36:21 +0000
parents bc697a218716
children 9f035cd139c6
line wrap: on
line diff
--- a/mde/file/paths.d	Fri Jan 30 15:51:42 2009 +0000
+++ b/mde/file/paths.d	Sun Feb 01 12:36:21 2009 +0000
@@ -36,7 +36,9 @@
 
 import mde.exception;
 import mde.file.mergetag.Reader;
+import mde.file.mergetag.MTTagReader;
 import mde.file.mergetag.Writer;
+import mde.file.mergetag.MTTagWriter;
 import mde.file.mergetag.DataSet;
 import mde.file.mergetag.exception;
 
@@ -69,28 +71,33 @@
 * In the case of confDir, the user path is guaranteed to exist (as highest priority path). */
 struct mdeDirectory
 {
-    /** Creates an MT reader for each file.
+    /** Creates an MT IReader for each file (using MTMultiReader).
     *
     * Params:
-    *   file      = The file path and name relative to the mdeDirectory, without a suffix
-    *               (e.g. "options")
-    *   readOrder = Read the highest priority or lowest priority files first? For correct merging,
-    *               this should be LOW_HIGH when newly-read items override old ones (as is the case
-    *               with DefaultData) and HIGH_LOW when the first-read items survive. Thus override
-    *               order needs to be the same for each section, except the header which is always
-    *               read with LOW_HIGH order.
-    *               Alternately, for files which shouldn't be
-    *               merged where only the highest priority file should be read, pass HIGH_ONLY.
+    *   file      = The file path and name relative to the mdeDirectory,
+    *   	    without a suffix (e.g. "options").
+    *   readOrder = Read the highest priority or lowest priority files first?
+    *   	    For correct merging, this should be LOW_HIGH when newly-
+    *   	    read items override old ones (as is the case with
+    *   	    DefaultData) and HIGH_LOW when the first-read items
+    *   	    survive. Thus override order needs to be the same for each
+    *   	    section, except the header which is always read with
+    *   	    LOW_HIGH order. Alternately, for files which shouldn't be
+    *   	    merged where only the highest priority file should be read,
+    *   	    pass HIGH_ONLY.
     *   ds        = The dataset, as for mergetag. Note: all actual readers share one dataset.
     *   rdHeader  = Read the headers for each file and merge if rdHeader == true.
     */
-    IReader makeMTReader (char[] file, PRIORITY readOrder, DataSet ds = null, bool rdHeader = false)
-    {
-        FilePath[] files = getFiles (file, readOrder);
-        if (files is null)
-            throw new NoFileException ("Unable to find the file: "~file~"[.mtt|mtb]");
-        
-        return new MTMultiReader (files, ds, rdHeader);
+    IReader makeMTReader (char[] file, PRIORITY readOrder, DataSet ds = null, bool rdHeader = false) {
+        return new MTMultiReader (getFiles (file, readOrder), ds, rdHeader);
+    }
+    
+    /** Creates an MTTagReader for each file (using MTMultiTagReader).
+     *
+     * Params as for makeMTReader.
+     */
+    MTTagReader makeMTTagReader (char[] file, PRIORITY readOrder) {
+        return new MTMultiTagReader (getFiles (file, readOrder));
     }
     
     /** Creates an MT writer for file deciding on the best path to use.
@@ -106,13 +113,24 @@
         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. */
+    /** Creates an MTTagWriter for file. */
+    MTTagWriter makeMTWriter (char[] file)
+    {
+        // FIXME: use highest priority writable path
+        return makeMTTagWriter (paths[pathsLen-1] ~ file);
+    }
+    
+    /** Returns a string listing the file name or names (if readOrder is not 
+     * HIGH_ONLY and multiple matches are found), or an error message. Intended
+     * for user output only. */
     char[] getFileName (char[] file, PRIORITY readOrder)
     {
-        FilePath[] files = getFiles (file, readOrder);
-        if (files is null)
-            return "no file found";
+        FilePath[] files;
+        try {
+            files = getFiles (file, readOrder);
+        } catch (NoFileException e) {
+            return e.msg;
+        }
         
         char[] ret = files[0].toString;
         foreach (f; files[1..$])
@@ -155,6 +173,8 @@
                 if (readOrder == PRIORITY.HIGH_ONLY) break;
             }
         }
+        if (ret is null)
+            throw new NoFileException ("Unable to find the file: "~filename~"[.mtt|mtb]");
         return ret;
     }
     
@@ -314,7 +334,6 @@
     
     // The maximum number of paths for any one "directory".
     const MAX_PATHS = 4;
-    static assert (MTMultiReader.MAX_READERS == MAX_PATHS, "MAX_PATHS not all equal");
     
     /* Try each path in succession, returning the first to exist and be a folder.
      * If none are valid and create is true, will try creating each in turn.