Mercurial > projects > mde
comparison mde/lookup/Options.d @ 81:d8fccaa45d5f
Moved file IO code from mde/mergetag to mde/file[/mergetag] and changed how some errors are caught.
author | Diggory Hardy <diggory.hardy@gmail.com> |
---|---|
date | Fri, 29 Aug 2008 11:59:43 +0100 |
parents | ea58f277f487 |
children | ac1e3fd07275 |
comparison
equal
deleted
inserted
replaced
80:ea58f277f487 | 81:d8fccaa45d5f |
---|---|
22 module mde.lookup.Options; | 22 module mde.lookup.Options; |
23 | 23 |
24 import mde.setup.paths; | 24 import mde.setup.paths; |
25 import mde.exception; | 25 import mde.exception; |
26 | 26 |
27 import mde.mergetag.Reader; | 27 import mde.file.mergetag.Reader; |
28 import mde.mergetag.Writer; | 28 import mde.file.mergetag.Writer; |
29 import mde.mergetag.DataSet; | 29 import mde.file.mergetag.DataSet; |
30 import mde.mergetag.exception; | 30 import mde.file.serialize; |
31 import mde.mergetag.serialize; | |
32 | 31 |
33 import tango.core.Exception : ArrayBoundsException; | 32 import tango.core.Exception : ArrayBoundsException; |
34 import tango.util.log.Log : Log, Logger; | 33 import tango.util.log.Log : Log, Logger; |
35 | 34 |
36 /** Base class for handling options. | 35 /** Base class for handling options. |
132 * | 131 * |
133 * If the file doesn't exist, no reading is attempted (options are left at default values). | 132 * If the file doesn't exist, no reading is attempted (options are left at default values). |
134 */ | 133 */ |
135 private const fileName = "options"; | 134 private const fileName = "options"; |
136 void load () { | 135 void load () { |
137 // Check it exists (if not it should still be created on exit). | |
138 // Don't bother checking it's not a folder, because it could still be a block or something. | |
139 if (!confDir.exists (fileName)) return; | |
140 | |
141 try { | 136 try { |
142 IReader reader; | 137 IReader reader; |
143 reader = confDir.makeMTReader (fileName, PRIORITY.LOW_HIGH); | 138 reader = confDir.makeMTReader (fileName, PRIORITY.LOW_HIGH); |
144 reader.dataSecCreator = delegate IDataSection(ID id) { | 139 reader.dataSecCreator = delegate IDataSection(ID id) { |
145 /* Recognise each defined section, and return null for unrecognised sections. */ | 140 /* Recognise each defined section, and return null for unrecognised sections. */ |
146 Options* p = id in subClasses; | 141 Options* p = id in subClasses; |
147 if (p !is null) return *p; | 142 if (p !is null) return *p; |
148 else return null; | 143 else return null; |
149 }; | 144 }; |
150 reader.read; | 145 reader.read; |
151 } catch (MTException e) { | 146 } catch (NoFileException e) { |
152 logger.fatal ("Loading options aborted:"); | 147 // Just return. Options file will be created on exit. |
153 logger.fatal (e.msg); | 148 } catch (Exception e) { |
154 throw new optionsLoadException ("Mergetag exception (see above message)"); | 149 logger.warn ("Loading options failed: "~e.msg); |
150 logger.warn ("If warning persists, delete the offending file."); // FIXME - delete the bad file somehow | |
155 } | 151 } |
156 } | 152 } |
157 void save () { | 153 void save () { |
158 if (!changed) return; // no changes to save | 154 if (!changed) return; // no changes to save |
159 | 155 |
168 return null; // All recognised sections are already in the dataset. | 164 return null; // All recognised sections are already in the dataset. |
169 }; | 165 }; |
170 reader.read; | 166 reader.read; |
171 } catch (NoFileException) { | 167 } catch (NoFileException) { |
172 // No user file exists; not an error. | 168 // No user file exists; not an error. |
173 } catch (MTException e) { | 169 } catch (Exception e) { |
174 // Log a message and continue, overwriting the file: | 170 // Log a message and continue, overwriting the file: |
175 logger.error ("Loading options aborted:"); | 171 logger.error ("Loading options aborted: " ~ e.msg); |
176 logger.error (e.msg); | |
177 } | 172 } |
178 | 173 |
179 try { | 174 try { |
180 IWriter writer; | 175 IWriter writer; |
181 writer = confDir.makeMTWriter (fileName, ds); | 176 writer = confDir.makeMTWriter (fileName, ds); |
182 writer.write(); | 177 writer.write(); |
183 } catch (MTException e) { | 178 } catch (Exception e) { |
184 logger.error ("Saving options aborted! Reason:"); | 179 logger.error ("Saving options aborted: "~e.msg); |
185 logger.error (e.msg); | |
186 } | 180 } |
187 } | 181 } |
188 | 182 |
189 private Logger logger; | 183 private Logger logger; |
190 static this() { | 184 static this() { |