C++實(shí)現(xiàn)讀寫(xiě)ini配置文件的示例代碼
1.概述
配置文件的讀取是每個(gè)程序必備的功能,配置文件的格式多種多樣,例如:ini格式、json格式、xml格式等。其中屬ini格式最為簡(jiǎn)單,且應(yīng)用廣泛。
2.ini格式語(yǔ)法
- 注釋內(nèi)容采用“#”或者“;”開(kāi)頭。
- 配置是由一系列的section組成,每個(gè)section就是一個(gè)關(guān)聯(lián)的配置塊,section使用[]包含起來(lái)。
- 每個(gè)section下配置的是具體的配置項(xiàng),每個(gè)配置項(xiàng)是使用“=”分隔的key-value對(duì)。
下面讓我們來(lái)看一個(gè)簡(jiǎn)單的示例,假設(shè)我們有一個(gè)配置文件demo.cfg,它的內(nèi)容如下所示。
[server]
ip = 127.0.0.1
port = 8088
上面的配置內(nèi)容中,有一個(gè)server的配置節(jié),在這個(gè)配置節(jié)里有兩個(gè)配置項(xiàng),它們分別是ip和port,ip的值為127.0.0.1,port的值為8088。
3.配置讀取
知道了ini格式語(yǔ)法之后,就可以根據(jù)語(yǔ)法規(guī)則來(lái)讀取配置文件內(nèi)容了,春哥這里實(shí)現(xiàn)了一個(gè)非常精簡(jiǎ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命名空間下實(shí)現(xiàn)了Ini配置讀取類(lèi)。Load函數(shù)用于加載配置文件內(nèi)容,GetStrValue函數(shù)和GetIntValue函數(shù)用于獲取配置項(xiàng)值并支持設(shè)置默認(rèn)值,Dump函數(shù)用于遍歷配置文件的內(nèi)容。由于在解析過(guò)程中需要?jiǎng)h除字符串中的前導(dǎo)和后導(dǎo)空白符,因此我們還實(shí)現(xiàn)了trim函數(shù)用于刪除前導(dǎo)和后導(dǎo)空白符。
這里重點(diǎn)講解一下Load函數(shù)的邏輯:每次從配置文件中讀取一行,然后先去掉注釋的內(nèi)容,接著再判斷剩余的內(nèi)容是一個(gè)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.自動(dòng)生成讀取代碼
如果這次分享的內(nèi)容到上面demo示例之后就進(jìn)入尾聲的話,那么春哥就太過(guò)于標(biāo)題黨了。假設(shè)我們的程序有幾十項(xiàng)配置內(nèi)容,如果每一項(xiàng)采用GetIntValue函數(shù)或者GetStrValue函數(shù)來(lái)讀取,那么編碼工作量還是不小的,并且也容易出錯(cuò),那么怎么做到提效呢?
其實(shí)提效方案并不難想到,我們可以自動(dòng)生成讀取配置項(xiàng)的代碼,并生成具體業(yè)務(wù)配置讀取類(lèi)。下面我們舉一個(gè)例子,假設(shè)我們有一個(gè)配置文件mysvr.cfg,它的內(nèi)容如下。
[server]
ip = 127.0.0.1
port = 8080
[pool]
conn_pool_size = 100
我們手動(dòng)編寫(xiě)了業(yè)務(wù)配置讀取類(lèi)代碼文件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)上面的代碼完全可以自動(dòng)生成?!肝覀兿茸x取配置的內(nèi)容,然后使用配置文件的內(nèi)容作為元數(shù)據(jù)驅(qū)動(dòng)生成這個(gè)MySvrCfg.hpp的內(nèi)容」。
自動(dòng)生成業(yè)務(wù)配置讀取類(lèi)的腳手架工具代碼文件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類(lèi)對(duì)象讀取了配置文件的內(nèi)容,然后遍歷配置文件的內(nèi)容,生成業(yè)務(wù)配置讀取類(lèi)中動(dòng)態(tài)變化的代碼內(nèi)容,最后使用模版生成最終的代碼」。
腳手架工具configtool的使用也非常簡(jiǎn)單,直接把配置文件名作為命令行參數(shù)傳入即可,如果執(zhí)行configtool時(shí)不攜帶任何參數(shù)則會(huì)使用生成的類(lèi)MysvrCfg來(lái)讀取上面的配置文件mysvr.cfg的內(nèi)容。
到此這篇關(guān)于C++實(shí)現(xiàn)讀寫(xiě)ini配置文件的示例代碼的文章就介紹到這了,更多相關(guān)C++讀寫(xiě)ini配置文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++?數(shù)據(jù)結(jié)構(gòu)超詳細(xì)講解順序表
程序中經(jīng)常需要將一組數(shù)據(jù)元素作為整體管理和使用,需要?jiǎng)?chuàng)建這種元素組,用變量記錄它們,傳進(jìn)傳出函數(shù)等。一組數(shù)據(jù)中包含的元素個(gè)數(shù)可能發(fā)生變化,順序表則是將元素順序地存放在一塊連續(xù)的存儲(chǔ)區(qū)里,元素間的順序關(guān)系由它們的存儲(chǔ)順序自然表示2022-03-03C語(yǔ)言實(shí)現(xiàn)圖的遍歷之深度優(yōu)先搜索實(shí)例
這篇文章主要介紹了C語(yǔ)言實(shí)現(xiàn)圖的遍歷之深度優(yōu)先搜索實(shí)例,采用不同的方法實(shí)現(xiàn)了深度優(yōu)先搜索算法,有不錯(cuò)的借鑒價(jià)值,需要的朋友可以參考下2014-09-09C語(yǔ)言的sleep、usleep、nanosleep等休眠函數(shù)的使用
本文主要介紹了C語(yǔ)言的sleep、usleep、nanosleep等休眠函數(shù)的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03Linux下控制(統(tǒng)計(jì))文件的生成的C代碼實(shí)現(xiàn)
這篇文章主要介紹了Linux下控制(統(tǒng)計(jì))文件的生成的C代碼實(shí)現(xiàn),感興趣的小伙伴們可以參考一下2016-01-01