/* azundris */ #include #include /* malloc(), free() */ #include /* str...() */ #include /* varargs in sprintf/appendf */ #include "ecore_private.h" #include "Ecore_Config.h" #include "ecore_config_util.h" #include "ecore_config_private.h" #define CHUNKLEN 4096 /*****************************************************************************/ /* STRINGS */ /***********/ estring * estring_new(int size) { estring *e = malloc(sizeof(estring)); if (e) { memset(e, 0, sizeof(estring)); if ((size > 0) && (e->str = malloc(size))) e->alloc = size; } return e; } char * estring_disown(estring * e) { if (e) { char *r = e->str; free(e); return r; } return NULL; } int estring_appendf(estring * e, const char *fmt, ...) { int need; va_list ap; char *p; if (!e) return ECORE_CONFIG_ERR_FAIL; if (!e->str) { e->used = e->alloc = 0; if (!(e->str = (char *)malloc(e->alloc = 512))) return ECORE_CONFIG_ERR_OOM; } retry: va_start(ap, fmt); need = vsnprintf(e->str + e->used, e->alloc - e->used, fmt, ap); va_end(ap); if ((need >= (e->alloc - e->used)) || (need < 0)) { if (need < 0) need = 2 * e->alloc; else need++; need += e->used; need += (CHUNKLEN - (need % CHUNKLEN)); if (!(p = (char *)realloc(e->str, need))) { free(e->str); e->alloc = e->used = 0; return ECORE_CONFIG_ERR_OOM; } e->alloc = need; e->str = p; goto retry; } return e->used += need; } int esprintf(char **result, const char *fmt, ...) { va_list ap; size_t need; char *n; if (!result) return ECORE_CONFIG_ERR_FAIL; va_start(ap, fmt); need = vsnprintf(NULL, 0, fmt, ap) + 1; va_end(ap); n = malloc(need + 1); if (n) { va_start(ap, fmt); need = vsnprintf(n, need, fmt, ap); va_end(ap); n[need] = 0; if(*result) free(result); *result = n; return need; } return ECORE_CONFIG_ERR_OOM; } /*****************************************************************************/