总结
如果只是把它当做一个工具,其实很简单,后面可以在这个上面添砖加瓦,增加一个自己需要的功能进去
克隆
[root@vbox tmp]# git clone https://gitee.com/brisk/conf-c
Cloning into 'conf-c'...
remote: Enumerating objects: 212, done.
remote: Total 212 (delta 0), reused 0 (delta 0), pack-reused 212 (from 1)
Receiving objects: 100% (212/212), 37.17 KiB | 594.00 KiB/s, done.
Resolving deltas: 100% (96/96), done.
[root@vbox tmp]# pwd
/tmp
[root@vbox conf-c]# ls
example LICENSE Makefile README.md src 更新日志
[root@vbox conf-c]# cd src/
[root@vbox src]# ls
conf.c conf.h hash stack
[root@vbox src]# pwd
/tmp/conf-c/src
[root@vbox src]# ls
conf.c conf.h hash stack
[root@vbox src]# ls
conf.c conf.h hash stack
[root@vbox src]# cp -r * root/ulility_private/include/
[root@vbox src]#
测试用例
//包含头文件
#include "conf.h"
voidprint_key(char **key)
{
int i=0;
printf("all key is:\n");
//遍历
while(key[i] != NULL)
{
printf("key : %s\n",key[i]);
++i;
}
}
voidprint_all(CONF_VALUE **value)
{
int i;
int j;
printf("all:\n");
//遍历
for(i=0;value[i] != NULL;++i)
{
j=0;
printf("key:%s ",value[i]->key);
while(value[i]->value[j] != NULL)
{
printf(" value:%s",value[i]->value[j]);
++j;
}
printf("\n");
}
}
intmain(int argc,char **argv)
{
CONF *conf; //需要的数据结构
CONF_VALUE *value; //键/值参数数据结构
char **key; //获取所有键
CONF_VALUE **list; //获取所有键/值参数
int code; //返回的错误代码
//打开并初始化数据结构
//出错时返回NULL
if((conf=conf_open("configrc")) == NULL)
return-1;
//开始解析配置文件
//成功时返回0
//出错时返回错误代码
//错误代码可使用conf_error函数打印错误信息
if((code=conf_parse(conf)) != 0)
{
conf_error(code);
return-2;
}
//取出一个参数
value=conf_value_get(conf,"arg1");
//如果有该参数时返回
//否则返回一个NULL
if(value)
printf("arg1 is %s\n",value->value[0]);
//多参数
value=conf_value_get(conf,"arg2");
if(value)
printf("arg2 is %s %s\n",value->value[0],value->value[1]);
//有空白符的参数
value=conf_value_get(conf,"arg3");
if(value)
printf("arg3 is %s\n",value->value[0]);
//特殊符号
value=conf_value_get(conf,"arg4");
if(value)
printf("arg4 is %s\n",value->value[0]);
//多参数加特殊符号
value=conf_value_get(conf,"arg5");
if(value)
printf("arg5 is %s %s %s\n",value->value[0],value->value[1],value->value[2]);
//得到当前配置文件中键的个数
printf("key has %d\n",conf_count(conf));
//得到所有键
//出错时返回NULL
key=conf_key_list(conf);
if(key)
print_key(key);
//得到所有键/值参数
//出错时返回NULL
list=conf_value_get_all(conf);
if(list)
print_all(list);
//释放内存
conf_free(conf);
return0;
}
编译
要把所有的C文件全部编译进去
[root@vbox conf]# gcc read.c ../include/conf.../include/hash/hash.c ../include/stack/stack.c -o tmp/read -I ../include
conf.c conf.h
[root@vbox conf]# gcc read.c ../include/conf.c ../include/hash/hash.c ../include/stack/stack.c -o tmp/read -I ../include
[root@vbox conf]# tmp/read
arg1 is abc
arg2 is abc def
arg3 is abc def
arg4 is abc ' def
arg5 is abc,def #this is value hello
key has 5
all key is:
key : arg1
key : arg2
key : arg3
key : arg4
key : arg5
all:
key:arg1 value:abc
key:arg2 value:abc value:def
key:arg3 value:abc def
key:arg4 value:abc ' def
key:arg5 value:abc,def value:#this is value value:hello
[root@vbox conf]# pwd
/root/ulility_private/conf
[root@vbox conf]# ll
total 32
-rw-r--r--. 1 root root 16805 Mar 2816:14 conf.c
-rw-r--r--. 1 root root 374 Mar 2815:52 configrc
-rw-r--r--. 1 root root 2034 Mar 2816:07 read.c
-rw-r--r--. 1 root root 58 Mar 2816:05 readme.txt
[root@vbox conf]# tmp/read
arg1 is abc
arg2 is abc def
arg3 is abc def
arg4 is abc ' def
arg5 is abc,def #this is value hello
key has 5
all key is:
key : arg1
key : arg2
key : arg3
key : arg4
key : arg5
all:
key:arg1 value:abc
key:arg2 value:abc value:def
key:arg3 value:abc def
key:arg4 value:abc ' def
key:arg5 value:abc,def value:#this is value value:hello
[root@vbox conf]#
参考
conf-c: 基于C语言的轻量级读取/创建配置文件的函数库
文章转载自SmallDB,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




