From: Chris W. <la...@us...> - 2005-02-28 00:58:28
|
Update of /cvsroot/openinteract/OpenInteract2/lib/OpenInteract2/Config In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv30473/OpenInteract2/Config Modified Files: Ini.pm Log Message: take care of some warnings; be able to output INI to something other than file Index: Ini.pm =================================================================== RCS file: /cvsroot/openinteract/OpenInteract2/lib/OpenInteract2/Config/Ini.pm,v retrieving revision 1.19 retrieving revision 1.20 diff -C2 -d -r1.19 -r1.20 *** Ini.pm 26 Feb 2005 23:24:35 -0000 1.19 --- Ini.pm 28 Feb 2005 00:58:18 -0000 1.20 *************** *** 19,28 **** # Stuff in metadata (_m): - # sections (\@): all full sections, in the order they were read - # comments (\%): key is full section name, value is comment scalar # filename ($): file read from # directory ($): directory file read from # order (\@): order of sections read/assigned # order_map (\%): to determine if section exists sub new { --- 19,29 ---- # Stuff in metadata (_m): # filename ($): file read from # directory ($): directory file read from + # sections (\@): all full sections, in the order they were read + # comments (\%): key is full section name, value is comment scalar # order (\@): order of sections read/assigned # order_map (\%): to determine if section exists + # global (\@): keys in the 'Global' section sub new { *************** *** 35,38 **** --- 36,41 ---- comments => {}, order => [], + order_map => {}, + global => [], }, $class ); if ( $self->{_m}{filename} = $params->{filename} ) { *************** *** 184,189 **** if ( /^\s*\#/ ) { push @comments, $_; ! $multiline_param = undef; ! $multiline_value = undef; $in_multiline = 0; next; --- 187,192 ---- if ( /^\s*\#/ ) { push @comments, $_; ! $multiline_param = ''; ! $multiline_value = ''; $in_multiline = 0; next; *************** *** 201,213 **** my $this_multiline = $_ =~ s|\\$||; if ( $in_multiline and $this_multiline ) { ! $multiline_value .= "$_\n"; next; } elsif ( $in_multiline ) { $param = $multiline_param; ! $value = $multiline_value . $_; ! $multiline_param = undef; ! $multiline_value = undef; $in_multiline = 0; } --- 204,217 ---- my $this_multiline = $_ =~ s|\\$||; + my $add_value = $_ || ''; if ( $in_multiline and $this_multiline ) { ! $multiline_value .= "$add_value\n"; next; } elsif ( $in_multiline ) { $param = $multiline_param; ! $value = $multiline_value . $add_value; ! $multiline_param = ''; ! $multiline_value = ''; $in_multiline = 0; } *************** *** 230,234 **** if ( $this_multiline ) { $multiline_param = $param; ! $multiline_value = $value; $in_multiline = 1; next; --- 234,238 ---- if ( $this_multiline ) { $multiline_param = $param; ! $multiline_value = $value || ''; $in_multiline = 1; next; *************** *** 239,244 **** my $show_section = ( $sub_section ) ? "$section.$sub_section" : $section; ! $log->debug( "Line $line_number: $show_section.$param ", ! "= '$value'" ); } $self->_read_item( $section, $sub_section, $param, $value ); --- 243,249 ---- my $show_section = ( $sub_section ) ? "$section.$sub_section" : $section; ! my $show_param = $param || ''; ! my $show_value = $value || ''; ! $log->debug( "Line $line_number: $show_section.$show_param = '$show_value'" ); } $self->_read_item( $section, $sub_section, $param, $value ); *************** *** 326,329 **** --- 331,335 ---- sub _set_value { my ( $self, $set_in, $param, $value ) = @_; + return unless ( $param ); my $existing = $set_in->{ $param }; my @values = ( ref $value ) ? @{ $value } : ( $value ); *************** *** 335,339 **** } elsif ( scalar @values > 1 ) { ! $set_in->{ $param } = \@values; } else { --- 341,345 ---- } elsif ( scalar @values > 1 ) { ! $set_in->{ $param } = [ @values ]; } else { *************** *** 346,349 **** --- 352,365 ---- ######################################## + # to STDOUT + sub output { + my ( $self ) = @_; + foreach my $key ( @{ $self->{_m}{global} } ) { + $self->{Global}{ $key } = $self->{ $key }; + } + print $self->_output_header(); + print $self->_output_all_sections(); + } + sub write_file { my ( $self, $filename ) = @_; *************** *** 369,383 **** open( OUT, '>', $filename ) || die "Cannot write configuration to [$filename]: $!"; ! print OUT "# Written by ", ref $self, " at ", scalar localtime, "\n"; ! foreach my $full_section ( $self->sections ) { ! my ( $section, $sub_section ) = split /\s+/, $full_section; ! my $comments = $self->get_comments( $section, $sub_section ); ! if ( $comments ) { ! print OUT "$comments\n"; ! } ! print OUT "[$full_section]\n", ! $self->_output_section( $section, $sub_section ), ! "\n\n"; ! } close( OUT ); if ( $original_filename ) { --- 385,390 ---- open( OUT, '>', $filename ) || die "Cannot write configuration to [$filename]: $!"; ! print OUT $self->_output_header(); ! print OUT $self->_output_all_sections(); close( OUT ); if ( $original_filename ) { *************** *** 389,392 **** --- 396,419 ---- } + sub _output_header { + my ( $self ) = @_; + return "# Written by ", ref $self, " at ", scalar localtime, "\n"; + } + + sub _output_all_sections { + my ( $self ) = @_; + my $out = ''; + foreach my $full_section ( $self->sections ) { + my ( $section, $sub_section ) = split /\s+/, $full_section; + my $comments = $self->get_comments( $section, $sub_section ); + if ( $comments ) { + $out .= "$comments\n"; + } + $out .= join( "\n", "[$full_section]", + $self->_output_section( $section, $sub_section ), + '' ); + } + return $out; + } sub _output_section { *************** *** 413,417 **** ! sub _show_item { return join( ' = ', $_[1], $_[2] ) } 1; --- 440,448 ---- ! sub _show_item { ! my $l = $_[1] || ''; ! my $r = $_[2] || ''; ! return join( ' = ', $l, $r ); ! } 1; |