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

Annotation of mandoc/compat_stringlist.c, Revision 1.4

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

CVSweb