diff options
author | Ingo Bürk <admin@airblader.de> | 2020-04-07 08:31:57 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-04-07 08:31:57 +0200 |
commit | e697a1f19fba6108a4aa62ea7d849d36acbcb033 (patch) | |
tree | 8971d9cbab994c3f3a6c366eba0f219099a81ed9 | |
parent | 1858cd8dfb8df43e332acdf7984a1ad26ceecab4 (diff) | |
parent | f0b5758c72f331f598d18f6127e3d55d803af5d7 (diff) |
Merge pull request #404 from Stunkymonkey/format_placeholder-file_content
use format_placeholder for file_content
-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); } |