-
Notifications
You must be signed in to change notification settings - Fork 2
/
ptrarray.c
76 lines (67 loc) · 1.5 KB
/
ptrarray.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <sys/file.h>
#include <sys/mman.h>
#include <ctype.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include "list.h"
#include "parseconfig.h"
#include "ptrarray.h"
struct ptrarray* ptrarray_create()
{
struct ptrarray *a = malloc(sizeof(*a));
memset(a, 0, sizeof(*a));
return a;
}
void ptrarray_free(struct ptrarray *a)
{
assert(a);
for (size_t i = 0; i < a->used; i++)
free(a->array[i]);
free(a);
}
#define PTRARRAY_MIN_ALLOC 16
struct ptrarray* ptrarray_add(struct ptrarray *a, void *ptr)
{
struct ptrarray *n;
assert(a->used <= a->alloc);
if (a->used == a->alloc) {
if (a->alloc == 0)
a->alloc = PTRARRAY_MIN_ALLOC / 2;
n = realloc(a, (a->alloc * 2)*sizeof(void*) + sizeof(*a));
if (n == NULL) /* FIXME, this should probably be detected by the caller */
return a;
a = n;
a->alloc *= 2;
}
a->array[a->used++] = ptr;
return a;
}
struct ptrarray* file_to_ptrarray(const char * const fn, struct ptrarray *a)
{
struct list_head conf_root = LIST_HEAD_INIT(conf_root);
struct config *conf;
char *name;
if (parse_config_file(fn, &conf_root))
return NULL;
list_for_each_entry(conf, &conf_root, list) {
name = strdup(conf->key);
if (!name) {
destroy_config(&conf_root);
return NULL;
}
a = ptrarray_add(a, name);
}
destroy_config(&conf_root);
return a;
}
void* ptrarray_get(struct ptrarray *a, unsigned int idx)
{
if (idx >= a->used)
return NULL;
return a->array[idx];
}