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