| 1 | # -*- perl -*- | ||
| 2 | |||
| 3 | # | ||
| 4 | # Author: Slaven Rezic | ||
| 5 | # | ||
| 6 | # Copyright (C) 2022 Slaven Rezic. All rights reserved. | ||
| 7 | # This package is free software; you can redistribute it and/or | ||
| 8 | # modify it under the same terms as Perl itself. | ||
| 9 | # | ||
| 10 | # Mail: srezic@cpan.org | ||
| 11 | # WWW: https://github.com/eserte/Doit | ||
| 12 | # | ||
| 13 | |||
| 14 | package Doit::Ini; | ||
| 15 | |||
| 16 | use strict; | ||
| 17 | use warnings; | ||
| 18 | our $VERSION = '0.04'; | ||
| 19 | |||
| 20 | use File::Temp (); | ||
| 21 | |||
| 22 | use Doit::Log; | ||
| 23 | |||
| 24 | my @try_implementations = qw(Config::IOD::INI Config::IniFiles Config::IniMan); | ||
| 25 | my %allowed_implementation = map { ($_,1) } @try_implementations; | ||
| 26 | |||
| 27 | sub new { bless {}, shift } | ||
| 28 | sub functions { qw(ini_change ini_set_implementation ini_adapter_class) } | ||
| 29 | |||
| 30 | sub ini_set_implementation { | ||
| 31 | my(undef, @new_implementations) = @_; | ||
| 32 | for my $new_implementation (@new_implementations) { | ||
| 33 | if (!$allowed_implementation{$new_implementation}) { | ||
| 34 | error "The implementation '$new_implementation' is unknown"; | ||
| 35 | } | ||
| 36 | } | ||
| 37 | @try_implementations = @new_implementations; | ||
| 38 | } | ||
| 39 | |||
| 40 | sub ini_adapter_class { | ||
| 41 | my(undef) = @_; | ||
| 42 | for my $impl (@try_implementations) { | ||
| 43 | my $adapter_class = "Doit::Ini::$impl"; | ||
| 44 | if ($adapter_class->available) { | ||
| 45 | return $adapter_class; | ||
| 46 | } | ||
| 47 | } | ||
| 48 | undef; | ||
| 49 | } | ||
| 50 | |||
| 51 | sub ini_change { | ||
| 52 | my($doit, $filename, @changes) = @_; | ||
| 53 | return 0 if !@changes; | ||
| 54 | |||
| 55 | my $code; | ||
| 56 | if (ref $changes[0] eq 'CODE') { | ||
| 57 | if (@changes > 1) { | ||
| 58 | error "Too many arguments, only one code reference is allowed"; | ||
| 59 | } | ||
| 60 | $code = $changes[0]; | ||
| 61 | @changes = (); | ||
| 62 | } else { | ||
| 63 | $code = sub { | ||
| 64 | my($self, @changes) = @_; | ||
| 65 | while(@changes) { | ||
| 66 | my($section_key, $val) = (shift(@changes), shift(@changes)); | ||
| 67 | my($section, $key) = split /\./, $section_key; | ||
| 68 | $self->set_value($section, $key, $val); | ||
| 69 | } | ||
| 70 | }; | ||
| 71 | } | ||
| 72 | |||
| 73 | my $use_adapter_class = $doit->ini_adapter_class; | ||
| 74 | if (!$use_adapter_class) { | ||
| 75 | error "No usable ini implementation found, tried: @try_implementations"; | ||
| 76 | } | ||
| 77 | |||
| 78 | my $o = $use_adapter_class->new; | ||
| 79 | $o->read_file($filename); | ||
| 80 | $code->($o, @changes); | ||
| 81 | my $new_ini = $o->dump_ini; | ||
| 82 | return $doit->write_binary($filename, $new_ini); | ||
| 83 | } | ||
| 84 | |||
| 85 | { | ||
| 86 | package | ||
| 87 | Doit::Ini::ConfigBase; | ||
| 88 | sub confobj { shift->{c} } | ||
| 89 | } | ||
| 90 | |||
| 91 | { | ||
| 92 | package | ||
| 93 | Doit::Ini::Config::IOD::INI; | ||
| 94 | use base 'Doit::Ini::ConfigBase'; | ||
| 95 | sub available { eval { require Config::IOD::INI; 1 } } | ||
| 96 | sub new { bless { o => Config::IOD->new }, shift } | ||
| 97 | sub read_file { | ||
| 98 | my($self, $filename) = @_; | ||
| 99 | $self->{c} = $self->{o}->read_file($filename); | ||
| 100 | } | ||
| 101 | sub set_value { | ||
| 102 | my($self, $section, $key, $val) = @_; | ||
| 103 | $self->confobj->set_value($section, $key, $val); | ||
| 104 | } | ||
| 105 | sub dump_ini { | ||
| 106 | my($self) = @_; | ||
| 107 | $self->confobj->as_string; | ||
| 108 | } | ||
| 109 | } | ||
| 110 | |||
| 111 | { | ||
| 112 | package | ||
| 113 | Doit::Ini::Config::IniFiles; | ||
| 114 | use base 'Doit::Ini::ConfigBase'; | ||
| 115 | sub available { eval { require Config::IniFiles; 1 } } | ||
| 116 | sub new { bless { }, shift } | ||
| 117 | sub read_file { | ||
| 118 | my($self, $filename) = @_; | ||
| 119 | $self->{c} = $self->{o} = Config::IniFiles->new(-file => $filename); | ||
| 120 | $self->{o}->ReadConfig(-file => $filename); | ||
| 121 | } | ||
| 122 | sub set_value { | ||
| 123 | my($self, $section, $key, $val) = @_; | ||
| 124 | $self->confobj->setval($section, $key, $val); | ||
| 125 | } | ||
| 126 | sub dump_ini { | ||
| 127 | my($self) = @_; | ||
| 128 | my $ini; | ||
| 129 | open my $fh, '>', \$ini or die $!; | ||
| 130 | binmode $fh; | ||
| 131 | $self->{o}->WriteConfig($fh); | ||
| 132 | $ini; | ||
| 133 | } | ||
| 134 | } | ||
| 135 | |||
| 136 | { | ||
| 137 | package | ||
| 138 | Doit::Ini::Config::IniMan; | ||
| 139 | use base 'Doit::Ini::ConfigBase'; | ||
| 140 | sub available { eval { die "NYI"; require Config::IniMan; 1 } } | ||
| 141 | } | ||
| 142 | |||
| 143 | 1; | ||
| 144 | |||
| 145 | __END__ |