diff options
| author | Ben Winston | 2024-05-19 12:18:44 -0400 |
|---|---|---|
| committer | Ben Winston | 2024-05-19 12:18:44 -0400 |
| commit | 45aef60358d61dc40daf4c32e89398cd9d81f376 (patch) | |
| tree | d4aa7ea8903d86cc4c30b4c5784b42c2d6855628 | |
| parent | 712ab7fe97f87b3f01eca6d3f4ca6a10132d8faf (diff) | |
add support for 'on' list
| -rw-r--r-- | booki.c | 59 |
1 files changed, 59 insertions, 0 deletions
@@ -28,6 +28,7 @@ struct Book { struct es isbn; struct es language; struct es translator; + struct es on[5]; int published; // maybe? @@ -44,6 +45,8 @@ struct Book* init_book(struct Book* book) { struct es language = { 7, "English" }; // default language book->language = language; book->translator = default_es; + for (int i = 0; i < 5; i++) + book->on[i] = default_es; book->published = 0; //book_count++; @@ -174,6 +177,36 @@ void parse_book(char* current_pos, struct Book* book) { int id = parse_int(current_pos, &new_pos); book->id = id; current_pos = new_pos; + } else if (ATTR_MATCH(attr, "on")) { + // this is always a list + // look until we hit the opening brace + while ((c = *current_pos) != '[') + current_pos++; + + current_pos++; // go past opening brace + + // loop past opening spaces + while ((c = *current_pos) == ' ') + current_pos++; + + // so long as we keep seeing quotes, keep reading them + int on_count = 0; + struct es value; + while ((c = *current_pos) == '"') { + // read the string + value = parse_string(current_pos, &new_pos); + book->on[on_count] = value; + on_count++; + current_pos = new_pos; + + // go past any commas or opening spaces + while ((c = *current_pos) == ' ') + current_pos++; + if (c != ',') + break; + while ((c = *current_pos) != '"') + current_pos++; + } } // go to (and then past) the newline @@ -333,6 +366,19 @@ void print_book(struct Book book, bool all_fields) { printf(intfmt, "pages", book.pages); if (book.published) printf(intfmt, "published", book.published); + + int on_count = 0; + struct es tmp = book.on[on_count]; + while (tmp.len != 0) { + if (on_count == 0) + printf(" - %s: [ %.*s", "on", tmp.len, tmp.ptr); + else + printf(", %.*s", tmp.len, tmp.ptr); + on_count++; + tmp = book.on[on_count]; + } + if (on_count != 0) + printf(" ]\n"); } } @@ -372,6 +418,19 @@ char** search(int argc, char* argv[]) { } else if (ATTR_MATCH(field, "language")) { if (!regex_match(search_opts.args[i], book->language)) break; + } else if (ATTR_MATCH(field, "on")) { + int cnt = 0; + struct es tmp = book->on[cnt]; + bool match = false; + while (tmp.len != 0) { + if (regex_match(search_opts.args[i], tmp)) + match = true; + cnt++; + tmp = book->on[cnt]; + } + if (!match) + break; + } else { printf("unsupported field: %s\n", field); break; |
