use format_placeholder for file_content

This commit is contained in:
Felix Buehler 2020-04-03 15:58:13 +02:00
parent 3374e1605d
commit f0b5758c72

View File

@ -12,6 +12,8 @@
#include <errno.h> #include <errno.h>
#include "i3status.h" #include "i3status.h"
#define STRING_SIZE 10
static void *scalloc(size_t size) { static void *scalloc(size_t size) {
void *result = calloc(size, 1); void *result = calloc(size, 1);
if (result == NULL) { if (result == NULL) {
@ -42,32 +44,32 @@ void print_file_contents(yajl_gen json_gen, char *buffer, const char *title, con
START_COLOR("color_bad"); START_COLOR("color_bad");
} }
for (; *walk != '\0'; walk++) { // remove newline chars
if (*walk != '%') { char *src, *dst;
*(outwalk++) = *walk; for (src = dst = buf; *src != '\0'; src++) {
} else if (BEGINS_WITH(walk + 1, "title")) { *dst = *src;
outwalk += sprintf(outwalk, "%s", title); if (*dst != '\n') {
walk += strlen("title"); dst++;
} else if (BEGINS_WITH(walk + 1, "content")) {
for (char *s = buf; *s != '\0' && n > 0; s++, n--) {
if (*s != '\n') {
*(outwalk++) = *s;
}
}
walk += strlen("content");
} else if (BEGINS_WITH(walk + 1, "errno")) {
outwalk += sprintf(outwalk, "%d", errno);
walk += strlen("errno");
} else if (BEGINS_WITH(walk + 1, "error")) {
outwalk += sprintf(outwalk, "%s", strerror(errno));
walk += strlen("error");
} else {
*(outwalk++) = '%';
} }
} }
*dst = '\0';
char string_errno[STRING_SIZE];
sprintf(string_errno, "%d", errno);
placeholder_t placeholders[] = {
{.name = "%title", .value = title},
{.name = "%content", .value = buf},
{.name = "%errno", .value = string_errno},
{.name = "%error", .value = strerror(errno)}};
const size_t num = sizeof(placeholders) / sizeof(placeholder_t);
buffer = format_placeholders(walk, &placeholders[0], num);
free(buf); free(buf);
END_COLOR; END_COLOR;
OUTPUT_FULL_TEXT(buffer); OUTPUT_FULL_TEXT(buffer);
free(buffer);
} }