diff options
author | Felix Buehler <account@buehler.rocks> | 2020-04-03 15:58:13 +0200 |
---|---|---|
committer | Felix Buehler <account@buehler.rocks> | 2020-04-03 16:15:38 +0200 |
commit | f0b5758c72f331f598d18f6127e3d55d803af5d7 (patch) | |
tree | 577990475ff54ac7962db95651d250f35650b7a9 /src/print_file_contents.c | |
parent | 3374e1605d718816d08ba91721aa82ec73086b9d (diff) |
use format_placeholder for file_content
Diffstat (limited to 'src/print_file_contents.c')
-rw-r--r-- | src/print_file_contents.c | 44 |
1 files changed, 23 insertions, 21 deletions
diff --git a/src/print_file_contents.c b/src/print_file_contents.c index 91ad80e..65813f5 100644 --- a/src/print_file_contents.c +++ b/src/print_file_contents.c @@ -12,6 +12,8 @@ #include <errno.h> #include "i3status.h" +#define STRING_SIZE 10 + static void *scalloc(size_t size) { void *result = calloc(size, 1); 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"); } - for (; *walk != '\0'; walk++) { - if (*walk != '%') { - *(outwalk++) = *walk; - } else if (BEGINS_WITH(walk + 1, "title")) { - outwalk += sprintf(outwalk, "%s", title); - walk += strlen("title"); - } 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++) = '%'; + // remove newline chars + char *src, *dst; + for (src = dst = buf; *src != '\0'; src++) { + *dst = *src; + if (*dst != '\n') { + dst++; } } + *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); END_COLOR; OUTPUT_FULL_TEXT(buffer); + free(buffer); } |