Annotation of mandoc/man_hash.c, Revision 1.5
1.5 ! kristaps 1: /* $Id: man_hash.c,v 1.4 2009/03/31 13:50:19 kristaps Exp $ */
1.1 kristaps 2: /*
3: * Copyright (c) 2008, 2009 Kristaps Dzonsons <kristaps@openbsd.org>
4: *
5: * Permission to use, copy, modify, and distribute this software for any
6: * purpose with or without fee is hereby granted, provided that the
7: * above copyright notice and this permission notice appear in all
8: * copies.
9: *
10: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
11: * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
12: * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
13: * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14: * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15: * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
16: * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
17: * PERFORMANCE OF THIS SOFTWARE.
18: */
1.5 ! kristaps 19: #include <assert.h>
1.1 kristaps 20: #include <stdlib.h>
21: #include <string.h>
22:
23: #include "libman.h"
24:
1.5 ! kristaps 25:
1.1 kristaps 26: /* ARGUSED */
27: void
28: man_hash_free(void *htab)
29: {
30:
1.5 ! kristaps 31: free(htab);
1.1 kristaps 32: }
33:
34:
35: /* ARGUSED */
36: void *
37: man_hash_alloc(void)
38: {
1.5 ! kristaps 39: int *htab;
! 40: int i, j, x;
! 41:
! 42: htab = calloc(26 * 4, sizeof(int));
! 43: if (NULL == htab)
! 44: return(NULL);
! 45:
! 46: for (i = 1; i < MAN_MAX; i++) {
! 47: x = man_macronames[i][0];
! 48:
! 49: assert((x >= 65 && x <= 90) ||
! 50: (x >= 97 && x <= 122));
! 51:
! 52: x -= (x <= 90) ? 65 : 97;
! 53: x *= 4;
! 54:
! 55: for (j = 0; j < 4; j++)
! 56: if (0 == htab[x + j]) {
! 57: htab[x + j] = i;
! 58: break;
! 59: }
! 60:
! 61: assert(j < 4);
! 62: }
1.1 kristaps 63:
1.5 ! kristaps 64: return((void *)htab);
1.1 kristaps 65: }
66:
67:
68: int
69: man_hash_find(const void *arg, const char *tmp)
70: {
1.5 ! kristaps 71: int x, i, tok;
! 72: const int *htab;
1.1 kristaps 73:
1.5 ! kristaps 74: htab = (const int *)arg;
1.2 kristaps 75:
1.5 ! kristaps 76: if (0 == (x = tmp[0]))
! 77: return(MAN_MAX);
! 78: if ( ! ((x >= 65 && x <= 90) || (x >= 97 && x <= 122)))
! 79: return(MAN_MAX);
! 80:
! 81: x -= (x <= 90) ? 65 : 97;
! 82: x *= 4;
! 83:
! 84: for (i = 0; i < 4; i++) {
! 85: if (0 == (tok = htab[x + i]))
! 86: return(MAN_MAX);
! 87: if (0 == strcmp(tmp, man_macronames[tok]))
! 88: return(tok);
! 89: }
1.1 kristaps 90:
91: return(MAN_MAX);
92: }
93:
CVSweb