]> mj.ucw.cz Git - pciids.git/blob - PciIds/Config.pm
8d7a98affe2f76f952854a284632d111fac1eab6
[pciids.git] / PciIds / Config.pm
1 package PciIds::Config;
2 use strict;
3 use warnings;
4 use PciIds::Startup;
5 use base 'Exporter';
6
7 our @EXPORT = qw(&checkConf &defConf %config &confList);
8
9 our %config;
10
11 sub loadConf() {
12         open CONFIG, $directory."/config" or die "Config file not found. Make sure config is in the directory and the correct path is in Startup.pm\n";
13         foreach( <CONFIG> ) {
14                 next if( /^\s*(|#.*)$/ );
15                 chomp;
16                 my( $name, $val );
17                 die "Invalid syntax on line $_\n" unless( ( $name, $val ) = /^\s*(.*\S)\s*=\s*(.*\S)\s*$/ );
18                 $val =~ s/^"(.*)"$/$1/;
19                 $config{$name} = $val;
20         }
21         close CONFIG;
22 }
23
24 sub checkConf( $ ) {
25         my( $names ) = @_;
26         foreach( @{$names} ) {
27                 die "Variable not set: $_\n" unless( defined $config{$_} );
28         }
29 }
30
31 sub defConf( $ ) {
32         my( $underlay ) = @_;
33         foreach( keys %{$underlay} ) {
34                 $config{$_} = $underlay->{$_} unless( defined $config{$_} );
35         }
36 }
37
38 sub confList( $ ) {
39         my( $names ) = @_;
40         my( @result );
41         push @result, $config{$_} foreach( @{$names} );
42         return( @result );
43 }
44
45 loadConf();
46
47 return 1;