comparison mde/setup/paths.d @ 101:71f0f1f83620

Some path adjustments for windows (untested) and fonts. All types of option can be edited. paths: support for getting the full path for a font when just the file name is entered, in order to unify usage on windows and linux. paths: Used getSpecialPath for some windows paths; needs testing. Content: Moved line-editing code to abstract ValueContent class and added some conversion functions, so that any type of ValueContent can be edited as text.
author Diggory Hardy <diggory.hardy@gmail.com>
date Sun, 16 Nov 2008 17:03:47 +0000
parents 4d5d53e4f881
children ba035eba07b4
comparison
equal deleted inserted replaced
100:0ea4a3e651ae 101:71f0f1f83620
37 import mde.file.mergetag.DataSet; 37 import mde.file.mergetag.DataSet;
38 import mde.file.mergetag.exception; 38 import mde.file.mergetag.exception;
39 39
40 import tango.io.Console; 40 import tango.io.Console;
41 import tango.io.FilePath; 41 import tango.io.FilePath;
42 import tango.sys.Environment; 42 version (linux) {
43 //import tango.scrapple.sys.win32.Registry; // Trouble getting this to work 43 import tango.io.FileScan;
44 import tango.util.container.SortedMap;
45 import tango.sys.Environment;
46 } else version (Windows)
47 import tango.sys.win32.SpecialPath;
48
49 debug {
50 import tango.util.log.Log : Log, Logger;
51 private Logger logger;
52 static this() {
53 logger = Log.getLogger ("mde.setup.paths");
54 }
55 }
44 56
45 /** Order to read files in. 57 /** Order to read files in.
46 * 58 *
47 * Values: HIGH_LOW, LOW_HIGH, HIGH_ONLY. */ 59 * Values: HIGH_LOW, LOW_HIGH, HIGH_ONLY. */
48 enum PRIORITY : byte { HIGH_LOW, LOW_HIGH, HIGH_ONLY } 60 enum PRIORITY : byte { HIGH_LOW, LOW_HIGH, HIGH_ONLY }
109 static void printPaths () { 121 static void printPaths () {
110 Cout ("Data paths found:"); 122 Cout ("Data paths found:");
111 dataDir.coutPaths; 123 dataDir.coutPaths;
112 Cout ("\nConf paths found:"); 124 Cout ("\nConf paths found:");
113 confDir.coutPaths; 125 confDir.coutPaths;
114 Cout ("\nLog file directory:\n\t")(logDir).newline; 126 Cout ("\nLog file directory:\n\t")(logDir);
127 version (Windows) {
128 Cout ("\nFont directory:\n\t")(fontDir).newline;
129 } else version (linux) {
130 Cout ("\nFont filse found:");
131 foreach (f,p; fontFiles)
132 Cout ("\n\t")(f)("\t")(p[0..$-1]);
133 Cout.newline;
134 }
115 } 135 }
116 136
117 private: 137 private:
118 FilePath[] getFiles (char[] filename, PRIORITY readOrder) 138 FilePath[] getFiles (char[] filename, PRIORITY readOrder)
119 { 139 {
188 ubyte pathsLen = 0; 208 ubyte pathsLen = 0;
189 } 209 }
190 210
191 /** These are the actual instances, one for each of the data and conf "directories". */ 211 /** These are the actual instances, one for each of the data and conf "directories". */
192 mdeDirectory dataDir, confDir; 212 mdeDirectory dataDir, confDir;
193 char[] logDir; 213 char[] logDir; /// Directory for log files
194 214
195 //BEGIN Path resolution 215 //BEGIN Path resolution
196 // These are used several times: 216 // These are used several times:
197 const DATA = "/data"; 217 const DATA = "/data";
198 const CONF = "/conf"; 218 const CONF = "/conf";
200 /** Find at least one path for each required directory. 220 /** Find at least one path for each required directory.
201 * 221 *
202 * Note: the logger cannot be used yet, so only output is exception messages. */ 222 * Note: the logger cannot be used yet, so only output is exception messages. */
203 // FIXME: use tango/sys/Environment.d 223 // FIXME: use tango/sys/Environment.d
204 version (linux) { 224 version (linux) {
225 SortedMap!(char[],char[]) fontFiles; // key is file name, value is CString path
226 /** Get the actual path of a font file, or throw NoFileException if not found.
227 *
228 * Returns a C string (null terminated). */
229 char[] getFontPath (char[] file) {
230 char[] ret;
231 if (fontFiles.get (file, ret))
232 return ret;
233 throw new NoFileException ("Unable to find font file: "~file);
234 }
235
205 // base-path not used on posix 236 // base-path not used on posix
206 void resolvePaths (char[] = null) { 237 void resolvePaths (char[] base = "data") {
207 // Home directory: 238 // Home directory:
208 char[] HOME = Environment.get("HOME", "."); 239 char[] HOME = Environment.get("HOME", ".");
209 240
210 // Base paths: 241 // Base paths:
211 // Static data (must exist): 242 // Static data (must exist):
212 FilePath staticPath = 243 FilePath staticPath =
213 findPath (false, "/usr/share/games/mde", "/usr/local/share/games/mde", "data"); 244 findPath (false, "/usr/share/games/mde", "/usr/local/share/games/mde", base);
214 // Config (can just use defaults if necessary, so long as we can save afterwards): 245 // Config (can just use defaults if necessary, so long as we can save afterwards):
215 FilePath userPath = findPath (true, HOME~"/.config/mde", HOME~"/.mde"); 246 FilePath userPath = findPath (true, HOME~"/.config/mde", HOME~"/.mde");
216 247
217 // Static data paths: 248 // Static data paths:
218 dataDir.addPath (staticPath.toString); // we know this is valid anyway 249 dataDir.addPath (staticPath.toString); // we know this is valid anyway
227 if (extraConfPath) confDir.tryPath (extraConfPath); 258 if (extraConfPath) confDir.tryPath (extraConfPath);
228 if (!confDir.pathsLen) throw new mdeException ("Fatal: no conf path found!"); 259 if (!confDir.pathsLen) throw new mdeException ("Fatal: no conf path found!");
229 260
230 // Logging path: 261 // Logging path:
231 logDir = userPath.toString; 262 logDir = userPath.toString;
263
264 // Font paths:
265 auto fs = new FileScan;
266 // Scan for directories containing truetype and type1 fonts:
267 fs.sweep ("/usr/share/fonts", (FilePath fp, bool isDir)
268 { return isDir || fp.suffix == ".ttf" || fp.suffix == ".pfb"; },
269 true);
270 fontFiles = new SortedMap!(char[],char[]);
271 foreach (fp; fs.files)
272 fontFiles.add (fp.file, fp.cString); // both strings should be slices of same memory
273 logger.trace ("found {} font files, {} dirs", fs.files.length, fs.folders.length);
232 } 274 }
233 } else version (Windows) { 275 } else version (Windows) {
276 char[] fontDir;
277 /** Get the actual path of a font file, or throw NoFileException if not found.
278 *
279 * Returns a C string (null terminated). */
280 char[] getFontPath (char[] file) {
281 FilePath path = new FilePath (fontDir~file);
282 if (path.exists && !path.isFolder)
283 return path.CString;
284 throw new NoFileException ("Unable to find font file: "~file);
285 }
286
234 void resolvePaths (char[] base = "./") { 287 void resolvePaths (char[] base = "./") {
235 //FIXME: Get path from registry 288 //FIXME: Get base path from registry
236 //FIXME: Get user path (Docs&Settings/USER/Local Settings/Application data/mde)
237 //http://www.dsource.org/projects/tango/forums/topic/187
238 289
239 // Base paths: 290 // Base paths:
240 FilePath installPath = findPath (false, base); 291 FilePath installPath = findPath (false, base);
241 FilePath staticPath = findPath (false, installPath.append("data").toString); 292 FilePath staticPath = findPath (false, installPath.toString);
242 FilePath userPath = findPath (true, installPath.append("user").toString); // FIXME: see above 293 FilePath userPath = findPath (true, getSpecialPath(CSIDL_LOCAL_APPDATA) ~ "/mde");
243 294
244 // Static data paths: 295 // Static data paths:
245 dataDir.addPath (staticPath.toString); // we know this is valid anyway 296 dataDir.addPath (staticPath.toString); // we know this is valid anyway
246 dataDir.tryPath (userPath.toString ~ DATA); 297 dataDir.tryPath (userPath.toString ~ DATA);
247 if (extraDataPath) dataDir.tryPath (extraDataPath); 298 if (extraDataPath) dataDir.tryPath (extraDataPath);
254 if (extraConfPath) confDir.tryPath (extraConfPath); 305 if (extraConfPath) confDir.tryPath (extraConfPath);
255 if (!confDir.pathsLen) throw new mdeException ("Fatal: no conf path found!"); 306 if (!confDir.pathsLen) throw new mdeException ("Fatal: no conf path found!");
256 307
257 // Logging path: 308 // Logging path:
258 logDir = userPath.toString; 309 logDir = userPath.toString;
310
311 // Font path:
312 fontDir = getSpecialPath (CSIDL_FONTS) ~ "/"; // append separator
259 } 313 }
260 } else { 314 } else {
261 static assert (false, "Platform is not linux or Windows: no support for paths on this platform yet!"); 315 static assert (false, "Platform is not linux or Windows: no support for paths on this platform yet!");
262 } 316 }
263 317