46 template <
class CharT =
char>
50 std::map<string_type, std::map<string_type, string_type>>
data_;
74 std::fstream fs = filename.
stream();
91 std::ofstream fout(filename.
str(), std::ios::trunc);
93 throw FileError(
"Cannot Open File \"" + filename.
str() +
'"');
95 std::ostringstream oss;
100 "The Stream of \"" + filename.
str() +
"\" is Not Good");
105 for (
const auto &p :
data_) {
106 if (!p.first.empty())
107 os <<
'[' <<
to_narrow(p.first) <<
']' <<
'\n';
108 for (
const auto &kv : p.second)
116 return std::string(s.begin(), s.end());
122 while (std::getline(is, line)) {
125 if (trimmed_line.empty() || trimmed_line[0] == CharT(
';')
126 || trimmed_line[0] == CharT(
'#'))
128 if (trimmed_line.front() == CharT(
'[')
129 && trimmed_line.back() == CharT(
']'))
131 =
trim(trimmed_line.substr(1, trimmed_line.size() - 2));
133 auto pos = trimmed_line.find(CharT(
'='));
134 if (pos != string_type::npos) {
137 data_[current_section][key] = value;
149 for (
const auto &p : config.
data_) {
150 if (!p.first.empty())
151 os << CharT(
'[') << p.first << CharT(
']') << CharT(
'\n');
152 for (
const auto &kv : p.second)
153 os << kv.first <<
" = " << kv.second << CharT(
'\n');
162 friend std::basic_istream<CharT> &
166 while (std::getline(is, line)) {
168 if (trimmed_line.empty() || trimmed_line[0] == CharT(
';')
169 || trimmed_line[0] == CharT(
'#'))
171 if (trimmed_line.front() == CharT(
'[')
172 && trimmed_line.back() == CharT(
']'))
174 =
trim(trimmed_line.substr(1, trimmed_line.size() - 2));
176 auto pos = trimmed_line.find(CharT(
'='));
177 if (pos != string_type::npos) {
180 config.
data_[current_section][key] = value;
198 operator bool()
const {
200 for (
auto &c :
lower) c = std::tolower(c, std::locale{});
228 std::basic_istringstream<CharT> iss(
str_);
233 +
"\" to Target Type");
246 if (pr.middle.empty())
247 throw IndexError(
"Invalid Section and Key Format: \""
249 auto section = pr.left;
251 auto sec_it =
data_.find(section);
252 if (sec_it ==
data_.end())
254 "Section \"" +
to_narrow(section) +
"\" Not Found");
255 auto key_it = sec_it->second.find(key);
256 if (key_it == sec_it->second.end())
258 +
"\" Not Found in Section \""
260 return Item(key_it->second);
265 return static_cast<T>(
get(section_and_key));
279 if (pr.middle.empty())
return default_value;
280 auto section = pr.left;
282 auto sec_it =
data_.find(section);
283 if (sec_it ==
data_.end())
return default_value;
284 auto key_it = sec_it->second.find(key);
285 if (key_it == sec_it->second.end())
return default_value;
286 return T(
Item(key_it->second));
298 if (pr.middle.empty())
299 throw IndexError(
"Invalid Section and Key Format: \""
301 auto section = pr.left;
303 data_[section][key] = value;
313 if (pr.middle.empty()) {
314 auto section = pr.left;
317 auto section = pr.left;
319 auto sec_it =
data_.find(section);
320 if (sec_it ==
data_.end())
return false;
321 return sec_it->second.find(key) != sec_it->second.end();
332 if (pr.middle.empty()) {
333 auto section = pr.left;
334 return data_.erase(section) > 0;
336 auto section = pr.left;
338 auto sec_it =
data_.find(section);
339 if (sec_it ==
data_.end())
return false;
340 return sec_it->second.erase(key) > 0;
347 const std::map<string_type, std::map<string_type, string_type>> &
Item(const string_type &s)
Definition config.h:194
string_type str_
Definition config.h:191
INI 配置文件操作类。
Definition config.h:47
friend std::basic_ostream< CharT > & operator<<(std::basic_ostream< CharT > &os, const BasicINIConfig &config)
配置对象的输出流运算符,按 INI 格式输出配置内容。
Definition config.h:147
void set(const string_type §ion_and_key, const string_type &value)
设置配置项的值。
Definition config.h:296
bool has(const string_type §ion_and_key) const
检查配置项或节是否存在。
Definition config.h:311
std::map< string_type, std::map< string_type, string_type > > data_
Definition config.h:50
void save(const Path &filename) const
将配置保存到文件。
Definition config.h:90
T get(const string_type §ion_and_key, const T &default_value) const
获取配置项的值,若不存在则返回默认值。
Definition config.h:277
void read_from_stream(std::istream &is)
Definition config.h:119
void load(const Path &filename)
Definition config.h:73
bool remove(const string_type §ion_and_key)
删除配置项或节。
Definition config.h:330
static std::string to_narrow(const string_type &s)
Definition config.h:115
T get(const string_type §ion_and_key) const
Definition config.h:264
const std::map< string_type, std::map< string_type, string_type > > & data() const
获取原始数据结构的只读引用。
Definition config.h:348
BasicINIConfig(const Path &filename)
从指定文件加载配置的构造函数。
Definition config.h:66
std::basic_string< CharT > string_type
Definition config.h:48
void write_to_stream(std::ostream &os) const
Definition config.h:104
BasicINIConfig(const std::string &filename)
从指定文件加载配置的构造函数。
Definition config.h:60
BasicINIConfig()=default
默认构造函数,创建空的配置对象。
void save(const std::string &filename)
将配置保存到文件。
Definition config.h:83
friend std::basic_istream< CharT > & operator>>(std::basic_istream< CharT > &is, BasicINIConfig &config)
配置对象的输入流运算符,按 INI 格式输入配置内容。
Definition config.h:163
Item get(const string_type §ion_and_key) const
获取配置项的值。
Definition config.h:244
std::fstream stream(std::ios_base::openmode mode=std::ios_base::in|std::ios_base::out) const
以指定模式打开文件流。
Definition file.h:350
const string_type & str() const
获取文件路径字符串。
Definition file.h:343
FileError(const std::string &msg)
构造 FileError。
Definition csexc.h:87
IndexError(const std::string &msg)
构造 IndexError。
Definition csexc.h:153
Item()
默认构造一个空 Item(ptr 为 nullptr)。
Definition box.h:126
TypeError(const std::string &msg)
构造 TypeError。
Definition csexc.h:102
std::basic_string< CharT, Traits, Allocator > trim(std::basic_string< CharT, Traits, Allocator > str)
移除字符串两侧的空白字符。
Definition strpp.h:102
BasicPartitionResult< CharT, Traits, Allocator > partition(const std::basic_string< CharT, Traits, Allocator > &text, const std::basic_string< CharT, Traits, Allocator > &sep)
在字符串中查找第一个分隔符,并返回分隔符之前、分隔符本身、分隔符之后的三部分。
Definition strpp.h:301
std::basic_string< CharT, Traits, Allocator > lower(std::basic_string< CharT, Traits, Allocator > str, const std::locale &loc=std::locale{})
将字符串转换为小写。
Definition strpp.h:222
提供类似 std::optional 的可选值容器 Maybe,支持空状态(nothing)。
BasicINIConfig< wchar_t > WINIConfig
BasicINIConfig<wchar_t> 的类型别名。
Definition config.h:357
BasicPath< char > Path
窄字符路径类型。
Definition file.h:357
BasicINIConfig< char > INIConfig
BasicINIConfig<char> 的类型别名。
Definition config.h:354