]> git.p6c8.net - devedit.git/blob - modules/Config/DevEdit.pm
- Format HTTP root as hyperlink
[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: 2004-12-31
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
63 foreach my $line(@lines)
64 {
65 next if($line =~ /^\s*#/);
66 next if($line !~ /^\s*\S+\s*=.+$/);
67
68 my ($key,$value) = split(/=/,$line,2);
69
70 # Remove whitespaces at the beginning and at the end
71
72 $key =~ s/^\s+//g;
73 $key =~ s/\s+$//g;
74 $value =~ s/^\s+//g;
75 $value =~ s/\s+$//g;
76
77 croak "Double defined value '$key' in configuration file '$file'" if($config->{$key});
78
79 $config->{$key} = $value;
80 }
81
82 return $config;
83 }
84
85 # it's true, baby ;-)
86
87 1;
88
89 #
90 ### End ###

patrick-canterino.de