fzy
terminal fuzzy finder picker
git clone https://9o.is/git/fzy.git
commit 20a41f4065401a29fd5448754260e753ccfe0068 parent c38e32f36780a5bc45b879e8db3f84778e3de8ab Author: John Hawthorn <john.hawthorn@gmail.com> Date: Sat, 23 Apr 2016 11:25:47 -0700 Move input reading to choices.c Also refactored and added comments. Diffstat:
| M | choices.c | | | 46 | +++++++++++++++++++++++++++++++++++++++++----- |
| M | choices.h | | | 3 | +++ |
| M | fzy.c | | | 33 | +-------------------------------- |
3 files changed, 45 insertions(+), 37 deletions(-)
diff --git a/choices.c b/choices.c @@ -1,5 +1,6 @@ #include <stdlib.h> #include <stdio.h> +#include <string.h> #include "choices.h" #include "match.h" @@ -18,14 +19,49 @@ static int cmpchoice(const void *_idx1, const void *_idx2) { return -1; } -static void choices_resize(choices_t *c, size_t new_capacity) { - c->strings = realloc(c->strings, new_capacity * sizeof(const char *)); - - if (!c->strings) { - fprintf(stderr, "Error: Can't allocate memory\n"); +static void *safe_realloc(void *buffer, size_t size) { + buffer = realloc(buffer, size); + if (!buffer) { + fprintf(stderr, "Error: Can't allocate memory (%zu bytes)\n", size); abort(); } + return buffer; +} + +void choices_fread(choices_t *c, FILE *file) { + size_t bufsize = 65536, pos = 0; + char *buf = safe_realloc(NULL, bufsize); + + /* Continue reading until we get a "short" read, indicating EOF */ + while ((pos += fread(buf + pos, 1, bufsize - pos, file)) == bufsize) { + bufsize *= 2; + buf = safe_realloc(buf, bufsize); + } + buf[pos] = 0; + + /* Truncate buffer to used size, (maybe) freeing some memory for + * future allocations. + */ + buf = safe_realloc(buf, pos + 1); + + /* Tokenize input and add to choices */ + char *line = buf; + do { + char *nl = strchr(line, '\n'); + if (nl) + *nl++ = '\0'; + + /* Skip empty lines */ + if (*line) + choices_add(c, line); + + line = nl; + } while (line); +} + +static void choices_resize(choices_t *c, size_t new_capacity) { + c->strings = safe_realloc(c->strings, new_capacity * sizeof(const char *)); c->capacity = new_capacity; } diff --git a/choices.h b/choices.h @@ -1,6 +1,8 @@ #ifndef CHOICES_H #define CHOICES_H CHOICES_H +#include <stdio.h> + struct scored_result { double score; const char *str; @@ -18,6 +20,7 @@ typedef struct { } choices_t; void choices_init(choices_t *c); +void choices_fread(choices_t *c, FILE *file); void choices_free(choices_t *c); void choices_add(choices_t *c, const char *choice); size_t choices_available(choices_t *c); diff --git a/fzy.c b/fzy.c @@ -18,37 +18,6 @@ static size_t scrolloff = 1; static const char *prompt = "> "; -static void read_choices(choices_t *c) { - size_t bufsize = 65536; - size_t pos = 0; - size_t sizeread; - - /* Read entire file into contiguous memory buffer */ - char *buf = malloc(bufsize); - while ((sizeread = fread(buf + pos, 1, bufsize - pos, stdin))) { - pos += sizeread; - bufsize *= 2; - buf = realloc(buf, bufsize); - } - buf = realloc(buf, pos + 1); - - buf[pos] = 0; - - /* Tokenize input and add to choices */ - char *line = buf; - do { - char *nl = strchr(line, '\n'); - if (nl) - *nl++ = '\0'; - - /* Skip empty lines */ - if (*line) - choices_add(c, line); - - line = nl; - } while (line); -} - #define SEARCH_SIZE_MAX 4096 static size_t search_size; static char search[SEARCH_SIZE_MAX + 1] = {0}; @@ -279,7 +248,7 @@ int main(int argc, char *argv[]) { choices_t choices; choices_init(&choices); - read_choices(&choices); + choices_fread(&choices, stdin); if (num_lines > choices.size) num_lines = choices.size;