comparison mde/mergetag/read.d @ 2:78eb491bd642

mergetag: partially redesigned dataset and text reader classes. Changed text format. committer: Diggory Hardy <diggory.hardy@gmail.com>
author Diggory Hardy <diggory.hardy@gmail.com>
date Sat, 03 Nov 2007 15:15:43 +0000
parents 18491334a525
children 485c98ecbd91
comparison
equal deleted inserted replaced
1:18491334a525 2:78eb491bd642
61 */ 61 */
62 DataSection function (ID) dataSecCreator = null; 62 DataSection function (ID) dataSecCreator = null;
63 63
64 private: 64 private:
65 // Static symbols: 65 // Static symbols:
66 typedef void delegate (char[],ID,char[]) readDelg; // Delegate for accepting tags. 66 typedef void delegate (TypeInfo,ID,char[]) readDelg; // Delegate for accepting tags.
67 67
68 static bool initialised = false;
69 static TypeInfo[char[]] typeTable;
68 static Logger logger; 70 static Logger logger;
69 71
70 // Error messages as const variables. Could be loaded from files to support other languages? 72 // Error messages as const variables. Could be loaded from files to support other languages?
71 static const char[] ERR_FILEREAD = "Error reading file: "; 73 static const char[] ERR_FILEREAD = "Error reading file: ";
72 static const char[] ERR_MTHEAD = "Not a valid MergeTag text file"; 74 static const char[] ERR_MTHEAD = "Not a valid MergeTag text file";
102 } 104 }
103 SecMD [ID] secTable; 105 SecMD [ID] secTable;
104 //END DATA 106 //END DATA
105 107
106 //BEGIN METHODS: CTOR / DTOR 108 //BEGIN METHODS: CTOR / DTOR
107 static this () { 109 // Could be a static this(), but this way it's only called if the class is used.
110 private void init () {
111 init_addType!(bool);
112 init_addType!(byte);
113 init_addType!(short);
114 init_addType!(int);
115 init_addType!(long);
116 init_addType!(ubyte);
117 init_addType!(ushort);
118 init_addType!(uint);
119 init_addType!(ulong);
120 init_addType!(char);
121 init_addType!(float);
122 init_addType!(double);
123 init_addType!(real);
124 init_addType!(bool[]);
125 init_addType!(byte[]);
126 init_addType!(short[]);
127 init_addType!(int[]);
128 init_addType!(long[]);
129 init_addType!(ubyte[]);
130 init_addType!(ushort[]);
131 init_addType!(uint[]);
132 init_addType!(ulong[]);
133 init_addType!(char[]);
134 init_addType!(float[]);
135 init_addType!(double[]);
136 init_addType!(real[]);
137 // aliases:
138 typeTable["string"] = typeid(char[]);
139 typeTable["binary"] = typeid(ubyte[]);
140
108 logger = Log.getLogger ("mde.mergetag.read.Reader"); 141 logger = Log.getLogger ("mde.mergetag.read.Reader");
142 initialised = true;
143 }
144 private static void init_addType(T) () {
145 typeTable[T.stringof] = typeid(T);
109 } 146 }
110 147
111 /** Tries to open file path and read it into a buffer. 148 /** Tries to open file path and read it into a buffer.
112 * 149 *
113 * Params: 150 * Params:
134 public this (char[] path, DataSet* dataset_ = null, bool rdHeader = false) { 171 public this (char[] path, DataSet* dataset_ = null, bool rdHeader = false) {
135 this (new FilePath (path), dataset_, rdHeader); 172 this (new FilePath (path), dataset_, rdHeader);
136 } 173 }
137 /** ditto */ 174 /** ditto */
138 public this (PathView path, DataSet* dataset_ = null, bool rdHeader = false) { 175 public this (PathView path, DataSet* dataset_ = null, bool rdHeader = false) {
176 if (!initialised) init(); // on-demand static this()
177
139 // Create a dataset or use an existing one 178 // Create a dataset or use an existing one
140 if (dataset_) dataset = *dataset_; 179 if (dataset_) dataset = *dataset_;
141 else dataset = new DataSet(); 180 else dataset = new DataSet();
142 181
143 // Open & read the file 182 // Open & read the file
300 pos_s = pos; 339 pos_s = pos;
301 fbufLocateDataTagChar (pos, true); // find end of data section 340 fbufLocateDataTagChar (pos, true); // find end of data section
302 if (fbuf[pos] != '>') throwMTErr (ERR_DTAG ~ ErrInFile); 341 if (fbuf[pos] != '>') throwMTErr (ERR_DTAG ~ ErrInFile);
303 data = fbuf[pos_s..pos]; 342 data = fbuf[pos_s..pos];
304 343
305 if (!comment) { 344 if (!comment && addTag != null) {
306 if (addTag != null) addTag (type, tagID, data); 345 TypeInfo* ti_p = type in typeTable;
346 if (ti_p) addTag (*ti_p, tagID, data);
347 else logger.warn ("Type not supported: " ~ type);
307 } else comment = false; // cancel comment status now 348 } else comment = false; // cancel comment status now
308 } 349 }
309 else if (fbuf[pos] == '{') { 350 else if (fbuf[pos] == '{') {
310 if (comment) { // simple block comment 351 if (comment) { // simple block comment
311 uint depth = 0; // depth of embedded comment blocks 352 uint depth = 0; // depth of embedded comment blocks