On Thu, 25 Jan 2001, Niklas Frykholm wrote:

> In article <J%Hb6.6735$wz.209253 / nntp1.chello.se>, Niklas Backlund wrote:
> >The "main loop" in the method that reads the file, reads
> >a parameter on each line (for simplicity) according to a
> >keyword-value scheme, and constructs the appropriate
> >objects. However, some of the data I want to read are
> >strings containing line breaks. I solved it by doing
> >something along the lines of here documents, i e
> >something like
> >
> >parameter value
> >parameter value
> >long-parameter WHATEVER
> >  data
> >  data
> >WHATEVER
> >parameter value
> >...
> >
> >So I'd like the main loop to read normal parameters, and
> >when it encounters a long parameter-type, let another
> >method copy a few lines "verbatim", and when it's done,
> >assign the data to the appropriate instance variable and
> >let the main loop pick up after the end marker as if
> >nothing happened...
>
> Do you mean that the main loop would have to know which parameters are
> long and normal? It seems better to encode this in the file.
>
> In my opinion, rolling your own parser for a config file is seldom a good
> solution. It is better to use an existing parser that provides a familiar
> syntax, supports comments, can report about errors, etc.
>
> In ruby, we can use the ruby parser itself. One way is to write your config
> file as.
>
> 	param1 = "value1"
> 	param2 = "value2"
> 	param3 = <<PARAM3
> 	A
> 	long
> 	value
> 	PARAM3
>
> And use it as:
>
> 	def read_config(file)
> 		eval(IO::File.new(file).read)
> 		binding
> 	end
>
> 	params = read_config("config")
> 	p eval "param1", params
> 	p eval "param3", params
>
> Note that you should only use this method if the person who writes the
> config file is trusted, since that person can specify arbitrary code for
> the program to execute.
>
> // Niklas


You may also have a look at EDF (Easy Data Format) available at RAA.
A file is divided into sections, which can be of any name, each section contains
any number of parameters. Of course you can have a file without sections which parameter
belong to the default-section named "":

require "edf"

str = <<EOF
param: Value 1
       this line belongs to param because the first character is a whitespace
param2: Value2
EOF

default = EDF::parse(str)[0]
p default["param"]
p default["param2"]


------
Multiple sections are written like this:

:section1
param: test
param2: test2

:section2
para6: why



--
Michael Neumann