1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
|
/* dctrl-tools - Debian control file inspection tools
Copyright © 2012 Antti-Juhani Kaijanaho
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <assert.h>
#include <stdbool.h>
#include <stdlib.h>
#include "strlist.h"
struct strlist {
const char **strs;
size_t n;
size_t maxn;
};
struct strlist *strlist_new(void)
{
struct strlist *rv = malloc(sizeof *rv);
if (rv == 0) return 0;
rv->strs = 0;
rv->n = 0;
rv->maxn = 0;
return rv;
}
bool strlist_append(struct strlist *sl, const char *s)
{
if (sl->n + 1 >= sl->maxn) {
size_t newmax = sl->maxn * 2;
if (newmax == 0) newmax = 2;
const char **newstrs = realloc(sl->strs,
newmax * sizeof *newstrs);
if (newstrs == 0) return false;
sl->maxn = newmax;
sl->strs = newstrs;
}
assert(sl->n < sl->maxn);
sl->strs[sl->n++] = s;
return true;
}
bool strlist_is_empty(struct strlist *sl)
{
return sl->n == 0;
}
void strlist_free(struct strlist *sl)
{
free(sl->strs);
free(sl);
}
struct strlist_iterator strlist_begin(struct strlist *sl)
{
struct strlist_iterator it = {
.sl = sl,
.i = 0
};
return it;
}
bool strlist_iterator_at_end(struct strlist_iterator it)
{
return it.i >= it.sl->n;
}
const char *strlist_iterator_get(struct strlist_iterator it)
{
return it.sl->strs[it.i];
}
void strlist_iterator_next(struct strlist_iterator * it)
{
it->i++;
}
struct strlist_memento strlist_save(struct strlist *sl)
{
struct strlist_memento rv = {
.sl = sl,
.n = sl->n
};
return rv;
}
void strlist_restore(struct strlist_memento mem)
{
mem.sl->n = mem.n;
}
|