project_files/frontlib/util/buffer.c
changeset 7271 5608ac657362
parent 7234 613998625a3c
child 7275 15f722e0b96f
equal deleted inserted replaced
7269:5b0aeef8ba2a 7271:5608ac657362
     6 #include <limits.h>
     6 #include <limits.h>
     7 #include <string.h>
     7 #include <string.h>
     8 
     8 
     9 #define MIN_VECTOR_CAPACITY 16
     9 #define MIN_VECTOR_CAPACITY 16
    10 
    10 
    11 typedef struct _flib_vector {
    11 struct _flib_vector {
    12 	void *data;
    12 	void *data;
    13 	size_t size;
    13 	size_t size;
    14 	size_t capacity;
    14 	size_t capacity;
    15 } _flib_vector;
    15 };
    16 
    16 
    17 flib_vector *flib_vector_create() {
    17 flib_vector *flib_vector_create() {
    18 	flib_vector *result = NULL;
    18 	flib_vector *result = NULL;
    19 	flib_vector *tmpVector = flib_calloc(1, sizeof(_flib_vector));
    19 	flib_vector *tmpVector = flib_calloc(1, sizeof(flib_vector));
    20 	if(tmpVector) {
    20 	if(tmpVector) {
    21 		tmpVector->data = flib_malloc(MIN_VECTOR_CAPACITY);
    21 		tmpVector->data = flib_malloc(MIN_VECTOR_CAPACITY);
    22 		if(tmpVector->data) {
    22 		if(tmpVector->data) {
    23 			tmpVector->size = 0;
    23 			tmpVector->size = 0;
    24 			tmpVector->capacity = MIN_VECTOR_CAPACITY;
    24 			tmpVector->capacity = MIN_VECTOR_CAPACITY;
    65 		return -1;
    65 		return -1;
    66 	}
    66 	}
    67 
    67 
    68 	if(vec->capacity < newSize) {
    68 	if(vec->capacity < newSize) {
    69 		// Resize exponentially for constant amortized time,
    69 		// Resize exponentially for constant amortized time,
    70 		// But at least by as much as we need of course,
    70 		// But at least by as much as we need of course
    71 		// and be extra careful with integer overflows...
       
    72 		size_t extraCapacity = (vec->capacity)/2;
    71 		size_t extraCapacity = (vec->capacity)/2;
    73 		size_t minExtraCapacity = newSize - vec->capacity;
    72 		size_t minExtraCapacity = newSize - vec->capacity;
    74 		if(extraCapacity < minExtraCapacity) {
    73 		if(extraCapacity < minExtraCapacity) {
    75 			extraCapacity = minExtraCapacity;
    74 			extraCapacity = minExtraCapacity;
    76 		}
    75 		}
   111 
   110 
   112 	memmove(((uint8_t*)vec->data) + oldSize, data, len);
   111 	memmove(((uint8_t*)vec->data) + oldSize, data, len);
   113 	return len;
   112 	return len;
   114 }
   113 }
   115 
   114 
       
   115 int flib_vector_appendf(flib_vector *vec, const char *fmt, ...) {
       
   116 	int result = -1;
       
   117 	if(!vec || !fmt) {
       
   118 		flib_log_e("null parameter in flib_vector_appendf");
       
   119 	} else {
       
   120 		va_list argp;
       
   121 		va_start(argp, fmt);
       
   122 		char *formatted = flib_vasprintf(fmt, argp);
       
   123 		va_end(argp);
       
   124 
       
   125 
       
   126 		if(formatted) {
       
   127 			size_t len = strlen(formatted);
       
   128 			if(flib_vector_append(vec, formatted, len) == len) {
       
   129 				result = 0;
       
   130 			}
       
   131 		}
       
   132 	}
       
   133 	return result;
       
   134 }
       
   135 
   116 flib_buffer flib_vector_as_buffer(flib_vector *vec) {
   136 flib_buffer flib_vector_as_buffer(flib_vector *vec) {
   117 	if(!vec) {
   137 	if(!vec) {
   118 		flib_log_e("null parameter in flib_vector_as_buffer");
   138 		flib_log_e("null parameter in flib_vector_as_buffer");
   119 		flib_buffer result = {NULL, 0};
   139 		flib_buffer result = {NULL, 0};
   120 		return result;
   140 		return result;