]> mj.ucw.cz Git - pciids.git/blob - PciIds/Config.pm
Revision of the help texts.
[pciids.git] / PciIds / Config.pm
1 #       PciIds web database
2 #       Copyright (C) 2008 Michal Vaner (vorner@ucw.cz)
3 #
4 #       This program is free software; you can redistribute it and/or modify
5 #       it under the terms of the GNU General Public License as published by
6 #       he Free Software Foundation; either version 2 of the License, or
7 #       (at your option) any later version.
8 #
9 #       This program is distributed in the hope that it will be useful,
10 #       but WITHOUT ANY WARRANTY; without even the implied warranty of
11 #       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 #
13 #       GNU General Public License for more details.
14 #
15 #       You should have received a copy of the GNU General Public License
16 #       along with this program; if not, write to the Free Software
17 #       Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
18
19 package PciIds::Config;
20 use strict;
21 use warnings;
22 use PciIds::Startup;
23 use base 'Exporter';
24
25 our @EXPORT = qw(&checkConf &defConf %config &confList);
26
27 our %config;
28
29 sub loadConf() {
30         open CONFIG, $directory."cf/config" or die "Config file not found. Make sure config is in the cf directory and the correct path is in Startup.pm\n";
31         foreach( <CONFIG> ) {
32                 next if( /^\s*(|#.*)$/ );
33                 chomp;
34                 my( $name, $val );
35                 die "Invalid syntax on line $_\n" unless( ( $name, $val ) = /^\s*(.*\S)\s*=\s*(.*\S)\s*$/ );
36                 $val =~ s/^"(.*)"$/$1/;
37                 ( $val ) = ( $val =~ /(.*)/ ); #Untaint the value - config is considered part of the program
38                 $config{$name} = $val;
39         }
40         close CONFIG;
41 }
42
43 sub checkConf( $ ) {
44         my( $names ) = @_;
45         foreach( @{$names} ) {
46                 die "Variable not set: $_\n" unless( defined $config{$_} );
47         }
48 }
49
50 sub defConf( $ ) {
51         my( $underlay ) = @_;
52         foreach( keys %{$underlay} ) {
53                 $config{$_} = $underlay->{$_} unless( defined $config{$_} );
54         }
55 }
56
57 sub confList( $ ) {
58         my( $names ) = @_;
59         my( @result );
60         push @result, $config{$_} foreach( @{$names} );
61         return( @result );
62 }
63
64 loadConf();
65
66 return 1;