26#include "configuration.hpp"
27#include "configuration_tree.hpp"
46const int ConfigParser::VERSION_UNDEFINED = -1;
82 reset(rootName, version);
96 reset(rootName, version);
108 m_rootName = rootName;
109 m_checkVersion =
false;
110 m_version = VERSION_UNDEFINED;
124 m_rootName = rootName;
125 m_checkVersion =
true;
142 m_rootName = rootName;
143 m_checkVersion =
true;
146 m_multiSections = multiSections;
169 std::string extension =
"";
170 std::size_t dotPosition = filename.find_last_of(
".");
171 if (dotPosition != std::string::npos) {
172 extension = filename.substr(dotPosition + 1);
176 config::SourceFormat fileFormat;
177 if (extension ==
"xml" || extension ==
"XML") {
178 fileFormat = config::SOURCE_FORMAT_XML;
179 }
else if (extension ==
"json" || extension ==
"JSON") {
180 fileFormat = config::SOURCE_FORMAT_JSON;
182 throw std::runtime_error(
"ConfigParser::read - Unsupported file format");
185 bool checkFileVersion =
false;
186 if (fileFormat == config::SOURCE_FORMAT_XML) {
187 checkFileVersion = m_checkVersion;
190 config::tree::readConfiguration(filename, fileFormat, m_rootName, checkFileVersion, m_version,
this);
212 bool checkFileVersion =
false;
213 if (format == config::SOURCE_FORMAT_XML) {
214 checkFileVersion = m_checkVersion;
217 std::stringstream contentStream(content, std::ios_base::in | std::ios_base::out | std::ios_base::binary);
219 config::tree::readConfiguration(contentStream, format, m_rootName, checkFileVersion, m_version,
this);
232 std::string extension =
"";
233 std::size_t dotPosition = filename.find_last_of(
".");
234 if (dotPosition != std::string::npos) {
235 extension = filename.substr(dotPosition + 1);
239 config::SourceFormat fileFormat;
240 if (extension ==
"xml" || extension ==
"XML") {
241 fileFormat = config::SOURCE_FORMAT_XML;
242 }
else if (extension ==
"json" || extension ==
"JSON") {
243 fileFormat = config::SOURCE_FORMAT_JSON;
245 throw std::runtime_error(
"ConfigParser::read - Unsupported file format");
248 config::tree::writeConfiguration(filename, fileFormat, m_rootName, m_version,
this);
261 std::stringstream contentStream;
262 config::tree::writeConfiguration(contentStream, format, m_rootName, m_version,
this);
264 *content = contentStream.str();
278std::unique_ptr<GlobalConfigParser> GlobalConfigParser::m_parser =
nullptr;
283const std::string GlobalConfigParser::DEFAULT_ROOT_NAME =
"bitpit";
288const int GlobalConfigParser::DEFAULT_VERSION = 1;
293GlobalConfigParser::GlobalConfigParser()
304GlobalConfigParser::GlobalConfigParser(
const std::string &rootName,
int version)
305 : ConfigParser(rootName, version)
316GlobalConfigParser::GlobalConfigParser(
const std::string &rootName,
bool multiSections)
317 : ConfigParser(rootName, DEFAULT_VERSION, multiSections)
329GlobalConfigParser::GlobalConfigParser(
const std::string &rootName,
int version,
bool multiSections)
330 : ConfigParser(rootName, version, multiSections)
361 void reset(
const std::string &rootName)
374 void reset(
const std::string &rootName,
int version)
386 void reset(
const std::string &rootName,
int version,
bool multiSections)
388 root.
reset(rootName, version, multiSections);
401 void read(
const std::string &filename,
bool append)
418 void read(config::SourceFormat format,
const std::string &content,
bool append)
430 void write(
const std::string &filename)
443 void write(config::SourceFormat format, std::string *content)
Configuration file parser.
void write(const std::string &filename) const
ConfigParser(const std::string &rootName)
void read(const std::string &filename, bool append=true)
void reset(const std::string &rootName)
Global configuration file parser.
static GlobalConfigParser & parser()
std::string & trim(std::string &s)
void write(const std::string &filename)
GlobalConfigParser & root
void reset(const std::string &rootName)
void read(const std::string &filename, bool append)