myconfconf 配置文件解析庫(kù)
myconf是一個(gè)可以方便讀取key-value配置的C/C++庫(kù)。
1 規(guī)則如下
1)、配置為 key : value 格式
2)、可以支持讀取int(正負(fù)數(shù)), unsigend int, char *, 支持默認(rèn)缺省值。
3)、string如果有特殊字符(如空格等)需要加上""(如"你 好");
4)、使用完必須調(diào)用關(guān)閉日志文件句柄,否則有內(nèi)存泄漏。
5)、需要使用mylog編譯依賴, 編譯主要修改Makefile置頂工作目錄WORKROOT,然后通過(guò)makefile.env指定MYLOG、MYCONF的路徑
2 API
1) 初始化日志句柄
MY_CONF_INS* my_conf_init(const char* file_path, const char* file_name);
file_path: 配置目錄
file_name: 配置文件
2) 獲得int
MY_CONF_GET_INT32(key, confs, input_value)
key :獲得key
confs: 配置文件句柄
input_value : 配置返回的結(jié)果引用
注意:成功返回true,否則返回false
3) 缺省獲得int
MY_CONF_GET_INT32_DEFAULT(key, confs, input_value, default_value)
key :獲得key
confs: 配置文件句柄
input_value : 配置返回的結(jié)果引用
default_value :如果沒(méi)有結(jié)果,返回default_value
注意:成功返回true,否則返回false
4) 獲得unsigned int
MY_CONF_GET_UINT32(key, confs, input_value)
key :獲得key
confs: 配置文件句柄
input_value : 配置返回的結(jié)果引用
注意:成功返回true,否則返回false
5) 缺省獲得unsigned int
MY_CONF_GET_INT32_DEFAULT(key, confs, input_value, default_value)
key :獲得key
confs: 配置文件句柄
input_value : 配置返回的結(jié)果引用
default_value :如果沒(méi)有結(jié)果,返回default_value
注意:成功返回true,否則返回false
6) 獲得string
MY_CONF_GET_STR(key, confs, input_str)
key :獲得key
confs: 配置文件句柄
input_str : 配置返回的結(jié)果引用
注意:成功返回true,否則返回false, input_str必須申請(qǐng)初始化大小
7) 缺省獲得string
MY_CONF_GET_STR_DEFAULT(key, confs, input_str, default_str)
key :獲得key
confs: 配置文件句柄
input_str : 配置返回的結(jié)果引用
default_str : 如果沒(méi)有結(jié)果返回缺省值
注意:成功返回true,否則返回false, input_str必須申請(qǐng)初始化大小
3 示例
1) 代碼
#include "mylog.h"
#include "myconf.h"
#include
using namespace std;
int main()
{
my_log_init("./log", "sample.log", "sample.log.wf", 16);
MY_LOG_DEBUG("main begin");
MY_CONF_INS* my_ins = my_conf_init("./conf", "sample.conf");
if( my_ins == NULL)
{
MY_LOG_FATAL("file is not exist or not vaild");
return -1;
}
bool ret;
//get int
int int_a1;
ret = MY_CONF_GET_INT32("int_a", my_ins, int_a1);
if(ret == true)
{
cout << "int_a1 is " << int_a1 << endl;
}
else
{
cout << "get int_a1 fail";
}
//get uint32
uint32 uint32_b1;
ret = MY_CONF_GET_UINT32("int_b", my_ins, uint32_b1);
cout << "uint32_b1 is " << uint32_b1 << endl;
//get int 缺省值
uint32 int_a2;
ret = MY_CONF_GET_UINT32_DEFAULT("int_a_default", my_ins, int_a2, 100);
cout << "int_a_default " << int_a2 << endl;
char string_noraml[1024];
ret = MY_CONF_GET_STR("string_noraml", my_ins, string_noraml);
cout << "string_noraml is " << string_noraml << endl;
char string_quoto[1024];
ret = MY_CONF_GET_STR("string_quoto", my_ins, string_quoto);
cout << "string_quoto is " << string_quoto << endl;
my_conf_close(my_ins);
MY_LOG_DEBUG("main end");
return 0;
}
b) 運(yùn)行結(jié)果
