Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IO: miscellaneous fixes for the XML reader/writer #341

Merged
merged 5 commits into from
Sep 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/IO/configuration_JSON.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ void writeConfiguration(const std::string &filename, const Config *rootConfig, b
void writeNode(const Config *config, rapidjson::Value &rootJSONData, rapidjson::Document::AllocatorType &allocator)
{
// Write the options
for (auto &entry : config->getOptions()) {
for (const auto &entry : config->getOptions()) {
const std::string &configKey = entry.first;
const std::string &configValue = entry.second;

Expand Down
24 changes: 20 additions & 4 deletions src/IO/configuration_XML.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,22 @@ void readNode(xmlNodePtr root, Config *config)
*/
void writeNode(xmlTextWriterPtr writer, const Config *config, const std::string &encoding)
{
int status;

// Write the options
for (auto &entry : config->getOptions()) {
for (const auto &entry : config->getOptions()) {
const std::string &key = entry.first;
const std::string &value = entry.second;

xmlChar *elementName = encodeString(key, encoding);
xmlChar *elementText = encodeString(value, encoding);
int status = xmlTextWriterWriteFormatElement(writer, BAD_CAST elementName, "%s", elementText);
if (elementText) {
xmlFree(elementText);
}
if (elementName) {
xmlFree(elementName);
}
if (status < 0) {
throw std::runtime_error("Error at xmlTextWriterWriteFormatElement");
}
Expand All @@ -103,7 +111,10 @@ void writeNode(xmlTextWriterPtr writer, const Config *config, const std::string

// Start the section
xmlChar *elementName = encodeString(key, encoding);
int status = xmlTextWriterStartElement(writer, BAD_CAST elementName);
status = xmlTextWriterStartElement(writer, BAD_CAST elementName);
if (elementName) {
xmlFree(elementName);
}
if (status < 0) {
throw std::runtime_error("Error at xmlTextWriterStartElement");
}
Expand Down Expand Up @@ -194,8 +205,7 @@ void readConfiguration(const std::string &filename, const std::string &rootname,
}

// Check if the version is supported
const xmlChar *versionAttributeName =
reinterpret_cast<const xmlChar *>("version");
const xmlChar *versionAttributeName = reinterpret_cast<const xmlChar *>("version");
if (checkVersion && xmlHasProp(rootElement, versionAttributeName)) {
xmlChar *versionValue = xmlGetProp(rootElement, versionAttributeName);
std::string versionString((char *) versionValue);
Expand Down Expand Up @@ -250,6 +260,9 @@ void writeConfiguration(const std::string &filename, const std::string &rootname
// Start the root element
xmlChar *elementName = encodeString(rootname, DEFAULT_ENCODING);
status = xmlTextWriterStartElement(writer, BAD_CAST elementName);
if (elementName) {
xmlFree(elementName);
}
if (status < 0) {
throw std::runtime_error("Error at xmlTextWriterStartElement");
}
Expand All @@ -260,6 +273,9 @@ void writeConfiguration(const std::string &filename, const std::string &rootname

xmlChar *versionAttr = encodeString(versionStream.str(), DEFAULT_ENCODING);
status = xmlTextWriterWriteAttribute(writer, BAD_CAST "version", BAD_CAST versionAttr);
if (versionAttr) {
xmlFree(versionAttr);
}
if (status < 0) {
throw std::runtime_error("Error at xmlTextWriterWriteAttribute");
}
Expand Down
13 changes: 3 additions & 10 deletions src/IO/configuration_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,6 @@ Config & Config::operator=(Config other)
return *this;
}

/*!
Destructor.
*/
Config::~Config()
{
}

/*!
Swap operator.

Expand Down Expand Up @@ -158,7 +151,7 @@ bool Config::hasOption(const std::string &key) const
\param key is the name of the option
\result The specified option.
*/
std::string Config::get(const std::string &key) const
const std::string & Config::get(const std::string &key) const
{
return m_options->at(key);
}
Expand Down Expand Up @@ -408,15 +401,15 @@ void Config::dump(std::ostream &out, int indentLevel) const
out << std::endl;
out << indent << "Options..." << std::endl;
if (getOptionCount() > 0) {
for (auto &entry : getOptions()) {
for (const auto &entry : getOptions()) {
out << indent << padding << entry.first << " = " << entry.second << std::endl;
}
} else {
out << indent << padding << "No options." << std::endl;
}

++indentLevel;
for (auto &entry : getSections()) {
for (const auto &entry : getSections()) {
out << std::endl;
out << indent << padding << "::: Section " << entry.first << " :::" << std::endl;
entry.second->dump(out, indentLevel);
Expand Down
4 changes: 2 additions & 2 deletions src/IO/configuration_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class Config
Config & operator=(Config other);
Config & operator=(Config &&other) = default;

virtual ~Config();
virtual ~Config() = default;

void swap(Config &other);

Expand All @@ -58,7 +58,7 @@ class Config
Options & getOptions();
const Options & getOptions() const;
bool hasOption(const std::string &key) const;
std::string get(const std::string &key) const;
const std::string & get(const std::string &key) const;
std::string get(const std::string &key, const std::string &fallback) const;
void set(const std::string &key, const std::string &value);
bool removeOption(const std::string &key);
Expand Down