Reading config file

Mr_Gee

Active Member
Licensed User
I'm creating my first (simple) application and I like to save the config..
I have been playing around with this this
Which works, but i'd like to see "name=value" in the config file..
I've already adapted the write part, but the reading part is giving me headaches
So my question to you is
:sign0085:

The code in the tutorial is reading the ini file line by line, this also prevents comments (which I would also like :)
I think I might be able to use the StrIndexOf and then a StrSlice, but first things first..

I was thinking something like the below to start with (bold part is changed)
B4X:
Sub LoadINI
  ErrorLabel(errLoadINI)
  If Not(FileExist("Config.ini")) Then Return
  FileOpen(c2,"Config.ini",cRead)
  [B]CheckBox1.Checked = StrSplit(FileRead(c2),"=")
  CheckBox2.Checked = StrSplit(FileRead(c2),"=")[/B]
  FileClose(c2)
  Return
  errLoadINI:
  Msgbox("Error reading INI file.","",cMsgboxOK,cMsgboxHand)
  FileClose(c2)
End Sub
the only issue with this is that there is no value assigned to the CheckBox
Because i'm not specifying which part of the split I want to use...
I couldn't find how to do this, I tried the JScript way, but that didn't work :-(

Any help is highly appreciated

Thanks!
 

agraham

Expert
Licensed User
You can ignore comments and blank lines with something like this. In this case a comment begins with a single quote

B4X:
Do
  r = FileRead(c1)
Loop Until r <> "" AND StrAt(r,0) <> "'"
You could parse your lines something like this.
B4X:
Sub Globals
  'Declare the global variables here.
  Dim temp(0)
End Sub

Sub App_Start
  Form1.Show
   temp() = StrSplit("Checkbox1=true","=")
  Control(temp(0)).checked = StrToLower(temp(1))
End Sub
Note that if you are using true and false like this they must be "true" and "false" not "True" and "False" or an error will occur. If the values are human entered you may need to check for leading/trailing spaces.
 

Mr_Gee

Active Member
Licensed User
You can ignore comments and blank lines with something like this.
In this case a comment begins with a single quote
Note that if you are using true and false like this they must be "true" and "false" not "True" and "False" or an error will occur. If the values are human entered you may need to check for leading/trailing spaces.
Cool, so by replacing the "'" with "#" would accomplish the same
Maybe i should put in a function which does a StrToLower and StrReplace spaces, just to be sure

Thanks for the help :)
 

Mr_Gee

Active Member
Licensed User
Yes, it looks usable, but my italian is not really up-to-par :)
I need to dive into it, but thatnks for the heads-up
 

maXim

Active Member
Licensed User
Hi Mr_Gee

in db2000 - INI library (DLL version) there are two operating modes whose functions are very simple:

MODE 1

' initialize mode 1
.New1([INIFileName])
' example:
INI.New1("test.ini")

' write or update a row into INI file
.write1([Section], [Key], [Value])
' example:
INI.write1("USER", "Name", "maXim")

' read a row from INI file
.read1([Section], [Key], [DefaultValue])
' example:
myVAR = INI.read1("USER", "Name", "empity")
' if row not exist or is not valid, value of myVar is equal as [DefaultValue]

' delete from INI file a specific [Section] and all his keys
.DeleteSection1([Section])
' example:
INI.DeleteSection1("USER")

MODE 2

' initialize mode 2
.New2
' example:
INI.New2

' write or update a row into INI file ([INIFileName])
.write2([INIFileName], [Section], [Key], [Value])
' example:
INI.write2("test.ini", "USER", "Name", "maXim")

' read a row from INI file ([INIFileName])
.read2([INIFileName], [Section], [Key], [DefaultValue])
' if row not exist or is not valid, value of myVar is equal as [DefaultValue]
' example:
myVAR = INI.read2("test.ini", "USER", "Name", "empity")

' delete from INI file ([INIFileName]) a specific [Section] and all his keys
.DeleteSection2([INIFileName], [Section])
' example:
INI.DeleteSection2("test.ini", "USER")

I am always available for You if has difficulty of use or if it needs more clarification.

Best Regards.

Massimo
 
Last edited:
Top