Add string-vector accumulator utility for C

This commit is contained in:
Lassi Kortela 2019-10-13 22:56:45 +03:00
parent bc33a9b728
commit 5e9bb704c8
2 changed files with 49 additions and 0 deletions

View File

@ -1053,6 +1053,15 @@ void accum_elt(struct accum *accum, value_t elt);
void accum_pair(struct accum *accum, value_t a, value_t d);
void accum_name_value(struct accum *accum, const char *name, value_t value);
struct sv_accum {
char **vec;
size_t cap;
size_t fill;
};
void sv_accum_init(struct sv_accum *accum);
void sv_accum_strdup(struct sv_accum *accum, const char *str);
// boot_image.c
extern char boot_image[];

View File

@ -14,6 +14,13 @@
#include "scheme.h"
static void *reallocarray(void *ptr, size_t nmemb, size_t size)
{
return realloc(ptr, nmemb * size);
}
//
void accum_elt(struct accum *accum, value_t elt)
{
value_t newtail;
@ -36,3 +43,36 @@ void accum_name_value(struct accum *accum, const char *name, value_t value)
{
accum_pair(accum, string_from_cstr(name), value);
}
//
void sv_accum_init(struct sv_accum *accum)
{
accum->fill = 0;
accum->cap = 8;
accum->vec = calloc(accum->cap, sizeof(char *));
if (!accum->vec) {
lerror(MemoryError, "out of memory");
}
}
void sv_accum_strdup(struct sv_accum *accum, const char *str)
{
char *dup;
if (__unlikely(accum->cap <= accum->fill + 1)) {
while (accum->cap <= accum->fill + 1) {
accum->cap *= 2;
}
accum->vec = reallocarray(accum->vec, accum->cap, sizeof(char *));
if (!accum->vec) {
lerror(MemoryError, "out of memory");
}
}
dup = strdup(str);
if (!dup) {
lerror(MemoryError, "out of memory");
}
accum->vec[accum->fill++] = dup;
accum->vec[accum->fill] = 0;
}