-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfiguration.pm
115 lines (102 loc) · 2.26 KB
/
configuration.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package configuration;
use strict;
use warnings;
our %config = (
"DataBase_Type" => "SQLite",
"DataBase_SQLite_File" => "database/sqlite.db",
"DataBase_SQLite_Bin" => "/usr/bin/sqlite3",
"DataBase_Dumps" => "database/dumps",
"DataBase_DumpCmd_Prefix" => "sqlite_",
"PathSeparator_DataBase" => "/",
"PathSeparator_System" => "/",
"HashFunction" => "md5_hex",
"NullFile" => "/dev/null",
);
our %keys = (
"--config-file" => ["config-file"],
"--verbose" => ["verbose"],
"-v" => ["verbose"],
"--debug" => ["debug", "verbose"],
"--debug-lib" => ["debug-lib", "debug", "verbose"],
"--cut" => ["cut"],
);
our %options = (
"config-file" => "core.conf",
"verbose" => "no",
"debug" => "no",
"debug-lib" => "no",
"cut" => "no",
);
sub keys_to_options {
while ( my $arg = shift ) {
unless ( $arg =~ /^-/ ) {
unshift @_, $arg;
last;
}
my ($key, $value);
if ( $arg =~ /^--.+=.+/ ) {
# long key with value
($key, $value) = split /=/, $arg, 2;
} else {
# short key or key without value
($key, $value) = ($arg, "yes");
}
unless ( defined @{ $keys{$key} } ) {
die "Unknown key: '$arg'";
}
my @options_list = @{ $keys{$key} };
foreach (@options_list) {
$options{$_} = $value;
}
}
return @_;
}
sub key_add {
my $key = shift;
my @options_list = @_;
foreach ( @options_list ) {
if ( ! defined $options{$_} ) {
die "Unknown option: '$_'";
}
}
@{ $keys{$key} } = @options_list;
}
sub option_add {
my ($option, $value) = @_;
$options{$option} = $value;
}
sub config_read {
my $config_fd;
open $config_fd, $options{"config-file"}
or die "Can not open config file '$options{\"config-file\"}': $!";
my $line = 1;
while ( <$config_fd> ) {
$_ =~ s/[\n\r]//g;
if ( ( $_ =~ /^#/ ) or ( $_ =~ /^[\t ]?$/ ) ) {
# skip comments and empty lines
next;
}
my ($key, $value) = split /=/, $_, 2;
if ( ! defined $value ) {
die "Error in config file, line $line: '$_'"
}
if ( ! defined $config{$key} ) {
die "Unknown option in config file, line $line: '$key'";
}
$config{$key} = $value;
} continue {
$line++;
}
close $config_fd;
}
sub config_print {
foreach ( sort keys %config ) {
print "$_ => \"$config{$_}\"\n";
}
}
sub options_print {
foreach (sort keys %options ) {
print "$_ => \"$options{$_}\"\n";
}
}
1