diff options
Diffstat (limited to 'man_hash.c')
-rw-r--r-- | man_hash.c | 55 |
1 files changed, 46 insertions, 9 deletions
@@ -1,4 +1,4 @@ -/* $Id: man_hash.c,v 1.4 2009/03/31 13:50:19 kristaps Exp $ */ +/* $Id: man_hash.c,v 1.5 2009/04/02 06:51:44 kristaps Exp $ */ /* * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@openbsd.org> * @@ -16,17 +16,19 @@ * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR * PERFORMANCE OF THIS SOFTWARE. */ +#include <assert.h> #include <stdlib.h> #include <string.h> #include "libman.h" + /* ARGUSED */ void man_hash_free(void *htab) { - /* Do nothing. */ + free(htab); } @@ -34,22 +36,57 @@ man_hash_free(void *htab) void * man_hash_alloc(void) { + int *htab; + int i, j, x; + + htab = calloc(26 * 4, sizeof(int)); + if (NULL == htab) + return(NULL); + + for (i = 1; i < MAN_MAX; i++) { + x = man_macronames[i][0]; + + assert((x >= 65 && x <= 90) || + (x >= 97 && x <= 122)); + + x -= (x <= 90) ? 65 : 97; + x *= 4; - /* Do nothing. */ - return(NULL); + for (j = 0; j < 4; j++) + if (0 == htab[x + j]) { + htab[x + j] = i; + break; + } + + assert(j < 4); + } + + return((void *)htab); } int man_hash_find(const void *arg, const char *tmp) { - int i; + int x, i, tok; + const int *htab; + + htab = (const int *)arg; + + if (0 == (x = tmp[0])) + return(MAN_MAX); + if ( ! ((x >= 65 && x <= 90) || (x >= 97 && x <= 122))) + return(MAN_MAX); - /* TODO */ + x -= (x <= 90) ? 65 : 97; + x *= 4; - for (i = 0; i < MAN_MAX; i++) - if (0 == strcmp(tmp, man_macronames[i])) - return(i); + for (i = 0; i < 4; i++) { + if (0 == (tok = htab[x + i])) + return(MAN_MAX); + if (0 == strcmp(tmp, man_macronames[tok])) + return(tok); + } return(MAN_MAX); } |