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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
#include "conf.h"
#include "array.h"
#include "log.h"
#include <errno.h>
#include <stdlib.h>
#include <string.h>
RCSID("$Id: array.c,v 1.10 2000/10/17 23:38:27 shmit Exp $");
string_t *
string_new(int slen, char *strdata)
{
string_t *tmp;
tmp = malloc(sizeof(string_t));
if (tmp == NULL) {
log_err("Couldn't allocate data for string: %s.",
strerror(errno));
return NULL;
}
tmp->str = malloc(slen * sizeof(char *));
if (tmp->str == NULL) {
log_err("Couldn't allocate string data: %s.",
strerror(errno));
return NULL;
}
memcpy(tmp->str, strdata, slen);
tmp->strlen = slen;
return tmp;
}
void
string_delete(string_t *string)
{
if (string == NULL)
return;
if (string->str != NULL)
free(string->str);
string->strlen = 0;
string->str = NULL;
free(string);
}
array_t *
array_new()
{
array_t *tmp;
tmp = malloc(sizeof(array_t));
if (tmp == NULL)
return NULL;
tmp->nitems = 0;
tmp->items = NULL;
return tmp;
}
void
array_delete(array_t *aa)
{
int i;
if (aa == NULL)
return;
for (i = 0; i < aa->nitems; i++)
string_delete(aa->items[i]);
free(aa->items);
aa->items = NULL;
free(aa);
}
int
va_array_add(array_t *aa, va_list ap)
{
const int GRANULARITY = 10;
int slen;
char *s;
slen = va_arg(ap, int);
if (slen != ARRTERM)
s = va_arg(ap, char *);
else
s = NULL;
while (s) {
if (aa->nitems % GRANULARITY == 0) {
aa->items = realloc(aa->items, sizeof(string_t *) *
(GRANULARITY + aa->nitems));
if (aa->items == NULL)
return -1;
}
aa->nitems++;
aa->items[aa->nitems-1] = string_new(slen, s);
if (aa->items[aa->nitems-1] == NULL)
return -1;
slen = va_arg(ap, int);
if (slen != ARRTERM)
s = va_arg(ap, char *);
else
s = NULL;
}
return 0;
}
int
array_add(array_t *aa, ...)
{
va_list ap;
int rc;
va_start(ap, aa);
rc = va_array_add(aa, ap);
va_end(ap);
return rc;
}
int
array_dup(array_t *dst, array_t *src)
{
int i;
if (dst == NULL)
return -1;
dst->nitems = src->nitems;
dst->items = malloc(dst->nitems * sizeof(string_t *));
if (dst->items == NULL) {
log_err("Couldn't allocate dup array items list: %s.",
strerror(errno));
return -1;
}
for (i = 0; i < dst->nitems; i++) {
dst->items[i] = string_new(src->items[i]->strlen,
src->items[i]->str);
if (dst->items[i] == NULL) {
dst->nitems = i;
return -1;
}
}
return 0;
}
|