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 #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); }