[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Some code. was: Re: !!! sync digest: All in one letter



> > 
> >    Can that person advice us how to do it with more
> >    order than it is now?
>
>post code post code post code
>post code post code post code post code.

Yes. This is good advice which I will try to follow it with QZB!

BTW, I have a small Perl utility  for  parsing  ini-files.  Its
very small (6 Kb) and I have not submitted  it  to  metalab  or
freshmeat, because I am not sure if it is worth it.

Is anybody here interested in such utility?
Anyway, it is so short that I attached it :-P

Please, enjoy!
And test it ;-)

This program (ini-file parser) is something I wish I  had  for
XML...

What do you think about using ini-file format?

Sincerely yours,
Roman A. Suzi

 -- Petrozavodsk -- Karelia -- Russia --
    -- powered by Linux RedHat 5.1 --

#!/usr/bin/perl -w

$ver="forconf 0.9";
# inspired by the program 'conf' written by Breyten Ernsti

$comment="\#";
$out="-";
$fmt="%k=%e\\n";

$usage= <<EOH

$ver * Reads/writes INI-style files
(c) 1998 Roman A.Susi, rnd\@sampo.karelia.ru

Because the program is licensed free of  charge,  there  is  no
warranty for the program. Read well-known GPL for the terms  of
use & distribution.

  forconf -r|-w   -s <section> [-k <key> [-d <default-value>]]
          [<in-file>]   [-o <out-file>]
          [-c <comment-chars>]   [-fmt <format-string>]
  forconf -h | --help
  forconf -v | --version

where %k and %e can be used in <format-string> to print-out key
and entry respectively (this switch is not used in -w mode).

Examples:

  forconf -r -s S -k=K -d D -c ';#' -fmt "%k = %e" /etc/smb.conf
  -- show key K value in section S in the special format
  forconf -w -s=S             -- deletes section S
  forconf -w -s S -k K        -- deletes entry K in section S
  forconf -w -s=S -k K -d E   -- changes entry K in section S to E

EOH
;

# man Getopt::Long

use Getopt::Long;
GetOptions( 
 '-r'          => \$rmode,
 '-w'          => \$wmode,
 '-c=s'        => \$comment,
 '-comment=s'  => \$comment,
 '-s=s'        => \$section,
 '--section=s' => \$section,
 '-k=s'        => \$key,
 '--key=s'     => \$key,
 '-d=s'        => \$default,
 '--default=s' => \$default,
 '-o=s'        => \$out,
 '--strip'     => \$strip,
 '--output=s'  => \$out,
 '--fmt=s'     => \$fmt,
 '-v'          => \$version,
 '--version'   => \$version,
 '-h'          => \$help,
 '--help'      => \$help
);


if ($version) { print "$ver\n"; exit (0);};

if (not ($rmode or $wmode))
{
  print STDERR "ERROR: mode switch not found.\n";
}

if ($help or not ($rmode or $wmode) or ($#ARGV >0))
{
 print $usage;
 exit(0);
}

if (!$section and !$wmode)
{
  print STDERR "ERROR: Which section?\n";
  exit(1);
}

if ($default and !$key)
{
  print STDERR "ERROR: Default without key.\n";
  exit(1);
}

$in=($#ARGV==0)?$ARGV[0]:"-";

# make sure OUT =! IN (if IN =! STDIN)

if ( ($in eq $out) and !($in eq "-") )
{
  print STDERR "ERROR: in and out are same file. Not supported yet.\n";
  exit(1);
}

open IN,  "< $in"  or die "File '$in' not found.";
open OUT, "> $out" or die "Can't open '$out'.";

$cmatch="\[$comment\]";

# do we need to escape escape chars?

$fmt =~ s/\%e/\$e/g;
$fmt =~ s/\%k/\$k/g;

$namec ="\[A-Za-z0-9_ \]";
$namecl="\[A-Za-z0-9_\]";

$cursec="";            # empty section at the beg

if ($rmode)
{
  $done=($default)?0:1;
  while ( <IN> )
  {
    chomp;
    s/\s*$cmatch.*$//go;      # once compiled pattern here and below /o !!!

    if ( /\s*\[($namec*$namecl)\]\s*/o )
    {
      $cursec=$1;
    }
    else
    {
      $c=$_;
      if ($cursec eq $section )
      {
        if ($c=~/\s*($namec*$namecl)\s*\=\s*(.*)\s*/o )
        {
          $k=$1;
          $e=$2;
          if ($key)
          {
            if ( $k eq $key )
            {
              print OUT "$e";
              $done=1;
          } }
          else
          {
            print OUT eval('"'.$fmt.'"');
  } } } } }
  if ($done==0) { print OUT "$default"; };
}

if ($wmode)
{
  # in wmode we print everything and change
  # only specified things
  #   -s  -k  -d     action
  #   +              1 delete the whole section
  #   +   +          2 delete entry
  #   +   +   +      3 change entry
  #                  0 none

  $action=0;
  if ($section)
  {
    $action=1;
    if ($key)
    {
      $action=2;
      if ($default)
      {
        $action=3;
  } } };

  if ($action==1)
  {
    while ( <IN> )
    {
      $o=$_;
      chomp;
      s/\s*$cmatch.*$//go;
      if ($strip) { $o=$_."\n"; if ($o eq "\n") {$o="";}  }
      if ( /\s*\[($namec*$namecl)\]\s*/o )
      {
        $cursec=$1;
        if ( $cursec eq $section) { $o=""; };
      }
      else
      {
        $c=$_;
        if ($c=~/\s*($namec*$namecl)\s*\=\s*(.*)\s*/o )
        {
          if ( $cursec eq $section) { $o=""; };
      } }
      print OUT $o;
  } }

  if ($action==2)
  {
    while ( <IN> )
    {
      $o=$_;
      chomp;
      s/\s*$cmatch.*$//go;
      if ($strip) { $o=$_."\n"; if ($o eq "\n") {$o="";} }
      if ( /\s*\[($namec*$namecl)\]\s*/o )
      {
        $cursec=$1;
      }
      else
      {
        $c=$_;
        if ( ($cursec eq $section) and
             ($c=~/\s*($namec*$namecl)\s*\=\s*(.*)\s*/o) )
        {
          $k=$1; 
          if ( $k eq $key ) { $o=""; };
      } }
      print OUT $o;
  } }

  if ($action==3)
  {
    while ( <IN> )
    {
      $o=$_; chomp;
      s/\s*$cmatch.*$//go;
      if ($strip) { $o=$_."\n"; if ($o eq "\n") {$o="";} }

      if ( /\s*\[($namec*$namecl)\]\s*/o )
      {
        $cursec=$1;
      }
      else
      {
        $c=$_;
        if ( ($cursec eq $section) and
             ($c=~/\s*($namec*$namecl)\s*\=\s*(.*)\s*/o) )
        {
          $k=$1; 
          if ( $k eq $key )
          {
            $o = $k."=".$default."\n";
      } } }
      print OUT $o;
  } }

  if ($action==0)
  {
    while ( <IN> )
    {
      $o=$_; chomp;
      s/\s*$cmatch.*$//go;
      if ($strip) { $o=$_."\n"; if ($o eq "\n") {$o="";} }
      print OUT $o;
  } }

}

# that is all, folks!