]> git.p6c8.net - devedit.git/blob - modules/Config/DevEdit.pm
7402ff19361a15a0089e2b5491967067e214d36b
[devedit.git] / modules / Config / DevEdit.pm
1 package Config::DevEdit;
2
3 #
4 # Dev-Editor - Module Config::DevEdit
5 #
6 # Read and parse the configuration files
7 #
8 # Author: Patrick Canterino <patrick@patshaping.de>
9 # Last modified: 2005-01-06
10 #
11
12 use strict;
13
14 use vars qw(@EXPORT);
15 use Carp qw(croak);
16
17 ### Export ###
18
19 use base qw(Exporter);
20
21 @EXPORT = qw(read_config);
22
23 # read_config()
24 #
25 # Read the configuration files of Dev-Editor
26 #
27 # Params: Path to main configuration file
28 #
29 # Return: Configuration (Hash Reference)
30
31 sub read_config($)
32 {
33 my $file = shift;
34
35 my $config = parse_config($file);
36
37 $config->{'errors'} = parse_config($config->{'error_file'});
38 $config->{'templates'} = parse_config($config->{'template_file'});
39
40 return $config;
41 }
42
43 # parse_config()
44 #
45 # Parse a configuration file
46 #
47 # Params: Path to configuration file
48 #
49 # Return: Configuration (Hash Reference)
50
51 sub parse_config($)
52 {
53 my $file = shift;
54 local *CF;
55
56 open(CF,'<'.$file) or croak("Open $file: $!");
57 read(CF, my $data, -s $file);
58 close(CF);
59
60 my @lines = split(/\015\012|\012|\015/,$data);
61 my $config = {};
62 my $count = 0;
63
64 foreach my $line(@lines)
65 {
66 $count++;
67
68 next if($line =~ /^\s*#/);
69 next if($line !~ /^\s*\S+\s*=.*$/);
70
71 my ($key,$value) = split(/=/,$line,2);
72
73 # Remove whitespaces at the beginning and at the end
74
75 $key =~ s/^\s+//g;
76 $key =~ s/\s+$//g;
77 $value =~ s/^\s+//g;
78 $value =~ s/\s+$//g;
79
80 croak "Configuration option '$key' defined twice in line $count of configuration file '$file'" if($config->{$key});
81
82 $config->{$key} = $value;
83 }
84
85 return $config;
86 }
87
88 # it's true, baby ;-)
89
90 1;
91
92 #
93 ### End ###

patrick-canterino.de