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
|
/* genvec charvec char ; 1999-10-05 22:59:09 */
#include "charvec.h"
charvec_t* charvec_new(void* ((*ra)(void*,size_t)), void (*fr)(void*))
{
charvec_t *vec = ra(0, sizeof(*vec));
if (vec) {
if (charvec_init(vec, ra, fr))
return vec;
fr(vec);
}
return 0;
}
charvec_t* charvec_free(charvec_t *t)
{
charvec_fini(t);
t->free(t);
return 0;
}
int charvec_init(charvec_t *t, void* ((*a)(void*,size_t)), void (*f)(void*))
{
assert(t);
t->a = 0;
t->len = 0;
t->max = 0;
t->alloc = a;
t->free = f;
return charvec_need(t, 0);
}
int charvec_fini(charvec_t *t)
{
assert(t);
if (t->a && t->free) { t->free(t->a); t->a = 0; t->max = 0; }
t->len = 0;
return 1;
}
#ifndef BASE_ALLOC
#define BASE_ALLOC 30
#endif
enum { BASE = BASE_ALLOC };
int charvec_need(charvec_t *t, unsigned int n)
{
assert(t);
if (t->a == 0) {
assert(t->alloc);
t->len = 0;
t->max = n;
t->a = t->alloc(0, n * sizeof(char));
return t->a != 0;
}
if (n > t->max) {
unsigned int i = BASE + n + (n >> 3);
void *p = t->alloc(t->a, i * sizeof(char));
if (!p)
return 0;
t->max = i;
t->a = p;
}
return 1;
}
int charvec_more(charvec_t *t, unsigned int n)
{
assert(t);
return charvec_need(t, n + t->len);
}
int charvec_append(charvec_t *t, char e)
{
assert(t);
if (!charvec_more(t, 1))
return 0;
t->a[t->len++] = e;
return 1;
}
int charvec_fit(charvec_t *t)
{
assert(t);
assert(t->alloc);
{
unsigned int i = t->len;
void *p = t->alloc(t->a, i * sizeof(char));
if (!p)
return 0;
t->max = i;
t->a = p;
return 1;
}
}
|