Mercurial > projects > mde
comparison 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 |
comparison
equal
deleted
inserted
replaced
135:bc697a218716 | 136:4084f07f2c7a |
---|---|
34 * FilePaths for adjusted paths should be no slower than mutating existing ones. */ | 34 * FilePaths for adjusted paths should be no slower than mutating existing ones. */ |
35 module mde.file.paths; | 35 module mde.file.paths; |
36 | 36 |
37 import mde.exception; | 37 import mde.exception; |
38 import mde.file.mergetag.Reader; | 38 import mde.file.mergetag.Reader; |
39 import mde.file.mergetag.MTTagReader; | |
39 import mde.file.mergetag.Writer; | 40 import mde.file.mergetag.Writer; |
41 import mde.file.mergetag.MTTagWriter; | |
40 import mde.file.mergetag.DataSet; | 42 import mde.file.mergetag.DataSet; |
41 import mde.file.mergetag.exception; | 43 import mde.file.mergetag.exception; |
42 | 44 |
43 import tango.io.Console; | 45 import tango.io.Console; |
44 import tango.io.FilePath; | 46 import tango.io.FilePath; |
67 * It is the only item within this module that you should need to interact with. | 69 * It is the only item within this module that you should need to interact with. |
68 * | 70 * |
69 * In the case of confDir, the user path is guaranteed to exist (as highest priority path). */ | 71 * In the case of confDir, the user path is guaranteed to exist (as highest priority path). */ |
70 struct mdeDirectory | 72 struct mdeDirectory |
71 { | 73 { |
72 /** Creates an MT reader for each file. | 74 /** Creates an MT IReader for each file (using MTMultiReader). |
73 * | 75 * |
74 * Params: | 76 * Params: |
75 * file = The file path and name relative to the mdeDirectory, without a suffix | 77 * file = The file path and name relative to the mdeDirectory, |
76 * (e.g. "options") | 78 * without a suffix (e.g. "options"). |
77 * readOrder = Read the highest priority or lowest priority files first? For correct merging, | 79 * readOrder = Read the highest priority or lowest priority files first? |
78 * this should be LOW_HIGH when newly-read items override old ones (as is the case | 80 * For correct merging, this should be LOW_HIGH when newly- |
79 * with DefaultData) and HIGH_LOW when the first-read items survive. Thus override | 81 * read items override old ones (as is the case with |
80 * order needs to be the same for each section, except the header which is always | 82 * DefaultData) and HIGH_LOW when the first-read items |
81 * read with LOW_HIGH order. | 83 * survive. Thus override order needs to be the same for each |
82 * Alternately, for files which shouldn't be | 84 * section, except the header which is always read with |
83 * merged where only the highest priority file should be read, pass HIGH_ONLY. | 85 * LOW_HIGH order. Alternately, for files which shouldn't be |
86 * merged where only the highest priority file should be read, | |
87 * pass HIGH_ONLY. | |
84 * ds = The dataset, as for mergetag. Note: all actual readers share one dataset. | 88 * ds = The dataset, as for mergetag. Note: all actual readers share one dataset. |
85 * rdHeader = Read the headers for each file and merge if rdHeader == true. | 89 * rdHeader = Read the headers for each file and merge if rdHeader == true. |
86 */ | 90 */ |
87 IReader makeMTReader (char[] file, PRIORITY readOrder, DataSet ds = null, bool rdHeader = false) | 91 IReader makeMTReader (char[] file, PRIORITY readOrder, DataSet ds = null, bool rdHeader = false) { |
88 { | 92 return new MTMultiReader (getFiles (file, readOrder), ds, rdHeader); |
89 FilePath[] files = getFiles (file, readOrder); | 93 } |
90 if (files is null) | 94 |
91 throw new NoFileException ("Unable to find the file: "~file~"[.mtt|mtb]"); | 95 /** Creates an MTTagReader for each file (using MTMultiTagReader). |
92 | 96 * |
93 return new MTMultiReader (files, ds, rdHeader); | 97 * Params as for makeMTReader. |
98 */ | |
99 MTTagReader makeMTTagReader (char[] file, PRIORITY readOrder) { | |
100 return new MTMultiTagReader (getFiles (file, readOrder)); | |
94 } | 101 } |
95 | 102 |
96 /** Creates an MT writer for file deciding on the best path to use. | 103 /** Creates an MT writer for file deciding on the best path to use. |
97 * | 104 * |
98 * Params: | 105 * Params: |
104 { | 111 { |
105 // FIXME: use highest priority writable path | 112 // FIXME: use highest priority writable path |
106 return makeWriter (paths[pathsLen-1] ~ file, ds, WriterMethod.Text); | 113 return makeWriter (paths[pathsLen-1] ~ file, ds, WriterMethod.Text); |
107 } | 114 } |
108 | 115 |
109 /** Returns a string listing the file name or names (if readOrder is not HIGH_ONLY and multiple | 116 /** Creates an MTTagWriter for file. */ |
110 * matches are found), or "no file found". Intended for user output only. */ | 117 MTTagWriter makeMTWriter (char[] file) |
118 { | |
119 // FIXME: use highest priority writable path | |
120 return makeMTTagWriter (paths[pathsLen-1] ~ file); | |
121 } | |
122 | |
123 /** Returns a string listing the file name or names (if readOrder is not | |
124 * HIGH_ONLY and multiple matches are found), or an error message. Intended | |
125 * for user output only. */ | |
111 char[] getFileName (char[] file, PRIORITY readOrder) | 126 char[] getFileName (char[] file, PRIORITY readOrder) |
112 { | 127 { |
113 FilePath[] files = getFiles (file, readOrder); | 128 FilePath[] files; |
114 if (files is null) | 129 try { |
115 return "no file found"; | 130 files = getFiles (file, readOrder); |
131 } catch (NoFileException e) { | |
132 return e.msg; | |
133 } | |
116 | 134 |
117 char[] ret = files[0].toString; | 135 char[] ret = files[0].toString; |
118 foreach (f; files[1..$]) | 136 foreach (f; files[1..$]) |
119 ret ~= ", " ~ f.toString; | 137 ret ~= ", " ~ f.toString; |
120 return ret; | 138 return ret; |
153 if (file !is null) | 171 if (file !is null) |
154 ret ~= file; | 172 ret ~= file; |
155 if (readOrder == PRIORITY.HIGH_ONLY) break; | 173 if (readOrder == PRIORITY.HIGH_ONLY) break; |
156 } | 174 } |
157 } | 175 } |
176 if (ret is null) | |
177 throw new NoFileException ("Unable to find the file: "~filename~"[.mtt|mtb]"); | |
158 return ret; | 178 return ret; |
159 } | 179 } |
160 | 180 |
161 // Unconditionally add a path | 181 // Unconditionally add a path |
162 void addPath (char[] path) { | 182 void addPath (char[] path) { |
312 } | 332 } |
313 } | 333 } |
314 | 334 |
315 // The maximum number of paths for any one "directory". | 335 // The maximum number of paths for any one "directory". |
316 const MAX_PATHS = 4; | 336 const MAX_PATHS = 4; |
317 static assert (MTMultiReader.MAX_READERS == MAX_PATHS, "MAX_PATHS not all equal"); | |
318 | 337 |
319 /* Try each path in succession, returning the first to exist and be a folder. | 338 /* Try each path in succession, returning the first to exist and be a folder. |
320 * If none are valid and create is true, will try creating each in turn. | 339 * If none are valid and create is true, will try creating each in turn. |
321 * If still none are valid, throws. */ | 340 * If still none are valid, throws. */ |
322 FilePath findPath (bool create, char[][] paths ...) { | 341 FilePath findPath (bool create, char[][] paths ...) { |