]> git.mxchange.org Git - simgear.git/blob - simgear/misc/props_io.cxx
Added exceptions.[ch]xx
[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 #define DEFAULT_MODE (SGPropertyNode::READ|SGPropertyNode::WRITE)
34
35
36 \f
37 ////////////////////////////////////////////////////////////////////////
38 // Property list visitor, for XML parsing.
39 ////////////////////////////////////////////////////////////////////////
40
41 class PropsVisitor : public XMLVisitor
42 {
43 public:
44
45   PropsVisitor (SGPropertyNode * root, const string &base)
46     : _root(root), _level(0), _base(base), _exception(0) {}
47
48   virtual ~PropsVisitor () { delete _exception; }
49
50   void startXML ();
51   void endXML ();
52   void startElement (const char * name, const XMLAttributes &atts);
53   void endElement (const char * name);
54   void data (const char * s, int length);
55   void warning (const char * message, int line, int column);
56
57   bool hasException () const { return (_exception != 0); }
58   const sg_io_exception * getException () const { return _exception; }
59
60 private:
61
62   struct State
63   {
64     State () : node(0), type(""), mode(DEFAULT_MODE) {}
65     State (SGPropertyNode * _node, const char * _type, int _mode)
66       : node(_node), type(_type), mode(_mode) {}
67     SGPropertyNode * node;
68     string type;
69     int mode;
70     map<string,int> counters;
71   };
72
73   State &state () { return _state_stack[_state_stack.size() - 1]; }
74
75   void push_state (SGPropertyNode * node, const char * type, int mode) {
76     if (type == 0)
77       _state_stack.push_back(State(node, "unspecified", mode));
78     else
79       _state_stack.push_back(State(node, type, mode));
80     _level++;
81     _data = "";
82   }
83
84   void pop_state () {
85     _state_stack.pop_back();
86     _level--;
87   }
88
89   string _data;
90   SGPropertyNode * _root;
91   int _level;
92   vector<State> _state_stack;
93   string _base;
94   sg_io_exception * _exception;
95 };
96
97 void
98 PropsVisitor::startXML ()
99 {
100   _level = 0;
101   _state_stack.resize(0);
102 }
103
104 void
105 PropsVisitor::endXML ()
106 {
107   _level = 0;
108   _state_stack.resize(0);
109 }
110
111
112 /**
113  * Check a yes/no flag, with default.
114  */
115 static bool
116 checkFlag (const char * flag, bool defaultState = true)
117 {
118   if (flag == 0)
119     return defaultState;
120   else if (string(flag) == "y")
121     return true;
122   else if (string(flag) == "n")
123     return false;
124   else {
125     string message = "Unrecognized flag value '";
126     message += flag;
127     message += '\'';
128                                 // FIXME: add location info
129     throw sg_io_exception(message, "SimGear Property Reader");
130   }
131 }
132
133 void
134 PropsVisitor::startElement (const char * name, const XMLAttributes &atts)
135 {
136   State &st = state();
137
138   if (_level == 0) {
139     if (string(name) != (string)"PropertyList") {
140       string message = "Root element name is ";
141       message += name;
142       message += "; expected PropertyList";
143       throw sg_io_exception(message, "SimGear Property Reader");
144     }
145     push_state(_root, "", DEFAULT_MODE);
146   }
147
148   else {
149
150     const char * attval;
151                                 // Get the index.
152     attval = atts.getValue("n");
153     int index = 0;
154     if (attval != 0) {
155       index = atoi(attval);
156       st.counters[name] = SG_MAX2(st.counters[name], index+1);
157     } else {
158       index = st.counters[name];
159       st.counters[name]++;
160     }
161
162                                 // Got the index, so grab the node.
163     SGPropertyNode * node = st.node->getChild(name, index, true);
164
165                                 // Get the access-mode attributes,
166                                 // but don't set yet (in case they
167                                 // prevent us from recording the value).
168     int mode = 0;
169
170     attval = atts.getValue("read");
171     if (checkFlag(attval, true))
172       mode |= SGPropertyNode::READ;
173     attval = atts.getValue("write");
174     if (checkFlag(attval, true))
175       mode |= SGPropertyNode::WRITE;
176     attval = atts.getValue("archive");
177     if (checkFlag(attval, false))
178       mode |= SGPropertyNode::ARCHIVE;
179
180                                 // Check for an alias.
181     attval = atts.getValue("alias");
182     if (attval != 0) {
183       if (!node->alias(attval))
184         SG_LOG(SG_INPUT, SG_ALERT, "Failed to set alias to " << attval);
185     }
186
187                                 // Check for an include.
188     attval = atts.getValue("include");
189     if (attval != 0) {
190       SGPath path(SGPath(_base).dir());
191       path.append(attval);
192       try {
193         readProperties(path.str(), node);
194       } catch (const sg_io_exception &t) {
195         cerr << "Caught exception\n";
196         _exception = t.clone();
197       }
198     }
199
200     push_state(node, atts.getValue("type"), mode);
201   }
202 }
203
204 void
205 PropsVisitor::endElement (const char * name)
206 {
207   State &st = state();
208   bool ret;
209
210                                 // If there are no children and it's
211                                 // not an alias, then it's a leaf value.
212   if (st.node->nChildren() == 0 && !st.node->isAlias()) {
213     if (st.type == "bool") {
214       if (_data == "true" || atoi(_data.c_str()) != 0)
215         ret = st.node->setBoolValue(true);
216       else
217         ret = st.node->setBoolValue(false);
218     } else if (st.type == "int") {
219       ret = st.node->setIntValue(atoi(_data.c_str()));
220     } else if (st.type == "long") {
221       ret = st.node->setLongValue(strtol(_data.c_str(), 0, 0));
222     } else if (st.type == "float") {
223       ret = st.node->setFloatValue(atof(_data.c_str()));
224     } else if (st.type == "double") {
225       ret = st.node->setDoubleValue(strtod(_data.c_str(), 0));
226     } else if (st.type == "string") {
227       ret = st.node->setStringValue(_data);
228     } else if (st.type == "unspecified") {
229       ret = st.node->setUnspecifiedValue(_data);
230     } else {
231       string message = "Unrecognized data type '";
232       message += st.type;
233       message += '\'';
234                                 // FIXME: add location information
235       throw sg_io_exception(message, "SimGear Property Reader");
236     }
237     if (!ret)
238       SG_LOG(SG_INPUT, SG_ALERT, "readProperties: Failed to set "
239              << st.node->getPath() << " to value \""
240              << _data << "\" with type " << st.type);
241   }
242
243                                 // Set the access-mode attributes now,
244                                 // once the value has already been 
245                                 // assigned.
246   st.node->setAttributes(st.mode);
247
248   pop_state();
249 }
250
251 void
252 PropsVisitor::data (const char * s, int length)
253 {
254   if (state().node->nChildren() == 0)
255     _data.append(string(s, length));
256 }
257
258 void
259 PropsVisitor::warning (const char * message, int line, int column)
260 {
261   SG_LOG(SG_INPUT, SG_ALERT, "readProperties: warning: "
262          << message << " at line " << line << ", column " << column);
263 }
264
265
266 \f
267 ////////////////////////////////////////////////////////////////////////
268 // Property list reader.
269 ////////////////////////////////////////////////////////////////////////
270
271
272 /**
273  * Read properties from an input stream.
274  *
275  * @param input The input stream containing an XML property file.
276  * @param start_node The root node for reading properties.
277  * @param base A base path for resolving external include references.
278  * @return true if the read succeeded, false otherwise.
279  */
280 void
281 readProperties (istream &input, SGPropertyNode * start_node,
282                 const string &base)
283 {
284   PropsVisitor visitor(start_node, base);
285   readXML(input, visitor, base);
286   if (visitor.hasException())
287     throw *(visitor.getException());
288 }
289
290
291 /**
292  * Read properties from a file.
293  *
294  * @param file A string containing the file path.
295  * @param start_node The root node for reading properties.
296  * @return true if the read succeeded, false otherwise.
297  */
298 void
299 readProperties (const string &file, SGPropertyNode * start_node)
300 {
301   PropsVisitor visitor(start_node, file);
302   readXML(file, visitor);
303   if (visitor.hasException())
304     throw *(visitor.getException());
305 }
306
307
308 \f
309 ////////////////////////////////////////////////////////////////////////
310 // Property list writer.
311 ////////////////////////////////////////////////////////////////////////
312
313 #define INDENT_STEP 2
314
315 /**
316  * Return the type name.
317  */
318 static const char *
319 getTypeName (SGPropertyNode::Type type)
320 {
321   switch (type) {
322   case SGPropertyNode::UNSPECIFIED:
323     return "unspecified";
324   case SGPropertyNode::BOOL:
325     return "bool";
326   case SGPropertyNode::INT:
327     return "int";
328   case SGPropertyNode::LONG:
329     return "long";
330   case SGPropertyNode::FLOAT:
331     return "float";
332   case SGPropertyNode::DOUBLE:
333     return "double";
334   case SGPropertyNode::STRING:
335     return "string";
336   }
337
338   // keep the compiler from squawking
339   return "unspecified";
340 }
341
342
343 /**
344  * Escape characters for output.
345  */
346 static void
347 writeData (ostream &output, const string &data)
348 {
349   for (int i = 0; i < (int)data.size(); i++) {
350     switch (data[i]) {
351     case '&':
352       output << "&amp;";
353       break;
354     case '<':
355       output << "&lt;";
356       break;
357     case '>':
358       output << "&gt;";
359       break;
360     default:
361       output << data[i];
362       break;
363     }
364   }
365 }
366
367 static void
368 doIndent (ostream &output, int indent)
369 {
370   while (indent-- > 0) {
371     output << ' ';
372   }
373 }
374
375
376 static void
377 writeAtts (ostream &output, const SGPropertyNode * node)
378 {
379   int index = node->getIndex();
380
381   if (index != 0)
382     output << " n=\"" << index << '"';
383
384 #if 0
385   if (!node->getAttribute(SGPropertyNode::READ))
386     output << " read=\"n\"";
387
388   if (!node->getAttribute(SGPropertyNode::WRITE))
389     output << " write=\"n\"";
390
391   if (node->getAttribute(SGPropertyNode::ARCHIVE))
392     output << " archive=\"y\"";
393 #endif
394
395 }
396
397
398 /**
399  * Test whether a node is archivable or has archivable descendants.
400  */
401 static bool
402 isArchivable (const SGPropertyNode * node)
403 {
404   // FIXME: it's inefficient to do this all the time
405   if (node->getAttribute(SGPropertyNode::ARCHIVE))
406     return true;
407   else {
408     int nChildren = node->nChildren();
409     for (int i = 0; i < nChildren; i++)
410       if (isArchivable(node->getChild(i)))
411         return true;
412   }
413   return false;
414 }
415
416
417 static bool
418 writeNode (ostream &output, const SGPropertyNode * node, int indent)
419 {
420                                 // Don't write the node or any of
421                                 // its descendants unless it is
422                                 // allowed to be archived.
423   if (!isArchivable(node))
424     return true;                // Everything's OK, but we won't write.
425
426   const string &name = node->getName();
427   int index = node->getIndex();
428   int nChildren = node->nChildren();
429
430                                 // If there is a literal value,
431                                 // write it first.
432   if (node->hasValue() && node->getAttribute(SGPropertyNode::ARCHIVE)) {
433     doIndent(output, indent);
434     output << '<' << name;
435     writeAtts(output, node);
436     if (node->isAlias() && node->getAliasTarget() != 0) {
437       output << " alias=\"" << node->getAliasTarget()->getPath()
438              << "\"/>" << endl;
439     } else {
440       if (node->getType() != SGPropertyNode::UNSPECIFIED)
441         output << " type=\"" << getTypeName(node->getType()) << '"';
442       output << '>';
443       writeData(output, node->getStringValue());
444       output << "</" << name << '>' << endl;
445     }
446   }
447
448                                 // If there are children, write them
449                                 // next.
450   if (nChildren > 0 || node->isAlias()) {
451     doIndent(output, indent);
452     output << '<' << name;
453     writeAtts(output, node);
454     output << '>' << endl;
455     for (int i = 0; i < nChildren; i++)
456       writeNode(output, node->getChild(i), indent + INDENT_STEP);
457     doIndent(output, indent);
458     output << "</" << name << '>' << endl;
459   }
460
461   return true;
462 }
463
464
465 void
466 writeProperties (ostream &output, const SGPropertyNode * start_node)
467 {
468   int nChildren = start_node->nChildren();
469
470   output << "<?xml version=\"1.0\"?>" << endl << endl;
471   output << "<PropertyList>" << endl;
472
473   for (int i = 0; i < nChildren; i++) {
474     writeNode(output, start_node->getChild(i), INDENT_STEP);
475   }
476
477   output << "</PropertyList>" << endl;
478 }
479
480
481 void
482 writeProperties (const string &file, const SGPropertyNode * start_node)
483 {
484   ofstream output(file.c_str());
485   if (output.good()) {
486     writeProperties(output, start_node);
487   } else {
488     throw sg_io_exception("Cannot open file", sg_location(file));
489   }
490 }
491
492
493 \f
494 ////////////////////////////////////////////////////////////////////////
495 // Copy properties from one tree to another.
496 ////////////////////////////////////////////////////////////////////////
497
498
499 /**
500  * Copy one property tree to another.
501  * 
502  * @param in The source property tree.
503  * @param out The destination property tree.
504  * @return true if all properties were copied, false if some failed
505  *  (for example, if the property's value is tied read-only).
506  */
507 bool
508 copyProperties (const SGPropertyNode *in, SGPropertyNode *out)
509 {
510   bool retval = true;
511
512                                 // First, copy the actual value,
513                                 // if any.
514   if (in->hasValue()) {
515     switch (in->getType()) {
516     case SGPropertyNode::BOOL:
517       if (!out->setBoolValue(in->getBoolValue()))
518         retval = false;
519       break;
520     case SGPropertyNode::INT:
521       if (!out->setIntValue(in->getIntValue()))
522         retval = false;
523       break;
524     case SGPropertyNode::LONG:
525       if (!out->setLongValue(in->getLongValue()))
526         retval = false;
527       break;
528     case SGPropertyNode::FLOAT:
529       if (!out->setFloatValue(in->getFloatValue()))
530         retval = false;
531       break;
532     case SGPropertyNode::DOUBLE:
533       if (!out->setDoubleValue(in->getDoubleValue()))
534         retval = false;
535       break;
536     case SGPropertyNode::STRING:
537       if (!out->setStringValue(in->getStringValue()))
538         retval = false;
539       break;
540     case SGPropertyNode::UNSPECIFIED:
541       if (!out->setUnspecifiedValue(in->getStringValue()))
542         retval = false;
543       break;
544     default:
545       string message = "Unknown internal SGPropertyNode type";
546       message += in->getType();
547       throw sg_error(message, "SimGear Property Reader");
548     }
549   }
550
551                                 // Next, copy the children.
552   int nChildren = in->nChildren();
553   for (int i = 0; i < nChildren; i++) {
554     const SGPropertyNode * in_child = in->getChild(i);
555     SGPropertyNode * out_child = out->getChild(in_child->getName(),
556                                                in_child->getIndex(),
557                                                true);
558     if (!copyProperties(in_child, out_child))
559       retval = false;
560   }
561
562   return retval;
563 }
564
565 // end of props_io.cxx