diff options
| author | Ben Winston | 2024-05-31 22:34:41 -0400 |
|---|---|---|
| committer | Ben Winston | 2024-05-31 22:34:41 -0400 |
| commit | 30175e8f0f732ae081033351730a0363378c1710 (patch) | |
| tree | 77a6d10331b8dbe6587929d3089a47825c2fd324 | |
| parent | ae9025ab31c4588b11c2c8cfc870af0462ce8d66 (diff) | |
initial commit of int comparisons
| -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); |
