From 1d38b390b5e1559e0465f29fa98457fd566e1285 Mon Sep 17 00:00:00 2001 From: Joe Thornber Date: Wed, 6 Aug 2014 15:29:02 +0100 Subject: [PATCH] Factor common code out of the xml_format files. --- Makefile.in | 7 +++++ base/xml_utils.cc | 25 +++++++++++++++++ base/xml_utils.h | 42 +++++++++++++++++++++++++++++ caching/xml_format.cc | 48 +++------------------------------ thin-provisioning/xml_format.cc | 44 +++--------------------------- 5 files changed, 81 insertions(+), 85 deletions(-) create mode 100644 base/xml_utils.cc create mode 100644 base/xml_utils.h diff --git a/Makefile.in b/Makefile.in index 4c13737..eb6ecde 100644 --- a/Makefile.in +++ b/Makefile.in @@ -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 \ diff --git a/base/xml_utils.cc b/base/xml_utils.cc new file mode 100644 index 0000000..cc60f2d --- /dev/null +++ b/base/xml_utils.cc @@ -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++; + } +} + + +//---------------------------------------------------------------- diff --git a/base/xml_utils.h b/base/xml_utils.h new file mode 100644 index 0000000..279f43f --- /dev/null +++ b/base/xml_utils.h @@ -0,0 +1,42 @@ +#ifndef BASE_XML_UTILS_H +#define BASE_XML_UTILS_H + +#include +#include +#include + +using namespace std; + +//---------------------------------------------------------------- + +namespace xml_utils { + typedef std::map attributes; + + void build_attributes(attributes &a, char const **attr); + + template + 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(it->second); + } + + template + boost::optional get_opt_attr(attributes const &attr, string const &key) { + typedef boost::optional rtype; + attributes::const_iterator it = attr.find(key); + if (it == attr.end()) + return rtype(); + + return rtype(boost::lexical_cast(it->second)); + } +} + +//---------------------------------------------------------------- + +#endif diff --git a/caching/xml_format.cc b/caching/xml_format.cc index 84d6fc2..4eb1f14 100644 --- a/caching/xml_format.cc +++ b/caching/xml_format.cc @@ -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 #include @@ -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 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 - 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(it->second); - } - - template - boost::optional get_opt_attr(attributes const &attr, string const &key) { - typedef boost::optional rtype; - attributes::const_iterator it = attr.find(key); - if (it == attr.end()) - return rtype(); - - return rtype(boost::lexical_cast(it->second)); - } - void parse_superblock(emitter *e, attributes const &attr) { e->begin_superblock(get_attr(attr, "uuid"), get_attr(attr, "block_size"), diff --git a/thin-provisioning/xml_format.cc b/thin-provisioning/xml_format.cc index f868e2c..488fac5 100644 --- a/thin-provisioning/xml_format.cc +++ b/thin-provisioning/xml_format.cc @@ -17,7 +17,9 @@ // . #include "xml_format.h" + #include "base/indented_stream.h" +#include "base/xml_utils.h" #include #include @@ -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 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 - 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(it->second); - } - - template - boost::optional get_opt_attr(attributes const &attr, string const &key) { - typedef boost::optional rtype; - attributes::const_iterator it = attr.find(key); - if (it == attr.end()) - return rtype(); - - return rtype(boost::lexical_cast(it->second)); - } - void parse_superblock(emitter *e, attributes const &attr) { e->begin_superblock(get_attr(attr, "uuid"), get_attr(attr, "time"),