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

Annotation of pod2mdoc/dict.c, Revision 1.3

1.3     ! schwarze    1: /*     $Id: dict.c,v 1.2 2015/02/13 09:56:59 schwarze Exp $    */
1.1       schwarze    2: /*
                      3:  * Copyright (c) 2015 Ingo Schwarze <schwarze@openbsd.org>
                      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 AUTHORS DISCLAIM ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHORS 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:  */
                     17: #include <sys/types.h>
                     18:
                     19: #include <stddef.h>
                     20: #include <stdint.h>
                     21: #include <stdio.h>
                     22: #include <stdlib.h>
                     23: #include <string.h>
1.2       schwarze   24:
                     25: #if HAVE_OHASH
1.1       schwarze   26: #include <ohash.h>
1.2       schwarze   27: #else
                     28: #include "compat_ohash.h"
                     29: #endif
1.1       schwarze   30:
                     31: #include "dict.h"
                     32:
                     33: struct dict_entry {
                     34:        enum mdoc_type   t;
                     35:        char             s[];
                     36: };
                     37:
                     38: static void    *dict_malloc(size_t, void *);
                     39: static void    *dict_calloc(size_t, size_t, void *);
                     40: static void     dict_free(void *, void *);
                     41:
                     42: static struct ohash dict_data;
                     43:
                     44:
                     45: void
                     46: dict_init(void)
                     47: {
                     48:        struct ohash_info        dict_info;
                     49:
                     50:        dict_info.key_offset = offsetof(struct dict_entry, s);
                     51:        dict_info.data = NULL;
                     52:        dict_info.alloc = dict_malloc;
                     53:        dict_info.calloc = dict_calloc;
                     54:        dict_info.free = dict_free;
                     55:
                     56:        ohash_init(&dict_data, 4, &dict_info);
                     57: }
                     58:
                     59: enum mdoc_type
                     60: dict_get(const char *s, size_t len)
                     61: {
                     62:        struct dict_entry       *entry;
                     63:        const char              *end;
                     64:        unsigned int             slot;
                     65:
                     66:        if (len == 0)
                     67:                len = strlen(s);
                     68:        end = s + len;
                     69:        slot = ohash_qlookupi(&dict_data, s, &end);
                     70:        entry = ohash_find(&dict_data, slot);
                     71:        return(entry == NULL ? MDOC_MAX : entry->t);
                     72: }
                     73:
                     74: void
1.3     ! schwarze   75: dict_put(const char *s, size_t len, enum mdoc_type t)
1.1       schwarze   76: {
                     77:        struct dict_entry       *entry;
                     78:        const char              *end;
                     79:        unsigned int             slot;
                     80:
1.3     ! schwarze   81:        if (len == 0)
        !            82:                len = strlen(s);
1.1       schwarze   83:        end = s + len;
                     84:        slot = ohash_qlookupi(&dict_data, s, &end);
                     85:        entry = ohash_find(&dict_data, slot);
                     86:        if (entry == NULL) {
                     87:                entry = malloc(sizeof(*entry) + len + 1);
                     88:                if (entry == NULL) {
                     89:                        perror(NULL);
                     90:                        exit(1);
                     91:                }
1.3     ! schwarze   92:                memcpy(entry->s, s, len);
        !            93:                entry->s[len] = '\0';
1.1       schwarze   94:                ohash_insert(&dict_data, slot, entry);
                     95:        }
                     96:        entry->t = t;
                     97: }
                     98:
                     99: void
                    100: dict_destroy(void)
                    101: {
                    102:        struct dict_entry       *entry;
                    103:        unsigned int             slot;
                    104:
                    105:        entry = ohash_first(&dict_data, &slot);
                    106:        while (entry != NULL) {
                    107:                free(entry);
                    108:                entry = ohash_next(&dict_data, &slot);
                    109:        }
                    110:        ohash_delete(&dict_data);
                    111: }
                    112:
                    113: static void *
                    114: dict_malloc(size_t size, void *dummy)
                    115: {
                    116:
                    117:        return(malloc(size));
                    118: }
                    119:
                    120: static void *
                    121: dict_calloc(size_t nmemb, size_t size, void *dummy)
                    122: {
                    123:
                    124:        return(calloc(nmemb, size));
                    125: }
                    126:
                    127: static void
                    128: dict_free(void *ptr, void *dummy)
                    129: {
                    130:
                    131:        free(ptr);
                    132: }

CVSweb