From d391b8d1912b9ce1254fb1fede13419aad66fe49 Mon Sep 17 00:00:00 2001 From: Thadeu Lima de Souza Cascardo Date: Tue, 11 Jun 2013 08:33:03 -0300 Subject: [PATCH] Introduce poor man's hash. This lookup array is helpful for adding fields parsed from the DEC file. --- Makefile.am | 2 +- pmhash.c | 97 +++++++++++++++++++++++++++++++++++++++++++++++++++++ pmhash.h | 29 ++++++++++++++++ 3 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 pmhash.c create mode 100644 pmhash.h diff --git a/Makefile.am b/Makefile.am index a9d3f7e..c823c67 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,2 +1,2 @@ bin_PROGRAMS = rnetclient -rnetclient_SOURCES = rnetclient.c decfile.c decfile.h +rnetclient_SOURCES = rnetclient.c decfile.c decfile.h pmhash.c pmhash.h diff --git a/pmhash.c b/pmhash.c new file mode 100644 index 0000000..d228856 --- /dev/null +++ b/pmhash.c @@ -0,0 +1,97 @@ +/* + * Copyright (C) 2012-2013 Thadeu Lima de Souza Cascardo + * + * 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 along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include "pmhash.h" +#include +#include + +struct item { + char *key; + void *val; +}; + +struct pmhash { + size_t len; + struct item items[]; +}; + +struct pmhash * pmhash_new(void) +{ + struct pmhash *pmhash; + size_t len = 128; + pmhash = malloc(sizeof(*pmhash) + len * sizeof(struct item)); + if (!pmhash) + return NULL; + pmhash->len = len; + memset(pmhash->items, 0, len * sizeof(struct item)); + return pmhash; +} + +int pmhash_add(struct pmhash **pmhash, char *key, void *val) +{ + int i; + struct pmhash *hash = *pmhash; + i = 0; +repeat: + for (; i < hash->len; i++) { + if (hash->items[i].key == NULL) { + hash->items[i].key = key; + hash->items[i].val = val; + break; + } + } + if (i == hash->len) { + struct pmhash *nhash; + size_t len = hash->len * sizeof(struct item); + size_t nlen = len * 2; + nhash = realloc(hash, sizeof(*nhash) + nlen); + if (!nhash) + goto out; + *pmhash = hash = nhash; + memset(&hash->items[hash->len], 0, len); + hash->len = hash->len * 2; + goto repeat; + } + return 0; +out: + return -1; +} + +void * pmhash_get(struct pmhash *pmhash, char *key) +{ + int i; + for (i = 0; i < pmhash->len; i++) { + if (pmhash->items[i].key == NULL) + return NULL; + if (!strcmp(pmhash->items[i].key, key)) + return pmhash->items[i].val; + } + return NULL; +} + +void pmhash_del(struct pmhash *pmhash) +{ + int i; + for (i = 0; i < pmhash->len; i++) { + if (pmhash->items[i].key == NULL) + break; + free(pmhash->items[i].key); + free(pmhash->items[i].val); + } + free(pmhash); +} diff --git a/pmhash.h b/pmhash.h new file mode 100644 index 0000000..d1c0206 --- /dev/null +++ b/pmhash.h @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2012-2013 Thadeu Lima de Souza Cascardo + * + * 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 along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef _PMHASH_H +#define _PMHASH_H + +struct pmhash; + +struct pmhash * pmhash_new(void); +int pmhash_add(struct pmhash **pmhash, char *key, void *val); +void * pmhash_get(struct pmhash *pmhash, char *key); +void pmhash_del(struct pmhash *pmhash); + +#endif -- 2.20.1