Preferences

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
nospam
Enthusiast
Enthusiast
Posts: 130
Joined: Mon Nov 12, 2012 9:15 am

Preferences

Post by nospam »

Make the default value optional and return an error on a failed ReadPreference if no default is specified.

The preferences feature is very useful but being forced to provide a default creates extra work that is not necessary in many situations.

The help text states:
If the 'Keyword$' isn't found or the preference file haven't been opened correctly (file missing for example) the specified default value is returned.
Good programming practice requires that if the preferences file is missing then you create it and save the defaults accordingly. While I load all my preferences in one go, the requirement to specify a default would be especially onerous if ad hoc ReadPreference calls were being made throughout one's code; it would surely require the programmer to keep going back to find the correct default value.
MachineCode
Addict
Addict
Posts: 1482
Joined: Tue Feb 22, 2011 1:16 pm

Re: Preferences

Post by MachineCode »

nospam wrote:Good programming practice requires that if the preferences file is missing then you create it and save the defaults accordingly.
Um, yes... so if OpenPreferences() fails due to no prefs file existing, it returns 0 and you can just go ahead and create it (as you said). What's the problem, then? It's not like you need to start using default values at all, if it returns 0 when opening.
Microsoft Visual Basic only lasted 7 short years: 1991 to 1998.
PureBasic: Born in 1998 and still going strong to this very day!
User avatar
bobobo
Enthusiast
Enthusiast
Posts: 206
Joined: Mon Jun 09, 2003 8:30 am

Re: Preferences

Post by bobobo »

no prob if a pref exits or not if it IS a pref-file.
of course a pref-file with noncompliant content will spoil this. (this may be academic)
you could check in advance, wether the pref-file is "your" pref-file (no file or filled with "my" pref-stuff)

Code: Select all

If OpenPreferences(GetTemporaryDirectory()+"mypref.ini") =0
  ;write def values
  PreferenceGroup("def")
  WritePreferenceString("val1","whatever")
  WritePreferenceString("val2","whatever")
  PreferenceGroup("g2")
  WritePreferenceString("val3","whatever")
Else
  ;readDefvalues
  ;check if it's "my" pref-file (finding specific group )
  If  ExaminePreferenceGroups()
    While NextPreferenceGroup()
      If PreferenceGroupName()="g2"
        found=1
      EndIf
    Wend
  EndIf
  If found
    PreferenceGroup("def")
    Debug "read "+ ReadPreferenceString("val1","")
    Debug "read "+ ReadPreferenceString("val3","")
    PreferenceGroup("g2")
    Debug "read "+ ReadPreferenceString("val3","")
  EndIf
EndIf
ClosePreferences()
RunProgram(GetTemporaryDirectory()+"mypref.ini")
사십 둘 .
nospam
Enthusiast
Enthusiast
Posts: 130
Joined: Mon Nov 12, 2012 9:15 am

Re: Preferences

Post by nospam »

MachineCode wrote:
nospam wrote:Good programming practice requires that if the preferences file is missing then you create it and save the defaults accordingly.
Um, yes... so if OpenPreferences() fails due to no prefs file existing, it returns 0 and you can just go ahead and create it (as you said). What's the problem, then? It's not like you need to start using default values at all, if it returns 0 when opening.
I already said what the problem is. A default value is required, but the default value is not needed.

Code: Select all

      prefSystemNextID = ReadPreferenceLong(Str(#prefSystemNextID) ,0)
      prefSystemWindowLocationX = ReadPreferenceLong(Str(#prefSystemWindowLocationX) ,0)
      prefSystemWindowLocationY = ReadPreferenceLong(Str(#prefSystemWindowLocationY) ,0)
      prefOptionsProxyListeningIP = ReadPreferenceString(Str(#prefOptionsProxyListeningIP) ,"")
      prefSystemLogColumnDisplayIndex = ReadPreferenceString(Str(#prefSystemLogColumnDisplayIndex) ,"")
      prefSystemLogColumnNames = ReadPreferenceString(Str(#prefSystemLogColumnNames) ,"")
      prefSystemLogColumnVisible = ReadPreferenceString(Str(#prefSystemLogColumnVisible) ,"")
The code is inconsistent with other commands that use optional values, such as StringGadget's flags. The key word being "inconsistent".
nospam
Enthusiast
Enthusiast
Posts: 130
Joined: Mon Nov 12, 2012 9:15 am

Re: Preferences

Post by nospam »

bobobo wrote:no prob if a pref exits or not if it IS a pref-file.
of course a pref-file with noncompliant content will spoil this. (this may be academic)
you could check in advance, wether the pref-file is "your" pref-file (no file or filled with "my" pref-stuff)
I don't know what you're talking about. Being forced to always specify a default value has nothing to do, per se, with whether or not a prefs file exists. It has nothing to do with non-compliant prefs files. It has nothing to do with checking in advance, and it has nothing to do with whether it's my pref file or someone else's.
User_Russian
Addict
Addict
Posts: 1518
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: Preferences

Post by User_Russian »

If you can, please add the functions

Code: Select all

CatchPreference(*Point, Length)
SavePreferenceMem(*Point, Length)
Sometimes you have to work with the data in memory (for example, program settings of its resources).
Post Reply