Mercurial > projects > mde
view 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 |
line wrap: on
line source
/* LICENSE BLOCK Part of mde: a Modular D game-oriented Engine Copyright © 2007-2008 Diggory Hardy This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ /************************************************************************************************** * mde SSI (Single Seriazied Item) format - functions to read a file into a struct and vice-versa. * * This is a very simple format capable of handling any type supported by the (de)serializer. *************************************************************************************************/ module mde.file.ssi; import mde.file.exception; import mde.file.serialize; import tango.io.UnicodeFile; S read(S) (FilePath path) { char[] buf; try { scope file = new UnicodeFile!(char) (path.toString, Encoding.Unknown); // from BOM or use UTF-8 buf = cast(char[]) file.read; } catch (Exception e) { throw new ioException ("While reading \""~path.toString~"\": "~e.msg); } // Read header. Note: may be followed by new-line, but serializer strips white-space anyway. if (buf.length < 8 || buf[0..8] != "mdessi00") throw new parseException (path.toString ~ " is not a recognized mde ssi file: it doesn't start mdessi00"); try { return deserialize!(S) (buf[8..$]); } catch (Exception e) { throw new parseException ("Failed to read mde ssi file: "~e.msg); } } void write(S) (FilePath path, S content) { try { scope file = new UnicodeFile!(char) (path.toString, Encoding.UTF_8N); file.write ("mdessi00\n"~serialize(content), true); } catch (Exception e) { throw new ioException ("Unable to write file "~path.toString~": "~e.msg); } } debug (mdeUnitTest) { import tango.util.log.Log : Log, Logger; import tango.io.FilePath; private Logger logger; static this() { logger = Log.getLogger ("mde.file.ssi"); } unittest { struct A { float x; dchar y; long z; } A a; a.x = 0.0f; a.y = '搀'; a.z = (cast(long) uint.max) + 1; FilePath path = FilePath ("SSIUnitTest.ssi"); write (path, a); assert (a == read!(A)(path)); path.remove; // get rid of the file logger.info ("Unittest complete."); } }