当前位置:网站首页>[iniparser] simple use of the project configuration tool iniparser
[iniparser] simple use of the project configuration tool iniparser
2022-07-25 19:07:00 【Half raw melon blog】
Project configuration tool iniparser
Preface
For many programs, if the parameters to be used are variable , Then the best way to deal with it is through main Function to pass parameters , Or get it from other places , One way to do this is to use configuration files , In a mature and well structured system , Generally, it will be configured automatically , Automatic deployment .
So there will be a separate configuration service in some systems , Every other service configuration will be obtained from the configuration center service .
The operation and maintenance personnel will distribute the configuration information to the configuration center service through the operation interface , Other services obtain change information from the configuration center .
Almost every large Internet project involves project configuration .
Installation and construction
- git clone
git clone https://github.com/ndevilla/iniparser.git
- cd iniparser
- make
- Compile the generated ,.a Is a static library ,.so For dynamic library
test
- This test item directory tree .
share_bike |-- conf | `-- share_bike.ini |-- git | `-- iniparser | |-- A little |-- 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
- Be careful :ubuntu You can use tree Command print directory 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{
// Configuration of database
std::string db_ip;
unsigned short db_port;
std::string db_user;
std::string db_pwd;
std::string db_name;
// Server configuration
unsigned short svr_port;
// Constructors
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();
// Load the configuration
bool loadfile(const std::string& path);
// Get configuration
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;
}
// Get the information , Dictionaries ,—— section : Information in the section , The default value is ( If it is not found, take this default value )
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);
// Information assignment
_config = st_env_config(std::string(ip), port, std::string(user),
std::string(pwd), std::string(db), sport);
// Free dictionary resources
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)
# Collect the file names of all source files under the specified path , Store the output result in the specified variable
aux_source_directory(. SOURCES_COMMON_FILES)
# Build libraries for other modules
ADD_LIBRARY(common ${SOURCES_COMMON_FILES})
# Used to explicitly define variables
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -rdynamic -Wall -m64 -pipe -std=c++0x -lrt -Wno-reorder -Wdeprecated-declarations")
# Add the specified directory to the compiler's existing file search path
INCLUDE_DIRECTORIES(../../third/include)
# Add the specified directory to the directory of the library file that needs to be linked
LINK_DIRECTORIES(../../third/lib/iniparser)
# Link the target file to the library file
TARGET_LINK_LIBRARIES(common iniparser)
TARGET_LINK_LIBRARIES(common dl)
- /src/main.cpp
#include "iniconfig.h" // Pay attention to the path , Will be in CMake Adjust in
#include "configdef.h"
int main(int argc,char** argv){
if(argc != 2){// Lack of parameter
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;
}
// Test whether the reading is successful
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 Minimum version required
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
# project name
PROJECT(share_bike)
# Add the specified directory to the compiler's header file search path
INCLUDE_DIRECTORIES(../third/include)
INCLUDE_DIRECTORIES(./common)
# Add the specified directory to the directory of the library file that needs to be linked
LINK_DIRECTORIES(../third/lib/iniparser)
# The library just generated in the subfolder
LINK_DIRECTORIES(./common)
# Collect the file names of all source files under the specified path , Store the output result list in the specified variable
# Built-in variables CMAKE_SOURCE_DIR Defines the top level CMakeLists.txt Folder in which
# PROJECT_SOURCE_DIR Defines that contains the most recent project() Ordered CMakeLists.txt Folder in which
aux_source_directory(${PROJECT_SOURCE_DIR} SOURCE_FILES)
# Use the given source file , Introduce an executable file for the project
ADD_EXECUTABLE(share_bike ${SOURCE_FILES})
# GCC Compilation options
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 subdirectories
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;
- compile , stay /src Under the table of contents
cmake .
make
- perform
./share_bike ../conf/share_bike.ini

边栏推荐
- How to prohibit the use of 360 browser (how to disable the built-in browser)
- GDB help
- 李宏毅《机器学习》丨1. Introduction of this course(机器学习介绍)
- 21 days proficient in typescript-4 - type inference and semantic check
- Single arm routing experiment demonstration (Huawei router device configuration)
- Basic mode of music theory
- 【加密周报】加密市场有所回温?寒冬仍未解冻!盘点上周加密市场发生的重大事件!
- Process communication (Systemv communication mode: shared memory, message queue, semaphore)
- Interface automation test platform fasterrunner series (IV) - continuous integration and solution of multi domain names
- 接口自动化测试平台FasterRunner系列(一)- 简介、安装部署、启动服务、访问地址、配置补充
猜你喜欢

21 days proficient in typescript-4 - type inference and semantic check

What is the application value of MES management system

Basic mode of music theory

SQL Server 2019 安装教程

Process communication (Systemv communication mode: shared memory, message queue, semaphore)

基础乐理--配置和弦

Real estate enterprises have launched a "war of guarantee"

Based on easycv to reproduce Detr and dab-detr, the correct opening method of object query

Share six practical applet plug-ins

Yyds dry inventory interview must brush top101: reverse linked list
随机推荐
ThreadLocal夺命11连问
Basic mode of music theory
SQL Server 2019 安装教程
无惧高温暴雨,有孚网络如何保您无忧?
ES6 implements the observer mode through proxy and reflection
阿里云技术专家邓青琳:云上跨可用区容灾和异地多活最佳实践
Deng Qinglin, a technical expert of Alibaba cloud: Best Practices for disaster recovery and remote multi activity across availability zones on cloud
Yarn 安装与使用教程[通俗易懂]
Baklib:制作优秀的产品说明手册
[cloud native kubernetes] management of secret storage objects under kubernetes cluster
MES管理系统有什么应用价值
The second "future Cup" knowledge map championship was officially launched
Pymoo学习 (6):终止条件
2022 IAA industry category development insight series report - phase II
Fruit chain "siege": it's a journey of sweetness and bitterness next to apples
In the first half of the year, the shipment volume has exceeded that of the whole year of last year, and centritec millimeter wave radar has "captured" the international giant
蓝牙协议详解(蓝牙是什么)
Alibaba cloud free SSL certificate application detailed process
MySQL sub query (selected 20 sub query exercises)
qt exec和show的区别
