From 6fe01eb21c5e079e5b205d4ff27c3c5f341bcfdc Mon Sep 17 00:00:00 2001 From: "Rob Swindell (on Debian Linux)" <rob@synchro.net> Date: Wed, 15 Feb 2023 23:37:48 -0800 Subject: [PATCH] Add iniGetClampedInt() to return an int key val clamped to a min/max range Very similar to iniGetIntInRange(), but the default value is only returned if the key or value is missing. --- src/xpdev/ini_file.c | 19 +++++++++++++++++++ src/xpdev/ini_file.h | 2 ++ 2 files changed, 21 insertions(+) diff --git a/src/xpdev/ini_file.c b/src/xpdev/ini_file.c index 3c8f1e18d3..bac0e4e8ff 100644 --- a/src/xpdev/ini_file.c +++ b/src/xpdev/ini_file.c @@ -1728,6 +1728,7 @@ int iniGetInteger(str_list_t list, const char* section, const char* key, int def return(parseInteger(vp)); } +/* Returns the default value if key value is out of range */ int iniGetIntInRange(str_list_t list, const char* section, const char* key, int min, int deflt, int max) { char* vp=NULL; @@ -1744,6 +1745,24 @@ int iniGetIntInRange(str_list_t list, const char* section, const char* key, int return result; } +/* Returns the min or max value if key value is out of range */ +int iniGetClampedInt(str_list_t list, const char* section, const char* key, int min, int deflt, int max) +{ + char* vp=NULL; + int result; + + get_value(list, section, key, NULL, &vp, /* literals_supported: */FALSE); + + if(vp==NULL || *vp==0) /* blank value or missing key */ + return deflt; + + result = parseInteger(vp); + if(result < min) + return min; + if(result > max) + return max; + return result; +} uint iniGetUInteger(str_list_t list, const char* section, const char* key, uint deflt) { diff --git a/src/xpdev/ini_file.h b/src/xpdev/ini_file.h index ebf3ba6f59..7377d40f1e 100644 --- a/src/xpdev/ini_file.h +++ b/src/xpdev/ini_file.h @@ -158,6 +158,8 @@ DLLEXPORT int iniGetInteger(str_list_t, const char* section, const char* key ,int deflt); DLLEXPORT int iniGetIntInRange(str_list_t, const char* section, const char* key ,int min, int deflt, int max); +DLLEXPORT int iniGetClampedInt(str_list_t, const char* section, const char* key + ,int min, int deflt, int max); DLLEXPORT uint iniGetUInteger(str_list_t, const char* section, const char* key ,uint deflt); DLLEXPORT short iniGetShortInt(str_list_t, const char* section, const char* key -- GitLab