]> git.mxchange.org Git - simgear.git/blob - simgear/misc/props_io.cxx
Attempt to resolve ambiguity between #include <config.h> for simgear vs.
[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
34 \f
35 ////////////////////////////////////////////////////////////////////////
36 // Property list visitor, for XML parsing.
37 ////////////////////////////////////////////////////////////////////////
38
39 class PropsVisitor : public XMLVisitor
40 {
41 public:
42
43   PropsVisitor (SGPropertyNode * root, const string &base)
44     : _ok(true), _root(root), _level(0), _base(base) {}
45
46   void startXML ();
47   void endXML ();
48   void startElement (const char * name, const XMLAttributes &atts);
49   void endElement (const char * name);
50   void data (const char * s, int length);
51   void warning (const char * message, int line, int column);
52   void error (const char * message, int line, int column);
53
54   bool isOK () const { return _ok; }
55
56 private:
57
58   struct State
59   {
60     State () : node(0), type("") {}
61     State (SGPropertyNode * _node, const char * _type)
62       : node(_node), type(_type) {}
63     SGPropertyNode * node;
64     string type;
65     map<string,int> counters;
66   };
67
68   State &state () { return _state_stack[_state_stack.size() - 1]; }
69
70   void push_state (SGPropertyNode * node, const char * type) {
71     if (type == 0)
72       _state_stack.push_back(State(node, "unknown"));
73     else
74       _state_stack.push_back(State(node, type));
75     _level++;
76     _data = "";
77   }
78
79   void pop_state () {
80     _state_stack.pop_back();
81     _level--;
82   }
83
84   bool _ok;
85   string _data;
86   SGPropertyNode * _root;
87   int _level;
88   vector<State> _state_stack;
89   string _base;
90 };
91
92 void
93 PropsVisitor::startXML ()
94 {
95   _level = 0;
96   _state_stack.resize(0);
97 }
98
99 void
100 PropsVisitor::endXML ()
101 {
102   _level = 0;
103   _state_stack.resize(0);
104 }
105
106 void
107 PropsVisitor::startElement (const char * name, const XMLAttributes &atts)
108 {
109   State &st = state();
110
111   if (_level == 0) {
112     if (string(name) != (string)"PropertyList") {
113       SG_LOG(SG_INPUT, SG_ALERT, "Root element name is " <<
114              name << "; expected PropertyList");
115       _ok = false;
116     }
117     push_state(_root, "");
118   }
119
120   else {
121                                 // Get the index.
122     const char * att_n = atts.getValue("n");
123     int index = 0;
124     if (att_n != 0) {
125       index = atoi(att_n);
126       st.counters[name] = SG_MAX2(st.counters[name], index+1);
127     } else {
128       index = st.counters[name];
129       st.counters[name]++;
130     }
131
132                                 // Check for an alias.
133     SGPropertyNode * node = st.node->getChild(name, index, true);
134     const char * att_alias = atts.getValue("alias");
135     if (att_alias != 0) {
136       if (!node->alias(att_alias))
137         SG_LOG(SG_INPUT, SG_ALERT, "Failed to set alias to " << att_alias);
138     }
139
140                                 // Check for an include.
141     const char * att_include = atts.getValue("include");
142     if (att_include != 0) {
143       SGPath path(SGPath(_base).dir());
144       cerr << "Base is " << _base << endl;
145       cerr << "Dir is " << SGPath(_base).dir() << endl;
146       path.append(att_include);
147       if (!readProperties(path.str(), node)) {
148         SG_LOG(SG_INPUT, SG_ALERT, "Failed to read include file "
149                << att_include);
150         _ok = false;
151       }
152     }
153
154     push_state(node, atts.getValue("type"));
155   }
156 }
157
158 void
159 PropsVisitor::endElement (const char * name)
160 {
161   State &st = state();
162   bool ret;
163
164                                 // If there are no children and it's
165                                 // not an alias, then it's a leaf value.
166   if (st.node->nChildren() == 0 && !st.node->isAlias()) {
167     if (st.type == "bool") {
168       if (_data == "true" || atoi(_data.c_str()) != 0)
169         ret = st.node->setBoolValue(true);
170       else
171         ret = st.node->setBoolValue(false);
172     } else if (st.type == "int") {
173       ret = st.node->setIntValue(atoi(_data.c_str()));
174     } else if (st.type == "long") {
175       ret = st.node->setLongValue(strtol(_data.c_str(), 0, 0));
176     } else if (st.type == "float") {
177       ret = st.node->setFloatValue(atof(_data.c_str()));
178     } else if (st.type == "double") {
179       ret = st.node->setDoubleValue(strtod(_data.c_str(), 0));
180     } else if (st.type == "string") {
181       ret = st.node->setStringValue(_data);
182     } else if (st.type == "unknown") {
183       ret = st.node->setUnknownValue(_data);
184     } else {
185       SG_LOG(SG_INPUT, SG_ALERT, "Unknown data type " << st.type
186              << " assuming 'unknown'");
187       ret = st.node->setUnknownValue(_data);
188     }
189     if (!ret)
190       SG_LOG(SG_INPUT, SG_ALERT, "readProperties: Failed to set "
191              << st.node->getPath() << " to value \""
192              << _data << "\" with type " << st.type);
193   }
194
195   pop_state();
196 }
197
198 void
199 PropsVisitor::data (const char * s, int length)
200 {
201   if (state().node->nChildren() == 0)
202     _data.append(string(s, length));
203 }
204
205 void
206 PropsVisitor::warning (const char * message, int line, int column)
207 {
208   SG_LOG(SG_INPUT, SG_ALERT, "readProperties: warning: "
209          << message << " at line " << line << ", column " << column);
210 }
211
212 void
213 PropsVisitor::error (const char * message, int line, int column)
214 {
215   SG_LOG(SG_INPUT, SG_ALERT, "readProperties: FATAL: " <<
216          message << " at line " << line << ", column " << column);
217   _ok = false;
218 }
219
220
221 \f
222 ////////////////////////////////////////////////////////////////////////
223 // Property list reader.
224 ////////////////////////////////////////////////////////////////////////
225
226
227 /**
228  * Read properties from an input stream.
229  *
230  * @param input The input stream containing an XML property file.
231  * @param start_node The root node for reading properties.
232  * @param base A base path for resolving external include references.
233  * @return true if the read succeeded, false otherwise.
234  */
235 bool
236 readProperties (istream &input, SGPropertyNode * start_node,
237                 const string &base)
238 {
239   PropsVisitor visitor(start_node, base);
240   return readXML(input, visitor) && visitor.isOK();
241 }
242
243
244 /**
245  * Read properties from a file.
246  *
247  * @param file A string containing the file path.
248  * @param start_node The root node for reading properties.
249  * @return true if the read succeeded, false otherwise.
250  */
251 bool
252 readProperties (const string &file, SGPropertyNode * start_node)
253 {
254   cerr << "Reading properties from " << file << endl;
255   ifstream input(file.c_str());
256   if (input.good()) {
257     return readProperties(input, start_node, file);
258   } else {
259     SG_LOG(SG_INPUT, SG_ALERT, "Error reading property list from file "
260            << file);
261     return false;
262   }
263 }
264
265
266 \f
267 ////////////////////////////////////////////////////////////////////////
268 // Property list writer.
269 ////////////////////////////////////////////////////////////////////////
270
271 #define INDENT_STEP 2
272
273 /**
274  * Return the type name.
275  */
276 static const char *
277 getTypeName (SGValue::Type type)
278 {
279   switch (type) {
280   case SGValue::UNKNOWN:
281     return "unknown";
282   case SGValue::BOOL:
283     return "bool";
284   case SGValue::INT:
285     return "int";
286   case SGValue::LONG:
287     return "long";
288   case SGValue::FLOAT:
289     return "float";
290   case SGValue::DOUBLE:
291     return "double";
292   case SGValue::STRING:
293     return "string";
294   }
295
296   // keep the compiler from squawking
297   return "unknown";
298 }
299
300
301 /**
302  * Escape characters for output.
303  */
304 static void
305 writeData (ostream &output, const string &data)
306 {
307   for (int i = 0; i < (int)data.size(); i++) {
308     switch (data[i]) {
309     case '&':
310       output << "&amp;";
311       break;
312     case '<':
313       output << "&lt;";
314       break;
315     case '>':
316       output << "&gt;";
317       break;
318     default:
319       output << data[i];
320       break;
321     }
322   }
323 }
324
325 static void
326 doIndent (ostream &output, int indent)
327 {
328   while (indent-- > 0) {
329     output << ' ';
330   }
331 }
332
333
334 static bool
335 writeNode (ostream &output, const SGPropertyNode * node, int indent)
336 {
337   const string &name = node->getName();
338   int index = node->getIndex();
339   int nChildren = node->nChildren();
340
341                                 // If there is a literal value,
342                                 // write it first.
343   if (node->hasValue()) {
344     doIndent(output, indent);
345     output << '<' << name << " n=\"" << index;
346     if (node->isAlias() && node->getAliasTarget() != 0) {
347       output << "\" alias=\""
348              << node->getAliasTarget()->getPath() << "\"/>" << endl;
349     } else {
350       if (node->getType() != SGValue::UNKNOWN)
351         output << "\" type=\"" << getTypeName(node->getType()) << '"';
352       output << '>';
353       writeData(output, node->getStringValue());
354       output << "</" << name << '>' << endl;;
355     }
356   }
357
358                                 // If there are children, write them
359                                 // next.
360   if (nChildren > 0) {
361     doIndent(output, indent);
362     output << '<' << name << " n=\"" << index << "\">" << endl;;
363     for (int i = 0; i < nChildren; i++)
364       writeNode(output, node->getChild(i), indent + INDENT_STEP);
365     doIndent(output, indent);
366     output << "</" << name << '>' << endl;
367   }
368
369                                 // If there were no children and no
370                                 // value, at least note the presence
371                                 // of the node.
372   if (nChildren == 0 && !node->hasValue()) {
373     doIndent(output, indent);
374     output << '<' << name << " n=\"" << index << "\"/>" << endl;
375   }
376
377   return true;
378 }
379
380
381 /**
382  * Write a property tree to an output stream in XML format.
383  *
384  * @param output The output stream.
385  * @param start_node The root node to write.
386  * @return true if the write succeeded, false otherwise.
387  */
388 bool
389 writeProperties (ostream &output, const SGPropertyNode * start_node)
390 {
391   int nChildren = start_node->nChildren();
392
393   output << "<?xml version=\"1.0\"?>" << endl << endl;
394   output << "<PropertyList>" << endl;
395
396   for (int i = 0; i < nChildren; i++) {
397     writeNode(output, start_node->getChild(i), INDENT_STEP);
398   }
399
400   output << "</PropertyList>" << endl;
401
402   return true;
403 }
404
405
406 /**
407  * Write a property tree to a file in XML format.
408  *
409  * @param file The destination file.
410  * @param start_node The root node to write.
411  * @return true if the write succeeded, false otherwise.
412  */
413 bool
414 writeProperties (const string &file, const SGPropertyNode * start_node)
415 {
416   ofstream output(file.c_str());
417   if (output.good()) {
418     return writeProperties(output, start_node);
419   } else {
420     SG_LOG(SG_INPUT, SG_ALERT, "Cannot write properties to file "
421            << file);
422     return false;
423   }
424 }
425
426
427 \f
428 ////////////////////////////////////////////////////////////////////////
429 // Copy properties from one tree to another.
430 ////////////////////////////////////////////////////////////////////////
431
432
433 /**
434  * Copy one property tree to another.
435  * 
436  * @param in The source property tree.
437  * @param out The destination property tree.
438  * @return true if all properties were copied, false if some failed
439  *  (for example, if the property's value is tied read-only).
440  */
441 bool
442 copyProperties (const SGPropertyNode *in, SGPropertyNode *out)
443 {
444   bool retval = true;
445
446                                 // First, copy the actual value,
447                                 // if any.
448   if (in->hasValue()) {
449     switch (in->getType()) {
450     case SGValue::BOOL:
451       if (!out->setBoolValue(in->getBoolValue()))
452         retval = false;
453       break;
454     case SGValue::INT:
455       if (!out->setIntValue(in->getIntValue()))
456         retval = false;
457       break;
458     case SGValue::LONG:
459       if (!out->setLongValue(in->getLongValue()))
460         retval = false;
461       break;
462     case SGValue::FLOAT:
463       if (!out->setFloatValue(in->getFloatValue()))
464         retval = false;
465       break;
466     case SGValue::DOUBLE:
467       if (!out->setDoubleValue(in->getDoubleValue()))
468         retval = false;
469       break;
470     case SGValue::STRING:
471       if (!out->setStringValue(in->getStringValue()))
472         retval = false;
473       break;
474     case SGValue::UNKNOWN:
475       if (!out->setUnknownValue(in->getStringValue()))
476         retval = false;
477       break;
478     default:
479       throw string("Unknown SGValue type");
480     }
481   }
482
483                                 // Next, copy the children.
484   int nChildren = in->nChildren();
485   for (int i = 0; i < nChildren; i++) {
486     const SGPropertyNode * in_child = in->getChild(i);
487     SGPropertyNode * out_child = out->getChild(in_child->getName(),
488                                                in_child->getIndex(),
489                                                true);
490     if (!copyProperties(in_child, out_child))
491       retval = false;
492   }
493
494   return retval;
495 }
496
497 // end of props_io.cxx