更新底层操作不可见

master
brisk 2014-05-05 15:21:33 +08:00
parent 4e8bb9a0cf
commit 1b45a77a89
5 changed files with 97 additions and 100 deletions

View File

@ -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/

View File

@ -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;
}

View File

@ -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 <stdio.h>
#include <stdlib.h>
#include <unistd.h>
@ -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的数据结构
* fpFILE
* 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

View File

@ -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)
{

View File

@ -2,48 +2,9 @@
#ifndef _HASH_H
#define _HASH_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*值数据结构*/
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的数据结构
* fpFILE
* 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