Preference
Reading and writing preference files: a program's settings, kept between runs, in the form of Windows .ini files.
; a comment
Key = Value
[Group]
Width = 800
- One preference file is open at a time.
OpenPreferencesorCreatePreferencesopens it, andClosePreferenceswrites it back if it was changed (an unchanged file isn't touched). - Keys are in groups (
[Name]). Keys before the first group are outside any group:PreferenceGroup(""). Group and key names match without case; a group's name is as written, spaces included. - A line starting with
;is a comment, kept where it is. Every other line is a key: a line without=is a key with an empty value. Blank lines are dropped. - Values are read without the spaces around them.
- A changed file is written back the way PureBasic writes it: UTF-8, with a byte order mark followed by a line break (unless
#PB_Preference_NoBOM),Key = Valuelines, comments without the spaces in front, Windows line breaks on Windows. x was checked against PureBasic 6.50 for all of this.
OpenPreferences ✅
Result = OpenPreferences(Filename$ [, Flags [, Encoding]])
Opens a preference file. Even if it isn't there (Result is 0), the program can read (getting the defaults) and write: ClosePreferences then creates the file.
| Parameter | Meaning |
|---|---|
Flags |
for writing it back: #PB_Preference_NoSpace (Key=Value, no spaces), #PB_Preference_GroupSeparator (an empty line between groups), #PB_Preference_NoBOM |
Encoding |
for a file without a UTF-8 byte order mark: #PB_Ascii (the default) or #PB_UTF8 |
CreatePreferences ✅
Result = CreatePreferences(Filename$ [, Flags])
Creates an empty preference file (replacing one already there), with the same Flags. Returns 0 if the file can't be created.
ClosePreferences, FlushPreferenceBuffers ✅
ClosePreferences()
Result = FlushPreferenceBuffers()
ClosePreferences writes the file if it was changed, and closes it. FlushPreferenceBuffers writes it now, and returns 0 if that failed.
PreferenceGroup ✅
Result = PreferenceGroup(Name$)
The group the next reads and writes use. A group that isn't there yet appears when the first key is written to it. Returns non-zero if the group is in the file already.
WritePreferenceString and the other writes ✅
WritePreferenceString(Key$, Value$)
WritePreferenceLong(Key$, Value) WritePreferenceInteger(Key$, Value) WritePreferenceQuad(Key$, Value)
WritePreferenceFloat(Key$, Value) WritePreferenceDouble(Key$, Value)
Writes a key in the current group, replacing it if it's there (its name as written this time), or adding it after the group's last line. A float is written with 6 decimals; a double with every digit of its exact value (0.1 is 0.1000000000000000055511151231257827021181583404541015625), so it reads back exactly.
ReadPreferenceString and the other reads ✅
Result$ = ReadPreferenceString(Key$, Default$)
Result = ReadPreferenceLong(Key$, Default) Result = ReadPreferenceInteger(Key$, Default) Result.q = ReadPreferenceQuad(Key$, Default)
Result.f = ReadPreferenceFloat(Key$, Default) Result.d = ReadPreferenceDouble(Key$, Default)
A key's value in the current group, or Default if it isn't there (or the file wasn't opened). The whole-number reads take decimal digits only ($1F is 0); the others read as ValF and ValD do.
OpenConsole()
file$ = GetTemporaryDirectory() + "x_doc_example.prefs"
CreatePreferences(file$)
PreferenceComment("Example settings")
PreferenceGroup("Window")
WritePreferenceInteger("Width", 800)
WritePreferenceInteger("Height", 600)
WritePreferenceDouble("Zoom", 1.25)
PreferenceGroup("User")
WritePreferenceString("Name", "Ann")
ClosePreferences()
OpenPreferences(file$)
PreferenceGroup("window")
PrintN(Str(ReadPreferenceInteger("width", 0)) + "x" + Str(ReadPreferenceInteger("Height", 0)) + " at " + StrD(ReadPreferenceDouble("Zoom", 1)))
PrintN("Depth " + Str(ReadPreferenceInteger("Depth", 32)))
PreferenceGroup("User")
PrintN("Hello " + ReadPreferenceString("Name", "nobody"))
ClosePreferences()
DeleteFile(file$)
Output:
800x600 at 1.25
Depth 32
Hello Ann
PreferenceComment ✅
PreferenceComment(Text$)
Adds the line ; Text$ at the end of the current group (or of the file, for a group not written yet).
RemovePreferenceKey, RemovePreferenceGroup ✅
RemovePreferenceKey(Key$)
RemovePreferenceGroup(Name$)
Removes a key of the current group, or a group with its keys and comments.
ExaminePreferenceGroups, NextPreferenceGroup, PreferenceGroupName ✅
Result = ExaminePreferenceGroups()
Result = NextPreferenceGroup()
Name$ = PreferenceGroupName()
Goes through the groups in file order. NextPreferenceGroup also makes the group current, so its keys can be read or examined. A file with two groups of the same name gives both.
ExaminePreferenceKeys, NextPreferenceKey, PreferenceKeyName, PreferenceKeyValue ✅
Result = ExaminePreferenceKeys()
Result = NextPreferenceKey()
Name$ = PreferenceKeyName()
Value$ = PreferenceKeyValue()
Goes through the keys of the current group. NextPreferenceKey without ExaminePreferenceKeys starts at the group's first key.
OpenConsole()
file$ = GetTemporaryDirectory() + "x_doc_example2.prefs"
CreatePreferences(file$)
WritePreferenceString("Version", "2")
PreferenceGroup("Colours")
WritePreferenceString("Red", "FF0000")
WritePreferenceString("Green", "00FF00")
PreferenceGroup("Sizes")
WritePreferenceInteger("Small", 8)
ClosePreferences()
OpenPreferences(file$)
If ExaminePreferenceGroups()
While NextPreferenceGroup()
Print(PreferenceGroupName() + ":")
If ExaminePreferenceKeys()
While NextPreferenceKey()
Print(" " + PreferenceKeyName() + "=" + PreferenceKeyValue())
Wend
EndIf
PrintN("")
Wend
EndIf
ClosePreferences()
DeleteFile(file$)
Output:
Colours: Red=FF0000 Green=00FF00
Sizes: Small=8
PreferenceKeyExists (x addition) ✅
Result = PreferenceKeyExists(Key$)
Non-zero if the current group has the key: a way to tell a missing key from one that has the default's value.