-/* $Id: chars.c,v 1.17 2010/03/23 13:25:01 kristaps Exp $ */
+/* $Id: chars.c,v 1.25 2010/07/31 23:52:58 schwarze Exp $ */
/*
- * Copyright (c) 2009 Kristaps Dzonsons <kristaps@kth.se>
+ * Copyright (c) 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
#include <stdlib.h>
#include <string.h>
+#include "mandoc.h"
#include "chars.h"
#define PRINT_HI 126
struct ln *next;
const char *code;
const char *ascii;
- const char *html;
- size_t codesz;
- size_t asciisz;
- size_t htmlsz;
+ int unicode;
int type;
#define CHARS_CHAR (1 << 0)
#define CHARS_STRING (1 << 1)
#define CHARS_BOTH (CHARS_CHAR | CHARS_STRING)
};
-#define LINES_MAX 369
+#define LINES_MAX 370
-#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(in, ch, code) \
+ { NULL, (in), (ch), (code), CHARS_CHAR },
+#define STRING(in, ch, code) \
+ { NULL, (in), (ch), (code), CHARS_STRING },
+#define BOTH(in, ch, code) \
+ { NULL, (in), (ch), (code), CHARS_BOTH },
#define CHAR_TBL_START static struct ln lines[LINES_MAX] = {
#define CHAR_TBL_END };
static inline int match(const struct ln *,
const char *, size_t, int);
-static const char *find(struct tbl *, const char *,
- size_t, size_t *, int);
+static const struct ln *find(struct tbl *, const char *, size_t, int);
void
}
+/*
+ * Special character to Unicode codepoint.
+ */
+int
+chars_spec2cp(void *arg, const char *p, size_t sz)
+{
+ const struct ln *ln;
+
+ ln = find((struct tbl *)arg, p, sz, CHARS_CHAR);
+ if (NULL == ln)
+ return(-1);
+ return(ln->unicode);
+}
+
+
+/*
+ * Reserved word to Unicode codepoint.
+ */
+int
+chars_res2cp(void *arg, const char *p, size_t sz)
+{
+ const struct ln *ln;
+
+ ln = find((struct tbl *)arg, p, sz, CHARS_STRING);
+ if (NULL == ln)
+ return(-1);
+ return(ln->unicode);
+}
+
+
+/*
+ * Special character to string array.
+ */
const char *
-chars_a2ascii(void *arg, const char *p, size_t sz, size_t *rsz)
+chars_spec2str(void *arg, const char *p, size_t sz, size_t *rsz)
{
+ const struct ln *ln;
+
+ ln = find((struct tbl *)arg, p, sz, CHARS_CHAR);
+ if (NULL == ln)
+ return(NULL);
- return(find((struct tbl *)arg, p, sz, rsz, CHARS_CHAR));
+ *rsz = strlen(ln->ascii);
+ return(ln->ascii);
}
+/*
+ * Reserved word to string array.
+ */
const char *
-chars_a2res(void *arg, const char *p, size_t sz, size_t *rsz)
+chars_res2str(void *arg, const char *p, size_t sz, size_t *rsz)
{
+ const struct ln *ln;
+
+ ln = find((struct tbl *)arg, p, sz, CHARS_STRING);
+ if (NULL == ln)
+ return(NULL);
- return(find((struct tbl *)arg, p, sz, rsz, CHARS_STRING));
+ *rsz = strlen(ln->ascii);
+ return(ln->ascii);
}
-static const char *
-find(struct tbl *tab, const char *p, size_t sz, size_t *rsz, int type)
+static const struct ln *
+find(struct tbl *tab, const char *p, size_t sz, int type)
{
struct ln *pp, *prev;
struct ln **htab;
int hash;
assert(p);
- assert(sz > 0);
+ if (0 == sz)
+ return(NULL);
if (p[0] < PRINT_LO || p[0] > PRINT_HI)
return(NULL);
htab[hash] = pp;
}
- if (CHARS_HTML == tab->type) {
- *rsz = pp->htmlsz;
- return(pp->html);
- }
- *rsz = pp->asciisz;
- return(pp->ascii);
+ return(pp);
}
return(NULL);
if ( ! (ln->type & type))
return(0);
- if (ln->codesz != sz)
+ if (strncmp(ln->code, p, sz))
return(0);
- return(0 == strncmp(ln->code, p, sz));
+ return('\0' == ln->code[(int)sz]);
}