Changeset 839:4063da6f3edd
- Timestamp:
- 08/21/08 11:51:04 (5 months ago)
- Tags:
- Files:
-
- src/Settings.d (modified) (1 diff)
- src/SettingsLoader.d (modified) (7 diffs)
- src/config.d (modified) (1 diff)
- src/main.d (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
src/Settings.d
r806 r839 10 10 { 11 11 static: 12 /// Path to the data directory. 13 string dataDir = "data/"; 12 14 /// Predefined version identifiers. 13 15 string[] versionIds; src/SettingsLoader.d
r831 r839 17 17 18 18 import tango.io.FilePath; 19 import tango.sys.Environment; 20 import tango.text.Util : substitute; 19 21 20 22 /// Loads settings from a D module file. 21 class SettingsLoader23 abstract class SettingsLoader 22 24 { 23 25 InfoManager infoMan; /// Collects error messages. … … 27 29 { 28 30 this.infoMan = infoMan; 29 }30 31 static SettingsLoader opCall(InfoManager infoMan)32 {33 return new SettingsLoader(infoMan);34 31 } 35 32 … … 72 69 73 70 void load() 74 { 75 scope execPath = new FilePath(GetExecutableFilePath()); 76 execPath = new FilePath(execPath.folder()); 77 78 // Load config.d 79 auto filePath = resolvePath(execPath, "config.d"); 71 {} 72 } 73 74 /// Loads the configuration file of dil. 75 class ConfigLoader : SettingsLoader 76 { 77 static string configFileName = "config.d"; /// Name of the configuration file. 78 string executablePath; /// Absolute path to the executable of dil. 79 string executableDir; /// Absolte path to the directory of the executable of dil. 80 string dataDir; /// Absolute path to dil's data directory. 81 string homePath; /// Path to the home directory. 82 83 this(InfoManager infoMan) 84 { 85 super(infoMan); 86 } 87 88 static ConfigLoader opCall(InfoManager infoMan) 89 { 90 return new ConfigLoader(infoMan); 91 } 92 93 string expandVariables(string val) 94 { 95 val = substitute(val, "${DATADIR}", dataDir); 96 val = substitute(val, "${HOME}", homePath); 97 val = substitute(val, "${EXECDIR}", executableDir); 98 return val; 99 } 100 101 void load() 102 { 103 homePath = Environment.get("HOME"); 104 executablePath = GetExecutableFilePath(); 105 executableDir = (new FilePath(executablePath)).folder(); 106 107 // Load the configuration file. 108 auto filePath = findConfigurationFilePath(); 109 if (filePath is null) 110 { 111 infoMan ~= new Error(new Location("",0), 112 "the configuration file "~configFileName~" could not be found."); 113 return; 114 } 80 115 mod = new Module(filePath, infoMan); 81 116 mod.parse(); … … 88 123 pass1.run(); 89 124 90 if (auto array = getValue!(ArrayInitExpression)("version_ids")) 125 // Initialize the dataDir member. 126 if (auto val = getValue!(StringExpression)("DATADIR")) 127 dataDir = val.getString(); 128 dataDir = resolvePath(executableDir, dataDir); 129 GlobalSettings.dataDir = dataDir; 130 131 if (auto array = getValue!(ArrayInitExpression)("VERSION_IDS")) 91 132 foreach (value; array.values) 92 if (auto str= castTo!(StringExpression)(value))93 GlobalSettings.versionIds ~= str.getString();94 if (auto val = getValue!(StringExpression)(" langfile"))95 GlobalSettings.langFile = val.getString();96 if (auto array = getValue!(ArrayInitExpression)(" import_paths"))133 if (auto val = castTo!(StringExpression)(value)) 134 GlobalSettings.versionIds ~= val.getString(); 135 if (auto val = getValue!(StringExpression)("LANG_FILE")) 136 GlobalSettings.langFile = expandVariables(val.getString()); 137 if (auto array = getValue!(ArrayInitExpression)("IMPORT_PATHS")) 97 138 foreach (value; array.values) 98 if (auto str= castTo!(StringExpression)(value))99 GlobalSettings.importPaths ~= str.getString();100 if (auto array = getValue!(ArrayInitExpression)(" ddoc_files"))139 if (auto val = castTo!(StringExpression)(value)) 140 GlobalSettings.importPaths ~= expandVariables(val.getString()); 141 if (auto array = getValue!(ArrayInitExpression)("DDOC_FILES")) 101 142 foreach (value; array.values) 102 if (auto str= castTo!(StringExpression)(value))103 GlobalSettings.ddocFilePaths ~= resolvePath(execPath, str.getString());104 if (auto val = getValue!(StringExpression)(" xml_map"))105 GlobalSettings.xmlMapFile = val.getString();106 if (auto val = getValue!(StringExpression)(" html_map"))107 GlobalSettings.htmlMapFile = val.getString();108 if (auto val = getValue!(StringExpression)(" lexer_error"))143 if (auto val = castTo!(StringExpression)(value)) 144 GlobalSettings.ddocFilePaths ~= expandVariables(val.getString()); 145 if (auto val = getValue!(StringExpression)("XML_MAP")) 146 GlobalSettings.xmlMapFile = expandVariables(val.getString()); 147 if (auto val = getValue!(StringExpression)("HTML_MAP")) 148 GlobalSettings.htmlMapFile = expandVariables(val.getString()); 149 if (auto val = getValue!(StringExpression)("LEXER_ERROR")) 109 150 GlobalSettings.lexerErrorFormat = val.getString(); 110 if (auto val = getValue!(StringExpression)(" parser_error"))151 if (auto val = getValue!(StringExpression)("PARSER_ERROR")) 111 152 GlobalSettings.parserErrorFormat = val.getString(); 112 if (auto val = getValue!(StringExpression)(" semantic_error"))153 if (auto val = getValue!(StringExpression)("SEMANTIC_ERROR")) 113 154 GlobalSettings.semanticErrorFormat = val.getString(); 114 155 115 156 // Load language file. 116 filePath = resolvePath(execPath, GlobalSettings.langFile); 157 // TODO: create a separate class for this? 158 filePath = expandVariables(GlobalSettings.langFile); 117 159 mod = new Module(filePath); 118 160 mod.parse(); … … 128 170 char[][] messages; 129 171 foreach (value; array.values) 130 if (auto str= castTo!(StringExpression)(value))131 messages ~= str.getString();172 if (auto val = castTo!(StringExpression)(value)) 173 messages ~= val.getString(); 132 174 if (messages.length != MID.max+1) 133 175 error(mod.firstToken, … … 139 181 if (auto val = getValue!(StringExpression)("lang_code")) 140 182 GlobalSettings.langCode = val.getString(); 183 } 184 185 /// Searches for the configuration file of dil. 186 /// Returns: the filePath or null if the file couldn't be found. 187 string findConfigurationFilePath() 188 { 189 // 1. Look in environment variable DILCONF. 190 auto filePath = new FilePath(Environment.get("DILCONF")); 191 if (filePath.exists()) 192 return filePath.toString(); 193 // 2. Look in the current working directory. 194 filePath.set(this.configFileName); 195 if (filePath.exists()) 196 return filePath.toString(); 197 // 3. Look in the directory set by HOME. 198 filePath.set(this.homePath); 199 filePath.append(this.configFileName); 200 if (filePath.exists()) 201 return filePath.toString(); 202 // 4. Look in the binary's directory. 203 filePath.set(this.executableDir); 204 filePath.append(this.configFileName); 205 if (filePath.exists()) 206 return filePath.toString(); 207 return null; 141 208 } 142 209 } … … 184 251 /// if it is relative. 185 252 /// Returns: filePath if it is absolute or execPath + filePath. 186 string resolvePath(FilePath execPath, string filePath) 187 { 188 if ((new FilePath(filePath)).isAbsolute()) 253 string resolvePath(string execPath, string filePath) 254 { 255 scope path = new FilePath(filePath); 256 if (path.isAbsolute()) 189 257 return filePath; 190 return execPath.dup.append(filePath).toString(); 258 path.set(execPath).append(filePath); 259 return path.toString(); 191 260 } 192 261 src/config.d
r806 r839 1 1 /// The configuration file of dil. 2 2 /// 3 /// Relative paths are resolved from the directory of the executable. 3 /// The file is searched for in the following order: 4 /// $(OL 5 /// $(LI The environment variable DILCONF.) 6 /// $(LI The current working directory.) 7 /// $(LI The directory set in the environment variable HOME.) 8 /// $(LI The executable's directory.) 9 /// ) 10 /// The program will fail with an error msg if the file couldn't be found.$(BR) 11 /// 12 /// The following variables are expanded inside strings (only where paths are expected): 13 /// $(UL 14 /// $(LI ${DATADIR} -> the data directory of dil (e.g. /home/user/dil/bin/data or C:\dil\bin\data).) 15 /// $(LI ${HOME} -> the home directory (e.g. /home/name or C:\Documents and Settings\name).) 16 /// $(LI ${EXECDIR} -> the absolute path to the directory of dil's executable (e.g. /home/name/dil/bin).) 17 /// ) 4 18 module config; 5 19 20 /// Files needed by dil are located in this directory. 21 /// 22 /// A relative path is resolved from the directory of dil's executable. 23 var DATADIR = "data/"; 24 6 25 /// Predefined version identifiers. 7 var version_ids= ["X86", "linux", "LittleEndian"];26 var VERSION_IDS = ["X86", "linux", "LittleEndian"]; 8 27 // "X86_64", "Windows", "Win32", "Win64", "BigEndian" 9 28 10 /// Path to the language file.11 var langfile = "lang_en.d";12 13 29 /// An array of import paths to look for modules. 14 var import_paths = []; /// E.g.: ["src/", "import/"] 30 /// 31 /// Relative paths are resolved from the current working directory. 32 var IMPORT_PATHS = []; /// E.g.: ["src/", "import/"] 15 33 16 34 /// DDoc macro file paths. 17 35 /// 18 /// Macro definitions in ddoc_files[n] override the ones in ddoc_files[n-1]. 19 var ddoc_files = ["predefined.ddoc"]; /// E.g.: ["src/mymacros.ddoc", "othermacros.ddoc"] 36 /// Macro definitions in ddoc_files[n] override the ones in ddoc_files[n-1].$(BR) 37 /// Relative paths are resolved from the current working directory. 38 var DDOC_FILES = ["${DATADIR}/predefined.ddoc"]; /// E.g.: ["src/mymacros.ddoc", "othermacros.ddoc"] 20 39 21 var xml_map = "xml_map.d"; 22 var html_map = "html_map.d"; 40 /// Path to the language file. 41 var LANG_FILE = "${DATADIR}/lang_en.d"; 42 /// Path to the xml map. 43 var XML_MAP = "${DATADIR}/xml_map.d"; 44 /// Path to the html map. 45 var HTML_MAP = "${DATADIR}/html_map.d"; 23 46 24 47 /// Customizable formats for error messages. 25 48 /// 26 /// <ul>27 /// <li>0: file path to the source text.</li>28 /// <li>1: line number.</li>29 /// <li>2: column number.</li>30 /// <li>3: error message.</li>31 /// </ul>32 var lexer_error= "{0}({1},{2})L: {3}";33 var parser_error= "{0}({1},{2})P: {3}"; /// ditto34 var semantic_error= "{0}({1},{2})S: {3}"; /// ditto49 /// $(UL 50 /// $(LI 0: file path to the source text.) 51 /// $(LI 1: line number.) 52 /// $(LI 2: column number.) 53 /// $(LI 3: error message.) 54 /// ) 55 var LEXER_ERROR = "{0}({1},{2})L: {3}"; 56 var PARSER_ERROR = "{0}({1},{2})P: {3}"; /// ditto 57 var SEMANTIC_ERROR = "{0}({1},{2})S: {3}"; /// ditto src/main.d
r832 r839 47 47 { 48 48 auto infoMan = new InfoManager(); 49 SettingsLoader.SettingsLoader(infoMan).load();49 ConfigLoader(infoMan).load(); 50 50 if (infoMan.hasInfo) 51 51 return printErrors(infoMan); … … 392 392 else if (info.classinfo is Warning.classinfo) 393 393 errorFormat = "{0}: Warning: {3}"; 394 else if (info.classinfo is dil.Information.Error.classinfo) 395 errorFormat = "Error: {3}"; 394 396 else 395 397 continue;
