广告位联系
返回顶部
分享到

C++实现读写ini配置文件的代码

C语言 来源:互联网 作者:佚名 发布时间:2023-05-08 22:35:29 人浏览
摘要

1.概述 配置文件的读取是每个程序必备的功能,配置文件的格式多种多样,例如:ini格式、json格式、xml格式等。其中属ini格式最为简单,且应用广泛。 2.ini格式语法 注释内容采用#或者

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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

#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内容的读取为例,示例代码如下。

1

2

3

4

5

6

7

8

9

10

11

12

#include <iostream>

 

#include "config.hpp"

 

int main(int argc, char *argv[]) {

  Config::Ini ini;

  ini.Load("./demo.cfg");

  ini.Dump([](const std::string &section, 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,它的内容如下。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

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

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

#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 &section, 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的内容。


版权声明 : 本文内容来源于互联网或用户自行发布贡献,该文观点仅代表原作者本人。本站仅提供信息存储空间服务和不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权, 违法违规的内容, 请发送邮件至2530232025#qq.cn(#换@)举报,一经查实,本站将立刻删除。
原文链接 : https://juejin.cn/post/7229976104009269304
相关文章
  • C++模拟实现vector的方法教程
    一、迭代器 定义 vector类型的迭代器就是原生态的指针,对T*进行重命名即可 1 2 typedef T* iterator; typedef const T* const_iterator; 普通迭代器 1 2 3
  • C++实现读写ini配置文件的代码
    1.概述 配置文件的读取是每个程序必备的功能,配置文件的格式多种多样,例如:ini格式、json格式、xml格式等。其中属ini格式最为简单,且
  • C++20中的span容器及用法总结
    一.span容器 span是 C++20 中引入的一个新的标准容器,它用于表示连续的一段内存区间,类似于一个轻量级的只读数组容器。 span是一个轻量级
  • C++20中的std::span介绍
    span就是一个连续对象存储的观察者。类似std::string_view是string的观察者。 连续的存储,不一定是数组。例如: 1 2 3 4 5 6 7 8 zero(char (arr) [10]
  • C++11之std::future对象的使用以及说明

    C++11之std::future对象的使用以及说明
    std::future介绍 在前面几篇文章中基本都用到thread对象,它是C++11中提供异步创建多线程的工具。 但是我们想要从线程中返回异步任务结果,
  • C语言中#define在多行宏定义出错的原因及分析
    C语言中#define在多行宏定义出错的原因 1.第一种错误 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 #includestdio.h #define echange(a,b) {\/*宏定义中允许包含多行命
  • vs2022 x64 C/C++和汇编混编(案例代码)

    vs2022 x64 C/C++和汇编混编(案例代码)
    vs2022环境x64 C/C++和汇编混编 vs64位程序不支持__asm内嵌汇编,需要单独编写汇编源文件 示例如下 1、新建空的win32项目,新建main.cpp,示例代码
  • C语言中带返回值的宏定义方式
    C语言中带返回值的宏定义 相信大家在实际工作中,一定有遇到需要编写一个宏定义,且希望它能带返回值的场景吧? 比如我之前就遇到一
  • OpenCV实现视频绿幕背景替换功能
    1、概述 案例:使用OpenCV实现视频绿幕背景替换 算法步骤: 1.初始化VideoCapture并使用其open方法加载视频 2.while循环加读取frame capture.read(fra
  • OpenCV通过透视变换实现矫正图像介绍

    OpenCV通过透视变换实现矫正图像介绍
    1、概述 案例:使用OpenCV将一张折射的图片给矫正过来 实现步骤: 1.载入图像 2.图像灰度化 3.二值分割 4.形态学操作去除噪点 5.轮廓发现
  • 本站所有内容来源于互联网或用户自行发布,本站仅提供信息存储空间服务,不拥有版权,不承担法律责任。如有侵犯您的权益,请您联系站长处理!
  • Copyright © 2017-2022 F11.CN All Rights Reserved. F11站长开发者网 版权所有 | 苏ICP备2022031554号-1 | 51LA统计