5 #if defined(CVEC_MALLOC) && defined(CVEC_FREE) && defined(CVEC_REALLOC)
7 #elif !defined(CVEC_MALLOC) && !defined(CVEC_FREE) && !defined(CVEC_REALLOC)
10 #error "Must define all or none of CVEC_MALLOC, CVEC_FREE, and CVEC_REALLOC."
14 #define CVEC_MALLOC(sz) malloc(sz)
15 #define CVEC_REALLOC(p, sz) realloc(p, sz)
16 #define CVEC_FREE(p) free(p)
21 #define CVEC_MEMMOVE(dst, src, sz) memmove(dst, src, sz)
26 #define CVEC_ASSERT(x) assert(x)
31 #define CVEC_STR_ALLOCATOR(x) ((x+1) * 2)
33 #if CVEC_STRDUP == cvec_strdup
84 memset(vec->
a, 0, vec->
capacity*
sizeof(
char*));
109 for(i=0; i<num; i++) {
132 memset(vec->
a, 0, vec->
capacity*
sizeof(
char*));
152 for(i=0; i<num; i++) {
200 for (i=0; i<src->
size; ++i) {
218 if (!(tmp = (
char**)
CVEC_REALLOC(vec->
a,
sizeof(
char*)*tmp_sz))) {
237 if (!(tmp = (
char**)
CVEC_REALLOC(vec->
a,
sizeof(
char*)*tmp_sz))) {
245 vec->
a[vec->
size++] = a;
257 strcpy(ret, vec->
a[vec->
size]);
264 return &vec->
a[vec->
size-1];
276 if (!(tmp = (
char**)
CVEC_REALLOC(vec->
a,
sizeof(
char*)*tmp_sz))) {
284 memset(&vec->
a[vec->
size], 0, num*
sizeof(
char*));
299 if (!(tmp = (
char**)
CVEC_REALLOC(vec->
a,
sizeof(
char*)*tmp_sz))) {
322 if (!(tmp = (
char**)
CVEC_REALLOC(vec->
a,
sizeof(
char*)*tmp_sz))) {
347 if (!(tmp = (
char**)
CVEC_REALLOC(vec->
a,
sizeof(
char*)*tmp_sz))) {
356 for (j=0; j<num; ++j) {
373 if (!(tmp = (
char**)
CVEC_REALLOC(vec->
a,
sizeof(
char*)*tmp_sz))) {
395 strcpy(ret, vec->
a[i]);
409 for (i=start; i<=end; i++) {
448 if (size < vec->size) {
449 for(i=vec->
size-1; i>size-1; i--) {
456 if (!(tmp = (
char**)
CVEC_REALLOC(vec->
a,
sizeof(
char*)*size))) {
469 for(i=0; i<vec->
size; i++) {
498 for (i=0; i<vec->
size; i++) {
512 for (i=0; i<tmp->
size; i++) {
525 for (i=0; i<tmp->
size; i++) {
int cvec_init_str(cvector_str *vec, char **vals, cvec_sz num)
Same as cvec_init_str_heap() except the vector passed in was declared on the stack so it isn't alloca...
void cvec_clear_str(cvector_str *vec)
Clears the contents of vector (frees all strings) and sets size to 0.
char * cvec_strdup(const char *str)
Useful utility function since strdup isn't in standard C.
int cvec_copy_str(cvector_str *dest, cvector_str *src)
Makes dest a copy of src.
int cvec_set_cap_str(cvector_str *vec, cvec_sz size)
Set capacity to size.
#define CVEC_REALLOC(p, sz)
int cvec_extend_str(cvector_str *vec, cvec_sz num)
Increase the size of the array num items.
void cvec_set_val_cap_str(cvector_str *vec, char *val)
Fills entire allocated array (capacity) with val.
int cvec_copyc_str(void *dest, void *src)
Makes dest a copy of src.
#define CVEC_MEMMOVE(dst, src, sz)
void cvec_set_val_sz_str(cvector_str *vec, char *val)
Sets all size elements to val.
int cvec_insert_array_str(cvector_str *vec, cvec_sz i, char **a, cvec_sz num)
Insert the first num elements of array a at index i.
int cvec_insertm_str(cvector_str *vec, cvec_sz i, char *a)
Same as insert except no CVEC_STRDUP.
void cvec_erase_str(cvector_str *vec, cvec_sz start, cvec_sz end)
Erases strings from start to end inclusive.
void cvec_free_str(void *vec)
Frees the internal array and sets size and capacity to 0.
char ** cvec_back_str(cvector_str *vec)
Return pointer to last element.
int cvec_push_str(cvector_str *vec, char *a)
Append a to end of vector (size increased 1).
int cvec_insert_arraym_str(cvector_str *vec, cvec_sz i, char **a, cvec_sz num)
Same as insert_array except no CVEC_STRDUP.
int cvec_pushm_str(cvector_str *vec, char *a)
same as push but without calling CVEC_STRDUP(a), m suffix is for "move"
void cvec_replace_str(cvector_str *vec, cvec_sz i, char *a, char *ret)
Replace string at i with a.
void cvec_pop_str(cvector_str *vec, char *ret)
Remove the last element (size decreased 1).
void cvec_free_str_heap(void *vec)
Frees contents (individual strings and array) and frees vector so don't use after calling this.
#define CVEC_STR_ALLOCATOR(x)
int cvec_reserve_str(cvector_str *vec, cvec_sz size)
Makes sure the vector capacity is >= size (parameter not member).
int cvec_insert_str(cvector_str *vec, cvec_sz i, char *a)
Insert a at index i (0 based).
cvector_str * cvec_str_heap(cvec_sz size, cvec_sz capacity)
Create a new cvector_str on the heap.
void cvec_remove_str(cvector_str *vec, cvec_sz start, cvec_sz end)
Same as erase except it does not call CVEC_FREE.
int cvec_str(cvector_str *vec, cvec_sz size, cvec_sz capacity)
Same as cvec_str_heap() except the vector passed in was declared on the stack so it isn't allocated i...
cvector_str * cvec_init_str_heap(char **vals, cvec_sz num)
Create (on the heap) and initialize cvector_str with num elements of vals.
cvec_sz CVEC_STR_START_SZ
Data structure for string vector.
cvec_sz capacity
Allocated size of array; always >= size.
cvec_sz size
Current size (amount you use when manipulating array directly).