aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Winston2024-06-07 22:56:22 -0400
committerBen Winston2024-06-07 22:56:22 -0400
commit0f7d71e15e2671125330dbea0104ac58b9353188 (patch)
treece454b86adeb2943196c87b2be29f5abbc0691b9
parent2f0934a848506741dc409e6ae6dbbf9e1e8ba67b (diff)
when writing, lookup the type of field and write that way
-rw-r--r--booki.c59
1 files changed, 20 insertions, 39 deletions
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