fisx: fisx_simpleini.h Source File

fisx

fisx_simpleini.h
1 #/*##########################################################################
2 #
3 # The fisx library for X-Ray Fluorescence
4 #
5 # Copyright (c) 2014-2016 European Synchrotron Radiation Facility
6 #
7 # This file is part of the fisx X-ray developed by V.A. Sole
8 #
9 # Permission is hereby granted, free of charge, to any person obtaining a copy
10 # of this software and associated documentation files (the "Software"), to deal
11 # in the Software without restriction, including without limitation the rights
12 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 # copies of the Software, and to permit persons to whom the Software is
14 # furnished to do so, subject to the following conditions:
15 #
16 # The above copyright notice and this permission notice shall be included in
17 # all copies or substantial portions of the Software.
18 #
19 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 # THE SOFTWARE.
26 #
27 #############################################################################*/
28 #ifndef FISX_SIMPLE_INI_H
29 #define FISX_SIMPLE_INI_H
30 #include <string>
31 #include <vector>
32 #include <map>
33 #include <sstream>
34 #include <locale> // std::locale, std::tolower, std::toupper
35
36 namespace fisx
37 {
38
39 class SimpleIni
40 {
41 public:
42 SimpleIni();
43 SimpleIni(std::string fileName);
44 void readFileName(std::string fileName);
48 const std::vector<std:: string> & getSections();
49
53 void getSubsections(const std::string & parent, \
54 std::vector<std::string> & destination, \
55 const bool & caseSensitive = true);
56
63 const std::map<std::string, std::string> & readSection(const std::string & section,
64 const bool & caseSensitive = true);
65
69 static void toUpper(std::string & s, const std::locale & loc = std::locale())
70 {
71 std::string::size_type i;
72 for (i = 0; i < s.size(); i++)
73 {
74 s[i] = std::toupper(s[i], loc);
75 }
76 }
77
81 static void toLower(std::string & s, const std::locale & loc = std::locale())
82 {
83 std::string::size_type i;
84 for (i = 0; i < s.size(); i++)
85 {
86 s[i] = std::tolower(s[i], loc);
87 }
88 }
89
93 template<typename T>
94 static void parseStringAsSingleValue(const std::string & keyContent,\
95 T & destination,
96 const T & defaultValue)
97 {
98 std::stringstream stream(keyContent);
99 stream >> destination;
100 if (stream.fail())
101 {
102 destination = defaultValue;
103 }
104 };
105
106 template<typename T>
107 static void parseStringAsMultipleValues(const std::string & keyContent,
108 std::vector<T> & destination,
109 const T & defaultValue,
110 const char & separator = ',')
111 {
112 std::stringstream ss(keyContent);
113 T result;
114 std::string item;
115 destination.clear();
116 while (std::getline(ss, item, separator))
117 {
118 if (SimpleIni::stringConverter(item, result))
119 destination.push_back(result);
120 else
121 destination.push_back(defaultValue);
122 }
123 };
124
125 template<typename T>
126 static bool stringConverter(const std::string& str, T & number)
127 {
128 std::istringstream i(str);
129 if (!(i >> number))
130 {
131 // Number conversion failed
132 return false;
133 }
134 return true;
135 };
136
140 static bool startsWith(const std::string& stringToCheck, const std::string& testString) {
141 return testString.length() <= stringToCheck.length()
142 && std::equal(testString.begin(), testString.end(), stringToCheck.begin());
143 }
144
145 private:
146 std::string fileName;
147 std::map<std::string, std::map<std::string, std::string> > sectionContents;
148 std::vector<std::string> sections;
149 std::map<std::string, long> sectionPositions;
150 std::map<std::string, std::string> defaultContent;
151 };
152
153 } // namespace fisx
154
155 #endif // FISX_SIMPLE_INI_H
fisx::SimpleIni::toUpper
static void toUpper(std::string &s, const std::locale &loc=std::locale())
Definition: fisx_simpleini.h:69
fisx
Definition: fisx_element.cpp:34
fisx::SimpleIni::getSections
const std::vector< std::string > & getSections()
Definition: fisx_simpleini.cpp:233
fisx::SimpleIni::getSubsections
void getSubsections(const std::string &parent, std::vector< std::string > &destination, const bool &caseSensitive=true)
Definition: fisx_simpleini.cpp:238
fisx::SimpleIni::startsWith
static bool startsWith(const std::string &stringToCheck, const std::string &testString)
Definition: fisx_simpleini.h:140
fisx::SimpleIni::readSection
const std::map< std::string, std::string > & readSection(const std::string &section, const bool &caseSensitive=true)
Definition: fisx_simpleini.cpp:290
fisx::SimpleIni::parseStringAsSingleValue
static void parseStringAsSingleValue(const std::string &keyContent, T &destination, const T &defaultValue)
Definition: fisx_simpleini.h:94
fisx::SimpleIni
Definition: fisx_simpleini.h:39
fisx::SimpleIni::toLower
static void toLower(std::string &s, const std::locale &loc=std::locale())
Definition: fisx_simpleini.h:81