当前位置:网站首页>【iniparser】项目配置工具iniparser的简单使用
【iniparser】项目配置工具iniparser的简单使用
2022-07-25 18:59:00 【半生瓜のblog】
项目配置工具iniparser
前言
对于很多程序中要用的参数如果是可变的,那么最好的处理方式就是通过main函数进行参数传递,或者从别的地方去获取,这其中之一的做法就是使用配置文件,在一个成熟和架构完善的系统中,一般都会做到自动配置,自动部署。
所以有的系统里会有一个单独的配置服务存在,其它的每个服务配置都会从配置中心服务获取。
运维人员会通过操作界面把配置信息下发到配置中心服务,其余服务从配置中心获取变更信息。
几乎每个大型互联网项目都会涉及到项目配置。
安装与构建
- git clone
git clone https://github.com/ndevilla/iniparser.git
- cd iniparser
- make
- 编译生成,.a为静态库,.so为动态库
测试
- 本测试项目目录树。
share_bike |-- conf | `-- share_bike.ini |-- git | `-- iniparser | |--略 |-- src | |-- CMakeLists.txt | |-- common | | |-- CMakeLists.txt | | |-- configdef.h | | |-- iniconfig.cpp | | |-- iniconfig.h | |-- main.cpp |-- test `-- third |-- include | `-- iniparser | |-- dictionary.h | `-- iniparser.h `-- lib `-- iniparser |-- libiniparser.a `-- libiniparser.so.1
- 注意:ubuntu下可以使用tree命令打印目录树。 tree
sudo apt-get install tree
- /src/common/configdef.h
#ifndef SHBK_COMMON_CONFIGDEF_H_
#define SHBK_COMMON_CONFIGDEF_H_
#include <string>
typedef struct st_env_config{
//数据库的配置
std::string db_ip;
unsigned short db_port;
std::string db_user;
std::string db_pwd;
std::string db_name;
//服务器的配置
unsigned short svr_port;
//构造函数
st_env_config(){
}
st_env_config(const std::string& db_ip, unsigned int db_port, const std::string& db_user,
const std::string& db_pwd, const std::string& db_name, unsigned short svr_port){
this->db_ip = db_ip;
this->db_port = db_port;
this->db_user = db_user;
this->db_pwd = db_pwd;
this->db_name = db_name;
this->svr_port = svr_port;
}
st_env_config& operator =(const st_env_config& config){
if (this != &config){
this->db_ip = config.db_ip;
this->db_port = config.db_port;
this->db_user = config.db_user;
this->db_pwd = config.db_pwd;
this->db_name = config.db_name;
this->svr_port = config.svr_port;
}
return *this;
}
}_st_env_config;
#endif
- /src/common/iniconfig.h
#ifndef SHBK_COMMON_INICONFIG_H_
#define SHBK_COMMON_INICONFIG_H_
#include <string>
#include "configdef.h"
class Iniconfig{
public:
Iniconfig();
~Iniconfig();
//加载配置
bool loadfile(const std::string& path);
//获取配置
const st_env_config& getconfig();
private:
st_env_config _config;
bool _isloaded;
};
#endif //SHBK_COMMON_INICONFIG_H_
- /src/common/iniconfig.cpp
#include "iniconfig.h"
#include <iniparser/iniparser.h>
Iniconfig::Iniconfig():_isloaded(false){
}
Iniconfig::~Iniconfig(){
}
bool Iniconfig::loadfile(const std::string& path){
dictionary* ini = NULL;
ini = iniparser_load(path.c_str());
if(ini == NULL){
fprintf(stderr,"cannot parse file:%s\n",path.c_str());
return false;
}
//拿到信息,字典,——区段:区段中的信息,默认值(如果没找到就取这个默认值)
const char* ip = iniparser_getstring(ini,"database:ip","127.0.0.1");
const int port = iniparser_getint(ini, "database:port", 3306);
const char* user = iniparser_getstring(ini, "database:user", "root");
const char* pwd = iniparser_getstring(ini, "database:pwd", "123456");
const char* db = iniparser_getstring(ini, "database:db", "dongnaobike");
const int sport = iniparser_getint(ini, "server:port", 9090);
//信息赋值
_config = st_env_config(std::string(ip), port, std::string(user),
std::string(pwd), std::string(db), sport);
//释放字典资源
iniparser_freedict(ini);
_isloaded = true;
return _isloaded;
}
const st_env_config& Iniconfig::getconfig(){
return _config;
}
- /src/common/CMakeLists.txt
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
# 搜集所有在指定路径下的源文件的文件名,将输出结果存储在指定的变量中
aux_source_directory(. SOURCES_COMMON_FILES)
# 构建库供其他模块使用
ADD_LIBRARY(common ${SOURCES_COMMON_FILES})
# 用来显式的定义变量
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -rdynamic -Wall -m64 -pipe -std=c++0x -lrt -Wno-reorder -Wdeprecated-declarations")
# 将指定目录添加到编译器的有文件搜索路径之下
INCLUDE_DIRECTORIES(../../third/include)
# 将指定目录添加到需要链接的库文件目录之下
LINK_DIRECTORIES(../../third/lib/iniparser)
# 将目标文件与库文件进行链接
TARGET_LINK_LIBRARIES(common iniparser)
TARGET_LINK_LIBRARIES(common dl)
- /src/main.cpp
#include "iniconfig.h" //注意路径,会在CMake中进行调整
#include "configdef.h"
int main(int argc,char** argv){
if(argc != 2){//缺少参数
printf("Please input shbk <config file path>!\n");
return -1;
}
Iniconfig config;
if(!config.loadfile(std::string(argv[1]))){
printf("load %s failed.\n",argv[1]);
return -2;
}
//测试是否读取成功
st_env_config conf_args = config.getconfig();
printf("[database]ip:%s\n [database]port:%d\n [database]user:%s\n [database]pwd:%s\n [database]db:%s\n [server]port:%d\n ",
conf_args.db_ip.c_str(),
conf_args.db_port,
conf_args.db_user.c_str(),
conf_args.db_pwd.c_str(),
conf_args.db_name.c_str(),
conf_args.svr_port);
return 0;
}
- /src/CMakeLists.txt
# CMake要求最低版本
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
# 工程名
PROJECT(share_bike)
# 将指定目录添加到编译器的头文件搜索路径之下
INCLUDE_DIRECTORIES(../third/include)
INCLUDE_DIRECTORIES(./common)
# 将指定目录添加到需要链接的库文件目录之下
LINK_DIRECTORIES(../third/lib/iniparser)
# 刚才在子文件夹中生成的库
LINK_DIRECTORIES(./common)
# 搜集所有在指定路径下的源文件的文件名,将输出的结果列表存储在指定的变量中
# 内置变量CMAKE_SOURCE_DIR 定义了顶级CMakeLists.txt所在的文件夹
# PROJECT_SOURCE_DIR 定义了包含最近的project()命令的CMakeLists.txt 所在的文件夹
aux_source_directory(${PROJECT_SOURCE_DIR} SOURCE_FILES)
# 使用给定的源文件,为工程引入一个可执行文件
ADD_EXECUTABLE(share_bike ${SOURCE_FILES})
# GCC编译选项
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -rdynamic -Wall -m64 -pipe -std=c++0x -lrt -Wno-reorder -Wdeprecated-declarations")
TARGET_LINK_LIBRARIES(share_bike iniparser)
TARGET_LINK_LIBRARIES(share_bike common)
# 增加子目录
ADD_SUBDIRECTORY(common)
SET(CMAKE_INSTALL_PREFIX ${CMAKE_BINARY_DIR})
INSTALL(TARGETS share_bike DESTINATION bin)
- /conf/share_bike.ini
[database]
ip = 127.0.0.1 ;
port = 3306 ;
user = root ;
pwd = 123456 ;
db = qiniubike;
[server]
port = 9090;
- 编译,在/src目录下
cmake .
make
- 执行
./share_bike ../conf/share_bike.ini

