This repository has been archived by the owner on Apr 30, 2023. It is now read-only.
/
conf.d
193 lines (175 loc) · 5.27 KB
/
conf.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*
Copyright (c) 2011-2014 Timur Gafarov
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
module conf;
import std.stdio;
import std.file;
import lexer;
version(Windows) string OS = "windows";
version(linux) string OS = "linux";
final class Config
{
string[string] data;
bool has(string key, bool platformSpecific = true)
{
if (platformSpecific)
{
string oskey = OS ~ "." ~ key;
if (oskey in data) return true;
}
if (key in data) return true;
else return false;
}
string get(string key, bool platformSpecific = true)
{
if (platformSpecific)
{
string oskey = OS ~ "." ~ key;
if (oskey in data) return data[oskey];
}
if (key in data) return data[key];
else return "";
}
/*
* TODO: The following should be rewrote in more clever way...
*/
void set(string key, string value, bool platformSpecific = true)
{
if (platformSpecific)
{
if (key.length > 7 && (key[0..7] == "windows" || key[0..5] == "linux"))
{
}
else
{
string oskey = OS ~ "." ~ key;
data[oskey] = value;
}
}
data[key] = value;
}
void append(string key, string value, bool platformSpecific = true)
{
if (platformSpecific)
{
if (key.length > 7 && (key[0..7] == "windows" || key[0..5] == "linux"))
{
}
else
{
string oskey = OS ~ "." ~ key;
data[oskey] ~= value;
}
}
data[key] ~= value;
}
}
/*
* Parse configuration file
*/
enum
{
tIdentifier,
tColon,
tValue,
tSemicolon
}
void readConfiguration(Config options, string filename)
{
auto text = readText(filename);
auto lex = new Lexer(text);
lex.addDelimiters();
auto nextToken = tIdentifier;
string tempId = "_default_";
string lexeme = "";
do
{
lexeme = lex.getLexeme();
if (lexeme.length > 0)
{
if (lexeme == ";")
{
if (nextToken == tSemicolon)
nextToken = tIdentifier;
else throw new Exception("unexpected \"" ~ lexeme ~ "\"");
}
else if (lexeme == ":")
{
if (nextToken == tColon)
nextToken = tValue;
else throw new Exception("unexpected \"" ~ lexeme ~ "\"");
}
else
{
if (nextToken == tIdentifier)
{
tempId = lexeme;
nextToken = tColon;
}
else if (nextToken == tValue)
{
if (lexeme[0] == '\"' && lexeme[$-1] == '\"')
{
if (lexeme.length > 2)
options.set(tempId, lexeme[1..$-1]);
else options.set(tempId, "");
}
else options.set(tempId, lexeme);
tempId = "_default_";
nextToken = tSemicolon;
}
else throw new Exception("unexpected \"" ~ lexeme ~ "\"");
}
}
}
while (lexeme.length > 0);
}
string formatPattern(string pat, Config data, dchar formattingSymbol)
{
string result;
string temp;
bool appending = true;
foreach(c; pat)
{
if (c == formattingSymbol)
{
if (appending)
{
appending = false;
}
else
{
appending = true;
if (data.has(temp))
result ~= formatPattern(data.get(temp), data, formattingSymbol); //data[temp]
temp = "";
}
}
else
{
if (appending) result ~= c;
else temp ~= c;
}
}
return result;
}