# Base.pm
#
# SuperClass with base methods
#
# Ralf Peine, 2004-05-26 Tue

use strict;

package Efg::Base;

sub new {
    my $self = shift;

    my $type = ref($self)  ||  $self;
    my $elem = bless {}, $type; 
    $elem->init();
    return $elem;
}

sub setVar {
    # my $self    = shift;
    # my $varName = shift;
    # my $val     = shift;
    $_[0]->{"_var_$_[1]"} = $_[2];
    # print "setvar ".$_[0]->{"_var_$_[1]"}."\n";
}

sub getVar {
    # my $self    = shift;
    # my $varName = shift;
    my $var = $_[0]->{"_var_$_[1]"};
    return $var if ($var);
    # later search in CFG, _O_PKT_RP Sun Nov 23 12:43:06 2003
    return undef;
}

sub getConfigKeyNamesRef {
    return undef;
}

sub Config {
    my $self = shift;
    
    my $key;
    my $val;

    my $configKeyNamesRef = $self->getConfigKeyNamesRef();

    while ($key = shift) {
	$val = shift;
	my $internalKey = $configKeyNamesRef->{$key};
	if ($internalKey) {
	    $self->{$internalKey} = $val;
	} else {
	    my @caller = caller(0);
	    my $func_name    = $caller[3];
	    my $line_nbr     = $caller[2];
	    my $file_name    = $caller[1];
	    
	    $self->warningMsg('0', "$file_name:$line_nbr", "Ignore unknown config variable $key");
	}
    }
}

sub parseForAndInsertVariables {
    my $self = shift;

    my $str;
    my $res;
    my $val;
    my $var;
    my @resArr;
    $self->{_error} = '';
    foreach my $i (0..$#_) {
	$str = $_[$i];
	$res = '';
	while ($str =~ /\$\(([^\)]+)\)/) {
	    $res .= $`;
	    $var = $1;
	    $str = $'; #' # for emacs
	    if ($var =~ /(\W)/) {
		$self->{_error} .= ",\n\t\t" if $self->{_error};
		$self->{_error} .= "nonword char '$1' found in varName '$var'";
	    } else {
		$val = $self->getVar($var);
		if (defined $val) {
		    $res .= $val ;
		} else {
		    $res .= "\$($var)";
		}
	    }
	}
	if ($str =~ /\$\(/) {
	    $self->{_error} .= "\n\t\t" if $self->{_error};
	    $self->{_error} .= "')' missing for varName '$str'";
	}
	$resArr[$i] = $res.$str;
    }
    return @resArr;
}

sub errorMsg {
    my $self = shift;
    my $errNbr = shift;
    my $errFile = shift;
    my $errMsg = shift;

    print "$errFile:Error (E_$errNbr): $errMsg\n";
}

sub warningMsg {
    my $self = shift;
    my $warnNbr = shift;
    my $warnFile = shift;
    my $warnMsg = shift;

    print "$warnFile:Warning (W_$warnNbr): $warnMsg\n";
}

1;
