annotate src/SettingsLoader.d @ 839:4063da6f3edd default tip

Refactored the config file and how it is loaded.
author Aziz K?ksal <aziz.koeksal@gmail.com>
date Thu, 21 Aug 2008 17:51:04 +0200
parents c60bd5cd61da
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
513
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
1 /++
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
2 Author: Aziz Köksal
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
3 License: GPL3
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
4 +/
779
8e6fed11bb68 Moved Settings.d and SettingsLoader.d to src/.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 778
diff changeset
5 module SettingsLoader;
513
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
6
779
8e6fed11bb68 Moved Settings.d and SettingsLoader.d to src/.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 778
diff changeset
7 import Settings;
513
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
8 import dil.Messages;
593
2848ce3becf5 Moved dil.Module to dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 586
diff changeset
9 import dil.ast.Node, dil.ast.Declarations, dil.ast.Expressions;
2848ce3becf5 Moved dil.Module to dil.semantic.Module.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 586
diff changeset
10 import dil.semantic.Module;
758
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
11 import dil.semantic.Pass1;
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
12 import dil.semantic.Symbol;
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
13 import dil.semantic.Symbols;
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
14 import dil.Information;
778
78be32e3e157 Implemented conditional compilation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 759
diff changeset
15 import dil.Compilation;
513
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
16 import common;
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
17
758
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
18 import tango.io.FilePath;
839
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
19 import tango.sys.Environment;
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
20 import tango.text.Util : substitute;
513
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
21
798
c24be8d4f6ab Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 779
diff changeset
22 /// Loads settings from a D module file.
839
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
23 abstract class SettingsLoader
758
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
24 {
798
c24be8d4f6ab Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 779
diff changeset
25 InfoManager infoMan; /// Collects error messages.
758
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
26 Module mod; /// Current module.
513
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
27
759
9c47f377ca0b Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 758
diff changeset
28 this(InfoManager infoMan)
9c47f377ca0b Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 758
diff changeset
29 {
9c47f377ca0b Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 758
diff changeset
30 this.infoMan = infoMan;
9c47f377ca0b Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 758
diff changeset
31 }
9c47f377ca0b Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 758
diff changeset
32
798
c24be8d4f6ab Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 779
diff changeset
33 /// Creates an error report.
c24be8d4f6ab Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 779
diff changeset
34 /// Params:
c24be8d4f6ab Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 779
diff changeset
35 /// token = where the error occurred.
c24be8d4f6ab Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 779
diff changeset
36 /// formatMsg = error message.
758
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
37 void error(Token* token, char[] formatMsg, ...)
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
38 {
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
39 auto location = token.getErrorLocation();
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
40 auto msg = Format(_arguments, _argptr, formatMsg);
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
41 infoMan ~= new SemanticError(location, msg);
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
42 }
513
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
43
758
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
44 T getValue(T)(char[] name)
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
45 {
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
46 auto var = mod.lookup(name);
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
47 if (!var) // Returning T.init instead of null, because dmd gives an error.
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
48 return error(mod.firstToken, "variable '{}' is not defined", name), T.init;
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
49 auto t = var.node.begin;
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
50 if (!var.isVariable)
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
51 return error(t, "'{}' is not a variable declaration", name), T.init;
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
52 auto value = var.to!(Variable).value;
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
53 if (!value)
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
54 return error(t, "'{}' variable has no value set", name), T.init;
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
55 T val = value.Is!(T); // Try casting to T.
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
56 if (!val)
831
c60bd5cd61da Fixed code that got broken by changes in dmd.1.033.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 810
diff changeset
57 error(value.begin, "the value of '{}' is not of type {}", name, T.stringof);
758
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
58 return val;
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
59 }
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
60
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
61 T castTo(T)(Node n)
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
62 {
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
63 char[] type;
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
64 is(T == StringExpression) && (type = "char[]");
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
65 if (!n.Is!(T))
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
66 error(n.begin, "expression is not of type {}", type);
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
67 return n.Is!(T);
513
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
68 }
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
69
758
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
70 void load()
839
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
71 {}
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
72 }
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
73
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
74 /// Loads the configuration file of dil.
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
75 class ConfigLoader : SettingsLoader
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
76 {
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
77 static string configFileName = "config.d"; /// Name of the configuration file.
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
78 string executablePath; /// Absolute path to the executable of dil.
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
79 string executableDir; /// Absolte path to the directory of the executable of dil.
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
80 string dataDir; /// Absolute path to dil's data directory.
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
81 string homePath; /// Path to the home directory.
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
82
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
83 this(InfoManager infoMan)
513
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
84 {
839
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
85 super(infoMan);
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
86 }
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
87
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
88 static ConfigLoader opCall(InfoManager infoMan)
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
89 {
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
90 return new ConfigLoader(infoMan);
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
91 }
758
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
92
839
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
93 string expandVariables(string val)
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
94 {
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
95 val = substitute(val, "${DATADIR}", dataDir);
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
96 val = substitute(val, "${HOME}", homePath);
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
97 val = substitute(val, "${EXECDIR}", executableDir);
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
98 return val;
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
99 }
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
100
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
101 void load()
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
102 {
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
103 homePath = Environment.get("HOME");
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
104 executablePath = GetExecutableFilePath();
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
105 executableDir = (new FilePath(executablePath)).folder();
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
106
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
107 // Load the configuration file.
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
108 auto filePath = findConfigurationFilePath();
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
109 if (filePath is null)
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
110 {
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
111 infoMan ~= new Error(new Location("",0),
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
112 "the configuration file "~configFileName~" could not be found.");
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
113 return;
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
114 }
758
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
115 mod = new Module(filePath, infoMan);
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
116 mod.parse();
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
117
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
118 if (mod.hasErrors)
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
119 return;
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
120
778
78be32e3e157 Implemented conditional compilation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 759
diff changeset
121 auto context = new CompilationContext;
78be32e3e157 Implemented conditional compilation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 759
diff changeset
122 auto pass1 = new SemanticPass1(mod, context);
810
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 806
diff changeset
123 pass1.run();
758
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
124
839
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
125 // Initialize the dataDir member.
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
126 if (auto val = getValue!(StringExpression)("DATADIR"))
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
127 dataDir = val.getString();
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
128 dataDir = resolvePath(executableDir, dataDir);
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
129 GlobalSettings.dataDir = dataDir;
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
130
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
131 if (auto array = getValue!(ArrayInitExpression)("VERSION_IDS"))
778
78be32e3e157 Implemented conditional compilation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 759
diff changeset
132 foreach (value; array.values)
839
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
133 if (auto val = castTo!(StringExpression)(value))
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
134 GlobalSettings.versionIds ~= val.getString();
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
135 if (auto val = getValue!(StringExpression)("LANG_FILE"))
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
136 GlobalSettings.langFile = expandVariables(val.getString());
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
137 if (auto array = getValue!(ArrayInitExpression)("IMPORT_PATHS"))
758
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
138 foreach (value; array.values)
839
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
139 if (auto val = castTo!(StringExpression)(value))
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
140 GlobalSettings.importPaths ~= expandVariables(val.getString());
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
141 if (auto array = getValue!(ArrayInitExpression)("DDOC_FILES"))
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
142 foreach (value; array.values)
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
143 if (auto val = castTo!(StringExpression)(value))
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
144 GlobalSettings.ddocFilePaths ~= expandVariables(val.getString());
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
145 if (auto val = getValue!(StringExpression)("XML_MAP"))
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
146 GlobalSettings.xmlMapFile = expandVariables(val.getString());
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
147 if (auto val = getValue!(StringExpression)("HTML_MAP"))
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
148 GlobalSettings.htmlMapFile = expandVariables(val.getString());
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
149 if (auto val = getValue!(StringExpression)("LEXER_ERROR"))
758
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
150 GlobalSettings.lexerErrorFormat = val.getString();
839
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
151 if (auto val = getValue!(StringExpression)("PARSER_ERROR"))
758
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
152 GlobalSettings.parserErrorFormat = val.getString();
839
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
153 if (auto val = getValue!(StringExpression)("SEMANTIC_ERROR"))
758
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
154 GlobalSettings.semanticErrorFormat = val.getString();
513
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
155
758
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
156 // Load language file.
839
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
157 // TODO: create a separate class for this?
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
158 filePath = expandVariables(GlobalSettings.langFile);
758
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
159 mod = new Module(filePath);
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
160 mod.parse();
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
161
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
162 if (mod.hasErrors)
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
163 return;
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
164
778
78be32e3e157 Implemented conditional compilation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 759
diff changeset
165 pass1 = new SemanticPass1(mod, context);
810
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 806
diff changeset
166 pass1.run();
758
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
167
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
168 if (auto array = getValue!(ArrayInitExpression)("messages"))
513
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
169 {
758
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
170 char[][] messages;
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
171 foreach (value; array.values)
839
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
172 if (auto val = castTo!(StringExpression)(value))
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
173 messages ~= val.getString();
758
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
174 if (messages.length != MID.max+1)
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
175 error(mod.firstToken,
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
176 "messages table in {} must exactly have {} entries, but not {}.",
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
177 filePath, MID.max+1, messages.length);
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
178 GlobalSettings.messages = messages;
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
179 dil.Messages.SetMessages(messages);
513
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
180 }
758
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
181 if (auto val = getValue!(StringExpression)("lang_code"))
f4b9680c0e16 Revised module dil.SettingsLoader.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 731
diff changeset
182 GlobalSettings.langCode = val.getString();
513
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
183 }
839
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
184
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
185 /// Searches for the configuration file of dil.
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
186 /// Returns: the filePath or null if the file couldn't be found.
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
187 string findConfigurationFilePath()
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
188 {
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
189 // 1. Look in environment variable DILCONF.
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
190 auto filePath = new FilePath(Environment.get("DILCONF"));
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
191 if (filePath.exists())
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
192 return filePath.toString();
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
193 // 2. Look in the current working directory.
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
194 filePath.set(this.configFileName);
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
195 if (filePath.exists())
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
196 return filePath.toString();
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
197 // 3. Look in the directory set by HOME.
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
198 filePath.set(this.homePath);
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
199 filePath.append(this.configFileName);
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
200 if (filePath.exists())
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
201 return filePath.toString();
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
202 // 4. Look in the binary's directory.
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
203 filePath.set(this.executableDir);
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
204 filePath.append(this.configFileName);
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
205 if (filePath.exists())
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
206 return filePath.toString();
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
207 return null;
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
208 }
513
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
209 }
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
210
798
c24be8d4f6ab Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 779
diff changeset
211 /// Loads an associative array from a D module file.
759
9c47f377ca0b Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 758
diff changeset
212 class TagMapLoader : SettingsLoader
9c47f377ca0b Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 758
diff changeset
213 {
9c47f377ca0b Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 758
diff changeset
214 this(InfoManager infoMan)
9c47f377ca0b Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 758
diff changeset
215 {
9c47f377ca0b Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 758
diff changeset
216 super(infoMan);
9c47f377ca0b Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 758
diff changeset
217 }
9c47f377ca0b Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 758
diff changeset
218
9c47f377ca0b Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 758
diff changeset
219 static TagMapLoader opCall(InfoManager infoMan)
9c47f377ca0b Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 758
diff changeset
220 {
9c47f377ca0b Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 758
diff changeset
221 return new TagMapLoader(infoMan);
9c47f377ca0b Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 758
diff changeset
222 }
9c47f377ca0b Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 758
diff changeset
223
9c47f377ca0b Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 758
diff changeset
224 string[string] load(string filePath)
9c47f377ca0b Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 758
diff changeset
225 {
9c47f377ca0b Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 758
diff changeset
226 mod = new Module(filePath, infoMan);
9c47f377ca0b Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 758
diff changeset
227 mod.parse();
9c47f377ca0b Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 758
diff changeset
228 if (mod.hasErrors)
9c47f377ca0b Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 758
diff changeset
229 return null;
9c47f377ca0b Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 758
diff changeset
230
778
78be32e3e157 Implemented conditional compilation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 759
diff changeset
231 auto context = new CompilationContext;
78be32e3e157 Implemented conditional compilation.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 759
diff changeset
232 auto pass1 = new SemanticPass1(mod, context);
810
525ee3f848d9 Added modules cmd.Compile and dil.ModuleManager.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 806
diff changeset
233 pass1.run();
759
9c47f377ca0b Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 758
diff changeset
234
9c47f377ca0b Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 758
diff changeset
235 string[string] map;
9c47f377ca0b Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 758
diff changeset
236 if (auto array = getValue!(ArrayInitExpression)("map"))
9c47f377ca0b Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 758
diff changeset
237 foreach (i, value; array.values)
9c47f377ca0b Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 758
diff changeset
238 {
9c47f377ca0b Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 758
diff changeset
239 auto key = array.keys[i];
9c47f377ca0b Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 758
diff changeset
240 if (auto valExp = castTo!(StringExpression)(value))
9c47f377ca0b Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 758
diff changeset
241 if (!key)
9c47f377ca0b Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 758
diff changeset
242 error(value.begin, "expected key : value");
9c47f377ca0b Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 758
diff changeset
243 else if (auto keyExp = castTo!(StringExpression)(key))
9c47f377ca0b Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 758
diff changeset
244 map[keyExp.getString()] = valExp.getString();
9c47f377ca0b Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 758
diff changeset
245 }
9c47f377ca0b Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 758
diff changeset
246 return map;
9c47f377ca0b Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 758
diff changeset
247 }
9c47f377ca0b Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 758
diff changeset
248 }
9c47f377ca0b Revised module cmd.Generate.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 758
diff changeset
249
798
c24be8d4f6ab Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 779
diff changeset
250 /// Resolves the path to a file from the executable's dir path
c24be8d4f6ab Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 779
diff changeset
251 /// if it is relative.
c24be8d4f6ab Added documentation comments.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 779
diff changeset
252 /// Returns: filePath if it is absolute or execPath + filePath.
839
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
253 string resolvePath(string execPath, string filePath)
721
8955296dd807 Added ddoc_files option to config.d
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 695
diff changeset
254 {
839
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
255 scope path = new FilePath(filePath);
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
256 if (path.isAbsolute())
721
8955296dd807 Added ddoc_files option to config.d
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 695
diff changeset
257 return filePath;
839
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
258 path.set(execPath).append(filePath);
4063da6f3edd Refactored the config file and how it is loaded.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 831
diff changeset
259 return path.toString();
721
8955296dd807 Added ddoc_files option to config.d
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 695
diff changeset
260 }
8955296dd807 Added ddoc_files option to config.d
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 695
diff changeset
261
779
8e6fed11bb68 Moved Settings.d and SettingsLoader.d to src/.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 778
diff changeset
262 version(DDoc)
8e6fed11bb68 Moved Settings.d and SettingsLoader.d to src/.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 778
diff changeset
263 {
8e6fed11bb68 Moved Settings.d and SettingsLoader.d to src/.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 778
diff changeset
264 /// Returns the fully qualified path to this executable.
8e6fed11bb68 Moved Settings.d and SettingsLoader.d to src/.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 778
diff changeset
265 char[] GetExecutableFilePath();
8e6fed11bb68 Moved Settings.d and SettingsLoader.d to src/.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 778
diff changeset
266 }
8e6fed11bb68 Moved Settings.d and SettingsLoader.d to src/.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 778
diff changeset
267 else version(Windows)
513
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
268 {
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
269 private extern(Windows) uint GetModuleFileNameA(void*, char*, uint);
779
8e6fed11bb68 Moved Settings.d and SettingsLoader.d to src/.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 778
diff changeset
270
513
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
271 char[] GetExecutableFilePath()
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
272 {
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
273 alias GetModuleFileNameA GetModuleFileName;
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
274 char[] buffer = new char[256];
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
275 uint count;
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
276
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
277 while (1)
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
278 {
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
279 if (buffer is null)
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
280 return null;
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
281
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
282 count = GetModuleFileName(null, buffer.ptr, buffer.length);
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
283 if (count == 0)
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
284 return null;
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
285 if (buffer.length != count && buffer[count] == 0)
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
286 break;
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
287 // Increase size of buffer
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
288 buffer.length = buffer.length * 2;
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
289 }
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
290 assert(buffer[count] == 0);
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
291 // Reduce buffer to the actual length of the string (excluding '\0'.)
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
292 if (count < buffer.length)
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
293 buffer.length = count;
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
294 return buffer;
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
295 }
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
296 }
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
297 else version(linux)
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
298 {
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
299 private extern(C) size_t readlink(char* path, char* buf, size_t bufsize);
779
8e6fed11bb68 Moved Settings.d and SettingsLoader.d to src/.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 778
diff changeset
300
513
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
301 char[] GetExecutableFilePath()
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
302 {
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
303 char[] buffer = new char[256];
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
304 size_t count;
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
305
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
306 while (1)
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
307 {
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
308 // This won't work on very old Linux systems.
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
309 count = readlink("/proc/self/exe".ptr, buffer.ptr, buffer.length);
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
310 if (count == -1)
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
311 return null;
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
312 if (count < buffer.length)
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
313 break;
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
314 buffer.length = buffer.length * 2;
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
315 }
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
316 buffer.length = count;
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
317 return buffer;
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
318 }
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
319 }
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
320 else
6160ab7b1816 Refactored code related to settings.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents:
diff changeset
321 static assert(0, "GetExecutableFilePath() is not implemented on this platform.");