Read a configuration file: Difference between revisions

From Rosetta Code
Content added Content deleted
(wiki formatting)
(Visual Basic)
Line 136: Line 136:
puts "needspeeling = #{needspeeling}"
puts "needspeeling = #{needspeeling}"
puts "seedsremoved = #{seedsremoved}"</lang>
puts "seedsremoved = #{seedsremoved}"</lang>

=={{header|Visual Basic}}==

<lang vb>
'
' Configuration file parser routines.
'
' (c) Copyright 1993 - 2011 Mark Hobley
'
' This configuration parser contains code ported from an application program
' written in Microsoft Quickbasic
'
' This code can be redistributed or modified under the terms of version 1.2 of
' the GNU Free Documentation Licence as published by the Free Software Foundation.
'

Sub readini()
var.filename = btrim$(var.winpath) & ini.inifile
var.filebuffersize = ini.inimaxlinelength
Call openfileread
If flg.error = "Y" Then
flg.abort = "Y"
Exit Sub
End If
If flg.exists <> "Y" Then
flg.abort = "Y"
Exit Sub
End If
var.inistream = var.stream
readinilabela:
Call readlinefromfile
If flg.error = "Y" Then
flg.abort = "Y"
Call closestream
flg.error = "Y"
Exit Sub
End If
If flg.endoffile <> "Y" Then
iniline$ = message$
If iniline$ <> "" Then
If Left$(iniline$, 1) <> ini.commentchar AND Left$(iniline$, 1) <> ini.ignorechar Then
endofinicommand% = 0
For l% = 1 To Len(iniline$)
If Mid$(iniline$, l%, 1) < " " Then
endofinicommand% = l%
End If
If Not (endofinicommand%) Then
If Mid$(iniline$, l%, 1) = " " Then
endofinicommand% = l%
End If
End If
If endofinicommand% Then
l% = Len(iniline$)
End If
Next l%
iniarg$ = ""
If endofinicommand% Then
If endofinicommand% <> Len(iniline$) Then
iniarg$ = btrim$(Mid$(iniline$, endofinicommand% + 1))
If iniarg$ = "" Then
GoTo readinilabelb
End If
inicommand$ = Left$(iniline$, endofinicommand% - 1)
End If
Else
inicommand$ = btrim$(iniline$)
End If
readinilabelb:
'interpret command
inicommand$ = UCase$(inicommand$)
Select Case inicommand$
Case "FULLNAME"
If iniarg$ <> "" Then
ini.fullname = iniarg$
End If
Case "FAVOURITEFRUIT"
If iniarg$ <> "" Then
ini.favouritefruit = iniarg$
End If
Case "NEEDSPEELING"
ini.needspeeling = "Y"
Case "SEEDSREMOVED"
ini.seedsremoved = "Y"
Case "OTHERFAMILY"
If iniarg$ <> "" Then
ini.otherfamily = iniarg$
CALL familyparser
End If
Case Else
'!! error handling required
End Select
End If
End If
GoTo readinilabela
End If
Call closestream
Exit Sub
readinierror:

End Sub

</lang>

Revision as of 23:14, 4 March 2011

Read a configuration file is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

The task is to read a configuration file in standard configuration file, 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 begininning 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.

  • othernames(1) = Rhu Barber
  • othernames(2) = Harry Barber

D

<lang d>import std.stdio, std.getopt, std.string, std.conv, std.regexp ;

template VarName(alias Var) { enum VarName = Var.stringof.toupper ; }

void setOpt(alias Var)(const string line) {

   auto m = RegExp(`^`~VarName!Var~`(\s+(.*))?`).match(line) ;
   if(m.length > 0) {
       static if (is(typeof(Var) == string))
           Var = m.length > 2 ? m[2] : "" ;
       static if (is(typeof(Var) == bool))
           Var = true ;
       static if (is(typeof(Var) == int))
           Var = m.length > 2 ? to!int(m[2]) : 0 ;
   }

}

void main(string[] args) {

   string fullname, favouritefruit ;
   bool needspeeling, seedsremoved ; // default false ;
   int count ; // a line of "COUNT 5" added at end of config file
   foreach(line ; File("readcfg.txt").byLine) {
       auto opt = chomp(text(line)) ;
       setOpt!fullname(opt) ;
       setOpt!favouritefruit(opt) ;
       setOpt!needspeeling(opt) ;
       setOpt!seedsremoved(opt) ;
       setOpt!count(opt) ;
   }
   writefln("%14s = %s", VarName!fullname, fullname) ;
   writefln("%14s = %s", VarName!favouritefruit, favouritefruit) ;
   writefln("%14s = %s", VarName!needspeeling, needspeeling) ;
   writefln("%14s = %s", VarName!seedsremoved, seedsremoved) ;
   writefln("%14s = %s", VarName!count, count) ;

}</lang>

