SPUG: Parsing a config file

Tim Maher/CONSULTIX tim at consultix-inc.com
Fri Jan 7 17:08:09 CST 2000


Those of you who want to post source code, please understand that the
majordomo server will get upset when it sees "sub" in your code, thinking
it's a subscription request being sent to the list!  In such cases, the
message is forwarded to the list-owner (me) for manual processing.
I'm going to change sub to ssub, in the hope that will get it through.

-Tim
==========================================================
|  Tim Maher, Ph.D.           Tel/Fax: (206) 781-UNIX    |
|  SPUG Founder & Leader      Email: spug at halcyon.com    |
|  Seattle Perl Users Group   HTTP: www.halcyon.com/spug |
==========================================================

----- Forwarded message from owner-spug-list at pm.org -----

From: owner-spug-list at pm.org
Date: Fri, 7 Jan 2000 17:48:41 -0500 (EST)
X-Authentication-Warning: happyfunball.pm.org: mjordomo set sender to owner-spug-list at pm.org using -f
To: owner-spug-list at pm.org
Subject: BOUNCE spug-list at pm.org:     Admin request of type /^sub\b/i at line 5  

>From tim at consultix-inc.com  Fri Jan  7 17:48:39 2000
Received: from smaug.wrq.com (smaug.wrq.com [150.215.17.2])
	by happyfunball.pm.org (8.9.3/8.9.1) with ESMTP id RAA23975
	for <spug-list at pm.org>; Fri, 7 Jan 2000 17:48:38 -0500 (EST)
Received: from abra.wrq.com (abra.wrq.com [150.215.8.10])
	by smaug.wrq.com (8.8.6 (PHNE_17135)/8.8.6) with ESMTP id OAA08338
	for <spug-list at pm.org>; Fri, 7 Jan 2000 14:54:10 -0800 (PST)
Received: by abra.wrq.com with Internet Mail Service (5.5.2448.0)
	id <CJYQ0CKL>; Fri, 7 Jan 2000 14:54:10 -0800
Message-ID: <1654BC972546D31189DA00508B318AC8A57F59 at charmander.wrq.com>
From: Todd Wells <toddw at wrq.com>
To: "'spug-list at pm.org'" <spug-list at pm.org>
Subject: RE: SPUG: parsing a config file
Date: Fri, 7 Jan 2000 14:54:09 -0800 
MIME-Version: 1.0
X-Mailer: Internet Mail Service (5.5.2448.0)
Content-Type: text/plain;
	charset="iso-8859-1"

Okay, I think I'm a lot closer now, I haven't tested it quite yet, but I'm
doing this based on p299 of the cookbook.  I wanted to send this possible
solution out before any of you spent more time on it:

ssub new_parse
{
        open (CONFIG, "< $config_file")
                or die "Couldn't open $config_file for reading: $!\n";

        while (<CONFIG>)
        {
                chomp;  #remove newline character
                s/^\s*{\s*(?!\S)//;     # remove a lone "{" on a line
                s/^\s*};//;             # remove a line with only "};"
                s/^\s*}\s*(?!\S)//;     # remove a lone "}" on a line
                s/^\s*\w+ = {\s*(?!\S)//; # remove if only "NAME = {"
                next unless length;     # is there anything left?
                s/(.+);$/$1/;           # remove ";" at the end of line
                # now split remaining string into variable and value
                my ($var, $value) = split(/\s*=\s*/, $_, 2);
                $config{$var} = $value; #create a hash of variable/value
pairs
        }
}


-----Original Message-----
From: Todd Wells [mailto:toddw at wrq.com]
Sent: Friday, January 07, 2000 1:38 PM
To: spug-list at pm.org
Subject: SPUG: parsing a config file


Hello, I have a regular expression question for you gurus.  I haven't done
any Perl programming in a couple months and my skills are showing it.  

I would like to parse variables from a data file, and the data file is
created by another app, so I can't change the file format.  Accurately
parsing this file is of utmost importance.  At the bottom of this mail, you
can see the format.

I'd love to have a routine that could parse these settings more easily.
Right now I'm sure I'm doing this the hard way, and I'd like to be more
flexible.  

I'm currently reading all the lines of the file into the array @lines.  Then
I parse as such (partially complete example):

        for ($i = 0; $i < @lines; $i++)
        { 
                if ($lines[$i] =~ /Domain =/) 
                { 
                        $domain_servers = $lines[++$i] =~ 
                                s/servers = (.*);/$1/;
                        $domain_status = $lines[++$i] =~
                                s/status = (.*);/$1/;
			}
	}

Note I've avoided using a foreach because within the routine I want to look
ahead at the next line(s).  I'm sure there's a magic Perl variable that will
tell me which array position I'm at during a foreach, but I couldn't figure
out what the correct variable is.  Forgive that the formatting in this email
didn't come through as cleanly as I would've liked.

I know Perl will let me parse easier than this, but my regular expression
skills are pretty rusty.  It'd be great if it could auto-name the variables
based on what's on the left side of the equals sign, but I could hard-code
variable names if necessary. The other thing I'm not sure how to deal with
are the nested brackets -- for instance, see that the Restart section is
enclosed by {} but it also has a nested {} inside of it.  And I don't need
variables called "Domain" or "Restart" (which are actually section names),
but it'd be great if it could set the variables enclosed in that section.

I'd like this to be more flexible than the way I've started to do it here,
because it may be that unanticipated variables could pop up and my simple
line incrementing may get out-of-synch with the way the config file is
actually formatted.

- Todd


Here's the general config file format:

{
	Domain = {
		servers = ();
		status = Enabled;
	};
	Log = {
		easLogLevel = Standard;
		log = On;
	};
	Restart = {
		Condition = 0;
		Enabled = NO;
		RestartTime = {
			CalenderFormat = "%d/%m/%Y %I:%M %p";
			FromTime = "18/07/1997 03:00 AM";
			ToTime = "18/07/1997 06:00 AM";
		};
		Schedule = 0;
	};
	Security = {
		administrator = Administrators;
		domain = MINE;
		manager = Users;
		security = Off;
		user = Users;
	};
}

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    POST TO: spug-list at pm.org        PROBLEMS: owner-spug-list at pm.org
 Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
 SUBSCRIBE/UNSUBSCRIBE: Replace ACTION below by subscribe or unsubscribe
        Email to majordomo at pm.org: ACTION spug-list your_address


----- End forwarded message -----

-- 
*========================================================================*
| Tim Maher, PhD  Consultix &              (206) 781-UNIX/8649           |
|  Pacific Software Gurus, Inc             Email: tim at consultix-inc.com  |
|  UNIX/Linux & Perl Training              http://www.consultix-inc.com  |
| 2/22: UNIX  2/28: Perl Modules  2/29: Int. Perl  3/3: Pattern Matching |
*========================================================================*

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    POST TO: spug-list at pm.org        PROBLEMS: owner-spug-list at pm.org
 Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
 SUBSCRIBE/UNSUBSCRIBE: Replace ACTION below by subscribe or unsubscribe
        Email to majordomo at pm.org: ACTION spug-list your_address





More information about the spug-list mailing list