Loading...
Searching...
No Matches
configuration.cpp
1/*---------------------------------------------------------------------------*\
2*
3* bitpit
4*
5* Copyright (C) 2015-2021 OPTIMAD engineering Srl
6*
7* -------------------------------------------------------------------------
8* License
9* This file is part of bitpit.
10*
11* bitpit is free software: you can redistribute it and/or modify it
12* under the terms of the GNU Lesser General Public License v3 (LGPL)
13* as published by the Free Software Foundation.
14*
15* bitpit is distributed in the hope that it will be useful, but WITHOUT
16* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
17* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
18* License for more details.
19*
20* You should have received a copy of the GNU Lesser General Public License
21* along with bitpit. If not, see <http://www.gnu.org/licenses/>.
22*
23\*---------------------------------------------------------------------------*/
24
25
26#include "configuration.hpp"
27#include "configuration_tree.hpp"
28
29#include <stringUtils.hpp>
30
31#include <sstream>
32
33namespace bitpit {
34
43/*
44 Undefined version.
45*/
46const int ConfigParser::VERSION_UNDEFINED = -1;
47
48
54ConfigParser::ConfigParser(const std::string &rootName)
55 : Config(false)
56{
57 reset(rootName);
58}
59
67ConfigParser::ConfigParser(const std::string &rootName, bool multiSections)
68 : Config(multiSections)
69{
70 reset(rootName);
71}
72
79ConfigParser::ConfigParser(const std::string &rootName, int version)
80 : Config(false)
81{
82 reset(rootName, version);
83}
84
93ConfigParser::ConfigParser(const std::string &rootName, int version, bool multiSections)
94 : Config(multiSections)
95{
96 reset(rootName, version);
97}
98
106void ConfigParser::reset(const std::string &rootName)
107{
108 m_rootName = rootName;
109 m_checkVersion = false;
110 m_version = VERSION_UNDEFINED;
111
112 clear();
113}
114
122void ConfigParser::reset(const std::string &rootName, int version)
123{
124 m_rootName = rootName;
125 m_checkVersion = true;
126 m_version = version;
127
128 clear();
129}
130
140void ConfigParser::reset(const std::string &rootName, int version, bool multiSections)
141{
142 m_rootName = rootName;
143 m_checkVersion = true;
144 m_version = version;
145
146 m_multiSections = multiSections;
147
148 clear();
149}
150
161void ConfigParser::read(const std::string &filename, bool append)
162{
163 // Processing not-append requests
164 if (!append) {
165 clear();
166 }
167
168 // Read the configuration
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);
173 extension = utils::string::trim(extension);
174 }
175
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;
181 } else {
182 throw std::runtime_error("ConfigParser::read - Unsupported file format");
183 }
184
185 bool checkFileVersion = false;
186 if (fileFormat == config::SOURCE_FORMAT_XML) {
187 checkFileVersion = m_checkVersion;
188 }
189
190 config::tree::readConfiguration(filename, fileFormat, m_rootName, checkFileVersion, m_version, this);
191}
192
204void ConfigParser::read(config::SourceFormat format, const std::string &content, bool append)
205{
206 // Processing not-append requests
207 if (!append) {
208 clear();
209 }
210
211 // Read the configuration
212 bool checkFileVersion = false;
213 if (format == config::SOURCE_FORMAT_XML) {
214 checkFileVersion = m_checkVersion;
215 }
216
217 std::stringstream contentStream(content, std::ios_base::in | std::ios_base::out | std::ios_base::binary);
218
219 config::tree::readConfiguration(contentStream, format, m_rootName, checkFileVersion, m_version, this);
220}
221
229void ConfigParser::write(const std::string &filename) const
230{
231 // Write the configuration
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);
236 extension = utils::string::trim(extension);
237 }
238
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;
244 } else {
245 throw std::runtime_error("ConfigParser::read - Unsupported file format");
246 }
247
248 config::tree::writeConfiguration(filename, fileFormat, m_rootName, m_version, this);
249}
250
259void ConfigParser::write(config::SourceFormat format, std::string *content) const
260{
261 std::stringstream contentStream;
262 config::tree::writeConfiguration(contentStream, format, m_rootName, m_version, this);
263
264 *content = contentStream.str();
265}
266
275/*
276 Initialize the global instance of the configuration file parser.
277*/
278std::unique_ptr<GlobalConfigParser> GlobalConfigParser::m_parser = nullptr;
279
280/*
281 Initialize the default name of the root element.
282*/
283const std::string GlobalConfigParser::DEFAULT_ROOT_NAME = "bitpit";
284
285/*
286 Initialize the default version of the configuration file.
287*/
288const int GlobalConfigParser::DEFAULT_VERSION = 1;
289
293GlobalConfigParser::GlobalConfigParser()
294 : ConfigParser(DEFAULT_ROOT_NAME, DEFAULT_VERSION)
295{
296}
297
304GlobalConfigParser::GlobalConfigParser(const std::string &rootName, int version)
305 : ConfigParser(rootName, version)
306{
307}
308
316GlobalConfigParser::GlobalConfigParser(const std::string &rootName, bool multiSections)
317 : ConfigParser(rootName, DEFAULT_VERSION, multiSections)
318{
319}
320
329GlobalConfigParser::GlobalConfigParser(const std::string &rootName, int version, bool multiSections)
330 : ConfigParser(rootName, version, multiSections)
331{
332}
333
339{
340 if (!m_parser) {
341 m_parser = std::unique_ptr<GlobalConfigParser>(new GlobalConfigParser());
342 }
343
344 return *m_parser;
345}
346
347// Routines for interacting with the global configuration file parser
348namespace config {
349
354
361 void reset(const std::string &rootName)
362 {
363 root.reset(rootName);
364 }
365
374 void reset(const std::string &rootName, int version)
375 {
376 root.reset(rootName, version);
377 }
378
386 void reset(const std::string &rootName, int version, bool multiSections)
387 {
388 root.reset(rootName, version, multiSections);
389 }
390
401 void read(const std::string &filename, bool append)
402 {
403 root.read(filename, append);
404 }
405
418 void read(config::SourceFormat format, const std::string &content, bool append)
419 {
420 root.read(format, content, append);
421 }
422
430 void write(const std::string &filename)
431 {
432 root.write(filename);
433 }
434
443 void write(config::SourceFormat format, std::string *content)
444 {
445 root.write(format, content);
446 }
447
448}
449
450}
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)
Configuration storage.
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)
--- layout: doxygen_footer ---