diff options
| author | Ben Winston | 2024-05-28 21:55:15 -0400 |
|---|---|---|
| committer | Ben Winston | 2024-05-28 21:55:15 -0400 |
| commit | 1009d97e11c95ced88e876b2ab340e507314fff3 (patch) | |
| tree | e4c111fdc6f5b140cac193a15bfc3b76059928ae /booki.c | |
| parent | 17420609eaf0c3c708248a3a7fb4a89aa12a2c1a (diff) | |
fix write_book to write lists of strings as lists
Diffstat (limited to 'booki.c')
| -rw-r--r-- | booki.c | 30 |
1 files changed, 23 insertions, 7 deletions
@@ -21,7 +21,7 @@ struct es { struct es default_es = { 0, NULL, NULL }; -int concat_es(struct es* str, char* buf) { +int concat_es_print(struct es* str, char* buf) { int size = sprintf(buf, "%.*s", str->len, str->ptr); struct es* next = str; while ((next = next->next) != NULL) @@ -29,6 +29,19 @@ int concat_es(struct es* str, char* buf) { return size; } +int concat_es_toml(struct es* str, char* buf) { + if (str->next == NULL) { + return sprintf(buf, "\"%.*s\"\n", str->len, str->ptr); + } + else { + int size = sprintf(buf, "[ \"%.*s\"", str->len, str->ptr); + struct es* next = str; + while ((next = next->next) != NULL) + size += sprintf(buf + size, ", \"%.*s\"", next->len, next->ptr); + return size + sprintf(buf + size, " ]\n"); + } +} + struct Book { int id; struct es title; @@ -404,7 +417,7 @@ struct search_opt parse_search_options(int argc, char* argv[]) { void print_book(struct Book book, bool all_fields) { char str[100]; - int size = concat_es(&(book.author), str); + int size = concat_es_print(&(book.author), str); printf("%.*s by %.*s\n", book.title.len, book.title.ptr, size, str); if (all_fields) { char* esfmt = " - %s: %.*s\n"; @@ -413,11 +426,11 @@ void print_book(struct Book book, bool all_fields) { printf(esfmt, "isbn", book.isbn.len, book.isbn.ptr); } if (book.language.ptr) { - size = concat_es(&(book.language), str); + size = concat_es_print(&(book.language), str); printf(esfmt, "language", size, str); } if (book.translator.ptr) { - size = concat_es(&(book.translator), str); + size = concat_es_print(&(book.translator), str); printf(esfmt, "translator", size, str); } if (book.pages) { @@ -459,7 +472,8 @@ void write_book(struct Book book, FILE *output) { fwrite(str, 1, size, output); } if (book.author.ptr) { - size = sprintf(str, "author = \"%.*s\"\n", book.author.len, book.author.ptr); + size = sprintf(str, "author = "); + size += concat_es_toml(&(book.author), str + size); fwrite(str, 1, size, output); } if (book.pages) { @@ -471,11 +485,13 @@ void write_book(struct Book book, FILE *output) { fwrite(str, 1, size, output); } if (book.language.ptr) { - size = sprintf(str, "language = \"%.*s\"\n", book.language.len, 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 = \"%.*s\"\n", book.translator.len, book.translator.ptr); + size = sprintf(str, "translator = "); + size += concat_es_toml(&(book.translator), str + size); fwrite(str, 1, size, output); } |
