使用iniparser处理ini文件
在程序中很多使用到的参数是不固定的,其处理方式可以是通过main函数的参数传递,也可以通过其他方式获取;配置文件就是其中之一。
一个成熟和架构完善的系统,一般都可以做到自动配置、自动部署,因此有些系统内会有一个单独的配置服务,每个其他服务的配置信息从配置服务内获取,这样运维人员就可以通过界面把配置信息下发给配置中心服务,其他服务再从配置中心获取变更信息。
ini文件格式
[database]
ip = 127.0.0.1 ;
port = 3306 ;
user = root ;
pwd = 123456 ;
db = database;
[server]
port = 9090;
下表即为以上示例数据做出说明
名称 | 说明 |
---|---|
database, server | 区段名 |
ip, port, user | 字段 |
127.0.0.1, 3306, root | 字段的值 |
iniparser的下载和编译
iniparser主页链接:iniparser
- 将权限切换至root
- (如已安装git则跳过此步骤) apt-get install git
- 执行 git clone https://github.com/ndevilla/iniparser 命令从git上克隆iniparser的源码
- 下载后进入iniparser目录,执行 make 命令编译项目
- 将生成的 libiniparser.a 和 libiniparser.so.1 两个文件移至项目内[此处将其移至 third/lib/iniparser/ 下],以备后续使用。
- 将iniparser/src目录下的 dictionary.h和iniparser.h移至项目内[此处将其移至 third/include/iniparser/ 下],以备后续使用。
iniparser的的使用示例
- 在config目录下新建一个名为 myini.ini 的文件,文件内容如下:
[database]
ip = 127.0.0.1 ;
port = 3306 ;
user = root ;
pwd = 123456 ;
db = database;
[server]
port = 9090;
- 源文件及代码
(1) 文件 configdef.h
#ifndef _SHARE_BIKE_COMMON_CONFIGDEF_H_
#define _SHARE_BIKE_COMMON_CONFIGDEF_H_
#include <string>
/**********************************************************************
* 此结构体用于存放项目中需要使用的字段,以便于访问
***********************************************************************/
typedef struct st_env_config {
// 数据库的配置
std::string db_ip; // 数据库IP地址
unsigned short db_port; // 数据库端口号
std::string db_user; // 数据库用户
std::string db_name; // 数据库名称
std::string db_pwd; // 数据库密码
// 服务器的配置
unsigned short svr_port; // 服务器端口号
st_env_config() {}
st_env_config(const std::string& db_ip, unsigned short db_port,
const std::string& db_user, const std::string& db_name,
const std::string& db_pwd, unsigned short svr_port) {
this->db_ip = db_ip;
this->db_port = db_port;
this->db_user = db_user;
this->db_name = db_name;
this->db_pwd = db_pwd;
this->svr_port = svr_port;
}
st_env_config& operator=(const st_env_config& other) {
if (this == &other) { return *this; }
this->db_ip = other.db_ip;
this->db_port = other.db_port;
this->db_user = other.db_user;
this->db_name = other.db_name;
this->db_pwd = other.db_pwd;
this->svr_port = other.svr_port;
return *this;
}
}_st_env_config;
#endif // !_SHARE_BIKE_COMMON_CONFIGDEF_H_
(2) 文件 iniconfig.h
#ifndef _SHARE_BIKE_COMMON_INICONFIG_H_
#define _SHARE_BIKE_COMMON_INICONFIG_H_
#include <string>
#include "configdef.h"
/**********************************************************************
* 此类基于iniparser做的封装
***********************************************************************/
class Iniconfig {
public:
Iniconfig();
~Iniconfig();
bool loadfile(const std::string& filepath);
const st_env_config& getconfig();
protected:
st_env_config _config;
bool _isloaded;
};
#endif // !_SHARE_BIKE_COMMON_INICONFIG_H_
(3) 文件 iniconfig.cpp
#include "iniconfig.h"
#include "iniparser/iniparser.h" // 此处不使用 ../../third/include/iniparser/iniparser.h 而使用 iniparser/iniparser.h 是因为会在CMakeLists.h中配置
#include "iniparser/dictionary.h"
Iniconfig::Iniconfig() : _isloaded(false) {}
Iniconfig::~Iniconfig() {}
bool Iniconfig::loadfile(const std::string& filepath) {
dictionary* ini = NULL;
// 此处不做已加载处理,如若需要可根据实际情况处理
ini = iniparser_load(filepath.c_str());
if (!ini) {
fprintf(stderr, "cannot parser fail: %s\n", filepath.c_str());
return false;
}
const char* ip = iniparser_getstring(ini, "database:ip", "127.0.0.1");
int port = iniparser_getint(ini, "database:port", 3306);
const char* user = iniparser_getstring(ini, "database:user", "root");
const char* name = iniparser_getstring(ini, "database:db", "share_bike");
const char* pwd = iniparser_getstring(ini, "database:pwd", "123456");
int _port = iniparser_getint(ini, "server:port", 9090);
_config = st_env_config(std::string(ip), port, std::string(user),
std::string(name), std::string(pwd), _port);
iniparser_freedict(ini); // 释放字典
_isloaded = true;
return _isloaded;
}
const st_env_config& Iniconfig::getconfig() {
return _config;
}
(4) 文件 main.cpp
#include "iniconfig.h" // 此处不使用common/iniconfig.h 而使用iniconfig.h是因为会在CMakeLists.h中配置
#include "configdef.h"
int main(int argc, char** argv) {
if (argc < 2) {
fprintf(stdout, "Please input share_bike <config file path>!\n");
return -1;
}
Iniconfig config;
if (!config.loadfile(std::string(argv[1]))) {
fprintf(stdout, "load inifile [%s] failed!\n", argv[1]);
return -2;
}
st_env_config conf_args = config.getconfig();
printf("[database]\nip:\t%s\nport:\t%d\nuser:\t%s\nname:\t%s\npwd:\t%s\n[server]\nport:\t%d\n",
conf_args.db_ip.c_str(), conf_args.db_port, conf_args.db_user.c_str(),
conf_args.db_name.c_str(), conf_args.db_pwd.c_str(), conf_args.svr_port);
return 0;
}
(5) 编译 执行