diff options
| author | Ben Winston | 2024-05-11 20:43:32 -0400 |
|---|---|---|
| committer | Ben Winston | 2024-05-11 20:52:31 -0400 |
| commit | 1b399ebaca063b62885943bf72d86796ebe60abf (patch) | |
| tree | f79ac285276b6841db6a17a7020e14a43135f6c1 | |
| parent | f961562be296bb1628d27eb18e2b6f01174b8bf4 (diff) | |
try using a pointer into an array of books
| -rw-r--r-- | booki.c | 83 |
1 files changed, 49 insertions, 34 deletions
@@ -10,6 +10,7 @@ #define BOOKI_FILE_REL ".local/share/booki/books.toml" #define MAX_SEARCH_OPTS 5 +#define MAX_BOOKS 5000 // STRUCTS struct es { @@ -20,7 +21,7 @@ struct es { struct es default_es = { 0, NULL }; struct Book { - struct es id; + int id; struct es title; struct es author; int pages; @@ -34,17 +35,18 @@ struct Book { //struct es translators[MAX_TRANSLATORS]; }; -struct Book new_book() { - struct Book book; - book.id = default_es; - book.title = default_es; - book.author = default_es; - book.pages = 0; - book.isbn = default_es; +struct Book* init_book(struct Book* book) { + book->id = 0; + book->title = default_es; + book->author = default_es; + book->pages = 0; + book->isbn = default_es; struct es language = { 7, "English" }; // default language - book.language = language; - book.translator = default_es; - book.published = 0; + book->language = language; + book->translator = default_es; + book->published = 0; + + //book_count++; return book; } @@ -106,8 +108,9 @@ struct es parse_string(char* current_pos, char** new_pos) { return output; } -struct Book parse_book(char* current_pos) { - struct Book book = new_book(); +#define ATTR_MATCH(cand, attr) (strncmp(cand, attr, strlen(attr)) == 0) + +void parse_book(char* current_pos, struct Book* book) { char* attr; char c = *current_pos; char* new_pos; @@ -123,33 +126,37 @@ struct Book parse_book(char* current_pos) { current_pos++; // attr should be the name of the attribute, with (possibly) trailing spaces - if (strncmp(attr, "title", 5) == 0) { + if (ATTR_MATCH(attr, "title")) { struct es title = parse_string(current_pos, &new_pos); - book.title = title; + book->title = title; current_pos = new_pos; - } else if (strncmp(attr, "author", 6) == 0) { + } else if (ATTR_MATCH(attr, "author")) { struct es author = parse_string(current_pos, &new_pos); - book.author = author; + book->author = author; current_pos = new_pos; - } else if (strncmp(attr, "language", 8) == 0) { + } else if (ATTR_MATCH(attr, "language")) { struct es language = parse_string(current_pos, &new_pos); - book.language = language; + book->language = language; current_pos = new_pos; - } else if (strncmp(attr, "isbn", 4) == 0) { + } else if (ATTR_MATCH(attr, "isbn")) { struct es isbn = parse_string(current_pos, &new_pos); - book.isbn = isbn; + book->isbn = isbn; current_pos = new_pos; - } else if (strncmp(attr, "translator", 10) == 0) { + } else if (ATTR_MATCH(attr, "translator")) { struct es translator = parse_string(current_pos, &new_pos); - book.translator = translator; + book->translator = translator; current_pos = new_pos; - } else if (strncmp(attr, "pages", 5) == 0) { + } else if (ATTR_MATCH(attr, "pages")) { int pages = parse_int(current_pos, &new_pos); - book.pages = pages; + book->pages = pages; current_pos = new_pos; - } else if (strncmp(attr, "published", 9) == 0) { + } else if (ATTR_MATCH(attr, "published")) { int published = parse_int(current_pos, &new_pos); - book.published = published; + book->published = published; + current_pos = new_pos; + } else if (ATTR_MATCH(attr, "id")) { + int id = parse_int(current_pos, &new_pos); + book->id = id; current_pos = new_pos; } @@ -160,7 +167,6 @@ struct Book parse_book(char* current_pos) { PARSE_LINE += 1; c = *(++current_pos); } - return book; } char* next_book(char* current_pos) { @@ -328,10 +334,13 @@ char** search(int argc, char* argv[]) { } // book loop - struct Book book; + struct Book books[MAX_BOOKS]; + int book_count = 0; + struct Book* book; char* cur_data = data; while ((cur_data = next_book(cur_data)) != NULL) { - book = parse_book(cur_data); + book = init_book(&(books[book_count])); + parse_book(cur_data, book); char* field; int i; @@ -341,13 +350,13 @@ char** search(int argc, char* argv[]) { // compare fields if (strcmp(field, "title") == 0) { - if (!regex_match(search_opts.args[i], book.title)) + if (!regex_match(search_opts.args[i], book->title)) break; } else if (strcmp(field, "author") == 0) { - if (!regex_match(search_opts.args[i], book.author)) + if (!regex_match(search_opts.args[i], book->author)) break; } else if (strcmp(field, "language") == 0) { - if (!regex_match(search_opts.args[i], book.language)) + if (!regex_match(search_opts.args[i], book->language)) break; } else { printf("unsupported field: %s\n", field); @@ -357,8 +366,14 @@ char** search(int argc, char* argv[]) { print = i == search_opts.count; if (print) { - print_book(book, search_opts.show); + print_book(*book, search_opts.show); } + book_count++; + } + + printf("\n--\ncount: %d\n", book_count); + for (int i = 0; i < book_count; i++) { + print_book(books[i], true); } free(data); |
