1 |
package Data::Storage::Handler::File; |
2 |
|
3 |
use strict; |
4 |
use warnings; |
5 |
|
6 |
require Exporter; |
7 |
our @ISA = qw( Exporter ); |
8 |
our @EXPORT_OK = qw( |
9 |
s2f |
10 |
a2f |
11 |
); |
12 |
|
13 |
sub s2f { |
14 |
my $filename = shift; |
15 |
my $string = shift; |
16 |
open(FH, '>' . $filename); |
17 |
print FH $string; |
18 |
print FH "\n"; |
19 |
close(FH); |
20 |
} |
21 |
|
22 |
sub f2s { |
23 |
my $filename = shift; |
24 |
# read file at once (be careful with big files!!!) |
25 |
open(FH, '<' . $filename); |
26 |
my @buf_arr = <FH>; |
27 |
my $buf = join("", @buf_arr); |
28 |
close(FH); |
29 |
return $buf; |
30 |
} |
31 |
|
32 |
sub a2f { |
33 |
my $filename = shift; |
34 |
my $string = shift; |
35 |
open(FH, '>>' . $filename); |
36 |
#print FH "\n"; |
37 |
print FH $string; |
38 |
print FH "\n"; |
39 |
close(FH); |
40 |
} |
41 |
|
42 |
sub ris { |
43 |
my $string = shift; |
44 |
my $rules = shift; |
45 |
|
46 |
our $ris_result = 1; |
47 |
|
48 |
if (ref $rules eq 'HASH') { |
49 |
my @re_find = keys %{$rules}; |
50 |
# replace all keys with substitutes from hash "%re_table" |
51 |
foreach my $find (@re_find) { |
52 |
my $replace = $rules->{$find}; |
53 |
$ris_result &= ($string =~ s/$find/$replace/g); |
54 |
} |
55 |
} |
56 |
|
57 |
if (ref $rules eq 'ARRAY') { |
58 |
foreach my $rule (@{$rules}) { |
59 |
my $find = $rule->[0]; |
60 |
my $replace = $rule->[1]; |
61 |
$ris_result &= ($string =~ s/$find/$replace/g); |
62 |
} |
63 |
} |
64 |
|
65 |
return $string; |
66 |
} |
67 |
|
68 |
sub rif { |
69 |
my $filename = shift; |
70 |
my $rules = shift; |
71 |
my $out_suffix = shift; |
72 |
|
73 |
my $outfile = $filename; |
74 |
$outfile .= '.' . $out_suffix if ($out_suffix); |
75 |
|
76 |
my $buf = f2s($filename); |
77 |
$buf = ris($buf, $rules); |
78 |
s2f($outfile, $buf); |
79 |
} |
80 |
|
81 |
sub findKeyEntries { |
82 |
my $string = shift; |
83 |
my $pattern = shift; |
84 |
my @arr = split("\n", $string); |
85 |
my @entries; |
86 |
foreach (@arr) { |
87 |
chomp; |
88 |
#print "l: ", $_, "\n"; |
89 |
if (m/$pattern/) { |
90 |
push @entries, $1; |
91 |
} |
92 |
} |
93 |
return \@entries; |
94 |
} |
95 |
|
96 |
# --------------------------------- |
97 |
# is a context-entry in a file? |
98 |
# a "context-entry" is an entry identified |
99 |
# by a certain keystring, which itself |
100 |
# is detected dynamically |
101 |
sub isEntryInFile { |
102 |
|
103 |
my $chk = shift; |
104 |
my $content_current = f2s($chk->{filename}); |
105 |
|
106 |
# try to find all key-entries via patterns which are "entry-identifiers" |
107 |
if (my @keys = @{ findKeyEntries($chk->{'out'}, $chk->{'pattern'}{'EntryIdent'}) }) { |
108 |
# iterate through all "entry-identifiers" |
109 |
foreach (@keys) { |
110 |
my $pattern = $chk->{'pattern'}{'EntryCheck'}; |
111 |
$pattern =~ s/\@\@KEY\@\@/$_/; |
112 |
my $bool_AlreadyThere = ($content_current =~ m/$pattern/); |
113 |
if ($bool_AlreadyThere) { |
114 |
$chk->{'EntryFound'} = $_; |
115 |
return 1; |
116 |
} |
117 |
} |
118 |
} |
119 |
|
120 |
} |
121 |
|
122 |
1; |