-/* $Id: chars.c,v 1.1 2009/09/17 07:41:28 kristaps Exp $ */
+/* $Id: chars.c,v 1.11 2009/10/31 06:10:57 kristaps Exp $ */
/*
* Copyright (c) 2009 Kristaps Dzonsons <kristaps@kth.se>
*
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <assert.h>
-#include <err.h>
+#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "chars.h"
-#define ASCII_PRINT_HI 126
-#define ASCII_PRINT_LO 32
+#define PRINT_HI 126
+#define PRINT_LO 32
struct ln {
struct ln *next;
const char *code;
- const char *out;
+ const char *ascii;
+ const char *html;
size_t codesz;
- size_t outsz;
+ size_t asciisz;
+ size_t htmlsz;
int type;
#define CHARS_CHAR (1 << 0)
#define CHARS_STRING (1 << 1)
#define CHARS_BOTH (0x03)
};
-#define LINES_MAX 266
+#define LINES_MAX 351
-#define CHAR(w, x, y, z) \
- { NULL, (w), (y), (x), (z), CHARS_CHAR },
-#define STRING(w, x, y, z) \
- { NULL, (w), (y), (x), (z), CHARS_STRING },
-#define BOTH(w, x, y, z) \
- { NULL, (w), (y), (x), (z), CHARS_BOTH },
+#define CHAR(w, x, y, z, a, b) \
+ { NULL, (w), (y), (a), (x), (z), (b), CHARS_CHAR },
+#define STRING(w, x, y, z, a, b) \
+ { NULL, (w), (y), (a), (x), (z), (b), CHARS_STRING },
+#define BOTH(w, x, y, z, a, b) \
+ { NULL, (w), (y), (a), (x), (z), (b), CHARS_BOTH },
static struct ln lines[LINES_MAX] = {
#include "chars.in"
};
struct tbl {
+ enum chars type;
struct ln **htab;
};
}
-/* ARGSUSED */
void *
chars_init(enum chars type)
{
* (they're in-line re-ordered during lookup).
*/
- if (NULL == (tab = malloc(sizeof(struct tbl))))
- err(1, "malloc");
-
- htab = calloc(ASCII_PRINT_HI - ASCII_PRINT_LO + 1,
- sizeof(struct ln **));
+ tab = malloc(sizeof(struct tbl));
+ if (NULL == tab) {
+ perror(NULL);
+ exit(EXIT_FAILURE);
+ }
- if (NULL == htab)
- err(1, "malloc");
+ htab = calloc(PRINT_HI - PRINT_LO + 1, sizeof(struct ln **));
+ if (NULL == htab) {
+ perror(NULL);
+ exit(EXIT_FAILURE);
+ }
for (i = 0; i < LINES_MAX; i++) {
- assert(lines[i].codesz > 0);
- assert(lines[i].code);
- assert(lines[i].out);
-
- hash = (int)lines[i].code[0] - ASCII_PRINT_LO;
+ hash = (int)lines[i].code[0] - PRINT_LO;
if (NULL == (pp = htab[hash])) {
htab[hash] = &lines[i];
for ( ; pp->next; pp = pp->next)
/* Scan ahead. */ ;
-
pp->next = &lines[i];
}
tab->htab = htab;
+ tab->type = type;
return(tab);
}
assert(p);
assert(sz > 0);
- if (p[0] < ASCII_PRINT_LO || p[0] > ASCII_PRINT_HI)
+ if (p[0] < PRINT_LO || p[0] > PRINT_HI)
return(NULL);
/*
* to optimise for repeat hits.
*/
- hash = (int)p[0] - ASCII_PRINT_LO;
+ hash = (int)p[0] - PRINT_LO;
htab = tab->htab;
if (NULL == (pp = htab[hash]))
if (NULL == pp->next) {
if ( ! match(pp, p, sz, type))
return(NULL);
- *rsz = pp->outsz;
- return(pp->out);
+
+ if (CHARS_HTML == tab->type) {
+ *rsz = pp->htmlsz;
+ return(pp->html);
+ }
+ *rsz = pp->asciisz;
+ return(pp->ascii);
}
for (prev = NULL; pp; pp = pp->next) {
continue;
}
- /* Re-order the hash chain. */
-
if (prev) {
prev->next = pp->next;
pp->next = htab[hash];
htab[hash] = pp;
}
- *rsz = pp->outsz;
- return(pp->out);
+ if (CHARS_HTML == tab->type) {
+ *rsz = pp->htmlsz;
+ return(pp->html);
+ }
+ *rsz = pp->asciisz;
+ return(pp->ascii);
}
return(NULL);