Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
folium-app
GitHub Repository: folium-app/Folium
Path: blob/a-new-beginning/SharedDependencies/Sources/inih/INIReader.cpp
2 views
1
// Read an INI file into easy-to-access name/value pairs.
2
3
// SPDX-License-Identifier: BSD-3-Clause
4
5
// Copyright (C) 2009-2025, Ben Hoyt
6
7
// inih and INIReader are released under the New BSD license (see LICENSE.txt).
8
// Go to the project home page for more info:
9
//
10
// https://github.com/benhoyt/inih
11
12
#include <algorithm>
13
#include <cctype>
14
#include <cstdlib>
15
#include "ini.h"
16
#include "INIReader.h"
17
18
using std::string;
19
20
INIReader::INIReader(const string& filename)
21
{
22
_error = ini_parse(filename.c_str(), ValueHandler, this);
23
}
24
25
INIReader::INIReader(const char *buffer, size_t buffer_size)
26
{
27
_error = ini_parse_string_length(buffer, buffer_size, ValueHandler, this);
28
}
29
30
int INIReader::ParseError() const
31
{
32
return _error;
33
}
34
35
string INIReader::ParseErrorMessage() const
36
{
37
// If _error is positive it means it is the line number on which a parse
38
// error occurred. This could be an overlong line, that ValueHandler
39
// indicated a user defined error, an unterminated section name, or a name
40
// without a value.
41
if (_error > 0) {
42
return "parse error on line " + std::to_string(_error) + "; missing ']' or '='?";
43
}
44
45
// If _error is negative it is a system type error, and 0 means success.
46
switch (_error) {
47
case -2:
48
return "unable to allocate memory";
49
50
case -1:
51
return "unable to open file";
52
53
case 0:
54
return "";
55
}
56
57
// This should never be reached. It probably means a new error code was
58
// added to the C API without updating this method.
59
return "unknown error " + std::to_string(_error);
60
}
61
62
string INIReader::Get(const string& section, const string& name, const string& default_value) const
63
{
64
string key = MakeKey(section, name);
65
// Use _values.find() here instead of _values.at() to support pre C++11 compilers
66
return _values.count(key) ? _values.find(key)->second : default_value;
67
}
68
69
string INIReader::GetString(const string& section, const string& name, const string& default_value) const
70
{
71
const string str = Get(section, name, "");
72
return str.empty() ? default_value : str;
73
}
74
75
long INIReader::GetInteger(const string& section, const string& name, long default_value) const
76
{
77
string valstr = Get(section, name, "");
78
const char* value = valstr.c_str();
79
char* end;
80
// This parses "1234" (decimal) and also "0x4D2" (hex)
81
long n = strtol(value, &end, 0);
82
return end > value ? n : default_value;
83
}
84
85
INI_API int64_t INIReader::GetInteger64(const string& section, const string& name, int64_t default_value) const
86
{
87
string valstr = Get(section, name, "");
88
const char* value = valstr.c_str();
89
char* end;
90
// This parses "1234" (decimal) and also "0x4D2" (hex)
91
int64_t n = strtoll(value, &end, 0);
92
return end > value ? n : default_value;
93
}
94
95
unsigned long INIReader::GetUnsigned(const string& section, const string& name, unsigned long default_value) const
96
{
97
string valstr = Get(section, name, "");
98
const char* value = valstr.c_str();
99
char* end;
100
// This parses "1234" (decimal) and also "0x4D2" (hex)
101
unsigned long n = strtoul(value, &end, 0);
102
return end > value ? n : default_value;
103
}
104
105
INI_API uint64_t INIReader::GetUnsigned64(const string& section, const string& name, uint64_t default_value) const
106
{
107
string valstr = Get(section, name, "");
108
const char* value = valstr.c_str();
109
char* end;
110
// This parses "1234" (decimal) and also "0x4D2" (hex)
111
uint64_t n = strtoull(value, &end, 0);
112
return end > value ? n : default_value;
113
}
114
115
double INIReader::GetReal(const string& section, const string& name, double default_value) const
116
{
117
string valstr = Get(section, name, "");
118
const char* value = valstr.c_str();
119
char* end;
120
double n = strtod(value, &end);
121
return end > value ? n : default_value;
122
}
123
124
bool INIReader::GetBoolean(const string& section, const string& name, bool default_value) const
125
{
126
string valstr = Get(section, name, "");
127
// Convert to lower case to make string comparisons case-insensitive
128
std::transform(valstr.begin(), valstr.end(), valstr.begin(),
129
[](const unsigned char& ch) { return static_cast<unsigned char>(::tolower(ch)); });
130
if (valstr == "true" || valstr == "yes" || valstr == "on" || valstr == "1")
131
return true;
132
else if (valstr == "false" || valstr == "no" || valstr == "off" || valstr == "0")
133
return false;
134
else
135
return default_value;
136
}
137
138
std::vector<string> INIReader::Sections() const
139
{
140
std::set<string> sectionSet;
141
for (std::map<string, string>::const_iterator it = _values.begin(); it != _values.end(); ++it) {
142
size_t pos = it->first.find('=');
143
if (pos != string::npos) {
144
sectionSet.insert(it->first.substr(0, pos));
145
}
146
}
147
return std::vector<string>(sectionSet.begin(), sectionSet.end());
148
}
149
150
std::vector<string> INIReader::Keys(const string& section) const
151
{
152
std::vector<string> keys;
153
string keyPrefix = MakeKey(section, "");
154
for (std::map<string, string>::const_iterator it = _values.begin(); it != _values.end(); ++it) {
155
if (it->first.compare(0, keyPrefix.length(), keyPrefix) == 0) {
156
keys.push_back(it->first.substr(keyPrefix.length()));
157
}
158
}
159
return keys;
160
}
161
162
bool INIReader::HasSection(const string& section) const
163
{
164
const string key = MakeKey(section, "");
165
std::map<string, string>::const_iterator pos = _values.lower_bound(key);
166
if (pos == _values.end())
167
return false;
168
// Does the key at the lower_bound pos start with "section"?
169
return pos->first.compare(0, key.length(), key) == 0;
170
}
171
172
bool INIReader::HasValue(const string& section, const string& name) const
173
{
174
string key = MakeKey(section, name);
175
return _values.count(key);
176
}
177
178
string INIReader::MakeKey(const string& section, const string& name)
179
{
180
string key = section + "=" + name;
181
// Convert to lower case to make section/name lookups case-insensitive
182
std::transform(key.begin(), key.end(), key.begin(),
183
[](const unsigned char& ch) { return static_cast<unsigned char>(::tolower(ch)); });
184
return key;
185
}
186
187
int INIReader::ValueHandler(void* user, const char* section, const char* name,
188
const char* value)
189
{
190
if (!name) // Happens when INI_CALL_HANDLER_ON_NEW_SECTION enabled
191
return 1;
192
INIReader* reader = static_cast<INIReader*>(user);
193
string key = MakeKey(section, name);
194
if (reader->_values[key].size() > 0)
195
reader->_values[key] += "\n";
196
reader->_values[key] += value ? value : "";
197
return 1;
198
}
199
200