package renderer;
require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(create_renderer);

#----------------------------------------------------------------------------
# /var/config/www/cgi-bin/renderer.pm - basics for rendering configuration
#
# Creation:     06.01.2004  max
# Last Update:  $Id$
#
# Copyright (c) 2004-2005  eisfair webconf team
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#----------------------------------------------------------------------------

use strict;
our $VERBOSE;

no strict "subs";
do "conf.pl";
use strict "subs";



# create_renderer description {{{
##############################################################################
# sub create_renderer($package, $classname)
# - creates base hash for renderer
# IN:  $package    - name of the package
#      $classname  - name for the CSS-class
# OUT: renderer    - reference to hash with rendering functions
##############################################################################
# }}}
# sub create_renderer($package, $classname) {{{
sub create_renderer
{
   my ($package, $classname) = @_;

   ### build up hashtable for rendering configfiles
   my (%renderer, %infohash, %statushash);

   $renderer{"info"}          = \%infohash;
   $renderer{"status"}        = \%statushash;

   ### save basic information
   $infohash{"package"}       = $package;
   $infohash{"classname"}     = $classname;

   ### initialize status
   $statushash{"in_group"}    = "false";

   ### add the interface
   $renderer{"call"}          = renderer_call(\%renderer);
   $renderer{"print"}         = renderer_print(\%renderer);

   ### helper functions
   $renderer{"get_source"}    = get_source(\%renderer);
#   $renderer{""}  =

   ### default will be used if no specific function is provided
   $renderer{"print_default"} = print_default(\%renderer);
   $renderer{"print_typeerror"} = print_typeerror(\%renderer);

   ### return renderer
   return \%renderer
}
# }}}

# renderer_call description {{{
########################################################################
# sub renderer_call($function, $param)
# - interface for calling the renderer
# IN:  $function  - function to be called
#      $param     - parameter for the function
# OUT: -
########################################################################
# }}}
# sub renderer_call($function, $param) {{{
sub renderer_call
{
my ($renderer) = @_;
return sub
{
   ### interface for this renderer
   my ($function, $param) = @_;

   $function = "" if (!defined($function));
   $param    = "" if (!defined($param));
   $function = lc($function);

   $renderer->{$function}->($param) if ($renderer->{$function});
}
}
# }}}

# renderer_print description {{{
########################################################################
# sub renderer_print($type, $name, $value, $param)
# - interface for calling the renderer
# IN:  $type      - type to render
#      $name      - name of the element
#      $value     - value of the element
#      $param     - hash with parameters
# OUT: -
########################################################################
# }}}
# sub renderer_print($type, $name, $value, $param) {{{
sub renderer_print
{
my ($renderer) = @_;
return sub
{
   ### interface for this renderer
   my ($type, $name, $value, $param, $param2) = @_;

   $type   = "default"   if (!defined($type));
   $name   = "unknown"   if (!defined($name));
   $value  = ""          if (!defined($value));
   $param  = ""          if (!defined($param));
   $param2 = ""          if (!defined($param2));

   $type  = lc($type);
   my $function = $renderer->{"print_$type"};

   if (!$function && $VERBOSE eq "yes")
   {
      $function  = $renderer->{"print_typeerror"};
   }
   if (!$function)
   {
      $function  = $renderer->{"print_default"};
   }

   $function->($name, $value, $param, $param2) if ($function);
}
}
# }}}

# get_source description {{{
########################################################################
# sub get_source($sname, $href, $do_subst)
# - get all lines within "sourcerange" from source file $sname
# IN:  $sname    - name of source file
#      $range    - sourcerange (from-to)
#      $do_subst - remove all "\r?\n" from the lines
# OUT: $aref     - reference on an array with the lines from source
#                  or "" if no lines were found
########################################################################
# }}}
# sub get_source($sname, $range, $do_subst) {{{
sub get_source
{
my ($renderer) = @_;
return sub
{
   my ($sname, $range, $do_subst) = @_;
   my @item;
   my ($srcfrom, $srcto) = (0, -1);

   if (-f $sname)
   {
      open(SOURCE, "<$sname");

      if ($range =~ /^([1-9]\d*)/)
      {
         $srcfrom = $1 - 1;
         $srcto   = $srcfrom;
      }
      if ($range =~ /^([1-9]\d*)-x/)
      {
         $srcfrom = $1 - 1;
         $srcto   = -1;
      }
      elsif ($range =~ /^([1-9]\d*)-([1-9]\d*)$/)
      {
         $srcfrom = $1 - 1;
         $srcto   = $2;
      }

      while(<SOURCE>)
      {
         next if ($srcfrom-- > 0 && $srcto--);
         s/\r?\n//g         if ($do_subst);
         push(@item, $_);
         last               if (!$srcto--);
      }

      close(SOURCE);
   }

   if (@item) { return \@item; }
   else       { return ""; }
}
}
# }}}


# print_default description {{{
########################################################################
# sub print_default($name, $value, $hstruct)
# - simply prints an element
# IN:  $name     - name of the field 
#      $value    - value of the field
#      $hstruct  - additional parameters
# OUT:           -
########################################################################
# }}}
# sub print_default($name, $value, $hstruct) {{{
sub print_default
{
my ($renderer) = @_;
return sub
{
   my ($name, $value, $hstruct) = @_;
   print "$name = $value\n";
}
}
# }}}

# print_typeerror description {{{
########################################################################
# sub print_typeerror($name, $value, $hstruct)
# - prints an error message
# IN:  $name     - name of the field
#      $value    - value of the field
#      $hstruct  - additional parameters
# OUT:           -
########################################################################
# }}}
# sub print_typeerror($name, $value, $hstruct) {{{
sub print_typeerror
{
my ($renderer) = @_;
return sub
{
   my ($name, $value, $hstruct) = @_;
   print "ERROR: $name = $value\n";
}
}
# }}}

1;

