December 03, 2020, 08:33:39 PM

Author Topic: [bb] Config file reader/changer by Andres [ 1+ years ago ]  (Read 519 times)

Offline BlitzBot

  • Jr. Member
  • **
  • Posts: 1
[bb] Config file reader/changer by Andres [ 1+ years ago ]
« on: June 29, 2017, 12:28:42 AM »
Title : Config file reader/changer
Author : Andres
Posted : 1+ years ago

Description : Variables aren't case sensitive.

Entry example:
variable$=value$


Code :
Code: BlitzBasic
  1. Const ConfigFile$ = "config.cfg"
  2.  
  3. Type file
  4.         Field txt$
  5. End Type
  6.  
  7. Function ConfigValue$(variable$)
  8.         rf = ReadFile(ConfigFile$)
  9.         If rf
  10.                 While Not Eof(rf)
  11.                         txt$ = ReadLine$(rf)
  12.                         If Left$(Lower(txt$), Instr(txt$, "=")) = Lower(variable$ + "=")
  13.                                 Return Right$(txt$, Len(txt$) - Instr(txt$, "="))
  14.                         EndIf
  15.                 Wend
  16.                 CloseFile rf
  17.         EndIf
  18.         Return False
  19. End Function
  20.  
  21. Function ChangeConfigValue(variable$, value$)
  22.         rf = ReadFile(ConfigFile$)
  23.         If rf
  24.                 For this.file = Each file
  25.                         Delete this
  26.                 Next
  27.                 While Not Eof(rf)
  28.                         this.file = New file
  29.                                 this    xt$ = ReadLine$(rf)
  30.                 Wend
  31.                 CloseFile rf
  32.         EndIf
  33.        
  34.         wf = WriteFile("config.cfg")
  35.         If wf
  36.                 For this.file = Each file
  37.                         If Left$(Lower(this     xt$), Instr(this        xt$, "=")) = Lower(variable$ + "=")
  38.                                 WriteLine wf, Left$(this        xt$, Instr(this xt$, "=")) + value$
  39.                         Else
  40.                                 WriteLine wf, this      xt$
  41.                         EndIf
  42.                 Next
  43.                 CloseFile wf
  44.         EndIf
  45. 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!'


 

SimplePortal 2.3.6 © 2008-2014, SimplePortal