=================================================================== RCS file: /cvs/mandoc/Attic/man_hash.c,v retrieving revision 1.4 retrieving revision 1.5 diff -u -p -r1.4 -r1.5 --- mandoc/Attic/man_hash.c 2009/03/31 13:50:19 1.4 +++ mandoc/Attic/man_hash.c 2009/04/02 06:51:44 1.5 @@ -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 * @@ -16,17 +16,19 @@ * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR * PERFORMANCE OF THIS SOFTWARE. */ +#include #include #include #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; - /* Do nothing. */ - return(NULL); + 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; + + 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; - /* TODO */ + htab = (const int *)arg; - for (i = 0; i < MAN_MAX; i++) - if (0 == strcmp(tmp, man_macronames[i])) - return(i); + if (0 == (x = tmp[0])) + return(MAN_MAX); + if ( ! ((x >= 65 && x <= 90) || (x >= 97 && x <= 122))) + return(MAN_MAX); + + x -= (x <= 90) ? 65 : 97; + x *= 4; + + 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); }