From 0f7d71e15e2671125330dbea0104ac58b9353188 Mon Sep 17 00:00:00 2001 From: Ben Winston Date: Fri, 7 Jun 2024 22:56:22 -0400 Subject: when writing, lookup the type of field and write that way --- booki.c | 59 ++++++++++++++++++++--------------------------------------- 1 file changed, 20 insertions(+), 39 deletions(-) (limited to 'booki.c') diff --git a/booki.c b/booki.c index d2a6262..d9ecfbb 100644 --- a/booki.c +++ b/booki.c @@ -480,45 +480,26 @@ void write_book(BOOK book, FILE *output) { fwrite("[[books]]\n", 1, 10, output); char str[100]; int size; - if (book.id) { - size = sprintf(str, "id = %d\n", book.id); - fwrite(str, 1, size, output); - } - if (book.isbn.ptr) { - size = sprintf(str, "isbn = \"%.*s\"\n", book.isbn.len, book.isbn.ptr); - fwrite(str, 1, size, output); - } - if (book.title.ptr) { - size = sprintf(str, "title = \"%.*s\"\n", book.title.len, book.title.ptr); - fwrite(str, 1, size, output); - } - if (book.author.ptr) { - size = sprintf(str, "author = "); - size += concat_es_toml(&(book.author), str + size); - fwrite(str, 1, size, output); - } - if (book.pages) { - size = sprintf(str, "pages = %d\n", book.pages); - fwrite(str, 1, size, output); - } - if (book.published) { - size = sprintf(str, "published = %d\n", book.published); - fwrite(str, 1, size, output); - } - if (book.language.ptr) { - size = sprintf(str, "language = "); - size += concat_es_toml(&(book.language), str + size); - fwrite(str, 1, size, output); - } - if (book.translator.ptr) { - size = sprintf(str, "translator = "); - size += concat_es_toml(&(book.translator), str + size); - fwrite(str, 1, size, output); - } - if (book.on.ptr) { - size = sprintf(str, "on = "); - size += concat_es_toml(&(book.on), str + size); - fwrite(str, 1, size, output); + + DataField datafield; + ES string_field; + int number_field; + for (int i = 0; i < BOOK_FIELDS_COUNT; i++) { + datafield = BOOK_FIELDS[i]; + if (datafield.type == booki_string) { + string_field = get_string_field(book, datafield.name); + if (!string_field.ptr) + continue; + size = sprintf(str, "%s = ", datafield.name); + size += concat_es_toml(&string_field, str + size); + fwrite(str, 1, size, output); + } else if (datafield.type == booki_number) { + number_field = get_number_field(book, datafield.name); + if (!number_field) + continue; + size = sprintf(str, "%s = %d\n", datafield.name, number_field); + fwrite(str, 1, size, output); + } } fwrite("\n", 1, 1, output); // trailing newline between books -- cgit v1.2.3