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);
35 ////////////////////////////////////////////////////////////////////////
36 // Property list visitor, for XML parsing.
37 ////////////////////////////////////////////////////////////////////////
39 class PropsVisitor : public XMLVisitor
43 PropsVisitor (SGPropertyNode * root, const string &base)
44 : _ok(true), _root(root), _level(0), _base(base) {}
48 void startElement (const char * name, const XMLAttributes &atts);
49 void endElement (const char * name);
50 void data (const char * s, int length);
51 void warning (const char * message, int line, int column);
52 void error (const char * message, int line, int column);
54 bool isOK () const { return _ok; }
60 State () : node(0), type("") {}
61 State (SGPropertyNode * _node, const char * _type)
62 : node(_node), type(_type) {}
63 SGPropertyNode * node;
65 map<string,int> counters;
68 State &state () { return _state_stack[_state_stack.size() - 1]; }
70 void push_state (SGPropertyNode * node, const char * type) {
72 _state_stack.push_back(State(node, "unknown"));
74 _state_stack.push_back(State(node, type));
80 _state_stack.pop_back();
86 SGPropertyNode * _root;
88 vector<State> _state_stack;
93 PropsVisitor::startXML ()
96 _state_stack.resize(0);
100 PropsVisitor::endXML ()
103 _state_stack.resize(0);
107 PropsVisitor::startElement (const char * name, const XMLAttributes &atts)
112 if (string(name) != (string)"PropertyList") {
113 SG_LOG(SG_INPUT, SG_ALERT, "Root element name is " <<
114 name << "; expected PropertyList");
117 push_state(_root, "");
122 const char * att_n = atts.getValue("n");
126 st.counters[name] = SG_MAX2(st.counters[name], index+1);
128 index = st.counters[name];
132 // Check for an alias.
133 SGPropertyNode * node = st.node->getChild(name, index, true);
134 const char * att_alias = atts.getValue("alias");
135 if (att_alias != 0) {
136 if (!node->alias(att_alias))
137 SG_LOG(SG_INPUT, SG_ALERT, "Failed to set alias to " << att_alias);
140 // Check for an include.
141 const char * att_include = atts.getValue("include");
142 if (att_include != 0) {
143 SGPath path(SGPath(_base).dir());
144 cerr << "Base is " << _base << endl;
145 cerr << "Dir is " << SGPath(_base).dir() << endl;
146 path.append(att_include);
147 if (!readProperties(path.str(), node)) {
148 SG_LOG(SG_INPUT, SG_ALERT, "Failed to read include file "
154 push_state(node, atts.getValue("type"));
159 PropsVisitor::endElement (const char * name)
164 // If there are no children and it's
165 // not an alias, then it's a leaf value.
166 if (st.node->nChildren() == 0 && !st.node->isAlias()) {
167 if (st.type == "bool") {
168 if (_data == "true" || atoi(_data.c_str()) != 0)
169 ret = st.node->setBoolValue(true);
171 ret = st.node->setBoolValue(false);
172 } else if (st.type == "int") {
173 ret = st.node->setIntValue(atoi(_data.c_str()));
174 } else if (st.type == "long") {
175 ret = st.node->setLongValue(strtol(_data.c_str(), 0, 0));
176 } else if (st.type == "float") {
177 ret = st.node->setFloatValue(atof(_data.c_str()));
178 } else if (st.type == "double") {
179 ret = st.node->setDoubleValue(strtod(_data.c_str(), 0));
180 } else if (st.type == "string") {
181 ret = st.node->setStringValue(_data);
182 } else if (st.type == "unknown") {
183 ret = st.node->setUnknownValue(_data);
185 SG_LOG(SG_INPUT, SG_ALERT, "Unknown data type " << st.type
186 << " assuming 'unknown'");
187 ret = st.node->setUnknownValue(_data);
190 SG_LOG(SG_INPUT, SG_ALERT, "readProperties: Failed to set "
191 << st.node->getPath() << " to value \""
192 << _data << "\" with type " << st.type);
199 PropsVisitor::data (const char * s, int length)
201 if (state().node->nChildren() == 0)
202 _data.append(string(s, length));
206 PropsVisitor::warning (const char * message, int line, int column)
208 SG_LOG(SG_INPUT, SG_ALERT, "readProperties: warning: "
209 << message << " at line " << line << ", column " << column);
213 PropsVisitor::error (const char * message, int line, int column)
215 SG_LOG(SG_INPUT, SG_ALERT, "readProperties: FATAL: " <<
216 message << " at line " << line << ", column " << column);
222 ////////////////////////////////////////////////////////////////////////
223 // Property list reader.
224 ////////////////////////////////////////////////////////////////////////
228 * Read properties from an input stream.
230 * @param input The input stream containing an XML property file.
231 * @param start_node The root node for reading properties.
232 * @param base A base path for resolving external include references.
233 * @return true if the read succeeded, false otherwise.
236 readProperties (istream &input, SGPropertyNode * start_node,
239 PropsVisitor visitor(start_node, base);
240 return readXML(input, visitor) && visitor.isOK();
245 * Read properties from a file.
247 * @param file A string containing the file path.
248 * @param start_node The root node for reading properties.
249 * @return true if the read succeeded, false otherwise.
252 readProperties (const string &file, SGPropertyNode * start_node)
254 cerr << "Reading properties from " << file << endl;
255 ifstream input(file.c_str());
257 return readProperties(input, start_node, file);
259 SG_LOG(SG_INPUT, SG_ALERT, "Error reading property list from file "
267 ////////////////////////////////////////////////////////////////////////
268 // Property list writer.
269 ////////////////////////////////////////////////////////////////////////
271 #define INDENT_STEP 2
274 * Return the type name.
277 getTypeName (SGValue::Type type)
280 case SGValue::UNKNOWN:
290 case SGValue::DOUBLE:
292 case SGValue::STRING:
296 // keep the compiler from squawking
302 * Escape characters for output.
305 writeData (ostream &output, const string &data)
307 for (int i = 0; i < (int)data.size(); i++) {
326 doIndent (ostream &output, int indent)
328 while (indent-- > 0) {
335 writeNode (ostream &output, const SGPropertyNode * node, int indent)
337 const string &name = node->getName();
338 int index = node->getIndex();
339 int nChildren = node->nChildren();
341 // If there is a literal value,
343 if (node->hasValue()) {
344 doIndent(output, indent);
345 output << '<' << name << " n=\"" << index << '"';
346 if (node->isAlias() && node->getAliasTarget() != 0)
347 output << " alias=\"" << node->getAliasTarget()->getPath() << "\"/>";
349 if (node->getType() != SGValue::UNKNOWN)
350 output << " type=\"" << getTypeName(node->getType()) << '"';
352 writeData(output, node->getStringValue());
353 output << "</" << name << '>' << endl;
357 // If there are children, write them
359 if (nChildren > 0 || node->isAlias()) {
360 doIndent(output, indent);
361 output << '<' << name << " n=\"" << index << '"';
362 if (node->isAlias() && node->getAliasTarget() != 0)
363 output << " alias=\"" << node->getAliasTarget()->getPath() << '"';
364 output << '>' << endl;
365 for (int i = 0; i < nChildren; i++)
366 writeNode(output, node->getChild(i), indent + INDENT_STEP);
367 doIndent(output, indent);
368 output << "</" << name << '>' << endl;
371 // If there were no children and no
372 // value, at least note the presence
374 // if (nChildren == 0 && !node->isAlias() && !node->hasValue()) {
375 // doIndent(output, indent);
376 // output << '<' << name << " n=\"" << index << "\"/>" << endl;
384 * Write a property tree to an output stream in XML format.
386 * @param output The output stream.
387 * @param start_node The root node to write.
388 * @return true if the write succeeded, false otherwise.
391 writeProperties (ostream &output, const SGPropertyNode * start_node)
393 int nChildren = start_node->nChildren();
395 output << "<?xml version=\"1.0\"?>" << endl << endl;
396 output << "<PropertyList>" << endl;
398 for (int i = 0; i < nChildren; i++) {
399 writeNode(output, start_node->getChild(i), INDENT_STEP);
402 output << "</PropertyList>" << endl;
409 * Write a property tree to a file in XML format.
411 * @param file The destination file.
412 * @param start_node The root node to write.
413 * @return true if the write succeeded, false otherwise.
416 writeProperties (const string &file, const SGPropertyNode * start_node)
418 ofstream output(file.c_str());
420 return writeProperties(output, start_node);
422 SG_LOG(SG_INPUT, SG_ALERT, "Cannot write properties to file "
430 ////////////////////////////////////////////////////////////////////////
431 // Copy properties from one tree to another.
432 ////////////////////////////////////////////////////////////////////////
436 * Copy one property tree to another.
438 * @param in The source property tree.
439 * @param out The destination property tree.
440 * @return true if all properties were copied, false if some failed
441 * (for example, if the property's value is tied read-only).
444 copyProperties (const SGPropertyNode *in, SGPropertyNode *out)
448 // First, copy the actual value,
450 if (in->hasValue()) {
451 switch (in->getType()) {
453 if (!out->setBoolValue(in->getBoolValue()))
457 if (!out->setIntValue(in->getIntValue()))
461 if (!out->setLongValue(in->getLongValue()))
465 if (!out->setFloatValue(in->getFloatValue()))
468 case SGValue::DOUBLE:
469 if (!out->setDoubleValue(in->getDoubleValue()))
472 case SGValue::STRING:
473 if (!out->setStringValue(in->getStringValue()))
476 case SGValue::UNKNOWN:
477 if (!out->setUnknownValue(in->getStringValue()))
481 throw string("Unknown SGValue type");
485 // Next, copy the children.
486 int nChildren = in->nChildren();
487 for (int i = 0; i < nChildren; i++) {
488 const SGPropertyNode * in_child = in->getChild(i);
489 SGPropertyNode * out_child = out->getChild(in_child->getName(),
490 in_child->getIndex(),
492 if (!copyProperties(in_child, out_child))
499 // end of props_io.cxx