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

Annotation of mandoc/compat_stringlist.c, Revision 1.5

1.5     ! schwarze    1: #include "config.h"
        !             2:
        !             3: #if HAVE_STRINGLIST
        !             4:
        !             5: int dummy;
        !             6:
        !             7: #else
        !             8:
        !             9: /*     $Id: compat_stringlist.c,v 1.4 2015/05/20 23:43:03 schwarze Exp $       */
1.1       schwarze   10: /*
1.2       schwarze   11:  * Copyright (c) 1994 Christos Zoulas <christos@netbsd.org>
1.1       schwarze   12:  * All rights reserved.
                     13:  *
                     14:  * Redistribution and use in source and binary forms, with or without
                     15:  * modification, are permitted provided that the following conditions
                     16:  * are met:
                     17:  * 1. Redistributions of source code must retain the above copyright
                     18:  *    notice, this list of conditions and the following disclaimer.
                     19:  * 2. Redistributions in binary form must reproduce the above copyright
                     20:  *    notice, this list of conditions and the following disclaimer in the
                     21:  *    documentation and/or other materials provided with the distribution.
                     22:  *
                     23:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
                     24:  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
                     25:  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
                     26:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
                     27:  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
                     28:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
                     29:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
                     30:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
                     31:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
                     32:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
                     33:  * SUCH DAMAGE.
                     34:  */
                     35:
                     36: #include <err.h>
                     37: #include <stdlib.h>
1.2       schwarze   38: #include <string.h>
                     39: #include "compat_stringlist.h"
1.1       schwarze   40:
                     41: #define _SL_CHUNKSIZE  20
                     42:
                     43: /*
                     44:  * sl_init(): Initialize a string list
                     45:  */
                     46: StringList *
                     47: sl_init(void)
                     48: {
                     49:        StringList *sl;
                     50:
                     51:        sl = malloc(sizeof(StringList));
                     52:        if (sl == NULL)
1.2       schwarze   53:                err(1, "stringlist");
1.1       schwarze   54:
                     55:        sl->sl_cur = 0;
                     56:        sl->sl_max = _SL_CHUNKSIZE;
1.3       schwarze   57:        sl->sl_str = reallocarray(NULL, sl->sl_max, sizeof(char *));
1.1       schwarze   58:        if (sl->sl_str == NULL)
1.2       schwarze   59:                err(1, "stringlist");
1.1       schwarze   60:        return sl;
                     61: }
                     62:
                     63:
                     64: /*
                     65:  * sl_add(): Add an item to the string list
                     66:  */
                     67: int
                     68: sl_add(StringList *sl, char *name)
                     69: {
                     70:        if (sl->sl_cur == sl->sl_max - 1) {
                     71:                sl->sl_max += _SL_CHUNKSIZE;
1.3       schwarze   72:                sl->sl_str = reallocarray(sl->sl_str,
                     73:                    sl->sl_max, sizeof(char *));
1.1       schwarze   74:                if (sl->sl_str == NULL)
                     75:                        return (-1);
                     76:        }
                     77:        sl->sl_str[sl->sl_cur++] = name;
                     78:        return (0);
                     79: }
                     80:
                     81:
                     82: /*
                     83:  * sl_free(): Free a stringlist
                     84:  */
                     85: void
                     86: sl_free(StringList *sl, int all)
                     87: {
                     88:        size_t i;
                     89:
                     90:        if (sl == NULL)
                     91:                return;
                     92:        if (sl->sl_str) {
                     93:                if (all)
                     94:                        for (i = 0; i < sl->sl_cur; i++)
                     95:                                free(sl->sl_str[i]);
                     96:                free(sl->sl_str);
                     97:        }
                     98:        free(sl);
                     99: }
                    100:
                    101:
                    102: /*
                    103:  * sl_find(): Find a name in the string list
                    104:  */
                    105: char *
                    106: sl_find(StringList *sl, const char *name)
                    107: {
                    108:        size_t i;
                    109:
                    110:        for (i = 0; i < sl->sl_cur; i++)
                    111:                if (strcmp(sl->sl_str[i], name) == 0)
                    112:                        return sl->sl_str[i];
                    113:
                    114:        return NULL;
                    115: }
1.5     ! schwarze  116:
        !           117: #endif

CVSweb