summaryrefslogtreecommitdiff
path: root/src/common.c
diff options
context:
space:
mode:
authorPřemysl Janouch <p.janouch@gmail.com>2014-07-14 02:06:02 +0200
committerPřemysl Janouch <p.janouch@gmail.com>2014-07-14 02:06:02 +0200
commitc7cd0c40e06c013b490c2f9f3939189503e9b060 (patch)
treee2489cd89399bb4ddab6c545bb3d8a0c8e32a249 /src/common.c
parent3206c864301431ea3c9024f84e50f2274e8deb0f (diff)
downloadxK-c7cd0c40e06c013b490c2f9f3939189503e9b060.tar.gz
xK-c7cd0c40e06c013b490c2f9f3939189503e9b060.tar.xz
xK-c7cd0c40e06c013b490c2f9f3939189503e9b060.zip
Add support for custom str_map key comp. fun.
That wasn't hard.
Diffstat (limited to 'src/common.c')
-rw-r--r--src/common.c8
1 files changed, 6 insertions, 2 deletions
diff --git a/src/common.c b/src/common.c
index d905929..2907159 100644
--- a/src/common.c
+++ b/src/common.c
@@ -571,6 +571,9 @@ struct str_map
size_t alloc; ///< Number of allocated entries
size_t len; ///< Number of entries in the table
void (*free) (void *); ///< Callback to destruct the payload
+
+ /// Callback to compare keys for equivalence
+ int (*key_cmp) (const char *, const char *);
};
// As long as you don't remove the current entry, you can modify the map.
@@ -593,6 +596,7 @@ str_map_init (struct str_map *self)
self->alloc = STR_MAP_MIN_ALLOC;
self->len = 0;
self->free = NULL;
+ self->key_cmp = strcmp;
self->map = xcalloc (self->alloc, sizeof *self->map);
}
@@ -692,7 +696,7 @@ str_map_set (struct str_map *self, const char *key, void *value)
struct str_map_link *iter = self->map[pos];
for (; iter; iter = iter->next)
{
- if (strcmp (key, iter->key))
+ if (self->key_cmp (key, iter->key))
continue;
// Storing the same data doesn't destroy it
@@ -741,7 +745,7 @@ str_map_find (struct str_map *self, const char *key)
{
struct str_map_link *iter = self->map[str_map_pos (self, key)];
for (; iter; iter = iter->next)
- if (!strcmp (key, (char *) iter + sizeof *iter))
+ if (!self->key_cmp (key, (const char *) iter + sizeof *iter))
return iter->data;
return NULL;
}