1 |
|
#!/usr/bin/perl |
2 |
|
|
3 |
|
## ------------------------------------------------------------------------ |
4 |
|
## $Id$ |
5 |
|
## ------------------------------------------------------------------------ |
6 |
|
## $Log$ |
7 |
|
## Revision 1.5 2003/02/18 19:18:50 joko |
8 |
|
## + sub constructor |
9 |
|
## + sub _stat |
10 |
|
## + sub _exists |
11 |
|
## +- rename key |
12 |
|
## |
13 |
|
## Revision 1.4 2003/02/11 05:15:45 joko |
14 |
|
## - no Exporter any more |
15 |
|
## |
16 |
|
## Revision 1.3 2003/02/09 04:54:27 joko |
17 |
|
## - refactored lots of code to Data::Storage::Handler::File::Basic |
18 |
|
## - sorry! |
19 |
|
## |
20 |
|
## ------------------------------------------------------------------------ |
21 |
|
|
22 |
|
|
23 |
package Data::Storage::Handler::File; |
package Data::Storage::Handler::File; |
24 |
|
|
25 |
use strict; |
use strict; |
26 |
use warnings; |
use warnings; |
27 |
|
|
28 |
require Exporter; |
use base qw( DesignPattern::Object ); |
29 |
our @ISA = qw( Exporter ); |
|
30 |
our @EXPORT_OK = qw( |
|
31 |
s2f |
use Data::Storage::Handler::File::Basic qw( s2f a2f f2s ); |
32 |
a2f |
|
33 |
); |
sub constructor { |
34 |
|
my $self = shift; |
35 |
sub s2f { |
# patch - backward compatibility |
36 |
my $filename = shift; |
if (!$self->{filename} && $self->{path}) { |
37 |
my $string = shift; |
$self->{filename} = $self->{path}; |
|
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); |
|
|
} |
|
38 |
} |
} |
39 |
|
} |
40 |
if (ref $rules eq 'ARRAY') { |
|
41 |
foreach my $rule (@{$rules}) { |
sub _stat { |
42 |
my $find = $rule->[0]; |
my $self = shift; |
43 |
my $replace = $rule->[1]; |
if (-e $self->{filename}) { |
44 |
$ris_result &= ($string =~ s/$find/$replace/g); |
$self->{_meta}->{exists} = 1; |
45 |
} |
} else { |
46 |
|
$self->{_meta}->{exists} = 0; |
47 |
} |
} |
|
|
|
|
return $string; |
|
48 |
} |
} |
49 |
|
|
50 |
sub rif { |
sub _read { |
51 |
my $filename = shift; |
my $self = shift; |
52 |
my $rules = shift; |
$self->_stat(); |
53 |
my $out_suffix = shift; |
if ($self->exists()) { |
54 |
|
$self->{_buffer} = f2s($self->{filename}); |
55 |
|
} |
56 |
|
#$self->{_buffer_orig} = f2s($self->{filename}); |
57 |
|
} |
58 |
|
|
59 |
my $outfile = $filename; |
sub toString { |
60 |
$outfile .= '.' . $out_suffix if ($out_suffix); |
my $self = shift; |
61 |
|
$self->_read(); |
62 |
|
return $self->{_buffer}; |
63 |
|
} |
64 |
|
|
65 |
my $buf = f2s($filename); |
sub addSuffix { |
66 |
$buf = ris($buf, $rules); |
my $self = shift; |
67 |
s2f($outfile, $buf); |
my $suffix = shift; |
68 |
|
$self->{filename} .= ".$suffix"; |
69 |
} |
} |
70 |
|
|
71 |
sub findKeyEntries { |
sub save { |
72 |
my $string = shift; |
my $self = shift; |
73 |
my $pattern = shift; |
if ($self->{_buffer}) { |
74 |
my @arr = split("\n", $string); |
s2f($self->{filename}, $self->{_buffer}); |
75 |
my @entries; |
} else { |
76 |
foreach (@arr) { |
print "please load $self->{filename} before saving.", "\n"; |
|
chomp; |
|
|
#print "l: ", $_, "\n"; |
|
|
if (m/$pattern/) { |
|
|
push @entries, $1; |
|
|
} |
|
77 |
} |
} |
|
return \@entries; |
|
78 |
} |
} |
79 |
|
|
80 |
# --------------------------------- |
sub backup { |
81 |
# is a context-entry in a file? |
my $self = shift; |
82 |
# a "context-entry" is an entry identified |
my $options = shift; |
83 |
# by a certain keystring, which itself |
my $path = $self->{filename}; |
84 |
# is detected dynamically |
$path .= '.' . $options->{suffix} if $options->{suffix}; |
85 |
sub isEntryInFile { |
s2f($path, $self->{_buffer_orig}); |
86 |
|
} |
87 |
my $chk = shift; |
|
88 |
my $content_current = f2s($chk->{filename}); |
sub matches { |
89 |
|
my $self = shift; |
90 |
# try to find all key-entries via patterns which are "entry-identifiers" |
my $pattern = shift; |
91 |
if (my @keys = @{ findKeyEntries($chk->{'out'}, $chk->{'pattern'}{'EntryIdent'}) }) { |
$self->_read(); |
92 |
# iterate through all "entry-identifiers" |
my $eval = '$self->{_buffer} =~ ' . $pattern . ';'; |
93 |
foreach (@keys) { |
if (eval($eval)) { return 1; } |
94 |
my $pattern = $chk->{'pattern'}{'EntryCheck'}; |
} |
|
$pattern =~ s/\@\@KEY\@\@/$_/; |
|
|
my $bool_AlreadyThere = ($content_current =~ m/$pattern/); |
|
|
if ($bool_AlreadyThere) { |
|
|
$chk->{'EntryFound'} = $_; |
|
|
return 1; |
|
|
} |
|
|
} |
|
|
} |
|
95 |
|
|
96 |
|
sub exists { |
97 |
|
my $self = shift; |
98 |
|
$self->_stat(); |
99 |
|
return $self->{_meta}->{exists}; |
100 |
} |
} |
101 |
|
|
|
1; |
|
102 |
|
1; |
103 |
|
__END__ |