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