2014-08-06 19:59:02 +05:30
|
|
|
#ifndef BASE_XML_UTILS_H
|
|
|
|
#define BASE_XML_UTILS_H
|
|
|
|
|
2014-08-26 17:35:21 +05:30
|
|
|
#include <base/progress_monitor.h>
|
2014-08-06 19:59:02 +05:30
|
|
|
#include <boost/lexical_cast.hpp>
|
|
|
|
#include <boost/optional.hpp>
|
2014-08-26 15:53:29 +05:30
|
|
|
#include <expat.h>
|
2014-08-26 17:35:21 +05:30
|
|
|
#include <iosfwd>
|
2014-08-06 19:59:02 +05:30
|
|
|
#include <map>
|
2014-09-01 19:41:04 +05:30
|
|
|
#include <stdexcept>
|
2014-08-06 19:59:02 +05:30
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
namespace xml_utils {
|
2014-08-26 15:53:29 +05:30
|
|
|
// Simple wrapper to ensure the parser gets freed if an exception
|
|
|
|
// is thrown during parsing.
|
|
|
|
class xml_parser {
|
|
|
|
public:
|
|
|
|
xml_parser()
|
|
|
|
: parser_(XML_ParserCreate(NULL)) {
|
|
|
|
|
|
|
|
if (!parser_)
|
|
|
|
throw runtime_error("couldn't create xml parser");
|
|
|
|
}
|
|
|
|
|
|
|
|
~xml_parser() {
|
|
|
|
XML_ParserFree(parser_);
|
|
|
|
}
|
|
|
|
|
|
|
|
XML_Parser get_parser() {
|
|
|
|
return parser_;
|
|
|
|
}
|
|
|
|
|
2014-08-26 17:35:21 +05:30
|
|
|
void parse(std::string const &backup_file, bool quiet);
|
|
|
|
|
2014-08-26 15:53:29 +05:30
|
|
|
private:
|
2014-08-26 17:35:21 +05:30
|
|
|
size_t get_file_length(string const &file) const;
|
|
|
|
auto_ptr<base::progress_monitor> create_monitor(bool quiet);
|
|
|
|
|
2014-08-26 15:53:29 +05:30
|
|
|
XML_Parser parser_;
|
|
|
|
};
|
|
|
|
|
2014-08-06 19:59:02 +05:30
|
|
|
typedef std::map<std::string, std::string> attributes;
|
|
|
|
|
|
|
|
void build_attributes(attributes &a, char const **attr);
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
T get_attr(attributes const &attr, string const &key) {
|
|
|
|
attributes::const_iterator it = attr.find(key);
|
|
|
|
if (it == attr.end()) {
|
|
|
|
ostringstream out;
|
|
|
|
out << "could not find attribute: " << key;
|
|
|
|
throw runtime_error(out.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
return boost::lexical_cast<T>(it->second);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
boost::optional<T> get_opt_attr(attributes const &attr, string const &key) {
|
|
|
|
typedef boost::optional<T> rtype;
|
|
|
|
attributes::const_iterator it = attr.find(key);
|
|
|
|
if (it == attr.end())
|
|
|
|
return rtype();
|
|
|
|
|
|
|
|
return rtype(boost::lexical_cast<T>(it->second));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
|
|
|
#endif
|