Hugin trunk 0.1
Loading...
Searching...
No Matches
IniParser.cpp
Go to the documentation of this file.
1// -*- c-basic-offset: 4 -*-
2
12 /* This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public
14 * License as published by the Free Software Foundation; either
15 * version 2 of the License, or (at your option) any later version.
16 *
17 * This software is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public
23 * License along with this software. If not, see
24 * <http://www.gnu.org/licenses/>.
25 *
26 */
27
28#include "IniParser.h"
29#include <iostream>
30#include <fstream>
31#include "hugin_utils/utils.h"
33
34int IniParser::Read(const std::string& file)
35{
36 //open file
37 std::ifstream inifile;
38 inifile.open(file);
39
40 if (!inifile.is_open())
41 {
42 return 1;
43 };
44 std::string line;
45 std::string currentSection;
46 // go through all lines
47 while (std::getline(inifile, line))
48 {
49 // remove trailing and leading white space and tabs
50 line = hugin_utils::StrTrim(line);
51 if (line.empty())
52 {
53 // empty line, skip
54 continue;
55 }
56 // skip comment lines, beginning with ; or #
57 if (line[0] == ';' || line[0] == '#')
58 {
59 continue;
60 };
61 // new section ?
62 if (line.front() == '[' && line.back() == ']')
63 {
64 currentSection = line.substr(1, line.length() - 2);
65 continue;
66 };
67 // key=value pair ?
68 size_t pos = line.find('=');
69 // found = and = is not the first character of the line
70 if (pos != std::string::npos && pos > 1)
71 {
72 // extract key and value string
73 const std::string key = line.substr(0, pos);
74 const std::string value = line.substr(pos + 1);
75 if (!currentSection.empty())
76 {
77 // store for later, updates earlier read values
79 };
80 };
81 };
82 // close file
83 inifile.close();
84 return 0;
85}
86
87bool IniParser::HasKey(const std::string& section, const std::string& key) const
88{
89 const auto& iniSection = m_iniValues.find(section);
90 if (iniSection != m_iniValues.end())
91 {
92 if (iniSection->second.find(key) != iniSection->second.end())
93 {
94 return true;
95 };
96 }
97 return false;
98}
99
100std::string IniParser::GetKey(const std::string& section, const std::string& key, const std::string& defaultValue) const
101{
102 const auto& iniSection = m_iniValues.find(section);
103 if (iniSection != m_iniValues.end())
104 {
105 if (iniSection->second.find(key) != iniSection->second.end())
106 {
107 return iniSection->second.at(key);
108 };
109 }
110 return defaultValue;
111}
112
113int IniParser::GetKey(const std::string& section, const std::string& key, const int defaultValue) const
114{
115 const auto& iniSection = m_iniValues.find(section);
116 if (iniSection != m_iniValues.end())
117 {
118 if (iniSection->second.find(key) != iniSection->second.end())
119 {
120 int value;
121 if (hugin_utils::stringToInt(iniSection->second.at(key), value))
122 {
123 return value;
124 };
125 };
126 }
127 // section/key not found or could not parse number as integer
128 return defaultValue;
129}
130
131bool IniParser::GetKey(const std::string& section, const std::string& key, const bool defaultValue) const
132{
133 const auto& iniSection = m_iniValues.find(section);
134 if (iniSection != m_iniValues.end())
135 {
136 if (iniSection->second.find(key) != iniSection->second.end())
137 {
138 const std::string text = hugin_utils::toupper(iniSection->second.at(key));
139 if (text == "TRUE" || text == "1")
140 {
141 return true;
142 }
143 else
144 {
145 if (text == "FALSE" || text == "0")
146 {
147 return false;
148 };
149 };
150 };
151 };
152 // section/key not found or could not parse as bool
153 return defaultValue;
154}
155
156// return vector of all known sections
157std::vector<std::string> IniParser::GetSections() const
158{
159 std::vector<std::string> sections;
160 for (const auto& section : m_iniValues)
161 {
162 sections.push_back(section.first);
163 }
164 return sections;
165}
166
167// return vector of all keys in given section
168std::vector<std::string> IniParser::GetKeys(const std::string& section) const
169{
170 std::vector<std::string> keys;
171 const auto iniSection = m_iniValues.find(section);
172 if (iniSection != m_iniValues.end())
173 {
174 for (const auto& key : iniSection->second)
175 {
176 keys.push_back(key.first);
177 };
178 };
179 return keys;
180}
181
183{
184 for (const auto& section : m_iniValues)
185 {
186 for (const auto& key : section.second)
187 {
188 std::cout << section.first << "/" << key.first << "=" << key.second << std::endl;
189 };
190 };
191}
reads and parse an ini file
std::vector< std::string > GetKeys(const std::string &section) const
returns a vector of all know keys in given section
std::vector< std::string > GetSections() const
returns a vector of all know sections
std::map< std::string, IniValues > m_iniValues
map to store the information from different sections
Definition IniParser.h:63
int Read(const std::string &file)
Reads the given ini file.
Definition IniParser.cpp:34
bool HasKey(const std::string &section, const std::string &key) const
Checks if given section/key exists.
Definition IniParser.cpp:87
void PrintValues() const
for debugging purpose, print all values
std::string GetKey(const std::string &section, const std::string &key, const std::string &defaultValue) const
returns the value of the given section/key or default value if it does not exists
std::string toupper(const std::string &s)
Definition stl_utils.h:59
std::string StrTrim(const std::string &str)
remove trailing and leading white spaces and tabs
Definition utils.cpp:208
bool stringToInt(const std::string &s, int &val)
convert string to integer value, returns true, if sucessful
Definition utils.cpp:264
std::vector< deghosting::BImagePtr > threshold(const std::vector< deghosting::FImagePtr > &inputImages, const double threshold, const uint16_t flags)
Threshold function used for creating alpha masks for images.
Definition threshold.h:41