加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
kv.c 1.88 KB
一键复制 编辑 原始数据 按行查看 历史
thor 提交于 2013-12-12 07:58 . 有颜色了
#include "kv.h"
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
kv_tbl_t *kv_tbl_init()
{
kv_tbl_t *table = malloc(sizeof(kv_tbl_t));
table->len = 0;
return table;
}
void kv_tbl_destroy(kv_tbl_t *table)
{
free(table);
}
kv_t *kv_tbl_lookup(kv_tbl_t *table, char *key)
{
int i;
for (i = 0; i < table->len; i++) {
if (strcmp(key, table->kvs[i].key) == 0) {
return &table->kvs[i];
}
}
return NULL;
}
kv_t *kv_tbl_lookup_whtpx(kv_tbl_t *table, char *key)
{
int i;
int len = strlen(key);
for (i = 0; i < table->len; i++) {
kv_t *kv = &table->kvs[i];
int yes = 1, j;
for (j = 0; j < len; j++) {
if (kv->key[j] != key[j]) {
yes = 0;
break;
}
}
if (yes && (kv->key[len] == '~' || kv->key[len] == 0))
return kv;
}
return NULL;
}
int kv_tbl_contains_whtpx(kv_tbl_t *table, char *key)
{
int i;
int len = strlen(key);
for (i = 0; i < table->len; i++) {
kv_t *kv = &table->kvs[i];
int yes = 1, j;
for (j = 0; j < len; j++) {
if (kv->key[j] != key[j]) {
yes = 0;
break;
}
}
if (yes && (kv->key[len] == '~' || kv->key[len] == 0))
return 1;
}
return 0;
}
kv_t *kv_tbl_insert(kv_tbl_t *table, char *key, int intval, char *strval)
{
int len = table->len;
if (len + 1 >= MAX_KV_NUM) return NULL;
strcpy(table->kvs[len].key, key);
if (strval != NULL)
strcpy(table->kvs[len].strval, strval);
table->kvs[len].intval = intval;
table->len += 1;
return &table->kvs[len];
}
int kv_tbl_contains(kv_tbl_t *table, char *key)
{
int i;
for (i = 0; i < table->len; i++) {
if (strcmp(key, table->kvs[i].key) == 0)
return 1;
}
return 0;
}
int kv_tbl_remove(kv_tbl_t *table, char *key)
{
int len = table->len;
int i;
for (i = 0; i < len; i++) {
if (strcmp(table->kvs[i].key, key) == 0) {
int j;
for (j = i; j < len - 1; j++) {
table->kvs[j] = table->kvs[j+1];
}
table->len -= 1;
return 1;
}
}
return 0;
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化