annotate trunk/src/dil/Settings.d @ 445:294353fe2514

Added functions for finding the executables path.
author Aziz K?ksal <aziz.koeksal@gmail.com>
date Wed, 17 Oct 2007 17:38:54 +0200
parents 3751db263679
children 325714d8aa6c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
309
b4d842b0d2c7 - Added new files Settings.d, config.d and lang_en.d
aziz
parents:
diff changeset
1 /++
b4d842b0d2c7 - Added new files Settings.d, config.d and lang_en.d
aziz
parents:
diff changeset
2 Author: Aziz Köksal
b4d842b0d2c7 - Added new files Settings.d, config.d and lang_en.d
aziz
parents:
diff changeset
3 License: GPL3
b4d842b0d2c7 - Added new files Settings.d, config.d and lang_en.d
aziz
parents:
diff changeset
4 +/
326
4a7359b88c11 - Added package 'dil' to module declarations.
aziz
parents: 325
diff changeset
5 module dil.Settings;
327
a48a987f7515 - Added package dil to import declarations.
aziz
parents: 326
diff changeset
6 import dil.Messages;
a48a987f7515 - Added package dil to import declarations.
aziz
parents: 326
diff changeset
7 import dil.Parser, dil.SyntaxTree, dil.Declarations, dil.Expressions;
358
0faf57d99c1c - Replaced calls to std.file.read() with dil.loadFile().
aziz
parents: 349
diff changeset
8 import dil.File;
428
3751db263679 Looking for config.d and language file in executables folder now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 399
diff changeset
9 import tango.io.FilePath;
391
33b566df6af4 Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 368
diff changeset
10 import std.metastrings : FormatT = Format, ToString;
33b566df6af4 Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 368
diff changeset
11 import common;
309
b4d842b0d2c7 - Added new files Settings.d, config.d and lang_en.d
aziz
parents:
diff changeset
12
331
cff12a1999ae - Added template Pad for padding numbers.
aziz
parents: 329
diff changeset
13 template Pad(char[] str, uint amount)
cff12a1999ae - Added template Pad for padding numbers.
aziz
parents: 329
diff changeset
14 {
cff12a1999ae - Added template Pad for padding numbers.
aziz
parents: 329
diff changeset
15 static if (str.length >= amount)
cff12a1999ae - Added template Pad for padding numbers.
aziz
parents: 329
diff changeset
16 const char[] Pad = str;
cff12a1999ae - Added template Pad for padding numbers.
aziz
parents: 329
diff changeset
17 else
cff12a1999ae - Added template Pad for padding numbers.
aziz
parents: 329
diff changeset
18 const char[] Pad = "0" ~ Pad!(str, amount-1);
cff12a1999ae - Added template Pad for padding numbers.
aziz
parents: 329
diff changeset
19 }
cff12a1999ae - Added template Pad for padding numbers.
aziz
parents: 329
diff changeset
20
cff12a1999ae - Added template Pad for padding numbers.
aziz
parents: 329
diff changeset
21 template Pad(int num, uint amount)
cff12a1999ae - Added template Pad for padding numbers.
aziz
parents: 329
diff changeset
22 {
cff12a1999ae - Added template Pad for padding numbers.
aziz
parents: 329
diff changeset
23 const char[] Pad = Pad!(ToString!(num), amount);
cff12a1999ae - Added template Pad for padding numbers.
aziz
parents: 329
diff changeset
24 }
cff12a1999ae - Added template Pad for padding numbers.
aziz
parents: 329
diff changeset
25
333
268d680d5275 - Moved Pad template to the top.
aziz
parents: 331
diff changeset
26 version(D2)
268d680d5275 - Moved Pad template to the top.
aziz
parents: 331
diff changeset
27 {
268d680d5275 - Moved Pad template to the top.
aziz
parents: 331
diff changeset
28 const VERSION_MAJOR = 2;
268d680d5275 - Moved Pad template to the top.
aziz
parents: 331
diff changeset
29 const VERSION_MINOR = 0;
268d680d5275 - Moved Pad template to the top.
aziz
parents: 331
diff changeset
30 }
268d680d5275 - Moved Pad template to the top.
aziz
parents: 331
diff changeset
31 else
268d680d5275 - Moved Pad template to the top.
aziz
parents: 331
diff changeset
32 {
268d680d5275 - Moved Pad template to the top.
aziz
parents: 331
diff changeset
33 const VERSION_MAJOR = 1;
268d680d5275 - Moved Pad template to the top.
aziz
parents: 331
diff changeset
34 const VERSION_MINOR = 0;
268d680d5275 - Moved Pad template to the top.
aziz
parents: 331
diff changeset
35 }
268d680d5275 - Moved Pad template to the top.
aziz
parents: 331
diff changeset
36
391
33b566df6af4 Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 368
diff changeset
37 const string VERSION = FormatT!("%s.%s", VERSION_MAJOR, Pad!(VERSION_MINOR, 3));
343
95f1b6e43214 - Removed TOK.Special and added an own entry for each special token.
aziz
parents: 339
diff changeset
38 const VENDOR = "dil";
309
b4d842b0d2c7 - Added new files Settings.d, config.d and lang_en.d
aziz
parents:
diff changeset
39
339
9954367bcd4b - Removed usageGenerate from Settings.d. Added some comments.
aziz
parents: 333
diff changeset
40 /// Used in main help message.
309
b4d842b0d2c7 - Added new files Settings.d, config.d and lang_en.d
aziz
parents:
diff changeset
41 const COMPILED_WITH = __VENDOR__;
339
9954367bcd4b - Removed usageGenerate from Settings.d. Added some comments.
aziz
parents: 333
diff changeset
42 /// ditto
391
33b566df6af4 Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 368
diff changeset
43 const COMPILED_VERSION = FormatT!("%s.%s", __VERSION__/1000, Pad!(__VERSION__%1000, 3));
339
9954367bcd4b - Removed usageGenerate from Settings.d. Added some comments.
aziz
parents: 333
diff changeset
44 /// ditto
309
b4d842b0d2c7 - Added new files Settings.d, config.d and lang_en.d
aziz
parents:
diff changeset
45 const COMPILED_DATE = __TIMESTAMP__;
b4d842b0d2c7 - Added new files Settings.d, config.d and lang_en.d
aziz
parents:
diff changeset
46
b4d842b0d2c7 - Added new files Settings.d, config.d and lang_en.d
aziz
parents:
diff changeset
47 struct GlobalSettings
b4d842b0d2c7 - Added new files Settings.d, config.d and lang_en.d
aziz
parents:
diff changeset
48 {
b4d842b0d2c7 - Added new files Settings.d, config.d and lang_en.d
aziz
parents:
diff changeset
49 static:
391
33b566df6af4 Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 368
diff changeset
50 string language; /// Language of loaded messages catalogue.
309
b4d842b0d2c7 - Added new files Settings.d, config.d and lang_en.d
aziz
parents:
diff changeset
51 string[] messages; /// Table of localized compiler messages.
367
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 358
diff changeset
52 string[] importPaths; /// Array of import paths to look for modules.
445
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
53 void load()
309
b4d842b0d2c7 - Added new files Settings.d, config.d and lang_en.d
aziz
parents:
diff changeset
54 {
445
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
55 scope execPath = new FilePath(GetExecutableFilePath());
428
3751db263679 Looking for config.d and language file in executables folder now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 399
diff changeset
56 auto fileName = execPath.file("config.d").toUtf8();
358
0faf57d99c1c - Replaced calls to std.file.read() with dil.loadFile().
aziz
parents: 349
diff changeset
57 auto sourceText = loadFile(fileName);
309
b4d842b0d2c7 - Added new files Settings.d, config.d and lang_en.d
aziz
parents:
diff changeset
58 auto parser = new Parser(sourceText, fileName);
368
2adf808343d6 - Renamed method start() to init() in Parser.
aziz
parents: 367
diff changeset
59 auto root = parser.start();
309
b4d842b0d2c7 - Added new files Settings.d, config.d and lang_en.d
aziz
parents:
diff changeset
60
b4d842b0d2c7 - Added new files Settings.d, config.d and lang_en.d
aziz
parents:
diff changeset
61 if (parser.errors.length || parser.lx.errors.length)
b4d842b0d2c7 - Added new files Settings.d, config.d and lang_en.d
aziz
parents:
diff changeset
62 {
b4d842b0d2c7 - Added new files Settings.d, config.d and lang_en.d
aziz
parents:
diff changeset
63 throw new Exception("There are errors in " ~ fileName ~ ".");
b4d842b0d2c7 - Added new files Settings.d, config.d and lang_en.d
aziz
parents:
diff changeset
64 }
b4d842b0d2c7 - Added new files Settings.d, config.d and lang_en.d
aziz
parents:
diff changeset
65
b4d842b0d2c7 - Added new files Settings.d, config.d and lang_en.d
aziz
parents:
diff changeset
66 foreach (decl; root.children)
b4d842b0d2c7 - Added new files Settings.d, config.d and lang_en.d
aziz
parents:
diff changeset
67 {
b4d842b0d2c7 - Added new files Settings.d, config.d and lang_en.d
aziz
parents:
diff changeset
68 auto v = Cast!(VariableDeclaration)(decl);
367
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 358
diff changeset
69 if (v is null)
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 358
diff changeset
70 continue;
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 358
diff changeset
71 auto vname = v.idents[0].srcText;
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 358
diff changeset
72 if (vname == "langfile")
309
b4d842b0d2c7 - Added new files Settings.d, config.d and lang_en.d
aziz
parents:
diff changeset
73 {
b4d842b0d2c7 - Added new files Settings.d, config.d and lang_en.d
aziz
parents:
diff changeset
74 auto e = v.values[0];
b4d842b0d2c7 - Added new files Settings.d, config.d and lang_en.d
aziz
parents:
diff changeset
75 if (!e)
349
a0711c57c1db - Added variable 'lang_code' to language files.
aziz
parents: 343
diff changeset
76 throw new Exception("langfile variable has no value set.");
399
ff1d11c27061 Renamed some Expression classes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 394
diff changeset
77 auto val = Cast!(StringExpression)(e);
309
b4d842b0d2c7 - Added new files Settings.d, config.d and lang_en.d
aziz
parents:
diff changeset
78 if (val)
349
a0711c57c1db - Added variable 'lang_code' to language files.
aziz
parents: 343
diff changeset
79 // Set fileName to d-file with messages table.
a0711c57c1db - Added variable 'lang_code' to language files.
aziz
parents: 343
diff changeset
80 fileName = val.getString();
367
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 358
diff changeset
81 }
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 358
diff changeset
82 else if (vname == "import_paths")
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 358
diff changeset
83 {
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 358
diff changeset
84 auto e = v.values[0];
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 358
diff changeset
85 if (e is null)
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 358
diff changeset
86 throw new Exception("import_paths variable has no variable set.");
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 358
diff changeset
87 if (auto array = Cast!(ArrayInitializer)(e))
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 358
diff changeset
88 {
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 358
diff changeset
89 foreach (value; array.values)
399
ff1d11c27061 Renamed some Expression classes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 394
diff changeset
90 if (auto str = Cast!(StringExpression)(value))
367
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 358
diff changeset
91 GlobalSettings.importPaths ~= str.getString();
309
b4d842b0d2c7 - Added new files Settings.d, config.d and lang_en.d
aziz
parents:
diff changeset
92 }
367
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 358
diff changeset
93 else
dda55fae37de - ImportGraph.execute() can parse all modules depending on the imports of the root module.
aziz
parents: 358
diff changeset
94 throw new Exception("import_paths variable is set to "~e.classinfo.name~" instead of an ArrayInitializer.");
309
b4d842b0d2c7 - Added new files Settings.d, config.d and lang_en.d
aziz
parents:
diff changeset
95 }
b4d842b0d2c7 - Added new files Settings.d, config.d and lang_en.d
aziz
parents:
diff changeset
96 }
b4d842b0d2c7 - Added new files Settings.d, config.d and lang_en.d
aziz
parents:
diff changeset
97
b4d842b0d2c7 - Added new files Settings.d, config.d and lang_en.d
aziz
parents:
diff changeset
98 // Load messages
428
3751db263679 Looking for config.d and language file in executables folder now.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 399
diff changeset
99 sourceText = loadFile(execPath.file(fileName).toUtf8());
349
a0711c57c1db - Added variable 'lang_code' to language files.
aziz
parents: 343
diff changeset
100 parser = new Parser(sourceText, fileName);
368
2adf808343d6 - Renamed method start() to init() in Parser.
aziz
parents: 367
diff changeset
101 root = parser.start();
349
a0711c57c1db - Added variable 'lang_code' to language files.
aziz
parents: 343
diff changeset
102
a0711c57c1db - Added variable 'lang_code' to language files.
aziz
parents: 343
diff changeset
103 if (parser.errors.length || parser.lx.errors.length)
309
b4d842b0d2c7 - Added new files Settings.d, config.d and lang_en.d
aziz
parents:
diff changeset
104 {
349
a0711c57c1db - Added variable 'lang_code' to language files.
aziz
parents: 343
diff changeset
105 throw new Exception("There are errors in "~fileName~".");
a0711c57c1db - Added variable 'lang_code' to language files.
aziz
parents: 343
diff changeset
106 }
309
b4d842b0d2c7 - Added new files Settings.d, config.d and lang_en.d
aziz
parents:
diff changeset
107
349
a0711c57c1db - Added variable 'lang_code' to language files.
aziz
parents: 343
diff changeset
108 char[][] messages;
a0711c57c1db - Added variable 'lang_code' to language files.
aziz
parents: 343
diff changeset
109 foreach (decl; root.children)
a0711c57c1db - Added variable 'lang_code' to language files.
aziz
parents: 343
diff changeset
110 {
a0711c57c1db - Added variable 'lang_code' to language files.
aziz
parents: 343
diff changeset
111 auto v = Cast!(VariableDeclaration)(decl);
a0711c57c1db - Added variable 'lang_code' to language files.
aziz
parents: 343
diff changeset
112 if (v is null)
a0711c57c1db - Added variable 'lang_code' to language files.
aziz
parents: 343
diff changeset
113 continue;
a0711c57c1db - Added variable 'lang_code' to language files.
aziz
parents: 343
diff changeset
114 if (v.idents[0].srcText == "messages")
309
b4d842b0d2c7 - Added new files Settings.d, config.d and lang_en.d
aziz
parents:
diff changeset
115 {
349
a0711c57c1db - Added variable 'lang_code' to language files.
aziz
parents: 343
diff changeset
116 auto e = v.values[0];
a0711c57c1db - Added variable 'lang_code' to language files.
aziz
parents: 343
diff changeset
117 if (!e)
a0711c57c1db - Added variable 'lang_code' to language files.
aziz
parents: 343
diff changeset
118 throw new Exception("messages variable in "~fileName~" has no value set.");
a0711c57c1db - Added variable 'lang_code' to language files.
aziz
parents: 343
diff changeset
119 if (auto array = Cast!(ArrayInitializer)(e))
309
b4d842b0d2c7 - Added new files Settings.d, config.d and lang_en.d
aziz
parents:
diff changeset
120 {
349
a0711c57c1db - Added variable 'lang_code' to language files.
aziz
parents: 343
diff changeset
121 foreach (value; array.values)
309
b4d842b0d2c7 - Added new files Settings.d, config.d and lang_en.d
aziz
parents:
diff changeset
122 {
399
ff1d11c27061 Renamed some Expression classes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 394
diff changeset
123 if (auto str = Cast!(StringExpression)(value))
349
a0711c57c1db - Added variable 'lang_code' to language files.
aziz
parents: 343
diff changeset
124 messages ~= str.getString();
309
b4d842b0d2c7 - Added new files Settings.d, config.d and lang_en.d
aziz
parents:
diff changeset
125 }
b4d842b0d2c7 - Added new files Settings.d, config.d and lang_en.d
aziz
parents:
diff changeset
126 }
349
a0711c57c1db - Added variable 'lang_code' to language files.
aziz
parents: 343
diff changeset
127 else
a0711c57c1db - Added variable 'lang_code' to language files.
aziz
parents: 343
diff changeset
128 throw new Exception("messages variable is set to "~e.classinfo.name~" instead of an ArrayInitializer.");
309
b4d842b0d2c7 - Added new files Settings.d, config.d and lang_en.d
aziz
parents:
diff changeset
129 }
349
a0711c57c1db - Added variable 'lang_code' to language files.
aziz
parents: 343
diff changeset
130 else if(v.idents[0].srcText == "lang_code")
a0711c57c1db - Added variable 'lang_code' to language files.
aziz
parents: 343
diff changeset
131 {
a0711c57c1db - Added variable 'lang_code' to language files.
aziz
parents: 343
diff changeset
132 auto e = v.values[0];
a0711c57c1db - Added variable 'lang_code' to language files.
aziz
parents: 343
diff changeset
133 if (!e)
a0711c57c1db - Added variable 'lang_code' to language files.
aziz
parents: 343
diff changeset
134 throw new Exception("lang_code variable in "~fileName~" has no value set.");
399
ff1d11c27061 Renamed some Expression classes.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 394
diff changeset
135 if (auto str = Cast!(StringExpression)(e))
349
a0711c57c1db - Added variable 'lang_code' to language files.
aziz
parents: 343
diff changeset
136 GlobalSettings.language = str.getString();
a0711c57c1db - Added variable 'lang_code' to language files.
aziz
parents: 343
diff changeset
137 }
309
b4d842b0d2c7 - Added new files Settings.d, config.d and lang_en.d
aziz
parents:
diff changeset
138 }
349
a0711c57c1db - Added variable 'lang_code' to language files.
aziz
parents: 343
diff changeset
139 if (messages.length != MID.max+1)
391
33b566df6af4 Migrated project to Tango.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 368
diff changeset
140 throw new Exception(Format("messages table in {0} must exactly have {1} entries, but {2} were found.", fileName, MID.max+1, messages.length));
349
a0711c57c1db - Added variable 'lang_code' to language files.
aziz
parents: 343
diff changeset
141 GlobalSettings.messages = messages;
394
6440da4adb07 Fixed forward references of enum MID complaints by compiler.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 391
diff changeset
142 dil.Messages.SetMessages(messages);
309
b4d842b0d2c7 - Added new files Settings.d, config.d and lang_en.d
aziz
parents:
diff changeset
143 }
b4d842b0d2c7 - Added new files Settings.d, config.d and lang_en.d
aziz
parents:
diff changeset
144 }
445
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
145
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
146 version(Windows)
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
147 {
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
148 private extern(Windows) uint GetModuleFileNameA(void*, char*, uint);
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
149 /++
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
150 Get the fully qualified path to this executable.
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
151 +/
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
152 char[] GetExecutableFilePath()
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
153 {
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
154 alias GetModuleFileNameA GetModuleFileName;
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
155 char[] buffer = new char[256];
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
156 uint count;
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
157
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
158 while (1)
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
159 {
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
160 if (buffer is null)
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
161 return null;
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
162
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
163 count = GetModuleFileName(null, buffer.ptr, buffer.length);
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
164 if (count == 0)
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
165 return null;
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
166 if (buffer.length != count && buffer[count] == 0)
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
167 break;
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
168 // Increase size of buffer
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
169 buffer.length = buffer.length * 2;
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
170 }
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
171 assert(buffer[count] == 0);
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
172 // Reduce buffer to the actual length of the string (excluding '\0'.)
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
173 if (count < buffer.length)
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
174 buffer.length = count;
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
175 return buffer;
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
176 }
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
177 }
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
178 else version(linux)
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
179 {
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
180 private extern(C) size_t readlink(char* path, char* buf, size_t bufsize);
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
181 /++
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
182 Get the fully qualified path to this executable.
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
183 +/
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
184 char[] GetExecutableFilePath()
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
185 {
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
186 char[] buffer = new char[256];
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
187 size_t count;
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
188
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
189 while (1)
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
190 {
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
191 // This won't work on very old Linux systems.
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
192 count = readlink("/proc/self/exe".ptr, buffer.ptr, buffer.length);
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
193 if (count == -1)
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
194 return null;
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
195 if (count < buffer.length)
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
196 break;
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
197 buffer.length = buffer.length * 2;
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
198 }
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
199 buffer.length = count;
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
200 return buffer;
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
201 }
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
202 }
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
203 else
294353fe2514 Added functions for finding the executables path.
Aziz K?ksal <aziz.koeksal@gmail.com>
parents: 428
diff changeset
204 static assert(0, "GetExecutableFilePath() is not implemented on your platform.");