Annotation of mandoc/hash.c, Revision 1.1
1.1 ! kristaps 1: /* $Id: roff.c,v 1.64 2008/12/12 10:11:10 kristaps Exp $ */
! 2: /*
! 3: * Copyright (c) 2008 Kristaps Dzonsons <kristaps@kth.se>
! 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: */
! 19: #include <assert.h>
! 20: #include <ctype.h>
! 21: #include <err.h>
! 22: #include <stdlib.h>
! 23: #include <stdio.h>
! 24: #include <string.h>
! 25:
! 26: #include "private.h"
! 27:
! 28:
! 29: void
! 30: mdoc_hash_free(void *htab)
! 31: {
! 32:
! 33: free(htab);
! 34: }
! 35:
! 36:
! 37: void *
! 38: mdoc_hash_alloc(void)
! 39: {
! 40: int i, major, minor, index;
! 41: const void **htab;
! 42:
! 43: htab = calloc(27 * 26, sizeof(struct mdoc_macro *));
! 44: if (NULL == htab)
! 45: err(1, "calloc");
! 46:
! 47: for (i = 1; i < MDOC_MAX; i++) {
! 48: major = mdoc_macronames[i][0];
! 49: assert((major >= 65 && major <= 90) ||
! 50: major == 37);
! 51:
! 52: if (major == 37)
! 53: major = 0;
! 54: else
! 55: major -= 64;
! 56:
! 57: minor = mdoc_macronames[i][1];
! 58: assert((minor >= 65 && minor <= 90) ||
! 59: (minor == 49) ||
! 60: (minor >= 97 && minor <= 122));
! 61:
! 62: if (minor == 49)
! 63: minor = 0;
! 64: else if (minor <= 90)
! 65: minor -= 65;
! 66: else
! 67: minor -= 97;
! 68:
! 69: assert(major >= 0 && major < 27);
! 70: assert(minor >= 0 && minor < 26);
! 71:
! 72: index = (major * 27) + minor;
! 73:
! 74: assert(NULL == htab[index]);
! 75: htab[index] = &mdoc_macros[i];
! 76: }
! 77:
! 78: return((void *)htab);
! 79: }
! 80:
! 81:
! 82: int
! 83: mdoc_hash_find(const void *arg, const char *tmp)
! 84: {
! 85: int major, minor, index, slot;
! 86: const void **htab;
! 87:
! 88: htab = (const void **)arg;
! 89:
! 90: if (0 == tmp[0] || 0 == tmp[1])
! 91: return(MDOC_MAX);
! 92:
! 93: if ( ! (tmp[0] == 37 || (tmp[0] >= 65 && tmp[0] <= 90)))
! 94: return(MDOC_MAX);
! 95:
! 96: if ( ! ((tmp[1] >= 65 && tmp[1] <= 90) ||
! 97: (tmp[1] == 49) ||
! 98: (tmp[1] >= 97 && tmp[1] <= 122)))
! 99: return(MDOC_MAX);
! 100:
! 101: if (tmp[0] == 37)
! 102: major = 0;
! 103: else
! 104: major = tmp[0] - 64;
! 105:
! 106: if (tmp[1] == 49)
! 107: minor = 0;
! 108: else if (tmp[1] <= 90)
! 109: minor = tmp[1] - 65;
! 110: else
! 111: minor = tmp[1] - 97;
! 112:
! 113: index = (major * 27) + minor;
! 114:
! 115: if (NULL == htab[index])
! 116: return(MDOC_MAX);
! 117:
! 118: slot = htab[index] - (void *)mdoc_macros;
! 119: assert(0 == slot % sizeof(struct mdoc_macro));
! 120: slot /= sizeof(struct mdoc_macro);
! 121:
! 122: if (0 != strcmp(mdoc_macronames[slot], tmp))
! 123: return(MDOC_MAX);
! 124: return(slot);
! 125: }
! 126:
CVSweb