[bb] Config file reader/changer by Andres [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:42

Previous topic - Next topic

BlitzBot

Title : Config file reader/changer
Author : Andres
Posted : 1+ years ago

Description : Variables aren't case sensitive.

Entry example:
variable$=value$


Code :
Code (blitzbasic) Select
Const ConfigFile$ = "config.cfg"

Type file
Field txt$
End Type

Function ConfigValue$(variable$)
rf = ReadFile(ConfigFile$)
If rf
While Not Eof(rf)
txt$ = ReadLine$(rf)
If Left$(Lower(txt$), Instr(txt$, "=")) = Lower(variable$ + "=")
Return Right$(txt$, Len(txt$) - Instr(txt$, "="))
EndIf
Wend
CloseFile rf
EndIf
Return False
End Function

Function ChangeConfigValue(variable$, value$)
rf = ReadFile(ConfigFile$)
If rf
For this.file = Each file
Delete this
Next
While Not Eof(rf)
this.file = New file
this xt$ = ReadLine$(rf)
Wend
CloseFile rf
EndIf

wf = WriteFile("config.cfg")
If wf
For this.file = Each file
If Left$(Lower(this xt$), Instr(this xt$, "=")) = Lower(variable$ + "=")
WriteLine wf, Left$(this xt$, Instr(this xt$, "=")) + value$
Else
WriteLine wf, this xt$
EndIf
Next
CloseFile wf
EndIf
End Function


Comments :


andy_mc(Posted 1+ years ago)

 It would be good to have a working example of this working


Mike Barwise(Posted 1+ years ago)

 For the purposes of getting it working:Create a file called config.cfg and place it in the same directory as your code.Inside the .cfg file, as a test, put:"Entry1=Hello!"Then within your code, to get it working, do:DebugLog ConfigValue$( "Entry1" )Your debuglog output should say 'Hello!'