Steve's Config File System - Printable Version +- QB64 Phoenix Edition (https://staging.qb64phoenix.com) +-- Forum: QB64 Rising (https://staging.qb64phoenix.com/forumdisplay.php?fid=1) +--- Forum: Prolific Programmers (https://staging.qb64phoenix.com/forumdisplay.php?fid=26) +---- Forum: SMcNeill (https://staging.qb64phoenix.com/forumdisplay.php?fid=29) +---- Thread: Steve's Config File System (/showthread.php?tid=69) |
Steve's Config File System - SMcNeill - 04-20-2022 Two little routines which can be used to read and write to a config file. (If you guys want to see an example of what an actual config file would look like using this, look at the config.txt in your QB64/internal folder. This is the exact same config routines which we imported into QB64. :-X) Code: (Select All) ConfigFile$ = "config.txt" The concept behind the routines here is rather simple: First, most config files have headings to help make finding and organizing things easier for the user to find if they ever open the file in a simple text editor. This supports those as a purely organizational type object, but they actually don't hold any value except to make things easier for the user in they alter things manually. For example, a config file might look like the following: '[COLORS] RED = 1 BLUE = 2 '[JOYSTICK] EnableJoystick = FALSE Now that's easy enough to read and alter in a text editor, but what if the user edits it so that it looks something more like the following: '[COLORS] RED = 1 '[JOYSTICK] EnableJoystick = FALSE BLUE = 2 That BLUE isn't in the right heading! So the choice here is: Do we ignore it since it's not in the expected place? Or read it as long as it's anywhere in the file?? I chose to read it no matter where it appears in the config file. The only drawback to this is that we can't have two different settings called BLUE under different headings, but that I don't see that as being an issue for us so much just for the simple fact that QB64 really isn't going to have that many settings to use that we can't give them all an unique name. So honestly, all we're really dealing with here is the item$ which is the setting we're looking for, and the value$ that we want to assign to it, with the heading$ only being there to help with making it all look pretty... Other point to note: ReadConfigSetting is a FUNCTION. It returns a 0 to us if it fails to find a setting, and a -1 if it found it successfully. the value$ is passed back to us via the 2nd parameter in the FUNCTION. work = ReadConfigSetting("TextColor", value$) Now if we have a setting for TextColor, work will be -1 and value$ will be whatever that setting is If there's no TextColor in the config file, work will be 0 and value$ might be anything.... I'd suggest to ALWAYS check to see if the return value of that function is true or not, before I'd accept the value that it gave us. work = ReadConfigSetting("TextColor", value$) IF work THEN PRINT "Textcolor value found. It's: "; value$ ELSE PRINT "No textcolor found" END IF All in all, I find it easier to use than I do to explain the use. LOL - try out the demo and you'll see how it works for yourself easily enough. WriteConfigSetting (heading$, item$, value$) writes the headings (just for prettification), item, and value to the proper place in your config file. It searches to see if the setting is already in the file, and if not it adds it to the proper area automatically for you. ReadConfigSetting (item$, value$) returns a 0 or -1 through the function to tell us if things worked as we expected, and returns our value to use through the second parameter if it can. It really is basically that simple to use. [/quote] |