3
package My::Config::Option;
10
my ($class, $option_name, $option_value)= @_;
11
my $self= bless { name => $option_name,
12
value => $option_value
26
return $self->{value};
30
package My::Config::Group;
37
my ($class, $group_name)= @_;
38
my $self= bless { name => $group_name,
40
options_by_name => {},
47
my ($self, $option_name, $value, $if_not_exist)= @_;
48
my $option= $self->option($option_name);
49
if (defined($option) and !$if_not_exist) {
50
$option->{value}= $value;
53
my $option= My::Config::Option->new($option_name, $value);
54
# Insert option in list
55
push(@{$self->{options}}, $option);
56
# Insert option in hash
57
$self->{options_by_name}->{$option_name}= $option;
63
my ($self, $option_name)= @_;
65
# Check that option exists
66
my $option= $self->option($option_name);
68
return undef unless defined $option;
70
# Remove from the hash
71
delete($self->{options_by_name}->{$option_name}) or die;
73
# Remove from the array
74
@{$self->{options}}= grep { $_->name ne $option_name } @{$self->{options}};
82
return @{$self->{options}};
93
# Return a specific option in the group
96
my ($self, $option_name)= @_;
98
return $self->{options_by_name}->{$option_name};
103
# Return a specific value for an option in the group
106
my ($self, $option_name)= @_;
107
my $option= $self->option($option_name);
109
die "No option named '$option_name' in this group"
110
if ! defined($option);
112
return $option->value();
124
# Constructor for My::Config
125
# - represents a my.cnf config file
130
my ($class, $path)= @_;
131
my $group_name= undef;
133
my $self= bless { groups => [] }, $class;
134
my $F= IO::File->new($path, "<")
135
or die "Could not open '$path': $!";
137
while ( my $line= <$F> ) {
141
if ( $line =~ /\[(.*)\]/ ) {
144
#print "group: $group_name\n";
146
$self->insert($group_name, undef, undef);
150
elsif ( $line =~ /^#\!/) {
152
die "Found magic comment '$magic' outside of group"
156
$self->insert($group_name, $magic, undef);
160
elsif ( $line =~ /^#/ || $line =~ /^;/) {
166
elsif ( $line =~ /^$/ ) {
171
# !include <filename>
172
elsif ( $line =~ /^\!include\s*(.*?)\s*$/ ) {
173
my $include_file_name= dirname($path)."/".$1;
174
# Check that the file exists
175
die "The include file '$include_file_name' does not exist"
176
unless -f $include_file_name;
178
$self->append(My::Config->new($include_file_name));
182
elsif ( $line =~ /^([\@\w-]+)\s*$/ ) {
185
die "Found option '$option' outside of group"
189
$self->insert($group_name, $option, undef);
193
elsif ( $line =~ /^([\@\w-]+)\s*=\s*(.*?)\s*$/ ) {
197
die "Found option '$option=$value' outside of group"
200
#print "$option=$value\n";
201
$self->insert($group_name, $option, $value);
203
die "Unexpected line '$line' found in '$path'";
207
undef $F; # Close the file
213
# Insert a new group if it does not already exist
214
# and add option if defined
217
my ($self, $group_name, $option, $value, $if_not_exist)= @_;
220
# Create empty array for the group if it doesn't exist
221
if ( !$self->group_exists($group_name) ) {
222
$group= $self->_group_insert($group_name);
225
$group= $self->group($group_name);
228
if ( defined $option ) {
229
#print "option: $option, value: $value\n";
231
# Add the option to the group
232
$group->insert($option, $value, $if_not_exist);
237
# Remove a option, given group and option name
240
my ($self, $group_name, $option_name)= @_;
241
my $group= $self->group($group_name);
243
die "group '$group_name' does not exist"
244
unless defined($group);
246
$group->remove($option_name) or
247
die "option '$option_name' does not exist";
253
# Check if group with given name exists in config
256
my ($self, $group_name)= @_;
258
foreach my $group ($self->groups()) {
259
return 1 if $group->{name} eq $group_name;
266
# Insert a new group into config
269
my ($self, $group_name)= @_;
270
caller eq __PACKAGE__ or die;
272
# Check that group does not already exist
273
die "Group already exists" if $self->group_exists($group_name);
275
my $group= My::Config::Group->new($group_name);
276
push(@{$self->{groups}}, $group);
282
# Append a configuration to current config
285
my ($self, $from)= @_;
287
foreach my $group ($from->groups()) {
288
foreach my $option ($group->options()) {
289
$self->insert($group->name(), $option->name(), $option->value());
297
# Return a list with all the groups in config
301
return ( @{$self->{groups}} );
306
# Return a list of all the groups in config
307
# starting with the given string
310
my ($self, $prefix)= @_;
311
return ( grep ( $_->{name} =~ /^$prefix/, $self->groups()) );
316
# Return the first group in config
317
# starting with the given string
320
my ($self, $prefix)= @_;
321
return ($self->like($prefix))[0];
326
# Return a specific group in the config
329
my ($self, $group_name)= @_;
331
foreach my $group ( $self->groups() ) {
332
return $group if $group->{name} eq $group_name;
339
# Return a list of all options in a specific group in the config
341
sub options_in_group {
342
my ($self, $group_name)= @_;
344
my $group= $self->group($group_name);
345
return () unless defined $group;
346
return $group->options();
351
# Return a value given group and option name
354
my ($self, $group_name, $option_name)= @_;
355
my $group= $self->group($group_name);
357
die "group '$group_name' does not exist"
358
unless defined($group);
360
my $option= $group->option($option_name);
361
die "option '$option_name' does not exist"
362
unless defined($option);
364
return $option->value();
369
# Check if an option exists
372
my ($self, $group_name, $option_name)= @_;
373
my $group= $self->group($group_name);
375
die "group '$group_name' does not exist"
376
unless defined($group);
378
my $option= $group->option($option_name);
379
return defined($option);
383
# Overload "to string"-operator with 'stringify'
388
# Return the config as a string in my.cnf file format
394
foreach my $group ($self->groups()) {
395
$res .= "[$group->{name}]\n";
397
foreach my $option ($group->options()) {
398
$res .= $option->name();
399
my $value= $option->value();
400
if (defined $value) {
412
# Save the config to named file
415
my ($self, $path)= @_;
416
my $F= IO::File->new($path, ">")
417
or die "Could not open '$path': $!";
419
undef $F; # Close the file