The task is to read a configuration file in standard configuration file format,
and set variables accordingly.

For this task, we have a configuration file as follows:

# This is a configuration file in standard configuration file format
#
# Lines beginning with a hash or a semicolon are ignored by the application
# program. Blank lines are also ignored by the application program.

# This is the fullname parameter
FULLNAME Foo Barber

# This is a favourite fruit
FAVOURITEFRUIT banana

# This is a boolean that should be set
NEEDSPEELING

# This boolean is commented out
; SEEDSREMOVED

# Configuration option names are not case sensitive, but configuration parameter
# data is case sensitive and may be preserved by the application program.

# An optional equals sign can be used to separate configuration parameter data
# from the option name. This is dropped by the parser. 

# A configuration option may take multiple parameters separated by commas.
# Leading and trailing whitespace around parameter names and parameter data fields
# are ignored by the application program.

OTHERFAMILY Rhu Barber, Harry Barber



For the task we need to set four variables according to the configuration entries as follows:

fullname = Foo Barber
favouritefruit = banana
needspeeling = true
seedsremoved = false


We also have an option that contains multiple parameters. These may be stored in an array.

 otherfamily(1) = Rhu Barber
 otherfamily(2) = Harry Barber


Related tasks
 Update a configuration file


' FB 1.05.0 Win64
 
Sub split (s As Const String, sepList As Const String, result() As String)
  If s = "" OrElse sepList = "" Then 
     Redim result(0)
     result(0) = s
     Return
  End If
  Dim As Integer i, j, count = 0, empty = 0, length
  Dim As Integer position(Len(s) + 1)
  position(0) = 0
 
  For i = 0 To len(s) - 1
    For j = 0 to Len(sepList) - 1
      If s[i] = sepList[j] Then 
        count += 1
        position(count) = i + 1       
      End If
    Next j
  Next i
 
  Redim result(count)
  If count  = 0 Then
    result(0) = s
    Return
  End If
 
  position(count + 1) = len(s) + 1
 
  For i = 1 To count + 1  
    length = position(i) - position(i - 1) - 1 
    result(i - 1) = Mid(s, position(i - 1) + 1, length)
  Next
End Sub
 
Type ConfigData
  fullName As String
  favouriteFruit As String
  needsPeeling As Boolean
  seedsRemoved As Boolean
  otherFamily(Any) As String
End Type
 
Sub readConfigData(fileName As String, cData As ConfigData)
  Dim fileNum As Integer = FreeFile
  Open fileName For Input As #fileNum
  If err > 0 Then
    Print "File could not be opened"
    Sleep
    End
  End If
  Dim ln As String
  While Not Eof(fileNum)
    Line Input #fileNum, ln
    If ln = "" OrElse Left(ln, 1) = "#" OrElse Left(ln, 1) = ";" Then Continue While
    If UCase(Left(ln, 8)) = "FULLNAME" Then 
      cData.fullName = Trim(Mid(ln, 9), Any " =")
    ElseIf UCase(Left(ln, 14)) = "FAVOURITEFRUIT" Then
      cData.favouriteFruit = Trim(Mid(ln, 15), Any " =")
    ElseIf UCase(Left(ln, 12)) = "NEEDSPEELING" Then
      Dim s As String = Trim(Mid(ln, 13), Any " =")
      If s = ""  OrElse UCase(s) = "TRUE" Then 
        cData.needsPeeling = True
      Else
        cData.needsPeeling = False
      End If
    ElseIf UCase(Left(ln, 12)) = "SEEDSREMOVED" Then
      Dim s As String = Trim(Mid(ln, 13), Any " =")
      If s = ""  OrElse UCase(s) = "TRUE" Then 
        cData.seedsRemoved = True
      Else
        cData.seedsRemoved = False
      End If
    ElseIf UCase(Left(ln, 11)) = "OTHERFAMILY" Then
       split Mid(ln, 12), ",", cData.otherFamily()
       For i As Integer = LBound(cData.otherFamily) To UBound(cData.otherFamily)
         cData.otherFamily(i) = Trim(cData.otherFamily(i), Any " =")
       Next
    End If
  Wend
  Close #fileNum
End Sub   
 
Dim fileName As String = "config.txt"
Dim cData As ConfigData
readConfigData fileName, cData
Print "Full name        = "; cData.fullName
Print "Favourite fruit  = "; cData.favouriteFruit
Print "Needs peeling    = "; cData.needsPeeling
Print "Seeds removed    = "; cData.seedsRemoved
For i As Integer = LBound(cData.otherFamily) To UBound(cData.otherFamily)
  Print "Other family("; Str(i); ")  = "; cData.otherFamily(i)
Next
Print
Print "Press any key to quit"
Sleep