c++ style / delay std::string creation in xml loading

This commit is contained in:
Dane Springmeyer 2014-10-03 16:27:24 -07:00
parent b09df2fa13
commit 2925a70046
4 changed files with 17 additions and 20 deletions

View file

@ -88,7 +88,7 @@ public:
bool is_text() const;
bool is(std::string const& name) const;
xml_node & add_child(std::string && name, unsigned line=0, bool is_text = false);
xml_node & add_child(const char * name, unsigned line=0, bool is_text = false);
void add_attribute(const char * name, const char * value);
attribute_map const& get_attributes() const;

View file

@ -154,7 +154,8 @@ private:
{
for (; attributes; attributes = attributes->next )
{
node.add_attribute((const char *)attributes->name,(const char *)attributes->children->content);
node.add_attribute(reinterpret_cast<const char *>(attributes->name),
reinterpret_cast<const char *>(attributes->children->content));
}
}
@ -166,18 +167,17 @@ private:
{
case XML_ELEMENT_NODE:
{
std::string name((const char *)cur_node->name);
xml_node &new_node = node.add_child(std::move(name), cur_node->line, false);
xml_node &new_node = node.add_child(reinterpret_cast<const char *>(cur_node->name), cur_node->line, false);
append_attributes(cur_node->properties, new_node);
populate_tree(cur_node->children, new_node);
}
break;
case XML_TEXT_NODE:
{
std::string trimmed((const char*)cur_node->content);
std::string trimmed(reinterpret_cast<const char *>(cur_node->content));
mapnik::util::trim(trimmed);
if (trimmed.empty()) break; //Don't add empty text nodes
node.add_child(std::move(trimmed), cur_node->line, true);
node.add_child(trimmed.c_str(), cur_node->line, true);
}
break;
case XML_COMMENT_NODE:

View file

@ -54,7 +54,7 @@ public:
{
}
void load(std::string const& filename, xml_node &node)
void load(std::string const& filename, xml_node & node)
{
if (!mapnik::util::exists(filename))
{
@ -107,19 +107,19 @@ public:
}
}
void load_string(std::string const& buffer, xml_node &node, std::string const & )
void load_string(std::string const& buffer, xml_node & node, std::string const & )
{
// Note: base_path ignored because its not relevant - only needed for xml2 to load entities (see libxml2_loader.cpp)
load_array(std::string(buffer), node);
}
private:
void populate_tree(rapidxml::xml_node<char> *cur_node, xml_node &node)
void populate_tree(rapidxml::xml_node<char> *cur_node, xml_node & node)
{
switch (cur_node->type())
{
case rapidxml::node_element:
{
xml_node &new_node = node.add_child((char *)cur_node->name(), 0, false);
xml_node & new_node = node.add_child(cur_node->name(), 0, false);
// Copy attributes
for (rapidxml::xml_attribute<char> *attr = cur_node->first_attribute();
attr; attr = attr->next_attribute())
@ -154,12 +154,12 @@ private:
std::string filename_;
};
void read_xml(std::string const & filename, xml_node &node)
void read_xml(std::string const& filename, xml_node & node)
{
rapidxml_loader loader;
loader.load(filename, node);
}
void read_xml_string(std::string const & str, xml_node &node, std::string const & base_path)
void read_xml_string(std::string const& str, xml_node & node, std::string const& base_path)
{
rapidxml_loader loader;
loader.load_string(str, node, base_path);

View file

@ -114,22 +114,19 @@ std::string const& xml_tree::filename() const
return file_;
}
xml_node &xml_tree::root()
xml_node & xml_tree::root()
{
return node_;
}
const xml_node &xml_tree::root() const
const xml_node & xml_tree::root() const
{
return node_;
}
xml_attribute::xml_attribute(const char * value_)
: value(value_),
processed(false)
{
}
processed(false) {}
node_not_found::node_not_found(std::string const& node_name)
: node_name_(node_name) {}
@ -221,9 +218,9 @@ bool xml_node::is(std::string const& name) const
return false;
}
xml_node &xml_node::add_child(std::string && name, unsigned line, bool is_text)
xml_node & xml_node::add_child(const char * name, unsigned line, bool is_text)
{
children_.emplace_back(tree_, std::move(name), line, is_text);
children_.emplace_back(tree_, name, line, is_text);
return children_.back();
}