Page 1 of 1

Renaming a preferences group

Posted: Fri Apr 25, 2008 11:16 pm
by Mistrel
I'm writing a procedure that removes tools from the PureBasic Tools.prefs. The problem I'm encountering is that after I've removed the tools I have to loop back and enumerate the group names [Tool_0], [Tool_1], etc. and then set the ToolCount value in [ToolsInfo].

This is all find an dandy with the exception that there is no command for renaming preference groups. Is there a simple way to do this?

Posted: Sat Apr 26, 2008 6:35 am
by Mistrel
Here is the code I ended up using to enumerate the group names:

Code: Select all

FileID=ReadFile(#PB_Any,ToolsPath.s)
If FileID
	While Eof(FileID)=0
		Line.s=ReadString(FileID)
		If Left(Line.s,6)="[Tool_"
			Line.s="[Tool_"+Str(ToolCount)+"]"
			ToolCount+1
		EndIf
		NewToolsFile.s+Line.s+#CRLF$
	Wend
	CloseFile(FileID)
Else
	Debug "Error"
EndIf
FileID=CreateFile(#PB_Any,ToolsPath.s)
If FileID
	WriteString(FileID,NewToolsFile.s)
	CloseFile(FileID)
Else
	Debug "Error"
EndIf
If OpenPreferences(ToolsPath.s)
	If PreferenceGroup("ToolsInfo")
		WritePreferenceString("ToolCount",Str(ToolCount))
	EndIf
	ClosePreferences()
Else
	Debug "Error"
EndIf

Posted: Sat Apr 26, 2008 9:54 am
by srod
I've never been in the position of having to remove tools, but when adding them I've used the preferences library to write the ToolsInfo->ToolCount field, but used the regular file commands to add a new tool. I did this for no other reason than to preserve the formatting of the file.

In your case, I wouldn't delete the group to be removed but simply change all of it's keys to match the next tool etc. This way you shift all the tools down. At the end you simply remove the last tool group etc.

Either that or create a new file.

Posted: Sat Apr 26, 2008 1:10 pm
by AND51
Mistrel wrote:Here is the code I ended up using to enumerate the group names
And here is my code to examine group names:

Code: Select all

ExaminePreferenceGroups()
:wink: