annotate mde/file/mergetag/MTTagWriter.d @ 151:e785e98d3b78

Updated for compatibility with tango 0.99.8.
author Diggory Hardy <diggory.hardy@gmail.com>
date Sat, 04 Apr 2009 17:32:18 +0200
parents 9f035cd139c6
children 1cbde9807293
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
136
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
1 /* LICENSE BLOCK
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
2 Part of mde: a Modular D game-oriented Engine
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
3 Copyright © 2007-2008 Diggory Hardy
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
4
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
5 This program is free software: you can redistribute it and/or modify it under
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
6 the terms of the GNU General Public License as published by the Free Software
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
7 Foundation, either version 2 of the License, or (at your option) any later version.
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
8
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful, but WITHOUT ANY
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
10 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
11 PARTICULAR PURPOSE. See the GNU General Public License for more details.
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
12
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
13 You should have received a copy of the GNU General Public License
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
14 along with this program. If not, see <http://www.gnu.org/licenses/>. */
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
15
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
16 /******************************************************************************
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
17 * This module contains a simpler, easier to use, mergetag writer.
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
18 *****************************************************************************/
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
19 module mde.file.mergetag.MTTagWriter;
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
20
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
21 import mde.file.mergetag.internal;
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
22 import mde.file.mergetag.exception;
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
23
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
24 import tango.io.device.File;
151
e785e98d3b78 Updated for compatibility with tango 0.99.8.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 137
diff changeset
25 import tango.io.stream.Buffered;
136
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
26 import tango.util.log.Log : Log, Logger;
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
27
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
28 private Logger logger;
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
29 static this() {
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
30 logger = Log.getLogger ("mde.file.mergetag.MTTagWriter");
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
31 }
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
32
137
9f035cd139c6 BIG commit. Major change: old Options class is gone, all content values are loaded and saved automatically. All options updated to reflect this, some changed.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 136
diff changeset
33 MTTagWriter getMTTagWriter (char[] path) {
136
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
34 if (path.length > 4 && path[$-4..$] == ".mtt")
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
35 return new MTTTagWriter (path);
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
36 else if (path.length > 4 && path[$-4..$] == ".mtb")
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
37 return new MTBTagWriter (path);
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
38 else {
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
39 logger.error ("Unable to determine writing format: text or binary");
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
40 throw new MTFileFormatException;
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
41 }
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
42 }
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
43
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
44 abstract class MTTagWriter
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
45 {
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
46 /// Set the current section
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
47 void sectionTag (char[] section);
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
48
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
49 /// Write a data tag
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
50 void dataTag (char[] type, char[] id, char[] data);
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
51
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
52 /// Change the section if necessary and write a data tag
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
53 void writeTag (char[] section, char[] type, char[] id, char[] data) {
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
54 if (section != sec)
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
55 sectionTag (section);
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
56 dataTag (type, id, data);
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
57 }
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
58
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
59 /// Close the file
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
60 void close ();
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
61
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
62 protected:
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
63 char[] sec; // current section
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
64 }
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
65
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
66 class MTTTagWriter : MTTagWriter
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
67 {
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
68 /** Opens the file path for writing. Call close() when done! */
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
69 this (char[] path) {
151
e785e98d3b78 Updated for compatibility with tango 0.99.8.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 137
diff changeset
70 buffer = new BufferedOutput (new File (path, File.WriteCreate));
136
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
71
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
72 buffer.append ("{" ~ CurrentVersionString ~ "}" ~ Eol);
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
73 }
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
74
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
75 void sectionTag (char[] section) {
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
76 sec = section;
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
77 buffer.append ("{" ~ section ~ "}" ~ Eol);
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
78 }
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
79
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
80 void dataTag (char[] type, char[] id, char[] data) {
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
81 buffer.append ("<" ~ type ~ "|" ~ id ~"=" ~ data ~ ">" ~ Eol);
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
82 }
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
83
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
84 void close () {
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
85 buffer.flush;
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
86 buffer.close;
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
87 }
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
88
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
89 private:
151
e785e98d3b78 Updated for compatibility with tango 0.99.8.
Diggory Hardy <diggory.hardy@gmail.com>
parents: 137
diff changeset
90 scope BufferedOutput buffer;
136
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
91 }
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
92
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
93 class MTBTagWriter : MTTagWriter
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
94 {
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
95 this (char[] path) {
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
96 throw new MTNotImplementedException;
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
97 }
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
98
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
99 void sectionTag (char[] section) {}
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
100 void dataTag (char[] type, char[] id, char[] data) {}
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
101 void close () {}
4084f07f2c7a Added simpler mergetag readers and writers, with unittest.
Diggory Hardy <diggory.hardy@gmail.com>
parents:
diff changeset
102 }