]> git.mxchange.org Git - simgear.git/blob - simgear/misc/props_io.cxx
- fixed XML syntax error with missing '"' for some attribute values
[simgear.git] / simgear / misc / props_io.cxx
1
2 #include <simgear/compiler.h>
3
4 #include <stdlib.h>             // atof() atoi()
5
6 #include <simgear/sg_inlines.h>
7 #include <simgear/debug/logstream.hxx>
8 #include <simgear/xml/easyxml.hxx>
9
10 #include "sg_path.hxx"
11 #include "props.hxx"
12
13 #include STL_IOSTREAM
14 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
15 #  include <fstream>
16 #else
17 #  include <fstream.h>
18 #endif
19 #include STL_STRING
20 #include <vector>
21 #include <map>
22
23 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
24 SG_USING_STD(istream);
25 SG_USING_STD(ifstream);
26 SG_USING_STD(ostream);
27 SG_USING_STD(ofstream);
28 #endif
29 SG_USING_STD(string);
30 SG_USING_STD(vector);
31 SG_USING_STD(map);
32
33
34 \f
35 ////////////////////////////////////////////////////////////////////////
36 // Property list visitor, for XML parsing.
37 ////////////////////////////////////////////////////////////////////////
38
39 class PropsVisitor : public XMLVisitor
40 {
41 public:
42
43   PropsVisitor (SGPropertyNode * root, const string &base)
44     : _ok(true), _root(root), _level(0), _base(base) {}
45
46   void startXML ();
47   void endXML ();
48   void startElement (const char * name, const XMLAttributes &atts);
49   void endElement (const char * name);
50   void data (const char * s, int length);
51   void warning (const char * message, int line, int column);
52   void error (const char * message, int line, int column);
53
54   bool isOK () const { return _ok; }
55
56 private:
57
58   struct State
59   {
60     State () : node(0), type("") {}
61     State (SGPropertyNode * _node, const char * _type)
62       : node(_node), type(_type) {}
63     SGPropertyNode * node;
64     string type;
65     map<string,int> counters;
66   };
67
68   State &state () { return _state_stack[_state_stack.size() - 1]; }
69
70   void push_state (SGPropertyNode * node, const char * type) {
71     if (type == 0)
72       _state_stack.push_back(State(node, "unknown"));
73     else
74       _state_stack.push_back(State(node, type));
75     _level++;
76     _data = "";
77   }
78
79   void pop_state () {
80     _state_stack.pop_back();
81     _level--;
82   }
83
84   bool _ok;
85   string _data;
86   SGPropertyNode * _root;
87   int _level;
88   vector<State> _state_stack;
89   string _base;
90 };
91
92 void
93 PropsVisitor::startXML ()
94 {
95   _level = 0;
96   _state_stack.resize(0);
97 }
98
99 void
100 PropsVisitor::endXML ()
101 {
102   _level = 0;
103   _state_stack.resize(0);
104 }
105
106 void
107 PropsVisitor::startElement (const char * name, const XMLAttributes &atts)
108 {
109   State &st = state();
110
111   if (_level == 0) {
112     if (string(name) != (string)"PropertyList") {
113       SG_LOG(SG_INPUT, SG_ALERT, "Root element name is " <<
114              name << "; expected PropertyList");
115       _ok = false;
116     }
117     push_state(_root, "");
118   }
119
120   else {
121                                 // Get the index.
122     const char * att_n = atts.getValue("n");
123     int index = 0;
124     if (att_n != 0) {
125       index = atoi(att_n);
126       st.counters[name] = SG_MAX2(st.counters[name], index+1);
127     } else {
128       index = st.counters[name];
129       st.counters[name]++;
130     }
131
132                                 // Check for an alias.
133     SGPropertyNode * node = st.node->getChild(name, index, true);
134     const char * att_alias = atts.getValue("alias");
135     if (att_alias != 0) {
136       if (!node->alias(att_alias))
137         SG_LOG(SG_INPUT, SG_ALERT, "Failed to set alias to " << att_alias);
138     }
139
140                                 // Check for an include.
141     const char * att_include = atts.getValue("include");
142     if (att_include != 0) {
143       SGPath path(SGPath(_base).dir());
144       cerr << "Base is " << _base << endl;
145       cerr << "Dir is " << SGPath(_base).dir() << endl;
146       path.append(att_include);
147       if (!readProperties(path.str(), node)) {
148         SG_LOG(SG_INPUT, SG_ALERT, "Failed to read include file "
149                << att_include);
150         _ok = false;
151       }
152     }
153
154     push_state(node, atts.getValue("type"));
155   }
156 }
157
158 void
159 PropsVisitor::endElement (const char * name)
160 {
161   State &st = state();
162   bool ret;
163
164                                 // If there are no children and it's
165                                 // not an alias, then it's a leaf value.
166   if (st.node->nChildren() == 0 && !st.node->isAlias()) {
167     if (st.type == "bool") {
168       if (_data == "true" || atoi(_data.c_str()) != 0)
169         ret = st.node->setBoolValue(true);
170       else
171         ret = st.node->setBoolValue(false);
172     } else if (st.type == "int") {
173       ret = st.node->setIntValue(atoi(_data.c_str()));
174     } else if (st.type == "long") {
175       ret = st.node->setLongValue(strtol(_data.c_str(), 0, 0));
176     } else if (st.type == "float") {
177       ret = st.node->setFloatValue(atof(_data.c_str()));
178     } else if (st.type == "double") {
179       ret = st.node->setDoubleValue(strtod(_data.c_str(), 0));
180     } else if (st.type == "string") {
181       ret = st.node->setStringValue(_data);
182     } else if (st.type == "unknown") {
183       ret = st.node->setUnknownValue(_data);
184     } else {
185       SG_LOG(SG_INPUT, SG_ALERT, "Unknown data type " << st.type
186              << " assuming 'unknown'");
187       ret = st.node->setUnknownValue(_data);
188     }
189     if (!ret)
190       SG_LOG(SG_INPUT, SG_ALERT, "readProperties: Failed to set "
191              << st.node->getPath() << " to value \""
192              << _data << "\" with type " << st.type);
193   }
194
195   pop_state();
196 }
197
198 void
199 PropsVisitor::data (const char * s, int length)
200 {
201   if (state().node->nChildren() == 0)
202     _data.append(string(s, length));
203 }
204
205 void
206 PropsVisitor::warning (const char * message, int line, int column)
207 {
208   SG_LOG(SG_INPUT, SG_ALERT, "readProperties: warning: "
209          << message << " at line " << line << ", column " << column);
210 }
211
212 void
213 PropsVisitor::error (const char * message, int line, int column)
214 {
215   SG_LOG(SG_INPUT, SG_ALERT, "readProperties: FATAL: " <<
216          message << " at line " << line << ", column " << column);
217   _ok = false;
218 }
219
220
221 \f
222 ////////////////////////////////////////////////////////////////////////
223 // Property list reader.
224 ////////////////////////////////////////////////////////////////////////
225
226
227 /**
228  * Read properties from an input stream.
229  *
230  * @param input The input stream containing an XML property file.
231  * @param start_node The root node for reading properties.
232  * @param base A base path for resolving external include references.
233  * @return true if the read succeeded, false otherwise.
234  */
235 bool
236 readProperties (istream &input, SGPropertyNode * start_node,
237                 const string &base)
238 {
239   PropsVisitor visitor(start_node, base);
240   return readXML(input, visitor) && visitor.isOK();
241 }
242
243
244 /**
245  * Read properties from a file.
246  *
247  * @param file A string containing the file path.
248  * @param start_node The root node for reading properties.
249  * @return true if the read succeeded, false otherwise.
250  */
251 bool
252 readProperties (const string &file, SGPropertyNode * start_node)
253 {
254   cerr << "Reading properties from " << file << endl;
255   ifstream input(file.c_str());
256   if (input.good()) {
257     return readProperties(input, start_node, file);
258   } else {
259     SG_LOG(SG_INPUT, SG_ALERT, "Error reading property list from file "
260            << file);
261     return false;
262   }
263 }
264
265
266 \f
267 ////////////////////////////////////////////////////////////////////////
268 // Property list writer.
269 ////////////////////////////////////////////////////////////////////////
270
271 #define INDENT_STEP 2
272
273 /**
274  * Return the type name.
275  */
276 static const char *
277 getTypeName (SGValue::Type type)
278 {
279   switch (type) {
280   case SGValue::UNKNOWN:
281     return "unknown";
282   case SGValue::BOOL:
283     return "bool";
284   case SGValue::INT:
285     return "int";
286   case SGValue::LONG:
287     return "long";
288   case SGValue::FLOAT:
289     return "float";
290   case SGValue::DOUBLE:
291     return "double";
292   case SGValue::STRING:
293     return "string";
294   }
295
296   // keep the compiler from squawking
297   return "unknown";
298 }
299
300
301 /**
302  * Escape characters for output.
303  */
304 static void
305 writeData (ostream &output, const string &data)
306 {
307   for (int i = 0; i < (int)data.size(); i++) {
308     switch (data[i]) {
309     case '&':
310       output << "&amp;";
311       break;
312     case '<':
313       output << "&lt;";
314       break;
315     case '>':
316       output << "&gt;";
317       break;
318     default:
319       output << data[i];
320       break;
321     }
322   }
323 }
324
325 static void
326 doIndent (ostream &output, int indent)
327 {
328   while (indent-- > 0) {
329     output << ' ';
330   }
331 }
332
333
334 static bool
335 writeNode (ostream &output, const SGPropertyNode * node, int indent)
336 {
337   const string &name = node->getName();
338   int index = node->getIndex();
339   int nChildren = node->nChildren();
340
341                                 // If there is a literal value,
342                                 // write it first.
343   if (node->hasValue()) {
344     doIndent(output, indent);
345     output << '<' << name << " n=\"" << index << '"';
346     if (node->isAlias() && node->getAliasTarget() != 0)
347       output << " alias=\"" << node->getAliasTarget()->getPath() << "\"/>";
348     else {
349       if (node->getType() != SGValue::UNKNOWN)
350         output << " type=\"" << getTypeName(node->getType()) << '"';
351       output << '>';
352       writeData(output, node->getStringValue());
353       output << "</" << name << '>' << endl;
354     }
355   }
356
357                                 // If there are children, write them
358                                 // next.
359   if (nChildren > 0 || node->isAlias()) {
360     doIndent(output, indent);
361     output << '<' << name << " n=\"" << index << '"';
362     if (node->isAlias() && node->getAliasTarget() != 0)
363       output << " alias=\"" << node->getAliasTarget()->getPath() << '"';
364     output << '>' << endl;
365     for (int i = 0; i < nChildren; i++)
366       writeNode(output, node->getChild(i), indent + INDENT_STEP);
367     doIndent(output, indent);
368     output << "</" << name << '>' << endl;
369   }
370
371                                 // If there were no children and no
372                                 // value, at least note the presence
373                                 // of the node.
374 //   if (nChildren == 0 && !node->isAlias() && !node->hasValue()) {
375 //     doIndent(output, indent);
376 //     output << '<' << name << " n=\"" << index << "\"/>" << endl;
377 //   }
378
379   return true;
380 }
381
382
383 /**
384  * Write a property tree to an output stream in XML format.
385  *
386  * @param output The output stream.
387  * @param start_node The root node to write.
388  * @return true if the write succeeded, false otherwise.
389  */
390 bool
391 writeProperties (ostream &output, const SGPropertyNode * start_node)
392 {
393   int nChildren = start_node->nChildren();
394
395   output << "<?xml version=\"1.0\"?>" << endl << endl;
396   output << "<PropertyList>" << endl;
397
398   for (int i = 0; i < nChildren; i++) {
399     writeNode(output, start_node->getChild(i), INDENT_STEP);
400   }
401
402   output << "</PropertyList>" << endl;
403
404   return true;
405 }
406
407
408 /**
409  * Write a property tree to a file in XML format.
410  *
411  * @param file The destination file.
412  * @param start_node The root node to write.
413  * @return true if the write succeeded, false otherwise.
414  */
415 bool
416 writeProperties (const string &file, const SGPropertyNode * start_node)
417 {
418   ofstream output(file.c_str());
419   if (output.good()) {
420     return writeProperties(output, start_node);
421   } else {
422     SG_LOG(SG_INPUT, SG_ALERT, "Cannot write properties to file "
423            << file);
424     return false;
425   }
426 }
427
428
429 \f
430 ////////////////////////////////////////////////////////////////////////
431 // Copy properties from one tree to another.
432 ////////////////////////////////////////////////////////////////////////
433
434
435 /**
436  * Copy one property tree to another.
437  * 
438  * @param in The source property tree.
439  * @param out The destination property tree.
440  * @return true if all properties were copied, false if some failed
441  *  (for example, if the property's value is tied read-only).
442  */
443 bool
444 copyProperties (const SGPropertyNode *in, SGPropertyNode *out)
445 {
446   bool retval = true;
447
448                                 // First, copy the actual value,
449                                 // if any.
450   if (in->hasValue()) {
451     switch (in->getType()) {
452     case SGValue::BOOL:
453       if (!out->setBoolValue(in->getBoolValue()))
454         retval = false;
455       break;
456     case SGValue::INT:
457       if (!out->setIntValue(in->getIntValue()))
458         retval = false;
459       break;
460     case SGValue::LONG:
461       if (!out->setLongValue(in->getLongValue()))
462         retval = false;
463       break;
464     case SGValue::FLOAT:
465       if (!out->setFloatValue(in->getFloatValue()))
466         retval = false;
467       break;
468     case SGValue::DOUBLE:
469       if (!out->setDoubleValue(in->getDoubleValue()))
470         retval = false;
471       break;
472     case SGValue::STRING:
473       if (!out->setStringValue(in->getStringValue()))
474         retval = false;
475       break;
476     case SGValue::UNKNOWN:
477       if (!out->setUnknownValue(in->getStringValue()))
478         retval = false;
479       break;
480     default:
481       throw string("Unknown SGValue type");
482     }
483   }
484
485                                 // Next, copy the children.
486   int nChildren = in->nChildren();
487   for (int i = 0; i < nChildren; i++) {
488     const SGPropertyNode * in_child = in->getChild(i);
489     SGPropertyNode * out_child = out->getChild(in_child->getName(),
490                                                in_child->getIndex(),
491                                                true);
492     if (!copyProperties(in_child, out_child))
493       retval = false;
494   }
495
496   return retval;
497 }
498
499 // end of props_io.cxx