Preference files

Just starting out? Need help? Post your questions and find answers here.
User avatar
Michael Vogel
Addict
Addict
Posts: 2680
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Preference files

Post by Michael Vogel »

Hm, making preference files bullet proof is really not that easy...

...first, it is needed to create an empty file if there's no ini file, because there's no way to force OpenPreferences to do that for you
...CreatePreferences is not an option when having larger preference files or when including additional information (e.g. comments)
...but what, when the file is locked or set to read only?

There is no return value available for WritePreference commands and when trying to check a value by a ReadPreference the cached value will be displayed (see below):

Code: Select all

WonderIni.s="Test.ini"
If CreateFile(#Null,WonderIni)
	CloseFile(#Null)
	SetFileAttributes(WonderIni,#PB_FileSystem_ReadOnly)
EndIf

; Test code
If OpenPreferences(WonderIni)
	Debug ReadPreferenceInteger("test",-1)
	WritePreferenceInteger("test",9999)
	Debug ReadPreferenceInteger("test",-1)
	ClosePreferences()
EndIf
Does anyone know a simple way to see if preferences are handles correctly? Actually I would have to reopen the preferences and reread all entries which is a little bit uncool :)
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1252
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Re: Preference files

Post by Paul »

From the help file...
If the file wasn't found Or can't be opened, it is still possible To use the Read functions And it will Return the specified Default value. It is very useful To initialize in one Step the program variables. The functions like ReadPreferenceString() can be used To Read the preference values stored in the file.

Code: Select all

;read preferences when startin program 
OpenPreferences("Test.ini")
  testvalue=ReadPreferenceInteger("test",-1) 

  ;if "Test.ini" exisists then it will
  ;read the value of "test" else it 
  ;will use default value of -1
ClosePreferences()


; Main Program
; do stuff
Debug testvalue
testvalue=777


;save preferences before existing program
CreatePreferences("Test.ini")
  WritePreferenceInteger("test",testvalue)
ClosePreferences()
Image Image
User avatar
Kurzer
Enthusiast
Enthusiast
Posts: 666
Joined: Sun Jun 11, 2006 12:07 am
Location: Near Hamburg

Re: Preference files

Post by Kurzer »

Michael means the writing commands of the Preference lib does not indicate whether the write process to the file was successful or not.

Unlike the commands of the file lib, they do not return a result. (result = 0 or <> 0). If the preference file is write protected or locked by another process the Write commands of the preference lib could return 0 as result in this case.

So he have to read the preference values right after writing them to disk, to check whether the write process was successful.

@Michael: You could use "CreatePreferences()" before you save your preferences. This command returns a result which indicates success or not. And CreatePreferences() does overwrite an existing prefs file if its not locked or write protected.

The german helpfile wrote:
Erstellt eine neue leere Preference-Datei. Existiert diese Datei bereits, so wird diese gelöscht.
Rückgabewert
Ein Wert ungleich Null, wenn die Datei erfolgreich erstellt wurde, andernfalls Null.
PB 6.02 x64, OS: Win 7 Pro x64 & Win 11 x64, Desktopscaling: 125%, CPU: I7 6500, RAM: 16 GB, GPU: Intel Graphics HD 520, User age in 2024: 56y
"Happiness is a pet." | "Never run a changing system!"
User avatar
Michael Vogel
Addict
Addict
Posts: 2680
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

Re: Preference files

Post by Michael Vogel »

@kurzer: exactly, there''s no return value when writing preferences and no guarantee that the values read from the preferences are stored in the file. But when you say "have to read the preference values right after writing them" you must keep in mind to close and reopen the preference file before doing so.

CreatePreferences, as written in the first post, has some disadvantages, as you need to write all configuration settings even when just one value has to be changed - but even then additional information in the file will be stripped.

Rereading the preferences would be a possibility to double check (!) if everything was written successfully, another test could be to control the file date (last modified), but all these things are only workarounds.
User avatar
Demivec
Addict
Addict
Posts: 4091
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Preference files

Post by Demivec »

Michael Vogel wrote:Rereading the preferences would be a possibility to double check (!) if everything was written successfully, another test could be to control the file date (last modified), but all these things are only workarounds.
Wouldn't you use FlushPreferenceBuffers() to write and get some verification that something was written to disk?
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1252
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Re: Preference files

Post by Paul »

kurzer wrote:Michael means the writing commands of the Preference lib does not indicate whether the write process to the file was successful or not.
Ah.. I understand now. Sorry ;)
Image Image
Post Reply