From 5b43f3caaeb35504a8f65eb4f20b78c9118d9e53 Mon Sep 17 00:00:00 2001 From: Ben Winston Date: Fri, 31 May 2024 23:04:13 -0400 Subject: add leading +/, for greater or less than --- booki.c | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/booki.c b/booki.c index 32ad609..c25ec91 100644 --- a/booki.c +++ b/booki.c @@ -462,15 +462,38 @@ bool match_string(const char* pattern, const struct es text) { return valid; } -bool match_int(const char* pattern, const int candidate) { - // need to parse the pattern to an int +bool match_int(char* pattern, int candidate) { + // we don't want to match the zero value, unless the pattern is also 0 + if (candidate == 0 && strcmp(pattern, "0") != 0) + return false; + + // check for leading signs + char* current_pos = pattern; + bool lt = false; + bool gt = false; + if (*current_pos == '+') { + gt = true; + current_pos++; + } else if (*current_pos == ',') { + lt = true; + current_pos++; + } + + // parse the string to an int char* endptr; - long ret = strtol(pattern, &endptr, 10); + long ret = strtol(current_pos, &endptr, 10); if (*endptr != '\0') { - printf("couldn't parse int!\n"); + printf("couldn't parse pattern as int: '%s'\n", pattern); return false; } - return ret == candidate; + + // do compares + if (lt) + return candidate < ret; + else if (gt) + return candidate > ret; + else + return ret == candidate; } static struct option search_options[] = { -- cgit v1.2.3