From 0f89439134f82b2fac9cfbe46977639f830894ba Mon Sep 17 00:00:00 2001 From: josuah Date: Sat, 26 Nov 2016 18:55:47 +0100 Subject: [PATCH] Simplified a little some functions --- buffer.c | 46 +++++++++++++++++++++------------------------- main.h | 8 ++------ 2 files changed, 23 insertions(+), 31 deletions(-) diff --git a/buffer.c b/buffer.c index d7b874d..f0549f5 100644 --- a/buffer.c +++ b/buffer.c @@ -26,11 +26,10 @@ fill_buffer(char *separator) buffer->first = buffer->current = buffer->last = last; /* read the file into a doubly linked list of lines */ - while (fgets(string, LINE_SIZE, fp)) { + while (fgets(string, LINE_SIZE, fp)) last = add_line(buffer, buffer->total, string, separator, last); - } - /* prevent initial current line to be a header and set it up */ + /* prevent initial current line to be a header */ buffer->current = buffer->first; while (buffer->current->next && buffer->current->header) buffer->current = buffer->current->next; @@ -109,19 +108,6 @@ parse_line(char *s, char *separator) } -void -free_line(Line *line) -{ - line->content = NULL; - free(line->content); - - line->comment = NULL; - free(line->comment); - - free(line); -} - - /* * Free the buffer, also recursing the doubly linked list. */ @@ -129,7 +115,15 @@ void free_buffer(Buffer *buffer) { while (buffer->first) { - free_line(buffer->first); + + buffer->first->content = NULL; + free(buffer->first->content); + + buffer->first->comment = NULL; + free(buffer->first->comment); + + free(buffer->first); + buffer->first = buffer->first->next; } @@ -147,17 +141,20 @@ free_buffer(Buffer *buffer) void filter_lines(Buffer *buffer, int inc) { - Line *line = buffer->first; - char **tokv = NULL; - char *s, buf[sizeof buffer->input]; - int tokc = 0, n = 0/*, len*/; + Line *line = buffer->first; + char **tokv = NULL; + char *s, buf[sizeof buffer->input]; + size_t n = 0, tokc = 0; /* tokenize input from space characters, this comes from dmenu */ strcpy(buf, buffer->input); - for (s = strtok(buf, " "); s; tokv[tokc - 1] = s, s = strtok(NULL, " ")) + for (s = strtok(buf, " "); s; s = strtok(NULL, " ")) { if (++tokc > n && !(tokv = realloc(tokv, ++n * sizeof(*tokv)))) die("cannot realloc memory for tokv\n"); + tokv[tokc - 1] = s; + } + /* match lines */ buffer->matching = 0; while (line) { @@ -178,10 +175,9 @@ filter_lines(Buffer *buffer, int inc) * Return whecher the line matches every string from tokv. */ int -match_line(Line *line, char **tokv, int tokc) +match_line(Line *line, char **tokv, size_t tokc) { - int i; - size_t match = 1, offset = 0; + size_t i, match = 1, offset = 0; if (line->header) return 1; diff --git a/main.h b/main.h index e749203..4d504ae 100644 --- a/main.h +++ b/main.h @@ -1,6 +1,6 @@ #define LINE_SIZE 1024 #define OFFSET 5 -#define CONTINUE 2 +#define CONTINUE 2 /* as opposed to EXIT_SUCCESS and EXIT_FAILURE */ #define LENGTH(x) (sizeof(x) / sizeof(*x)) #define CONTROL(char) (char ^ 0x40) @@ -8,9 +8,6 @@ #define MAX(X, Y) (((X) > (Y)) ? (X) : (Y)) -enum { PREV = 0, NEXT, ANY }; - - /* * Options from the command line, to pass to each function that need some */ @@ -65,12 +62,11 @@ void usage(void); Buffer * fill_buffer(char *); void free_buffer(Buffer *); -void free_line(Line *); Line * add_line(Buffer *, int, char *, char *, Line *); Line * parse_line(char *, char *); Line * matching_next(Line *); Line * matching_prev(Line *); -int match_line(Line *, char *[], int); +int match_line(Line *, char **, size_t); void filter_lines(Buffer *, int); -- 2.11.4.GIT