C++實現(xiàn)讀寫ini配置文件的示例代碼
1.概述
配置文件的讀取是每個程序必備的功能,配置文件的格式多種多樣,例如:ini格式、json格式、xml格式等。其中屬ini格式最為簡單,且應(yīng)用廣泛。
2.ini格式語法
- 注釋內(nèi)容采用“#”或者“;”開頭。
- 配置是由一系列的section組成,每個section就是一個關(guān)聯(lián)的配置塊,section使用[]包含起來。
- 每個section下配置的是具體的配置項,每個配置項是使用“=”分隔的key-value對。
下面讓我們來看一個簡單的示例,假設(shè)我們有一個配置文件demo.cfg,它的內(nèi)容如下所示。
[server]
ip = 127.0.0.1
port = 8088
上面的配置內(nèi)容中,有一個server的配置節(jié),在這個配置節(jié)里有兩個配置項,它們分別是ip和port,ip的值為127.0.0.1,port的值為8088。
3.配置讀取
知道了ini格式語法之后,就可以根據(jù)語法規(guī)則來讀取配置文件內(nèi)容了,春哥這里實現(xiàn)了一個非常精簡易用的版本,源代碼文件config.hpp的內(nèi)容如下。
#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] = {"#", ";"}; //去掉注釋的內(nèi)容
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命名空間下實現(xiàn)了Ini配置讀取類。Load函數(shù)用于加載配置文件內(nèi)容,GetStrValue函數(shù)和GetIntValue函數(shù)用于獲取配置項值并支持設(shè)置默認值,Dump函數(shù)用于遍歷配置文件的內(nèi)容。由于在解析過程中需要刪除字符串中的前導和后導空白符,因此我們還實現(xiàn)了trim函數(shù)用于刪除前導和后導空白符。
這里重點講解一下Load函數(shù)的邏輯:每次從配置文件中讀取一行,然后先去掉注釋的內(nèi)容,接著再判斷剩余的內(nèi)容是一個section頭配置,還是section下的key-value配置,再走不同的解析分支。
4.demo示例
以上面配置文件demo.cfg內(nèi)容的讀取為例,示例代碼如下。
#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.自動生成讀取代碼
如果這次分享的內(nèi)容到上面demo示例之后就進入尾聲的話,那么春哥就太過于標題黨了。假設(shè)我們的程序有幾十項配置內(nèi)容,如果每一項采用GetIntValue函數(shù)或者GetStrValue函數(shù)來讀取,那么編碼工作量還是不小的,并且也容易出錯,那么怎么做到提效呢?
其實提效方案并不難想到,我們可以自動生成讀取配置項的代碼,并生成具體業(yè)務(wù)配置讀取類。下面我們舉一個例子,假設(shè)我們有一個配置文件mysvr.cfg,它的內(nèi)容如下。
[server]
ip = 127.0.0.1
port = 8080
[pool]
conn_pool_size = 100
我們手動編寫了業(yè)務(wù)配置讀取類代碼文件MySvrCfg.hpp,它的內(nèi)容如下。
#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_;
};我們可以發(fā)現(xiàn)上面的代碼完全可以自動生成?!肝覀兿茸x取配置的內(nèi)容,然后使用配置文件的內(nèi)容作為元數(shù)據(jù)驅(qū)動生成這個MySvrCfg.hpp的內(nèi)容」。
自動生成業(yè)務(wù)配置讀取類的腳手架工具代碼文件configtool.cpp,它的內(nèi)容如下。
#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)) { // 整數(shù)
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類對象讀取了配置文件的內(nèi)容,然后遍歷配置文件的內(nèi)容,生成業(yè)務(wù)配置讀取類中動態(tài)變化的代碼內(nèi)容,最后使用模版生成最終的代碼」。
腳手架工具configtool的使用也非常簡單,直接把配置文件名作為命令行參數(shù)傳入即可,如果執(zhí)行configtool時不攜帶任何參數(shù)則會使用生成的類MysvrCfg來讀取上面的配置文件mysvr.cfg的內(nèi)容。
到此這篇關(guān)于C++實現(xiàn)讀寫ini配置文件的示例代碼的文章就介紹到這了,更多相關(guān)C++讀寫ini配置文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于Qt開發(fā)獲取CTP量化交易接口測試數(shù)據(jù)工具
這篇文章主要為大家詳細介紹了如何使用Qt軟件開發(fā)K線股P相關(guān)軟件,先開發(fā)一個通過CTP量化交易的sdk獲取相關(guān)推送數(shù)據(jù)的工具,需要的可以參考下2024-04-04
詳解C語言中fseek函數(shù)和ftell函數(shù)的使用方法
這篇文章主要介紹了C語言中fseek函數(shù)和ftell函數(shù)的使用方法,兩個函數(shù)分別用于設(shè)置和返回文件指針stream的位置,需要的朋友可以參考下2016-03-03

