经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C++ » 查看文章
C++实现读写ini配置文件的示例代码
来源:jb51  时间:2023/5/8 10:20:27  对本文有异议

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的内容如下。

  1. #pragma once
  2.  
  3. #include <fstream>
  4. #include <functional>
  5. #include <string>
  6. #include <unordered_map>
  7.  
  8. namespace Config {
  9. class Ini {
  10. public:
  11. void Dump(std::function<void(const std::string&, const std::string&, const std::string&)> deal) {
  12. auto iter = cfg_.begin();
  13. while (iter != cfg_.end()) {
  14. auto kv_iter = iter->second.begin();
  15. while (kv_iter != iter->second.end()) {
  16. deal(iter->first, kv_iter->first, kv_iter->second);
  17. ++kv_iter;
  18. }
  19. ++iter;
  20. }
  21. }
  22. bool Load(std::string file_name) {
  23. if (file_name == "") return false;
  24. std::ifstream in;
  25. std::string line;
  26. in.open(file_name.c_str());
  27. if (not in.is_open()) return false;
  28. while (getline(in, line)) {
  29. std::string section, key, value;
  30. if (not parseLine(line, section, key, value)) {
  31. continue;
  32. }
  33. setSectionKeyValue(section, key, value);
  34. }
  35. return true;
  36. }
  37. void GetStrValue(const std::string& section, const std::string& key, std::string& value, std::string default_value) {
  38. value = default_value;
  39. if (cfg_.find(section) == cfg_.end()) {
  40. return;
  41. }
  42. if (cfg_[section].find(key) == cfg_[section].end()) {
  43. return;
  44. }
  45. value = cfg_[section][key];
  46. }
  47. void GetIntValue(const std::string& section, const std::string& key, int64_t& value, int64_t default_value) {
  48. value = default_value;
  49. if (cfg_.find(section) == cfg_.end()) {
  50. return;
  51. }
  52. if (cfg_[section].find(key) == cfg_[section].end()) {
  53. return;
  54. }
  55. value = atol(cfg_[section][key].c_str());
  56. }
  57.  
  58. private:
  59. void ltrim(std::string& str) {
  60. if (str.empty()) return;
  61. size_t len = 0;
  62. char* temp = (char*)str.c_str();
  63. while (*temp && isblank(*temp)) {
  64. ++len;
  65. ++temp;
  66. }
  67. if (len > 0) str.erase(0, len);
  68. }
  69. void rtrim(std::string& str) {
  70. if (str.empty()) return;
  71. size_t len = str.length();
  72. size_t pos = len;
  73. while (pos > 0) {
  74. if (not isblank(str[pos - 1])) {
  75. break;
  76. }
  77. --pos;
  78. }
  79. if (pos != len) str.erase(pos);
  80. }
  81. void trim(std::string& str) {
  82. ltrim(str);
  83. rtrim(str);
  84. }
  85. void setSectionKeyValue(std::string& section, std::string& key, std::string& value) {
  86. if (cfg_.find(section) == cfg_.end()) {
  87. std::unordered_map<std::string, std::string> kv_map;
  88. cfg_[section] = kv_map;
  89. }
  90. if (key != "" && value != "") cfg_[section][key] = value;
  91. }
  92. bool parseLine(std::string& line, std::string& section, std::string& key, std::string& value) {
  93. static std::string cur_section = "";
  94. std::string nodes[2] = {"#", ";"}; //去掉注释的内容
  95. for (int i = 0; i < 2; ++i) {
  96. std::string::size_type pos = line.find(nodes[i]);
  97. if (pos != std::string::npos) line.erase(pos);
  98. }
  99. trim(line);
  100. if (line == "") return false;
  101. if (line[0] == '[' && line[line.size() - 1] == ']') {
  102. section = line.substr(1, line.size() - 2);
  103. trim(section);
  104. cur_section = section;
  105. return false;
  106. }
  107. if (cur_section == "") return false;
  108. bool is_key = true;
  109. for (size_t i = 0; i < line.size(); ++i) {
  110. if (line[i] == '=') {
  111. is_key = false;
  112. continue;
  113. }
  114. if (is_key) {
  115. key += line[i];
  116. } else {
  117. value += line[i];
  118. }
  119. }
  120. section = cur_section;
  121. trim(key);
  122. trim(value);
  123. return true;
  124. }
  125.  
  126. private:
  127. std::unordered_map<std::string, std::unordered_map<std::string, std::string>> cfg_;
  128. }; // ini格式配置文件的读取
  129. } // namespace Config

Config命名空间下实现了Ini配置读取类。Load函数用于加载配置文件内容,GetStrValue函数和GetIntValue函数用于获取配置项值并支持设置默认值,Dump函数用于遍历配置文件的内容。由于在解析过程中需要删除字符串中的前导和后导空白符,因此我们还实现了trim函数用于删除前导和后导空白符。

这里重点讲解一下Load函数的逻辑:每次从配置文件中读取一行,然后先去掉注释的内容,接着再判断剩余的内容是一个section头配置,还是section下的key-value配置,再走不同的解析分支。

4.demo示例

以上面配置文件demo.cfg内容的读取为例,示例代码如下。

  1. #include <iostream>
  2.  
  3. #include "config.hpp"
  4.  
  5. int main(int argc, char *argv[]) {
  6. Config::Ini ini;
  7. ini.Load("./demo.cfg");
  8. ini.Dump([](const std::string &section, const std::string &key, const std::string value) {
  9. std::cout << "section[" << section << "],key[" << key << "]->value[" << value << "]" << std::endl;
  10. });
  11. return 0;
  12. }

5.自动生成读取代码

