Version 2.0.59
[idzebra-moved-to-github.git] / util / strmap.c
index 8f0b237..c6f1c01 100644 (file)
@@ -1,8 +1,5 @@
-/* $Id: strmap.c,v 1.1 2007-12-02 11:30:28 adam Exp $
-   Copyright (C) 1995-2007
-   Index Data ApS
-
-This file is part of the Zebra server.
+/* This file is part of the Zebra server.
+   Copyright (C) Index Data
 
 Zebra 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
@@ -20,10 +17,14 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
 */
 
+#if HAVE_CONFIG_H
+#include <config.h>
+#endif
 #include <stddef.h>
 #include <string.h>
 #include <zebra_strmap.h>
 #include <yaz/nmem.h>
+#include <yaz/xmalloc.h>
 
 struct strmap_entry {
     char *name;
@@ -31,11 +32,12 @@ struct strmap_entry {
     void *data_buf;
     struct strmap_entry *next;
 };
-    
+
 struct zebra_strmap {
     NMEM nmem_str;
     NMEM nmem_ent;
     int hsize;
+    int size;
     struct strmap_entry **entries;
     struct strmap_entry *free_entries;
 };
@@ -48,6 +50,7 @@ zebra_strmap_t zebra_strmap_create(void)
     st->nmem_ent = nmem_ent;
     st->nmem_str = nmem_create();
     st->hsize = 1001;
+    st->size = 0;
     st->free_entries = 0;
     st->entries = nmem_malloc(nmem_ent, st->hsize * sizeof(*st->entries));
     for (i = 0; i < st->hsize; i++)
@@ -90,6 +93,7 @@ void zebra_strmap_add(zebra_strmap_t st, const char *name,
     ne->data_buf = nmem_malloc(st->nmem_str, data_len);
     memcpy(ne->data_buf, data_buf, data_len);
     ne->data_len = data_len;
+    (st->size)++;
 }
 
 void *zebra_strmap_lookup(zebra_strmap_t st, const char *name, int no,
@@ -122,14 +126,68 @@ int zebra_strmap_remove(zebra_strmap_t st, const char *name)
 
             tmp->next = st->free_entries;
             st->free_entries = tmp;
+
+            --(st->size);
             return 1;
         }
     return 0;
 }
-                        
+
+int zebra_strmap_get_size(zebra_strmap_t st)
+{
+    return st->size;
+}
+
+struct zebra_strmap_it_s {
+    int hno;
+    struct strmap_entry *ent;
+    zebra_strmap_t st;
+
+};
+
+zebra_strmap_it zebra_strmap_it_create(zebra_strmap_t st)
+{
+    zebra_strmap_it it = (zebra_strmap_it) xmalloc(sizeof(*it));
+    it->hno = 0;
+    it->ent = 0;
+    it->st = st;
+    return it;
+}
+
+void zebra_strmap_it_destroy(zebra_strmap_it it)
+{
+    xfree(it);
+}
+
+const char *zebra_strmap_it_next(zebra_strmap_it it, void **data_buf,
+                                 size_t *data_len)
+{
+    struct strmap_entry *ent = 0;
+    while (!it->ent && it->hno < it->st->hsize)
+    {
+        it->ent = it->st->entries[it->hno];
+        it->hno++;
+    }
+    if (it->ent)
+    {
+        ent = it->ent;
+        it->ent = ent->next;
+    }
+    if (ent)
+    {
+        if (data_buf)
+            *data_buf = ent->data_buf;
+        if (data_len)
+            *data_len = ent->data_len;
+        return ent->name;
+    }
+    return 0;
+}
+
 /*
  * Local variables:
  * c-basic-offset: 4
+ * c-file-style: "Stroustrup"
  * indent-tabs-mode: nil
  * End:
  * vim: shiftwidth=4 tabstop=8 expandtab