[BACK]Return to chars.c CVS log [TXT][DIR] Up to [cvsweb.bsd.lv] / mandoc

Annotation of mandoc/chars.c, Revision 1.49

1.49    ! kristaps    1: /*     $Id: chars.c,v 1.48 2011/07/21 15:21:13 kristaps Exp $ */
1.1       kristaps    2: /*
1.25      schwarze    3:  * Copyright (c) 2009, 2010 Kristaps Dzonsons <kristaps@bsd.lv>
1.32      schwarze    4:  * Copyright (c) 2011 Ingo Schwarze <schwarze@openbsd.org>
1.1       kristaps    5:  *
                      6:  * Permission to use, copy, modify, and distribute this software for any
                      7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
                      9:  *
                     10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     15:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     16:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     17:  */
1.14      kristaps   18: #ifdef HAVE_CONFIG_H
                     19: #include "config.h"
                     20: #endif
                     21:
1.1       kristaps   22: #include <assert.h>
1.40      kristaps   23: #include <ctype.h>
1.1       kristaps   24: #include <stdlib.h>
                     25: #include <string.h>
                     26:
1.18      kristaps   27: #include "mandoc.h"
1.43      kristaps   28: #include "libmandoc.h"
1.1       kristaps   29:
1.2       kristaps   30: #define        PRINT_HI         126
                     31: #define        PRINT_LO         32
1.1       kristaps   32:
                     33: struct ln {
                     34:        struct ln        *next;
                     35:        const char       *code;
1.2       kristaps   36:        const char       *ascii;
1.21      kristaps   37:        int               unicode;
1.1       kristaps   38: };
                     39:
1.49    ! kristaps   40: #define        LINES_MAX         328
1.1       kristaps   41:
1.24      kristaps   42: #define CHAR(in, ch, code) \
1.45      kristaps   43:        { NULL, (in), (ch), (code) },
1.1       kristaps   44:
1.13      kristaps   45: #define        CHAR_TBL_START    static struct ln lines[LINES_MAX] = {
                     46: #define        CHAR_TBL_END      };
                     47:
1.1       kristaps   48: #include "chars.in"
                     49:
1.36      kristaps   50: struct mchars {
1.1       kristaps   51:        struct ln       **htab;
                     52: };
                     53:
1.45      kristaps   54: static const struct ln  *find(struct mchars *, const char *, size_t);
1.1       kristaps   55:
                     56: void
1.36      kristaps   57: mchars_free(struct mchars *arg)
1.1       kristaps   58: {
                     59:
1.36      kristaps   60:        free(arg->htab);
                     61:        free(arg);
1.1       kristaps   62: }
                     63:
1.36      kristaps   64: struct mchars *
1.38      kristaps   65: mchars_alloc(void)
1.1       kristaps   66: {
1.36      kristaps   67:        struct mchars    *tab;
1.1       kristaps   68:        struct ln       **htab;
                     69:        struct ln        *pp;
                     70:        int               i, hash;
                     71:
                     72:        /*
                     73:         * Constructs a very basic chaining hashtable.  The hash routine
                     74:         * is simply the integral value of the first character.
1.47      kristaps   75:         * Subsequent entries are chained in the order they're processed.
1.1       kristaps   76:         */
                     77:
1.36      kristaps   78:        tab = mandoc_malloc(sizeof(struct mchars));
1.33      kristaps   79:        htab = mandoc_calloc(PRINT_HI - PRINT_LO + 1, sizeof(struct ln **));
1.1       kristaps   80:
                     81:        for (i = 0; i < LINES_MAX; i++) {
1.2       kristaps   82:                hash = (int)lines[i].code[0] - PRINT_LO;
1.1       kristaps   83:
                     84:                if (NULL == (pp = htab[hash])) {
                     85:                        htab[hash] = &lines[i];
                     86:                        continue;
                     87:                }
                     88:
                     89:                for ( ; pp->next; pp = pp->next)
                     90:                        /* Scan ahead. */ ;
                     91:                pp->next = &lines[i];
                     92:        }
                     93:
                     94:        tab->htab = htab;
                     95:        return(tab);
                     96: }
                     97:
1.21      kristaps   98: int
1.36      kristaps   99: mchars_spec2cp(struct mchars *arg, const char *p, size_t sz)
1.21      kristaps  100: {
                    101:        const struct ln *ln;
                    102:
1.45      kristaps  103:        ln = find(arg, p, sz);
1.21      kristaps  104:        if (NULL == ln)
                    105:                return(-1);
                    106:        return(ln->unicode);
                    107: }
                    108:
1.39      kristaps  109: char
1.36      kristaps  110: mchars_num2char(const char *p, size_t sz)
1.32      schwarze  111: {
                    112:        int               i;
                    113:
1.48      kristaps  114:        if ((i = mandoc_strntoi(p, sz, 10)) < 0)
1.39      kristaps  115:                return('\0');
1.44      kristaps  116:        return(isprint(i) ? i : '\0');
                    117: }
                    118:
                    119: int
                    120: mchars_num2uc(const char *p, size_t sz)
                    121: {
                    122:        int               i;
1.39      kristaps  123:
1.48      kristaps  124:        if ((i = mandoc_strntoi(p, sz, 16)) < 0)
1.44      kristaps  125:                return('\0');
                    126:        /* FIXME: make sure we're not in a bogus range. */
                    127:        return(i > 0x80 && i <= 0x10FFFF ? i : '\0');
1.21      kristaps  128: }
                    129:
1.1       kristaps  130: const char *
1.36      kristaps  131: mchars_spec2str(struct mchars *arg, const char *p, size_t sz, size_t *rsz)
1.1       kristaps  132: {
1.21      kristaps  133:        const struct ln *ln;
1.1       kristaps  134:
1.45      kristaps  135:        ln = find(arg, p, sz);
1.21      kristaps  136:        if (NULL == ln)
                    137:                return(NULL);
                    138:
1.24      kristaps  139:        *rsz = strlen(ln->ascii);
1.21      kristaps  140:        return(ln->ascii);
1.1       kristaps  141: }
                    142:
1.21      kristaps  143: static const struct ln *
1.45      kristaps  144: find(struct mchars *tab, const char *p, size_t sz)
1.1       kristaps  145: {
1.47      kristaps  146:        struct ln        *pp;
1.1       kristaps  147:        int               hash;
                    148:
                    149:        assert(p);
                    150:
1.47      kristaps  151:        if (0 == sz || p[0] < PRINT_LO || p[0] > PRINT_HI)
1.1       kristaps  152:                return(NULL);
                    153:
1.2       kristaps  154:        hash = (int)p[0] - PRINT_LO;
1.1       kristaps  155:
1.47      kristaps  156:        for (pp = tab->htab[hash]; pp; pp = pp->next)
                    157:                if (0 == strncmp(pp->code, p, sz) &&
                    158:                                '\0' == pp->code[(int)sz])
                    159:                        return(pp);
1.1       kristaps  160:
                    161:        return(NULL);
                    162: }

CVSweb