边栏推荐
- 无惧高温暴雨,有孚网络如何保您无忧?
- 2022 Robocom 省赛题解
- 《21天精通TypeScript-4》-类型推断与语义检查
- MES管理系统有什么应用价值
- The auction house is a VC, and the first time it makes a move, it throws a Web3
- PyQt5单击QTableView垂直表头verticalHeader获取行数据以及单击单元格获取行数据操作
- With a financing of 200million yuan, the former online bookstore is now closed nationwide, with only 3 stores left in 60 stores
- Common file operations
- What is national debt reverse repurchase? Is it safe?
- Alibaba cloud technology expert Qin long: reliability assurance is a must - how to carry out chaos engineering on the cloud?
猜你喜欢

Twitter acquired a public opinion war, which was turned into a child quarrel by musk

从目标检测到图像分割简要发展史

Dachang cloud business adjustment, a new round of war turn

Single arm routing experiment demonstration (Huawei router device configuration)

CircleIndicator组件,使指示器风格更加多样化

The understanding of domain adaptation in transfer learning and the introduction of three technologies

Real estate enterprises have launched a "war of guarantee"

华为交换机系统软件升级和安全漏洞修复教程

Vc/pe is running towards Qingdao

HTTP缓存通天篇,可能有你想要的
随机推荐
Osmosis extends its cross chain footprint to poca through integration with axelar and moonbeam
QT compiled successfully, but the program could not run
关爱一线防疫工作者,浩城嘉业携手高米店街道办事处共筑公益长城
如何创建一个有效的帮助文档?
Baklib:制作优秀的产品说明手册
Typescript对象代理器Proxy使用
GDB help
Interpretation of "cross chain interconnection smart contract"
#yyds干货盘点# 面试必刷TOP101:反转链表
[translation] LFX 2022 spring tutor qualification opening - apply for CNCF project before February 13!
Northeast people know sexiness best
软件测试(思维导图)
Gan, why ".Length! == 3??
With a market value of 30billion yuan, the largest IPO in Europe in the past decade was re launched on the New York Stock Exchange
无惧高温暴雨,有孚网络如何保您无忧?
Pixel2Mesh从单个RGB图像生成三维网格ECCV2018
[help center] provide your customers with the core options of self-service
APP测试点(思维导图)
韩国AI团队抄袭震动学界!1个导师带51个学生,还是抄袭惯犯
Deng Qinglin, a technical expert of Alibaba cloud: Best Practices for disaster recovery and remote multi activity across availability zones on cloud
