svn update: 48958 (latest:48959)
[framework/uifw/ecore.git] / src / lib / ecore_config / ecore_config_util.c
1 /*
2  * vim:ts=8:sw=3:sts=8:noexpandtab:cino=>5n-3f0^-2{2
3  */
4
5 #ifdef HAVE_CONFIG_H
6 # include <config.h>
7 #endif
8
9 /* azundris */
10
11 #include <sys/types.h>
12 #include <stdlib.h>             /* malloc(), free() */
13 #include <stdio.h>
14 #include <string.h>             /* str...() */
15
16 #include <stdarg.h>             /* varargs in sprintf/appendf */
17
18 #include "Ecore.h"
19 #include "ecore_private.h"
20
21 #include "Ecore_Config.h"
22 #include "ecore_config_util.h"
23
24 #include "ecore_config_private.h"
25
26 #define CHUNKLEN 4096
27
28 /*****************************************************************************/
29 /* STRINGS */
30 /***********/
31
32 estring            *
33 estring_new(int size)
34 {
35    estring            *e = malloc(sizeof(estring));
36
37    if (e)
38      {
39         memset(e, 0, sizeof(estring));
40         if ((size > 0) && (e->str = malloc(size)))
41            e->alloc = size;
42      }
43    return e;
44 }
45
46 char               *
47 estring_disown(estring * e)
48 {
49    if (e)
50      {
51         char               *r = e->str;
52
53         free(e);
54         return r;
55      }
56    return NULL;
57 }
58
59 int
60 estring_appendf(estring * e, const char *fmt, ...)
61 {
62    int      need;
63    va_list  ap;
64    char    *p;
65
66    if (!e)
67       return ECORE_CONFIG_ERR_FAIL;
68
69    if (!e->str)
70      {
71         e->used = e->alloc = 0;
72         if (!(e->str = (char *)malloc(e->alloc = CHUNKLEN)))
73            return ECORE_CONFIG_ERR_OOM;
74      }
75
76    va_start(ap, fmt);
77    need = vsnprintf(NULL, 0, fmt, ap);
78    va_end(ap);
79
80    if (need >= (e->alloc - e->used))
81      {
82         e->alloc = e->used + need + (CHUNKLEN - (need % CHUNKLEN));
83
84         if (!(p = (char *)realloc(e->str, e->alloc)))
85            {
86              free(e->str);
87              e->alloc = e->used = 0;
88              return ECORE_CONFIG_ERR_OOM;
89            }
90         e->str = p;
91      }
92
93    va_start(ap, fmt);
94    need = vsnprintf(e->str + e->used, e->alloc - e->used, fmt, ap);
95    va_end(ap);
96
97    return e->used += need;
98 }
99
100 int
101 esprintf(char **result, const char *fmt, ...)
102 {
103    va_list   ap;
104    size_t    need;
105    char     *n;
106
107    if (!result)
108       return ECORE_CONFIG_ERR_FAIL;
109
110    va_start(ap, fmt);
111    need = vsnprintf(NULL, 0, fmt, ap) + 1;
112    va_end(ap);
113    n = malloc(need + 1);
114
115    if (n)
116      {
117         va_start(ap, fmt);
118         need = vsnprintf(n, need, fmt, ap);
119         va_end(ap);
120
121         n[need] = 0;
122
123         if(*result)
124            free(result);
125         *result = n;
126
127         return need;
128      }
129
130    return ECORE_CONFIG_ERR_OOM;
131 }
132
133 /*****************************************************************************/