From afcd16dcc9397a0ba9fdc545c21a465f98dcdd8a Mon Sep 17 00:00:00 2001 From: sasha0552 Date: Sun, 18 Aug 2024 22:36:47 +0000 Subject: [PATCH] Refactor code --- src/main.c | 23 +++++-------------- src/utils.h | 64 ++++++++++++++++++++++++++--------------------------- 2 files changed, 38 insertions(+), 49 deletions(-) diff --git a/src/main.c b/src/main.c index fe59342..9f77e2c 100644 --- a/src/main.c +++ b/src/main.c @@ -117,6 +117,7 @@ int main(int argc, char *argv[]) { /***** OPTION PARSING *****/ { + // Iterate through command-line arguments for (unsigned int i = 1; i < argc; i++) { // 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) { @@ -124,9 +125,6 @@ int main(int argc, char *argv[]) { if (!parse_uint_option(argv[++i], &iterations_before_switch)) { goto usage; } - - // Continue to the next argument - continue; } // Check if the option is "-psh" or "--performance-state-high" and if there is a next argument @@ -135,9 +133,6 @@ int main(int argc, char *argv[]) { if (!parse_uint_option(argv[++i], &performance_state_high)) { goto usage; } - - // Continue to the next argument - continue; } // Check if the option is "-psl" or "--performance-state-low" and if there is a next argument @@ -146,9 +141,6 @@ int main(int argc, char *argv[]) { if (!parse_uint_option(argv[++i], &performance_state_low)) { goto usage; } - - // Continue to the next argument - continue; } // Check if the option is "-si" or "--sleep-interval" and if there is a next argument @@ -157,9 +149,6 @@ int main(int argc, char *argv[]) { if (!parse_uint_option(argv[++i], &sleep_interval)) { goto usage; } - - // Continue to the next argument - continue; } // Check if the option is "-tt" or "--temperature-threshold" and if there is a next argument @@ -168,15 +157,15 @@ int main(int argc, char *argv[]) { if (!parse_uint_option(argv[++i], &temperature_threshold)) { goto usage; } - - // Continue to the next argument - continue; } + } - // If an invalid option is encountered, print the usage instructions + // Display usage instructions to the user + if (false) { + // Display usage instructions to the user usage: - // If an invalid option is provided, print the usage instructions + // Print the usage instructions printf("Usage: %s [options]\n", argv[0]); printf("\n"); printf("Options:\n"); diff --git a/src/utils.h b/src/utils.h index c251998..fa1e18c 100644 --- a/src/utils.h +++ b/src/utils.h @@ -27,41 +27,41 @@ #define HAS_NEXT_ARG (i + 1 < argc) // Macro to simplify NVAPI function calls and handle errors -#define NVAPI_CALL(call, label) do { \ - /* Evaluate the NVAPI function call and store the result */ \ - NvAPI_Status result = (call); \ - \ - /* Check if the result indicates an error */ \ - if (result != NVAPI_OK) { \ - /* Prepare a buffer to hold the error message */ \ - NvAPI_ShortString error; \ - \ - /* Retrieve the error message associated with the result code */ \ - if (NvAPI_GetErrorMessage(result, error) != NVAPI_OK) { \ - strcpy(error, ""); \ - } \ - \ - /* Print the error message to standard error */ \ - fprintf(stderr, "%s: %s\n", #call, error); \ - \ - /* Jump to the specified label */ \ - goto label; \ - } \ +#define NVAPI_CALL(call, label) do { \ + /* Evaluate the NVAPI function call and store the result */ \ + NvAPI_Status result = (call); \ + \ + /* Check if the result indicates an error */ \ + if (result != NVAPI_OK) { \ + /* Prepare a buffer to hold the error message */ \ + NvAPI_ShortString error; \ + \ + /* Retrieve the error message associated with the result code */ \ + if (NvAPI_GetErrorMessage(result, error) != NVAPI_OK) { \ + strcpy(error, ""); \ + } \ + \ + /* Print the error message to standard error */ \ + fprintf(stderr, "%s: %s\n", #call, error); \ + \ + /* Jump to the specified label */ \ + goto label; \ + } \ } while(0) // Macro to simplify NVML function calls and handle errors -#define NVML_CALL(call, label) do { \ - /* Evaluate the NVML function call and store the result */ \ - nvmlReturn_t result = (call); \ - \ - /* Check if the result indicates an error */ \ - if (result != NVML_SUCCESS) { \ - /* Print the error message to standard error */ \ - fprintf(stderr, "%s: %s\n", #call, nvmlErrorString(result)); \ - \ - /* Jump to the specified label */ \ - goto label; \ - } \ +#define NVML_CALL(call, label) do { \ + /* Evaluate the NVML function call and store the result */ \ + nvmlReturn_t result = (call); \ + \ + /* Check if the result indicates an error */ \ + if (result != NVML_SUCCESS) { \ + /* Print the error message to standard error */ \ + fprintf(stderr, "%s: %s\n", #call, nvmlErrorString(result)); \ + \ + /* Jump to the specified label */ \ + goto label; \ + } \ } while(0) /***** ***** ***** ***** ***** STRUCTURES ***** ***** ***** ***** *****/