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?
Renaming a preferences group
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
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.
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.
And here is my code to examine group names:Mistrel wrote:Here is the code I ended up using to enumerate the group names
Code: Select all
ExaminePreferenceGroups()

PB 4.30
Code: Select all
onErrorGoto(?Fred)