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