I wanted to have a preferences file using the PB preferences library but
- I want to be using them and updating them in memory during runtime and not going to disk except to load and save all
- I don't want to use global vars or arrays to hold them
- I want a dynamic way to add new vars and not need to add more vars etc and still support groups
- I want something I can reuse in an include file
I guess I could have used a static array but I didn't want to enum an array looking for matches.
I've only just started using this in a real app so it might be buggy still. I'll update if I spot any.
I'm not sure if having a static prefs file name is a good idea or not. perhaps this should be passed with load and save instructions but it still leaves static filenames in the main code calls.
comments, criticisms and advice welcome
Cheers
(Edits)
- (Code updated) I think this needs a passable "default" val. Perhaps if you pass the val with get if the reply is "" then it sends the val you passed back to you
Code: Select all
#Pref_GetVar = 1
#Pref_SetVar = 2
#Pref_Admin_SavePrefs = 3
#Pref_Admin_LoadPrefs = 4
Declare .s AppPreferences(ActionType.l, PrefGroup.s = "", PrefName.s = "", PrefVal.s = "")
;===============================================================================================
; Testing code
;===============================================================================================
AppPreferences(#Pref_Admin_LoadPrefs) ;creates empty prefs if not loaded
Debug "Write some vars in groups"
AppPreferences(#Pref_SetVar, "", "MyVar", "10") ;no group
AppPreferences(#Pref_SetVar, "MyGroup", "MyVar", "10")
AppPreferences(#Pref_SetVar, "MyGroup", "MyVar2", "10.00")
AppPreferences(#Pref_SetVar, "MyOtherGroup", "MyVar2", "Hello")
AppPreferences(#Pref_SetVar, "MyGroup", "MyVar3", "5")
Debug "Read them back"
Debug AppPreferences(#Pref_GetVar, "", "MyVar")
Debug AppPreferences(#Pref_GetVar, "MyGroup", "MyVar")
Debug AppPreferences(#Pref_GetVar, "MyGroup", "MyVar2")
Debug AppPreferences(#Pref_GetVar, "MyOtherGroup", "MyVar2")
Debug AppPreferences(#Pref_GetVar, "MyGroup", "MyVar3")
Debug AppPreferences(#Pref_GetVar, "MyGroup", "MyVar7", "100") ;look for a var that's missing and update and return result
Debug "flush to disk"
AppPreferences(#Pref_Admin_SavePrefs)
Debug "Update after disk write"
AppPreferences(#Pref_SetVar, "MyGroup", "MyVar4", "50")
Debug "read And check update"
Debug AppPreferences(#Pref_GetVar, "MyGroup", "MyVar4")
Debug "Refresh from disk"
AppPreferences(#Pref_Admin_LoadPrefs)
Debug "Check prefs from disk"
Debug AppPreferences(#Pref_GetVar, "", "MyVar")
Debug AppPreferences(#Pref_GetVar, "MyGroup", "MyVar")
Debug AppPreferences(#Pref_GetVar, "MyGroup", "MyVar2")
Debug AppPreferences(#Pref_GetVar, "MyOtherGroup", "MyVar2")
Debug AppPreferences(#Pref_GetVar, "MyGroup", "MyVar3")
Debug AppPreferences(#Pref_GetVar, "MyGroup", "MyVar7") ;note it's updated
Debug "Check removal of pref not written to disk "
Debug AppPreferences(#Pref_GetVar, "MyGroup", "MyVar4")
Debug "nothing returned"
;===============================================================================================
Procedure.s AppPreferences(ActionType.l, PrefGroup.s = "", PrefName.s = "", PrefVal.s = "")
;Map Format = "group.name"="val"
;if group or "." omitted then add to "Default" group, don't return "Default" name for that group, just name=val
Static NewMap AppPrefs.s()
Define Prefs_Filename.s = "AppPrefs.conf"
Define CurMapKey.s, CurMapGroup.s, CurMapName.s, CurMapVal.s
Define ReturnVal.s
Select ActionType
Case #Pref_GetVar ; (Gets a pref from the Map - in memory only, Not read from disk prefs file)
If PrefName = ""
ProcedureReturn ""
Else
If Trim(PrefGroup) = "" ; no group
ReturnVal = AppPrefs("Default." + PrefName)
Else
ReturnVal = AppPrefs(PrefGroup + "." + PrefName)
EndIf
EndIf
If Trim(ReturnVal) = "" ;send details back and update
AppPreferences(#Pref_SetVar, PrefGroup, PrefName, PrefVal)
ProcedureReturn PrefVal
Else
ProcedureReturn ReturnVal
EndIf
Case #Pref_SetVar ; (Sets a pref in the map - in memory only, not writen to disk prefs file)
If PrefName = "" Or PrefVal = ""
ProcedureReturn ""
Else
If Trim(PrefGroup) = "" ; no group
AppPrefs("Default." + PrefName) = PrefVal
Else
AppPrefs(PrefGroup + "." + PrefName) = PrefVal
EndIf
ProcedureReturn ""
EndIf
Case #Pref_Admin_SavePrefs ; (Saves prefs to file)
If OpenPreferences(Prefs_Filename,#PB_Preference_GroupSeparator)
Else
CreatePreferences(Prefs_Filename,#PB_Preference_GroupSeparator)
EndIf
ResetMap(AppPrefs())
While NextMapElement(AppPrefs())
CurMapKey = MapKey(AppPrefs())
If FindString(CurMapKey,".") ;contains group
CurMapGroup = StringField(CurMapKey,1,".")
CurMapName = StringField(CurMapKey,2,".")
CurMapVal = AppPrefs(CurMapKey)
Else ;add to "default" group
CurMapGroup = "Default"
CurMapName = StringField(CurMapKey,2,".")
CurMapVal = AppPrefs(CurMapKey)
EndIf
PreferenceGroup(CurMapGroup)
WritePreferenceString(CurMapName, CurMapVal)
Wend
ClosePreferences()
Case #Pref_Admin_LoadPrefs ; (Loads prefs to file)
;Clear MAP for loading
ClearMap(AppPrefs())
If OpenPreferences(Prefs_Filename,#PB_Preference_GroupSeparator)
ResetMap(AppPrefs())
ExaminePreferenceGroups()
While NextPreferenceGroup()
CurMapGroup = PreferenceGroupName()
ExaminePreferenceKeys()
While NextPreferenceKey()
CurMapName = PreferenceKeyName()
CurMapVal = PreferenceKeyValue()
AppPrefs(CurMapGroup + "." + CurMapName) = CurMapVal
Wend
Wend
ClosePreferences()
Else
;No prefs file so create
CreatePreferences(Prefs_Filename,#PB_Preference_GroupSeparator)
ClosePreferences()
ProcedureReturn ""
EndIf
Default
ProcedureReturn ""
EndSelect
EndProcedure