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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
|
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*-
nautilus-string-map.c: A many-to-one string mapping data structure.
Copyright (C) 1999, 2000 Eazel, Inc.
This program 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.
This program 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 this program; if not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
Author: Ramiro Estrugo <ramiro@eazel.com>
*/
#include <config.h>
#include "nautilus-string-map.h"
#include "nautilus-string-list.h"
#include "nautilus-string.h"
#include "nautilus-glib-extensions.h"
#include "nautilus-lib-self-check-functions.h"
#include <string.h>
struct _NautilusStringMap
{
GList *map;
gboolean case_sensitive;
};
typedef struct
{
char *string;
NautilusStringList *map_list;
} MapEntry;
/* MapEntry things */
static MapEntry *map_entry_new (const char *string,
gboolean case_sensitive);
static void map_entry_free (MapEntry *map_entry);
static MapEntry *map_entry_list_lookup_mapped_string (GList *entry_list,
const char *mapped_string,
gboolean case_sensitive);
static MapEntry *map_entry_list_lookup (GList *entry_list,
const char *string);
static gboolean str_is_equal (const char *a,
const char *b,
gboolean case_sensitive);
/**
* nautilus_string_map_new:
*
* @case_sensitive: Boolean flag indicating whether the string map is case sensitive.
*
* Allocate an empty string map.
*
* Return value: A newly allocated string map.
*/
NautilusStringMap *
nautilus_string_map_new (gboolean case_sensitive)
{
NautilusStringMap * string_map;
string_map = g_new (NautilusStringMap, 1);
string_map->map = NULL;
string_map->case_sensitive = case_sensitive;
return string_map;
}
/**
* nautilus_string_map_free:
*
* @string_map: A NautilusStringMap
*
* Free the string map.
*/
void
nautilus_string_map_free (NautilusStringMap *string_map)
{
if (string_map == NULL) {
return;
}
nautilus_string_map_clear (string_map);
g_free (string_map);
}
static void
map_for_each_node_free (gpointer data,
gpointer user_data)
{
MapEntry *map_entry = (MapEntry *) data;
g_assert (map_entry != NULL);
map_entry_free (map_entry);
}
/**
* nautilus_string_map_clear:
*
* @string_map: A NautilusStringMap
*
* Clear the string map.
*/
void
nautilus_string_map_clear (NautilusStringMap *string_map)
{
if (string_map == NULL) {
return;
}
g_return_if_fail (string_map != NULL);
nautilus_g_list_free_deep_custom (string_map->map, map_for_each_node_free, NULL);
string_map->map = NULL;
}
/**
* nautilus_string_map_lookup:
*
* @string_map: A NautilusStringMap
* @string: The mapped string to lookup
*
* Lookup the mapping for 'string'
*
* Return value: A copy of the mapped_to_string or NULL if no mapping exists.
*/
char *
nautilus_string_map_lookup (const NautilusStringMap *string_map,
const char *string)
{
MapEntry *map_entry;
if (string_map == NULL || string == NULL) {
return NULL;
}
map_entry = map_entry_list_lookup (string_map->map, string);
return map_entry ? g_strdup (map_entry->string) : NULL;
}
/**
* nautilus_string_map_add:
*
* @string_map: A NautilusStringMap
*
* Add a mapping from 'string' to 'strings_maps_to'
*/
void
nautilus_string_map_add (NautilusStringMap *string_map,
const char *string_maps_to,
const char *string)
{
MapEntry *map_entry;
g_return_if_fail (string_map != NULL);
g_return_if_fail (string_maps_to != NULL);
g_return_if_fail (string != NULL);
map_entry = map_entry_list_lookup_mapped_string (string_map->map, string_maps_to, string_map->case_sensitive);
if (map_entry == NULL) {
map_entry = map_entry_new (string_maps_to, string_map->case_sensitive);
/* Add a mapping for the string_maps_to to simplify things */
nautilus_string_list_insert (map_entry->map_list, string_maps_to);
string_map->map = g_list_append (string_map->map, map_entry);
}
nautilus_string_list_insert (map_entry->map_list, string);
}
/* MapEntry things */
static MapEntry *
map_entry_new (const char *string,
gboolean case_sensitive)
{
MapEntry *map_entry;
g_return_val_if_fail (string != NULL, NULL);
map_entry = g_new (MapEntry, 1);
map_entry->map_list = nautilus_string_list_new (case_sensitive);
map_entry->string = g_strdup (string);
return map_entry;
}
static void
map_entry_free (MapEntry *map_entry)
{
g_return_if_fail (map_entry);
nautilus_string_list_free (map_entry->map_list);
map_entry->map_list = NULL;
g_free (map_entry->string);
g_free (map_entry);
}
static MapEntry *
map_entry_list_lookup_mapped_string (GList *entry_list,
const char *mapped_string,
gboolean case_sensitive)
{
GList *iterator;
g_return_val_if_fail (mapped_string != NULL, NULL);
if (entry_list == NULL) {
return NULL;
}
for (iterator = entry_list; iterator != NULL; iterator = iterator->next) {
MapEntry *map_entry = (MapEntry *) iterator->data;
g_assert (map_entry != NULL);
if (str_is_equal (map_entry->string, mapped_string, case_sensitive)) {
return map_entry;
}
}
return NULL;
}
static MapEntry *
map_entry_list_lookup (GList *entry_list,
const char *string)
{
GList *iterator;
g_return_val_if_fail (string != NULL, NULL);
if (entry_list == NULL) {
return NULL;
}
for (iterator = entry_list; iterator != NULL; iterator = iterator->next) {
MapEntry *map_entry = (MapEntry *) iterator->data;
g_assert (map_entry != NULL);
if (nautilus_string_list_contains (map_entry->map_list, string)) {
return map_entry;
}
}
return NULL;
}
static gboolean
str_is_equal (const char *a,
const char *b,
gboolean case_sensitive)
{
return case_sensitive ? nautilus_str_is_equal (a, b) : nautilus_istr_is_equal (a, b);
}
#if !defined (NAUTILUS_OMIT_SELF_CHECK)
void
nautilus_self_check_string_map (void)
{
NautilusStringMap *map;
map = nautilus_string_map_new (TRUE);
NAUTILUS_CHECK_STRING_RESULT (nautilus_string_map_lookup (map, "foo"), NULL);
nautilus_string_map_clear (map);
nautilus_string_map_add (map, "animal", "dog");
nautilus_string_map_add (map, "animal", "cat");
nautilus_string_map_add (map, "animal", "mouse");
nautilus_string_map_add (map, "human", "geek");
nautilus_string_map_add (map, "human", "nerd");
nautilus_string_map_add (map, "human", "lozer");
NAUTILUS_CHECK_STRING_RESULT (nautilus_string_map_lookup (map, "animal"), "animal");
NAUTILUS_CHECK_STRING_RESULT (nautilus_string_map_lookup (map, "cat"), "animal");
NAUTILUS_CHECK_STRING_RESULT (nautilus_string_map_lookup (map, "dog"), "animal");
NAUTILUS_CHECK_STRING_RESULT (nautilus_string_map_lookup (map, "mouse"), "animal");
NAUTILUS_CHECK_STRING_RESULT (nautilus_string_map_lookup (map, "human"), "human");
NAUTILUS_CHECK_STRING_RESULT (nautilus_string_map_lookup (map, "geek"), "human");
NAUTILUS_CHECK_STRING_RESULT (nautilus_string_map_lookup (map, "nerd"), "human");
NAUTILUS_CHECK_STRING_RESULT (nautilus_string_map_lookup (map, "lozer"), "human");
NAUTILUS_CHECK_STRING_RESULT (nautilus_string_map_lookup (map, "Lozer"), NULL);
NAUTILUS_CHECK_STRING_RESULT (nautilus_string_map_lookup (map, "HuMaN"), NULL);
nautilus_string_map_free (map);
/*
* case insensitive tests
*
*/
map = nautilus_string_map_new (FALSE);
NAUTILUS_CHECK_STRING_RESULT (nautilus_string_map_lookup (map, "foo"), NULL);
nautilus_string_map_clear (map);
nautilus_string_map_add (map, "animal", "dog");
nautilus_string_map_add (map, "animal", "cat");
nautilus_string_map_add (map, "animal", "mouse");
NAUTILUS_CHECK_STRING_RESULT (nautilus_string_map_lookup (map, "animal"), "animal");
NAUTILUS_CHECK_STRING_RESULT (nautilus_string_map_lookup (map, "Animal"), "animal");
NAUTILUS_CHECK_STRING_RESULT (nautilus_string_map_lookup (map, "cat"), "animal");
NAUTILUS_CHECK_STRING_RESULT (nautilus_string_map_lookup (map, "CAT"), "animal");
NAUTILUS_CHECK_STRING_RESULT (nautilus_string_map_lookup (map, "dog"), "animal");
NAUTILUS_CHECK_STRING_RESULT (nautilus_string_map_lookup (map, "DoG"), "animal");
nautilus_string_map_free (map);
}
#endif /* !NAUTILUS_OMIT_SELF_CHECK */
|