aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Winston2024-06-09 21:56:45 -0400
committerBen Winston2024-06-09 21:56:45 -0400
commit587d0af0dd2060a033a69ea641e3993511809a7d (patch)
tree41efd44dcd6ecd069e61b6a09ab6b1b23d66d8b9
parent134e4f29275ba41254aae5a0ea2b81e3a7de8276 (diff)
preserve unrecognized parts of a book in their entirety
-rw-r--r--booki.c44
1 files changed, 28 insertions, 16 deletions
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