From: Matthias Braun Date: Sat, 11 Jun 2005 21:11:03 +0000 (+0000) Subject: added beginning of automatic docu generation for the scripting interface X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=7bc083aca4963cb30540ca793580ccf74c4e72e3;p=supertux.git added beginning of automatic docu generation for the scripting interface SVN-Revision: 2590 --- diff --git a/tools/miniswig/xmlwriter.cpp b/tools/miniswig/xmlwriter.cpp new file mode 100644 index 000000000..1a85247ec --- /dev/null +++ b/tools/miniswig/xmlwriter.cpp @@ -0,0 +1,78 @@ +#include + +#include +#include +#include "xmlwriter.h" + +XmlWriter::XmlWriter(std::ostream& outstream) + : out(outstream), indent(0) +{ +} + +XmlWriter::~XmlWriter() +{ + if(sections.size() > 0) { + std::cerr << "WARNING: NOT CLOSED: "; + for(std::vector::iterator i = sections.begin(); + i != sections.end(); ++i) + std::cerr << *i << " "; + std::cerr << "\n"; + } + + closeTag(); +} + +void XmlWriter::openTag(const char* name) +{ + newLine(); + out << "<" << name; + closetag = ">"; + indent++; + + sections.push_back(name); +} + +void XmlWriter::closeTag(const char* name) +{ + if(sections.size() == 0) + throw std::runtime_error("got closeSection without prior openSection."); + + const std::string& lastsection = sections.back(); + if (lastsection != name) { + std::ostringstream msg; + msg << "mismtach in open/closeSection. Expected '" + << lastsection << "' got '" << name << "'"; + throw std::runtime_error(msg.str()); + } + sections.pop_back(); + + indent--; + newLine(); + // XXX: We should check for consistency here + out << "" ; +} + +void XmlWriter::writeTag(const char* name) +{ + newLine(); + out << "<" << name; + closetag = "/>"; + lasttag = name; +} + +void XmlWriter::newLine() +{ + if(closetag != "") { + closeTag(); + for (int i=0;i +#include +#include + +/** This class is a class which helps printing formated xml output. + * Example: + * This sequence: + * xml.openTag("world"); + * xml.writeAttribute("name", "foo"); + * xml.writeTag("bar"); + * xml.writeTag("baz"); + * xml.writeAttribute("name", "boo"); + * xml.writeAttribute("style", "old"); + * xml.write("text"); + * xml.closeTag("world"); + * results in this output: + * + * + * text + * + */ +class XmlWriter { +public: + XmlWriter(std::ostream& out); + ~XmlWriter(); + + /** Start a xml tag which contains subtags */ + void openTag(const char* name); + /** Closes an xml tag with subtags */ + void closeTag(const char* name); + + void writeTag(const char* name); + + template + void comment(const T& outp) + { // This routine writes just about anything as an XML comment. + newLine(); + out << ""; + } + + + template + void write(const T& text) + { + if (closetag[0]=='>') { + out << ">"; + closetag = ""; + } else if (closetag[0]=='/') { + out << ">"; // eventually we should place a \n here + closetag = ""; + } + out << text; + } + + template + void writeAttribute(const char* name, T value) + { + out << " " << name << "=\"" << value << "\""; + } + +private: + void newLine(); + void closeTag(); + + std::ostream& out; + int indent; + std::string closetag; + std::string lasttag; + std::vector sections; +}; + +#endif +