diff options
Diffstat (limited to 'booki.c')
| -rw-r--r-- | booki.c | 42 |
1 files changed, 25 insertions, 17 deletions
@@ -21,6 +21,14 @@ struct es { struct es default_es = { 0, NULL, NULL }; +int concat_es(struct es* str, char* buf) { + 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; +} + struct Book { int id; struct es title; @@ -395,29 +403,29 @@ struct search_opt parse_search_options(int argc, char* argv[]) { } void print_book(struct Book book, bool all_fields) { - if (book.author.next == NULL) { - printf("%.*s by %.*s\n", book.title.len, book.title.ptr, book.author.len, book.author.ptr); - } else { - char str[100]; - int size = sprintf(str, "%.*s", book.author.len, book.author.ptr); - struct es* next = &(book.author); - while ((next = next->next) != NULL) - size += sprintf(str + size, ", %.*s", next->len, next->ptr); - printf("%.*s by %.*s\n", book.title.len, book.title.ptr, size, str); - } + char str[100]; + int size = concat_es(&(book.author), str); + printf("%.*s by %.*s\n", book.title.len, book.title.ptr, size, str); if (all_fields) { char* esfmt = " - %s: %.*s\n"; char* intfmt = " - %s: %d\n"; - if (book.isbn.ptr) + if (book.isbn.ptr) { printf(esfmt, "isbn", book.isbn.len, book.isbn.ptr); - if (book.language.ptr) - printf(esfmt, "language", book.language.len, book.language.ptr); - if (book.translator.ptr) - printf(esfmt, "translator", book.translator.len, book.translator.ptr); - if (book.pages) + } + if (book.language.ptr) { + size = concat_es(&(book.language), str); + printf(esfmt, "language", size, str); + } + if (book.translator.ptr) { + size = concat_es(&(book.translator), str); + printf(esfmt, "translator", size, str); + } + if (book.pages) { printf(intfmt, "pages", book.pages); - if (book.published) + } + if (book.published) { printf(intfmt, "published", book.published); + } int on_count = 0; struct es tmp = book.on[on_count]; |
