diff --git a/Makefile b/Makefile index 974bcae..12f27cd 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,8 @@ all:libconf.a libconf.a:conf.o stack.o hash.o - mkdir -p include/conf-c include/conf-c/hash include/conf-c/stack lib + mkdir -p include/conf-c lib cp src/conf.h include/conf-c/ - cp src/stack/stack.h include/conf-c/stack/ - cp src/hash/hash.h include/conf-c/hash/ ar rcs libconf.a conf.o stack.o hash.o cp libconf.a lib/ diff --git a/src/conf.c b/src/conf.c index c62cab2..8f76f4f 100644 --- a/src/conf.c +++ b/src/conf.c @@ -1,4 +1,5 @@ -#include "conf.h" +#include "hash/hash.h" +#include "stack/stack.h" //解析对参数 //data为配置文件内容 @@ -442,3 +443,57 @@ void get_key(char **key,int *index,CONF_ARG *arg) arg=arg->next; } } + +CONF_VALUE *conf_value_get(CONF *conf,const char *key) +{ + int hash; + CONF_VALUE *value; + + /* 首先使用第一个哈希函数计算出哈希值,如果找到匹配的key则返回 + * 否则使用第二个哈希函数计算值并比对key + * 如果都未能匹配,则返回NULL */ + hash=hash_func1(key)%conf->len; + if(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)) + return value; + } + + return NULL; +} + +CONF_VALUE **conf_value_get_all(CONF *conf) +{ + CONF_VALUE **value; + int len=0; + int index=0; + int i; + + if(conf->len == 0) + return NULL; + + //第一次扫描数组中所有存在数据的地方并计算长度 + for(i=0;i != conf->len;++i) + if(conf->hash_data[i].len > 0) + ++len; + + //动态申请内存存储返回的数据 + value=malloc(sizeof(CONF_VALUE *)*(len+1)); + if(value == NULL) + return NULL; + value[len]=NULL; + //第二次扫描,返回数组中所有有数据的值 + for(i=0;i != conf->len;++i) + { + if(conf->hash_data[i].len > 0) + { + value[index]=conf->hash_data[i].value; + ++index; + } + } + + return value; +} diff --git a/src/conf.h b/src/conf.h index c00c2f5..015f0f5 100644 --- a/src/conf.h +++ b/src/conf.h @@ -7,8 +7,8 @@ #define _CONF_H /*配置文件中健/值以hash的方式存储在内存中*/ -#include "hash/hash.h" -#include "stack/stack.h" +//#include "hash/hash.h" +//#include "stack/stack.h" #include #include #include @@ -21,6 +21,37 @@ #define CONF_KEY_ERR -4 //键错误 #define CONF_VALUE_ERR -5 //值错误 +/*值数据结构*/ +typedef struct node +{ + 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为键 @@ -74,4 +105,10 @@ 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 diff --git a/src/hash/hash.c b/src/hash/hash.c index 82805dc..d4a88a6 100644 --- a/src/hash/hash.c +++ b/src/hash/hash.c @@ -38,60 +38,6 @@ void conf_value_insert(CONF_ARG *arg,CONF_VALUE *value,int len) //arg[hash].value=value; } -CONF_VALUE *conf_value_get(CONF *conf,const char *key) -{ - int hash; - CONF_VALUE *value; - - /* 首先使用第一个哈希函数计算出哈希值,如果找到匹配的key则返回 - * 否则使用第二个哈希函数计算值并比对key - * 如果都未能匹配,则返回NULL */ - hash=hash_func1(key)%conf->len; - if(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)) - return value; - } - - return NULL; -} - -CONF_VALUE **conf_value_get_all(CONF *conf) -{ - CONF_VALUE **value; - int len=0; - int index=0; - int i; - - if(conf->len == 0) - return NULL; - - //第一次扫描数组中所有存在数据的地方并计算长度 - for(i=0;i != conf->len;++i) - if(conf->hash_data[i].len > 0) - ++len; - - //动态申请内存存储返回的数据 - value=malloc(sizeof(CONF_VALUE *)*(len+1)); - if(value == NULL) - return NULL; - value[len]=NULL; - //第二次扫描,返回数组中所有有数据的值 - for(i=0;i != conf->len;++i) - { - if(conf->hash_data[i].len > 0) - { - value[index]=conf->hash_data[i].value; - ++index; - } - } - - return value; -} - /*BKDR 哈希算法*/ unsigned int hash_func1(const char *key) { diff --git a/src/hash/hash.h b/src/hash/hash.h index 8e07b2b..3b23572 100644 --- a/src/hash/hash.h +++ b/src/hash/hash.h @@ -2,48 +2,9 @@ #ifndef _HASH_H #define _HASH_H -#include -#include -#include - -/*值数据结构*/ -typedef struct node -{ - 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; +#include "../conf.h" //插入数据,arg为hash数组,value为值,len为数组长度 void conf_value_insert(CONF_ARG *arg,CONF_VALUE *value,int len); -//根据一个键查找数据 -CONF_VALUE *conf_value_get(CONF *conf,const char *key); - -//得到所有键值对 -CONF_VALUE **conf_value_get_all(CONF *conf); - #endif