Mercurial > projects > dynamin
annotate dynamin/core/settings.d @ 106:acdbb30fee7e
Port to D2.
Most of the effort was dealing with immutable and const.
author | Jordan Miner <jminer7@gmail.com> |
---|---|
date | Mon, 17 Dec 2012 23:41:50 -0600 |
parents | 73060bc3f004 |
children |
rev | line source |
---|---|
0 | 1 |
2 /* | |
103
73060bc3f004
Change license to Boost 1.0 and MPL 2.0.
Jordan Miner <jminer7@gmail.com>
parents:
61
diff
changeset
|
3 * Copyright Jordan Miner |
0 | 4 * |
103
73060bc3f004
Change license to Boost 1.0 and MPL 2.0.
Jordan Miner <jminer7@gmail.com>
parents:
61
diff
changeset
|
5 * Distributed under the Boost Software License, Version 1.0. |
73060bc3f004
Change license to Boost 1.0 and MPL 2.0.
Jordan Miner <jminer7@gmail.com>
parents:
61
diff
changeset
|
6 * (See accompanying file BOOST_LICENSE.txt or copy at |
73060bc3f004
Change license to Boost 1.0 and MPL 2.0.
Jordan Miner <jminer7@gmail.com>
parents:
61
diff
changeset
|
7 * http://www.boost.org/LICENSE_1_0.txt) |
0 | 8 * |
9 */ | |
10 | |
11 module dynamin.core.settings; | |
12 | |
13 import dynamin.core.string; | |
14 import tango.io.device.Conduit; | |
15 import tango.io.device.File; | |
16 import tango.io.device.Array; | |
17 import tango.io.stream.Text; | |
18 import tango.io.Stdout; | |
19 import tango.util.Convert; | |
20 import tango.core.Exception; | |
21 | |
22 // TODO: | |
23 align(1) | |
24 struct Pixel32 { | |
25 version(BigEndian) { | |
26 ubyte A; | |
27 ubyte R; | |
28 ubyte G; | |
29 ubyte B; | |
30 } else { | |
31 ubyte B; | |
32 ubyte G; | |
33 ubyte R; | |
34 ubyte A; | |
35 } | |
36 } | |
37 class Color2 { | |
106 | 38 Pixel32 toPixel32() { |
0 | 39 Pixel32 px; |
40 return px; | |
41 } | |
106 | 42 string toSetting() { |
43 auto px = toPixel32(); | |
0 | 44 return format("{}, {}, {}", px.R, px.G, px.B); |
45 } | |
106 | 46 static Color2 fromSetting(cstring str) { |
0 | 47 // allow "#AB00F2", "171, 0, 242" |
48 return null; | |
49 } | |
50 } | |
51 | |
52 string test = r" | |
53 ; Here is a comment | |
54 [Main] | |
55 UndoLevels=500 | |
56 # Here is another comment | |
57 CaretColor=255, 0, 0 | |
58 LineNumsVisible=true | |
59 | |
60 [RubyMode] | |
61 TabSize=4 | |
62 "; | |
63 unittest { | |
61
f8f5d6244795
Edit the Settings unittest to load, save, then load the saved test data.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
64 // test saving to the file |
0 | 65 auto settings = new Settings; |
66 settings.loadFromString(test); | |
106 | 67 test = settings.saveToString().idup; |
61
f8f5d6244795
Edit the Settings unittest to load, save, then load the saved test data.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
68 |
f8f5d6244795
Edit the Settings unittest to load, save, then load the saved test data.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
69 // test reading from the file |
f8f5d6244795
Edit the Settings unittest to load, save, then load the saved test data.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
70 settings = new Settings; |
f8f5d6244795
Edit the Settings unittest to load, save, then load the saved test data.
Jordan Miner <jminer7@gmail.com>
parents:
0
diff
changeset
|
71 settings.loadFromString(test); |
0 | 72 assert(settings.get("UndoLevels") == "500"); |
73 assert(settings.get("TabSize", "RubyMode") == "4"); | |
74 assert(getSetting!(int)(settings, "UndoLevels") == 500); | |
75 assert(getSetting!(bool)(settings, "LineNumsVisible") == true); | |
76 } | |
77 | |
78 /** | |
79 * This class will read from and write to what is basically a Windows INI file. | |
80 * Here is what a file would look like: | |
81 * Example: | |
82 * ----- | |
83 * ; Here is a comment | |
84 * [Main] | |
85 * UndoLevels=500 | |
86 * # Here is another comment | |
87 * CaretColor=255, 0, 0 | |
88 * LineNumsVisible=true | |
89 * | |
90 * [RubyMode] | |
91 * TabSize=4 | |
92 * ----- | |
93 */ | |
94 class Settings { | |
95 protected: | |
96 string[string][string] sections; | |
97 string[] comment; | |
98 void loadFromStream(InputStream stream) { | |
99 auto input = new TextInput(stream); | |
100 string section = MainSectionName; | |
101 bool inStartComment = true; | |
102 int lineNum = 0; | |
106 | 103 foreach(line; input) { |
0 | 104 lineNum++; |
105 // check for a line with just whitespace | |
106 if(line.trim().length == 0) | |
107 continue; | |
108 | |
109 // check for a comment | |
110 if(line.trimLeft().startsWith(";") || | |
111 line.trimLeft().startsWith("#")) { | |
112 if(inStartComment) { | |
113 comment.length = comment.length + 1; | |
106 | 114 comment[$-1] = line.trimLeft()[1..$].idup; |
0 | 115 } |
116 continue; | |
117 } | |
118 inStartComment = false; | |
119 | |
120 // check for a section header | |
121 if(line.startsWith("[")) { | |
122 if(!line.endsWith("]") || line.length < 3) | |
123 throw new Exception("Invalid section on line " ~ to!(string)(lineNum)); | |
106 | 124 section = line[1..$-1].idup; |
0 | 125 continue; |
126 } | |
127 | |
128 // parse key=value line (quickly) | |
129 int eqIndex; | |
130 for(eqIndex = 0; eqIndex < line.length; ++eqIndex) | |
131 if(line[eqIndex] == '=') | |
132 break; | |
133 if(eqIndex == line.length) | |
134 throw new Exception("Invalid format on line " ~ to!(string)(lineNum)); | |
106 | 135 string value = cast(immutable)(line[eqIndex+1..$].unescape()); |
136 sections[section][line[0..eqIndex].idup] = value; | |
0 | 137 } |
138 } | |
139 void saveToStream(OutputStream stream) { | |
140 auto output = new TextOutput(stream); | |
141 foreach(line; comment) | |
142 output(';')(line).newline; | |
143 foreach(string sectionName, string[string] sectionData; sections) { | |
144 output('[')(sectionName)(']').newline;// TODO: newline before each section | |
145 foreach(key, value; sectionData) | |
146 output(key)('=')(value).newline; // TODO: escape? | |
147 } | |
148 } | |
149 public: | |
106 | 150 enum string MainSectionName = "Main"; |
0 | 151 /** |
152 * Parses the specified file. | |
153 */ | |
106 | 154 void load(cstring file) { |
0 | 155 scope f = new File(file); |
156 loadFromStream(f); | |
157 } | |
106 | 158 void loadFromString(cstring str) { |
159 auto a = new Array(cast(void[])str); | |
0 | 160 loadFromStream(a); |
161 } | |
106 | 162 mstring saveToString() { |
0 | 163 scope a = new Array(256, 80); |
164 saveToStream(a); | |
106 | 165 return cast(mstring)a.slice(a.readable); |
0 | 166 } |
167 /** | |
168 * | |
169 * Examples: | |
170 * ----- | |
171 * settings.get("UndoLevels"); | |
172 * settings.get("TabSize", "RubyMode"); | |
173 * ----- | |
174 */ | |
106 | 175 string get(cstring name, cstring section = MainSectionName) { |
0 | 176 auto sect = section in sections; |
177 if(sect) { | |
178 auto val = name in *sect; | |
179 if(val) | |
180 return *val; | |
181 } | |
182 return ""; | |
183 } | |
184 | |
185 /** | |
186 * Examples: | |
187 * ----- | |
188 * settings.set("UndoLevels", "500") | |
189 * settings.set("TabSize", "4", "RubyMode"); | |
190 * ----- | |
191 */ | |
192 void set(string name, string value, string section = MainSectionName) { | |
193 if(section.contains('[') || section.contains(']')) | |
194 throw new IllegalArgumentException( | |
195 "Section name cannot contain a bracket"); | |
196 if(name.contains('=')) | |
197 throw new IllegalArgumentException( | |
198 "name cannot contain an equal sign"); | |
199 sections[section][name] = value; | |
200 } | |
201 /** | |
202 * Gets or sets the comment that appears at the beginning of the settings | |
203 * file. This is set when a file is read in. | |
204 */ | |
205 string[] commentLines() { | |
206 return comment; | |
207 } | |
208 /// ditto | |
209 void commentLines(string[] lines) { | |
210 comment = lines; | |
211 } | |
212 } | |
213 | |
214 /** | |
215 * Examples: | |
216 * ----- | |
217 * getSetting!(int)(settings, "UndoLevels"); | |
218 * getSetting!(int)(settings, "TabSize", "RubyMode"); | |
219 * ----- | |
220 */ | |
221 T getSetting(T)(Settings settings, | |
222 string name, string section = Settings.MainSectionName) { | |
223 string str = settings.get(name, section); | |
224 static if(is(T == bool)) | |
225 return str == "true" || str == "yes" || str == "on"; | |
226 else static if(is(T : long)) | |
227 return cast(T)to!(T)(str); | |
228 else | |
106 | 229 return T.fromSetting(str); |
0 | 230 } |
231 | |
232 /** | |
233 * Examples: | |
234 * ----- | |
235 * setSetting!(int)(settings, "UndoLevels", 500); | |
236 * setSetting!(int)(settings, "TabSize", 4, "RubyMode"); | |
237 * ----- | |
238 */ | |
239 void setSetting(T)(Settings settings, | |
240 string name, T value, string section = Settings.MainSectionName) { | |
106 | 241 mstring str; |
0 | 242 static if(is(T == bool)) |
243 str = value ? "true" : "false"; | |
244 else static if(is(T : long)) | |
106 | 245 str = to!(mstring)(value); |
0 | 246 else |
106 | 247 str = T.toSetting(); |
0 | 248 |
106 | 249 settings.set(name, str, section); |
0 | 250 } |
251 |