| 1 | package OpenInteract::Config::Ini; | ||
| 2 | |||
| 3 | # $Id: Ini.pm,v 1.91 2003/01/24 13:03:47 lachoy Exp $ | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use OpenInteract::Config qw( _w DEBUG ); | ||
| 7 | |||
| 8 | $OpenInteract::Config::Ini::VERSION = sprintf("%d.%02d", q$Revision: 1.91 $ =~ /(\d+)\.(\d+)/); | ||
| 9 | |||
| 10 | # Stuff in metadata (_m): | ||
| 11 | # sections (\@): all full sections, in the order they were read | ||
| 12 | # comments (\%): key is full section name, value is comment scalar | ||
| 13 | # filename ($): file read from | ||
| 14 | |||
| 15 | sub new { | ||
| 16 | my ( $pkg, $params ) = @_; | ||
| 17 | my $class = ref $pkg || $pkg; | ||
| 18 | my $self = bless( {}, $class ); | ||
| 19 | if ( $self->{_m}{filename} = $params->{filename} ) { | ||
| 20 | $self->read_file( $self->{_m}{filename} ); | ||
| 21 | } | ||
| 22 | return $self; | ||
| 23 | } | ||
| 24 | |||
| 25 | |||
| 26 | sub get { | ||
| 27 | my ( $self, $section, @p ) = @_; | ||
| 28 | my ( $sub_section, $param ) = ( $p[1] ) ? ( $p[0], $p[1] ) : ( undef, $p[0] ); | ||
| 29 | my $item = ( $sub_section ) | ||
| 30 | ? $self->{ $section }{ $sub_section }{ $param } | ||
| 31 | : $self->{ $section }{ $param }; | ||
| 32 | return $item unless ( ref $item eq 'ARRAY' ); | ||
| 33 | return wantarray ? @{ $item } : $item->[0]; | ||
| 34 | } | ||
| 35 | |||
| 36 | |||
| 37 | sub set { | ||
| 38 | my ( $self, $section, @p ) = @_; | ||
| 39 | my ( $sub_section, $param, $value ) = ( $p[2] ) ? ( $p[0], $p[1], $p[2] ) : ( undef, $p[0], $p[1] ); | ||
| 40 | return $self->{ $section }{ $sub_section }{ $param } = $value if ( $sub_section ); | ||
| 41 | return $self->{ $section }{ $param } = $value | ||
| 42 | } | ||
| 43 | |||
| 44 | |||
| 45 | sub delete { | ||
| 46 | my ( $self, $section, @p ) = @_; | ||
| 47 | my ( $sub_section, $param ) = ( $p[1] ) ? ( $p[0], $p[1] ) : ( undef, $p[0] ); | ||
| 48 | delete $self->{ $section }{ $sub_section }{ $param } if ( $sub_section ); | ||
| 49 | delete $self->{ $section }{ $param }; | ||
| 50 | } | ||
| 51 | |||
| 52 | |||
| 53 | sub sections { | ||
| 54 | my ( $self ) = @_; | ||
| 55 | return @{ $self->{_m}{order} }; | ||
| 56 | } | ||
| 57 | |||
| 58 | ######################################## | ||
| 59 | # INPUT | ||
| 60 | ######################################## | ||
| 61 | |||
| 62 | sub read_file { | ||
| 63 | my ( $self, $filename ) = @_; | ||
| 64 | |||
| 65 | DEBUG && _w( 1, "Trying to read INI file ($filename)" ); | ||
| 66 | |||
| 67 | open( CONF, $filename ) || die "Cannot open ($filename) for reading: $!"; | ||
| 68 | |||
| 69 | # Temporary holding for comments | ||
| 70 | |||
| 71 | my @comments = (); | ||
| 72 | my ( $section, $sub_section ); | ||
| 73 | |||
| 74 | # Cycle through the file: skip blanks; accumulate comments for | ||
| 75 | # each section; register section/subsection; add parameter/value | ||
| 76 | |||
| 77 | while ( <CONF> ) { | ||
| 78 | chomp; | ||
| 79 | next if ( /^\s*$/ ); | ||
| 80 | if ( /^# Written by OpenInteract::Config::Ini at/ ) { | ||
| 81 | my $dispose = <CONF>; # get rid of next blank line | ||
| 82 | next; # ... and the current line | ||
| 83 | } | ||
| 84 | s/\s+$//; | ||
| 85 | if ( /^\s*\#/ ) { | ||
| 86 | push @comments, $_; | ||
| 87 | next; | ||
| 88 | } | ||
| 89 | if ( /^\s*\[\s*(\S|\S.*\S)\s*\]\s*$/) { | ||
| 90 | DEBUG && _w( 2, "Found section ($1)" ); | ||
| 91 | ( $section, $sub_section ) = $self->read_section_head( $1, \@comments ); | ||
| 92 | @comments = (); | ||
| 93 | next; | ||
| 94 | } | ||
| 95 | my ( $param, $value ) = /^\s*([^=]+?)\s*=\s*(.*)\s*$/; | ||
| 96 | DEBUG && _w( 2, "Setting ($param) to ($value)" ); | ||
| 97 | $self->read_item( $section, $sub_section, $param, $value ); | ||
| 98 | } | ||
| 99 | close( CONF ); | ||
| 100 | $self->{_m}{filename} = $filename; | ||
| 101 | return $self; | ||
| 102 | } | ||
| 103 | |||
| 104 | |||
| 105 | sub read_section_head { | ||
| 106 | my ( $self, $full_section, $comments ) = @_; | ||
| 107 | push @{ $self->{_m}{order} }, $full_section; | ||
| 108 | $self->{_m}{comments}{ $full_section } = join "\n", @{ $comments }; | ||
| 109 | if ( $full_section =~ /^(\w+)\s+(\w+)$/ ) { | ||
| 110 | my ( $section, $sub_section ) = ( $1, $2 ); | ||
| 111 | $self->{ $section }{ $sub_section } ||= {}; | ||
| 112 | return ( $section, $sub_section ); | ||
| 113 | } | ||
| 114 | $self->{ $full_section } ||= {}; | ||
| 115 | return ( $full_section, undef ); | ||
| 116 | } | ||
| 117 | |||
| 118 | |||
| 119 | sub read_item { | ||
| 120 | my ( $self, $section, $sub_section, $param, $value ) = @_; | ||
| 121 | |||
| 122 | # Special case -- 'Global' stuff goes in the config object root | ||
| 123 | |||
| 124 | if ( $section eq 'Global' ) { | ||
| 125 | push @{ $self->{_m}{global} }, $param; | ||
| 126 | $self->set_value( $self, $param, $value ); | ||
| 127 | return; | ||
| 128 | } | ||
| 129 | |||
| 130 | return ( $sub_section ) | ||
| 131 | ? $self->set_value( $self->{ $section }{ $sub_section }, $param, $value ) | ||
| 132 | : $self->set_value( $self->{ $section }, $param, $value ); | ||
| 133 | } | ||
| 134 | |||
| 135 | |||
| 136 | sub set_value { | ||
| 137 | my ( $self, $set_in, $param, $value ) = @_; | ||
| 138 | my $existing = $set_in->{ $param }; | ||
| 139 | if ( $existing and ref $set_in->{ $param } eq 'ARRAY' ) { | ||
| 140 | push @{ $set_in->{ $param } }, $value; | ||
| 141 | } | ||
| 142 | elsif ( $existing ) { | ||
| 143 | $set_in->{ $param } = [ $existing, $value ]; | ||
| 144 | } | ||
| 145 | else { | ||
| 146 | $set_in->{ $param } = $value; | ||
| 147 | } | ||
| 148 | } | ||
| 149 | |||
| 150 | ######################################## | ||
| 151 | # OUTPUT | ||
| 152 | ######################################## | ||
| 153 | |||
| 154 | sub write_file { | ||
| 155 | my ( $self, $filename ) = @_; | ||
| 156 | $filename ||= $self->{_m}{filename} || 'config.ini'; | ||
| 157 | my ( $original_filename ); | ||
| 158 | if ( -f $filename ) { | ||
| 159 | $original_filename = $filename; | ||
| 160 | $filename = "$filename.new"; | ||
| 161 | } | ||
| 162 | |||
| 163 | # Set 'Global' items from the config object root | ||
| 164 | |||
| 165 | foreach my $key ( @{ $self->{_m}{global} } ) { | ||
| 166 | $self->{Global}{ $key } = $self->{ $key }; | ||
| 167 | } | ||
| 168 | |||
| 169 | DEBUG && _w( 1, "--Writing INI to ($filename) (original: $original_filename)" ); | ||
| 170 | open( OUT, "> $filename" ) || die "Cannot write configuration to ($filename): $!"; | ||
| 171 | print OUT "# Written by ", ref $self, " at ", scalar localtime, "\n\n"; | ||
| 172 | foreach my $full_section ( @{ $self->{_m}{order} } ) { | ||
| 173 | if ( $self->{_m}{comments}{ $full_section } ) { | ||
| 174 | print OUT $self->{_m}{comments}{ $full_section }, "\n"; | ||
| 175 | } | ||
| 176 | print OUT "[$full_section]\n", | ||
| 177 | $self->output_section( $full_section ), | ||
| 178 | "\n\n"; | ||
| 179 | } | ||
| 180 | close( OUT ); | ||
| 181 | if ( $original_filename ) { | ||
| 182 | unlink( $original_filename ); | ||
| 183 | rename( $filename, $original_filename ); | ||
| 184 | } | ||
| 185 | return $filename; | ||
| 186 | } | ||
| 187 | |||
| 188 | |||
| 189 | sub output_section { | ||
| 190 | my ( $self, $full_section ) = @_; | ||
| 191 | my ( $section, $sub_section ) = split /\s+/, $full_section; | ||
| 192 | my $show_from = ( $sub_section ) | ||
| 193 | ? $self->{ $section }{ $sub_section } | ||
| 194 | : $self->{ $section }; | ||
| 195 | my @items = (); | ||
| 196 | foreach my $key ( keys %{ $show_from } ) { | ||
| 197 | if ( ref $show_from->{ $key } eq 'ARRAY' ) { | ||
| 198 | foreach my $value ( @{ $show_from->{ $key } } ) { | ||
| 199 | push @items, $self->show_item( $key, $value ); | ||
| 200 | } | ||
| 201 | } | ||
| 202 | else { | ||
| 203 | push @items, $self->show_item( $key, $show_from->{ $key } ); | ||
| 204 | } | ||
| 205 | } | ||
| 206 | return join "\n", @items; | ||
| 207 | } | ||
| 208 | |||
| 209 | |||
| 210 | sub show_item { return join( ' = ', $_[1], $_[2] ) } | ||
| 211 | |||
| 212 | 1; | ||
| 213 | |||
| 214 | __END__ |