]> git.mxchange.org Git - simgear.git/blob - simgear/misc/props_io.cxx
- changed the default value of the archive flag to false; it must now
[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|SGPropertyNode::ARCHIVE)
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 that defaults to 'yes'.
112  */
113 static bool
114 checkFlag (const char * flag)
115 {
116   if (flag == 0 || string(flag) == "y")
117     return true;
118   else if (string(flag) == "n")
119     return false;
120   else {
121     SG_LOG(SG_INPUT, SG_ALERT, "Unrecognized flag value '" << flag
122            << "', assuming 'y'");
123     return true;
124   }
125 }
126
127 void
128 PropsVisitor::startElement (const char * name, const XMLAttributes &atts)
129 {
130   State &st = state();
131
132   if (_level == 0) {
133     if (string(name) != (string)"PropertyList") {
134       SG_LOG(SG_INPUT, SG_ALERT, "Root element name is " <<
135              name << "; expected PropertyList");
136       _ok = false;
137     }
138     push_state(_root, "", DEFAULT_MODE);
139   }
140
141   else {
142
143     const char * attval;
144                                 // Get the index.
145     attval = atts.getValue("n");
146     int index = 0;
147     if (attval != 0) {
148       index = atoi(attval);
149       st.counters[name] = SG_MAX2(st.counters[name], index+1);
150     } else {
151       index = st.counters[name];
152       st.counters[name]++;
153     }
154
155                                 // Got the index, so grab the node.
156     SGPropertyNode * node = st.node->getChild(name, index, true);
157
158                                 // Get the access-mode attributes,
159                                 // but don't set yet (in case they
160                                 // prevent us from recording the value).
161     int mode = 0;
162
163     attval = atts.getValue("read");
164     if (checkFlag(attval))
165       mode |= SGPropertyNode::READ;
166     attval = atts.getValue("write");
167     if (checkFlag(attval))
168       mode |= SGPropertyNode::WRITE;
169     attval = atts.getValue("archive");
170     if (checkFlag(attval))
171       mode |= SGPropertyNode::ARCHIVE;
172
173                                 // Check for an alias.
174     attval = atts.getValue("alias");
175     if (attval != 0) {
176       if (!node->alias(attval))
177         SG_LOG(SG_INPUT, SG_ALERT, "Failed to set alias to " << attval);
178     }
179
180                                 // Check for an include.
181     attval = atts.getValue("include");
182     if (attval != 0) {
183       SGPath path(SGPath(_base).dir());
184       cerr << "Base is " << _base << endl;
185       cerr << "Dir is " << SGPath(_base).dir() << endl;
186       path.append(attval);
187       if (!readProperties(path.str(), node)) {
188         SG_LOG(SG_INPUT, SG_ALERT, "Failed to read include file "
189                << attval);
190         _ok = false;
191       }
192     }
193
194     push_state(node, atts.getValue("type"), mode);
195   }
196 }
197
198 void
199 PropsVisitor::endElement (const char * name)
200 {
201   State &st = state();
202   bool ret;
203
204                                 // If there are no children and it's
205                                 // not an alias, then it's a leaf value.
206   if (st.node->nChildren() == 0 && !st.node->isAlias()) {
207     if (st.type == "bool") {
208       if (_data == "true" || atoi(_data.c_str()) != 0)
209         ret = st.node->setBoolValue(true);
210       else
211         ret = st.node->setBoolValue(false);
212     } else if (st.type == "int") {
213       ret = st.node->setIntValue(atoi(_data.c_str()));
214     } else if (st.type == "long") {
215       ret = st.node->setLongValue(strtol(_data.c_str(), 0, 0));
216     } else if (st.type == "float") {
217       ret = st.node->setFloatValue(atof(_data.c_str()));
218     } else if (st.type == "double") {
219       ret = st.node->setDoubleValue(strtod(_data.c_str(), 0));
220     } else if (st.type == "string") {
221       ret = st.node->setStringValue(_data);
222     } else if (st.type == "unspecified") {
223       ret = st.node->setUnspecifiedValue(_data);
224     } else {
225       SG_LOG(SG_INPUT, SG_ALERT, "Unrecognized data type " << st.type
226              << " assuming 'unspecified'");
227       ret = st.node->setUnspecifiedValue(_data);
228     }
229     if (!ret)
230       SG_LOG(SG_INPUT, SG_ALERT, "readProperties: Failed to set "
231              << st.node->getPath() << " to value \""
232              << _data << "\" with type " << st.type);
233   }
234
235                                 // Set the access-mode attributes now,
236                                 // once the value has already been 
237                                 // assigned.
238   st.node->setAttributes(st.mode);
239
240   pop_state();
241 }
242
243 void
244 PropsVisitor::data (const char * s, int length)
245 {
246   if (state().node->nChildren() == 0)
247     _data.append(string(s, length));
248 }
249
250 void
251 PropsVisitor::warning (const char * message, int line, int column)
252 {
253   SG_LOG(SG_INPUT, SG_ALERT, "readProperties: warning: "
254          << message << " at line " << line << ", column " << column);
255 }
256
257 void
258 PropsVisitor::error (const char * message, int line, int column)
259 {
260   SG_LOG(SG_INPUT, SG_ALERT, "readProperties: FATAL: " <<
261          message << " at line " << line << ", column " << column);
262   _ok = false;
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 bool
281 readProperties (istream &input, SGPropertyNode * start_node,
282                 const string &base)
283 {
284   PropsVisitor visitor(start_node, base);
285   return readXML(input, visitor) && visitor.isOK();
286 }
287
288
289 /**
290  * Read properties from a file.
291  *
292  * @param file A string containing the file path.
293  * @param start_node The root node for reading properties.
294  * @return true if the read succeeded, false otherwise.
295  */
296 bool
297 readProperties (const string &file, SGPropertyNode * start_node)
298 {
299   cerr << "Reading properties from " << file << endl;
300   ifstream input(file.c_str());
301   if (input.good()) {
302     return readProperties(input, start_node, file);
303   } else {
304     SG_LOG(SG_INPUT, SG_ALERT, "Error reading property list from file "
305            << file);
306     return false;
307   }
308 }
309
310
311 \f
312 ////////////////////////////////////////////////////////////////////////
313 // Property list writer.
314 ////////////////////////////////////////////////////////////////////////
315
316 #define INDENT_STEP 2
317
318 /**
319  * Return the type name.
320  */
321 static const char *
322 getTypeName (SGPropertyNode::Type type)
323 {
324   switch (type) {
325   case SGPropertyNode::UNSPECIFIED:
326     return "unspecified";
327   case SGPropertyNode::BOOL:
328     return "bool";
329   case SGPropertyNode::INT:
330     return "int";
331   case SGPropertyNode::LONG:
332     return "long";
333   case SGPropertyNode::FLOAT:
334     return "float";
335   case SGPropertyNode::DOUBLE:
336     return "double";
337   case SGPropertyNode::STRING:
338     return "string";
339   }
340
341   // keep the compiler from squawking
342   return "unspecified";
343 }
344
345
346 /**
347  * Escape characters for output.
348  */
349 static void
350 writeData (ostream &output, const string &data)
351 {
352   for (int i = 0; i < (int)data.size(); i++) {
353     switch (data[i]) {
354     case '&':
355       output << "&amp;";
356       break;
357     case '<':
358       output << "&lt;";
359       break;
360     case '>':
361       output << "&gt;";
362       break;
363     default:
364       output << data[i];
365       break;
366     }
367   }
368 }
369
370 static void
371 doIndent (ostream &output, int indent)
372 {
373   while (indent-- > 0) {
374     output << ' ';
375   }
376 }
377
378
379 static void
380 writeAtts (ostream &output, const SGPropertyNode * node)
381 {
382   int index = node->getIndex();
383
384   if (index != 0)
385     output << " n = \"" << index << '"';
386
387   if (!node->getAttribute(SGPropertyNode::READ))
388     output << " read=\"n\"";
389
390   if (!node->getAttribute(SGPropertyNode::WRITE))
391     output << " write=\"n\"";
392
393   if (!node->getAttribute(SGPropertyNode::ARCHIVE))
394     output << " archive=\"n\"";
395
396 }
397
398
399 static bool
400 writeNode (ostream &output, const SGPropertyNode * node, int indent)
401 {
402                                 // Don't write the node or any of
403                                 // its descendants unless it is
404                                 // allowed to be archived.
405   if (!node->getAttribute(SGPropertyNode::ARCHIVE))
406     return true;                // Everything's OK, but we won't write.
407
408   const string &name = node->getName();
409   int index = node->getIndex();
410   int nChildren = node->nChildren();
411
412                                 // If there is a literal value,
413                                 // write it first.
414   if (node->hasValue()) {
415     doIndent(output, indent);
416     output << '<' << name;
417     writeAtts(output, node);
418     if (node->isAlias() && node->getAliasTarget() != 0) {
419       output << " alias=\"" << node->getAliasTarget()->getPath()
420              << "\"/>" << endl;
421     } else {
422       if (node->getType() != SGPropertyNode::UNSPECIFIED)
423         output << " type=\"" << getTypeName(node->getType()) << '"';
424       output << '>';
425       writeData(output, node->getStringValue());
426       output << "</" << name << '>' << endl;
427     }
428   }
429
430                                 // If there are children, write them
431                                 // next.
432   if (nChildren > 0 || node->isAlias()) {
433     doIndent(output, indent);
434     output << '<' << name;
435     writeAtts(output, node);
436     output << '>' << endl;
437     for (int i = 0; i < nChildren; i++)
438       writeNode(output, node->getChild(i), indent + INDENT_STEP);
439     doIndent(output, indent);
440     output << "</" << name << '>' << endl;
441   }
442
443   return true;
444 }
445
446
447 /**
448  * Write a property tree to an output stream in XML format.
449  *
450  * @param output The output stream.
451  * @param start_node The root node to write.
452  * @return true if the write succeeded, false otherwise.
453  */
454 bool
455 writeProperties (ostream &output, const SGPropertyNode * start_node)
456 {
457   int nChildren = start_node->nChildren();
458
459   output << "<?xml version=\"1.0\"?>" << endl << endl;
460   output << "<PropertyList>" << endl;
461
462   for (int i = 0; i < nChildren; i++) {
463     writeNode(output, start_node->getChild(i), INDENT_STEP);
464   }
465
466   output << "</PropertyList>" << endl;
467
468   return true;
469 }
470
471
472 /**
473  * Write a property tree to a file in XML format.
474  *
475  * @param file The destination file.
476  * @param start_node The root node to write.
477  * @return true if the write succeeded, false otherwise.
478  */
479 bool
480 writeProperties (const string &file, const SGPropertyNode * start_node)
481 {
482   ofstream output(file.c_str());
483   if (output.good()) {
484     return writeProperties(output, start_node);
485   } else {
486     SG_LOG(SG_INPUT, SG_ALERT, "Cannot write properties to file "
487            << file);
488     return false;
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       throw string("Unrecognized SGPropertyNode type");
546     }
547   }
548
549                                 // Next, copy the children.
550   int nChildren = in->nChildren();
551   for (int i = 0; i < nChildren; i++) {
552     const SGPropertyNode * in_child = in->getChild(i);
553     SGPropertyNode * out_child = out->getChild(in_child->getName(),
554                                                in_child->getIndex(),
555                                                true);
556     if (!copyProperties(in_child, out_child))
557       retval = false;
558   }
559
560   return retval;
561 }
562
563 // end of props_io.cxx