-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunix_config.cpp
281 lines (219 loc) · 6.29 KB
/
unix_config.cpp
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
* unix_config.h
*
* Created on: 2011-10-18
* Author: gxl2007@hotmail.com
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include "unix_config.h"
namespace xlnet
{
static const char* node_delimiter_string = " \t\r=\n" ;
unsigned int unix_config::hash(const char *p)
{
unsigned int h = 0;
while(*p)
{
h = h * 11 + (*p << 4) + (*p >> 4);
p++;
}
return h;
}
unix_config::unix_config()
{
memset(m_buckets,0,sizeof(m_buckets)) ;
}
unix_config::~unix_config()
{
clear() ;
}
unix_config::config_node_type* unix_config::get_node(const char* key,config_node_type** pre_node_return)
{
if ( key == NULL ) return NULL ;
unsigned int hash_value = hash(key) ;
unsigned int key_size = strlen(key) ;
if ( key_size > MAX_LINE_SIZE ) return NULL ;
config_node_type* cur_node = m_buckets[ bucket(hash_value) ] ;
config_node_type* pre_node = NULL ;
while( cur_node!=NULL )
{
if ( hash_value == cur_node->hash_value &&
key_size == cur_node->key_size &&
strncmp(cur_node->key,key,key_size)==0 )
{
if ( pre_node_return != NULL) *pre_node_return = pre_node ;
return cur_node ;
}
pre_node = cur_node ;
cur_node = cur_node->next ;
}
return NULL ;
}
const char* unix_config::get(const char* key,const char* default_value)
{
config_node_type* node = get_node(key) ;
if ( node != NULL ) return node->key + node->key_size + 1 ;
return default_value ;
}
int unix_config::get(const char* key,int default_value)
{
const char* v = get(key) ;
if(v == NULL ) return default_value ;
return atoi(v) ;
}
void unix_config::remove(const char* key)
{
config_node_type* pre_node = NULL ;
config_node_type* node = get_node(key,&pre_node) ;
if ( node != NULL )
{
if ( pre_node != NULL )
{
pre_node->next = node->next ;
}
else
{
unsigned int bucket_index = bucket(hash(key)) ;
m_buckets[ bucket_index] = node->next ;
}
free(node) ;
}
}
int unix_config::set(const char* key,const char* value)
{
if ( key == NULL || value == NULL ) return -1 ;
unsigned int key_size = strlen(key) ;
unsigned int value_size = strlen(value) ;
return set(key,key_size,value,value_size) ;
}
int unix_config::set(const char* key,int key_size,const char* value,int value_size)
{
if ( key_size > MAX_LINE_SIZE || value_size > MAX_LINE_SIZE ) return -1 ;
unsigned int node_size = sizeof(config_node_type) + key_size + 1 + value_size + 1 ;
config_node_type* node = (config_node_type*)malloc(node_size) ;
if ( node == NULL ) return -1 ;
node->key_size = key_size ;
strncpy(node->key,key,key_size) ;
node->key[key_size] = '\0' ;
char* node_value = node->key + key_size + 1 ;
strncpy(node_value,value,value_size) ;
node_value[value_size] = '\0' ;
config_node_type* pre_node = NULL ;
config_node_type* old_node = get_node(node->key,&pre_node) ;
if ( old_node != NULL)
{
node->hash_value = old_node->hash_value ;
node->next = old_node->next ;
if ( pre_node == NULL )
{
m_buckets[ bucket(node->hash_value)] = node ;
}
else
{
pre_node->next = node ;
}
free(old_node) ;
}
else
{
node->hash_value = hash(node->key) ;
unsigned int bucket_index = bucket(node->hash_value) ;
node->next = m_buckets[bucket_index] ;
m_buckets[ bucket_index] = node ;
}
return 0 ;
}
int unix_config::load(const char* filename)
{
if ( filename == NULL ) return -1 ;
char line[MAX_LINE_SIZE] = {0} ;
FILE* fp = fopen(filename,"r") ;
if ( fp == NULL ) return -1 ;
while(fgets(line,MAX_LINE_SIZE-1,fp))
{
parse_line(line) ;
}
fclose(fp) ;
return 0 ;
}
int unix_config::parse_line(const char* data)
{
const char *key = NULL ;
const char *val = NULL ;
int key_size = 0 ;
int val_size = 0 ;
const char* p = data ;
p += strspn(p,node_delimiter_string);
if ( *p == '\0' || *p == '#' || *p=='\n' ) return -1 ;
key = p ; /* start of key */
key_size = strcspn(p,node_delimiter_string);
//strncpy(key_data,key,key_size) ;
p += key_size ;
p += strspn(p,node_delimiter_string); /* start of value */
if ( *p == '\0' || *p == '#' || *p=='\n' ) return -1 ;
val = p ;
val_size = strcspn(p,node_delimiter_string);
//strncpy(val_data,val,val_size) ;
p += val_size ;
return set(key,key_size,val,val_size) ;
}
int unix_config::load(const char* data,int size)
{
char line[MAX_LINE_SIZE] = {0} ;
const char* begin = data ;
const char* end = data + size -1 ;
int line_size = 0 ;
for(const char* p = data ; p<= end ; ++p)
{
if(*p == '\n' || p == end )
{
line_size = p - begin +1 ;
strncpy(line,begin,line_size) ;
line[line_size] = '\0' ;
parse_line(line) ;
begin = p +1 ;
}
}
return 0 ;
}
int unix_config::save(const char* filename)
{
int fd = open(filename,O_CREAT | O_WRONLY,0755) ;
if ( fd < 0 ) return -1 ;
dump(fd) ;
close(fd) ;
return 0 ;
}
void unix_config::dump(int fd)
{
char line[MAX_LINE_SIZE] = {0} ;
for(int i = 0 ; i < BUCKETS_SIZE ; ++i )
{
config_node_type* node = m_buckets[i] ;
while(node!=NULL)
{
snprintf(line,sizeof(line),"%s=%s\n",node->key,node->key+node->key_size+1) ;
write(fd,line,strlen(line)) ;
node = node->next ;
}
}
}
void unix_config::clear()
{
for(int i = 0 ; i < BUCKETS_SIZE ; ++i )
{
config_node_type* node = m_buckets[i] ;
m_buckets[i] = NULL ;
while(node!=NULL)
{
config_node_type* next_node = node->next ;
free(node) ;
node = next_node ;
}
}
}
}