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

@ -47,6 +47,7 @@ SOURCE=\
base/endian_utils.cc \
base/error_state.cc \
base/progress_monitor.cc \
base/xml_utils.cc \
\
caching/hint_array.cc \
caching/superblock.cc \
@ -175,6 +176,7 @@ THIN_CHECK_SOURCE=\
\
base/error_state.cc \
base/endian_utils.cc \
base/xml_utils.cc \
\
persistent-data/checksum.cc \
persistent-data/error_set.cc \
@ -198,6 +200,7 @@ THIN_DELTA_SOURCE=\
\
base/error_state.cc \
base/endian_utils.cc \
base/xml_utils.cc \
\
persistent-data/checksum.cc \
persistent-data/error_set.cc \
@ -287,6 +290,7 @@ CACHE_CHECK_SOURCE=\
base/base64.cc \
base/error_state.cc \
base/endian_utils.cc \
base/xml_utils.cc \
\
persistent-data/checksum.cc \
persistent-data/error_set.cc \
@ -348,6 +352,7 @@ ERA_CHECK_SOURCE=\
base/base64.cc \
base/error_state.cc \
base/endian_utils.cc \
base/xml_utils.cc \
\
era/writeset_tree.cc \
era/era_detail.cc \
@ -380,6 +385,7 @@ ERA_DUMP_SOURCE=\
base/error_state.cc \
base/endian_utils.cc \
base/progress_monitor.cc \
base/xml_utils.cc \
\
era/writeset_tree.cc \
era/era_detail.cc \
@ -413,6 +419,7 @@ ERA_INVALIDATE_SOURCE=\
base/base64.cc \
base/error_state.cc \
base/endian_utils.cc \
base/xml_utils.cc \
\
era/writeset_tree.cc \
era/era_detail.cc \

25
base/xml_utils.cc Normal file
View File

@ -0,0 +1,25 @@
#include "xml_utils.h"
//----------------------------------------------------------------
void
xml_utils::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++;
}
}
//----------------------------------------------------------------

42
base/xml_utils.h Normal file
View File

@ -0,0 +1,42 @@
#ifndef BASE_XML_UTILS_H
#define BASE_XML_UTILS_H
#include <boost/lexical_cast.hpp>
#include <boost/optional.hpp>
#include <map>
using namespace std;
//----------------------------------------------------------------
namespace xml_utils {
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

View File

@ -1,6 +1,8 @@
#include "caching/xml_format.h"
#include "base/base64.h"
#include "base/indented_stream.h"
#include "caching/xml_format.h"
#include "base/xml_utils.h"
#include <boost/lexical_cast.hpp>
#include <expat.h>
@ -8,6 +10,7 @@
using namespace caching;
using namespace persistent_data;
using namespace std;
using namespace xml_utils;
//----------------------------------------------------------------
@ -116,49 +119,6 @@ namespace {
//--------------------------------
// Parser
//--------------------------------
// FIXME: factor out common code with thinp one
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, "block_size"),

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"),