1 /* $Id: buff.c 3483 2007-05-07 11:18:56Z mquinson $ */
3 /* strbuff -- string buffers */
5 /* Copyright (c) 2007 Martin Quinson. */
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 void xbt_strbuff_empty(xbt_strbuff_t b)
33 xbt_strbuff_t xbt_strbuff_new(void)
35 xbt_strbuff_t res = malloc(sizeof(s_xbt_strbuff_t));
36 res->data = malloc(512);
38 xbt_strbuff_empty(res);
42 /** @brief creates a new string buffer containing the provided string
44 * Beware, we store the ctn directly, not a copy of it
46 xbt_strbuff_t xbt_strbuff_new_from(char *ctn)
48 xbt_strbuff_t res = malloc(sizeof(s_xbt_strbuff_t));
50 res->used = res->size = strlen(ctn);
54 /** @brief frees only the container without touching to the contained string */
55 void xbt_strbuff_free_container(xbt_strbuff_t b)
60 /** @brief frees the buffer and its content */
61 void xbt_strbuff_free(xbt_strbuff_t b)
70 void xbt_strbuff_append(xbt_strbuff_t b, const char *toadd)
76 THROW0(arg_error, 0, "Asked to append stuff to NULL buffer");
78 addlen = strlen(toadd);
79 needed_space = b->used + addlen + 1;
81 if (needed_space > b->size) {
83 realloc(b->data, MAX(minimal_increment + b->used, needed_space));
84 b->size = MAX(minimal_increment + b->used, needed_space);
86 strcpy(b->data + b->used, toadd);
90 void xbt_strbuff_chomp(xbt_strbuff_t b)
92 while (b->data[b->used] == '\n') {
93 b->data[b->used] = '\0';
99 void xbt_strbuff_trim(xbt_strbuff_t b)
101 xbt_str_trim(b->data, " ");
102 b->used = strlen(b->data);
105 /** @brief Replaces a set of variables by their values
107 * @param b buffer to modify
108 * @param patterns variables to substitute in the buffer
110 * Both '$toto' and '${toto}' are valid (and the two writing are equivalent).
112 * If the variable name contains spaces, use the brace version (ie, ${toto tutu})
114 * You can provide a default value to use if the variable is not set in the dict by using
115 * '${var:=default}' or '${var:-default}'. These two forms are equivalent, even if they
116 * shouldn't to respect the shell standard (:= form should set the value in the dict,
117 * but does not) (BUG).
119 void xbt_strbuff_varsubst(xbt_strbuff_t b, xbt_dict_t patterns)
122 char *end; /* pointers around the parsed chunk */
123 int in_simple_quote = 0, in_double_quote = 0;
126 if (b->data[0] == '\0')
133 /* Protected char; pass the protection */
136 THROW0(arg_error, 0, "String ends with \\");
140 if (!in_double_quote) {
141 /* simple quote not protected by double ones, note it */
142 in_simple_quote = !in_simple_quote;
146 if (!in_simple_quote) {
147 /* double quote protected by simple ones, note it */
148 in_double_quote = !in_double_quote;
153 if (!in_simple_quote) {
154 /* Go for the substitution. First search the variable name */
155 char *beg_var, *end_var; /* variable name boundary */
156 char *beg_subst, *end_subst = NULL; /* where value should be written to */
157 char *value, *default_value = NULL;
162 if (*(++end) == '{') {
163 /* the variable name is enclosed in braces. */
165 /* Search name's end */
167 while (*end_var != '\0' && *end_var != '}') {
168 /* TODO: we do not respect the standard for ":=", we should set this value in the dict */
170 && ((*(end_var + 1) == '=') || (*(end_var + 1) == '-'))) {
171 /* damn, we have a default value */
172 char *p = end_var + 1;
173 while (*p != '\0' && *p != '}')
177 "Variable default value not terminated ('}' missing)");
179 default_value = xbt_malloc(p - end_var - 1);
180 memcpy(default_value, end_var + 2, p - end_var - 2);
181 default_value[p - end_var - 2] = '\0';
183 end_subst = p + 1; /* eat '}' */
189 if (*end_var == '\0')
191 "Variable name not terminated ('}' missing)");
193 if (!end_subst) /* already set if there's a default value */
194 end_subst = end_var + 1; /* also kill the } in the name */
196 if (end_var == beg_var)
197 THROW0(arg_error, 0, "Variable name empty (${} is not valid)");
201 /* name given directly */
204 while (*end_var != '\0' && *end_var != ' ' && *end_var != '\t'
208 if (end_var == beg_var)
209 THROW0(arg_error, 0, "Variable name empty ($ is not valid)");
211 /* DEBUG5("var='%.*s'; subst='%.*s'; End_var = '%s'",
212 end_var-beg_var,beg_var,
213 end_subst-beg_subst,beg_subst,
216 /* ok, we now have the variable name. Search the dictionary for the substituted value */
218 xbt_dict_get_or_null_ext(patterns, beg_var, end_var - beg_var);
219 /* DEBUG1("Deal with '%s'",b->data);
220 DEBUG4("Search for %.*s, found %s (default value = %s)\n",
221 end_var-beg_var,beg_var,
222 (value?value:"(no value)"),
223 (default_value?default_value:"(no value)"));*/
226 value = xbt_strdup(value);
227 else if (default_value)
228 value = xbt_strdup(default_value);
230 value = xbt_strdup("");
232 /* En route for the actual substitution */
233 val_len = strlen(value);
234 // DEBUG2("val_len = %d, key_len=%d",val_len,end_subst-beg_subst);
235 if (val_len <= end_subst - beg_subst) {
236 /* enough room to do the substitute in place */
237 // DEBUG4("Substitute key name by its value: ie '%.*s' by '%.*s'",end_subst-beg_subst,beg_subst,val_len,value);
238 memmove(beg_subst, value, val_len); /* substitute */
239 // DEBUG1("String is now: '%s'",b->data);
240 /* DEBUG8("Move end of string closer (%d chars moved) :\n-'%.*s%.*s'\n+'%.*s%s'",
241 b->used - (end_subst - b->data) + 1,
242 beg_subst-b->data,b->data,
243 b->used-(end_subst-b->data)+1,beg_subst+val_len,
244 beg_subst-b->data,b->data,
246 memmove(beg_subst + val_len, end_subst, b->used - (end_subst - b->data) + 1); /* move the end of the string closer */
247 // DEBUG1("String is now: '%s'",b->data);
248 end = beg_subst + val_len; /* update the currently explored char in the overall loop */
249 // DEBUG1("end of substituted section is now '%s'",end);
250 b->used -= end_subst - beg_subst - val_len; /* update string buffer used size */
251 // DEBUG3("Used:%d end:%d ending char:%d",b->used,end-b->data,*end);
253 /* we have to extend the data area */
255 val_len - (end_subst - beg_subst) + 1 /*don't forget \0 */ ;
256 int newused = b->used + tooshort;
257 end += tooshort; /* update the pointer of the overall loop */
258 // DEBUG2("Too short (by %d chars; %d chars left in area)",val_len- (end_subst-beg_subst), b->size - b->used);
259 if (newused > b->size) {
260 /* We have to realloc the data area before (because b->size is too small). We have to update our pointers, too */
262 realloc(b->data, b->used + MAX(minimal_increment, tooshort));
263 int offset = newdata - b->data;
265 b->size = b->used + MAX(minimal_increment, tooshort);
270 memmove(beg_subst + val_len, end_subst, b->used - (end_subst - b->data) + 2); /* move the end of the string a bit further */
271 memmove(beg_subst, value, val_len); /* substitute */
273 // DEBUG1("String is now: %s",b->data);
280 end--; /* compensate the next end++*/
292 #include "xbt/strbuff.h"
294 /* 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 */
295 #define force_resize \
296 "1.........1.........2.........3.........4.........5.........6.........7.........8.........9........." \
297 "2.........1.........2.........3.........4.........5.........6.........7.........8.........9........." \
298 "3.........1.........2.........3.........4.........5.........6.........7.........8.........9........." \
299 "4.........1.........2.........3.........4.........5.........6.........7.........8.........9........." \
300 "5.........1.........2.........3.........4.........5.........6.........7.........8.........9........." \
301 "6.........1.........2.........3.........4.........5.........6.........7.........8.........9........." \
302 "7.........1.........2.........3.........4.........5.........6.........7.........8.........9........." \
303 "8.........1.........2.........3.........4.........5.........6.........7.........8.........9........." \
304 "9.........1.........2.........3.........4.........5.........6.........7.........8.........9........." \
305 "0.........1.........2.........3.........4.........5.........6.........7.........8.........9........."
307 static void mytest(const char *input, const char *patterns,
308 const char *expected)
310 xbt_dynar_t dyn_patterns; /* splited string */
311 xbt_dict_t p; /* patterns */
313 char *str; /*foreach */
314 xbt_strbuff_t sb; /* what we test */
317 dyn_patterns = xbt_str_split(patterns, " ");
318 xbt_dynar_foreach(dyn_patterns, cpt, str) {
319 xbt_dynar_t keyvals = xbt_str_split(str, "=");
320 char *key = xbt_dynar_get_as(keyvals, 0, char *);
321 char *val = xbt_dynar_get_as(keyvals, 1, char *);
322 xbt_str_subst(key, '_', ' ', 0); // to put space in names without breaking the enclosing dynar_foreach
323 xbt_dict_set(p, key, xbt_strdup(val), free);
324 xbt_dynar_free(&keyvals);
326 xbt_dynar_free(&dyn_patterns);
327 sb = xbt_strbuff_new();
328 xbt_strbuff_append(sb, input);
329 xbt_strbuff_varsubst(sb, p);
331 xbt_test_assert4(!strcmp(sb->data, expected),
332 "Input (%s) with patterns (%s) leads to (%s) instead of (%s)",
333 input, patterns, sb->data, expected);
334 xbt_strbuff_free(sb);
337 XBT_TEST_SUITE("xbt_strbuff", "String Buffers");
338 XBT_TEST_UNIT("xbt_strbuff_substitute", test_strbuff_substitute,"test the function xbt_strbuff_substitute")
340 xbt_test_add0("Empty");
343 xbt_test_add0("Value shorter, no braces, only variable");
344 mytest("$tutu", "tutu=t", "t");
345 xbt_test_add0("Value shorter, braces, only variable");
346 mytest("${tutu}", "tutu=t", "t");
347 xbt_test_add0("Value shorter, no braces, data after");
348 mytest("$tutu toto", "tutu=t", "t toto");
349 xbt_test_add0("Value shorter, braces, data after");
350 mytest("${tutu} toto", "tutu=t", "t toto");
351 xbt_test_add0("Value shorter, no braces, data before");
352 mytest("toto $tutu", "tutu=t", "toto t");
353 xbt_test_add0("Value shorter, braces, data before");
354 mytest("toto ${tutu}", "tutu=t", "toto t");
355 xbt_test_add0("Value shorter, no braces, data before and after");
356 mytest("toto $tutu tata", "tutu=t", "toto t tata");
357 xbt_test_add0("Value shorter, braces, data before and after");
358 mytest("toto ${tutu} tata", "tutu=t", "toto t tata");
360 xbt_test_add0("Value as long, no braces, only variable");
361 mytest("$tutu", "tutu=12345", "12345");
362 xbt_test_add0("Value as long, braces, only variable");
363 mytest("${tutu}", "tutu=1234567", "1234567");
364 xbt_test_add0("Value as long, no braces, data after");
365 mytest("$tutu toto", "tutu=12345", "12345 toto");
366 xbt_test_add0("Value as long, braces, data after");
367 mytest("${tutu} toto", "tutu=1234567", "1234567 toto");
368 xbt_test_add0("Value as long, no braces, data before");
369 mytest("toto $tutu", "tutu=12345", "toto 12345");
370 xbt_test_add0("Value as long, braces, data before");
371 mytest("toto ${tutu}", "tutu=1234567", "toto 1234567");
372 xbt_test_add0("Value as long, no braces, data before and after");
373 mytest("toto $tutu tata", "tutu=12345", "toto 12345 tata");
374 xbt_test_add0("Value as long, braces, data before and after");
375 mytest("toto ${tutu} tata", "tutu=1234567", "toto 1234567 tata");
377 xbt_test_add0("Value longer, no braces, only variable");
378 mytest("$t", "t=tututu", "tututu");
379 xbt_test_add0("Value longer, braces, only variable");
380 mytest("${t}", "t=tututu", "tututu");
381 xbt_test_add0("Value longer, no braces, data after");
382 mytest("$t toto", "t=tututu", "tututu toto");
383 xbt_test_add0("Value longer, braces, data after");
384 mytest("${t} toto", "t=tututu", "tututu toto");
385 xbt_test_add0("Value longer, no braces, data before");
386 mytest("toto $t", "t=tututu", "toto tututu");
387 xbt_test_add0("Value longer, braces, data before");
388 mytest("toto ${t}", "t=tututu", "toto tututu");
389 xbt_test_add0("Value longer, no braces, data before and after");
390 mytest("toto $t tata", "t=tututu", "toto tututu tata");
391 xbt_test_add0("Value longer, braces, data before and after");
392 mytest("toto ${t} tata", "t=tututu", "toto tututu tata");
394 xbt_test_add0("Value much longer, no braces, only variable");
395 mytest("$t", "t=" force_resize, force_resize);
396 xbt_test_add0("Value much longer, no braces, data after");
397 mytest("$t toto", "t=" force_resize, force_resize " toto");
398 xbt_test_add0("Value much longer, braces, data after");
399 mytest("${t} toto", "t=" force_resize, force_resize " toto");
400 xbt_test_add0("Value much longer, no braces, data before");
401 mytest("toto $t", "t=" force_resize, "toto " force_resize);
402 xbt_test_add0("Value much longer, braces, data before");
403 mytest("toto ${t}", "t=" force_resize, "toto " force_resize);
404 xbt_test_add0("Value much longer, no braces, data before and after");
405 mytest("toto $t tata", "t=" force_resize, "toto " force_resize " tata");
406 xbt_test_add0("Value much longer, braces, data before and after");
407 mytest("toto ${t} tata", "t=" force_resize, "toto " force_resize " tata");
409 xbt_test_add0("Escaped $");
410 mytest("\\$tutu", "tutu=t", "\\$tutu");
411 xbt_test_add0("Space in var name (with braces)");
412 mytest("${tu ti}", "tu_ti=t", "t");
414 xbt_test_add0("Two variables");
415 mytest("$toto $tutu", "toto=1 tutu=2", "1 2");
417 // Commented: I'm too lazy to do a memmove in var name to remove the backslash after use.
418 // Users should use braces.
419 // xbt_test_add0("Escaped space in var name", "$tu\\ ti", "tu_ti=t", "t");
421 xbt_test_add0("Default value");
422 mytest("${t:-toto}", "", "toto");
423 xbt_test_add0("Useless default value (variable already defined)");
424 mytest("${t:-toto}", "t=TRUC", "TRUC");
428 #endif /* SIMGRID_TEST */