comparison mde/file/mergetag/Writer.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 b16a534f5302
children 9f035cd139c6
comparison
equal deleted inserted replaced
135:bc697a218716 136:4084f07f2c7a
36 36
37 // tango imports 37 // tango imports
38 import tango.core.Exception; 38 import tango.core.Exception;
39 import tango.io.device.File; 39 import tango.io.device.File;
40 import tango.io.stream.Buffer; 40 import tango.io.stream.Buffer;
41 import convInt = tango.text.convert.Integer;
42 import tango.util.log.Log : Log, Logger; 41 import tango.util.log.Log : Log, Logger;
43 42
44 private Logger logger; 43 private Logger logger;
45 static this () { 44 static this () {
46 logger = Log.getLogger ("mde.file.mergetag.Writer"); 45 logger = Log.getLogger ("mde.file.mergetag.Writer");
133 void dataset (DataSet ds) /// ditto 132 void dataset (DataSet ds) /// ditto
134 { _dataset = ds; } 133 { _dataset = ds; }
135 134
136 135
137 private: 136 private:
138 // taken from tango.io.Console, mostly to make sure notepad can read our files:
139 version (Win32)
140 const char[] Eol = "\r\n";
141 else
142 const char[] Eol = "\n";
143
144 /* The container where data is written from. */ 137 /* The container where data is written from. */
145 DataSet _dataset; 138 DataSet _dataset;
146 139
147 char[] _path; 140 char[] _path;
148 //END DATA 141 //END DATA
186 179
187 buffer = new BufferOutput(conduit); // And a buffer 180 buffer = new BufferOutput(conduit); // And a buffer
188 scope(exit) buffer.flush(); 181 scope(exit) buffer.flush();
189 182
190 // Write the header: 183 // Write the header:
191 buffer.append ("{MT" ~ MTFormatVersion.CurrentString ~ "}" ~ Eol); 184 buffer.append ("{" ~ CurrentVersionString ~ "}" ~ Eol);
192 if (_dataset.header !is null) writeSection (buffer, _dataset.header); 185 if (_dataset.header !is null) writeSection (buffer, _dataset.header);
193 186
194 // Write the rest: 187 // Write the rest:
195 foreach (ID id, IDataSection sec; _dataset.sec) { 188 foreach (ID id, IDataSection sec; _dataset.sec) {
196 writeSectionIdentifier (buffer, id); 189 writeSectionIdentifier (buffer, id);
207 throwMTErr ("Unexpected exception when writing file: " ~ e.msg); 200 throwMTErr ("Unexpected exception when writing file: " ~ e.msg);
208 } 201 }
209 } 202 }
210 203
211 private void writeSectionIdentifier (BufferOutput buffer, ID id) { 204 private void writeSectionIdentifier (BufferOutput buffer, ID id) {
212 char[] tp = "{" ~ cast(char[])id ~ "}" ~ Eol; 205 buffer.append ("{" ~ id ~ "}" ~ Eol);
213 buffer.append (tp);
214 } 206 }
215 207
216 private void writeSection (BufferOutput buffer, IDataSection sec) { 208 private void writeSection (BufferOutput buffer, IDataSection sec) {
217 void writeItem (char[] tp, ID id, char[] dt) { // actually writes an item 209 void writeItem (char[] tp, ID id, char[] dt) { // actually writes an item
218 buffer.append ("<" ~ tp ~ "|" ~ cast(char[])id ~"=" ~ dt ~ ">" ~ Eol); 210 buffer.append ("<" ~ tp ~ "|" ~ id ~"=" ~ dt ~ ">" ~ Eol);
219 } 211 }
220 sec.writeAll (&writeItem); 212 sec.writeAll (&writeItem);
221 213
222 buffer.append (Eol); // blank line at end of each section 214 buffer.append (Eol); // blank line at end of each section
223 } 215 }