2 * vim:ts=4:sw=4:expandtab
4 * i3 - an improved dynamic tiling window manager
5 * © 2009 Michael Stapelberg and contributors (see also: LICENSE)
15 #define STARTS_WITH(string, needle) (strncasecmp((string), (needle), strlen((needle))) == 0)
19 * Replaces occurrences of the defined placeholders in the format string.
22 char *format_placeholders(char *format, placeholder_t *placeholders, int num) {
26 /* We have to first iterate over the string to see how much buffer space
27 * we need to allocate. */
28 int buffer_len = strlen(format) + 1;
29 for (char *walk = format; *walk != '\0'; walk++) {
30 for (int i = 0; i < num; i++) {
31 if (!STARTS_WITH(walk, placeholders[i].name))
34 buffer_len = buffer_len - strlen(placeholders[i].name) + strlen(placeholders[i].value);
35 walk += strlen(placeholders[i].name) - 1;
40 /* Now we can parse the format string. */
41 char buffer[buffer_len];
42 char *outwalk = buffer;
43 for (char *walk = format; *walk != '\0'; walk++) {
50 for (int i = 0; i < num; i++) {
51 if (!STARTS_WITH(walk, placeholders[i].name)) {
56 outwalk += sprintf(outwalk, "%s", placeholders[i].value);
57 walk += strlen(placeholders[i].name) - 1;
66 return sstrdup(buffer);