From 587d0af0dd2060a033a69ea641e3993511809a7d Mon Sep 17 00:00:00 2001 From: Ben Winston Date: Sun, 9 Jun 2024 21:56:45 -0400 Subject: preserve unrecognized parts of a book in their entirety --- booki.c | 44 ++++++++++++++++++++++++++++---------------- 1 file changed, 28 insertions(+), 16 deletions(-) (limited to 'booki.c') diff --git a/booki.c b/booki.c index 2379943..bfdcf24 100644 --- a/booki.c +++ b/booki.c @@ -581,6 +581,7 @@ long parse_int(char* current_pos, char** new_pos) { } #define SEEK_UNTIL(ptr, ch) while (*ptr != ch) ptr++; +#define SEEK_UNTIL_EITHER(ptr, ch1, ch2) while (*ptr != ch1 && *ptr != ch2) ptr++; #define SEEK_WHILE(ptr, ch) while (*ptr == ch) ptr++; ES parse_string(char* current_pos, char** new_pos) { @@ -645,6 +646,23 @@ ES parse_strings(char* current_pos, char** new_pos) { } } +void add_to_other(BOOK* book, char* ptr, int len) { + if (book->_other.len == 0) { + ES other; + other.len = len; + other.ptr = ptr; + other.next = NULL; + book->_other = other; + } else { + ES* last = &(book->_other); + while (last->next != NULL) + last = last->next; + last->next = (ES*)malloc(sizeof(ES)); + last->next->len = len; + last->next->ptr = ptr; + last->next->next = NULL; + } +} void parse_book(char* current_pos, BOOK* book) { char* attr; @@ -654,7 +672,15 @@ void parse_book(char* current_pos, BOOK* book) { while (c != '\n') { // we start at the beginning of a line attr = current_pos; - SEEK_UNTIL(current_pos, '='); + SEEK_UNTIL_EITHER(current_pos, '=', '\n'); + + // if we reached the end of a line, we never hit an equals sign + // so capture the line as-is and continue + if (*current_pos == '\n') { + add_to_other(book, attr, current_pos - attr); + c = *(++current_pos); + continue; + } // go past the equals sign current_pos++; @@ -699,21 +725,7 @@ void parse_book(char* current_pos, BOOK* book) { } else { // anything we didn't match should be captured as-is SEEK_UNTIL(current_pos, '\n'); - if (book->_other.len == 0) { - ES other; - other.len = current_pos - attr; - other.ptr = attr; - other.next = NULL; - book->_other = other; - } else { - ES* last = &(book->_other); - while (last->next != NULL) - last = last->next; - last->next = (ES*)malloc(sizeof(ES)); - last->next->len = current_pos - attr; - last->next->ptr = attr; - last->next->next = NULL; - } + add_to_other(book, attr, current_pos - attr); } // go to (and then past) the newline -- cgit v1.2.3