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