# -*- Perl -*-

use XML::Parser;

my $target_section  = "";
my $target_entry    = "";
my $current_section = "";
my $entry_value     = "";

sub getProfileString {
    my($cfgfile, $section, $variable, $default) = @_;
    my $parser = new XML::Parser(ErrorContext => 2);
    $parser->setHandlers(Start => \&start_handler);

    $target_section = $section;
    $target_entry = $variable;
    $current_section = "";
    $entry_value = $default;

    $parser->parsefile($cfgfile);

    return $entry_value;
}

sub start_handler {
    my $parser_context = shift;
    my $element = shift;
    my %attr = @_;

    if ($element eq 'section') {
	$current_section = $attr{'name'};
    } elsif ($element eq 'entry' && $current_section eq $target_section) {
	if ($attr{'name'} eq $target_entry) {
	    $entry_value = $attr{'value'};
	    $parser_context->finish();
	}
    }
}

1;
    
