Factor common code out of the xml_format files.

This commit is contained in:
Joe Thornber
2014-08-06 15:29:02 +01:00
parent d8f678b079
commit 1d38b390b5
5 changed files with 81 additions and 85 deletions

View File

@@ -17,7 +17,9 @@
// <http://www.gnu.org/licenses/>.
#include "xml_format.h"
#include "base/indented_stream.h"
#include "base/xml_utils.h"
#include <boost/lexical_cast.hpp>
#include <boost/optional.hpp>
@@ -30,6 +32,7 @@
using namespace std;
using namespace thin_provisioning;
using namespace xml_utils;
namespace tp = thin_provisioning;
@@ -134,47 +137,6 @@ namespace {
//------------------------------------------------
// XML parser
//------------------------------------------------
typedef std::map<string, string> attributes;
void build_attributes(attributes &a, char const **attr) {
while (*attr) {
char const *key = *attr;
attr++;
if (!*attr) {
ostringstream out;
out << "No value given for xml attribute: " << key;
throw runtime_error(out.str());
}
char const *value = *attr;
a.insert(make_pair(string(key), string(value)));
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));
}
void parse_superblock(emitter *e, attributes const &attr) {
e->begin_superblock(get_attr<string>(attr, "uuid"),
get_attr<uint64_t>(attr, "time"),