1 /* $Id: buff.c 3483 2007-05-07 11:18:56Z mquinson $ */
3 /* strbuff -- string buffers */
5 /* Copyright (c) 2007, 2008, 2009, 2010. The SimGrid Team.
6 * All rights reserved. */
8 /* This program is free software; you can redistribute it and/or modify it
9 * under the terms of the license (GNU LGPL) which comes with this package. */
11 /* specific to Borland Compiler */
16 #include "xbt/strbuff.h"
18 #define minimal_increment 512
20 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(strbuff, xbt, "String buffers");
26 XBT_INLINE void xbt_strbuff_empty(xbt_strbuff_t b)
32 xbt_strbuff_t xbt_strbuff_new(void)
34 xbt_strbuff_t res = malloc(sizeof(s_xbt_strbuff_t));
35 res->data = malloc(512);
37 xbt_strbuff_empty(res);
41 /** @brief creates a new string buffer containing the provided string
43 * Beware, the ctn is copied, you want to free it afterward, anyhow
45 XBT_INLINE xbt_strbuff_t xbt_strbuff_new_from(const char *ctn)
47 xbt_strbuff_t res = malloc(sizeof(s_xbt_strbuff_t));
48 res->data = xbt_strdup(ctn);
49 res->used = res->size = strlen(ctn);
53 /** @brief frees only the container without touching to the contained string */
54 XBT_INLINE void xbt_strbuff_free_container(xbt_strbuff_t b)
59 /** @brief frees the buffer and its content */
60 XBT_INLINE void xbt_strbuff_free(xbt_strbuff_t b)
68 void xbt_strbuff_append(xbt_strbuff_t b, const char *toadd)
74 THROWF(arg_error, 0, "Asked to append stuff to NULL buffer");
76 addlen = strlen(toadd);
77 needed_space = b->used + addlen + 1;
79 if (needed_space > b->size) {
80 b->size = MAX(minimal_increment + b->used, needed_space);
81 b->data = realloc(b->data, b->size);
83 strcpy(b->data + b->used, toadd);
87 XBT_INLINE void xbt_strbuff_chomp(xbt_strbuff_t b)
89 while (b->used && b->data[b->used - 1] == '\n') {
91 b->data[b->used] = '\0';
95 XBT_INLINE void xbt_strbuff_trim(xbt_strbuff_t b)
97 xbt_str_trim(b->data, " ");
98 b->used = strlen(b->data);
101 /** @brief Replaces a set of variables by their values
103 * @param b buffer to modify
104 * @param patterns variables to substitute in the buffer
106 * Both '$toto' and '${toto}' are valid (and the two writing are equivalent).
108 * If the variable name contains spaces, use the brace version (ie, ${toto tutu})
110 * You can provide a default value to use if the variable is not set in the dict by using
111 * '${var:=default}' or '${var:-default}'. These two forms are equivalent, even if they
112 * shouldn't to respect the shell standard (:= form should set the value in the dict,
113 * but does not) (BUG).
115 void xbt_strbuff_varsubst(xbt_strbuff_t b, xbt_dict_t patterns)
118 char *end; /* pointers around the parsed chunk */
119 int in_simple_quote = 0, in_double_quote = 0;
122 if (b->data[0] == '\0')
129 /* Protected char; pass the protection */
132 THROWF(arg_error, 0, "String ends with \\");
136 if (!in_double_quote) {
137 /* simple quote not protected by double ones, note it */
138 in_simple_quote = !in_simple_quote;
142 if (!in_simple_quote) {
143 /* double quote protected by simple ones, note it */
144 in_double_quote = !in_double_quote;
149 if (!in_simple_quote) {
150 /* Go for the substitution. First search the variable name */
151 char *beg_var, *end_var; /* variable name boundary */
152 char *beg_subst, *end_subst = NULL; /* where value should be written to */
153 char *value, *default_value = NULL;
158 if (*(++end) == '{') {
159 /* the variable name is enclosed in braces. */
161 /* Search name's end */
163 while (*end_var != '\0' && *end_var != '}') {
164 /* TODO: we do not respect the standard for ":=", we should set this value in the dict */
166 && ((*(end_var + 1) == '=') || (*(end_var + 1) == '-'))) {
167 /* damn, we have a default value */
168 char *p = end_var + 1;
169 while (*p != '\0' && *p != '}')
173 "Variable default value not terminated ('}' missing)");
175 default_value = xbt_malloc(p - end_var - 1);
176 memcpy(default_value, end_var + 2, p - end_var - 2);
177 default_value[p - end_var - 2] = '\0';
179 end_subst = p + 1; /* eat '}' */
185 if (*end_var == '\0')
187 "Variable name not terminated ('}' missing)");
189 if (!end_subst) /* already set if there's a default value */
190 end_subst = end_var + 1; /* also kill the } in the name */
192 if (end_var == beg_var)
193 THROWF(arg_error, 0, "Variable name empty (${} is not valid)");
197 /* name given directly */
200 while (*end_var != '\0' && *end_var != ' ' && *end_var != '\t'
204 if (end_var == beg_var)
205 THROWF(arg_error, 0, "Variable name empty ($ is not valid)");
207 /* XBT_DEBUG("var='%.*s'; subst='%.*s'; End_var = '%s'",
208 end_var-beg_var,beg_var,
209 end_subst-beg_subst,beg_subst,
212 /* ok, we now have the variable name. Search the dictionary for the substituted value */
214 xbt_dict_get_or_null_ext(patterns, beg_var, end_var - beg_var);
215 /* XBT_DEBUG("Deal with '%s'",b->data);
216 XBT_DEBUG("Search for %.*s, found %s (default value = %s)\n",
217 end_var-beg_var,beg_var,
218 (value?value:"(no value)"),
219 (default_value?default_value:"(no value)"));*/
222 value = xbt_strdup(value);
223 else if (default_value)
224 value = xbt_strdup(default_value);
226 value = xbt_strdup("");
228 /* En route for the actual substitution */
229 val_len = strlen(value);
230 // XBT_DEBUG("val_len = %d, key_len=%d",val_len,end_subst-beg_subst);
231 if (val_len <= end_subst - beg_subst) {
232 /* enough room to do the substitute in place */
233 // XBT_DEBUG("Substitute key name by its value: ie '%.*s' by '%.*s'",end_subst-beg_subst,beg_subst,val_len,value);
234 memmove(beg_subst, value, val_len); /* substitute */
235 // XBT_DEBUG("String is now: '%s'",b->data);
236 /* XBT_DEBUG("Move end of string closer (%d chars moved) :\n-'%.*s%.*s'\n+'%.*s%s'",
237 b->used - (end_subst - b->data) + 1,
238 beg_subst-b->data,b->data,
239 b->used-(end_subst-b->data)+1,beg_subst+val_len,
240 beg_subst-b->data,b->data,
242 memmove(beg_subst + val_len, end_subst, b->used - (end_subst - b->data) + 1); /* move the end of the string closer */
243 // XBT_DEBUG("String is now: '%s'",b->data);
244 end = beg_subst + val_len; /* update the currently explored char in the overall loop */
245 // XBT_DEBUG("end of substituted section is now '%s'",end);
246 b->used -= end_subst - beg_subst - val_len; /* update string buffer used size */
247 // XBT_DEBUG("Used:%d end:%d ending char:%d",b->used,end-b->data,*end);
249 /* we have to extend the data area */
251 val_len - (end_subst - beg_subst) + 1 /*don't forget \0 */ ;
252 int newused = b->used + tooshort;
253 end += tooshort; /* update the pointer of the overall loop */
254 // XBT_DEBUG("Too short (by %d chars; %d chars left in area)",val_len- (end_subst-beg_subst), b->size - b->used);
255 if (newused > b->size) {
256 /* We have to realloc the data area before (because b->size is too small). We have to update our pointers, too */
257 char *newdata = realloc(b->data,
258 b->used + MAX(minimal_increment,
260 int offset = newdata - b->data;
262 b->size = b->used + MAX(minimal_increment, tooshort);
267 memmove(beg_subst + val_len, end_subst, b->used - (end_subst - b->data) + 1); /* move the end of the string a bit further */
268 memmove(beg_subst, value, val_len); /* substitute */
270 // XBT_DEBUG("String is now: %s",b->data);
276 end--; /* compensate the next end++ */
288 #include "xbt/strbuff.h"
290 /* buffstr have 512 chars by default. Adding 1000 chars like this will force a resize, allowing us to test that b->used and b->size are consistent */
291 #define force_resize \
292 "1.........1.........2.........3.........4.........5.........6.........7.........8.........9........." \
293 "2.........1.........2.........3.........4.........5.........6.........7.........8.........9........." \
294 "3.........1.........2.........3.........4.........5.........6.........7.........8.........9........." \
295 "4.........1.........2.........3.........4.........5.........6.........7.........8.........9........." \
296 "5.........1.........2.........3.........4.........5.........6.........7.........8.........9........." \
297 "6.........1.........2.........3.........4.........5.........6.........7.........8.........9........." \
298 "7.........1.........2.........3.........4.........5.........6.........7.........8.........9........." \
299 "8.........1.........2.........3.........4.........5.........6.........7.........8.........9........." \
300 "9.........1.........2.........3.........4.........5.........6.........7.........8.........9........." \
301 "0.........1.........2.........3.........4.........5.........6.........7.........8.........9........."
303 static void mytest(const char *input, const char *patterns,
304 const char *expected)
306 xbt_dynar_t dyn_patterns; /* splited string */
307 xbt_dict_t p; /* patterns */
309 char *str; /*foreach */
310 xbt_strbuff_t sb; /* what we test */
313 dyn_patterns = xbt_str_split(patterns, " ");
314 xbt_dynar_foreach(dyn_patterns, cpt, str) {
315 xbt_dynar_t keyvals = xbt_str_split(str, "=");
316 char *key = xbt_dynar_get_as(keyvals, 0, char *);
317 char *val = xbt_dynar_get_as(keyvals, 1, char *);
318 xbt_str_subst(key, '_', ' ', 0); // to put space in names without breaking the enclosing dynar_foreach
319 xbt_dict_set(p, key, xbt_strdup(val), free);
320 xbt_dynar_free(&keyvals);
322 xbt_dynar_free(&dyn_patterns);
323 sb = xbt_strbuff_new();
324 xbt_strbuff_append(sb, input);
325 xbt_strbuff_varsubst(sb, p);
327 xbt_test_assert(!strcmp(sb->data, expected),
328 "Input (%s) with patterns (%s) leads to (%s) instead of (%s)",
329 input, patterns, sb->data, expected);
330 xbt_strbuff_free(sb);
333 XBT_TEST_SUITE("xbt_strbuff", "String Buffers");
334 XBT_TEST_UNIT("xbt_strbuff_substitute", test_strbuff_substitute, "test the function xbt_strbuff_substitute")
336 xbt_test_add("Empty");
339 xbt_test_add("Value shorter, no braces, only variable");
340 mytest("$tutu", "tutu=t", "t");
341 xbt_test_add("Value shorter, braces, only variable");
342 mytest("${tutu}", "tutu=t", "t");
343 xbt_test_add("Value shorter, no braces, data after");
344 mytest("$tutu toto", "tutu=t", "t toto");
345 xbt_test_add("Value shorter, braces, data after");
346 mytest("${tutu} toto", "tutu=t", "t toto");
347 xbt_test_add("Value shorter, no braces, data before");
348 mytest("toto $tutu", "tutu=t", "toto t");
349 xbt_test_add("Value shorter, braces, data before");
350 mytest("toto ${tutu}", "tutu=t", "toto t");
351 xbt_test_add("Value shorter, no braces, data before and after");
352 mytest("toto $tutu tata", "tutu=t", "toto t tata");
353 xbt_test_add("Value shorter, braces, data before and after");
354 mytest("toto ${tutu} tata", "tutu=t", "toto t tata");
356 xbt_test_add("Value as long, no braces, only variable");
357 mytest("$tutu", "tutu=12345", "12345");
358 xbt_test_add("Value as long, braces, only variable");
359 mytest("${tutu}", "tutu=1234567", "1234567");
360 xbt_test_add("Value as long, no braces, data after");
361 mytest("$tutu toto", "tutu=12345", "12345 toto");
362 xbt_test_add("Value as long, braces, data after");
363 mytest("${tutu} toto", "tutu=1234567", "1234567 toto");
364 xbt_test_add("Value as long, no braces, data before");
365 mytest("toto $tutu", "tutu=12345", "toto 12345");
366 xbt_test_add("Value as long, braces, data before");
367 mytest("toto ${tutu}", "tutu=1234567", "toto 1234567");
368 xbt_test_add("Value as long, no braces, data before and after");
369 mytest("toto $tutu tata", "tutu=12345", "toto 12345 tata");
370 xbt_test_add("Value as long, braces, data before and after");
371 mytest("toto ${tutu} tata", "tutu=1234567", "toto 1234567 tata");
373 xbt_test_add("Value longer, no braces, only variable");
374 mytest("$t", "t=tututu", "tututu");
375 xbt_test_add("Value longer, braces, only variable");
376 mytest("${t}", "t=tututu", "tututu");
377 xbt_test_add("Value longer, no braces, data after");
378 mytest("$t toto", "t=tututu", "tututu toto");
379 xbt_test_add("Value longer, braces, data after");
380 mytest("${t} toto", "t=tututu", "tututu toto");
381 xbt_test_add("Value longer, no braces, data before");
382 mytest("toto $t", "t=tututu", "toto tututu");
383 xbt_test_add("Value longer, braces, data before");
384 mytest("toto ${t}", "t=tututu", "toto tututu");
385 xbt_test_add("Value longer, no braces, data before and after");
386 mytest("toto $t tata", "t=tututu", "toto tututu tata");
387 xbt_test_add("Value longer, braces, data before and after");
388 mytest("toto ${t} tata", "t=tututu", "toto tututu tata");
390 xbt_test_add("Value much longer, no braces, only variable");
391 mytest("$t", "t=" force_resize, force_resize);
392 xbt_test_add("Value much longer, no braces, data after");
393 mytest("$t toto", "t=" force_resize, force_resize " toto");
394 xbt_test_add("Value much longer, braces, data after");
395 mytest("${t} toto", "t=" force_resize, force_resize " toto");
396 xbt_test_add("Value much longer, no braces, data before");
397 mytest("toto $t", "t=" force_resize, "toto " force_resize);
398 xbt_test_add("Value much longer, braces, data before");
399 mytest("toto ${t}", "t=" force_resize, "toto " force_resize);
400 xbt_test_add("Value much longer, no braces, data before and after");
401 mytest("toto $t tata", "t=" force_resize, "toto " force_resize " tata");
402 xbt_test_add("Value much longer, braces, data before and after");
403 mytest("toto ${t} tata", "t=" force_resize,
404 "toto " force_resize " tata");
406 xbt_test_add("Escaped $");
407 mytest("\\$tutu", "tutu=t", "\\$tutu");
408 xbt_test_add("Space in var name (with braces)");
409 mytest("${tu ti}", "tu_ti=t", "t");
411 xbt_test_add("Two variables");
412 mytest("$toto $tutu", "toto=1 tutu=2", "1 2");
414 // Commented: I'm too lazy to do a memmove in var name to remove the backslash after use.
415 // Users should use braces.
416 // xbt_test_add("Escaped space in var name", "$tu\\ ti", "tu_ti=t", "t");
418 xbt_test_add("Default value");
419 mytest("${t:-toto}", "", "toto");
420 xbt_test_add("Useless default value (variable already defined)");
421 mytest("${t:-toto}", "t=TRUC", "TRUC");
425 #endif /* SIMGRID_TEST */