Annotation of mandoc/chars.c, Revision 1.31
1.31 ! kristaps 1: /* $Id: chars.c,v 1.30 2010/09/15 13:10:30 kristaps Exp $ */
1.1 kristaps 2: /*
1.25 schwarze 3: * Copyright (c) 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
1.1 kristaps 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 above
7: * copyright notice and this permission notice appear in all copies.
8: *
9: * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10: * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11: * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12: * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13: * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14: * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15: * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16: */
1.14 kristaps 17: #ifdef HAVE_CONFIG_H
18: #include "config.h"
19: #endif
20:
1.1 kristaps 21: #include <assert.h>
1.10 kristaps 22: #include <stdio.h>
1.1 kristaps 23: #include <stdlib.h>
24: #include <string.h>
25:
1.18 kristaps 26: #include "mandoc.h"
1.1 kristaps 27: #include "chars.h"
28:
1.2 kristaps 29: #define PRINT_HI 126
30: #define PRINT_LO 32
1.1 kristaps 31:
32: struct ln {
33: struct ln *next;
34: const char *code;
1.2 kristaps 35: const char *ascii;
1.21 kristaps 36: int unicode;
1.1 kristaps 37: int type;
38: #define CHARS_CHAR (1 << 0)
39: #define CHARS_STRING (1 << 1)
1.12 kristaps 40: #define CHARS_BOTH (CHARS_CHAR | CHARS_STRING)
1.1 kristaps 41: };
42:
1.30 kristaps 43: #define LINES_MAX 351
1.1 kristaps 44:
1.24 kristaps 45: #define CHAR(in, ch, code) \
46: { NULL, (in), (ch), (code), CHARS_CHAR },
47: #define STRING(in, ch, code) \
48: { NULL, (in), (ch), (code), CHARS_STRING },
49: #define BOTH(in, ch, code) \
50: { NULL, (in), (ch), (code), CHARS_BOTH },
1.1 kristaps 51:
1.13 kristaps 52: #define CHAR_TBL_START static struct ln lines[LINES_MAX] = {
53: #define CHAR_TBL_END };
54:
1.1 kristaps 55: #include "chars.in"
56:
1.31 ! kristaps 57: struct ctab {
1.2 kristaps 58: enum chars type;
1.1 kristaps 59: struct ln **htab;
60: };
61:
62: static inline int match(const struct ln *,
63: const char *, size_t, int);
1.31 ! kristaps 64: static const struct ln *find(struct ctab *, const char *, size_t, int);
1.1 kristaps 65:
66:
67: void
68: chars_free(void *arg)
69: {
1.31 ! kristaps 70: struct ctab *tab;
1.1 kristaps 71:
1.31 ! kristaps 72: tab = (struct ctab *)arg;
1.1 kristaps 73:
74: free(tab->htab);
75: free(tab);
76: }
77:
78:
79: void *
80: chars_init(enum chars type)
81: {
1.31 ! kristaps 82: struct ctab *tab;
1.1 kristaps 83: struct ln **htab;
84: struct ln *pp;
85: int i, hash;
86:
87: /*
88: * Constructs a very basic chaining hashtable. The hash routine
89: * is simply the integral value of the first character.
90: * Subsequent entries are chained in the order they're processed
91: * (they're in-line re-ordered during lookup).
92: */
93:
1.31 ! kristaps 94: tab = malloc(sizeof(struct ctab));
1.10 kristaps 95: if (NULL == tab) {
1.11 kristaps 96: perror(NULL);
1.29 kristaps 97: exit((int)MANDOCLEVEL_SYSERR);
1.10 kristaps 98: }
1.1 kristaps 99:
1.2 kristaps 100: htab = calloc(PRINT_HI - PRINT_LO + 1, sizeof(struct ln **));
1.10 kristaps 101: if (NULL == htab) {
1.11 kristaps 102: perror(NULL);
1.29 kristaps 103: exit((int)MANDOCLEVEL_SYSERR);
1.10 kristaps 104: }
1.1 kristaps 105:
106: for (i = 0; i < LINES_MAX; i++) {
1.2 kristaps 107: hash = (int)lines[i].code[0] - PRINT_LO;
1.1 kristaps 108:
109: if (NULL == (pp = htab[hash])) {
110: htab[hash] = &lines[i];
111: continue;
112: }
113:
114: for ( ; pp->next; pp = pp->next)
115: /* Scan ahead. */ ;
116: pp->next = &lines[i];
117: }
118:
119: tab->htab = htab;
1.10 kristaps 120: tab->type = type;
1.1 kristaps 121: return(tab);
122: }
123:
124:
1.21 kristaps 125: /*
126: * Special character to Unicode codepoint.
127: */
128: int
129: chars_spec2cp(void *arg, const char *p, size_t sz)
130: {
131: const struct ln *ln;
132:
1.31 ! kristaps 133: ln = find((struct ctab *)arg, p, sz, CHARS_CHAR);
1.21 kristaps 134: if (NULL == ln)
135: return(-1);
136: return(ln->unicode);
137: }
138:
139:
140: /*
141: * Reserved word to Unicode codepoint.
142: */
143: int
144: chars_res2cp(void *arg, const char *p, size_t sz)
145: {
146: const struct ln *ln;
147:
1.31 ! kristaps 148: ln = find((struct ctab *)arg, p, sz, CHARS_STRING);
1.21 kristaps 149: if (NULL == ln)
150: return(-1);
151: return(ln->unicode);
152: }
153:
154:
155: /*
156: * Special character to string array.
157: */
1.1 kristaps 158: const char *
1.21 kristaps 159: chars_spec2str(void *arg, const char *p, size_t sz, size_t *rsz)
1.1 kristaps 160: {
1.21 kristaps 161: const struct ln *ln;
162:
1.31 ! kristaps 163: ln = find((struct ctab *)arg, p, sz, CHARS_CHAR);
1.21 kristaps 164: if (NULL == ln)
165: return(NULL);
1.1 kristaps 166:
1.24 kristaps 167: *rsz = strlen(ln->ascii);
1.21 kristaps 168: return(ln->ascii);
1.1 kristaps 169: }
170:
171:
1.21 kristaps 172: /*
173: * Reserved word to string array.
174: */
1.1 kristaps 175: const char *
1.21 kristaps 176: chars_res2str(void *arg, const char *p, size_t sz, size_t *rsz)
1.1 kristaps 177: {
1.21 kristaps 178: const struct ln *ln;
1.1 kristaps 179:
1.31 ! kristaps 180: ln = find((struct ctab *)arg, p, sz, CHARS_STRING);
1.21 kristaps 181: if (NULL == ln)
182: return(NULL);
183:
1.24 kristaps 184: *rsz = strlen(ln->ascii);
1.21 kristaps 185: return(ln->ascii);
1.1 kristaps 186: }
187:
188:
1.21 kristaps 189: static const struct ln *
1.31 ! kristaps 190: find(struct ctab *tab, const char *p, size_t sz, int type)
1.1 kristaps 191: {
192: struct ln *pp, *prev;
193: struct ln **htab;
194: int hash;
195:
196: assert(p);
1.23 kristaps 197: if (0 == sz)
198: return(NULL);
1.1 kristaps 199:
1.2 kristaps 200: if (p[0] < PRINT_LO || p[0] > PRINT_HI)
1.1 kristaps 201: return(NULL);
202:
203: /*
204: * Lookup the symbol in the symbol hash. See ascii2htab for the
205: * hashtable specs. This dynamically re-orders the hash chain
206: * to optimise for repeat hits.
207: */
208:
1.2 kristaps 209: hash = (int)p[0] - PRINT_LO;
1.1 kristaps 210: htab = tab->htab;
211:
212: if (NULL == (pp = htab[hash]))
213: return(NULL);
214:
215: for (prev = NULL; pp; pp = pp->next) {
216: if ( ! match(pp, p, sz, type)) {
217: prev = pp;
218: continue;
219: }
220:
221: if (prev) {
222: prev->next = pp->next;
223: pp->next = htab[hash];
224: htab[hash] = pp;
225: }
226:
1.21 kristaps 227: return(pp);
1.1 kristaps 228: }
229:
230: return(NULL);
231: }
232:
233:
234: static inline int
235: match(const struct ln *ln, const char *p, size_t sz, int type)
236: {
237:
238: if ( ! (ln->type & type))
239: return(0);
1.22 kristaps 240: if (strncmp(ln->code, p, sz))
1.1 kristaps 241: return(0);
1.22 kristaps 242: return('\0' == ln->code[(int)sz]);
1.1 kristaps 243: }
CVSweb