From 391b6178b85dc7fbefb6c6394acbf65da16fdbb0 Mon Sep 17 00:00:00 2001 From: Cory Smith Date: Fri, 7 Oct 2022 10:17:15 -0500 Subject: [PATCH] Moved config related functions to the new config.bas file. --- source/qb64.bas | 15 +++--- source/utilities/config.bas | 84 +++++++++++++++++++++++++++++ source/utilities/strings.bas | 100 +++++------------------------------ 3 files changed, 102 insertions(+), 97 deletions(-) create mode 100644 source/utilities/config.bas diff --git a/source/qb64.bas b/source/qb64.bas index 9781be82c..0b5c654ef 100644 --- a/source/qb64.bas +++ b/source/qb64.bas @@ -25752,15 +25752,6 @@ SUB Give_Error (a$) Error_Message = a$ END SUB -SUB WriteConfigSetting (section$, item$, value$) - WriteSetting ConfigFile$, section$, item$, value$ -END SUB - -FUNCTION ReadConfigSetting (section$, item$, value$) - value$ = ReadSetting$(ConfigFile$, section$, item$) - ReadConfigSetting = (LEN(value$) > 0) -END FUNCTION - FUNCTION VRGBS~& (text$, DefaultColor AS _UNSIGNED LONG) 'Value of RGB String = VRGBS without a ton of typing 'A function to get the RGB value back from a string such as _RGB32(255,255,255) @@ -26309,7 +26300,13 @@ SUB increaseUDTArrays REDIM _PRESERVE udtenext(x + 1000) AS LONG END SUB +' Generic method (can be used outside of QB64) includes: + '$INCLUDE:'utilities\strings.bas' + +' Custom method (designed specifically for QB64) includes: + +'$INCLUDE:'utilities\config.bas' '$INCLUDE:'utilities\file.bas' '$INCLUDE:'subs_functions\extensions\opengl\opengl_methods.bas' '$INCLUDE:'utilities\ini-manager\ini.bm' diff --git a/source/utilities/config.bas b/source/utilities/config.bas new file mode 100644 index 000000000..dca757323 --- /dev/null +++ b/source/utilities/config.bas @@ -0,0 +1,84 @@ +SUB WriteConfigSetting (section$, item$, value$) + WriteSetting ConfigFile$, section$, item$, value$ +END SUB + +FUNCTION ReadConfigSetting (section$, item$, value$) + value$ = ReadSetting$(ConfigFile$, section$, item$) + ReadConfigSetting = (LEN(value$) > 0) +END FUNCTION + +' Convert a boolean value to 'True' or 'False'. +FUNCTION BoolToTFString$ (b AS LONG) + IF b THEN BoolToTFString$ = "True" ELSE BoolToTFString$ = "False" +END FUNCTION + +' Convert 'True' or 'False' to a boolean value. +' Any string not 'True' or 'False' is returned as -2. +FUNCTION TFStringToBool% (s AS STRING) + SELECT CASE _TRIM$(UCASE$(s)) + CASE "TRUE": TFStringToBool% = -1 + CASE "FALSE": TFStringToBool% = 0 + CASE ELSE: TFStringToBool% = -2 + END SELECT +END FUNCTION + +' Reads the bool setting at section:setting. +' If it is not there or invalid, writes the default value to it. +FUNCTION ReadWriteBooleanSettingValue% (section AS STRING, setting AS STRING, default AS INTEGER) + + DIM checkResult AS INTEGER + DIM value AS STRING + DIM result AS INTEGER + + result = ReadConfigSetting(section, setting, value) + + checkResult = TFStringToBool%(value) + + IF checkResult = -2 THEN + WriteConfigSetting section, setting, BoolToTFString$(default) + ReadWriteBooleanSettingValue% = default + ELSE + ReadWriteBooleanSettingValue% = checkResult + END IF + +END FUNCTION + +' Reads the string setting at section:setting. +' If it is not there or invalid, writes the default value to it. +FUNCTION ReadWriteStringSettingValue$ (section AS STRING, setting AS STRING, default AS STRING) + + DIM value AS STRING + DIM result AS INTEGER + + result = ReadConfigSetting(section, setting, value) + + IF result = 0 THEN + WriteConfigSetting section, setting, default + ReadWriteStringSettingValue$ = default + ELSE + ReadWriteStringSettingValue$ = value + END IF + +END FUNCTION + +' Reads the integer setting at section:setting. +' If it is not there or invalid, writes the default value to it. +' Verifies the value is positive and non-zero. +FUNCTION ReadWriteLongSettingValue& (section AS STRING, setting AS STRING, default AS LONG) + + DIM value AS STRING + DIM result AS INTEGER + DIM checkResult AS LONG + + result = ReadConfigSetting(section, setting, value) + + checkResult = VAL(value) + + IF result = 0 OR checkResult <= 0 THEN + WriteConfigSetting section, setting, str2$(default) + ReadWriteLongSettingValue& = default + ELSE + ReadWriteLongSettingValue& = checkResult + END IF + +END FUNCTION \ No newline at end of file diff --git a/source/utilities/strings.bas b/source/utilities/strings.bas index e68ab0071..09e27e1eb 100644 --- a/source/utilities/strings.bas +++ b/source/utilities/strings.bas @@ -1,5 +1,11 @@ -''' String manipulation functions -FUNCTION StrRemove$ (myString$, whatToRemove$) ' noncase sensitive +' String Extensions + +' Please reserve this module for only string methods that can be +' utilized outside of QB64. Meaning, anyone could add this to their +' personal project and leverage the same functionality. + +' Removes a string pattern from an existing string (case-insensitive). +FUNCTION StrRemove$ (myString$, whatToRemove$) DIM a$, b$ DIM AS LONG i a$ = myString$ @@ -12,7 +18,8 @@ FUNCTION StrRemove$ (myString$, whatToRemove$) ' noncase sensitive StrRemove$ = a$ END FUNCTION -FUNCTION StrReplace$ (myString$, find$, replaceWith$) ' noncase sensitive +' Replaces a string pattern within an existing string (case-insensitive). +FUNCTION StrReplace$ (myString$, find$, replaceWith$) DIM a$, b$ DIM AS LONG basei, i IF LEN(myString$) = 0 THEN EXIT FUNCTION @@ -28,90 +35,7 @@ FUNCTION StrReplace$ (myString$, find$, replaceWith$) ' noncase sensitive StrReplace$ = a$ END FUNCTION +' Adds quotation (ASCII 034) marks around a string. FUNCTION AddQuotes$ (s$) AddQuotes$ = CHR$(34) + s$ + CHR$(34) -END FUNCTION - -' Convert a boolean value to 'True' or 'False'. -FUNCTION BoolToTFString$ (b AS LONG) - IF b THEN - BoolToTFString$ = "True" - ELSE - BoolToTFString$ = "False" - END IF -END FUNCTION - -' Convert 'True' or 'False' to a boolean value. -' Any string not 'True' or 'False' is returned as -2. -FUNCTION TFStringToBool% (s AS STRING) - DIM s2 AS STRING - s2 = _TRIM$(UCASE$(s)) - IF s2 = "TRUE" THEN - TFStringToBool% = -1 - ELSEIF s2 = "FALSE" THEN - TFStringToBool% = 0 - ELSE - TFStringToBool% = -2 - END IF -END FUNCTION - -''' Reads the bool setting at section:setting. -''' If it is not there or invalid, writes the default value to it. -FUNCTION ReadWriteBooleanSettingValue% (section AS STRING, setting AS STRING, default AS INTEGER) - - DIM checkResult AS INTEGER - DIM value AS STRING - DIM result AS INTEGER - - result = ReadConfigSetting(section, setting, value) - - checkResult = TFStringToBool%(value) - - IF checkResult = -2 THEN - WriteConfigSetting section, setting, BoolToTFString$(default) - ReadWriteBooleanSettingValue% = default - ELSE - ReadWriteBooleanSettingValue% = checkResult - END IF - -END FUNCTION - -''' Reads the string setting at section:setting. -''' If it is not there or invalid, writes the default value to it. -FUNCTION ReadWriteStringSettingValue$ (section AS STRING, setting AS STRING, default AS STRING) - - DIM value AS STRING - DIM result AS INTEGER - - result = ReadConfigSetting(section, setting, value) - - IF result = 0 THEN - WriteConfigSetting section, setting, default - ReadWriteStringSettingValue$ = default - ELSE - ReadWriteStringSettingValue$ = value - END IF - -END FUNCTION - -''' Reads the integer setting at section:setting. -''' If it is not there or invalid, writes the default value to it. -''' Verifies the value is positive and non-zero. -FUNCTION ReadWriteLongSettingValue& (section AS STRING, setting AS STRING, default AS LONG) - - DIM value AS STRING - DIM result AS INTEGER - DIM checkResult AS LONG - - result = ReadConfigSetting(section, setting, value) - - checkResult = VAL(value) - - IF result = 0 OR checkResult <= 0 THEN - WriteConfigSetting section, setting, str2$(default) - ReadWriteLongSettingValue& = default - ELSE - ReadWriteLongSettingValue& = checkResult - END IF - -END FUNCTION +END FUNCTION \ No newline at end of file