当前位置:网站首页>[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

边栏推荐
- 【加密周报】加密市场有所回温?寒冬仍未解冻!盘点上周加密市场发生的重大事件!
- 【小程序开发】你了解小程序开发吗?
- Modelsim and quartus jointly simulate PLL FIFO and other IP cores
- C 调的满级和玄
- 软件测试流程(思维导图)
- Everyone can participate in the official launch of open source activities. We sincerely invite you to experience!
- 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
- Huawei recruited "talented teenagers" twice this year; 5.4 million twitter account information was leaked, with a selling price of $30000; Google fired engineers who believed in AI consciousness | gee
猜你喜欢

The degree of interval of basic music theory

聊聊sql优化的15个小技巧

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

Everyone can participate in the official launch of open source activities. We sincerely invite you to experience!

Care for front-line epidemic prevention workers, Haocheng JIAYE and Gaomidian sub district office jointly build the great wall of public welfare

聊聊接口性能优化的11个小技巧

阿里云技术专家秦隆:可靠性保障必备——云上如何进行混沌工程?

Ultimate doll 2.0 | cloud native delivery package

SQL Server 2019 安装教程

2022 IAA industry category development insight series report - phase II
随机推荐
Pyqt5 click qtableview vertical header to get row data and click cell to get row data
Modelsim and quartus jointly simulate PLL FIFO and other IP cores
Huawei recruited "talented teenagers" twice this year; 5.4 million twitter account information was leaked, with a selling price of $30000; Google fired engineers who believed in AI consciousness | gee
基于Mysql-Exporter监控Mysql
乐理基础 调式
2022 robocom provincial competition solution
HTTP缓存通天篇,可能有你想要的
浅析IM即时通讯开发出现上网卡顿?网络掉线?
基于FPGA的1080P 60Hz BT1120接口调试过程记录
【iniparser】项目配置工具iniparser的简单使用
The difference between QT exec and show
Yarn installation and use tutorial [easy to understand]
How to prohibit the use of 360 browser (how to disable the built-in browser)
怎么禁止使用360浏览器(怎么才能把自带的浏览器停用)
Weak network test tool -qnet
【919. 完全二叉树插入器】
A free image download warehouse website
A brief history from object detection to image segmentation
Gan, why ".Length! == 3??
软件测试(思维导图)
