1 #ifdef HAVE_CONFIG_H
2 # include "config.h"
3 #endif
4
5 #include <stdio.h>
6 #include <string.h>
7 #include <ctype.h>
8
9 #include "eina_private.h"
10 #include "eina_str.h"
11 #include "eina_strbuf_common.h"
12 #include "eina_unicode.h"
13
14 /*============================================================================*
15 * Local *
16 *============================================================================*/
17
18 /**
19 * @cond LOCAL
20 */
21
22 #ifdef _STRBUF_DATA_TYPE
23 # undef _STRBUF_DATA_TYPE
24 #endif
25
26 #ifdef _STRBUF_CSIZE
27 # undef _STRBUF_CSIZE
28 #endif
29
30 #ifdef _STRBUF_STRUCT_NAME
31 # undef _STRBUF_STRUCT_NAME
32 #endif
33
34 #ifdef _STRBUF_STRLEN_FUNC
35 # undef _STRBUF_STRLEN_FUNC
36 #endif
37
38 #ifdef _STRBUF_STRESCAPE_FUNC
39 # undef _STRBUF_STRESCAPE_FUNC
40 #endif
41
42 #ifdef _STRBUF_MAGIC
43 # undef _STRBUF_MAGIC
44 #endif
45
46 #ifdef _STRBUF_MAGIC_STR
47 # undef _STRBUF_MAGIC_STR
48 #endif
49
50 #ifdef _FUNC_EXPAND
51 # undef _FUNC_EXPAND
52 #endif
53
54
55 #define _STRBUF_DATA_TYPE char
56 #define _STRBUF_CSIZE sizeof(_STRBUF_DATA_TYPE)
57 #define _STRBUF_STRUCT_NAME Eina_Strbuf
58 #define _STRBUF_STRLEN_FUNC(x) strlen(x)
59 #define _STRBUF_STRESCAPE_FUNC(x) eina_str_escape(x)
60 #define _STRBUF_MAGIC EINA_MAGIC_STRBUF
61 #define _STRBUF_MAGIC_STR __STRBUF_MAGIC_STR
62 static const char __STRBUF_MAGIC_STR[] = "Eina Strbuf";
63
64 #define _FUNC_EXPAND(y) eina_strbuf_ ## y
65
66 /**
67 * @endcond
68 */
69
70
71 /*============================================================================*
72 * Global *
73 *============================================================================*/
74
75
76 /*============================================================================*
77 * API *
78 *============================================================================*/
79
80
81 EAPI Eina_Bool
eina_strbuf_append_printf(Eina_Strbuf * buf,const char * fmt,...)82 eina_strbuf_append_printf(Eina_Strbuf *buf, const char *fmt, ...)
83 {
84 va_list args;
85 char *str;
86 size_t len;
87 Eina_Bool ret;
88
89 va_start(args, fmt);
90 len = vasprintf(&str, fmt, args);
91 va_end(args);
92
93 if (len == 0 || !str)
94 return EINA_FALSE;
95
96 ret = eina_strbuf_append_length(buf, str, len);
97 free(str);
98 return ret;
99 }
100
101 EAPI Eina_Bool
eina_strbuf_append_vprintf(Eina_Strbuf * buf,const char * fmt,va_list args)102 eina_strbuf_append_vprintf(Eina_Strbuf *buf, const char *fmt, va_list args)
103 {
104 char *str;
105 size_t len;
106 Eina_Bool ret;
107
108 len = vasprintf(&str, fmt, args);
109
110 if (len == 0 || !str)
111 return EINA_FALSE;
112
113 ret = eina_strbuf_append_length(buf, str, len);
114 free(str);
115 return ret;
116 }
117
118 EAPI Eina_Bool
eina_strbuf_insert_printf(Eina_Strbuf * buf,const char * fmt,size_t pos,...)119 eina_strbuf_insert_printf(Eina_Strbuf *buf, const char *fmt, size_t pos, ...)
120 {
121 va_list args;
122 char *str;
123 size_t len;
124 Eina_Bool ret;
125
126 va_start(args, pos);
127 len = vasprintf(&str, fmt, args);
128 va_end(args);
129
130 if (len == 0 || !str)
131 return EINA_FALSE;
132
133 ret = eina_strbuf_insert(buf, str, pos);
134 free(str);
135 return ret;
136 }
137
138 EAPI Eina_Bool
eina_strbuf_insert_vprintf(Eina_Strbuf * buf,const char * fmt,size_t pos,va_list args)139 eina_strbuf_insert_vprintf(Eina_Strbuf *buf,
140 const char *fmt,
141 size_t pos,
142 va_list args)
143 {
144 char *str;
145 size_t len;
146 Eina_Bool ret;
147
148 len = vasprintf(&str, fmt, args);
149
150 if (len == 0 || !str)
151 return EINA_FALSE;
152
153 ret = eina_strbuf_insert(buf, str, pos);
154 free(str);
155 return ret;
156 }
157
158 EAPI void
eina_strbuf_trim(Eina_Strbuf * buf)159 eina_strbuf_trim(Eina_Strbuf *buf)
160 {
161 unsigned char *c = buf->buf;
162
163 while (buf->len > 0 && isspace(c[buf->len - 1]))
164 buf->len--;
165 while (buf->len > 0 && isspace(*c))
166 {
167 c++;
168 buf->len--;
169 }
170 memmove(buf->buf, c, buf->len);
171 ((unsigned char *)buf->buf)[buf->len] = '\0';
172 }
173
174 EAPI void
eina_strbuf_ltrim(Eina_Strbuf * buf)175 eina_strbuf_ltrim(Eina_Strbuf *buf)
176 {
177 unsigned char *c = buf->buf;
178
179 while (buf->len > 0 && isspace(*c))
180 {
181 c++;
182 buf->len--;
183 }
184 memmove(buf->buf, c, buf->len);
185 ((unsigned char *)buf->buf)[buf->len] = '\0';
186 }
187
188 EAPI void
eina_strbuf_rtrim(Eina_Strbuf * buf)189 eina_strbuf_rtrim(Eina_Strbuf *buf)
190 {
191 while (buf->len > 0 && isspace(((unsigned char*)(buf->buf))[buf->len - 1]))
192 buf->len--;
193 ((unsigned char *)buf->buf)[buf->len] = '\0';
194 }
195
196 EAPI void
eina_strbuf_tolower(Eina_Strbuf * buf)197 eina_strbuf_tolower(Eina_Strbuf *buf)
198 {
199 if (!buf || !(buf->buf)) return;
200
201 eina_str_tolower((char **)&(buf->buf));
202 }
203
204 EAPI Eina_Strbuf *
eina_strbuf_substr_get(Eina_Strbuf * buf,size_t pos,size_t len)205 eina_strbuf_substr_get(Eina_Strbuf *buf, size_t pos, size_t len)
206 {
207 char *str;
208
209 if ((!buf) || ((pos + len) > buf->len))
210 return NULL;
211
212 str = calloc(1, len + 1);
213
214 strncpy(str,((char *)(buf->buf)) + pos, len);
215
216 return eina_strbuf_manage_new(str);
217 }
218
219 EAPI Eina_Bool
eina_strbuf_append_strftime(Eina_Strbuf * buf,const char * format,const struct tm * tm)220 eina_strbuf_append_strftime(Eina_Strbuf *buf, const char *format, const struct tm *tm)
221 {
222 char *outputbuf;
223
224 outputbuf = eina_strftime(format, tm);
225 if (!outputbuf) return EINA_FALSE;
226
227 eina_strbuf_append(buf, outputbuf);
228 free(outputbuf);
229
230 return EINA_TRUE;
231 }
232
233 EAPI Eina_Bool
eina_strbuf_insert_strftime(Eina_Strbuf * buf,const char * format,const struct tm * tm,size_t pos)234 eina_strbuf_insert_strftime(Eina_Strbuf *buf, const char *format, const struct tm *tm, size_t pos)
235 {
236 char *outputbuf;
237
238 outputbuf = eina_strftime(format, tm);
239 if (!outputbuf) return EINA_FALSE;
240
241 eina_strbuf_insert_length(buf, outputbuf, strlen(outputbuf), pos);
242 free(outputbuf);
243
244 return EINA_TRUE;
245 }
246
247 /* Unicode */
248
249 #include "eina_strbuf_template_c.x"
250