]> git.notmuchmail.org Git - notmuch/blobdiff - lib/string-map.c
emacs: Add new option notmuch-search-hide-excluded
[notmuch] / lib / string-map.c
index 591ff6d5bdc525d5b507d30c12f8d41921c29502..99bc2ea2450fe7a188f527106d5f45fb711f8f10 100644 (file)
@@ -14,7 +14,7 @@
  * 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, see http://www.gnu.org/licenses/ .
+ * along with this program.  If not, see https://www.gnu.org/licenses/ .
  *
  * Author: David Bremner <david@tethera.net>
  */
@@ -33,14 +33,14 @@ typedef struct _notmuch_string_pair_t {
 } notmuch_string_pair_t;
 
 struct _notmuch_string_map {
-    notmuch_bool_t sorted;
+    bool sorted;
     size_t length;
     notmuch_string_pair_t *pairs;
 };
 
 struct _notmuch_string_map_iterator {
     notmuch_string_pair_t *current;
-    notmuch_bool_t exact;
+    bool exact;
     const char *key;
 };
 
@@ -55,7 +55,7 @@ _notmuch_string_map_create (const void *ctx)
 
     map->length = 0;
     map->pairs = NULL;
-    map->sorted = TRUE;
+    map->sorted = true;
 
     return map;
 }
@@ -67,7 +67,7 @@ _notmuch_string_map_append (notmuch_string_map_t *map,
 {
 
     map->length++;
-    map->sorted = FALSE;
+    map->sorted = false;
 
     if (map->pairs)
        map->pairs = talloc_realloc (map, map->pairs, notmuch_string_pair_t, map->length + 1);
@@ -86,10 +86,14 @@ _notmuch_string_map_append (notmuch_string_map_t *map,
 static int
 cmppair (const void *pa, const void *pb)
 {
+    int cmp = 0;
     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);
+    cmp = strcmp (a->key, b->key);
+    if (cmp == 0)
+       cmp = strcmp (a->value, b->value);
+    return cmp;
 }
 
 static void
@@ -103,11 +107,11 @@ _notmuch_string_map_sort (notmuch_string_map_t *map)
 
     qsort (map->pairs, map->length, sizeof (notmuch_string_pair_t), cmppair);
 
-    map->sorted = TRUE;
+    map->sorted = true;
 }
 
-static notmuch_bool_t
-string_cmp (const char *a, const char *b, notmuch_bool_t exact)
+static int
+string_cmp (const char *a, const char *b, bool exact)
 {
     if (exact)
        return (strcmp (a, b));
@@ -116,7 +120,7 @@ string_cmp (const char *a, const char *b, notmuch_bool_t exact)
 }
 
 static notmuch_string_pair_t *
-bsearch_first (notmuch_string_pair_t *array, size_t len, const char *key, notmuch_bool_t exact)
+bsearch_first (notmuch_string_pair_t *array, size_t len, const char *key, bool exact)
 {
     size_t first = 0;
     size_t last = len - 1;
@@ -143,6 +147,24 @@ bsearch_first (notmuch_string_pair_t *array, size_t len, const char *key, notmuc
 
 }
 
+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)
+       _notmuch_string_map_append (map, key, val);
+    else {
+       talloc_free (pair->value);
+       pair->value = talloc_strdup (map->pairs, val);
+    }
+}
+
 const char *
 _notmuch_string_map_get (notmuch_string_map_t *map, const char *key)
 {
@@ -151,7 +173,7 @@ _notmuch_string_map_get (notmuch_string_map_t *map, const char *key)
     /* this means that calling append invalidates iterators */
     _notmuch_string_map_sort (map);
 
-    pair = bsearch_first (map->pairs, map->length, key, TRUE);
+    pair = bsearch_first (map->pairs, map->length, key, true);
     if (! pair)
        return NULL;
 
@@ -160,7 +182,7 @@ _notmuch_string_map_get (notmuch_string_map_t *map, const char *key)
 
 notmuch_string_map_iterator_t *
 _notmuch_string_map_iterator_create (notmuch_string_map_t *map, const char *key,
-                                    notmuch_bool_t exact)
+                                    bool exact)
 {
     notmuch_string_map_iterator_t *iter;
 
@@ -170,21 +192,24 @@ _notmuch_string_map_iterator_create (notmuch_string_map_t *map, const char *key,
     if (unlikely (iter == NULL))
        return NULL;
 
+    if (unlikely (talloc_reference (iter, map) == NULL))
+       return NULL;
+
     iter->key = talloc_strdup (iter, key);
     iter->exact = exact;
     iter->current = bsearch_first (map->pairs, map->length, key, exact);
     return iter;
 }
 
-notmuch_bool_t
+bool
 _notmuch_string_map_iterator_valid (notmuch_string_map_iterator_t *iterator)
 {
     if (iterator->current == NULL)
-       return FALSE;
+       return false;
 
     /* sentinel */
     if (iterator->current->key == NULL)
-       return FALSE;
+       return false;
 
     return (0 == string_cmp (iterator->key, iterator->current->key, iterator->exact));