Annotation of mandoc/man_hash.c, Revision 1.6
1.6 ! kristaps 1: /* $Id: man_hash.c,v 1.5 2009/04/02 06:51:44 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:
1.6 ! kristaps 42: htab = calloc(26 * 5, sizeof(int));
1.5 kristaps 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;
1.6 ! kristaps 53: x *= 5;
1.5 kristaps 54:
1.6 ! kristaps 55: for (j = 0; j < 5; j++)
1.5 kristaps 56: if (0 == htab[x + j]) {
57: htab[x + j] = i;
58: break;
59: }
60:
1.6 ! kristaps 61: assert(j < 5);
1.5 kristaps 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;
1.6 ! kristaps 82: x *= 5;
1.5 kristaps 83:
1.6 ! kristaps 84: for (i = 0; i < 5; i++) {
1.5 kristaps 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