aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBen Winston2024-05-28 21:28:46 -0400
committerBen Winston2024-05-28 21:28:46 -0400
commit9e76cc03e35e623556c6388006be755dde467555 (patch)
treee89e0fbb66c8228c56422bb9aae3829eb1fd778f
parent6f52b2fb97db698fd1616fb6c26901b966f8989b (diff)
streamline concat-ing strings
-rw-r--r--booki.c42
1 files changed, 25 insertions, 17 deletions
diff --git a/booki.c b/booki.c
index 843ed9b..adecca2 100644
--- a/booki.c
+++ b/booki.c
@@ -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];