1 |
|
#!/usr/bin/perl |
2 |
|
|
3 |
|
## ------------------------------------------------------------------------ |
4 |
|
## $Id$ |
5 |
|
## ------------------------------------------------------------------------ |
6 |
|
## $Log$ |
7 |
|
## Revision 1.3 2003/02/09 04:54:27 joko |
8 |
|
## - refactored lots of code to Data::Storage::Handler::File::Basic |
9 |
|
## - sorry! |
10 |
|
## |
11 |
|
## ------------------------------------------------------------------------ |
12 |
|
|
13 |
|
|
14 |
package Data::Storage::Handler::File; |
package Data::Storage::Handler::File; |
15 |
|
|
16 |
use strict; |
use strict; |
17 |
use warnings; |
use warnings; |
18 |
|
|
19 |
|
use base qw( DesignPattern::Object ); |
20 |
|
|
21 |
|
=pod |
22 |
require Exporter; |
require Exporter; |
23 |
our @ISA = qw( Exporter ); |
our @ISA = qw( Exporter ); |
24 |
our @EXPORT_OK = qw( |
our @EXPORT_OK = qw( |
25 |
s2f |
s2f |
26 |
a2f |
a2f |
27 |
); |
); |
28 |
|
=cut |
29 |
|
|
30 |
sub s2f { |
use Data::Storage::Handler::File::Basic qw( s2f a2f f2s ); |
|
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 = <FH>; |
|
|
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; |
|
|
} |
|
31 |
|
|
32 |
sub rif { |
sub _read { |
33 |
my $filename = shift; |
my $self = shift; |
34 |
my $rules = shift; |
$self->{_buffer_orig} = f2s($self->{path}); |
35 |
my $out_suffix = shift; |
$self->{_buffer} = f2s($self->{path}); |
36 |
|
} |
37 |
|
|
38 |
my $outfile = $filename; |
sub toString { |
39 |
$outfile .= '.' . $out_suffix if ($out_suffix); |
my $self = shift; |
40 |
|
$self->_read(); |
41 |
|
return $self->{_buffer}; |
42 |
|
} |
43 |
|
|
44 |
my $buf = f2s($filename); |
sub addSuffix { |
45 |
$buf = ris($buf, $rules); |
my $self = shift; |
46 |
s2f($outfile, $buf); |
my $suffix = shift; |
47 |
|
$self->{path} .= ".$suffix"; |
48 |
} |
} |
49 |
|
|
50 |
sub findKeyEntries { |
sub save { |
51 |
my $string = shift; |
my $self = shift; |
52 |
my $pattern = shift; |
if ($self->{_buffer}) { |
53 |
my @arr = split("\n", $string); |
s2f($self->{path}, $self->{_buffer}); |
54 |
my @entries; |
} else { |
55 |
foreach (@arr) { |
print "please load $self->{path} before saving.", "\n"; |
|
chomp; |
|
|
#print "l: ", $_, "\n"; |
|
|
if (m/$pattern/) { |
|
|
push @entries, $1; |
|
|
} |
|
56 |
} |
} |
|
return \@entries; |
|
57 |
} |
} |
58 |
|
|
59 |
# --------------------------------- |
sub backup { |
60 |
# is a context-entry in a file? |
my $self = shift; |
61 |
# a "context-entry" is an entry identified |
my $options = shift; |
62 |
# by a certain keystring, which itself |
my $path = $self->{path}; |
63 |
# is detected dynamically |
$path .= '.' . $options->{suffix} if $options->{suffix}; |
64 |
sub isEntryInFile { |
s2f($path, $self->{_buffer_orig}); |
65 |
|
} |
|
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; |
|
|
} |
|
|
} |
|
|
} |
|
66 |
|
|
67 |
|
sub matches { |
68 |
|
my $self = shift; |
69 |
|
my $pattern = shift; |
70 |
|
$self->_read(); |
71 |
|
my $eval = '$self->{_buffer} =~ ' . $pattern . ';'; |
72 |
|
if (eval($eval)) { return 1; } |
73 |
} |
} |
74 |
|
|
|
1; |
|
75 |
|
1; |
76 |
|
__END__ |