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
|
/*
dict.c - dictionary functions
This file is part of the nss-ldapd library.
Copyright (C) 2007 Arthur de Jong
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include "dict.h"
struct dict_entry {
const char *key;
void *value;
struct dict_entry *next;
};
struct dictionary {
struct dict_entry *head;
struct dict_entry *ptr; /* for searching */
};
static struct dict_entry *dict_entry_new(const char *key)
{
struct dict_entry *entry;
entry=(struct dict_entry *)malloc(sizeof(struct dict_entry));
if (entry==NULL)
return NULL;
entry->key=strdup(key);
if (entry->key==NULL)
{
free(entry);
return NULL;
}
entry->value=NULL;
entry->next=NULL;
return entry;
}
static void dict_entry_free(struct dict_entry *entry)
{
/* free key */
free((void *)entry->key);
/* free entry */
free(entry);
}
static struct dict_entry *dict_entry_find(
DICT *dict,const char *key)
{
struct dict_entry *ptr;
for (ptr=dict->head;ptr!=NULL;ptr=ptr->next)
{
if (strcasecmp(ptr->key,key)==0)
return ptr;
}
return NULL;
}
DICT *dict_new(void)
{
struct dictionary *dict;
dict=(struct dictionary *)malloc(sizeof(struct dictionary));
if (dict==NULL)
return NULL;
dict->head=NULL;
dict->ptr=NULL;
return dict;
}
int dict_put(DICT *dict,const char *key,void *value)
{
struct dict_entry *entry;
if (key==NULL)
return -1;
entry=dict_entry_find(dict,key);
if ((entry==NULL)&&(value==NULL))
{
/* the key didn't exist, no point in creating one */
return 0;
}
else if (entry==NULL)
{
/* create new entry and insert it in the list */
entry=dict_entry_new(key);
if (entry==NULL)
return -1;
/* insert entry in list */
entry->next=dict->head;
dict->head=entry;
}
/* set value */
entry->value=value;
return 0;
}
void *dict_get(DICT *dict,const char *key)
{
struct dict_entry *entry;
entry=dict_entry_find(dict,key);
if (entry==NULL)
return NULL;
return entry->value;
}
void dict_free(DICT *dict)
{
struct dict_entry *ptr,*nxt;
/* free all entries */
ptr=dict->head;
while (ptr!=NULL)
{
nxt=ptr->next;
dict_entry_free(ptr);
ptr=nxt;
}
/* clear some references */
dict->head=NULL;
dict->ptr=NULL;
/* free struct itself */
free(dict);
}
void dict_values_first(DICT *dict)
{
dict->ptr=dict->head;
}
void *dict_values_next(DICT *dict)
{
struct dict_entry *ptr;
ptr=dict->ptr;
/* search until we get a non-NULL value */
while ((ptr!=NULL)&&(ptr->value==NULL))
ptr=ptr->next;
/* we went through the whole list */
if (ptr==NULL)
{
dict->ptr=NULL;
return NULL;
}
dict->ptr=ptr->next;
return ptr->value;
}
|