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