From 0458f06afe3b0815505c4da88d416ac0b31fd215 Mon Sep 17 00:00:00 2001 From: Scoopta Date: Mon, 16 Dec 2019 23:38:25 -0800 Subject: [PATCH] Caching now checks for an exact match, this prevents some weird edge cases when using search parsing --- src/wofi.c | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/src/wofi.c b/src/wofi.c index b424f5e..98b2ff0 100644 --- a/src/wofi.c +++ b/src/wofi.c @@ -342,17 +342,36 @@ void wofi_write_cache(const gchar* mode, const gchar* cmd) { char* line = NULL; size_t size = 0; while(getline(&line, &size, file) != -1) { - struct cache_line* node = malloc(sizeof(struct cache_line)); - if(strstr(line, cmd) != NULL) { + char* space = strchr(line, ' '); + char* nl = strchr(line, '\n'); + if(nl != NULL) { + *nl = 0; + } + if(space != NULL && strcmp(cmd, space + 1) == 0) { + struct cache_line* node = malloc(sizeof(struct cache_line)); uint64_t count = strtol(line, NULL, 10) + 1; char num[6]; snprintf(num, 5, "%" PRIu64, count); node->line = utils_concat(4, num, " ", cmd, "\n"); inc_count = true; - } else { - node->line = strdup(line); + wl_list_insert(&lines, &node->link); + } + } + free(line); + line = NULL; + size = 0; + fseek(file, 0, SEEK_SET); + while(getline(&line, &size, file) != -1) { + char* space = strchr(line, ' '); + char* nl = strchr(line, '\n'); + if(nl != NULL) { + *nl = 0; + } + if(space == NULL || strcmp(cmd, space + 1) != 0) { + struct cache_line* node = malloc(sizeof(struct cache_line)); + node->line = utils_concat(2, line, "\n"); + wl_list_insert(&lines, &node->link); } - wl_list_insert(&lines, &node->link); } free(line); fclose(file);