| 1 | #(c)2000 XML Global Technologies, Inc. | ||
| 2 | # $Id: Config.pm,v 1.2 2000/05/21 23:41:03 matt Exp $ | ||
| 3 | |||
| 4 | package XML::Config; | ||
| 5 | 0 | ▶ | use XML::Parser; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 6 | use vars qw($VERSION); | ||
| 7 | |||
| 8 | my $err_str = undef; | ||
| 9 | $VERSION = 0.2; | ||
| 10 | |||
| 11 | |||
| 12 | sub new { | ||
| 13 | my $class = shift; | ||
| 14 | my $self = {}; | ||
| 15 | bless($self,$class); | ||
| 16 | return($self); | ||
| 17 | } | ||
| 18 | |||
| 19 | |||
| 20 | |||
| 21 | sub load_conf { | ||
| 22 | my ($self, $file, $h, $dno) = @_; | ||
| 23 | |||
| 24 | |||
| 25 | $self->{fill_callers_hash} = 1; | ||
| 26 | $self->{enforce_overwrite} = 1; | ||
| 27 | |||
| 28 | if (!defined($h)) { | ||
| 29 | $self->{fill_callers_hash} = 0; | ||
| 30 | } | ||
| 31 | |||
| 32 | if (!defined($dno)) { | ||
| 33 | $self->{enforce_overwrite} = 0; | ||
| 34 | } | ||
| 35 | |||
| 36 | |||
| 37 | eval { | ||
| 38 | my $xp = new XML::Parser(parent => $self, Handlers => {Char => \&XML::Config::__charparse}); | ||
| 39 | $xp->parsefile($file); | ||
| 40 | undef($xp); | ||
| 41 | }; | ||
| 42 | |||
| 43 | if ($@) { | ||
| 44 | |||
| 45 | if ($@ !~ m!file.*found!i) { | ||
| 46 | my $fbak = $file . '.bak'; | ||
| 47 | undef($@); | ||
| 48 | |||
| 49 | eval { | ||
| 50 | my $xp = new XML::Parser(parent => $self, Handlers => {Char => \&XML::Config::__charparse}); | ||
| 51 | $xp->parsefile($file); | ||
| 52 | $err_str = "WARN: Loaded backup configuration\n"; | ||
| 53 | undef($xp); | ||
| 54 | }; | ||
| 55 | |||
| 56 | if ($@) { | ||
| 57 | $err_str = "PARSE ERROR, BACKUP READ ATTEMPTED: $@\n"; | ||
| 58 | return(undef); | ||
| 59 | } | ||
| 60 | } | ||
| 61 | |||
| 62 | else { | ||
| 63 | $err_str = "PARSE ERROR: $@\n"; | ||
| 64 | return(undef); | ||
| 65 | } | ||
| 66 | |||
| 67 | } | ||
| 68 | |||
| 69 | |||
| 70 | my $conf = $self->{conf}; | ||
| 71 | |||
| 72 | |||
| 73 | if ($self->{fill_callers_hash} > 0) { | ||
| 74 | |||
| 75 | if ($self->{enforce_overwrite}) { | ||
| 76 | for (keys(%{$conf})) { | ||
| 77 | my $k = $_; | ||
| 78 | foreach my $no_o (@{$dno}) { | ||
| 79 | if ($no_o eq $k) { | ||
| 80 | next(); | ||
| 81 | } | ||
| 82 | else { | ||
| 83 | $h->{$k} = $conf->{$k}; | ||
| 84 | } | ||
| 85 | } | ||
| 86 | |||
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | else { | ||
| 91 | for (keys(%{$conf})) { | ||
| 92 | $h->{$_} = $conf->{$_}; | ||
| 93 | } | ||
| 94 | } | ||
| 95 | return(1); | ||
| 96 | } | ||
| 97 | |||
| 98 | else { | ||
| 99 | return %{$conf}; | ||
| 100 | } | ||
| 101 | } | ||
| 102 | |||
| 103 | |||
| 104 | sub err_str { return $err_str } | ||
| 105 | |||
| 106 | sub __charparse { | ||
| 107 | my ($xp,$str) = @_; | ||
| 108 | my $self = $xp->{parent}; | ||
| 109 | return if $str =~ /^\s*$/m; | ||
| 110 | $self->{conf}{$xp->current_element} = $str; | ||
| 111 | } | ||
| 112 | |||
| 113 | __END__ | ||