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/xml/easyxml.hxx>
14 #include "sg_path.hxx"
18 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
27 #if !defined(SG_HAVE_NATIVE_SGI_COMPILERS)
28 SG_USING_STD(istream);
29 SG_USING_STD(ifstream);
30 SG_USING_STD(ostream);
31 SG_USING_STD(ofstream);
39 ////////////////////////////////////////////////////////////////////////
40 // Property list visitor, for XML parsing.
41 ////////////////////////////////////////////////////////////////////////
43 class PropsVisitor : public XMLVisitor
47 PropsVisitor (SGPropertyNode * root, const string &base)
48 : _ok(true), _root(root), _level(0), _base(base) {}
52 void startElement (const char * name, const XMLAttributes &atts);
53 void endElement (const char * name);
54 void data (const char * s, int length);
55 void warning (const char * message, int line, int column);
56 void error (const char * message, int line, int column);
58 bool isOK () const { return _ok; }
64 State () : node(0), type("") {}
65 State (SGPropertyNode * _node, const char * _type)
66 : node(_node), type(_type) {}
67 SGPropertyNode * node;
69 map<string,int> counters;
72 State &state () { return _state_stack[_state_stack.size() - 1]; }
74 void push_state (SGPropertyNode * node, const char * type) {
76 _state_stack.push_back(State(node, "unknown"));
78 _state_stack.push_back(State(node, type));
84 _state_stack.pop_back();
90 SGPropertyNode * _root;
92 vector<State> _state_stack;
97 PropsVisitor::startXML ()
100 _state_stack.resize(0);
104 PropsVisitor::endXML ()
107 _state_stack.resize(0);
111 PropsVisitor::startElement (const char * name, const XMLAttributes &atts)
116 if (string(name) != (string)"PropertyList") {
117 SG_LOG(SG_INPUT, SG_ALERT, "Root element name is " <<
118 name << "; expected PropertyList");
121 push_state(_root, "");
126 const char * att_n = atts.getValue("n");
130 st.counters[name] = SG_MAX2(st.counters[name], index+1);
132 index = st.counters[name];
136 // Check for an alias.
137 SGPropertyNode * node = st.node->getChild(name, index, true);
138 const char * att_alias = atts.getValue("alias");
139 if (att_alias != 0) {
140 if (!node->alias(att_alias))
141 SG_LOG(SG_INPUT, SG_ALERT, "Failed to set alias to " << att_alias);
144 // Check for an include.
145 const char * att_include = atts.getValue("include");
146 if (att_include != 0) {
147 SGPath path(SGPath(_base).dir());
148 cerr << "Base is " << _base << endl;
149 cerr << "Dir is " << SGPath(_base).dir() << endl;
150 path.append(att_include);
151 if (!readProperties(path.str(), node)) {
152 SG_LOG(SG_INPUT, SG_ALERT, "Failed to read include file "
158 push_state(node, atts.getValue("type"));
163 PropsVisitor::endElement (const char * name)
168 // If there are no children and it's
169 // not an alias, then it's a leaf value.
170 if (st.node->nChildren() == 0 && !st.node->isAlias()) {
171 if (st.type == "bool") {
172 if (_data == "true" || atoi(_data.c_str()) != 0)
173 ret = st.node->setBoolValue(true);
175 ret = st.node->setBoolValue(false);
176 } else if (st.type == "int") {
177 ret = st.node->setIntValue(atoi(_data.c_str()));
178 } else if (st.type == "long") {
179 ret = st.node->setLongValue(strtol(_data.c_str(), 0, 0));
180 } else if (st.type == "float") {
181 ret = st.node->setFloatValue(atof(_data.c_str()));
182 } else if (st.type == "double") {
183 ret = st.node->setDoubleValue(strtod(_data.c_str(), 0));
184 } else if (st.type == "string") {
185 ret = st.node->setStringValue(_data);
186 } else if (st.type == "unknown") {
187 ret = st.node->setUnknownValue(_data);
189 SG_LOG(SG_INPUT, SG_ALERT, "Unknown data type " << st.type
190 << " assuming 'unknown'");
191 ret = st.node->setUnknownValue(_data);
194 SG_LOG(SG_INPUT, SG_ALERT, "readProperties: Failed to set "
195 << st.node->getPath() << " to value \""
196 << _data << "\" with type " << st.type);
203 PropsVisitor::data (const char * s, int length)
205 if (state().node->nChildren() == 0)
206 _data.append(string(s, length));
210 PropsVisitor::warning (const char * message, int line, int column)
212 SG_LOG(SG_INPUT, SG_ALERT, "readProperties: warning: "
213 << message << " at line " << line << ", column " << column);
217 PropsVisitor::error (const char * message, int line, int column)
219 SG_LOG(SG_INPUT, SG_ALERT, "readProperties: FATAL: " <<
220 message << " at line " << line << ", column " << column);
226 ////////////////////////////////////////////////////////////////////////
227 // Property list reader.
228 ////////////////////////////////////////////////////////////////////////
232 * Read properties from an input stream.
234 * @param input The input stream containing an XML property file.
235 * @param start_node The root node for reading properties.
236 * @param base A base path for resolving external include references.
237 * @return true if the read succeeded, false otherwise.
240 readProperties (istream &input, SGPropertyNode * start_node,
243 PropsVisitor visitor(start_node, base);
244 return readXML(input, visitor) && visitor.isOK();
249 * Read properties from a file.
251 * @param file A string containing the file path.
252 * @param start_node The root node for reading properties.
253 * @return true if the read succeeded, false otherwise.
256 readProperties (const string &file, SGPropertyNode * start_node)
258 cerr << "Reading properties from " << file << endl;
259 ifstream input(file.c_str());
261 return readProperties(input, start_node, file);
263 SG_LOG(SG_INPUT, SG_ALERT, "Error reading property list from file "
271 ////////////////////////////////////////////////////////////////////////
272 // Property list writer.
273 ////////////////////////////////////////////////////////////////////////
275 #define INDENT_STEP 2
278 * Return the type name.
281 getTypeName (SGValue::Type type)
284 case SGValue::UNKNOWN:
294 case SGValue::DOUBLE:
296 case SGValue::STRING:
300 // keep the compiler from squawking
306 * Escape characters for output.
309 writeData (ostream &output, const string &data)
311 for (int i = 0; i < (int)data.size(); i++) {
330 doIndent (ostream &output, int indent)
332 while (indent-- > 0) {
339 writeNode (ostream &output, const SGPropertyNode * node, int indent)
341 const string &name = node->getName();
342 int index = node->getIndex();
343 int nChildren = node->nChildren();
345 // If there is a literal value,
347 if (node->hasValue()) {
348 doIndent(output, indent);
349 output << '<' << name << " n=\"" << index;
350 if (node->isAlias() && node->getAliasTarget() != 0) {
351 output << "\" alias=\""
352 << node->getAliasTarget()->getPath() << "\"/>" << endl;
354 if (node->getType() != SGValue::UNKNOWN)
355 output << "\" type=\"" << getTypeName(node->getType()) << '"';
357 writeData(output, node->getStringValue());
358 output << "</" << name << '>' << endl;;
362 // If there are children, write them
365 doIndent(output, indent);
366 output << '<' << name << " n=\"" << index << "\">" << endl;;
367 for (int i = 0; i < nChildren; i++)
368 writeNode(output, node->getChild(i), indent + INDENT_STEP);
369 doIndent(output, indent);
370 output << "</" << name << '>' << endl;
373 // If there were no children and no
374 // value, at least note the presence
376 if (nChildren == 0 && !node->hasValue()) {
377 doIndent(output, indent);
378 output << '<' << name << " n=\"" << index << "\"/>" << endl;
386 * Write a property tree to an output stream in XML format.
388 * @param output The output stream.
389 * @param start_node The root node to write.
390 * @return true if the write succeeded, false otherwise.
393 writeProperties (ostream &output, const SGPropertyNode * start_node)
395 int nChildren = start_node->nChildren();
397 output << "<?xml version=\"1.0\"?>" << endl << endl;
398 output << "<PropertyList>" << endl;
400 for (int i = 0; i < nChildren; i++) {
401 writeNode(output, start_node->getChild(i), INDENT_STEP);
404 output << "</PropertyList>" << endl;
411 * Write a property tree to a file in XML format.
413 * @param file The destination file.
414 * @param start_node The root node to write.
415 * @return true if the write succeeded, false otherwise.
418 writeProperties (const string &file, const SGPropertyNode * start_node)
420 ofstream output(file.c_str());
422 return writeProperties(output, start_node);
424 SG_LOG(SG_INPUT, SG_ALERT, "Cannot write properties to file "
432 ////////////////////////////////////////////////////////////////////////
433 // Copy properties from one tree to another.
434 ////////////////////////////////////////////////////////////////////////
438 * Copy one property tree to another.
440 * @param in The source property tree.
441 * @param out The destination property tree.
442 * @return true if all properties were copied, false if some failed
443 * (for example, if the property's value is tied read-only).
446 copyProperties (const SGPropertyNode *in, SGPropertyNode *out)
450 // First, copy the actual value,
452 if (in->hasValue()) {
453 switch (in->getType()) {
455 if (!out->setBoolValue(in->getBoolValue()))
459 if (!out->setIntValue(in->getIntValue()))
463 if (!out->setLongValue(in->getLongValue()))
467 if (!out->setFloatValue(in->getFloatValue()))
470 case SGValue::DOUBLE:
471 if (!out->setDoubleValue(in->getDoubleValue()))
474 case SGValue::STRING:
475 if (!out->setStringValue(in->getStringValue()))
478 case SGValue::UNKNOWN:
479 if (!out->setUnknownValue(in->getStringValue()))
483 throw string("Unknown SGValue type");
487 // Next, copy the children.
488 int nChildren = in->nChildren();
489 for (int i = 0; i < nChildren; i++) {
490 const SGPropertyNode * in_child = in->getChild(i);
491 SGPropertyNode * out_child = out->getChild(in_child->getName(),
492 in_child->getIndex(),
494 if (!copyProperties(in_child, out_child))
501 // end of props_io.cxx