| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
use strict; |
|---|
| 8 |
use File::Basename; |
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
my $path = dirname(__FILE__) . "/"; |
|---|
| 12 |
my $configHash = {}; |
|---|
| 13 |
my $line; |
|---|
| 14 |
my $currentSection = ""; |
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
open CONFIGFILE, $path . "roguedetect.cfg"; |
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
while ($line = <CONFIGFILE>) { |
|---|
| 23 |
|
|---|
| 24 |
chomp $line; |
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
if ($line =~ m/^ |
|---|
| 28 |
next; |
|---|
| 29 |
} |
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
if ($line =~ /\[(.*)\]/) { |
|---|
| 33 |
$currentSection = $1; |
|---|
| 34 |
next; |
|---|
| 35 |
} |
|---|
| 36 |
|
|---|
| 37 |
|
|---|
| 38 |
if ($line =~ /(.*?)=(.*)/) { |
|---|
| 39 |
|
|---|
| 40 |
my $value = $2; |
|---|
| 41 |
my $key = $1; |
|---|
| 42 |
$value =~ s/"//g; |
|---|
| 43 |
|
|---|
| 44 |
# trim whitespace off ends |
|---|
| 45 |
$value =~ s/^\s+//; |
|---|
| 46 |
$value =~ s/\s+$//; |
|---|
| 47 |
$key =~ s/^\s+//; |
|---|
| 48 |
$key =~ s/\s+$//; |
|---|
| 49 |
|
|---|
| 50 |
#no section yet |
|---|
| 51 |
if($currentSection !~ m/.+/) { |
|---|
| 52 |
$configHash->{$key} = $value; |
|---|
| 53 |
} |
|---|
| 54 |
|
|---|
| 55 |
#section; |
|---|
| 56 |
else { |
|---|
| 57 |
$configHash->{$currentSection}->{$key} = $value; |
|---|
| 58 |
} |
|---|
| 59 |
} |
|---|
| 60 |
} #read per line |
|---|
| 61 |
|
|---|
| 62 |
|
|---|
| 63 |
close CONFIGFILE; |
|---|
| 64 |
|
|---|
| 65 |
#pass $configHash to conf, this will put it into the static array |
|---|
| 66 |
configure($configHash); |
|---|
| 67 |
|
|---|
| 68 |
#destory $configHash, $line, and $currentSection, freeing memory |
|---|
| 69 |
undef $configHash; |
|---|
| 70 |
undef $line; |
|---|
| 71 |
undef $currentSection; |
|---|
| 72 |
|
|---|
| 73 |
#make $conf in-accessable without using configure |
|---|
| 74 |
BEGIN { |
|---|
| 75 |
my $conf = {}; |
|---|
| 76 |
sub configure { |
|---|
| 77 |
my $param = shift; |
|---|
| 78 |
my $clobber = shift; |
|---|
| 79 |
if (ref($param) eq 'HASH') { |
|---|
| 80 |
#check if param is hashref, if so we are setting values |
|---|
| 81 |
#set values if not there, or clobber set, if trying to replace |
|---|
| 82 |
#without clobber set, die |
|---|
| 83 |
while ((my $key, my $val) = each(%{$param})) { |
|---|
| 84 |
if (ref($val) eq 'HASH') { |
|---|
| 85 |
while ((my $key2, my $val2) = each(%{$val})) { |
|---|
| 86 |
if (exists($conf->{$key}->{$key2}) and not $clobber) { |
|---|
| 87 |
die "Trying to clobber '$key->$key2'."; |
|---|
| 88 |
} #end clobber check |
|---|
| 89 |
#write value to array |
|---|
| 90 |
$conf->{$key}->{$key2} = $val2; |
|---|
| 91 |
} |
|---|
| 92 |
} |
|---|
| 93 |
else { |
|---|
| 94 |
if (exists($conf->{$key}) and not $clobber) { |
|---|
| 95 |
die "Trying to clobber '$key'."; |
|---|
| 96 |
} #end clobber check |
|---|
| 97 |
$conf->{$key} = $val; |
|---|
| 98 |
} |
|---|
| 99 |
} |
|---|
| 100 |
} #end setting vals |
|---|
| 101 |
else { |
|---|
| 102 |
#if param isn't a hashref, then return the request, if returning hash, |
|---|
| 103 |
#copy it to a new one so that conf values can't be changed from outside |
|---|
| 104 |
#the conf function |
|---|
| 105 |
if (ref($conf->{$param}) eq 'HASH') { |
|---|
| 106 |
return %{$conf->{$param}}; |
|---|
| 107 |
} #return hash val |
|---|
| 108 |
else { |
|---|
| 109 |
return $conf->{$param}; |
|---|
| 110 |
} #return normal val |
|---|
| 111 |
} #end getting vals |
|---|
| 112 |
} #end configure sub |
|---|
| 113 |
} #end begin |
|---|
| 114 |
|
|---|
| 115 |
return 1; |
|---|
| 116 |
|
|---|
| 117 |
|
|---|