summaryrefslogtreecommitdiff
path: root/src/format_placeholders.c
diff options
context:
space:
mode:
authorFelix Buehler <account@buehler.rocks>2020-02-10 18:52:04 +0100
committerFelix Buehler <account@buehler.rocks>2020-02-10 21:05:18 +0100
commitb5a804d1afaef1969de9c32060808fefb81a91e7 (patch)
tree2b03051f15bb30ae2c82321d856658af96d7245e /src/format_placeholders.c
parenteccd4a761817c791e702dbd60f16d36fae82cffe (diff)
use format_placeholder for battery
Diffstat (limited to 'src/format_placeholders.c')
-rw-r--r--src/format_placeholders.c70
1 files changed, 70 insertions, 0 deletions
diff --git a/src/format_placeholders.c b/src/format_placeholders.c
new file mode 100644
index 0000000..650089d
--- /dev/null
+++ b/src/format_placeholders.c
@@ -0,0 +1,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);
+}