-/* $Id: chars.c,v 1.1 2009/09/17 07:41:28 kristaps Exp $ */
+/* $Id: chars.c,v 1.17 2010/03/23 13:25:01 kristaps Exp $ */
/*
* Copyright (c) 2009 Kristaps Dzonsons <kristaps@kth.se>
*
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
#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 CHARS_BOTH (CHARS_CHAR | CHARS_STRING)
};
-#define LINES_MAX 266
+#define LINES_MAX 369
+
+#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 },
-#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_TBL_START static struct ln lines[LINES_MAX] = {
+#define CHAR_TBL_END };
-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]))
return(NULL);
- if (NULL == pp->next) {
- if ( ! match(pp, p, sz, type))
- return(NULL);
- *rsz = pp->outsz;
- return(pp->out);
- }
-
for (prev = NULL; pp; pp = pp->next) {
if ( ! match(pp, p, sz, type)) {
prev = pp;
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);