如果这次分享的内容到上面demo示例之后就进入尾声的话,那么春哥就太过于标题党了。假设我们的程序有几十项配置内容,如果每一项采用GetIntValue函数或者GetStrValue函数来读取,那么编码工作量还是不小的,并且也容易出错,那么怎么做到提效呢?

其实提效方案并不难想到,我们可以自动生成读取配置项的代码,并生成具体业务配置读取类。下面我们举一个例子,假设我们有一个配置文件mysvr.cfg,它的内容如下。

[server]
ip = 127.0.0.1
port = 8080

[pool]
conn_pool_size = 100

我们手动编写了业务配置读取类代码文件MySvrCfg.hpp,它的内容如下。

  1. #include <string>
  2.  
  3. #include "config.hpp"
  4.  
  5. class MysvrCfg {
  6. public:
  7. bool Load(std::string file_name) {
  8. Config::Ini ini;
  9. if (not ini.Load(file_name)) {
  10. return false;
  11. }
  12. ini.GetIntValue("pool", "conn_pool_size", conn_pool_size_, 0);
  13. ini.GetIntValue("server", "port", port_, 0);
  14. ini.GetStrValue("server", "ip", ip_, "");
  15.  
  16. return true;
  17. }
  18. int64_t conn_pool_size() { return conn_pool_size_; }
  19. int64_t port() { return port_; }
  20. std::string ip() { return ip_; }
  21.  
  22. public:
  23. int64_t conn_pool_size_;
  24. int64_t port_;
  25. std::string ip_;
  26. };

我们可以发现上面的代码完全可以自动生成。「我们先读取配置的内容,然后使用配置文件的内容作为元数据驱动生成这个MySvrCfg.hpp的内容」。

自动生成业务配置读取类的脚手架工具代码文件configtool.cpp,它的内容如下。

  1. #include <iostream>
  2. #include <regex>
  3. #include <string>
  4.  
  5. #include "MysvrCfg.hpp"
  6.  
  7. using namespace std;
  8.  
  9. int genCfgReadFile(Config::Ini &ini, string file_name) {
  10. string prefix = "";
  11. for (size_t i = 0; i < file_name.size(); i++) {
  12. if (file_name[i] == '.') break;
  13. if (prefix == "") {
  14. prefix = toupper(file_name[i]);
  15. } else {
  16. prefix += file_name[i];
  17. }
  18. }
  19. string class_name = prefix + "Cfg";
  20. string output_file_name = prefix + "Cfg.hpp";
  21. ofstream out;
  22. out.open(output_file_name);
  23. if (not out.is_open()) {
  24. cout << "open " << output_file_name << " failed." << endl;
  25. return -1;
  26. }
  27. string cfg_read_content;
  28. string class_func_content;
  29. string class_member_content;
  30. ini.Dump([&cfg_read_content, &class_func_content, &class_member_content](const string &section, const string &key,
  31. const string &value) {
  32. regex integer_regex("[+-]?[0-9]+");
  33. if (regex_match(value, integer_regex)) { // 整数
  34. cfg_read_content += " ini.GetIntValue("" + section + "", "" + key + "", " + key + "_, 0);\n";
  35. class_func_content += " int64_t " + key + "() { return " + key + "_; }\n";
  36. class_member_content += " int64_t " + key + "_;\n";
  37. } else {
  38. cfg_read_content += " ini.GetStrValue("" + section + "", "" + key + "", " + key + "_, "");\n";
  39. class_func_content += " std::string " + key + "() { return " + key + "_; }\n";
  40. class_member_content += " std::string " + key + "_;\n";
  41. }
  42. });
  43. //
  44. string content = R"(#include <string>
  45.  
  46. #include "config.hpp"
  47.  
  48. class )" + class_name +
  49. R"( {
  50. public:
  51. bool Load(std::string file_name) {
  52. Config::Ini ini;
  53. if (not ini.Load(file_name)) {
  54. return false;
  55. }
  56. )" + cfg_read_content +
  57. R"(
  58. return true;
  59. }
  60. )" + class_func_content +
  61. R"(
  62. public:
  63. )" + class_member_content +
  64. "};";
  65. out << content;
  66. return 0;
  67. }
  68.  
  69. int readDemoCfg() {
  70. MysvrCfg cfg;
  71. cout << "usage: configtool cfg_file_name" << endl;
  72. cout << "read demo cfg mysvr.cfg" << endl;
  73. cfg.Load("./mysvr.cfg");
  74. cout << "ip = " << cfg.ip() << endl;
  75. cout << "port = " << cfg.port() << endl;
  76. cout << "conn_pool_size = " << cfg.conn_pool_size() << endl;
  77. return 0;
  78. }
  79.  
  80. int main(int argc, char *argv[]) {
  81. if (argc == 1) {
  82. return readDemoCfg();
  83. }
  84. if (argc != 2) {
  85. cout << "usage: configtool mysvr.cfg" << endl;
  86. return -1;
  87. }
  88. Config::Ini ini;
  89. string file_name = argv[1];
  90. if (not ini.Load(file_name)) {
  91. cout << "load " << file_name << " failed." << endl;
  92. return -1;
  93. }
  94. return genCfgReadFile(ini, file_name);
  95. }

在configtool脚手架工具中,「我们先使用Config::Ini类对象读取了配置文件的内容,然后遍历配置文件的内容,生成业务配置读取类中动态变化的代码内容,最后使用模版生成最终的代码」。

脚手架工具configtool的使用也非常简单,直接把配置文件名作为命令行参数传入即可,如果执行configtool时不携带任何参数则会使用生成的类MysvrCfg来读取上面的配置文件mysvr.cfg的内容。

到此这篇关于C++实现读写ini配置文件的示例代码的文章就介绍到这了,更多相关C++读写ini配置文件内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号