Mercurial > projects > mde
annotate mde/file/ssi.d @ 114:b16a534f5302
Changes for tango r4201. Added override keyword in a lot of places.
author | Diggory Hardy <diggory.hardy@gmail.com> |
---|---|
date | Fri, 19 Dec 2008 15:15:06 +0000 |
parents | ac1e3fd07275 |
children |
rev | line source |
---|---|
82 | 1 /* LICENSE BLOCK |
2 Part of mde: a Modular D game-oriented Engine | |
3 Copyright © 2007-2008 Diggory Hardy | |
4 | |
5 This program is free software: you can redistribute it and/or modify it under the terms | |
6 of the GNU General Public License as published by the Free Software Foundation, either | |
7 version 2 of the License, or (at your option) any later version. | |
8 | |
9 This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; | |
10 without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
11 See the GNU General Public License for more details. | |
12 | |
13 You should have received a copy of the GNU General Public License | |
14 along with this program. If not, see <http://www.gnu.org/licenses/>. */ | |
15 | |
16 /************************************************************************************************** | |
17 * mde SSI (Single Seriazied Item) format - functions to read a file into a struct and vice-versa. | |
18 * | |
19 * This is a very simple format capable of handling any type supported by the (de)serializer. | |
20 *************************************************************************************************/ | |
21 module mde.file.ssi; | |
22 | |
23 import mde.file.exception; | |
24 import mde.file.serialize; | |
25 | |
26 import tango.io.UnicodeFile; | |
27 | |
28 S read(S) (FilePath path) { | |
29 char[] buf; | |
30 try { | |
114
b16a534f5302
Changes for tango r4201. Added override keyword in a lot of places.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
82
diff
changeset
|
31 scope file = new UnicodeFile!(char) (path.toString, Encoding.Unknown); // from BOM or use UTF-8 |
82 | 32 buf = cast(char[]) file.read; |
33 } catch (Exception e) { | |
34 throw new ioException ("While reading \""~path.toString~"\": "~e.msg); | |
35 } | |
36 | |
37 // Read header. Note: may be followed by new-line, but serializer strips white-space anyway. | |
38 if (buf.length < 8 || buf[0..8] != "mdessi00") | |
39 throw new parseException (path.toString ~ " is not a recognized mde ssi file: it doesn't start mdessi00"); | |
40 | |
41 try { | |
42 return deserialize!(S) (buf[8..$]); | |
43 } catch (Exception e) { | |
44 throw new parseException ("Failed to read mde ssi file: "~e.msg); | |
45 } | |
46 } | |
47 | |
48 void write(S) (FilePath path, S content) { | |
49 try { | |
114
b16a534f5302
Changes for tango r4201. Added override keyword in a lot of places.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
82
diff
changeset
|
50 scope file = new UnicodeFile!(char) (path.toString, Encoding.UTF_8N); |
82 | 51 file.write ("mdessi00\n"~serialize(content), true); |
52 } catch (Exception e) { | |
53 throw new ioException ("Unable to write file "~path.toString~": "~e.msg); | |
54 } | |
55 } | |
56 | |
57 debug (mdeUnitTest) { | |
58 import tango.util.log.Log : Log, Logger; | |
59 import tango.io.FilePath; | |
60 | |
61 private Logger logger; | |
62 static this() { | |
63 logger = Log.getLogger ("mde.file.ssi"); | |
64 } | |
65 | |
66 unittest { | |
67 struct A { | |
68 float x; | |
69 dchar y; | |
70 long z; | |
71 } | |
72 A a; | |
73 a.x = 0.0f; | |
74 a.y = '搀'; | |
75 a.z = (cast(long) uint.max) + 1; | |
76 | |
77 FilePath path = FilePath ("SSIUnitTest.ssi"); | |
78 write (path, a); | |
79 assert (a == read!(A)(path)); | |
80 path.remove; // get rid of the file | |
81 | |
82 logger.info ("Unittest complete."); | |
83 } | |
84 } |