Skip to content

Commit 0ba6429

Browse files
committed
small refactorings regarding InputReader::parseAttributes()
1 parent b0e1485 commit 0ba6429

File tree

4 files changed

+23
-14
lines changed

4 files changed

+23
-14
lines changed

src/InputReader.cpp

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -371,11 +371,11 @@ string InputReader::getLine () {
371371

372372
/** Parses a sequence of key-value pairs of the form KEY=VALUE or KEY="VALUE".
373373
* If parameter 'requireValues' is false, attributes may also consist of a key only.
374-
* @param[out] attr the scanned attributes
375374
* @param[in] requireValues true if all attributes require a value
376375
* @param[in] quotechars recognized quote characters used to enclose the attribute values
377-
* @return number of attributes scanned */
378-
int InputReader::parseAttributes (map<string,string> &attr, bool requireValues, const char *quotechars) {
376+
* @return the scanned attributes */
377+
map<string,string> InputReader::parseAttributes (bool requireValues, const char *quotechars) {
378+
map<string,string> attribs;
379379
while (!eof()) {
380380
string key;
381381
skipSpace();
@@ -389,12 +389,24 @@ int InputReader::parseAttributes (map<string,string> &attr, bool requireValues,
389389
get();
390390
skipSpace();
391391
string val = getQuotedString(quotechars);
392-
attr.emplace(std::move(key), std::move(val));
392+
attribs.emplace(std::move(key), std::move(val));
393393
}
394394
else if (!requireValues)
395-
attr.emplace(std::move(key), "");
395+
attribs.emplace(std::move(key), "");
396396
}
397-
return int(attr.size());
397+
return attribs;
398+
}
399+
400+
401+
/** Parses a sequence of key-value pairs of the form KEY=VALUE or KEY="VALUE".
402+
* If parameter 'requireValues' is false, attributes may also consist of a key only.
403+
* @param[out] attribs the scanned attributes
404+
* @param[in] requireValues true if all attributes require a value
405+
* @param[in] quotechars recognized quote characters used to enclose the attribute values
406+
* @return number of attributes scanned */
407+
int InputReader::parseAttributes (map<string,string> &attribs, bool requireValues, const char *quotechars) {
408+
attribs = parseAttributes(requireValues, quotechars);
409+
return int(attribs.size());
398410
}
399411

400412
//////////////////////////////////////////
@@ -424,4 +436,4 @@ streamsize BufferInputReader::read (char *buf, streamsize size) {
424436
while (c >= 0 && size-- > 0)
425437
*p++ = char(c);
426438
return p-buf;
427-
}
439+
}

src/InputReader.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class InputReader {
5656
virtual std::string getString (const char *delim);
5757
virtual std::string getLine ();
5858
virtual int parseAttributes (std::map<std::string,std::string> &attr, bool requireValues, const char *quotechars=nullptr);
59+
virtual std::map<std::string,std::string> parseAttributes (bool requireValues, const char *quotechars=nullptr);
5960
virtual operator bool () const {return !eof();}
6061
};
6162

src/PsSpecialHandler.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,7 @@ bool PsSpecialHandler::process (const string &prefix, istream &is, SpecialAction
231231
fileType = FileType::BITMAP;
232232
}
233233
map<string,string> attr;
234-
in.parseAttributes(attr, false);
235-
imgfile(fileType, fname, attr);
234+
imgfile(fileType, fname, in.parseAttributes(false));
236235
}
237236
}
238237
else if (prefix == "ps::") {

src/XMLParser.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,11 +185,8 @@ 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-
map<string, string> attribs;
189-
if (ir.parseAttributes(attribs, true, "\"'")) {
190-
for (const auto &attrpair : attribs)
191-
elemNode->addAttribute(attrpair.first, attrpair.second);
192-
}
188+
for (const auto &attrpair : ir.parseAttributes(true, "\"'"))
189+
elemNode->addAttribute(attrpair.first, attrpair.second);
193190
ir.skipSpace();
194191
XMLElement *elemPtr = elemNode.get();
195192
if (ir.peek() == '/') // end of empty element tag

0 commit comments

Comments
 (0)