]> git.mxchange.org Git - simgear.git/blob - simgear/misc/props_io.cxx
Patch to property tree to allow repeating tag names. The property manager
[simgear.git] / simgear / misc / props_io.cxx
1
2 #ifdef HAVE_CONFIG_H
3 #  include <config.h>
4 #endif
5
6 #include <simgear/compiler.h>
7
8 #include <stdlib.h>             // atof() atoi()
9
10 #include <simgear/debug/logstream.hxx>
11 #include <simgear/xml/easyxml.hxx>
12
13 #include "props.hxx"
14
15 #include STL_IOSTREAM
16 #if !defined(FG_HAVE_NATIVE_SGI_COMPILERS)
17 #  include <fstream>
18 #else
19 #  include <fstream.h>
20 #endif
21 #include STL_STRING
22 #include <vector>
23 #include <map>
24
25 #if !defined(FG_HAVE_NATIVE_SGI_COMPILERS)
26 FG_USING_STD(istream);
27 FG_USING_STD(ifstream);
28 FG_USING_STD(ostream);
29 FG_USING_STD(ofstream);
30 #endif
31 FG_USING_STD(string);
32 FG_USING_STD(vector);
33 FG_USING_STD(map);
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) : _ok(true), _root(root), _level(0) {}
46
47   void startXML ();
48   void endXML ();
49   void startElement (const char * name, const XMLAttributes &atts);
50   void endElement (const char * name);
51   void data (const char * s, int length);
52   void warning (const char * message, int line, int column);
53   void error (const char * message, int line, int column);
54
55   bool isOK () const { return _ok; }
56
57 private:
58
59   struct State
60   {
61     State () : node(0), type("") {}
62     State (SGPropertyNode * _node, const char * _type)
63       : node(_node), type(_type) {}
64     SGPropertyNode * node;
65     string type;
66     map<string,int> counters;
67   };
68
69   State &state () { return _state_stack[_state_stack.size() - 1]; }
70
71   void push_state (SGPropertyNode * node, const char * type) {
72     if (type == 0)
73       _state_stack.push_back(State(node, "unknown"));
74     else
75       _state_stack.push_back(State(node, type));
76     _level++;
77     _data = "";
78   }
79
80   void pop_state () {
81     _state_stack.pop_back();
82     _level--;
83   }
84
85   bool _ok;
86   string _data;
87   SGPropertyNode * _root;
88   int _level;
89   vector<State> _state_stack;
90
91 };
92
93 void
94 PropsVisitor::startXML ()
95 {
96   _level = 0;
97   _state_stack.resize(0);
98 }
99
100 void
101 PropsVisitor::endXML ()
102 {
103   _level = 0;
104   _state_stack.resize(0);
105 }
106
107 void
108 PropsVisitor::startElement (const char * name, const XMLAttributes &atts)
109 {
110   State &st = state();
111
112   if (_level == 0) {
113     push_state(_root, "");
114   }
115
116   else {
117     const char * att_n = atts.getValue("n");
118     int index = 0;
119     if (att_n != 0) {
120       index = atoi(att_n);
121       st.counters[name] = max(st.counters[name], index+1);
122     } else {
123       index = st.counters[name];
124       st.counters[name]++;
125     }
126     push_state(st.node->getChild(name, index, true),
127                atts.getValue("type"));
128   }
129 }
130
131 void
132 PropsVisitor::endElement (const char * name)
133 {
134   State &st = state();
135   bool ret;
136
137                                 // If there are no children, then
138                                 // it is a leaf value.
139   if (st.node->nChildren() == 0) {
140     if (st.type == "bool") {
141       if (_data == "true" || atoi(_data.c_str()) != 0)
142         ret = st.node->setBoolValue(true);
143       else
144         ret = st.node->setBoolValue(false);
145     } else if (st.type == "int") {
146       ret = st.node->setIntValue(atoi(_data.c_str()));
147     } else if (st.type == "float") {
148       ret = st.node->setFloatValue(atof(_data.c_str()));
149     } else if (st.type == "double") {
150       ret = st.node->setDoubleValue(atof(_data.c_str()));
151     } else if (st.type == "string") {
152       ret = st.node->setStringValue(_data);
153     } else if (st.type == "unknown") {
154       ret = st.node->setUnknownValue(_data);
155     } else {
156       FG_LOG(FG_INPUT, FG_ALERT, "Unknown data type " << st.type
157              << " assuming 'unknown'");
158       ret = st.node->setUnknownValue(_data);
159     }
160     if (!ret)
161       FG_LOG(FG_INPUT, FG_ALERT, "readProperties: Failed to set "
162              << st.node->getPath() << " to value \""
163              << _data << "\" with type " << st.type);
164   }
165
166   pop_state();
167 }
168
169 void
170 PropsVisitor::data (const char * s, int length)
171 {
172   if (state().node->nChildren() == 0)
173     _data.append(string(s, length));
174 }
175
176 void
177 PropsVisitor::warning (const char * message, int line, int column)
178 {
179   FG_LOG(FG_INPUT, FG_ALERT, "readProperties: warning: "
180          << message << " at line " << line << ", column " << column);
181 }
182
183 void
184 PropsVisitor::error (const char * message, int line, int column)
185 {
186   FG_LOG(FG_INPUT, FG_ALERT, "readProperties: FATAL: "
187          << message << " at line " << line << ", column " << column);
188   _ok = false;
189 }
190
191
192 \f
193 ////////////////////////////////////////////////////////////////////////
194 // Property list reader.
195 ////////////////////////////////////////////////////////////////////////
196
197 bool
198 readProperties (istream &input, SGPropertyNode * start_node)
199 {
200   PropsVisitor visitor(start_node);
201   return readXML(input, visitor) && visitor.isOK();
202 }
203
204 bool
205 readProperties (const string &file, SGPropertyNode * start_node)
206 {
207   ifstream input(file.c_str());
208   if (input.good()) {
209     return readProperties(input, start_node);
210   } else {
211     FG_LOG(FG_INPUT, FG_ALERT, "Error reading property list from file "
212            << file);
213     return false;
214   }
215 }
216
217
218 \f
219 ////////////////////////////////////////////////////////////////////////
220 // Property list writer.
221 ////////////////////////////////////////////////////////////////////////
222
223 #define INDENT_STEP 2
224
225 /**
226  * Return the type name.
227  */
228 static const char *
229 getTypeName (SGValue::Type type)
230 {
231   switch (type) {
232   case SGValue::UNKNOWN:
233     return "unknown";
234   case SGValue::BOOL:
235     return "bool";
236   case SGValue::INT:
237     return "int";
238   case SGValue::FLOAT:
239     return "float";
240   case SGValue::DOUBLE:
241     return "double";
242   case SGValue::STRING:
243     return "string";
244   }
245
246   // keep the compiler from squawking
247   return "unknown";
248 }
249
250
251 /**
252  * Escape characters for output.
253  */
254 static void
255 writeData (ostream &output, const string &data)
256 {
257   for (int i = 0; i < (int)data.size(); i++) {
258     switch (data[i]) {
259     case '&':
260       output << "&amp;";
261       break;
262     case '<':
263       output << "&lt;";
264       break;
265     case '>':
266       output << "&gt;";
267       break;
268     default:
269       output << data[i];
270       break;
271     }
272   }
273 }
274
275 static void
276 doIndent (ostream &output, int indent)
277 {
278   while (indent-- > 0) {
279     output << ' ';
280   }
281 }
282
283
284 static bool
285 writeNode (ostream &output, const SGPropertyNode * node, int indent)
286 {
287   const string &name = node->getName();
288   int index = node->getIndex();
289   int nChildren = node->nChildren();
290
291                                 // If there is a literal value,
292                                 // write it first.
293   if (node->hasValue()) {
294     doIndent(output, indent);
295     output << '<' << name << " n=\"" << index
296            << "\" type=\"" << getTypeName(node->getType()) << "\">";
297     writeData(output, node->getStringValue());
298     output << "</" << name << '>' << endl;;
299   }
300
301                                 // If there are children, write them
302                                 // next.
303   if (nChildren > 0) {
304     doIndent(output, indent);
305     output << '<' << name << " n=\"" << index << "\">" << endl;;
306     for (int i = 0; i < nChildren; i++)
307       writeNode(output, node->getChild(i), indent + INDENT_STEP);
308     doIndent(output, indent);
309     output << "</" << name << '>' << endl;
310   }
311
312                                 // If there were no children and no
313                                 // value, at least note the presence
314                                 // of the node.
315   if (nChildren == 0 && !node->hasValue()) {
316     doIndent(output, indent);
317     output << '<' << name << " n=\"" << index << "\"/>" << endl;
318   }
319
320   return true;
321 }
322
323 bool
324 writeProperties (ostream &output, const SGPropertyNode * start_node)
325 {
326   int nChildren = start_node->nChildren();
327
328   output << "<?xml version=\"1.0\"?>" << endl << endl;
329   output << "<PropertyList>" << endl;
330
331   for (int i = 0; i < nChildren; i++) {
332     writeNode(output, start_node->getChild(i), INDENT_STEP);
333   }
334
335   output << "</PropertyList>" << endl;
336
337   return true;
338 }
339
340 bool
341 writeProperties (const string &file, const SGPropertyNode * start_node)
342 {
343   ofstream output(file.c_str());
344   if (output.good()) {
345     return writeProperties(output, start_node);
346   } else {
347     FG_LOG(FG_INPUT, FG_ALERT, "Cannot write properties to file "
348            << file);
349     return false;
350   }
351 }
352
353
354 /**
355  * Copy one property list to another.
356  */
357 bool
358 copyProperties (const SGPropertyNode *in, SGPropertyNode *out)
359 {
360   bool retval = true;
361
362                                 // First, copy the actual value,
363                                 // if any.
364   if (in->hasValue()) {
365     switch (in->getType()) {
366     case SGValue::BOOL:
367       if (!out->setBoolValue(in->getBoolValue()))
368         retval = false;
369       break;
370     case SGValue::INT:
371       if (!out->setIntValue(in->getIntValue()))
372         retval = false;
373       break;
374     case SGValue::FLOAT:
375       if (!out->setFloatValue(in->getFloatValue()))
376         retval = false;
377       break;
378     case SGValue::DOUBLE:
379       if (!out->setDoubleValue(in->getDoubleValue()))
380         retval = false;
381       break;
382     case SGValue::STRING:
383       if (!out->setStringValue(in->getStringValue()))
384         retval = false;
385       break;
386     case SGValue::UNKNOWN:
387       if (!out->setUnknownValue(in->getStringValue()))
388         retval = false;
389       break;
390     default:
391       throw string("Unknown SGValue type"); // FIXME!!!
392     }
393   }
394
395                                 // Next, copy the children.
396   int nChildren = in->nChildren();
397   for (int i = 0; i < nChildren; i++) {
398     const SGPropertyNode * in_child = in->getChild(i);
399     SGPropertyNode * out_child = out->getChild(in_child->getName(),
400                                                in_child->getIndex(),
401                                                true);
402     if (!copyProperties(in_child, out_child))
403       retval = false;
404   }
405
406   return retval;
407 }
408
409 // end of props_io.cxx