5
# This program is free software; you can redistribute it and/or modify
6
# it under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; version 2 of the License.
9
# This program is distributed in the hope that it will be useful,
10
# WITHOUT ANY WARRANTY; without even the implied warranty of
11
# or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, write to the Free Software
16
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
18
package My::Config::Option;
25
my ($class, $option_name, $option_value)= @_;
26
my $self= bless { name => $option_name,
27
value => $option_value
41
return $self->{value};
45
package My::Config::Group;
52
my ($class, $group_name)= @_;
53
my $self= bless { name => $group_name,
55
options_by_name => {},
62
my ($self, $option_name, $value, $if_not_exist)= @_;
63
my $option= $self->option($option_name);
64
if (defined($option) and !$if_not_exist) {
65
$option->{value}= $value;
68
my $option= My::Config::Option->new($option_name, $value);
69
# Insert option in list
70
push(@{$self->{options}}, $option);
71
# Insert option in hash
72
$self->{options_by_name}->{$option_name}= $option;
78
my ($self, $option_name)= @_;
80
# Check that option exists
81
my $option= $self->option($option_name);
83
return undef unless defined $option;
85
# Remove from the hash
86
delete($self->{options_by_name}->{$option_name}) or die;
88
# Remove from the array
89
@{$self->{options}}= grep { $_->name ne $option_name } @{$self->{options}};
97
return @{$self->{options}};
103
return $self->{name};
108
# Return a specific option in the group
111
my ($self, $option_name)= @_;
113
return $self->{options_by_name}->{$option_name};
118
# Return a specific value for an option in the group
121
my ($self, $option_name)= @_;
122
my $option= $self->option($option_name);
124
die "No option named '$option_name' in this group"
125
if ! defined($option);
127
return $option->value();
139
# Constructor for My::Config
140
# - represents a my.cnf config file
145
my ($class, $path)= @_;
146
my $group_name= undef;
148
my $self= bless { groups => [] }, $class;
149
my $F= IO::File->new($path, "<")
150
or die "Could not open '$path': $!";
152
while ( my $line= <$F> ) {
156
if ( $line =~ /\[(.*)\]/ ) {
159
#print "group: $group_name\n";
161
$self->insert($group_name, undef, undef);
165
elsif ( $line =~ /^#\!/) {
167
die "Found magic comment '$magic' outside of group"
171
$self->insert($group_name, $magic, undef);
175
elsif ( $line =~ /^#/ || $line =~ /^;/) {
181
elsif ( $line =~ /^$/ ) {
186
# !include <filename>
187
elsif ( $line =~ /^\!include\s*(.*?)\s*$/ ) {
188
my $include_file_name= dirname($path)."/".$1;
189
# Check that the file exists
190
die "The include file '$include_file_name' does not exist"
191
unless -f $include_file_name;
193
$self->append(My::Config->new($include_file_name));
197
elsif ( $line =~ /^([\@\w-]+)\s*$/ ) {
200
die "Found option '$option' outside of group"
204
$self->insert($group_name, $option, undef);
208
elsif ( $line =~ /^([\@\w-]+)\s*=\s*(.*?)\s*$/ ) {
212
die "Found option '$option=$value' outside of group"
215
#print "$option=$value\n";
216
$self->insert($group_name, $option, $value);
218
die "Unexpected line '$line' found in '$path'";
222
undef $F; # Close the file
228
# Insert a new group if it does not already exist
229
# and add option if defined
232
my ($self, $group_name, $option, $value, $if_not_exist)= @_;
235
# Create empty array for the group if it doesn't exist
236
if ( !$self->group_exists($group_name) ) {
237
$group= $self->_group_insert($group_name);
240
$group= $self->group($group_name);
243
if ( defined $option ) {
244
#print "option: $option, value: $value\n";
246
# Add the option to the group
247
$group->insert($option, $value, $if_not_exist);
252
# Remove a option, given group and option name
255
my ($self, $group_name, $option_name)= @_;
256
my $group= $self->group($group_name);
258
die "group '$group_name' does not exist"
259
unless defined($group);
261
$group->remove($option_name) or
262
die "option '$option_name' does not exist";
268
# Check if group with given name exists in config
271
my ($self, $group_name)= @_;
273
foreach my $group ($self->groups()) {
274
return 1 if $group->{name} eq $group_name;
281
# Insert a new group into config
284
my ($self, $group_name)= @_;
285
caller eq __PACKAGE__ or die;
287
# Check that group does not already exist
288
die "Group already exists" if $self->group_exists($group_name);
290
my $group= My::Config::Group->new($group_name);
291
push(@{$self->{groups}}, $group);
297
# Append a configuration to current config
300
my ($self, $from)= @_;
302
foreach my $group ($from->groups()) {
303
foreach my $option ($group->options()) {
304
$self->insert($group->name(), $option->name(), $option->value());
312
# Return a list with all the groups in config
316
return ( @{$self->{groups}} );
321
# Return a list of all the groups in config
322
# starting with the given string
325
my ($self, $prefix)= @_;
326
return ( grep ( $_->{name} =~ /^$prefix/, $self->groups()) );
331
# Return the first group in config
332
# starting with the given string
335
my ($self, $prefix)= @_;
336
return ($self->like($prefix))[0];
341
# Return a specific group in the config
344
my ($self, $group_name)= @_;
346
foreach my $group ( $self->groups() ) {
347
return $group if $group->{name} eq $group_name;
354
# Return a list of all options in a specific group in the config
356
sub options_in_group {
357
my ($self, $group_name)= @_;
359
my $group= $self->group($group_name);
360
return () unless defined $group;
361
return $group->options();
366
# Return a value given group and option name
369
my ($self, $group_name, $option_name)= @_;
370
my $group= $self->group($group_name);
372
die "group '$group_name' does not exist"
373
unless defined($group);
375
my $option= $group->option($option_name);
376
die "option '$option_name' does not exist"
377
unless defined($option);
379
return $option->value();
384
# Check if an option exists
387
my ($self, $group_name, $option_name)= @_;
388
my $group= $self->group($group_name);
390
die "group '$group_name' does not exist"
391
unless defined($group);
393
my $option= $group->option($option_name);
394
return defined($option);
398
# Overload "to string"-operator with 'stringify'
403
# Return the config as a string in my.cnf file format
409
foreach my $group ($self->groups()) {
410
$res .= "[$group->{name}]\n";
412
foreach my $option ($group->options()) {
413
$res .= $option->name();
414
my $value= $option->value();
415
if (defined $value) {
427
# Save the config to named file
430
my ($self, $path)= @_;
431
my $F= IO::File->new($path, ">")
432
or die "Could not open '$path': $!";
434
undef $F; # Close the file