Skip to content

Commit e6bca54

Browse files
committed
added XMLElement::addAttributes()
to assign multiple attributes at once
1 parent 0ba6429 commit e6bca54

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed

src/XMLNode.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include <algorithm>
2222
#include <fstream>
23+
#include <iterator>
2324
#include <list>
2425
#include <map>
2526
#include <sstream>
@@ -136,6 +137,12 @@ void XMLElement::addAttribute (const string &name, double value) {
136137
}
137138

138139

140+
void XMLElement::addAttributes (const map<string, string> &attribs) {
141+
for (auto &attr : attribs)
142+
_attributes.emplace_back(attr);
143+
}
144+
145+
139146
void XMLElement::removeAttribute (const std::string &name) {
140147
_attributes.erase(find_if(_attributes.begin(), _attributes.end(), [&](const Attribute &attr) {
141148
return attr.name == name;

src/XMLNode.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ class XMLElement : public XMLNode {
111111
public:
112112
struct Attribute {
113113
Attribute (std::string nam, std::string val) : name(std::move(nam)), value(std::move(val)) {}
114+
Attribute (std::pair<std::string, std::string> attr) : name(std::move(attr.first)), value(std::move(attr.second)) {}
114115
bool inheritable () const;
115116
std::string name;
116117
std::string value;
@@ -127,6 +128,7 @@ class XMLElement : public XMLNode {
127128
void clear () override;
128129
void addAttribute (const std::string &name, const std::string &value);
129130
void addAttribute (const std::string &name, double value);
131+
void addAttributes (const std::map<std::string, std::string> &attribs);
130132
void removeAttribute (const std::string &name);
131133
XMLNode* append (std::unique_ptr<XMLNode> child);
132134
XMLNode* append (const std::string &str);

src/XMLParser.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,7 @@ XMLElement* XMLParser::openElement (const string &tag) {
185185
string name = ir.getString("/ \t\n\r");
186186
ir.skipSpace();
187187
unique_ptr<XMLElement> elemNode{createElementPtr(name)};
188-
for (const auto &attrpair : ir.parseAttributes(true, "\"'"))
189-
elemNode->addAttribute(attrpair.first, attrpair.second);
188+
elemNode->addAttributes(ir.parseAttributes(true, "\"'"));
190189
ir.skipSpace();
191190
XMLElement *elemPtr = elemNode.get();
192191
if (ir.peek() == '/') // end of empty element tag

tests/XMLNodeTest.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ TEST(XMLNodeTest, prependText) {
200200
}
201201

202202

203-
TEST(XMLNodeTest, attributes) {
203+
TEST(XMLNodeTest, attributes1) {
204204
XMLElement root("root");
205205
root.addAttribute("string", "text");
206206
root.addAttribute("integer", 42);
@@ -209,14 +209,29 @@ TEST(XMLNodeTest, attributes) {
209209
EXPECT_TRUE(root.hasAttribute("string"));
210210
EXPECT_TRUE(root.hasAttribute("integer"));
211211
EXPECT_TRUE(root.hasAttribute("double"));
212-
EXPECT_FALSE(root.hasAttribute("noname "));
212+
EXPECT_FALSE(root.hasAttribute("noname"));
213213
EXPECT_STREQ(root.getAttributeValue("string"), "text");
214214
EXPECT_STREQ(root.getAttributeValue("integer"), "42");
215215
EXPECT_STREQ(root.getAttributeValue("double"), "42.24");
216216
EXPECT_EQ(root.getAttributeValue("none"), nullptr);
217217
}
218218

219219

220+
TEST(XMLNodeTest, attributes2) {
221+
XMLElement root("root");
222+
root.addAttributes({{"attr1", "val1"}, {"attr2", "val2"}, {"attr3", "val3"}});
223+
EXPECT_TRUE(root.empty());
224+
EXPECT_TRUE(root.hasAttribute("attr1"));
225+
EXPECT_TRUE(root.hasAttribute("attr2"));
226+
EXPECT_TRUE(root.hasAttribute("attr3"));
227+
EXPECT_FALSE(root.hasAttribute("noname"));
228+
EXPECT_STREQ(root.getAttributeValue("attr1"), "val1");
229+
EXPECT_STREQ(root.getAttributeValue("attr2"), "val2");
230+
EXPECT_STREQ(root.getAttributeValue("attr3"), "val3");
231+
EXPECT_EQ(root.getAttributeValue("none"), nullptr);
232+
}
233+
234+
220235
TEST(XMLNodeTest, clone) {
221236
XMLElement root ("root");
222237
root.addAttribute("string", "text");

0 commit comments

Comments
 (0)