File: dict.c

package info (click to toggle)
ladish 1%2Bdfsg0-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 3,940 kB
  • sloc: ansic: 36,406; python: 11,237; cpp: 705; makefile: 22; ruby: 20; sh: 17
file content (223 lines) | stat: -rw-r--r-- 5,352 bytes parent folder | download | duplicates (3)
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
/* -*- Mode: C ; c-basic-offset: 2 -*- */
/*
 * LADI Session Handler (ladish)
 *
 * Copyright (C) 2009, 2010 Nedko Arnaudov <nedko@arnaudov.name>
 *
 **************************************************************************
 * This file contains the implementation of the dictionary objects
 **************************************************************************
 *
 * LADI Session Handler is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * LADI Session Handler 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with LADI Session Handler. If not, see <http://www.gnu.org/licenses/>
 * or write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "dict.h"

struct ladish_dict_entry
{
  struct list_head siblings;
  char * key;
  char * value;
};

struct ladish_dict
{
  struct list_head entries;
};

bool ladish_dict_create(ladish_dict_handle * dict_handle_ptr)
{
  struct ladish_dict * dict_ptr;

  dict_ptr = malloc(sizeof(struct ladish_dict));
  if (dict_ptr == NULL)
  {
    log_error("malloc() failed to allocate struct ladish_dict");
    return false;
  }

  INIT_LIST_HEAD(&dict_ptr->entries);

  *dict_handle_ptr = (ladish_dict_handle)dict_ptr;

  return true;
}

static struct ladish_dict_entry * ladish_dict_find_key(struct ladish_dict * dict_ptr, const char * key)
{
  struct list_head * node_ptr;
  struct ladish_dict_entry * entry_ptr;

  list_for_each(node_ptr, &dict_ptr->entries)
  {
    entry_ptr = list_entry(node_ptr, struct ladish_dict_entry, siblings);
    if (strcmp(entry_ptr->key, key) == 0)
    {
      return entry_ptr;
    }
  }

  return NULL;
}

static void ladish_dict_drop_entry(struct ladish_dict_entry * entry_ptr)
{
  list_del(&entry_ptr->siblings);
  free(entry_ptr->key);
  free(entry_ptr->value);
  free(entry_ptr);
}

#define dict_ptr ((struct ladish_dict *)dict_handle)

void ladish_dict_destroy(ladish_dict_handle dict_handle)
{
  ladish_dict_clear(dict_handle);
  free(dict_ptr);
}

bool ladish_dict_set(ladish_dict_handle dict_handle, const char * key, const char * value)
{
  struct ladish_dict_entry * entry_ptr;
  char * new_value;

  entry_ptr = ladish_dict_find_key(dict_ptr, key);
  if (entry_ptr != NULL)
  {
    new_value = strdup(value);
    if (new_value == NULL)
    {
      log_error("strdup() failed to duplicate dict value");
      return false;
    }

    free(entry_ptr->value);
    entry_ptr->value = new_value;
    return true;
  }

  entry_ptr = malloc(sizeof(struct ladish_dict_entry));
  if (entry_ptr == NULL)
  {
    log_error("malloc() failed to allocate struct ladish_dict_entry");
    return false;
  }

  entry_ptr->key = strdup(key);
  if (entry_ptr->key == NULL)
  {
    log_error("strdup() failed to duplicate dict key");
    free(entry_ptr);
    return false;
  }

  entry_ptr->value = strdup(value);
  if (entry_ptr->value == NULL)
  {
    log_error("strdup() failed to duplicate dict value");
    free(entry_ptr->key);
    free(entry_ptr);
    return false;
  }

  list_add_tail(&entry_ptr->siblings, &dict_ptr->entries);

  return true;
}

const char * ladish_dict_get(ladish_dict_handle dict_handle, const char * key)
{
  struct ladish_dict_entry * entry_ptr;

  entry_ptr = ladish_dict_find_key(dict_ptr, key);
  if (entry_ptr == NULL)
  {
    return NULL;
  }

  ASSERT(entry_ptr->value != NULL);
  return entry_ptr->value;
}

void ladish_dict_drop(ladish_dict_handle dict_handle, const char * key)
{
  struct ladish_dict_entry * entry_ptr;

  entry_ptr = ladish_dict_find_key(dict_ptr, key);
  if (entry_ptr != NULL)
  {
    ladish_dict_drop_entry(entry_ptr);
  }
}

void ladish_dict_clear(ladish_dict_handle dict_handle)
{
  struct ladish_dict_entry * entry_ptr;

  while (!list_empty(&dict_ptr->entries))
  {
    entry_ptr = list_entry(dict_ptr->entries.next, struct ladish_dict_entry, siblings);
    ladish_dict_drop_entry(entry_ptr);
  }
}

bool ladish_dict_iterate(ladish_dict_handle dict_handle, void * context, bool (* callback)(void * context, const char * key, const char * value))
{
  struct list_head * node_ptr;
  struct ladish_dict_entry * entry_ptr;

  list_for_each(node_ptr, &dict_ptr->entries)
  {
    entry_ptr = list_entry(node_ptr, struct ladish_dict_entry, siblings);
    if (!callback(context, entry_ptr->key, entry_ptr->value))
    {
      return false;
    }
  }

  return true;
}

bool ladish_dict_is_empty(ladish_dict_handle dict_handle)
{
  return list_empty(&dict_ptr->entries);
}

#undef dict_ptr

static bool dup_key(void * context, const char * key, const char * value)
{
  return ladish_dict_set(context, key, value);
}

bool ladish_dict_dup(ladish_dict_handle src, ladish_dict_handle * dst_ptr)
{
  ladish_dict_handle dst;

  if (!ladish_dict_create(&dst))
  {
    return false;
  }

  if (!ladish_dict_iterate(src, dst, dup_key))
  {
    ladish_dict_destroy(dst);
    return false;
  }

  *dst_ptr = dst;
  return true;
}