3 * Started Fall 2000 by David Megginson, david@megginson.com
4 * This code is released into the Public Domain.
6 * See props.html for documentation [replace with URL when available].
12 # include <simgear_config.h>
15 #include <simgear/compiler.h>
17 #include <stdlib.h> // atof() atoi()
19 #include <simgear/sg_inlines.h>
20 #include <simgear/debug/logstream.hxx>
21 #include <simgear/misc/sg_path.hxx>
22 #include <simgear/xml/easyxml.hxx>
25 #include "props_io.hxx"
33 SG_USING_STD(istream);
34 SG_USING_STD(ifstream);
35 SG_USING_STD(ostream);
36 SG_USING_STD(ofstream);
41 #define DEFAULT_MODE (SGPropertyNode::READ|SGPropertyNode::WRITE)
45 ////////////////////////////////////////////////////////////////////////
46 // Property list visitor, for XML parsing.
47 ////////////////////////////////////////////////////////////////////////
49 class PropsVisitor : public XMLVisitor
53 PropsVisitor (SGPropertyNode * root, const string &base, int default_mode = 0)
54 : _default_mode(default_mode), _root(root), _level(0), _base(base), _hasException(false) {}
56 virtual ~PropsVisitor () {}
60 void startElement (const char * name, const XMLAttributes &atts);
61 void endElement (const char * name);
62 void data (const char * s, int length);
63 void warning (const char * message, int line, int column);
65 bool hasException () const { return _hasException; }
66 sg_io_exception &getException () { return _exception; }
67 void setException (const sg_io_exception &exception) {
68 _exception = exception;
76 State () : node(0), type(""), mode(DEFAULT_MODE) {}
77 State (SGPropertyNode * _node, const char * _type, int _mode)
78 : node(_node), type(_type), mode(_mode) {}
79 SGPropertyNode * node;
82 map<string,int> counters;
85 State &state () { return _state_stack[_state_stack.size() - 1]; }
87 void push_state (SGPropertyNode * node, const char * type, int mode) {
89 _state_stack.push_back(State(node, "unspecified", mode));
91 _state_stack.push_back(State(node, type, mode));
97 _state_stack.pop_back();
103 SGPropertyNode * _root;
105 vector<State> _state_stack;
107 sg_io_exception _exception;
112 PropsVisitor::startXML ()
115 _state_stack.resize(0);
119 PropsVisitor::endXML ()
122 _state_stack.resize(0);
127 * Check a yes/no flag, with default.
130 checkFlag (const char * flag, bool defaultState = true)
134 else if (!strcmp(flag, "y"))
136 else if (!strcmp(flag, "n"))
139 string message = "Unrecognized flag value '";
142 // FIXME: add location info
143 throw sg_io_exception(message, "SimGear Property Reader");
148 PropsVisitor::startElement (const char * name, const XMLAttributes &atts)
153 if (strcmp(name, "PropertyList")) {
154 string message = "Root element name is ";
156 message += "; expected PropertyList";
157 throw sg_io_exception(message, "SimGear Property Reader");
160 // Check for an include.
161 attval = atts.getValue("include");
163 SGPath path(SGPath(_base).dir());
166 readProperties(path.str(), _root);
167 } catch (sg_io_exception &e) {
172 push_state(_root, "", DEFAULT_MODE);
178 attval = atts.getValue("n");
181 index = atoi(attval);
182 st.counters[name] = SG_MAX2(st.counters[name], index+1);
184 index = st.counters[name];
188 // Got the index, so grab the node.
189 SGPropertyNode * node = st.node->getChild(name, index, true);
191 // Get the access-mode attributes,
192 // but don't set yet (in case they
193 // prevent us from recording the value).
194 int mode = _default_mode;
196 attval = atts.getValue("read");
197 if (checkFlag(attval, true))
198 mode |= SGPropertyNode::READ;
199 attval = atts.getValue("write");
200 if (checkFlag(attval, true))
201 mode |= SGPropertyNode::WRITE;
202 attval = atts.getValue("archive");
203 if (checkFlag(attval, false))
204 mode |= SGPropertyNode::ARCHIVE;
205 attval = atts.getValue("trace-read");
206 if (checkFlag(attval, false))
207 mode |= SGPropertyNode::TRACE_READ;
208 attval = atts.getValue("trace-write");
209 if (checkFlag(attval, false))
210 mode |= SGPropertyNode::TRACE_WRITE;
211 attval = atts.getValue("userarchive");
212 if (checkFlag(attval, false))
213 mode |= SGPropertyNode::USERARCHIVE;
215 // Check for an alias.
216 attval = atts.getValue("alias");
218 if (!node->alias(attval))
219 SG_LOG(SG_INPUT, SG_ALERT, "Failed to set alias to " << attval);
222 // Check for an include.
223 attval = atts.getValue("include");
225 SGPath path(SGPath(_base).dir());
228 readProperties(path.str(), node);
229 } catch (sg_io_exception &e) {
233 const char *omit = atts.getValue("omit-node");
234 if (omit && !strcmp(omit, "y")) {
235 int nChildren = node->nChildren();
236 for (int i = 0; i < nChildren; i++) {
237 SGPropertyNode *src = node->getChild(i);
238 const char *name = src->getName();
239 int index = st.counters[name];
241 SGPropertyNode *dst = st.node->getChild(name, index, true);
242 copyProperties(src, dst);
244 st.node->removeChild(node->getName(), node->getIndex(), false);
249 const char *type = atts.getValue("type");
252 push_state(node, type, mode);
257 PropsVisitor::endElement (const char * name)
262 // If there are no children and it's
263 // not an alias, then it's a leaf value.
264 if (st.node->nChildren() == 0 && !st.node->isAlias()) {
265 if (st.type == "bool") {
266 if (_data == "true" || atoi(_data.c_str()) != 0)
267 ret = st.node->setBoolValue(true);
269 ret = st.node->setBoolValue(false);
270 } else if (st.type == "int") {
271 ret = st.node->setIntValue(atoi(_data.c_str()));
272 } else if (st.type == "long") {
273 ret = st.node->setLongValue(strtol(_data.c_str(), 0, 0));
274 } else if (st.type == "float") {
275 ret = st.node->setFloatValue(atof(_data.c_str()));
276 } else if (st.type == "double") {
277 ret = st.node->setDoubleValue(strtod(_data.c_str(), 0));
278 } else if (st.type == "string") {
279 ret = st.node->setStringValue(_data.c_str());
280 } else if (st.type == "unspecified") {
281 ret = st.node->setUnspecifiedValue(_data.c_str());
283 string message = "Unrecognized data type '";
286 // FIXME: add location information
287 throw sg_io_exception(message, "SimGear Property Reader");
290 SG_LOG(SG_INPUT, SG_ALERT, "readProperties: Failed to set "
291 << st.node->getPath() << " to value \""
292 << _data << "\" with type " << st.type);
295 // Set the access-mode attributes now,
296 // once the value has already been
298 st.node->setAttributes(st.mode);
304 PropsVisitor::data (const char * s, int length)
306 if (state().node->nChildren() == 0)
307 _data.append(string(s, length));
311 PropsVisitor::warning (const char * message, int line, int column)
313 SG_LOG(SG_INPUT, SG_ALERT, "readProperties: warning: "
314 << message << " at line " << line << ", column " << column);
319 ////////////////////////////////////////////////////////////////////////
320 // Property list reader.
321 ////////////////////////////////////////////////////////////////////////
325 * Read properties from an input stream.
327 * @param input The input stream containing an XML property file.
328 * @param start_node The root node for reading properties.
329 * @param base A base path for resolving external include references.
330 * @return true if the read succeeded, false otherwise.
333 readProperties (istream &input, SGPropertyNode * start_node,
334 const string &base, int default_mode)
336 PropsVisitor visitor(start_node, base, default_mode);
337 readXML(input, visitor, base);
338 if (visitor.hasException())
339 throw visitor.getException();
344 * Read properties from a file.
346 * @param file A string containing the file path.
347 * @param start_node The root node for reading properties.
348 * @return true if the read succeeded, false otherwise.
351 readProperties (const string &file, SGPropertyNode * start_node,
354 PropsVisitor visitor(start_node, file, default_mode);
355 readXML(file, visitor);
356 if (visitor.hasException())
357 throw visitor.getException();
362 * Read properties from an in-memory buffer.
364 * @param buf A character buffer containing the xml data.
365 * @param size The size/length of the buffer in bytes
366 * @param start_node The root node for reading properties.
367 * @return true if the read succeeded, false otherwise.
369 void readProperties (const char *buf, const int size,
370 SGPropertyNode * start_node, int default_mode)
372 PropsVisitor visitor(start_node, "", default_mode);
373 readXML(buf, size, visitor);
374 if (visitor.hasException())
375 throw visitor.getException();
379 ////////////////////////////////////////////////////////////////////////
380 // Property list writer.
381 ////////////////////////////////////////////////////////////////////////
383 #define INDENT_STEP 2
386 * Return the type name.
389 getTypeName (SGPropertyNode::Type type)
392 case SGPropertyNode::UNSPECIFIED:
393 return "unspecified";
394 case SGPropertyNode::BOOL:
396 case SGPropertyNode::INT:
398 case SGPropertyNode::LONG:
400 case SGPropertyNode::FLOAT:
402 case SGPropertyNode::DOUBLE:
404 case SGPropertyNode::STRING:
406 case SGPropertyNode::ALIAS:
407 case SGPropertyNode::NONE:
408 return "unspecified";
411 // keep the compiler from squawking
412 return "unspecified";
417 * Escape characters for output.
420 writeData (ostream &output, const string &data)
422 for (int i = 0; i < (int)data.size(); i++) {
441 doIndent (ostream &output, int indent)
443 while (indent-- > 0) {
450 writeAtts (ostream &output, const SGPropertyNode * node, bool forceindex)
452 int index = node->getIndex();
454 if (index != 0 || forceindex)
455 output << " n=\"" << index << '"';
458 if (!node->getAttribute(SGPropertyNode::READ))
459 output << " read=\"n\"";
461 if (!node->getAttribute(SGPropertyNode::WRITE))
462 output << " write=\"n\"";
464 if (node->getAttribute(SGPropertyNode::ARCHIVE))
465 output << " archive=\"y\"";
472 * Test whether a node is archivable or has archivable descendants.
475 isArchivable (const SGPropertyNode * node, SGPropertyNode::Attribute archive_flag)
477 // FIXME: it's inefficient to do this all the time
478 if (node->getAttribute(archive_flag))
481 int nChildren = node->nChildren();
482 for (int i = 0; i < nChildren; i++)
483 if (isArchivable(node->getChild(i), archive_flag))
491 writeNode (ostream &output, const SGPropertyNode * node,
492 bool write_all, int indent, SGPropertyNode::Attribute archive_flag)
494 // Don't write the node or any of
495 // its descendants unless it is
496 // allowed to be archived.
497 if (!write_all && !isArchivable(node, archive_flag))
498 return true; // Everything's OK, but we won't write.
500 const string name = node->getName();
501 int nChildren = node->nChildren();
502 bool node_has_value = false;
504 // If there is a literal value,
506 if (node->hasValue() && (write_all || node->getAttribute(archive_flag))) {
507 doIndent(output, indent);
508 output << '<' << name;
509 writeAtts(output, node, nChildren != 0);
510 if (node->isAlias() && node->getAliasTarget() != 0) {
511 output << " alias=\"" << node->getAliasTarget()->getPath()
514 if (node->getType() != SGPropertyNode::UNSPECIFIED)
515 output << " type=\"" << getTypeName(node->getType()) << '"';
517 writeData(output, node->getStringValue());
518 output << "</" << name << '>' << endl;
520 node_has_value = true;
523 // If there are children, write them next.
525 doIndent(output, indent);
526 output << '<' << name;
527 writeAtts(output, node, node_has_value);
528 output << '>' << endl;
529 for (int i = 0; i < nChildren; i++)
530 writeNode(output, node->getChild(i), write_all, indent + INDENT_STEP, archive_flag);
531 doIndent(output, indent);
532 output << "</" << name << '>' << endl;
540 writeProperties (ostream &output, const SGPropertyNode * start_node,
541 bool write_all, SGPropertyNode::Attribute archive_flag)
543 int nChildren = start_node->nChildren();
545 output << "<?xml version=\"1.0\"?>" << endl << endl;
546 output << "<PropertyList>" << endl;
548 for (int i = 0; i < nChildren; i++) {
549 writeNode(output, start_node->getChild(i), write_all, INDENT_STEP, archive_flag);
552 output << "</PropertyList>" << endl;
557 writeProperties (const string &file, const SGPropertyNode * start_node,
558 bool write_all, SGPropertyNode::Attribute archive_flag)
560 SGPath path(file.c_str());
561 path.create_dir(0777);
563 ofstream output(file.c_str());
565 writeProperties(output, start_node, write_all, archive_flag);
567 throw sg_io_exception("Cannot open file", sg_location(file));
573 ////////////////////////////////////////////////////////////////////////
574 // Copy properties from one tree to another.
575 ////////////////////////////////////////////////////////////////////////
579 * Copy one property tree to another.
581 * @param in The source property tree.
582 * @param out The destination property tree.
583 * @return true if all properties were copied, false if some failed
584 * (for example, if the property's value is tied read-only).
587 copyProperties (const SGPropertyNode *in, SGPropertyNode *out)
591 // First, copy the actual value,
593 if (in->hasValue()) {
594 switch (in->getType()) {
595 case SGPropertyNode::BOOL:
596 if (!out->setBoolValue(in->getBoolValue()))
599 case SGPropertyNode::INT:
600 if (!out->setIntValue(in->getIntValue()))
603 case SGPropertyNode::LONG:
604 if (!out->setLongValue(in->getLongValue()))
607 case SGPropertyNode::FLOAT:
608 if (!out->setFloatValue(in->getFloatValue()))
611 case SGPropertyNode::DOUBLE:
612 if (!out->setDoubleValue(in->getDoubleValue()))
615 case SGPropertyNode::STRING:
616 if (!out->setStringValue(in->getStringValue()))
619 case SGPropertyNode::UNSPECIFIED:
620 if (!out->setUnspecifiedValue(in->getStringValue()))
626 string message = "Unknown internal SGPropertyNode type";
627 message += in->getType();
628 throw sg_error(message, "SimGear Property Reader");
632 // copy the attributes.
633 out->setAttributes( in->getAttributes() );
635 // Next, copy the children.
636 int nChildren = in->nChildren();
637 for (int i = 0; i < nChildren; i++) {
638 const SGPropertyNode * in_child = in->getChild(i);
639 SGPropertyNode * out_child = out->getChild(in_child->getName(),
640 in_child->getIndex(),
642 if (!copyProperties(in_child, out_child))
649 // end of props_io.cxx