2 #include <simgear/compiler.h>
4 #include <stdlib.h> // atof() atoi()
6 #include <simgear/sg_inlines.h>
7 #include <simgear/debug/logstream.hxx>
8 #include <simgear/xml/easyxml.hxx>
10 #include "sg_path.hxx"
14 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
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);
33 #define DEFAULT_MODE (SGPropertyNode::READ|SGPropertyNode::WRITE)
37 ////////////////////////////////////////////////////////////////////////
38 // Property list visitor, for XML parsing.
39 ////////////////////////////////////////////////////////////////////////
41 class PropsVisitor : public XMLVisitor
45 PropsVisitor (SGPropertyNode * root, const string &base)
46 : _root(root), _level(0), _base(base), _hasException(false) {}
48 virtual ~PropsVisitor () {}
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);
57 bool hasException () const { return _hasException; }
58 sg_io_exception &getException () { return _exception; }
59 void setException (const sg_io_exception &exception) {
60 _exception = exception;
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;
74 map<string,int> counters;
77 State &state () { return _state_stack[_state_stack.size() - 1]; }
79 void push_state (SGPropertyNode * node, const char * type, int mode) {
81 _state_stack.push_back(State(node, "unspecified", mode));
83 _state_stack.push_back(State(node, type, mode));
89 _state_stack.pop_back();
94 SGPropertyNode * _root;
96 vector<State> _state_stack;
98 sg_io_exception _exception;
103 PropsVisitor::startXML ()
106 _state_stack.resize(0);
110 PropsVisitor::endXML ()
113 _state_stack.resize(0);
118 * Check a yes/no flag, with default.
121 checkFlag (const char * flag, bool defaultState = true)
125 else if (string(flag) == "y")
127 else if (string(flag) == "n")
130 string message = "Unrecognized flag value '";
133 // FIXME: add location info
134 throw sg_io_exception(message, "SimGear Property Reader");
139 PropsVisitor::startElement (const char * name, const XMLAttributes &atts)
144 if (string(name) != (string)"PropertyList") {
145 string message = "Root element name is ";
147 message += "; expected PropertyList";
148 throw sg_io_exception(message, "SimGear Property Reader");
150 push_state(_root, "", DEFAULT_MODE);
157 attval = atts.getValue("n");
160 index = atoi(attval);
161 st.counters[name] = SG_MAX2(st.counters[name], index+1);
163 index = st.counters[name];
167 // Got the index, so grab the node.
168 SGPropertyNode * node = st.node->getChild(name, index, true);
170 // Get the access-mode attributes,
171 // but don't set yet (in case they
172 // prevent us from recording the value).
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;
191 // Check for an alias.
192 attval = atts.getValue("alias");
194 if (!node->alias(attval))
195 SG_LOG(SG_INPUT, SG_ALERT, "Failed to set alias to " << attval);
198 // Check for an include.
199 attval = atts.getValue("include");
201 SGPath path(SGPath(_base).dir());
204 readProperties(path.str(), node);
205 } catch (sg_io_exception &e) {
210 push_state(node, atts.getValue("type"), mode);
215 PropsVisitor::endElement (const char * name)
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);
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);
241 string message = "Unrecognized data type '";
244 // FIXME: add location information
245 throw sg_io_exception(message, "SimGear Property Reader");
248 SG_LOG(SG_INPUT, SG_ALERT, "readProperties: Failed to set "
249 << st.node->getPath() << " to value \""
250 << _data << "\" with type " << st.type);
253 // Set the access-mode attributes now,
254 // once the value has already been
256 st.node->setAttributes(st.mode);
262 PropsVisitor::data (const char * s, int length)
264 if (state().node->nChildren() == 0)
265 _data.append(string(s, length));
269 PropsVisitor::warning (const char * message, int line, int column)
271 SG_LOG(SG_INPUT, SG_ALERT, "readProperties: warning: "
272 << message << " at line " << line << ", column " << column);
277 ////////////////////////////////////////////////////////////////////////
278 // Property list reader.
279 ////////////////////////////////////////////////////////////////////////
283 * Read properties from an input stream.
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.
291 readProperties (istream &input, SGPropertyNode * start_node,
294 PropsVisitor visitor(start_node, base);
295 readXML(input, visitor, base);
296 if (visitor.hasException())
297 throw visitor.getException();
302 * Read properties from a file.
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.
309 readProperties (const string &file, SGPropertyNode * start_node)
311 PropsVisitor visitor(start_node, file);
312 readXML(file, visitor);
313 if (visitor.hasException())
314 throw visitor.getException();
319 ////////////////////////////////////////////////////////////////////////
320 // Property list writer.
321 ////////////////////////////////////////////////////////////////////////
323 #define INDENT_STEP 2
326 * Return the type name.
329 getTypeName (SGPropertyNode::Type type)
332 case SGPropertyNode::UNSPECIFIED:
333 return "unspecified";
334 case SGPropertyNode::BOOL:
336 case SGPropertyNode::INT:
338 case SGPropertyNode::LONG:
340 case SGPropertyNode::FLOAT:
342 case SGPropertyNode::DOUBLE:
344 case SGPropertyNode::STRING:
348 // keep the compiler from squawking
349 return "unspecified";
354 * Escape characters for output.
357 writeData (ostream &output, const string &data)
359 for (int i = 0; i < (int)data.size(); i++) {
378 doIndent (ostream &output, int indent)
380 while (indent-- > 0) {
387 writeAtts (ostream &output, const SGPropertyNode * node)
389 int index = node->getIndex();
392 output << " n=\"" << index << '"';
395 if (!node->getAttribute(SGPropertyNode::READ))
396 output << " read=\"n\"";
398 if (!node->getAttribute(SGPropertyNode::WRITE))
399 output << " write=\"n\"";
401 if (node->getAttribute(SGPropertyNode::ARCHIVE))
402 output << " archive=\"y\"";
409 * Test whether a node is archivable or has archivable descendants.
412 isArchivable (const SGPropertyNode * node)
414 // FIXME: it's inefficient to do this all the time
415 if (node->getAttribute(SGPropertyNode::ARCHIVE))
418 int nChildren = node->nChildren();
419 for (int i = 0; i < nChildren; i++)
420 if (isArchivable(node->getChild(i)))
428 writeNode (ostream &output, const SGPropertyNode * node, int indent)
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.
436 const string &name = node->getName();
437 int index = node->getIndex();
438 int nChildren = node->nChildren();
440 // If there is a literal value,
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()
450 if (node->getType() != SGPropertyNode::UNSPECIFIED)
451 output << " type=\"" << getTypeName(node->getType()) << '"';
453 writeData(output, node->getStringValue());
454 output << "</" << name << '>' << endl;
458 // If there are children, write them
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;
476 writeProperties (ostream &output, const SGPropertyNode * start_node)
478 int nChildren = start_node->nChildren();
480 output << "<?xml version=\"1.0\"?>" << endl << endl;
481 output << "<PropertyList>" << endl;
483 for (int i = 0; i < nChildren; i++) {
484 writeNode(output, start_node->getChild(i), INDENT_STEP);
487 output << "</PropertyList>" << endl;
492 writeProperties (const string &file, const SGPropertyNode * start_node)
494 ofstream output(file.c_str());
496 writeProperties(output, start_node);
498 throw sg_io_exception("Cannot open file", sg_location(file));
504 ////////////////////////////////////////////////////////////////////////
505 // Copy properties from one tree to another.
506 ////////////////////////////////////////////////////////////////////////
510 * Copy one property tree to another.
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).
518 copyProperties (const SGPropertyNode *in, SGPropertyNode *out)
522 // First, copy the actual value,
524 if (in->hasValue()) {
525 switch (in->getType()) {
526 case SGPropertyNode::BOOL:
527 if (!out->setBoolValue(in->getBoolValue()))
530 case SGPropertyNode::INT:
531 if (!out->setIntValue(in->getIntValue()))
534 case SGPropertyNode::LONG:
535 if (!out->setLongValue(in->getLongValue()))
538 case SGPropertyNode::FLOAT:
539 if (!out->setFloatValue(in->getFloatValue()))
542 case SGPropertyNode::DOUBLE:
543 if (!out->setDoubleValue(in->getDoubleValue()))
546 case SGPropertyNode::STRING:
547 if (!out->setStringValue(in->getStringValue()))
550 case SGPropertyNode::UNSPECIFIED:
551 if (!out->setUnspecifiedValue(in->getStringValue()))
555 string message = "Unknown internal SGPropertyNode type";
556 message += in->getType();
557 throw sg_error(message, "SimGear Property Reader");
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(),
568 if (!copyProperties(in_child, out_child))
575 // end of props_io.cxx