#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "utarray.h"
struct upload {
const char *filename;
FILE *stream;
};
static void upload_init(void *p) {
struct upload *up = p;
up->stream = fopen(up->filename, "r");
}
static void upload_dtor(void *p) {
struct upload *up = p;
fclose((up)->stream);
free(up);
}
UT_icd upload_icd = {sizeof(struct upload), &upload_init, NULL, &upload_dtor};
int main() {
char line[10]; // <- just for tests
struct upload *up, *p;
UT_array *ups;
utarray_new(ups, &upload_icd);
// upload 1
up = malloc(sizeof(struct upload));
up->filename = "foo.txt";
utarray_push_back(ups, up);
// upload 2
up = malloc(sizeof(struct upload));
up->filename = "bar.txt";
utarray_push_back(ups, up);
for (p = (struct upload *) utarray_front(ups); p != NULL; p = (struct upload *) utarray_next(ups, p)) {
printf("filename: %s\n", p->filename);
while (fgets(line, sizeof(line), up->file))
printf("content: %s\n", line);
}
utarray_free(ups);
return 0;
}
In the real scenario "up" if allocated in a scope and its reference reused in other scopes, so its life cycle must be managed outside utarray.
Therefore, how do I use utarray with pre-allocated itens? (I took a look its sources and can't find any utarray_push_back_ptr :-/)
Thank you!
--