summaryrefslogtreecommitdiffstats
path: root/src/lib.c
blob: b6a5f3a8be4e72f81bc42283fdbd7c96e2968f84 (plain) (blame)
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
struct writefunc_string {
	char *ptr;
	size_t len;
};
void init_writefunc_string(struct writefunc_string *s) {
	s->len = 0;
	s->ptr = malloc(s->len+1);
	if (s->ptr == NULL) {
		fprintf(stderr, "malloc() " DC_I18N_FAILED "\n");
		exit(EXIT_FAILURE);
	}
	s->ptr[0] = '\0';
}
size_t writefunc(void *ptr, size_t size, size_t nmemb, struct writefunc_string *s) {
	size_t new_len = s->len + size*nmemb;
	s->ptr = realloc(s->ptr, new_len+1);
	if (s->ptr == NULL) {
		fprintf(stderr, "realloc() " DC_I18N_FAILED "\n");
		exit(EXIT_FAILURE);
	}
	memcpy(s->ptr+s->len, ptr, size*nmemb);
	s->ptr[new_len] = '\0';
	s->len = new_len;
	return size*nmemb;
}