diff options
| -rw-r--r-- | booki.c | 26 |
1 files changed, 20 insertions, 6 deletions
@@ -421,7 +421,7 @@ char* next_book(char* current_pos) { } /*** search ***/ -bool regex_match(const char* pattern, const struct es text) { +bool match_string(const char* pattern, const struct es text) { // empty pattern matches everything if (!*pattern) return true; @@ -457,11 +457,22 @@ bool regex_match(const char* pattern, const struct es text) { if (valid) return valid; else if (text.next != NULL) - return regex_match(pattern, *(text.next)); + return match_string(pattern, *(text.next)); return valid; } +bool match_int(const char* pattern, const int candidate) { + // need to parse the pattern to an int + char* endptr; + long ret = strtol(pattern, &endptr, 10); + if (*endptr != '\0') { + printf("couldn't parse int!\n"); + return false; + } + return ret == candidate; +} + static struct option search_options[] = { {"show", no_argument, 0, 's'}, {"edit", no_argument, 0, 'e'}, @@ -550,16 +561,19 @@ void search(int argc, char* argv[], char* booki_file) { // compare fields if (ATTR_MATCH(field, "title")) { - if (!regex_match(search_opts.args[i], book.title)) + if (!match_string(search_opts.args[i], book.title)) break; } else if (ATTR_MATCH(field, "author")) { - if (!regex_match(search_opts.args[i], book.author)) + if (!match_string(search_opts.args[i], book.author)) break; } else if (ATTR_MATCH(field, "language")) { - if (!regex_match(search_opts.args[i], book.language)) + if (!match_string(search_opts.args[i], book.language)) break; } else if (ATTR_MATCH(field, "on")) { - if (!regex_match(search_opts.args[i], book.on)) + if (!match_string(search_opts.args[i], book.on)) + break; + } else if (ATTR_MATCH(field, "pages")) { + if (!match_int(search_opts.args[i], book.pages)) break; } else { printf("unsupported field: %s\n", field); |
