2016-06-13 03:05:49 +02:00
|
|
|
/* string-map.c - associative arrays of strings
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Copyright © 2016 David Bremner
|
|
|
|
*
|
|
|
|
* 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 3 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
|
2018-04-29 10:35:30 +02:00
|
|
|
* along with this program. If not, see https://www.gnu.org/licenses/ .
|
2016-06-13 03:05:49 +02:00
|
|
|
*
|
|
|
|
* Author: David Bremner <david@tethera.net>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "notmuch-private.h"
|
|
|
|
|
|
|
|
/* Create a new notmuch_string_map_t object, with 'ctx' as its
|
|
|
|
* talloc owner.
|
|
|
|
*
|
|
|
|
* This function can return NULL in case of out-of-memory.
|
|
|
|
*/
|
|
|
|
|
|
|
|
typedef struct _notmuch_string_pair_t {
|
|
|
|
char *key;
|
|
|
|
char *value;
|
|
|
|
} notmuch_string_pair_t;
|
|
|
|
|
|
|
|
struct _notmuch_string_map {
|
2017-10-07 10:44:05 +02:00
|
|
|
bool sorted;
|
2016-06-13 03:05:49 +02:00
|
|
|
size_t length;
|
|
|
|
notmuch_string_pair_t *pairs;
|
|
|
|
};
|
|
|
|
|
2016-06-13 03:05:51 +02:00
|
|
|
struct _notmuch_string_map_iterator {
|
|
|
|
notmuch_string_pair_t *current;
|
2017-10-07 10:44:05 +02:00
|
|
|
bool exact;
|
2016-06-13 03:05:51 +02:00
|
|
|
const char *key;
|
|
|
|
};
|
|
|
|
|
2016-06-13 03:05:49 +02:00
|
|
|
notmuch_string_map_t *
|
|
|
|
_notmuch_string_map_create (const void *ctx)
|
|
|
|
{
|
|
|
|
notmuch_string_map_t *map;
|
|
|
|
|
|
|
|
map = talloc (ctx, notmuch_string_map_t);
|
|
|
|
if (unlikely (map == NULL))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
map->length = 0;
|
|
|
|
map->pairs = NULL;
|
2017-10-07 10:44:05 +02:00
|
|
|
map->sorted = true;
|
2016-06-13 03:05:49 +02:00
|
|
|
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_notmuch_string_map_append (notmuch_string_map_t *map,
|
|
|
|
const char *key,
|
|
|
|
const char *value)
|
|
|
|
{
|
|
|
|
|
|
|
|
map->length++;
|
2017-10-07 10:44:05 +02:00
|
|
|
map->sorted = false;
|
2016-06-13 03:05:49 +02:00
|
|
|
|
|
|
|
if (map->pairs)
|
|
|
|
map->pairs = talloc_realloc (map, map->pairs, notmuch_string_pair_t, map->length + 1);
|
|
|
|
else
|
|
|
|
map->pairs = talloc_array (map, notmuch_string_pair_t, map->length + 1);
|
|
|
|
|
|
|
|
map->pairs[map->length - 1].key = talloc_strdup (map, key);
|
|
|
|
map->pairs[map->length - 1].value = talloc_strdup (map, value);
|
|
|
|
|
|
|
|
/* Add sentinel */
|
|
|
|
map->pairs[map->length].key = NULL;
|
|
|
|
map->pairs[map->length].value = NULL;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
cmppair (const void *pa, const void *pb)
|
|
|
|
{
|
|
|
|
notmuch_string_pair_t *a = (notmuch_string_pair_t *) pa;
|
|
|
|
notmuch_string_pair_t *b = (notmuch_string_pair_t *) pb;
|
|
|
|
|
|
|
|
return strcmp (a->key, b->key);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
_notmuch_string_map_sort (notmuch_string_map_t *map)
|
|
|
|
{
|
|
|
|
if (map->length == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (map->sorted)
|
|
|
|
return;
|
|
|
|
|
|
|
|
qsort (map->pairs, map->length, sizeof (notmuch_string_pair_t), cmppair);
|
|
|
|
|
2017-10-07 10:44:05 +02:00
|
|
|
map->sorted = true;
|
2016-06-13 03:05:49 +02:00
|
|
|
}
|
|
|
|
|
2019-03-02 19:10:51 +01:00
|
|
|
static int
|
2017-10-07 10:44:05 +02:00
|
|
|
string_cmp (const char *a, const char *b, bool exact)
|
2016-06-13 03:05:49 +02:00
|
|
|
{
|
|
|
|
if (exact)
|
|
|
|
return (strcmp (a, b));
|
|
|
|
else
|
|
|
|
return (strncmp (a, b, strlen (a)));
|
|
|
|
}
|
|
|
|
|
|
|
|
static notmuch_string_pair_t *
|
2017-10-07 10:44:05 +02:00
|
|
|
bsearch_first (notmuch_string_pair_t *array, size_t len, const char *key, bool exact)
|
2016-06-13 03:05:49 +02:00
|
|
|
{
|
|
|
|
size_t first = 0;
|
|
|
|
size_t last = len - 1;
|
|
|
|
size_t mid;
|
|
|
|
|
|
|
|
if (len <= 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
while (last > first) {
|
|
|
|
mid = (first + last) / 2;
|
|
|
|
int sign = string_cmp (key, array[mid].key, exact);
|
|
|
|
|
|
|
|
if (sign <= 0)
|
|
|
|
last = mid;
|
|
|
|
else
|
|
|
|
first = mid + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (string_cmp (key, array[first].key, exact) == 0)
|
|
|
|
return array + first;
|
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-12-20 22:10:53 +01:00
|
|
|
void
|
|
|
|
_notmuch_string_map_set (notmuch_string_map_t *map,
|
|
|
|
const char *key,
|
|
|
|
const char *val)
|
|
|
|
{
|
|
|
|
notmuch_string_pair_t *pair;
|
|
|
|
|
|
|
|
/* this means that calling string_map_set invalidates iterators */
|
|
|
|
_notmuch_string_map_sort (map);
|
|
|
|
pair = bsearch_first (map->pairs, map->length, key, true);
|
|
|
|
if (! pair)
|
2021-03-13 13:45:34 +01:00
|
|
|
_notmuch_string_map_append (map, key, val);
|
2020-12-20 22:10:53 +01:00
|
|
|
else {
|
2021-03-13 13:45:34 +01:00
|
|
|
talloc_free (pair->value);
|
|
|
|
pair->value = talloc_strdup (map->pairs, val);
|
2020-12-20 22:10:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-13 03:05:49 +02:00
|
|
|
const char *
|
|
|
|
_notmuch_string_map_get (notmuch_string_map_t *map, const char *key)
|
|
|
|
{
|
|
|
|
notmuch_string_pair_t *pair;
|
|
|
|
|
|
|
|
/* this means that calling append invalidates iterators */
|
|
|
|
_notmuch_string_map_sort (map);
|
|
|
|
|
2017-10-07 10:44:05 +02:00
|
|
|
pair = bsearch_first (map->pairs, map->length, key, true);
|
2016-06-13 03:05:49 +02:00
|
|
|
if (! pair)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return pair->value;
|
|
|
|
}
|
2016-06-13 03:05:51 +02:00
|
|
|
|
|
|
|
notmuch_string_map_iterator_t *
|
|
|
|
_notmuch_string_map_iterator_create (notmuch_string_map_t *map, const char *key,
|
2017-10-07 10:44:05 +02:00
|
|
|
bool exact)
|
2016-06-13 03:05:51 +02:00
|
|
|
{
|
|
|
|
notmuch_string_map_iterator_t *iter;
|
|
|
|
|
|
|
|
_notmuch_string_map_sort (map);
|
|
|
|
|
|
|
|
iter = talloc (map, notmuch_string_map_iterator_t);
|
|
|
|
if (unlikely (iter == NULL))
|
|
|
|
return NULL;
|
|
|
|
|
2016-09-23 11:33:19 +02:00
|
|
|
if (unlikely (talloc_reference (iter, map) == NULL))
|
|
|
|
return NULL;
|
|
|
|
|
2016-06-13 03:05:51 +02:00
|
|
|
iter->key = talloc_strdup (iter, key);
|
|
|
|
iter->exact = exact;
|
|
|
|
iter->current = bsearch_first (map->pairs, map->length, key, exact);
|
|
|
|
return iter;
|
|
|
|
}
|
|
|
|
|
2017-10-07 10:44:05 +02:00
|
|
|
bool
|
2016-06-13 03:05:51 +02:00
|
|
|
_notmuch_string_map_iterator_valid (notmuch_string_map_iterator_t *iterator)
|
|
|
|
{
|
|
|
|
if (iterator->current == NULL)
|
2017-10-07 10:44:05 +02:00
|
|
|
return false;
|
2016-06-13 03:05:51 +02:00
|
|
|
|
|
|
|
/* sentinel */
|
|
|
|
if (iterator->current->key == NULL)
|
2017-10-07 10:44:05 +02:00
|
|
|
return false;
|
2016-06-13 03:05:51 +02:00
|
|
|
|
|
|
|
return (0 == string_cmp (iterator->key, iterator->current->key, iterator->exact));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_notmuch_string_map_iterator_move_to_next (notmuch_string_map_iterator_t *iterator)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (! _notmuch_string_map_iterator_valid (iterator))
|
|
|
|
return;
|
|
|
|
|
|
|
|
(iterator->current)++;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
_notmuch_string_map_iterator_key (notmuch_string_map_iterator_t *iterator)
|
|
|
|
{
|
|
|
|
if (! _notmuch_string_map_iterator_valid (iterator))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return iterator->current->key;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char *
|
|
|
|
_notmuch_string_map_iterator_value (notmuch_string_map_iterator_t *iterator)
|
|
|
|
{
|
|
|
|
if (! _notmuch_string_map_iterator_valid (iterator))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return iterator->current->value;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_notmuch_string_map_iterator_destroy (notmuch_string_map_iterator_t *iterator)
|
|
|
|
{
|
|
|
|
talloc_free (iterator);
|
|
|
|
}
|