J

<lang j>require'regex' set=:4 :'(x)=:y'

cfgString=:4 :0

 y set 
 (1;&,~'(?i:',y,')\s*(.*)') y&set rxapply x

)

cfgBoolean=:4 :0

 y set 0
 (1;&,~'(?i:',y,')\s*(.*)') y&set rxapply x
 if.-.0-:y do.y set 1 end.

)

taskCfg=:3 :0

 cfg=: ('[#;].*';) rxrplc 1!:1<y
 cfg cfgString 'fullname'
 cfg cfgString 'favouritefruit'
 cfg cfgBoolean 'needspeeling'
 cfg cfgBoolean 'seedsremoved'
 i.0 0

)</lang>

Example use:

<lang j> taskCfg 'fruit.conf'

  (,' = ',]&.do)&>;: 'fullname favouritefruit needspeeling seedsremoved'

fullname = Foo Barber favouritefruit = banana needspeeling = 1 seedsremoved = 0 </lang>

Ruby

<lang ruby>fullname = favouritefruit = needspeeling = seedsremoved = false

open("fruit.conf", "r") do |file|

 file.each_line do |line|
   line.chomp!
   key, value = line.split(nil, 2)
   case key
   when /^([#;]|$)/; # ignore line
   when "FULLNAME"; fullname = value
   when "FAVOURITEFRUIT"; favouritefruit = value
   when "NEEDSPEELING"; needspeeling = true
   when "SEEDSREMOVED"; seedsremoved = true
   when /^./; puts "#{key}: unknown key"
   end
 end

end

puts "fullname = #{fullname}" puts "favouritefruit = #{favouritefruit}" puts "needspeeling = #{needspeeling}" puts "seedsremoved = #{seedsremoved}"</lang>

Visual Basic

<lang vb> ' ' Configuration file parser routines. ' ' (c) Copyright 1993 - 2011 Mark Hobley ' ' This configuration parser contains code ported from an application program ' written in Microsoft Quickbasic ' ' This code can be redistributed or modified under the terms of version 1.2 of ' the GNU Free Documentation Licence as published by the Free Software Foundation. '

Sub readini()

 var.filename = btrim$(var.winpath) & ini.inifile
 var.filebuffersize = ini.inimaxlinelength
 Call openfileread
 If flg.error = "Y" Then
   flg.abort = "Y"
   Exit Sub
 End If
 If flg.exists <> "Y" Then
   flg.abort = "Y"
   Exit Sub
 End If
 var.inistream = var.stream

readinilabela:

 Call readlinefromfile
 If flg.error = "Y" Then
   flg.abort = "Y"
   Call closestream
   flg.error = "Y"
   Exit Sub
 End If
 If flg.endoffile <> "Y" Then
   iniline$ = message$
   If iniline$ <> "" Then
     If Left$(iniline$, 1) <> ini.commentchar AND Left$(iniline$, 1) <> ini.ignorechar Then
       endofinicommand% = 0
       For l% = 1 To Len(iniline$)
         If Mid$(iniline$, l%, 1) < " " Then
           endofinicommand% = l%
         End If
         If Not (endofinicommand%) Then
           If Mid$(iniline$, l%, 1) = " " Then
             endofinicommand% = l%
           End If
         End If
         If endofinicommand% Then
           l% = Len(iniline$)
         End If
       Next l%
       iniarg$ = ""
       If endofinicommand% Then
         If endofinicommand% <> Len(iniline$) Then
           iniarg$ = btrim$(Mid$(iniline$, endofinicommand% + 1))
           If iniarg$ = "" Then
             GoTo readinilabelb
           End If
           inicommand$ = Left$(iniline$, endofinicommand% - 1)
         End If
       Else
         inicommand$ = btrim$(iniline$)
       End If

readinilabelb:

       'interpret command
       inicommand$ = UCase$(inicommand$)
       Select Case inicommand$
         Case "FULLNAME"
           If iniarg$ <> "" Then
             ini.fullname = iniarg$
           End If
         Case "FAVOURITEFRUIT"
           If iniarg$ <> "" Then
             ini.favouritefruit = iniarg$
           End If
         Case "NEEDSPEELING"
           ini.needspeeling = "Y"
         Case "SEEDSREMOVED"
           ini.seedsremoved = "Y"
         Case "OTHERFAMILY"
           If iniarg$ <> "" Then
             ini.otherfamily = iniarg$
             CALL familyparser
           End If
         Case Else
           '!! error handling required
       End Select
     End If
   End If
   GoTo readinilabela
 End If
 Call closestream
 Exit Sub

readinierror:

End Sub

</lang>