-
Notifications
You must be signed in to change notification settings - Fork 0
/
kv.c
109 lines (96 loc) · 1.88 KB
/
kv.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#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;
}