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