一、库介绍
平常的ini配置文件只能读取,但是这个库不光可以读取、写入配置项,还能给配置项写注释。只有一个hpp头文件,不需要编译,支持C++11及之后版本。
MIT license。
二、库使用
git clone https://github.com/dujingning/inicpp.git
1.读取INI文件示例
#include "inicpp.hpp"
#include <iostream>
int main()
{
inicpp::IniManager _ini("config.ini"); // Load and parse the INI file.
std::cout << _ini["rtsp"]["port"] << std::endl;
}
2.写示例
#include "inicpp.hpp"
#include <iostream>
int main()
{
inicpp::IniManager _ini("config.ini"); // Load and parse the INI file.
_ini.modify("rtsp","port","554");
std::cout << _ini["rtsp"]["port"] << std::endl;
}
3.添加注释
#include "inicpp.hpp"
#include <iostream>
int main()
{
inicpp::iniReader _ini("config.ini"); // Load and parse the INI file.
_ini.modify("rtsp","port","554","this is the listen port for rtsp server");
std::cout << _ini["rtsp"]["port"] << std::endl;
}
4.toString()、toInt()、toDouble()
#include "inicpp.hpp"
#include <iostream>
int main()
{
inicpp::IniManager _ini("config.ini"); // Load and parse the INI file.
_ini.modify("rtsp","port","554","this is the listen port for rtsp server");
std::cout << _ini["rtsp"]["port"] << std::endl;
// Convert to string, default is string
std::string http_port_s = _ini["http"].toString("port");
std::cout << "to string:\thttp.port = " << http_port_s << std::endl;
// Convert to double
double http_port_d = _ini["http"].toDouble("port");
std::cout << "to double:\thttp.port = " << http_port_d << std::endl;
// Convert to int
int http_port_i = _ini["http"].toInt("port");
std::cout << "to int:\t\thttp.port = " << http_port_i << std::endl;
}
5.检查key是否存在:isKeyExists()
#include "inicpp.hpp"
#include <iostream>
int main()
{
inicpp::IniManager _ini("config.ini");
if (!_ini["rtsp"].isKeyExist("port"))
{
std::cout << "rtsp.port: not exist!" << "\n";
}
else
{
std::cout << "rtsp.port: not exist!" << "\n";
}
return 0;
}
三.代码自定义修改inicpp.hpp:
默认该库会把读取的行去除所有空格(" ")和制表格(\t),这会导致配置项的值无法读取到字符串中间的字符串,例如:filePath = /home/xxx/test 1/aaa bbb/test.txt,路径中包含空格,默认读取后后的路径是:/home/xxx/test1/aaabbb/test.txt,丢掉了中间的空格,故这里本人修改了一段代码:
修改bool filterData(std::string &data)方法中的过滤字符代码:(fileter new片段)
bool filterData(std::string &data)
{
// filter
// data.erase(std::remove_if(data.begin(), data.end(), [](char c)
// { return c == ' ' || c == '\t'; }),
// data.end());
// filter new ----------------------------------------------------
// 查找最后一个非空格字符的位置
size_t end_pos = data.find_last_not_of(' ');
if (end_pos != std::string::npos) {
// 删除最后一个非空格字符后的所有字符
data.erase(end_pos + 1);
}
bool value_start = false;
bool value_start_space = true;
data.erase(std::remove_if(data.begin(), data.end(), [&](char c)
{
if(value_start){
if(c != ' '){
value_start_space = false;
}
if(value_start_space){
return c == ' ' || c == '\t';
}
return false;
}else {
if(c == '='){
value_start = true;
}
return c == ' ' || c == '\t';
}
}),
data.end());
// filter new ----------------------------------------------------
if (data.length() == 0)
{
return false;
}
if (data[0] == ';')
{
return false;
}
if (data[0] == '#')
{
return false;
}
return true;
}
三.也可以自定义实现配置文件读取使用boost库:
在启动程序或者服务器时,都要读取些配置文件,windows有自带的WritePrivateProfileString可以将配置信息写入ini文件;但是跨平台的话,还是用boost的比较好。
如果配置文件名为test.ini,里面的配置信息如下:
[command]
host = 127.0.0.1
port = 7000
minsize = 2
maxsize = 10
timeout = 10
要将这些配置信息读出来,程序的实现如下:
#include <iostream>
#include <string>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ini_parser.hpp>
void read_configure_ini()
{
boost::property_tree::ptree pt, commonCfg;
boost::property_tree::ini_parser::read_ini("./test.ini", pt);
string NodeIpStr;
int port;
int minsize;
int maxsize;
int timeout;
commonCfg= pt.get_child("command");
NodeIpStr = commonCfg.get<std::string>("host", "127.0.0.1");
port = commonCfg.get<int>("port", 7000);
minsize = commonCfg.get<int>("minsize", 2);
maxsize = commonCfg.get<int>("maxsize", 10);
timeout = commonCfg.get<int>("timeout", 10);
cout << "the ip is:" << redisNodeIpStr << endl;
cout << "the port is:" << port << endl;
cout << "the minsize is:" << minsize << endl;
cout << "the maxsize is:" << maxsize << endl;
cout << "the timeout is:" << timeout << endl;
}
int main()
{
read_configure_ini();
return 0;
}
这样的话就能将读取到的ini文件的配置信息加载到定义的这些变量当中
参考:
https://www.cnblogs.com/Unclebigdata/p/14866604.html
C++ 的 ini 配置文件读写/注释库 inicpp 用法 [ header-file-only ]-腾讯云开发者社区-腾讯云