comparison mde/file/mergetag/MTTagWriter.d @ 136:4084f07f2c7a

Added simpler mergetag readers and writers, with unittest.
author Diggory Hardy <diggory.hardy@gmail.com>
date Sun, 01 Feb 2009 12:36:21 +0000
parents
children 9f035cd139c6
comparison
equal deleted inserted replaced
135:bc697a218716 136:4084f07f2c7a
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
6 the terms of the GNU General Public License as published by the Free Software
7 Foundation, either 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
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
11 PARTICULAR PURPOSE. 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 * This module contains a simpler, easier to use, mergetag writer.
18 *****************************************************************************/
19 module mde.file.mergetag.MTTagWriter;
20
21 import mde.file.mergetag.internal;
22 import mde.file.mergetag.exception;
23
24 import tango.io.device.File;
25 import tango.io.stream.Buffer;
26 import tango.util.log.Log : Log, Logger;
27
28 private Logger logger;
29 static this() {
30 logger = Log.getLogger ("mde.file.mergetag.MTTagWriter");
31 }
32
33 MTTagWriter makeMTTagWriter (char[] path) {
34 if (path.length > 4 && path[$-4..$] == ".mtt")
35 return new MTTTagWriter (path);
36 else if (path.length > 4 && path[$-4..$] == ".mtb")
37 return new MTBTagWriter (path);
38 else {
39 logger.error ("Unable to determine writing format: text or binary");
40 throw new MTFileFormatException;
41 }
42 }
43
44 abstract class MTTagWriter
45 {
46 /// Set the current section
47 void sectionTag (char[] section);
48
49 /// Write a data tag
50 void dataTag (char[] type, char[] id, char[] data);
51
52 /// Change the section if necessary and write a data tag
53 void writeTag (char[] section, char[] type, char[] id, char[] data) {
54 if (section != sec)
55 sectionTag (section);
56 dataTag (type, id, data);
57 }
58
59 /// Close the file
60 void close ();
61
62 protected:
63 char[] sec; // current section
64 }
65
66 class MTTTagWriter : MTTagWriter
67 {
68 /** Opens the file path for writing. Call close() when done! */
69 this (char[] path) {
70 buffer = new BufferOutput (new File (path, File.WriteCreate));
71
72 buffer.append ("{" ~ CurrentVersionString ~ "}" ~ Eol);
73 }
74
75 void sectionTag (char[] section) {
76 sec = section;
77 buffer.append ("{" ~ section ~ "}" ~ Eol);
78 }
79
80 void dataTag (char[] type, char[] id, char[] data) {
81 buffer.append ("<" ~ type ~ "|" ~ id ~"=" ~ data ~ ">" ~ Eol);
82 }
83
84 void close () {
85 buffer.flush;
86 buffer.close;
87 }
88
89 private:
90 scope BufferOutput buffer;
91 }
92
93 class MTBTagWriter : MTTagWriter
94 {
95 this (char[] path) {
96 throw new MTNotImplementedException;
97 }
98
99 void sectionTag (char[] section) {}
100 void dataTag (char[] type, char[] id, char[] data) {}
101 void close () {}
102 }