comparison mde/setup/paths.d @ 107:20f7d813bb0f

Translation: now has a file for each locale, instead of a file for each section. Items updated; all strings translated. New unittest for Translation; I realised the old one wasn't run anymore. Changed unittest data and conf dirs.
author Diggory Hardy <diggory.hardy@gmail.com>
date Sun, 30 Nov 2008 17:17:56 +0000
parents 7f7b40fed72b
children 1b1e2297e2fc
comparison
equal deleted inserted replaced
106:7f7b40fed72b 107:20f7d813bb0f
137 private: 137 private:
138 FilePath[] getFiles (char[] filename, PRIORITY readOrder) 138 FilePath[] getFiles (char[] filename, PRIORITY readOrder)
139 { 139 {
140 FilePath[] ret; 140 FilePath[] ret;
141 141
142 debug (mdeUnitTest) {
143 /* Alternate approach - just try from current directory.
144 * Really just for unittests, but with the if condition it shouldn't break anything else. */
145 if (pathsLen == 0) {
146 FilePath file = findFile (filename);
147 if (file !is null)
148 ret ~= file;
149 return ret;
150 }
151 }
152
153 if (readOrder == PRIORITY.LOW_HIGH) { 142 if (readOrder == PRIORITY.LOW_HIGH) {
154 for (size_t i = 0; i < pathsLen; ++i) { 143 for (size_t i = 0; i < pathsLen; ++i) {
155 FilePath file = findFile (paths[i]~filename); 144 FilePath file = findFile (paths[i]~filename);
156 if (file !is null) 145 if (file !is null)
157 ret ~= file; 146 ret ~= file;
170 return ret; 159 return ret;
171 } 160 }
172 161
173 // Unconditionally add a path 162 // Unconditionally add a path
174 void addPath (char[] path) { 163 void addPath (char[] path) {
164 assert (pathsLen < MAX_PATHS);
175 paths[pathsLen++] = path~'/'; 165 paths[pathsLen++] = path~'/';
176 } 166 }
177 167
178 // Test a path and add if is a folder. 168 // Test a path and add if is a folder.
179 bool tryPath (char[] path, bool create = false) { 169 bool tryPath (char[] path, bool create = false) {
180 FilePath fp = FilePath (path); 170 assert (pathsLen < MAX_PATHS);
171 FilePath fp = FilePath (path);
181 if (fp.exists && fp.isFolder) { 172 if (fp.exists && fp.isFolder) {
182 paths[pathsLen++] = path~'/'; 173 paths[pathsLen++] = path~'/';
183 return true; 174 return true;
184 } else if (create) { 175 } else if (create) {
185 try { 176 try {
211 /** These are the actual instances, one for each of the data and conf "directories". */ 202 /** These are the actual instances, one for each of the data and conf "directories". */
212 mdeDirectory dataDir, confDir; 203 mdeDirectory dataDir, confDir;
213 char[] logDir; /// Directory for log files 204 char[] logDir; /// Directory for log files
214 205
215 //BEGIN Path resolution 206 //BEGIN Path resolution
216 // These are used several times: 207 /** Find at least one path for each required directory. */
217 const DATA = "/data"; 208 debug (mdeUnitTest) {
218 const CONF = "/conf"; 209 /** In unittest mode, add paths unittest/data and unittest/conf before init runs. */
219 210 static this () {
220 /** Find at least one path for each required directory. 211 dataDir.tryPath ("unittest/data");
221 * 212 confDir.tryPath ("unittest/conf");
222 * Note: the logger cannot be used yet, so only output is exception messages. */ 213 if (!(dataDir.pathsLen && confDir.pathsLen))
214 throw new mdeException ("Fatal: unittest/data and unittest/conf don't both exist");
215 // Don't bother with real paths or logDir or font dir(s) for unittest.
216 }
217 }
223 version (linux) { 218 version (linux) {
224 SortedMap!(char[],char[]) fontFiles; // key is file name, value is CString path 219 SortedMap!(char[],char[]) fontFiles; // key is file name, value is CString path
225 /** Get the actual path of a font file, or throw NoFileException if not found. 220 /** Get the actual path of a font file, or throw NoFileException if not found.
226 * 221 *
227 * Returns a C string (null terminated). */ 222 * Returns a C string (null terminated). */
244 // Config (can just use defaults if necessary, so long as we can save afterwards): 239 // Config (can just use defaults if necessary, so long as we can save afterwards):
245 FilePath userPath = findPath (true, HOME~"/.config/mde", HOME~"/.mde"); 240 FilePath userPath = findPath (true, HOME~"/.config/mde", HOME~"/.mde");
246 241
247 // Static data paths: 242 // Static data paths:
248 dataDir.addPath (staticPath.toString); // we know this is valid anyway 243 dataDir.addPath (staticPath.toString); // we know this is valid anyway
249 dataDir.tryPath (userPath.toString ~ DATA); 244 dataDir.tryPath (userPath.toString ~ "/data");
250 if (extraDataPath) dataDir.tryPath (extraDataPath); 245 if (extraDataPath) dataDir.tryPath (extraDataPath);
251 if (!dataDir.pathsLen) throw new mdeException ("Fatal: no data path found!"); 246 if (!dataDir.pathsLen) throw new mdeException ("Fatal: no data path found!");
252 247
253 // Configuration paths: 248 // Configuration paths:
254 confDir.tryPath (staticPath.toString ~ CONF); 249 confDir.tryPath (staticPath.toString ~ "/conf");
255 confDir.tryPath ("/etc/mde"); 250 confDir.tryPath ("/etc/mde");
256 confDir.tryPath (userPath.toString ~ CONF, true); 251 confDir.tryPath (userPath.toString ~ "/conf", true);
257 if (extraConfPath) confDir.tryPath (extraConfPath); 252 if (extraConfPath) confDir.tryPath (extraConfPath);
258 if (!confDir.pathsLen) throw new mdeException ("Fatal: no conf path found!"); 253 if (!confDir.pathsLen) throw new mdeException ("Fatal: no conf path found!");
259 254
260 // Logging path: 255 // Logging path:
261 logDir = userPath.toString; 256 logDir = userPath.toString;
291 FilePath staticPath = findPath (false, installPath.toString); 286 FilePath staticPath = findPath (false, installPath.toString);
292 FilePath userPath = findPath (true, getSpecialPath(CSIDL_LOCAL_APPDATA) ~ "/mde"); 287 FilePath userPath = findPath (true, getSpecialPath(CSIDL_LOCAL_APPDATA) ~ "/mde");
293 288
294 // Static data paths: 289 // Static data paths:
295 dataDir.addPath (staticPath.toString); // we know this is valid anyway 290 dataDir.addPath (staticPath.toString); // we know this is valid anyway
296 dataDir.tryPath (userPath.toString ~ DATA); 291 dataDir.tryPath (userPath.toString ~ "/data");
297 if (extraDataPath) dataDir.tryPath (extraDataPath); 292 if (extraDataPath) dataDir.tryPath (extraDataPath);
298 if (!dataDir.pathsLen) throw new mdeException ("Fatal: no data path found!"); 293 if (!dataDir.pathsLen) throw new mdeException ("Fatal: no data path found!");
299 294
300 // Configuration paths: 295 // Configuration paths:
301 confDir.tryPath (staticPath.toString ~ CONF); 296 confDir.tryPath (staticPath.toString ~ "/conf");
302 confDir.tryPath (installPath.append("user").toString); 297 confDir.tryPath (installPath.append("user").toString);
303 confDir.tryPath (userPath.toString ~ CONF, true); 298 confDir.tryPath (userPath.toString ~ "/conf", true);
304 if (extraConfPath) confDir.tryPath (extraConfPath); 299 if (extraConfPath) confDir.tryPath (extraConfPath);
305 if (!confDir.pathsLen) throw new mdeException ("Fatal: no conf path found!"); 300 if (!confDir.pathsLen) throw new mdeException ("Fatal: no conf path found!");
306 301
307 // Logging path: 302 // Logging path:
308 logDir = userPath.toString; 303 logDir = userPath.toString;
322 this(char[] msg) { 317 this(char[] msg) {
323 super (msg); 318 super (msg);
324 } 319 }
325 } 320 }
326 321
327 // The maximum number of paths for any one "directory". 322 // The maximum number of paths for any one "directory".
328 // There are NO CHECKS that this is not exceeded.
329 const MAX_PATHS = 4; 323 const MAX_PATHS = 4;
330 324
331 /* Try each path in succession, returning the first to exist and be a folder. 325 /* Try each path in succession, returning the first to exist and be a folder.
332 * If none are valid and create is true, will try creating each in turn. 326 * If none are valid and create is true, will try creating each in turn.
333 * If still none are valid, throws. */ 327 * If still none are valid, throws. */