Renaming a preferences group

Just starting out? Need help? Post your questions and find answers here.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Renaming a preferences group

Post 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?
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post 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
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post 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.
I may look like a mule, but I'm not a complete ass.
AND51
Addict
Addict
Posts: 1040
Joined: Sun Oct 15, 2006 8:56 pm
Location: Germany
Contact:

Post 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:
PB 4.30

Code: Select all

onErrorGoto(?Fred)
Post Reply