blob: 650089dc3cc3c9d143f0858185754c49c40e3b66 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
/*
* vim:ts=4:sw=4:expandtab
*
* i3 - an improved dynamic tiling window manager
* © 2009 Michael Stapelberg and contributors (see also: LICENSE)
*
*/
// copied from i3:libi3/format_placeholders.c
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "i3status.h"
#ifndef CS_STARTS_WITH
#define CS_STARTS_WITH(string, needle) (strncmp((string), (needle), strlen((needle))) == 0)
#endif
/*
* Replaces occurrences of the defined placeholders in the format string.
*
*/
char *format_placeholders(const char *format, placeholder_t *placeholders, int num) {
if (format == NULL)
return NULL;
/* We have to first iterate over the string to see how much buffer space
* we need to allocate. */
int buffer_len = strlen(format) + 1;
for (const char *walk = format; *walk != '\0'; walk++) {
for (int i = 0; i < num; i++) {
if (!CS_STARTS_WITH(walk, placeholders[i].name))
continue;
buffer_len = buffer_len - strlen(placeholders[i].name) + strlen(placeholders[i].value);
walk += strlen(placeholders[i].name) - 1;
break;
}
}
/* Now we can parse the format string. */
char buffer[buffer_len];
char *outwalk = buffer;
for (const char *walk = format; *walk != '\0'; walk++) {
if (*walk != '%') {
*(outwalk++) = *walk;
continue;
}
bool matched = false;
for (int i = 0; i < num; i++) {
if (!CS_STARTS_WITH(walk, placeholders[i].name)) {
continue;
}
matched = true;
outwalk += sprintf(outwalk, "%s", placeholders[i].value);
walk += strlen(placeholders[i].name) - 1;
break;
}
if (!matched)
*(outwalk++) = *walk;
}
*outwalk = '\0';
return sstrdup(buffer);
}
|