函数加前缀
parent
c4153d1719
commit
b46d38bb39
|
|
@ -0,0 +1,114 @@
|
|||
/* conf-c一个基于c语言的轻量级读取/创建配置文件的库函数。
|
||||
* 该库使用MIT许可协议,被授权人有权利有使用、复制、修改、合并、出版发布、散布、再授权和/或贩售软件及软件的副本,及授予被供应人同等权利,惟服从以下义务。
|
||||
* 在软件和软件的所有副本中都必须包含以上版权声明和本许可声明。
|
||||
*/
|
||||
|
||||
#ifndef _CONF_H
|
||||
#define _CONF_H
|
||||
|
||||
/*配置文件中健/值以hash的方式存储在内存中*/
|
||||
//#include "hash/hash.h"
|
||||
//#include "stack/stack.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
//定义错误代码
|
||||
#define CONF_OK 0 //操作成功
|
||||
#define CONF_NO_DATA -1 //没有数据
|
||||
#define CONF_NO_INIT -2 //未初始化
|
||||
#define CONF_NO_MEM -3 //申请内存出错
|
||||
#define CONF_KEY_ERR -4 //键错误
|
||||
#define CONF_VALUE_ERR -5 //值错误
|
||||
|
||||
/*值数据结构*/
|
||||
typedef struct conf_value
|
||||
{
|
||||
char *key;
|
||||
char **value;
|
||||
}CONF_VALUE;
|
||||
|
||||
/* 键/值对数据结构
|
||||
* len为当前结点个数
|
||||
* next为冲突时的链表
|
||||
*/
|
||||
typedef struct arg
|
||||
{
|
||||
int len;
|
||||
CONF_VALUE *value;
|
||||
|
||||
struct arg *next;
|
||||
}CONF_ARG;
|
||||
|
||||
/* conf-c的数据结构
|
||||
* fp为指向配置文件的FILE指针
|
||||
* len为键/值对个数
|
||||
* hash_data为存储键/值的数据结构
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
FILE *fp;
|
||||
int len;
|
||||
CONF_ARG *hash_data;
|
||||
}CONF;
|
||||
|
||||
//保存配置文件数据结构
|
||||
//len为当前参数个数
|
||||
//key为键
|
||||
//value值
|
||||
//note为注释
|
||||
typedef struct creater
|
||||
{
|
||||
FILE *fp;
|
||||
int len;
|
||||
|
||||
char *key;
|
||||
char *value;
|
||||
char *note;
|
||||
|
||||
struct creater *next;
|
||||
}CONF_CREATER;
|
||||
//打开并初始化,path为配置文件的路经,返回CONF数据
|
||||
CONF *conf_open(const char *path);
|
||||
|
||||
//解析配置文件,解析正确返回0,出错返回小于0的错误代码
|
||||
int conf_parse(CONF *conf);
|
||||
|
||||
//获取当前配置文件中键值对个数
|
||||
int conf_count(CONF *conf);
|
||||
|
||||
//得到所有键
|
||||
char **conf_key_list(CONF *conf);
|
||||
|
||||
//创建一个配置文件,path为配置文件的路经
|
||||
CONF_CREATER *conf_creater_new(const char *path);
|
||||
|
||||
/* 添加一个配置键/值对
|
||||
* value为键值对
|
||||
* note为注释内容
|
||||
* 文件中以#开头
|
||||
* 返回值:
|
||||
* 正确时返回0
|
||||
* 错误时返回小于0的错误代码
|
||||
*/
|
||||
int conf_insert(CONF_CREATER *creater,char *key,char *value,char *note);
|
||||
|
||||
//保存配置文件,正确时返回0,错误时返回小于0的错误代码
|
||||
int conf_save(CONF_CREATER *creater);
|
||||
|
||||
//释放内存
|
||||
void conf_free(CONF *conf);
|
||||
|
||||
//释放CONF_CREATER内存
|
||||
void conf_creater_free(CONF_CREATER *creater);
|
||||
|
||||
//打印错误信息函数,errcode为错误代码
|
||||
void conf_error(int errcode);
|
||||
|
||||
//根据一个键查找数据
|
||||
CONF_VALUE *conf_value_get(CONF *conf,const char *key);
|
||||
|
||||
//得到所有键值对
|
||||
CONF_VALUE **conf_value_get_all(CONF *conf);
|
||||
|
||||
#endif
|
||||
44
src/conf.c
44
src/conf.c
|
|
@ -275,7 +275,7 @@ int parse_value(CONF *conf,char *data,CONF_VALUE **res)
|
|||
CONF_VALUE *value=NULL; //存放键值参数
|
||||
STACK stack; //存放参数的栈
|
||||
|
||||
stack_init(&stack);
|
||||
conf_stack_init(&stack);
|
||||
|
||||
while(data[i])
|
||||
{
|
||||
|
|
@ -287,17 +287,17 @@ int parse_value(CONF *conf,char *data,CONF_VALUE **res)
|
|||
value=realloc(value,sizeof(CONF_VALUE)*(len+1));
|
||||
if(value == NULL)
|
||||
return CONF_NO_MEM;
|
||||
value[len].key=malloc(sizeof(char)*stack_length(&stack)+1);
|
||||
value[len].key=malloc(sizeof(char)*conf_stack_length(&stack)+1);
|
||||
if(value[len].key == NULL)
|
||||
return CONF_NO_MEM;
|
||||
snprintf(value[len].key,sizeof(char)*stack_length(&stack)+1,"%s",stack.data);
|
||||
snprintf(value[len].key,sizeof(char)*conf_stack_length(&stack)+1,"%s",stack.data);
|
||||
key=1;
|
||||
value[len].value=NULL;
|
||||
stack_cleanup(&stack);
|
||||
conf_stack_cleanup(&stack);
|
||||
}
|
||||
else
|
||||
{
|
||||
if(stack_push(&stack,data[i]) != 0) //否则则是值压入栈
|
||||
if(conf_stack_push(&stack,data[i]) != 0) //否则则是值压入栈
|
||||
return STACK_MAX;
|
||||
}
|
||||
break;
|
||||
|
|
@ -312,7 +312,7 @@ int parse_value(CONF *conf,char *data,CONF_VALUE **res)
|
|||
flags=0;
|
||||
else if(flags == 2)
|
||||
{
|
||||
if(stack_push(&stack,data[i]) != 0)
|
||||
if(conf_stack_push(&stack,data[i]) != 0)
|
||||
return STACK_MAX;
|
||||
}
|
||||
break;
|
||||
|
|
@ -323,7 +323,7 @@ int parse_value(CONF *conf,char *data,CONF_VALUE **res)
|
|||
flags=0;
|
||||
else if(flags == 1)
|
||||
{
|
||||
if(stack_push(&stack,data[i]) != 0)
|
||||
if(conf_stack_push(&stack,data[i]) != 0)
|
||||
return STACK_MAX;
|
||||
}
|
||||
break;
|
||||
|
|
@ -334,7 +334,7 @@ int parse_value(CONF *conf,char *data,CONF_VALUE **res)
|
|||
*/
|
||||
case ',':
|
||||
if(flags)
|
||||
stack_push(&stack,data[i]);
|
||||
conf_stack_push(&stack,data[i]);
|
||||
if(!flags && !arg)
|
||||
arg=1;
|
||||
if(arg) //存入多参数
|
||||
|
|
@ -342,12 +342,12 @@ int parse_value(CONF *conf,char *data,CONF_VALUE **res)
|
|||
value[len].value=realloc(value[len].value,sizeof(char *)*(count+1));
|
||||
if(value[len].value == NULL)
|
||||
return CONF_NO_MEM;
|
||||
value[len].value[count]=malloc(sizeof(char)*stack_length(&stack)+1);
|
||||
value[len].value[count]=malloc(sizeof(char)*conf_stack_length(&stack)+1);
|
||||
if(value[len].value[count] == NULL)
|
||||
return CONF_NO_MEM;
|
||||
snprintf(value[len].value[count],sizeof(char)*stack_length(&stack)+1,"%s",stack.data);
|
||||
snprintf(value[len].value[count],sizeof(char)*conf_stack_length(&stack)+1,"%s",stack.data);
|
||||
++count;
|
||||
stack_cleanup(&stack);
|
||||
conf_stack_cleanup(&stack);
|
||||
arg=0;
|
||||
}
|
||||
break;
|
||||
|
|
@ -356,7 +356,7 @@ int parse_value(CONF *conf,char *data,CONF_VALUE **res)
|
|||
i+=next_line(data+i);
|
||||
else
|
||||
{
|
||||
if(stack_push(&stack,data[i]) != 0)
|
||||
if(conf_stack_push(&stack,data[i]) != 0)
|
||||
return STACK_MAX;
|
||||
}
|
||||
break;
|
||||
|
|
@ -364,28 +364,28 @@ int parse_value(CONF *conf,char *data,CONF_VALUE **res)
|
|||
case '\t':
|
||||
if(flags)
|
||||
{
|
||||
if(stack_push(&stack,data[i]) != 0)
|
||||
if(conf_stack_push(&stack,data[i]) != 0)
|
||||
return STACK_MAX;
|
||||
}
|
||||
break;
|
||||
case '\n': //一行数据读完
|
||||
if(stack_empty(&stack))
|
||||
if(conf_stack_empty(&stack))
|
||||
break;
|
||||
value[len].value=realloc(value[len].value,sizeof(char *)*(count+2));
|
||||
if(value[len].value == NULL)
|
||||
return CONF_NO_MEM;
|
||||
value[len].value[count]=malloc(sizeof(char)*stack_length(&stack)+1);
|
||||
value[len].value[count]=malloc(sizeof(char)*conf_stack_length(&stack)+1);
|
||||
if(value[len].value[count] == NULL)
|
||||
return CONF_NO_MEM;
|
||||
snprintf(value[len].value[count],sizeof(char)*stack_length(&stack)+1,"%s",stack.data);
|
||||
snprintf(value[len].value[count],sizeof(char)*conf_stack_length(&stack)+1,"%s",stack.data);
|
||||
value[len].value[count+1]=NULL;
|
||||
count=0;
|
||||
++len;
|
||||
key=0;
|
||||
stack_cleanup(&stack);
|
||||
conf_stack_cleanup(&stack);
|
||||
break;
|
||||
default: //插入字符
|
||||
if(stack_push(&stack,data[i]) != 0)
|
||||
if(conf_stack_push(&stack,data[i]) != 0)
|
||||
return STACK_MAX;
|
||||
}
|
||||
|
||||
|
|
@ -464,13 +464,13 @@ CONF_VALUE *conf_value_get(CONF *conf,const char *key)
|
|||
/* 首先使用第一个哈希函数计算出哈希值,如果找到匹配的key则返回
|
||||
* 否则使用第二个哈希函数计算值并比对key
|
||||
* 如果都未能匹配,则返回NULL */
|
||||
hash=hash_func1(key)%conf->len;
|
||||
if(hash_search(&conf->hash_data[hash],key,&value))
|
||||
hash=conf_hash_func1(key)%conf->len;
|
||||
if(conf_hash_search(&conf->hash_data[hash],key,&value))
|
||||
return value;
|
||||
else
|
||||
{
|
||||
hash=hash_func2(key)%conf->len;
|
||||
if(hash_search(&conf->hash_data[hash],key,&value))
|
||||
hash=conf_hash_func2(key)%conf->len;
|
||||
if(conf_hash_search(&conf->hash_data[hash],key,&value))
|
||||
return value;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,14 +1,14 @@
|
|||
#include "hash.h"
|
||||
|
||||
/*两个计算哈希值的哈希函数*/
|
||||
unsigned int hash_func1(const char *key);
|
||||
unsigned int hash_func2(const char *key);
|
||||
unsigned int conf_hash_func1(const char *key);
|
||||
unsigned int conf_hash_func2(const char *key);
|
||||
/* 在hash_data中找出key
|
||||
* 如果找到则将值赋给value并返回1
|
||||
* 否则返回0 */
|
||||
int hash_search(CONF_ARG *arg,char *key,CONF_VALUE **value);
|
||||
int conf_hash_search(CONF_ARG *arg,char *key,CONF_VALUE **value);
|
||||
//插入数据
|
||||
void hash_insert(CONF_ARG *arg,CONF_VALUE *value);
|
||||
void conf_hash_insert(CONF_ARG *arg,CONF_VALUE *value);
|
||||
|
||||
void conf_value_insert(CONF_ARG *arg,CONF_VALUE *value,int len)
|
||||
{
|
||||
|
|
@ -17,13 +17,13 @@ void conf_value_insert(CONF_ARG *arg,CONF_VALUE *value,int len)
|
|||
int hash,hash2;
|
||||
|
||||
//默认使用第一个哈希函数
|
||||
hash=hash2=hash_func1(value->key)%len;
|
||||
hash=hash2=conf_hash_func1(value->key)%len;
|
||||
|
||||
/* 如果要插入的地方已经有一个值则使用第二个哈希函数计算一个新的哈希值
|
||||
* 如果新的哈希值所在的位置中数据的个数比第一个哈希值所计算的位置少则使用新的值*/
|
||||
if(arg[hash].len > 0)
|
||||
{
|
||||
hash2=hash_func2(value->key)%len;
|
||||
hash2=conf_hash_func2(value->key)%len;
|
||||
if(arg[hash2].len < arg[hash].len)
|
||||
hash=hash2;
|
||||
}
|
||||
|
|
@ -33,13 +33,13 @@ void conf_value_insert(CONF_ARG *arg,CONF_VALUE *value,int len)
|
|||
//插入数据
|
||||
/*while(arg[hash].next != NULL)
|
||||
arg[hash]=arg[hash].next;*/
|
||||
hash_insert(&arg[hash],value);
|
||||
conf_hash_insert(&arg[hash],value);
|
||||
|
||||
//arg[hash].value=value;
|
||||
}
|
||||
|
||||
/*BKDR 哈希算法*/
|
||||
unsigned int hash_func1(const char *key)
|
||||
unsigned int conf_hash_func1(const char *key)
|
||||
{
|
||||
unsigned int seed=131;
|
||||
unsigned int hash=0;
|
||||
|
|
@ -55,7 +55,7 @@ unsigned int hash_func1(const char *key)
|
|||
}
|
||||
|
||||
/*ELF 哈希算法*/
|
||||
unsigned int hash_func2(const char *key)
|
||||
unsigned int conf_hash_func2(const char *key)
|
||||
{
|
||||
unsigned int hash=0;
|
||||
unsigned int x=0;
|
||||
|
|
@ -75,7 +75,7 @@ unsigned int hash_func2(const char *key)
|
|||
return hash&0x7FFFFFFF;
|
||||
}
|
||||
|
||||
int hash_search(CONF_ARG *arg,char *key,CONF_VALUE **value)
|
||||
int conf_hash_search(CONF_ARG *arg,char *key,CONF_VALUE **value)
|
||||
{
|
||||
//如果该位置没有数据返回0
|
||||
if(arg->len == 0)
|
||||
|
|
@ -98,7 +98,7 @@ int hash_search(CONF_ARG *arg,char *key,CONF_VALUE **value)
|
|||
return 0;
|
||||
}
|
||||
|
||||
void hash_insert(CONF_ARG *arg,CONF_VALUE *value)
|
||||
void conf_hash_insert(CONF_ARG *arg,CONF_VALUE *value)
|
||||
{
|
||||
CONF_ARG *temp;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
#include "stack.h"
|
||||
|
||||
//初始化栈
|
||||
void stack_init(STACK *stack)
|
||||
void conf_stack_init(STACK *stack)
|
||||
{
|
||||
//栈置空
|
||||
stack->len=0;
|
||||
|
|
@ -10,7 +10,7 @@ void stack_init(STACK *stack)
|
|||
|
||||
//插入字符到栈中,data为插入的字符
|
||||
//成功时返回0,出错时返回错误代码
|
||||
int stack_push(STACK *stack,char data)
|
||||
int conf_stack_push(STACK *stack,char data)
|
||||
{
|
||||
if(stack->len >= MAX_ARG_SIZE)
|
||||
return STACK_MAX;
|
||||
|
|
@ -22,13 +22,13 @@ int stack_push(STACK *stack,char data)
|
|||
}
|
||||
|
||||
//获取当前栈中字符个数
|
||||
int stack_length(STACK *stack)
|
||||
int conf_stack_length(STACK *stack)
|
||||
{
|
||||
return stack->len;
|
||||
}
|
||||
|
||||
//判断当前栈是否为空
|
||||
int stack_empty(STACK *stack)
|
||||
int conf_stack_empty(STACK *stack)
|
||||
{
|
||||
if(stack->len == 0)
|
||||
return 1;
|
||||
|
|
@ -37,7 +37,7 @@ int stack_empty(STACK *stack)
|
|||
}
|
||||
|
||||
//清理栈数据
|
||||
void stack_cleanup(STACK *stack)
|
||||
void conf_stack_cleanup(STACK *stack)
|
||||
{
|
||||
stack_init(stack);
|
||||
conf_stack_init(stack);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -18,17 +18,17 @@ typedef struct
|
|||
}STACK;
|
||||
|
||||
//初始化栈结构
|
||||
void stack_init(STACK *stack);
|
||||
void conf_stack_init(STACK *stack);
|
||||
|
||||
//插入字符
|
||||
int stack_push(STACK *stack,char data);
|
||||
int conf_stack_push(STACK *stack,char data);
|
||||
|
||||
//得到当前字符长度
|
||||
int stack_length(STACK *stack);
|
||||
int conf_stack_length(STACK *stack);
|
||||
|
||||
//判断当前本是否为空
|
||||
int stack_empty(STACK *stack);
|
||||
int conf_stack_empty(STACK *stack);
|
||||
|
||||
//清理栈数据结构
|
||||
void stack_cleanup(STACK *stack);
|
||||
void conf_stack_cleanup(STACK *stack);
|
||||
#endif
|
||||
|
|
|
|||
Loading…
Reference in New Issue