From 10364e83e4bf44ca129c5cc9ea1ee10c18de77cd Mon Sep 17 00:00:00 2001 From: sasha0552 Date: Sun, 18 Aug 2024 22:49:35 +0000 Subject: [PATCH] Add macros to check true/false --- src/main.c | 22 ++++++---------------- src/utils.h | 24 ++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/src/main.c b/src/main.c index 97394ba..ee03dff 100644 --- a/src/main.c +++ b/src/main.c @@ -122,41 +122,31 @@ int main(int argc, char *argv[]) { // Check if the option is "-ibs" or "--iterations-before-switch" and if there is a next argument if ((IS_OPTION("-ibs") || IS_OPTION("--iterations-before-switch")) && HAS_NEXT_ARG) { // Parse the integer option and store it in iterations_before_switch - if (!parse_uint(argv[++i], &iterations_before_switch)) { - goto usage; - } + IS_TRUE(parse_uint(argv[++i], &iterations_before_switch), usage); } // Check if the option is "-psh" or "--performance-state-high" and if there is a next argument if ((IS_OPTION("-psh") || IS_OPTION("--performance-state-high")) && HAS_NEXT_ARG) { // Parse the integer option and store it in performance_state_high - if (!parse_uint(argv[++i], &performance_state_high)) { - goto usage; - } + IS_TRUE(parse_uint(argv[++i], &performance_state_high), usage); } // Check if the option is "-psl" or "--performance-state-low" and if there is a next argument if ((IS_OPTION("-psl") || IS_OPTION("--performance-state-low")) && HAS_NEXT_ARG) { // Parse the integer option and store it in performance_state_low - if (!parse_uint(argv[++i], &performance_state_low)) { - goto usage; - } + IS_TRUE(parse_uint(argv[++i], &performance_state_low), usage); } // Check if the option is "-si" or "--sleep-interval" and if there is a next argument if ((IS_OPTION("-si") || IS_OPTION("--sleep-interval")) && HAS_NEXT_ARG) { // Parse the integer option and store it in sleep_interval - if (!parse_uint(argv[++i], &sleep_interval)) { - goto usage; - } + IS_TRUE(parse_uint(argv[++i], &sleep_interval), usage); } // Check if the option is "-tt" or "--temperature-threshold" and if there is a next argument if ((IS_OPTION("-tt") || IS_OPTION("--temperature-threshold")) && HAS_NEXT_ARG) { - // Parse the integer option and store it in sleep_interval - if (!parse_uint(argv[++i], &temperature_threshold)) { - goto usage; - } + // Parse the integer option and store it in temperature_threshold + IS_TRUE(parse_uint(argv[++i], &temperature_threshold), usage); } } diff --git a/src/utils.h b/src/utils.h index fa1e18c..c1864ad 100644 --- a/src/utils.h +++ b/src/utils.h @@ -26,6 +26,30 @@ // Macro to check if there is a next argument #define HAS_NEXT_ARG (i + 1 < argc) +// Macro to check if a condition is true and jump to a label if it is not +#define IS_TRUE(call, label) do { \ + /* Evaluate the condition */ \ + int result = (call); \ + \ + /* Check if the condition is false */ \ + if (!result) { \ + /* Jump to the specified label */ \ + goto label; \ + } \ +} while(0); + +// Macro to check if a condition is false and jump to a label if it is not +#define IS_FALSE(call, label) do { \ + /* Evaluate the condition */ \ + int result = (call); \ + \ + /* Check if the condition is true */ \ + if (result) { \ + /* Jump to the specified label */ \ + goto label; \ + } \ +} while(0); + // Macro to simplify NVAPI function calls and handle errors #define NVAPI_CALL(call, label) do { \ /* Evaluate the NVAPI function call and store the result */ \