This repository has been archived by the owner on Mar 19, 2021. It is now read-only.
forked from inteos/pgsql-plugin
-
Notifications
You must be signed in to change notification settings - Fork 7
/
keylist.c
240 lines (196 loc) · 4.81 KB
/
keylist.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
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
/*
* Copyright (c) 2013 by Inteos sp. z o.o.
* All rights reserved. See LICENSE.Inteos for details.
*
* KeyList is a linked list implementation (using dlist from libbac) with searcheable
* key attribute. It is used in utility functions and plugins. List item consist of three
* atributes: key - string, value - string and attrs - number
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "keylist.h"
/*
* prints keylist - for debuging
*/
void print_keylist ( keylist * list ){
keyitem * ki;
foreach_dlist ( ki, list ){
printf ( "%p: K(%p)[%s] V(%p)[%s] A[%i]\n", ki, ki->key, ki->key, ki->value, ki->value, ki->attrs);
}
}
/*
* frees memory allocated for keyitem
*/
void keyitem_free ( keyitem * item ){
if ( item ){
if ( item->key )
free ( item->key );
if ( item->value )
free ( item->value );
// free ( item );
}
}
/*
* create new keyitem element with required values: key, value, attrs,
* without connections to other nodes.
* in:
* key, value and attrs
* out:
* keyitem on success
* NULL on error
*/
keyitem * new_keyitem ( const char * key, const char * value, const int attrs ){
keyitem * item;
/* allocate item */
item = (keyitem *) malloc ( sizeof ( keyitem ) );
if ( ! item ){
/* no memory error? */
return NULL;
}
/* reset all variables */
memset ( item, 0, sizeof ( keyitem ) );
/* produce an item from supplied data */
item->key = key ? bstrdup(key) : NULL;
item->value = value ? bstrdup(value) : NULL;
item->attrs = attrs;
return item;
}
/*
* create a new keylist with supplied keyitem
* in:
* key, value and attrs
* out:
* keylist on success
* NULL on error
*/
keylist * new_keylist ( keyitem * item ){
keylist * list;
/* create a list */
list = New ( keylist ( item, &item->link ) );
if ( ! list ){
/* no memory error? */
keyitem_free(item);
return NULL;
}
return list;
}
/*
* create a new keylist with new keyitem allocated from supplied data
* in:
* key, value and attrs
* out:
* keylist on success
* NULL on error
*/
keylist * new_keylist ( const char * key, const char * value, const int attrs){
keylist * list;
keyitem * item;
/* allocate item */
item = new_keyitem ( key, value, attrs );
if ( ! item ){
/* no memory error? */
return NULL;
}
/* create a list */
list = New ( keylist ( item, &item->link ) );
if ( ! list ){
/* no memory error? */
keyitem_free(item);
return NULL;
}
return list;
}
/*
* appends an item to the list
* if list is null then create a new list
* in:
* keyitem do add
* out:
* old list or newly created
*/
keylist * add_keylist_item ( keylist * list, keyitem * add ){
keylist * nl = list;
if ( ! list ){
nl = New ( keylist ( add, &add->link ) );
}
if ( add ){
nl->append ( add );
}
/* return list or add as a new list */
return nl;
}
/*
* add new keyitem structure do the linked list pointed by list
* in:
* key, value, attrs
* out:
* keylist
*/
keylist * add_keylist_attr ( keylist * list, const char * key, const char * value, const int attrs){
keyitem * item;
keylist * l;
/* allocate item */
item = new_keyitem ( key, value, attrs );
if ( ! item ){
/* no memory error? */
return NULL;
}
l = add_keylist_item ( list, item );
return l;
}
/*
* add new keylist structure do the linked list pointed by list
* in:
* key, value
* out:
* keylist
*/
keylist * add_keylist ( keylist * list, const char * key, const char * value){
keylist * nl;
nl = add_keylist_attr ( list, key, value, KEY_NO_ATTR );
return nl;
}
/*
* frees all nodes of a keylist including every key and value. suplied keylist pointer cannot
* be used.
*/
void keylist_free ( keylist * list ){
keyitem * ki;
if ( list ){
foreach_dlist ( ki, list ){
keyitem_free ( ki );
}
list->destroy();
}
}
/*
* searches for a value of supplied key. it return only first occurance of a key. search is
* not case sensitive, so values of "Key" or "KEY" are returned
*/
char * search_key ( keylist * list, const char * key ){
keyitem * item;
if ( list ){
foreach_dlist ( item, list ){
if ( !strcasecmp ( key, item->key ) ){
return item->value;
}
}
}
return NULL;
}
/*
* searches for a attrs of supplied key. it return only first occurance of a key. search is
* not case sensitive, so attrs of "Key" or "KEY" are returned
*/
int search_key_attr ( keylist * list, const char * key ){
keyitem * item;
if ( list ){
foreach_dlist ( item, list ){
if ( !strcasecmp ( key, item->key ) ){
return item->attrs;
}
}
}
return KEY_NOT_FOUND;
}