blob: 4aa3b6c16ef1138107260f068a93dab645b93b6c (
plain)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
|
#!/usr/bin/perl
package docbot::config;
#
# config class for docbot
#
# config file schema:
#
# key = value
# key= value
# key =value
# key=value
# key=
# key = value
# ...
# usage:
#
# $config = docbot::config->new(<configfile>);
# $config->set_autosave(<on/off>);
# $var = $config->get_key(<key name>);
# $config->set_key(<key name>, <new value>);
# $config->delete_key(<key name>);
# %var = $config->get_config_keys();
# $config->save_config(); # <configfile> from new() will be used
# $config->save_config(<new filename>);
use strict;
use POSIX; # some standards
use FileHandle; # have variables for the filehandles
# new()
#
# constructor
#
# parameter:
# - class name
# return:
# - pointer to config class
sub new {
my $class = shift;
# get config file
my $config_file = shift;
# test if config file exists
if (!-f $config_file) {
die "could not find config file: $config_file\n";
}
my $self = {};
# bless mysqlf
bless($self, $class);
# define own variables
# open config
$self->{config} = $self->open_config($config_file);
if (!defined($self->{config})) {
die "could not open/parse config file: $config_file\n";
}
# save config file name for later use
$self->{config_file} = $config_file;
# set config to 'not changed'
$self->{changed} = 0;
# deactivate auto-save mode
$self->{autosave} = 0;
# return reference
return $self;
}
# open_config()
#
# read in a config file
#
# parameter:
# - self
# - config filename
# return:
# - reference to hash with config values
sub open_config {
my $self = shift;
if (!ref($self)) {
die "do not call me from outside!\n";
}
# should be already validated for existence in new()
my $config_file = shift;
my $fh = new FileHandle;
# define hash for config
my %config = ();
open($fh, $config_file) || die "could not open config file ($config_file): $!\n";
# read every line
while (my $line = <$fh>) {
# remove any line ending char
$line =~ s/^(.*?)[\s\r\n]*$/$1/g;
if ($line =~ /^([a-zA-Z0-9\-_]+)[\s\t]*=[\s\t]*(.*)$/) {
$config{$1} = $2;
#print "read config line: $1 -> $2\n";
}
}
close($fh);
# return config
return \%config;
}
# set_autosave()
#
# set autosave mode for config
#
# parameter:
# - self
# - autosave mode (on/off)
# return:
# none
sub set_autosave {
my $self = shift;
my $autosave = shift;
# validate the given mode
if ($autosave eq "1" or lc($autosave) eq "yes" or lc($autosave) eq "on") {
# set to 'on'
$self->{autosave} = 1;
} elsif ($autosave eq "0" or lc($autosave) eq "no" or lc($autosave) eq "off") {
# set to 'off'
$self->{autosave} = 0;
} else {
die "could not recognize autosave mode: $autosave (please use on/off)\n";
}
}
# DESTROY()
#
# destructor
#
# parameter:
# - self
# return:
# none
sub DESTROY {
my $self = shift;
# check autosave mode
if ($self->{autosave} == 1) {
# check if config is saved
if ($self->{changed} == 1) {
# save the config
$self->save_config();
}
}
}
# get_key()
#
# return a config value
#
# parameter:
# - self
# - config key name
# return:
# - value of config parameter (or undef)
sub get_key {
my $self = shift;
my $key = shift;
# return value
return $self->{config}->{$key};
}
# set_key()
#
# set a new config value
#
# parameter:
# - self
# - config key name
# - new value
# return:
# none
sub set_key {
my $self = shift;
my $key = shift;
my $new_value = shift;
# set new value
$self->{config}->{$key} = $new_value;
# mark config changed
$self->{changed} = 1;
}
# delete_key()
#
# delete a config key
#
# parameter:
# - self
# - config key name
# return:
# none
sub delete_key {
my $self = shift;
my $key = shift;
# delete key
delete($self->{config}->{$key});
# mark config changed
$self->{changed} = 1;
}
# get_config_keys()
#
# return hash with all defined config keys
#
# parameter:
# - self
# return:
# - hash with config keys
sub get_config_keys {
my $self = shift;
# return sorted hash
return sort(keys(%{$self->{config}}));
}
# save_config()
#
# write the config to disk
#
# parameter:
# - self
# - (optional) config filename
# return:
# none
# comment:
# - if no filename is given, the original config filename will be used
sub save_config {
my $self = shift;
# get original config filename
my $config_file = $self->{config_file};
if (defined($_[0])) {
# another config filename is given
$config_file = shift;
}
my $fh = new FileHandle;
# open config file for write
open($fh, ">$config_file") || die "could not open config file for write ($config_file): $!\n";
my ($key, $value);
# get all keys
foreach $key ($self->get_config_keys()) {
# get the value for the key
$value = $self->get_key($key);
#print "write config line: $key -> $value\n";
print $fh "$key = $value\n";
}
# flush the filehandle to get stuff written to disk
$fh->flush;
# close filehandle
close($fh);
#mark config unchanged
$self->{changed} = 0;
}
# finish module
1;
|