Read a configuration file: Difference between revisions

From Rosetta Code
Content added Content deleted
(Append a quick Ruby solution. Renamed variable 'needpeeling' to 'needspeeling' to match line 'NEEDSPEELING'.)
(J)
Line 27: Line 27:
*needspeeling = true
*needspeeling = true
*seedsremoved = false
*seedsremoved = false

=={{header|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>


=={{header|Ruby}}==
=={{header|Ruby}}==

Revision as of 14:35, 12 February 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 that begin with a hash symbol or semicolon are treated as
# comments and will be ignored

# 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


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

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>