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:
346 case SGPropertyNode::ALIAS:
347 case SGPropertyNode::NONE:
348 return "unspecified";
351 // keep the compiler from squawking
352 return "unspecified";
357 * Escape characters for output.
360 writeData (ostream &output, const string &data)
362 for (int i = 0; i < (int)data.size(); i++) {
381 doIndent (ostream &output, int indent)
383 while (indent-- > 0) {
390 writeAtts (ostream &output, const SGPropertyNode * node)
392 int index = node->getIndex();
395 output << " n=\"" << index << '"';
398 if (!node->getAttribute(SGPropertyNode::READ))
399 output << " read=\"n\"";
401 if (!node->getAttribute(SGPropertyNode::WRITE))
402 output << " write=\"n\"";
404 if (node->getAttribute(SGPropertyNode::ARCHIVE))
405 output << " archive=\"y\"";
412 * Test whether a node is archivable or has archivable descendants.
415 isArchivable (const SGPropertyNode * node)
417 // FIXME: it's inefficient to do this all the time
418 if (node->getAttribute(SGPropertyNode::ARCHIVE))
421 int nChildren = node->nChildren();
422 for (int i = 0; i < nChildren; i++)
423 if (isArchivable(node->getChild(i)))
431 writeNode (ostream &output, const SGPropertyNode * node,
432 bool write_all, int indent)
434 // Don't write the node or any of
435 // its descendants unless it is
436 // allowed to be archived.
437 if (!write_all && !isArchivable(node))
438 return true; // Everything's OK, but we won't write.
440 const string &name = node->getName();
441 int nChildren = node->nChildren();
443 // If there is a literal value,
445 if (node->hasValue() && (write_all || node->getAttribute(SGPropertyNode::ARCHIVE))) {
446 doIndent(output, indent);
447 output << '<' << name;
448 writeAtts(output, node);
449 if (node->isAlias() && node->getAliasTarget() != 0) {
450 output << " alias=\"" << node->getAliasTarget()->getPath()
453 if (node->getType() != SGPropertyNode::UNSPECIFIED)
454 output << " type=\"" << getTypeName(node->getType()) << '"';
456 writeData(output, node->getStringValue());
457 output << "</" << name << '>' << endl;
461 // If there are children, write them next.
462 if (nChildren > 0 || node->isAlias()) {
463 doIndent(output, indent);
464 output << '<' << name;
465 writeAtts(output, node);
466 output << '>' << endl;
467 for (int i = 0; i < nChildren; i++)
468 writeNode(output, node->getChild(i), write_all, indent + INDENT_STEP);
469 doIndent(output, indent);
470 output << "</" << name << '>' << endl;
478 writeProperties (ostream &output, const SGPropertyNode * start_node,
481 int nChildren = start_node->nChildren();
483 output << "<?xml version=\"1.0\"?>" << endl << endl;
484 output << "<PropertyList>" << endl;
486 for (int i = 0; i < nChildren; i++) {
487 writeNode(output, start_node->getChild(i), write_all, INDENT_STEP);
490 output << "</PropertyList>" << endl;
495 writeProperties (const string &file, const SGPropertyNode * start_node,
498 ofstream output(file.c_str());
500 writeProperties(output, start_node, write_all);
502 throw sg_io_exception("Cannot open file", sg_location(file));
508 ////////////////////////////////////////////////////////////////////////
509 // Copy properties from one tree to another.
510 ////////////////////////////////////////////////////////////////////////
514 * Copy one property tree to another.
516 * @param in The source property tree.
517 * @param out The destination property tree.
518 * @return true if all properties were copied, false if some failed
519 * (for example, if the property's value is tied read-only).
522 copyProperties (const SGPropertyNode *in, SGPropertyNode *out)
526 // First, copy the actual value,
528 if (in->hasValue()) {
529 switch (in->getType()) {
530 case SGPropertyNode::BOOL:
531 if (!out->setBoolValue(in->getBoolValue()))
534 case SGPropertyNode::INT:
535 if (!out->setIntValue(in->getIntValue()))
538 case SGPropertyNode::LONG:
539 if (!out->setLongValue(in->getLongValue()))
542 case SGPropertyNode::FLOAT:
543 if (!out->setFloatValue(in->getFloatValue()))
546 case SGPropertyNode::DOUBLE:
547 if (!out->setDoubleValue(in->getDoubleValue()))
550 case SGPropertyNode::STRING:
551 if (!out->setStringValue(in->getStringValue()))
554 case SGPropertyNode::UNSPECIFIED:
555 if (!out->setUnspecifiedValue(in->getStringValue()))
559 string message = "Unknown internal SGPropertyNode type";
560 message += in->getType();
561 throw sg_error(message, "SimGear Property Reader");
565 // Next, copy the children.
566 int nChildren = in->nChildren();
567 for (int i = 0; i < nChildren; i++) {
568 const SGPropertyNode * in_child = in->getChild(i);
569 SGPropertyNode * out_child = out->getChild(in_child->getName(),
570 in_child->getIndex(),
572 if (!copyProperties(in_child, out_child))
579 // end of props_io.cxx