-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcharvector.c
225 lines (195 loc) · 4.73 KB
/
charvector.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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#include "charvector.h"
#include "utils.h"
#include "talloc.h"
#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MINALLOC 8
void cv_sprintf(cv_t* pcv, const char* fmt, ...)
{
va_list arglist, cpyarglist;
va_copy(cpyarglist, arglist);
va_start(arglist, fmt);
size_t bwritten = vsnprintf(pcv->data, pcv->capacity, fmt, arglist);
va_end(arglist);
va_start(cpyarglist, fmt);
pcv->length = bwritten + 1;
if(bwritten >= pcv->capacity)
{
//Our buffer wasn't large enough. Resize it!
pcv->capacity = max(1, pcv->length);
pcv->data = (el_t*) trealloc(pcv->data,
sizeof(el_t) * pcv->capacity);
memset(pcv->data, 0, sizeof(el_t) * pcv->capacity);
vsnprintf(pcv->data, pcv->capacity, fmt, cpyarglist);
}
va_end(cpyarglist);
}
int cv_append(cv_t* cv, const el_t* data, size_t len)
{
if((cv->length + len) > cv->capacity)
{
cv->capacity = max((cv->capacity << 1),(cv->capacity + len));
cv->capacity = max(cv->capacity, MINALLOC);
cv->data = (el_t*) trealloc(cv->data,
sizeof(el_t) * cv->capacity);
if(!cv->data)
{
return -1;
}
}
memcpy(&(cv->data[cv->length]), data, len * sizeof(el_t));
cv->length = cv->length + len;
return cv->length;
}
void cv_clear(cv_t* cv)
{
memset(cv->data, 0, sizeof(el_t) * cv->capacity);
cv->length = 0;
}
int cv_appendcv(cv_t* dest, cv_t* src)
{
return cv_append(dest, src->data, src->length);
}
int cv_appendstr(cv_t* cv, const el_t* data)
{
cv_strcat(cv, data);
return 0;
}
int cv_init(cv_t* cv, size_t startsize)
{
cv->length = 0;
cv->capacity = max(startsize, MINALLOC);
cv->data = (el_t*) talloc(sizeof(el_t) * cv->capacity);
memset(cv->data, 0, sizeof(el_t) * cv->capacity);
return (startsize > 0 && cv->data) ? 0 : -1;
}
int cv_resize(cv_t* cv, size_t newcap)
{
if(!cv->data || newcap != cv->capacity)
{
cv->data = (el_t*) trealloc(cv->data, sizeof(el_t) * max(MINALLOC, newcap));
cv->capacity = newcap;
}
return cv->data ? 0 : -1;
}
void cv_swap(cv_t* a, cv_t* b)
{
cv_t temp;
temp.data = a->data;
temp.length = a->length;
temp.capacity = a->capacity;
a->data = b->data;
a->length = b->length;
a->capacity= b->capacity;
b->data = temp.data;
b->length = temp.length;
b->capacity = temp.capacity;
}
void cv_copy(cv_t* dest, cv_t* source)
{
dest->length = source->length;
dest->capacity = max(MINALLOC, source->capacity);
if(dest->data)
tfree(dest->data);
dest->data = (el_t*) talloc(sizeof(el_t) * dest->capacity);
memcpy(dest->data, source->data, dest->capacity);
}
void cv_strcpy(cv_t* dest, const el_t* source)
{
dest->length = strlen(source) + 1;
if(dest->capacity < dest->length)
{
dest->capacity = max(dest->length, (dest->capacity<<1));
dest->capacity = max(MINALLOC, dest->capacity);
dest->data = (el_t*) trealloc(dest->data, sizeof(el_t) * dest->capacity);
}
memcpy(dest->data, source, dest->capacity);
}
void cv_strncpy(cv_t* dest, const el_t* source, size_t len)
{
dest->length = len;
dest->capacity = max(MINALLOC, len);
if(dest->data)
tfree(dest->data);
dest->data = (el_t*) talloc(sizeof(el_t) * dest->capacity);
memcpy(dest->data, source, len);
}
void cv_strncat(cv_t* dest, const el_t* source, size_t len)
{
// To account for the null byte, the length must have 1 added to it and dest->length must have 1 subtracted
size_t new_len = max(MINALLOC, (dest->length + len));
if(new_len >= dest->capacity)
{
dest->capacity = max((dest->capacity << 1), new_len);
dest->data = (el_t*) trealloc(dest->data, sizeof(el_t) * dest->capacity);
}
dest->length = new_len;
strncat(dest->data, source, len);
}
void cv_strcat(cv_t* dest, const el_t* source)
{
size_t new_len = max(MINALLOC, (dest->length + strlen(source)));
if(new_len >= dest->capacity)
{
dest->capacity = max((dest->capacity << 1), new_len);
dest->data = (el_t*) trealloc(dest->data, sizeof(el_t) * dest->capacity);
}
strcat(dest->data, source);
dest->length = new_len;
}
void cv_destroy(cv_t* cv)
{
cv->length = 0;
cv->capacity = 0;
tfree(cv->data);
cv->data = 0;
}
int cv_pushlimited(cv_t* cv, el_t newel)
{
//Like push, but will not realloc on its own
if(cv->length < cv->capacity && cv->data)
{
cv->data[cv->length] = newel;
++cv->length;
return 0;
}
else
{
return -1;
}
}
int cv_push(cv_t* cv, el_t newel)
{
if(cv->length >= cv->capacity)
{
cv->capacity = max((cv->capacity << 1), (cv->capacity + cv->length));
cv->capacity = max(cv->capacity, MINALLOC);
cv->data = (el_t*) trealloc(cv->data, sizeof(el_t) * cv->capacity);
}
if(cv->data)
{
cv->data[cv->length] = newel;
++cv->length;
return 0;
}
else
{
return -1;
}
}
inline static void swap_elements(el_t* a, el_t* b)
{
el_t t = *b;
*b = *a;
*a = t;
}
el_t cv_at(cv_t* cv, size_t idx)
{
return cv->data[idx];
}
size_t cv_len(cv_t* cv)
{
return cv->length;
}