From 8c3ca5ec83b3dae9424e1d437724d6bbd4fa4c08 Mon Sep 17 00:00:00 2001 From: brisk Date: Wed, 16 Apr 2014 22:26:28 +0800 Subject: [PATCH] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E8=A7=A3=E6=9E=90=E9=85=8D?= =?UTF-8?q?=E7=BD=AE=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/conf.c | 49 +++++++++++++++++++++++++++++++++++-------------- src/conf.h | 3 +++ src/hash/hash.c | 22 ++++++++++++++++++---- 3 files changed, 56 insertions(+), 18 deletions(-) diff --git a/src/conf.c b/src/conf.c index ff0cbab..286806d 100644 --- a/src/conf.c +++ b/src/conf.c @@ -9,6 +9,8 @@ int parse_value(CONF *conf,char *data,CONF_VALUE **res); int next_line(char *data); //释放内存 void free_data(CONF_ARG *data); +//得到键 +void get_key(char **key,int *index,CONF_ARG *arg); //打开配置文件并初始化值 //path为配置文件路径 @@ -73,20 +75,7 @@ int conf_parse(CONF *conf) { //如果已经解析了则进行释放 if(value) - { - int i=0; - int j; - - while(value[i]) - { - free(value[i].key); - for(j=0;value[i].value[j] != NULL;++i) - free(value[i].value[j]); - - free(value[i]); - ++i; - } - } + free(value); //返回错误 retcode=CONF_NO_MEM; @@ -411,3 +400,35 @@ void free_data(CONF_ARG *data) data=data->next; } } + +char **conf_key_list(CONF *conf) +{ + char **key; + int i; + int index=0; + + //开辟足够的内存空间 + key=malloc(sizeof(char *)*(conf->len+1)); + if(key == NULL) + return NULL; + + key[conf->len]=NULL; + + //读取出所有键 + for(i=0;i < conf->len;++i) + if(conf->hash_data[i].len > 0) + get_key(key,&index,&conf->hash_data[i]); + + return key; +} + +void get_key(char **key,int *index,CONF_ARG *arg) +{ + while(arg != NULL) + { + key[*index]=arg->value->key; + + ++(*index); + arg=arg->next; + } +} diff --git a/src/conf.h b/src/conf.h index cd4bb44..c07506b 100644 --- a/src/conf.h +++ b/src/conf.h @@ -42,6 +42,9 @@ 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); diff --git a/src/hash/hash.c b/src/hash/hash.c index ab4cf4c..7c5f407 100644 --- a/src/hash/hash.c +++ b/src/hash/hash.c @@ -21,7 +21,7 @@ void conf_value_insert(CONF_ARG *arg,CONF_VALUE *value,int len) /* 如果要插入的地方已经有一个值则使用第二个哈希函数计算一个新的哈希值 * 如果新的哈希值所在的位置中数据的个数比第一个哈希值所计算的位置少则使用新的值*/ - if(arg[hash].len > 1) + if(arg[hash].len > 0) { hash2=hash_func2(value->key)%len; if(arg[hash2].len < arg[hash].len) @@ -149,8 +149,22 @@ int hash_search(CONF_ARG *arg,char *key,CONF_VALUE **value) void hash_insert(CONF_ARG *arg,CONF_VALUE *value) { - while(arg->next != NULL) - arg=arg->next; + CONF_ARG *temp; - arg->value=value; + if(arg->len == 1) + { + arg->value=value; + arg->next=NULL; + } + else + { + temp=malloc(sizeof(CONF_ARG)); + temp->value=value; + temp->next=NULL; + + while(arg->next != NULL) + arg=arg->next; + + arg->next=temp; + } }