Support header for 2016.
[cascardo/rnetclient.git] / pmhash.c
1 /*
2  *  Copyright (C) 2012-2013  Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 3 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License along
15  *  with this program; if not, write to the Free Software Foundation, Inc.,
16  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19 #include "pmhash.h"
20 #include <stdlib.h>
21 #include <string.h>
22
23 struct item {
24         char *key;
25         void *val;
26 };
27
28 struct pmhash {
29         size_t len;
30         struct item items[];
31 };
32
33 struct pmhash * pmhash_new(void)
34 {
35         struct pmhash *pmhash;
36         size_t len = 128;
37         pmhash = malloc(sizeof(*pmhash) + len * sizeof(struct item));
38         if (!pmhash)
39                 return NULL;
40         pmhash->len = len;
41         memset(pmhash->items, 0, len * sizeof(struct item));
42         return pmhash;
43 }
44
45 int pmhash_add(struct pmhash **pmhash, char *key, void *val)
46 {
47         unsigned int i;
48         struct pmhash *hash = *pmhash;
49         i = 0;
50 repeat:
51         for (; i < hash->len; i++) {
52                 if (hash->items[i].key == NULL) {
53                         hash->items[i].key = key;
54                         hash->items[i].val = val;
55                         break;
56                 }
57         }
58         if (i == hash->len) {
59                 struct pmhash *nhash;
60                 size_t len = hash->len * sizeof(struct item);
61                 size_t nlen = len * 2;
62                 nhash = realloc(hash, sizeof(*nhash) + nlen);
63                 if (!nhash)
64                         goto out;
65                 *pmhash = hash = nhash;
66                 memset(&hash->items[hash->len], 0, len);
67                 hash->len = hash->len * 2;
68                 goto repeat;
69         }
70         return 0;
71 out:
72         return -1;
73 }
74
75 void * pmhash_get(struct pmhash *pmhash, char *key)
76 {
77         unsigned int i;
78         for (i = 0; i < pmhash->len; i++) {
79                 if (pmhash->items[i].key == NULL)
80                         return NULL;
81                 if (!strcmp(pmhash->items[i].key, key))
82                         return pmhash->items[i].val;
83         }
84         return NULL;
85 }
86
87 void pmhash_del(struct pmhash *pmhash)
88 {
89         unsigned int i;
90         for (i = 0; i < pmhash->len; i++) {
91                 if (pmhash->items[i].key == NULL)
92                         break;
93                 free(pmhash->items[i].key);
94                 free(pmhash->items[i].val);
95         }
96         free(pmhash);
97 }