From bbae8d19b0ee88f7b6d9653fd783cb484b70169c Mon Sep 17 00:00:00 2001 From: Ben Winston Date: Tue, 28 May 2024 22:08:17 -0400 Subject: convert 'on' to use list of strings --- booki.c | 87 +++++++++++------------------------------------------------------ 1 file changed, 14 insertions(+), 73 deletions(-) (limited to 'booki.c') diff --git a/booki.c b/booki.c index 95669b8..35857ed 100644 --- a/booki.c +++ b/booki.c @@ -50,12 +50,8 @@ struct Book { struct es isbn; struct es language; struct es translator; - struct es on[5]; + struct es on; int published; - - // maybe? - //struct es authors[MAX_AUTHORS]; - //struct es translators[MAX_TRANSLATORS]; }; void init_book(struct Book* book) { @@ -67,8 +63,7 @@ void init_book(struct Book* book) { struct es language = { 7, "English", NULL }; // default language book->language = language; book->translator = default_es; - for (int i = 0; i < 5; i++) - book->on[i] = default_es; + book->on = default_es; book->published = 0; } @@ -227,32 +222,9 @@ void parse_book(char* current_pos, struct Book* book) { book->id = id; current_pos = new_pos; } else if (ATTR_MATCH(attr, "on")) { - // this is always a list - // look until we hit the opening brace - SEEK_UNTIL(current_pos, '['); - - current_pos++; // go past opening brace - - // loop past opening spaces - SEEK_WHILE(current_pos, ' '); - - // so long as we keep seeing quotes, keep reading them - int on_count = 0; - struct es value; - while ((c = *current_pos) == '"') { - // read the string - value = parse_string(current_pos, &new_pos); - book->on[on_count] = value; - on_count++; - current_pos = new_pos; - - // go past any commas or opening spaces - SEEK_WHILE(current_pos, ' '); - if (*current_pos != ',') - break; - - SEEK_UNTIL(current_pos, '"'); - } + struct es on = parse_strings(current_pos, &new_pos); + book->on = on; + current_pos = new_pos; } // go to (and then past) the newline @@ -439,19 +411,10 @@ void print_book(struct Book book, bool all_fields) { if (book.published) { printf(intfmt, "published", book.published); } - - int on_count = 0; - struct es tmp = book.on[on_count]; - while (tmp.len != 0) { - if (on_count == 0) - printf(" - %s: [ %.*s", "on", tmp.len, tmp.ptr); - else - printf(", %.*s", tmp.len, tmp.ptr); - on_count++; - tmp = book.on[on_count]; + if (book.on.ptr) { + size = concat_es_print(&(book.on), str); + printf(esfmt, "on", size, str); } - if (on_count != 0) - printf(" ]\n"); } } @@ -494,23 +457,10 @@ void write_book(struct Book book, FILE *output) { size += concat_es_toml(&(book.translator), str + size); fwrite(str, 1, size, output); } - - int on_count = 0; - int str_offset = 0; - struct es tmp = book.on[on_count]; - if (tmp.len != 0) { - // got at least one book - str_offset += sprintf(str, "on = [ "); - while (tmp.len != 0) { - if (on_count != 0) - str_offset += sprintf(str + str_offset, ", "); - - str_offset += sprintf(str + str_offset, "\"%.*s\"", tmp.len, tmp.ptr); - on_count++; - tmp = book.on[on_count]; - } - str_offset += sprintf(str + str_offset, " ]\n"); - fwrite(str, 1, str_offset, output); + if (book.on.ptr) { + size = sprintf(str, "on = "); + size += concat_es_toml(&(book.on), str + size); + fwrite(str, 1, size, output); } fwrite("\n", 1, 1, output); // trailing newline between books @@ -553,6 +503,7 @@ void free_book(struct Book book) { free_es(book.author.next); free_es(book.translator.next); free_es(book.language.next); + free_es(book.on.next); } void search(int argc, char* argv[], char* booki_file) { @@ -596,18 +547,8 @@ void search(int argc, char* argv[], char* booki_file) { if (!regex_match(search_opts.args[i], book.language)) break; } else if (ATTR_MATCH(field, "on")) { - int cnt = 0; - struct es tmp = book.on[cnt]; - bool match = false; - while (tmp.len != 0) { - if (regex_match(search_opts.args[i], tmp)) - match = true; - cnt++; - tmp = book.on[cnt]; - } - if (!match) + if (!regex_match(search_opts.args[i], book.on)) break; - } else { printf("unsupported field: %s\n", field); break; -- cgit v1.2.3