#!/usr/bin/perl
# -----------------------------------------------------------------------------
# TWC
# Treuwert-Computer GmbH
# Schloßbergring 9
# 79098 Freiburg
#
# Author: Klaus Reger <K.Reger@twc.de>
#
# -----------------------------------------------------------------------------
# Program-Data
# -----------------------------------------------------------------------------
# Project  :
# System   :
# Program  :
# Module   : $RCSfile: ConfigPool.pm,v $
# Version  : $Revision: 1.2 $
# Date     : $Date: 2002/02/25 10:50:44 $
# State    : $State: Exp $
#
# Description:
#   This module handles all the things of WWWdb, which have
#   to do with ConfigPools
#
# Change-log (See at end of this file)
#
# --- Known Bugs -------------
# please look at the BUGS-File
# --- TODO -------------------
# please look at the TODO-File
# ----------------------------------------------------------------------------

=head1 NAME

WWWdb::ConfigPool - ConfigPools for WWWdb

=head1 SYNOPSIS

 use WWWdb::ConfigPool;

 $oConfigPool = ConfigPool->new($cFileName,
                                [ReadOnly  => $bReadOnly,]
                                [MustExist => $bMustExist]);

 $cValue      = $oConfigPool->GetValue($cSection,
                                       $cEntry);

 @lValue      = $oConfigPool->GetList($cSection,
                                      $cEntry);

 $cValue      = $oConfigPool->SetValue($cSection,
                                       $cEntry,
                                       $cValue);

 $oConfigPool->DESTROY>();

=cut

# The POD text continues at the end of the file.


use WWWdb::Base;
use WWWdb::List;

use strict;




# --------------------------------------------------------------------
# ConfigPool
# --------------------------------------------------------------------
package ConfigPool;
use vars qw(%hMethods %hAttribs);

@ConfigPool::ISA = qw(Base);

{
    %hAttribs =
        (
         Files    => ["o", "private",  "r", undef]
         );
}


# --- constructor ---------------------------------------------------
sub ConfigPool::new()
{
   my ($cPkgPI) = shift;

   my $cClass   = ref($cPkgPI) || $cPkgPI;
   my $hSelf    = $cClass->SUPER::new(@_);

   bless ($hSelf, $cClass);

   $hSelf->{"Files"} = List->new();

   return $hSelf;
}





# --- get all Sections of the Config-File -------------------------------------
sub ConfigPool::addConfigFile($)
{
   my $hSelfPI    = shift;
   my $oConfigFilePI = shift;

   my @lResult = $hSelfPI->{"Files"}->Append($oConfigFilePI);

   return @lResult;
}



# --- get all Sections of the Config-File -------------------------------------
sub ConfigPool::getSectionNames($)
{
   my $hSelfPI    = shift;

   my @lResult = ();
   my %hSections = ();
   my $cSection;

   foreach (reverse $hSelfPI->{"Files"}->Get())
   {
      foreach $cSection ($_->getSectionNames())
      {

         push @lResult, $cSection
             unless defined $hSections{$cSection};

         $hSections{$cSection} = 1;

      }
   }

   return @lResult;
}



# --- Append a ConfigFile-element ----------------------------------------------
sub ConfigPool::getEntryNames($)
{
   my $hSelfPI        = shift;
   my $cSectionNamePI = shift;

   my @lResult = ();
   my %hEntries = ();
   my $cEntry;

   foreach (reverse $hSelfPI->{"Files"}->Get())
   {
      if($_->getSections()->Get($cSectionNamePI))
      {

         foreach $cEntry ($_->getSections()->
                          Get($cSectionNamePI)->
                          getEntryNames())
         {
            push @lResult, $cEntry
                unless defined $hEntries{$cEntry};

            $hEntries{$cEntry} = 1;
         }
      }
   }

   return @lResult;
}


# --- Get Value of a ConfigPool-element ---------------------------------------
sub ConfigPool::getValue($$;$)
{
   my $hSelfPI    = shift;
   my $cSectionPI = shift;
   my $cEntryPI   = shift;
   my $cDefaultPI = shift;

   my $oSection;
   my $oEntry;

   my $cResult = undef;

   foreach (reverse $hSelfPI->{"Files"}->Get())
   {

      $oSection = $_->getSections()->Get("$cSectionPI");

      if ($oSection)
      {
         $oEntry   = $oSection->getEntries()->Get("$cEntryPI");

         if ($oEntry)
         {
            $cResult = $oEntry->getValue();
            last;
         }
      }
   }

   # Set a default-value
   if (!defined $cResult)
   {
      $cResult = $cDefaultPI;
   }

   return $cResult;
}



# --- Get Value of a ConfigPool-element ---------------------------------------
sub ConfigPool::setValue($$$)
{
   my $hSelfPI    = shift;
   my $cSectionPI = shift;
   my $cEntryPI   = shift;
   my $cValuePI   = shift;

   my $oSection   = undef;
   my $oEntry;

   my $cResult = undef;

   foreach (reverse $hSelfPI->{"Files"}->Get())
   {

      $oSection = $_->getSections()->Get("$cSectionPI")
          unless $_->getReadOnly();

      # Create section if it does not exist
      if ($oSection)
      {
         $oEntry   = $oSection->getEntries()->Get("$cEntryPI");

         # Create entry if it does not exist
         if ($oEntry)
         {
            $cResult = $oEntry->setValue("$cValuePI");
         }
      }
   }

   return $cResult;
}






1;


__END__


=head1 DESCRIPTION

=head1 The ConfigPool class

...

=head2 Note

=over 4

Currently the configfile is supported in the ini-file style.

=item B<sections>

In this style, the entries can be placed in sections (and
subsections). Sections are placed in B<[> and B<]> parens.
This looks like the following:

 [section]

or

 [Section subsection]

=item B<entries>

The single entries are described in the form B<entry = value>.
Single-lined entries as well as multiple-lined entries are
supported. They look like the following examples:

 entry = value

 entry = "value1 value2"

 entry = <<EOF
 value1
 value2
 value3
 EOF

=item B<comments>

Comments are always on a separate line. They start with a B<#> or B<;>.
Some examples:

 # This is a comment
 ; This is a comment too
 no_comment = # no comment


=back

=head2 Class Methods

=over 4

=item B<new>

 $oConfigPool     = ConfigPool->new([FillAtInsert => $bFill]);

...

=back

=head2 Class Attributes

=over 4

=item B<...>

...

=back
