Working with INI Files This technote describes how to use Superbase to manipulate INI files. It makes use of user defined functions to achieve these manipulations. This technote presents two different approaches to INI file manipulation. The first approach uses the ReadINI$ function (provided with Superbase in SAMPLES/PROCS/SBLEXTEN.SBP), the second approach uses ExtractINI$ (provided in this technote). The ReadINI$ approach is designed to test for a parameter to have a certain value in an INI file. For example you could use this function if you wanted to test a directory setting before trying to opening a file. The ReadINI$ function uses four parameters: filename to be created or appended, section of the INI file to be read, INI parameter, and the value to return if not found. SUB main() REM Specify the ini file name with directory, the section name, the parameter to be searched for, and REM the value to be tested INIFile$ = "c:\win\superbas.ini" INISection$ = "Superbase" INIParam$ = "Iconbar" INIDefault$ = "1" REQUEST "Enter the ini filename (include directory and .ini extension)","",4,butt%,INIFile$ REQUEST "Enter the section to search for (no brackets required)","",4,butt%,INISection$ INISection$ = "[" + INISection$ + "]" REQUEST "Enter the parameter to search for","",4,butt%,INIParam$ REQUEST "Enter the value to test for","",4,butt%,INIDefault$ IF ReadINI$(INIFile$,INISection$,INIParam$,INIDefault$) = INIDefault$ THEN REQUEST "Yep, that's the value" ELSE REQUEST "Nice try, guess again" END IF END SUB '************************************************************************* ***** ' * ReadINI$() - This function returns a value from a given section in an ' * .INI file. Its action is similar to the Windows ' * function GetProfileString ' * INIFile$ - Name and path of The INI file to be read, e.g. C:\WIN31\WIN.INI ' * INISection$ - Section in INI file to be read, e.g. [Superbase] ' * INIParam$ - Parameter to be read, e.g. Panel ' * INIDefault$ - Default value to return if not found ' * ' * Example : ' * ' * IF ReadINI$("C:\WIN31\APPS\APP1.INI","[Window]","Warn","Yes") = "Yes" THEN BELL ' * Result : If the warn value was set to yes then sound the warning ' ************************************************************************* FUNCTION ReadINI$(INIFile$,INISection$,INIParam$,INIDefault$) REM *********************************************************************** REM * Declare all local variables as local REM *********************************************************************** DIM CurrentSection$ REM *********************************************************************** REM * Make sure that the file exists REM *********************************************************************** IF FN ext(INIFile$) = "" THEN INIFile$ = INIFile$ + ".INI" IF NOT EXISTS (INIFile$) THEN ReadINI$ = INIDefault$ ELSE REM *********************************************************************** REM * Read In An Existing .INI File REM *********************************************************************** ReadINI$ = INIDefault$ OPEN INIFile$ FOR INPUT WHILE NOT EOF ("*") INPUT LINE INILine$ CurrentSection$ = IF ( LEFT$ (INILine$,1) = "[",INILine$,CurrentSection$) IF CurrentSection$ = INISection$ AND INILine$ LIKE INIParam$ + "=*" THEN ReadINI$ = MID$ (INILine$, INSTR (INILine$,"=") + 1) END WHILE END IF WEND CLOSE INPUT END IF END FUNCTION The ExtractINI$ approach provides slightly different functionality than ReadINI$ because it allows you to return into Superbase the actual setting from the INI file you have interrogated. This allows you to notify your user of the current setting before changing it. The ExtractINI$ function uses 3 parameters, directory, filename, and setting. It returns the value of the setting requested. Here is a sample of how to use this function in an SBL program: SUB main() REM Specify the directory, ini file name, and the string to be searched for INIDir$ = "c:\windows" REQUEST "Enter the directory for your ini file","",4,butt%,INIDir$ INIFile$ = "Superbas.ini" REQUEST "Enter the ini filename (include .ini extension)","",4,butt%,INIFile$ REQUEST "Enter the string to search for","",4,butt%,INISetting$ REQUEST "Your current " + INISetting$ + " setting value is " + ExtractI NI$(INIDir$,INIFile$,INISetting$) END SUB Here is the code for the function: ' ************************************************************************* ' * ExtractINI$ - Retrieve a setting from an INI file ' * INIDir$ - Directory of INI file ' * INIFile$ - Name of the INI file ' * INISetting$ - Setting to look for in the INI file ' * ' * ' * Example: REQUEST ExtractINI$("c:\win","superbas.ini",FiltersDir") ' * Result : The request will contain the setting value you have ' * asked for i.e. C:\SPC\FILTER\ ' ************************************************************************* FUNCTION ExtractINI$(INIDir$,INIFile$,INISetting$) flag$ = "false" DIRECTORY INIDir$ OPEN INIFile$ FOR INPUT WHILE NOT EOF ("*") AND flag$ = "false" INPUT LINE INILine$ IF INSTR ( UCASE$ (INILine$), UCASE$ (INISetting$)) <> 0 THEN start% = INSTR (INILine$,"=") hold$ = MID$ (INILine$,start% + 1) ExtractINI$ = TRIM$ (hold$) flag$ = "True" END IF INILine%% = INILine%% + 1 WEND IF flag$ <> "True" THEN ExtractINI$ = "Not Found" END IF END FUNCTION After you have used one of these functions to interrogate a value from an INI file, you can use the AddINI$() function to write back to the INI file and also to create a brand new ini if needed, or if you are manipulating your Superbas.ini file, you could use an SBL command such as SET CURRENCYSYMBOL "$", BEFORE:SAVE SET (or any of the other SET commands to modify the INI settings. Program: Superbase Versions: 2.0 Date: March 17, 1994 D Date: