--- nfo/perl/libs/Data/Storage/Handler/File.pm 2003/01/19 02:13:24 1.2 +++ nfo/perl/libs/Data/Storage/Handler/File.pm 2003/02/09 04:54:27 1.3 @@ -1,129 +1,76 @@ +#!/usr/bin/perl + +## ------------------------------------------------------------------------ +## $Id: File.pm,v 1.3 2003/02/09 04:54:27 joko Exp $ +## ------------------------------------------------------------------------ +## $Log: File.pm,v $ +## Revision 1.3 2003/02/09 04:54:27 joko +## - refactored lots of code to Data::Storage::Handler::File::Basic +## - sorry! +## +## ------------------------------------------------------------------------ + + package Data::Storage::Handler::File; use strict; use warnings; +use base qw( DesignPattern::Object ); + +=pod require Exporter; our @ISA = qw( Exporter ); our @EXPORT_OK = qw( s2f a2f ); +=cut -sub s2f { - my $filename = shift; - my $string = shift; - open(FH, '>' . $filename); - print FH $string; - print FH "\n"; - close(FH); -} - -sub f2s { - my $filename = shift; - # read file at once (be careful with big files!!!) - open(FH, '<' . $filename); - my @buf_arr = ; - my $buf = join("", @buf_arr); - close(FH); - return $buf; -} - -sub a2f { - my $filename = shift; - my $string = shift; - open(FH, '>>' . $filename) or do { - print "Could not append to \"$filename\"!", "\n"; - print "Log-Message was: "; - print $string if $string; - print "\n"; - return; - }; - #print FH "\n"; - print FH $string; - print FH "\n"; - close(FH); - return 1; -} - -sub ris { - my $string = shift; - my $rules = shift; - - our $ris_result = 1; - - if (ref $rules eq 'HASH') { - my @re_find = keys %{$rules}; - # replace all keys with substitutes from hash "%re_table" - foreach my $find (@re_find) { - my $replace = $rules->{$find}; - $ris_result &= ($string =~ s/$find/$replace/g); - } - } - - if (ref $rules eq 'ARRAY') { - foreach my $rule (@{$rules}) { - my $find = $rule->[0]; - my $replace = $rule->[1]; - $ris_result &= ($string =~ s/$find/$replace/g); - } - } - - return $string; -} +use Data::Storage::Handler::File::Basic qw( s2f a2f f2s ); -sub rif { - my $filename = shift; - my $rules = shift; - my $out_suffix = shift; +sub _read { + my $self = shift; + $self->{_buffer_orig} = f2s($self->{path}); + $self->{_buffer} = f2s($self->{path}); +} - my $outfile = $filename; - $outfile .= '.' . $out_suffix if ($out_suffix); +sub toString { + my $self = shift; + $self->_read(); + return $self->{_buffer}; +} - my $buf = f2s($filename); - $buf = ris($buf, $rules); - s2f($outfile, $buf); +sub addSuffix { + my $self = shift; + my $suffix = shift; + $self->{path} .= ".$suffix"; } -sub findKeyEntries { - my $string = shift; - my $pattern = shift; - my @arr = split("\n", $string); - my @entries; - foreach (@arr) { - chomp; - #print "l: ", $_, "\n"; - if (m/$pattern/) { - push @entries, $1; - } +sub save { + my $self = shift; + if ($self->{_buffer}) { + s2f($self->{path}, $self->{_buffer}); + } else { + print "please load $self->{path} before saving.", "\n"; } - return \@entries; } -# --------------------------------- -# is a context-entry in a file? -# a "context-entry" is an entry identified -# by a certain keystring, which itself -# is detected dynamically -sub isEntryInFile { - - my $chk = shift; - my $content_current = f2s($chk->{filename}); - - # try to find all key-entries via patterns which are "entry-identifiers" - if (my @keys = @{ findKeyEntries($chk->{'out'}, $chk->{'pattern'}{'EntryIdent'}) }) { - # iterate through all "entry-identifiers" - foreach (@keys) { - my $pattern = $chk->{'pattern'}{'EntryCheck'}; - $pattern =~ s/\@\@KEY\@\@/$_/; - my $bool_AlreadyThere = ($content_current =~ m/$pattern/); - if ($bool_AlreadyThere) { - $chk->{'EntryFound'} = $_; - return 1; - } - } - } +sub backup { + my $self = shift; + my $options = shift; + my $path = $self->{path}; + $path .= '.' . $options->{suffix} if $options->{suffix}; + s2f($path, $self->{_buffer_orig}); +} +sub matches { + my $self = shift; + my $pattern = shift; + $self->_read(); + my $eval = '$self->{_buffer} =~ ' . $pattern . ';'; + if (eval($eval)) { return 1; } } -1; \ No newline at end of file +1; +__END__