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|SGPropertyNode::ARCHIVE)
37 ////////////////////////////////////////////////////////////////////////
38 // Property list visitor, for XML parsing.
39 ////////////////////////////////////////////////////////////////////////
41 class PropsVisitor : public XMLVisitor
45 PropsVisitor (SGPropertyNode * root, const string &base)
46 : _ok(true), _root(root), _level(0), _base(base) {}
50 void startElement (const char * name, const XMLAttributes &atts);
51 void endElement (const char * name);
52 void data (const char * s, int length);
53 void warning (const char * message, int line, int column);
54 void error (const char * message, int line, int column);
56 bool isOK () const { return _ok; }
62 State () : node(0), type(""), mode(DEFAULT_MODE) {}
63 State (SGPropertyNode * _node, const char * _type, int _mode)
64 : node(_node), type(_type), mode(_mode) {}
65 SGPropertyNode * node;
68 map<string,int> counters;
71 State &state () { return _state_stack[_state_stack.size() - 1]; }
73 void push_state (SGPropertyNode * node, const char * type, int mode) {
75 _state_stack.push_back(State(node, "unspecified", mode));
77 _state_stack.push_back(State(node, type, mode));
83 _state_stack.pop_back();
89 SGPropertyNode * _root;
91 vector<State> _state_stack;
96 PropsVisitor::startXML ()
99 _state_stack.resize(0);
103 PropsVisitor::endXML ()
106 _state_stack.resize(0);
111 * Check a yes/no flag that defaults to 'yes'.
114 checkFlag (const char * flag)
116 if (flag == 0 || string(flag) == "y")
118 else if (string(flag) == "n")
121 SG_LOG(SG_INPUT, SG_ALERT, "Unrecognized flag value '" << flag
122 << "', assuming 'y'");
128 PropsVisitor::startElement (const char * name, const XMLAttributes &atts)
133 if (string(name) != (string)"PropertyList") {
134 SG_LOG(SG_INPUT, SG_ALERT, "Root element name is " <<
135 name << "; expected PropertyList");
138 push_state(_root, "", DEFAULT_MODE);
145 attval = atts.getValue("n");
148 index = atoi(attval);
149 st.counters[name] = SG_MAX2(st.counters[name], index+1);
151 index = st.counters[name];
155 // Got the index, so grab the node.
156 SGPropertyNode * node = st.node->getChild(name, index, true);
158 // Get the access-mode attributes,
159 // but don't set yet (in case they
160 // prevent us from recording the value).
163 attval = atts.getValue("read");
164 if (checkFlag(attval))
165 mode |= SGPropertyNode::READ;
166 attval = atts.getValue("write");
167 if (checkFlag(attval))
168 mode |= SGPropertyNode::WRITE;
169 attval = atts.getValue("archive");
170 if (checkFlag(attval))
171 mode |= SGPropertyNode::ARCHIVE;
173 // Check for an alias.
174 attval = atts.getValue("alias");
176 if (!node->alias(attval))
177 SG_LOG(SG_INPUT, SG_ALERT, "Failed to set alias to " << attval);
180 // Check for an include.
181 attval = atts.getValue("include");
183 SGPath path(SGPath(_base).dir());
184 cerr << "Base is " << _base << endl;
185 cerr << "Dir is " << SGPath(_base).dir() << endl;
187 if (!readProperties(path.str(), node)) {
188 SG_LOG(SG_INPUT, SG_ALERT, "Failed to read include file "
194 push_state(node, atts.getValue("type"), mode);
199 PropsVisitor::endElement (const char * name)
204 // If there are no children and it's
205 // not an alias, then it's a leaf value.
206 if (st.node->nChildren() == 0 && !st.node->isAlias()) {
207 if (st.type == "bool") {
208 if (_data == "true" || atoi(_data.c_str()) != 0)
209 ret = st.node->setBoolValue(true);
211 ret = st.node->setBoolValue(false);
212 } else if (st.type == "int") {
213 ret = st.node->setIntValue(atoi(_data.c_str()));
214 } else if (st.type == "long") {
215 ret = st.node->setLongValue(strtol(_data.c_str(), 0, 0));
216 } else if (st.type == "float") {
217 ret = st.node->setFloatValue(atof(_data.c_str()));
218 } else if (st.type == "double") {
219 ret = st.node->setDoubleValue(strtod(_data.c_str(), 0));
220 } else if (st.type == "string") {
221 ret = st.node->setStringValue(_data);
222 } else if (st.type == "unspecified") {
223 ret = st.node->setUnspecifiedValue(_data);
225 SG_LOG(SG_INPUT, SG_ALERT, "Unrecognized data type " << st.type
226 << " assuming 'unspecified'");
227 ret = st.node->setUnspecifiedValue(_data);
230 SG_LOG(SG_INPUT, SG_ALERT, "readProperties: Failed to set "
231 << st.node->getPath() << " to value \""
232 << _data << "\" with type " << st.type);
235 // Set the access-mode attributes now,
236 // once the value has already been
238 st.node->setAttributes(st.mode);
244 PropsVisitor::data (const char * s, int length)
246 if (state().node->nChildren() == 0)
247 _data.append(string(s, length));
251 PropsVisitor::warning (const char * message, int line, int column)
253 SG_LOG(SG_INPUT, SG_ALERT, "readProperties: warning: "
254 << message << " at line " << line << ", column " << column);
258 PropsVisitor::error (const char * message, int line, int column)
260 SG_LOG(SG_INPUT, SG_ALERT, "readProperties: FATAL: " <<
261 message << " at line " << line << ", column " << column);
267 ////////////////////////////////////////////////////////////////////////
268 // Property list reader.
269 ////////////////////////////////////////////////////////////////////////
273 * Read properties from an input stream.
275 * @param input The input stream containing an XML property file.
276 * @param start_node The root node for reading properties.
277 * @param base A base path for resolving external include references.
278 * @return true if the read succeeded, false otherwise.
281 readProperties (istream &input, SGPropertyNode * start_node,
284 PropsVisitor visitor(start_node, base);
285 return readXML(input, visitor) && visitor.isOK();
290 * Read properties from a file.
292 * @param file A string containing the file path.
293 * @param start_node The root node for reading properties.
294 * @return true if the read succeeded, false otherwise.
297 readProperties (const string &file, SGPropertyNode * start_node)
299 cerr << "Reading properties from " << file << endl;
300 ifstream input(file.c_str());
302 return readProperties(input, start_node, file);
304 SG_LOG(SG_INPUT, SG_ALERT, "Error reading property list from file "
312 ////////////////////////////////////////////////////////////////////////
313 // Property list writer.
314 ////////////////////////////////////////////////////////////////////////
316 #define INDENT_STEP 2
319 * Return the type name.
322 getTypeName (SGPropertyNode::Type type)
325 case SGPropertyNode::UNSPECIFIED:
326 return "unspecified";
327 case SGPropertyNode::BOOL:
329 case SGPropertyNode::INT:
331 case SGPropertyNode::LONG:
333 case SGPropertyNode::FLOAT:
335 case SGPropertyNode::DOUBLE:
337 case SGPropertyNode::STRING:
341 // keep the compiler from squawking
342 return "unspecified";
347 * Escape characters for output.
350 writeData (ostream &output, const string &data)
352 for (int i = 0; i < (int)data.size(); i++) {
371 doIndent (ostream &output, int indent)
373 while (indent-- > 0) {
380 writeAtts (ostream &output, const SGPropertyNode * node)
382 int index = node->getIndex();
385 output << " n = \"" << index << '"';
387 if (!node->getAttribute(SGPropertyNode::READ))
388 output << " read=\"n\"";
390 if (!node->getAttribute(SGPropertyNode::WRITE))
391 output << " write=\"n\"";
393 if (!node->getAttribute(SGPropertyNode::ARCHIVE))
394 output << " archive=\"n\"";
400 writeNode (ostream &output, const SGPropertyNode * node, int indent)
402 // Don't write the node or any of
403 // its descendants unless it is
404 // allowed to be archived.
405 if (!node->getAttribute(SGPropertyNode::ARCHIVE))
406 return true; // Everything's OK, but we won't write.
408 const string &name = node->getName();
409 int index = node->getIndex();
410 int nChildren = node->nChildren();
412 // If there is a literal value,
414 if (node->hasValue()) {
415 doIndent(output, indent);
416 output << '<' << name;
417 writeAtts(output, node);
418 if (node->isAlias() && node->getAliasTarget() != 0) {
419 output << " alias=\"" << node->getAliasTarget()->getPath()
422 if (node->getType() != SGPropertyNode::UNSPECIFIED)
423 output << " type=\"" << getTypeName(node->getType()) << '"';
425 writeData(output, node->getStringValue());
426 output << "</" << name << '>' << endl;
430 // If there are children, write them
432 if (nChildren > 0 || node->isAlias()) {
433 doIndent(output, indent);
434 output << '<' << name;
435 writeAtts(output, node);
436 output << '>' << endl;
437 for (int i = 0; i < nChildren; i++)
438 writeNode(output, node->getChild(i), indent + INDENT_STEP);
439 doIndent(output, indent);
440 output << "</" << name << '>' << endl;
448 * Write a property tree to an output stream in XML format.
450 * @param output The output stream.
451 * @param start_node The root node to write.
452 * @return true if the write succeeded, false otherwise.
455 writeProperties (ostream &output, const SGPropertyNode * start_node)
457 int nChildren = start_node->nChildren();
459 output << "<?xml version=\"1.0\"?>" << endl << endl;
460 output << "<PropertyList>" << endl;
462 for (int i = 0; i < nChildren; i++) {
463 writeNode(output, start_node->getChild(i), INDENT_STEP);
466 output << "</PropertyList>" << endl;
473 * Write a property tree to a file in XML format.
475 * @param file The destination file.
476 * @param start_node The root node to write.
477 * @return true if the write succeeded, false otherwise.
480 writeProperties (const string &file, const SGPropertyNode * start_node)
482 ofstream output(file.c_str());
484 return writeProperties(output, start_node);
486 SG_LOG(SG_INPUT, SG_ALERT, "Cannot write properties to file "
494 ////////////////////////////////////////////////////////////////////////
495 // Copy properties from one tree to another.
496 ////////////////////////////////////////////////////////////////////////
500 * Copy one property tree to another.
502 * @param in The source property tree.
503 * @param out The destination property tree.
504 * @return true if all properties were copied, false if some failed
505 * (for example, if the property's value is tied read-only).
508 copyProperties (const SGPropertyNode *in, SGPropertyNode *out)
512 // First, copy the actual value,
514 if (in->hasValue()) {
515 switch (in->getType()) {
516 case SGPropertyNode::BOOL:
517 if (!out->setBoolValue(in->getBoolValue()))
520 case SGPropertyNode::INT:
521 if (!out->setIntValue(in->getIntValue()))
524 case SGPropertyNode::LONG:
525 if (!out->setLongValue(in->getLongValue()))
528 case SGPropertyNode::FLOAT:
529 if (!out->setFloatValue(in->getFloatValue()))
532 case SGPropertyNode::DOUBLE:
533 if (!out->setDoubleValue(in->getDoubleValue()))
536 case SGPropertyNode::STRING:
537 if (!out->setStringValue(in->getStringValue()))
540 case SGPropertyNode::UNSPECIFIED:
541 if (!out->setUnspecifiedValue(in->getStringValue()))
545 throw string("Unrecognized SGPropertyNode type");
549 // Next, copy the children.
550 int nChildren = in->nChildren();
551 for (int i = 0; i < nChildren; i++) {
552 const SGPropertyNode * in_child = in->getChild(i);
553 SGPropertyNode * out_child = out->getChild(in_child->getName(),
554 in_child->getIndex(),
556 if (!copyProperties(in_child, out_child))
563 // end of props_io.cxx