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=\""
348 << node->getAliasTarget()->getPath() << "\"/>" << endl;
350 if (node->getType() != SGValue::UNKNOWN)
351 output << "\" type=\"" << getTypeName(node->getType()) << '"';
353 writeData(output, node->getStringValue());
354 output << "</" << name << '>' << endl;;
358 // If there are children, write them
361 doIndent(output, indent);
362 output << '<' << name << " n=\"" << index << "\">" << endl;;
363 for (int i = 0; i < nChildren; i++)
364 writeNode(output, node->getChild(i), indent + INDENT_STEP);
365 doIndent(output, indent);
366 output << "</" << name << '>' << endl;
369 // If there were no children and no
370 // value, at least note the presence
372 if (nChildren == 0 && !node->hasValue()) {
373 doIndent(output, indent);
374 output << '<' << name << " n=\"" << index << "\"/>" << endl;
382 * Write a property tree to an output stream in XML format.
384 * @param output The output stream.
385 * @param start_node The root node to write.
386 * @return true if the write succeeded, false otherwise.
389 writeProperties (ostream &output, const SGPropertyNode * start_node)
391 int nChildren = start_node->nChildren();
393 output << "<?xml version=\"1.0\"?>" << endl << endl;
394 output << "<PropertyList>" << endl;
396 for (int i = 0; i < nChildren; i++) {
397 writeNode(output, start_node->getChild(i), INDENT_STEP);
400 output << "</PropertyList>" << endl;
407 * Write a property tree to a file in XML format.
409 * @param file The destination file.
410 * @param start_node The root node to write.
411 * @return true if the write succeeded, false otherwise.
414 writeProperties (const string &file, const SGPropertyNode * start_node)
416 ofstream output(file.c_str());
418 return writeProperties(output, start_node);
420 SG_LOG(SG_INPUT, SG_ALERT, "Cannot write properties to file "
428 ////////////////////////////////////////////////////////////////////////
429 // Copy properties from one tree to another.
430 ////////////////////////////////////////////////////////////////////////
434 * Copy one property tree to another.
436 * @param in The source property tree.
437 * @param out The destination property tree.
438 * @return true if all properties were copied, false if some failed
439 * (for example, if the property's value is tied read-only).
442 copyProperties (const SGPropertyNode *in, SGPropertyNode *out)
446 // First, copy the actual value,
448 if (in->hasValue()) {
449 switch (in->getType()) {
451 if (!out->setBoolValue(in->getBoolValue()))
455 if (!out->setIntValue(in->getIntValue()))
459 if (!out->setLongValue(in->getLongValue()))
463 if (!out->setFloatValue(in->getFloatValue()))
466 case SGValue::DOUBLE:
467 if (!out->setDoubleValue(in->getDoubleValue()))
470 case SGValue::STRING:
471 if (!out->setStringValue(in->getStringValue()))
474 case SGValue::UNKNOWN:
475 if (!out->setUnknownValue(in->getStringValue()))
479 throw string("Unknown SGValue type");
483 // Next, copy the children.
484 int nChildren = in->nChildren();
485 for (int i = 0; i < nChildren; i++) {
486 const SGPropertyNode * in_child = in->getChild(i);
487 SGPropertyNode * out_child = out->getChild(in_child->getName(),
488 in_child->getIndex(),
490 if (!copyProperties(in_child, out_child))
497 // end of props_io.cxx