3 # include <simgear_config.h>
6 #include <simgear/compiler.h>
8 #include <stdlib.h> // atof() atoi()
10 #include <simgear/sg_inlines.h>
11 #include <simgear/debug/logstream.hxx>
12 #include <simgear/misc/sg_path.hxx>
13 #include <simgear/xml/easyxml.hxx>
16 #include "props_io.hxx"
24 SG_USING_STD(istream);
25 SG_USING_STD(ifstream);
26 SG_USING_STD(ostream);
27 SG_USING_STD(ofstream);
32 #define DEFAULT_MODE (SGPropertyNode::READ|SGPropertyNode::WRITE)
36 ////////////////////////////////////////////////////////////////////////
37 // Property list visitor, for XML parsing.
38 ////////////////////////////////////////////////////////////////////////
40 class PropsVisitor : public XMLVisitor
44 PropsVisitor (SGPropertyNode * root, const string &base, int default_mode = 0)
45 : _default_mode(default_mode), _root(root), _level(0), _base(base), _hasException(false) {}
47 virtual ~PropsVisitor () {}
51 void startElement (const char * name, const XMLAttributes &atts);
52 void endElement (const char * name);
53 void data (const char * s, int length);
54 void warning (const char * message, int line, int column);
56 bool hasException () const { return _hasException; }
57 sg_io_exception &getException () { return _exception; }
58 void setException (const sg_io_exception &exception) {
59 _exception = exception;
67 State () : node(0), type(""), mode(DEFAULT_MODE) {}
68 State (SGPropertyNode * _node, const char * _type, int _mode)
69 : node(_node), type(_type), mode(_mode) {}
70 SGPropertyNode * node;
73 map<string,int> counters;
76 State &state () { return _state_stack[_state_stack.size() - 1]; }
78 void push_state (SGPropertyNode * node, const char * type, int mode) {
80 _state_stack.push_back(State(node, "unspecified", mode));
82 _state_stack.push_back(State(node, type, mode));
88 _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 (!strcmp(flag, "y"))
127 else if (!strcmp(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 (strcmp(name, "PropertyList")) {
145 string message = "Root element name is ";
147 message += "; expected PropertyList";
148 throw sg_io_exception(message, "SimGear Property Reader");
151 // Check for an include.
152 attval = atts.getValue("include");
154 SGPath path(SGPath(_base).dir());
157 readProperties(path.str(), _root);
158 } catch (sg_io_exception &e) {
163 push_state(_root, "", DEFAULT_MODE);
169 attval = atts.getValue("n");
172 index = atoi(attval);
173 st.counters[name] = SG_MAX2(st.counters[name], index+1);
175 index = st.counters[name];
179 // Got the index, so grab the node.
180 SGPropertyNode * node = st.node->getChild(name, index, true);
182 // Get the access-mode attributes,
183 // but don't set yet (in case they
184 // prevent us from recording the value).
185 int mode = _default_mode;
187 attval = atts.getValue("read");
188 if (checkFlag(attval, true))
189 mode |= SGPropertyNode::READ;
190 attval = atts.getValue("write");
191 if (checkFlag(attval, true))
192 mode |= SGPropertyNode::WRITE;
193 attval = atts.getValue("archive");
194 if (checkFlag(attval, false))
195 mode |= SGPropertyNode::ARCHIVE;
196 attval = atts.getValue("trace-read");
197 if (checkFlag(attval, false))
198 mode |= SGPropertyNode::TRACE_READ;
199 attval = atts.getValue("trace-write");
200 if (checkFlag(attval, false))
201 mode |= SGPropertyNode::TRACE_WRITE;
202 attval = atts.getValue("userarchive");
203 if (checkFlag(attval, false))
204 mode |= SGPropertyNode::USERARCHIVE;
206 // Check for an alias.
207 attval = atts.getValue("alias");
209 if (!node->alias(attval))
210 SG_LOG(SG_INPUT, SG_ALERT, "Failed to set alias to " << attval);
213 // Check for an include.
214 attval = atts.getValue("include");
216 SGPath path(SGPath(_base).dir());
219 readProperties(path.str(), node);
220 } catch (sg_io_exception &e) {
225 const char *type = atts.getValue("type");
228 push_state(node, type, mode);
233 PropsVisitor::endElement (const char * name)
238 // If there are no children and it's
239 // not an alias, then it's a leaf value.
240 if (st.node->nChildren() == 0 && !st.node->isAlias()) {
241 if (st.type == "bool") {
242 if (_data == "true" || atoi(_data.c_str()) != 0)
243 ret = st.node->setBoolValue(true);
245 ret = st.node->setBoolValue(false);
246 } else if (st.type == "int") {
247 ret = st.node->setIntValue(atoi(_data.c_str()));
248 } else if (st.type == "long") {
249 ret = st.node->setLongValue(strtol(_data.c_str(), 0, 0));
250 } else if (st.type == "float") {
251 ret = st.node->setFloatValue(atof(_data.c_str()));
252 } else if (st.type == "double") {
253 ret = st.node->setDoubleValue(strtod(_data.c_str(), 0));
254 } else if (st.type == "string") {
255 ret = st.node->setStringValue(_data.c_str());
256 } else if (st.type == "unspecified") {
257 ret = st.node->setUnspecifiedValue(_data.c_str());
259 string message = "Unrecognized data type '";
262 // FIXME: add location information
263 throw sg_io_exception(message, "SimGear Property Reader");
266 SG_LOG(SG_INPUT, SG_ALERT, "readProperties: Failed to set "
267 << st.node->getPath() << " to value \""
268 << _data << "\" with type " << st.type);
271 // Set the access-mode attributes now,
272 // once the value has already been
274 st.node->setAttributes(st.mode);
280 PropsVisitor::data (const char * s, int length)
282 if (state().node->nChildren() == 0)
283 _data.append(string(s, length));
287 PropsVisitor::warning (const char * message, int line, int column)
289 SG_LOG(SG_INPUT, SG_ALERT, "readProperties: warning: "
290 << message << " at line " << line << ", column " << column);
295 ////////////////////////////////////////////////////////////////////////
296 // Property list reader.
297 ////////////////////////////////////////////////////////////////////////
301 * Read properties from an input stream.
303 * @param input The input stream containing an XML property file.
304 * @param start_node The root node for reading properties.
305 * @param base A base path for resolving external include references.
306 * @return true if the read succeeded, false otherwise.
309 readProperties (istream &input, SGPropertyNode * start_node,
310 const string &base, int default_mode)
312 PropsVisitor visitor(start_node, base, default_mode);
313 readXML(input, visitor, base);
314 if (visitor.hasException())
315 throw visitor.getException();
320 * Read properties from a file.
322 * @param file A string containing the file path.
323 * @param start_node The root node for reading properties.
324 * @return true if the read succeeded, false otherwise.
327 readProperties (const string &file, SGPropertyNode * start_node,
330 PropsVisitor visitor(start_node, file, default_mode);
331 readXML(file, visitor);
332 if (visitor.hasException())
333 throw visitor.getException();
338 * Read properties from an in-memory buffer.
340 * @param buf A character buffer containing the xml data.
341 * @param size The size/length of the buffer in bytes
342 * @param start_node The root node for reading properties.
343 * @return true if the read succeeded, false otherwise.
345 void readProperties (const char *buf, const int size,
346 SGPropertyNode * start_node, int default_mode)
348 PropsVisitor visitor(start_node, "", default_mode);
349 readXML(buf, size, visitor);
350 if (visitor.hasException())
351 throw visitor.getException();
355 ////////////////////////////////////////////////////////////////////////
356 // Property list writer.
357 ////////////////////////////////////////////////////////////////////////
359 #define INDENT_STEP 2
362 * Return the type name.
365 getTypeName (SGPropertyNode::Type type)
368 case SGPropertyNode::UNSPECIFIED:
369 return "unspecified";
370 case SGPropertyNode::BOOL:
372 case SGPropertyNode::INT:
374 case SGPropertyNode::LONG:
376 case SGPropertyNode::FLOAT:
378 case SGPropertyNode::DOUBLE:
380 case SGPropertyNode::STRING:
382 case SGPropertyNode::ALIAS:
383 case SGPropertyNode::NONE:
384 return "unspecified";
387 // keep the compiler from squawking
388 return "unspecified";
393 * Escape characters for output.
396 writeData (ostream &output, const string &data)
398 for (int i = 0; i < (int)data.size(); i++) {
417 doIndent (ostream &output, int indent)
419 while (indent-- > 0) {
426 writeAtts (ostream &output, const SGPropertyNode * node)
428 int index = node->getIndex();
431 output << " n=\"" << index << '"';
434 if (!node->getAttribute(SGPropertyNode::READ))
435 output << " read=\"n\"";
437 if (!node->getAttribute(SGPropertyNode::WRITE))
438 output << " write=\"n\"";
440 if (node->getAttribute(SGPropertyNode::ARCHIVE))
441 output << " archive=\"y\"";
448 * Test whether a node is archivable or has archivable descendants.
451 isArchivable (const SGPropertyNode * node, SGPropertyNode::Attribute archive_flag)
453 // FIXME: it's inefficient to do this all the time
454 if (node->getAttribute(archive_flag))
457 int nChildren = node->nChildren();
458 for (int i = 0; i < nChildren; i++)
459 if (isArchivable(node->getChild(i), archive_flag))
467 writeNode (ostream &output, const SGPropertyNode * node,
468 bool write_all, int indent, SGPropertyNode::Attribute archive_flag)
470 // Don't write the node or any of
471 // its descendants unless it is
472 // allowed to be archived.
473 if (!write_all && !isArchivable(node, archive_flag))
474 return true; // Everything's OK, but we won't write.
476 const string name = node->getName();
477 int nChildren = node->nChildren();
479 // If there is a literal value,
481 if (node->hasValue() && (write_all || node->getAttribute(archive_flag))) {
482 doIndent(output, indent);
483 output << '<' << name;
484 writeAtts(output, node);
485 if (node->isAlias() && node->getAliasTarget() != 0) {
486 output << " alias=\"" << node->getAliasTarget()->getPath()
489 if (node->getType() != SGPropertyNode::UNSPECIFIED)
490 output << " type=\"" << getTypeName(node->getType()) << '"';
492 writeData(output, node->getStringValue());
493 output << "</" << name << '>' << endl;
497 // If there are children, write them next.
499 doIndent(output, indent);
500 output << '<' << name;
501 writeAtts(output, node);
502 output << '>' << endl;
503 for (int i = 0; i < nChildren; i++)
504 writeNode(output, node->getChild(i), write_all, indent + INDENT_STEP, archive_flag);
505 doIndent(output, indent);
506 output << "</" << name << '>' << endl;
514 writeProperties (ostream &output, const SGPropertyNode * start_node,
515 bool write_all, SGPropertyNode::Attribute archive_flag)
517 int nChildren = start_node->nChildren();
519 output << "<?xml version=\"1.0\"?>" << endl << endl;
520 output << "<PropertyList>" << endl;
522 for (int i = 0; i < nChildren; i++) {
523 writeNode(output, start_node->getChild(i), write_all, INDENT_STEP, archive_flag);
526 output << "</PropertyList>" << endl;
531 writeProperties (const string &file, const SGPropertyNode * start_node,
532 bool write_all, SGPropertyNode::Attribute archive_flag)
534 ofstream output(file.c_str());
536 writeProperties(output, start_node, write_all, archive_flag);
538 throw sg_io_exception("Cannot open file", sg_location(file));
544 ////////////////////////////////////////////////////////////////////////
545 // Copy properties from one tree to another.
546 ////////////////////////////////////////////////////////////////////////
550 * Copy one property tree to another.
552 * @param in The source property tree.
553 * @param out The destination property tree.
554 * @return true if all properties were copied, false if some failed
555 * (for example, if the property's value is tied read-only).
558 copyProperties (const SGPropertyNode *in, SGPropertyNode *out)
562 // First, copy the actual value,
564 if (in->hasValue()) {
565 switch (in->getType()) {
566 case SGPropertyNode::BOOL:
567 if (!out->setBoolValue(in->getBoolValue()))
570 case SGPropertyNode::INT:
571 if (!out->setIntValue(in->getIntValue()))
574 case SGPropertyNode::LONG:
575 if (!out->setLongValue(in->getLongValue()))
578 case SGPropertyNode::FLOAT:
579 if (!out->setFloatValue(in->getFloatValue()))
582 case SGPropertyNode::DOUBLE:
583 if (!out->setDoubleValue(in->getDoubleValue()))
586 case SGPropertyNode::STRING:
587 if (!out->setStringValue(in->getStringValue()))
590 case SGPropertyNode::UNSPECIFIED:
591 if (!out->setUnspecifiedValue(in->getStringValue()))
597 string message = "Unknown internal SGPropertyNode type";
598 message += in->getType();
599 throw sg_error(message, "SimGear Property Reader");
603 // copy the attributes.
604 out->setAttributes( in->getAttributes() );
606 // Next, copy the children.
607 int nChildren = in->nChildren();
608 for (int i = 0; i < nChildren; i++) {
609 const SGPropertyNode * in_child = in->getChild(i);
610 SGPropertyNode * out_child = out->getChild(in_child->getName(),
611 in_child->getIndex(),
613 if (!copyProperties(in_child, out_child))
620 // end of props_io.cxx