]> git.mxchange.org Git - simgear.git/blob - simgear/misc/props_io.cxx
Updates to build system to better support automake-1.5
[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   }
347
348   // keep the compiler from squawking
349   return "unspecified";
350 }
351
352
353 /**
354  * Escape characters for output.
355  */
356 static void
357 writeData (ostream &output, const string &data)
358 {
359   for (int i = 0; i < (int)data.size(); i++) {
360     switch (data[i]) {
361     case '&':
362       output << "&amp;";
363       break;
364     case '<':
365       output << "&lt;";
366       break;
367     case '>':
368       output << "&gt;";
369       break;
370     default:
371       output << data[i];
372       break;
373     }
374   }
375 }
376
377 static void
378 doIndent (ostream &output, int indent)
379 {
380   while (indent-- > 0) {
381     output << ' ';
382   }
383 }
384
385
386 static void
387 writeAtts (ostream &output, const SGPropertyNode * node)
388 {
389   int index = node->getIndex();
390
391   if (index != 0)
392     output << " n=\"" << index << '"';
393
394 #if 0
395   if (!node->getAttribute(SGPropertyNode::READ))
396     output << " read=\"n\"";
397
398   if (!node->getAttribute(SGPropertyNode::WRITE))
399     output << " write=\"n\"";
400
401   if (node->getAttribute(SGPropertyNode::ARCHIVE))
402     output << " archive=\"y\"";
403 #endif
404
405 }
406
407
408 /**
409  * Test whether a node is archivable or has archivable descendants.
410  */
411 static bool
412 isArchivable (const SGPropertyNode * node)
413 {
414   // FIXME: it's inefficient to do this all the time
415   if (node->getAttribute(SGPropertyNode::ARCHIVE))
416     return true;
417   else {
418     int nChildren = node->nChildren();
419     for (int i = 0; i < nChildren; i++)
420       if (isArchivable(node->getChild(i)))
421         return true;
422   }
423   return false;
424 }
425
426
427 static bool
428 writeNode (ostream &output, const SGPropertyNode * node, int indent)
429 {
430                                 // Don't write the node or any of
431                                 // its descendants unless it is
432                                 // allowed to be archived.
433   if (!isArchivable(node))
434     return true;                // Everything's OK, but we won't write.
435
436   const string &name = node->getName();
437   int index = node->getIndex();
438   int nChildren = node->nChildren();
439
440                                 // If there is a literal value,
441                                 // write it first.
442   if (node->hasValue() && node->getAttribute(SGPropertyNode::ARCHIVE)) {
443     doIndent(output, indent);
444     output << '<' << name;
445     writeAtts(output, node);
446     if (node->isAlias() && node->getAliasTarget() != 0) {
447       output << " alias=\"" << node->getAliasTarget()->getPath()
448              << "\"/>" << endl;
449     } else {
450       if (node->getType() != SGPropertyNode::UNSPECIFIED)
451         output << " type=\"" << getTypeName(node->getType()) << '"';
452       output << '>';
453       writeData(output, node->getStringValue());
454       output << "</" << name << '>' << endl;
455     }
456   }
457
458                                 // If there are children, write them
459                                 // next.
460   if (nChildren > 0 || node->isAlias()) {
461     doIndent(output, indent);
462     output << '<' << name;
463     writeAtts(output, node);
464     output << '>' << endl;
465     for (int i = 0; i < nChildren; i++)
466       writeNode(output, node->getChild(i), indent + INDENT_STEP);
467     doIndent(output, indent);
468     output << "</" << name << '>' << endl;
469   }
470
471   return true;
472 }
473
474
475 void
476 writeProperties (ostream &output, const SGPropertyNode * start_node)
477 {
478   int nChildren = start_node->nChildren();
479
480   output << "<?xml version=\"1.0\"?>" << endl << endl;
481   output << "<PropertyList>" << endl;
482
483   for (int i = 0; i < nChildren; i++) {
484     writeNode(output, start_node->getChild(i), INDENT_STEP);
485   }
486
487   output << "</PropertyList>" << endl;
488 }
489
490
491 void
492 writeProperties (const string &file, const SGPropertyNode * start_node)
493 {
494   ofstream output(file.c_str());
495   if (output.good()) {
496     writeProperties(output, start_node);
497   } else {
498     throw sg_io_exception("Cannot open file", sg_location(file));
499   }
500 }
501
502
503 \f
504 ////////////////////////////////////////////////////////////////////////
505 // Copy properties from one tree to another.
506 ////////////////////////////////////////////////////////////////////////
507
508
509 /**
510  * Copy one property tree to another.
511  * 
512  * @param in The source property tree.
513  * @param out The destination property tree.
514  * @return true if all properties were copied, false if some failed
515  *  (for example, if the property's value is tied read-only).
516  */
517 bool
518 copyProperties (const SGPropertyNode *in, SGPropertyNode *out)
519 {
520   bool retval = true;
521
522                                 // First, copy the actual value,
523                                 // if any.
524   if (in->hasValue()) {
525     switch (in->getType()) {
526     case SGPropertyNode::BOOL:
527       if (!out->setBoolValue(in->getBoolValue()))
528         retval = false;
529       break;
530     case SGPropertyNode::INT:
531       if (!out->setIntValue(in->getIntValue()))
532         retval = false;
533       break;
534     case SGPropertyNode::LONG:
535       if (!out->setLongValue(in->getLongValue()))
536         retval = false;
537       break;
538     case SGPropertyNode::FLOAT:
539       if (!out->setFloatValue(in->getFloatValue()))
540         retval = false;
541       break;
542     case SGPropertyNode::DOUBLE:
543       if (!out->setDoubleValue(in->getDoubleValue()))
544         retval = false;
545       break;
546     case SGPropertyNode::STRING:
547       if (!out->setStringValue(in->getStringValue()))
548         retval = false;
549       break;
550     case SGPropertyNode::UNSPECIFIED:
551       if (!out->setUnspecifiedValue(in->getStringValue()))
552         retval = false;
553       break;
554     default:
555       string message = "Unknown internal SGPropertyNode type";
556       message += in->getType();
557       throw sg_error(message, "SimGear Property Reader");
558     }
559   }
560
561                                 // Next, copy the children.
562   int nChildren = in->nChildren();
563   for (int i = 0; i < nChildren; i++) {
564     const SGPropertyNode * in_child = in->getChild(i);
565     SGPropertyNode * out_child = out->getChild(in_child->getName(),
566                                                in_child->getIndex(),
567                                                true);
568     if (!copyProperties(in_child, out_child))
569       retval = false;
570   }
571
572   return retval;
573 }
574
575 // end of props_io.cxx