[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Parsing files

barjunk

10/6/2006 1:11:00 AM

I'm new to ruby and one of the first projects I am trying to tackle is
parsing a file
with this sort of format:

zone "localdomain" IN {
type master;
file "localdomain.zone";
allow-update { none; };
};

or host devicename-1 {
hardware ethernet 00:10:51:61:74:FF;
fixed-address 192.168.0.15;
}


The basic issue I'm working to resolve is how can I treat these
sections as "records".

I know I could read the entire file into memory, but I was looking to
only deal with them
on a record by record basis.

One thought was to read through the file and keep a hash of information
about where the
record starts and ends, then when I want to read a record, use that
reference to get the
data.

Has anyone done something similar to this that I can use to learn how
to manage these
types of file formats?

There won't be an issues with concurrent access at this point.

These formats are found when using dhcp, freeradius, and nagios. I'm
sure there are others.

Mike

2 Answers

Logan Capaldo

10/6/2006 1:28:00 AM

0

On Fri, Oct 06, 2006 at 10:15:12AM +0900, barjunk wrote:
> I'm new to ruby and one of the first projects I am trying to tackle is
> parsing a file
> with this sort of format:
>
> zone "localdomain" IN {
> type master;
> file "localdomain.zone";
> allow-update { none; };
> };
>
> or host devicename-1 {
> hardware ethernet 00:10:51:61:74:FF;
> fixed-address 192.168.0.15;
> }
>
>
> The basic issue I'm working to resolve is how can I treat these
> sections as "records".
>
> I know I could read the entire file into memory, but I was looking to
> only deal with them
> on a record by record basis.
>
> One thought was to read through the file and keep a hash of information
> about where the
> record starts and ends, then when I want to read a record, use that
> reference to get the
> data.
>
> Has anyone done something similar to this that I can use to learn how
> to manage these
> types of file formats?
>
> There won't be an issues with concurrent access at this point.
>
> These formats are found when using dhcp, freeradius, and nagios. I'm
> sure there are others.
>
I would write a grammar and a recursive descent parser for files like
these. Someone is bound to reply with some crazy regexp that works, but
only they will ever understand it ;).
> Mike
>

Robert Klemme

10/6/2006 7:40:00 AM

0

On 06.10.2006 03:28, Logan Capaldo wrote:
> I would write a grammar and a recursive descent parser for files like
> these. Someone is bound to reply with some crazy regexp that works, but
> only they will ever understand it ;).

:-)) Basically it depends on how much nesting of brackets is allowed.
If there is indefinite nesting, then yes, a proper parser is the best
solution. If it is just two levels as shown in the example a regexp is
not too hard (to write and understand) IMHO.

Kind regards

robert