1.概述
配置文件的读取是每个程序必备的功能,配置文件的格式多种多样,例如:ini格式、json格式、xml格式等。其中属ini格式最为简单,且应用广泛。
2.ini格式语法
- 注释内容采用“#”或者“;”开头。
- 配置是由一系列的section组成,每个section就是一个关联的配置块,section使用[]包含起来。
- 每个section下配置的是具体的配置项,每个配置项是使用“=”分隔的key-value对。
下面让我们来看一个简单的示例,假设我们有一个配置文件demo.cfg,它的内容如下所示。
[server]
ip = 127.0.0.1
port = 8088
上面的配置内容中,有一个server的配置节,在这个配置节里有两个配置项,它们分别是ip和port,ip的值为127.0.0.1,port的值为8088。
3.配置读取
知道了ini格式语法之后,就可以根据语法规则来读取配置文件内容了,春哥这里实现了一个非常精简易用的版本,源代码文件config.hpp的内容如下。
- #pragma once
-
- #include <fstream>
- #include <functional>
- #include <string>
- #include <unordered_map>
-
- namespace Config {
- class Ini {
- public:
- void Dump(std::function<void(const std::string&, const std::string&, const std::string&)> deal) {
- auto iter = cfg_.begin();
- while (iter != cfg_.end()) {
- auto kv_iter = iter->second.begin();
- while (kv_iter != iter->second.end()) {
- deal(iter->first, kv_iter->first, kv_iter->second);
- ++kv_iter;
- }
- ++iter;
- }
- }
- bool Load(std::string file_name) {
- if (file_name == "") return false;
- std::ifstream in;
- std::string line;
- in.open(file_name.c_str());
- if (not in.is_open()) return false;
- while (getline(in, line)) {
- std::string section, key, value;
- if (not parseLine(line, section, key, value)) {
- continue;
- }
- setSectionKeyValue(section, key, value);
- }
- return true;
- }
- void GetStrValue(const std::string& section, const std::string& key, std::string& value, std::string default_value) {
- value = default_value;
- if (cfg_.find(section) == cfg_.end()) {
- return;
- }
- if (cfg_[section].find(key) == cfg_[section].end()) {
- return;
- }
- value = cfg_[section][key];
- }
- void GetIntValue(const std::string& section, const std::string& key, int64_t& value, int64_t default_value) {
- value = default_value;
- if (cfg_.find(section) == cfg_.end()) {
- return;
- }
- if (cfg_[section].find(key) == cfg_[section].end()) {
- return;
- }
- value = atol(cfg_[section][key].c_str());
- }
-
- private:
- void ltrim(std::string& str) {
- if (str.empty()) return;
- size_t len = 0;
- char* temp = (char*)str.c_str();
- while (*temp && isblank(*temp)) {
- ++len;
- ++temp;
- }
- if (len > 0) str.erase(0, len);
- }
- void rtrim(std::string& str) {
- if (str.empty()) return;
- size_t len = str.length();
- size_t pos = len;
- while (pos > 0) {
- if (not isblank(str[pos - 1])) {
- break;
- }
- --pos;
- }
- if (pos != len) str.erase(pos);
- }
- void trim(std::string& str) {
- ltrim(str);
- rtrim(str);
- }
- void setSectionKeyValue(std::string& section, std::string& key, std::string& value) {
- if (cfg_.find(section) == cfg_.end()) {
- std::unordered_map<std::string, std::string> kv_map;
- cfg_[section] = kv_map;
- }
- if (key != "" && value != "") cfg_[section][key] = value;
- }
- bool parseLine(std::string& line, std::string& section, std::string& key, std::string& value) {
- static std::string cur_section = "";
- std::string nodes[2] = {"#", ";"}; //去掉注释的内容
- for (int i = 0; i < 2; ++i) {
- std::string::size_type pos = line.find(nodes[i]);
- if (pos != std::string::npos) line.erase(pos);
- }
- trim(line);
- if (line == "") return false;
- if (line[0] == '[' && line[line.size() - 1] == ']') {
- section = line.substr(1, line.size() - 2);
- trim(section);
- cur_section = section;
- return false;
- }
- if (cur_section == "") return false;
- bool is_key = true;
- for (size_t i = 0; i < line.size(); ++i) {
- if (line[i] == '=') {
- is_key = false;
- continue;
- }
- if (is_key) {
- key += line[i];
- } else {
- value += line[i];
- }
- }
- section = cur_section;
- trim(key);
- trim(value);
- return true;
- }
-
- private:
- std::unordered_map<std::string, std::unordered_map<std::string, std::string>> cfg_;
- }; // ini格式配置文件的读取
- } // namespace Config
Config命名空间下实现了Ini配置读取类。Load函数用于加载配置文件内容,GetStrValue函数和GetIntValue函数用于获取配置项值并支持设置默认值,Dump函数用于遍历配置文件的内容。由于在解析过程中需要删除字符串中的前导和后导空白符,因此我们还实现了trim函数用于删除前导和后导空白符。
这里重点讲解一下Load函数的逻辑:每次从配置文件中读取一行,然后先去掉注释的内容,接着再判断剩余的内容是一个section头配置,还是section下的key-value配置,再走不同的解析分支。
4.demo示例
以上面配置文件demo.cfg内容的读取为例,示例代码如下。
- #include <iostream>
-
- #include "config.hpp"
-
- int main(int argc, char *argv[]) {
- Config::Ini ini;
- ini.Load("./demo.cfg");
- ini.Dump([](const std::string §ion, const std::string &key, const std::string value) {
- std::cout << "section[" << section << "],key[" << key << "]->value[" << value << "]" << std::endl;
- });
- return 0;
- }
5.自动生成读取代码
如果这次分享的内容到上面demo示例之后就进入尾声的话,那么春哥就太过于标题党了。假设我们的程序有几十项配置内容,如果每一项采用GetIntValue函数或者GetStrValue函数来读取,那么编码工作量还是不小的,并且也容易出错,那么怎么做到提效呢?
其实提效方案并不难想到,我们可以自动生成读取配置项的代码,并生成具体业务配置读取类。下面我们举一个例子,假设我们有一个配置文件mysvr.cfg,它的内容如下。
[server]
ip = 127.0.0.1
port = 8080
[pool]
conn_pool_size = 100
我们手动编写了业务配置读取类代码文件MySvrCfg.hpp,它的内容如下。
- #include <string>
-
- #include "config.hpp"
-
- class MysvrCfg {
- public:
- bool Load(std::string file_name) {
- Config::Ini ini;
- if (not ini.Load(file_name)) {
- return false;
- }
- ini.GetIntValue("pool", "conn_pool_size", conn_pool_size_, 0);
- ini.GetIntValue("server", "port", port_, 0);
- ini.GetStrValue("server", "ip", ip_, "");
-
- return true;
- }
- int64_t conn_pool_size() { return conn_pool_size_; }
- int64_t port() { return port_; }
- std::string ip() { return ip_; }
-
- public:
- int64_t conn_pool_size_;
- int64_t port_;
- std::string ip_;
- };
我们可以发现上面的代码完全可以自动生成。「我们先读取配置的内容,然后使用配置文件的内容作为元数据驱动生成这个MySvrCfg.hpp的内容」。
自动生成业务配置读取类的脚手架工具代码文件configtool.cpp,它的内容如下。
- #include <iostream>
- #include <regex>
- #include <string>
-
- #include "MysvrCfg.hpp"
-
- using namespace std;
-
- int genCfgReadFile(Config::Ini &ini, string file_name) {
- string prefix = "";
- for (size_t i = 0; i < file_name.size(); i++) {
- if (file_name[i] == '.') break;
- if (prefix == "") {
- prefix = toupper(file_name[i]);
- } else {
- prefix += file_name[i];
- }
- }
- string class_name = prefix + "Cfg";
- string output_file_name = prefix + "Cfg.hpp";
- ofstream out;
- out.open(output_file_name);
- if (not out.is_open()) {
- cout << "open " << output_file_name << " failed." << endl;
- return -1;
- }
- string cfg_read_content;
- string class_func_content;
- string class_member_content;
- ini.Dump([&cfg_read_content, &class_func_content, &class_member_content](const string §ion, const string &key,
- const string &value) {
- regex integer_regex("[+-]?[0-9]+");
- if (regex_match(value, integer_regex)) { // 整数
- cfg_read_content += " ini.GetIntValue("" + section + "", "" + key + "", " + key + "_, 0);\n";
- class_func_content += " int64_t " + key + "() { return " + key + "_; }\n";
- class_member_content += " int64_t " + key + "_;\n";
- } else {
- cfg_read_content += " ini.GetStrValue("" + section + "", "" + key + "", " + key + "_, "");\n";
- class_func_content += " std::string " + key + "() { return " + key + "_; }\n";
- class_member_content += " std::string " + key + "_;\n";
- }
- });
- //
- string content = R"(#include <string>
-
- #include "config.hpp"
-
- class )" + class_name +
- R"( {
- public:
- bool Load(std::string file_name) {
- Config::Ini ini;
- if (not ini.Load(file_name)) {
- return false;
- }
- )" + cfg_read_content +
- R"(
- return true;
- }
- )" + class_func_content +
- R"(
- public:
- )" + class_member_content +
- "};";
- out << content;
- return 0;
- }
-
- int readDemoCfg() {
- MysvrCfg cfg;
- cout << "usage: configtool cfg_file_name" << endl;
- cout << "read demo cfg mysvr.cfg" << endl;
- cfg.Load("./mysvr.cfg");
- cout << "ip = " << cfg.ip() << endl;
- cout << "port = " << cfg.port() << endl;
- cout << "conn_pool_size = " << cfg.conn_pool_size() << endl;
- return 0;
- }
-
- int main(int argc, char *argv[]) {
- if (argc == 1) {
- return readDemoCfg();
- }
- if (argc != 2) {
- cout << "usage: configtool mysvr.cfg" << endl;
- return -1;
- }
- Config::Ini ini;
- string file_name = argv[1];
- if (not ini.Load(file_name)) {
- cout << "load " << file_name << " failed." << endl;
- return -1;
- }
- return genCfgReadFile(ini, file_name);
- }
在configtool脚手架工具中,「我们先使用Config::Ini类对象读取了配置文件的内容,然后遍历配置文件的内容,生成业务配置读取类中动态变化的代码内容,最后使用模版生成最终的代码」。
脚手架工具configtool的使用也非常简单,直接把配置文件名作为命令行参数传入即可,如果执行configtool时不携带任何参数则会使用生成的类MysvrCfg来读取上面的配置文件mysvr.cfg的内容。
到此这篇关于C++实现读写ini配置文件的示例代码的文章就介绍到这了,更多相关C++读写ini配置文件内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!