/[cvs]/nfo/perl/libs/Data/Storage/Handler/File.pm
ViewVC logotype

Contents of /nfo/perl/libs/Data/Storage/Handler/File.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.2 - (show annotations)
Sun Jan 19 02:13:24 2003 UTC (21 years, 5 months ago) by joko
Branch: MAIN
Changes since 1.1: +10 -2 lines
+ modified and exported 'a2f'

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) or do {
36 print "Could not append to \"$filename\"!", "\n";
37 print "Log-Message was: ";
38 print $string if $string;
39 print "\n";
40 return;
41 };
42 #print FH "\n";
43 print FH $string;
44 print FH "\n";
45 close(FH);
46 return 1;
47 }
48
49 sub ris {
50 my $string = shift;
51 my $rules = shift;
52
53 our $ris_result = 1;
54
55 if (ref $rules eq 'HASH') {
56 my @re_find = keys %{$rules};
57 # replace all keys with substitutes from hash "%re_table"
58 foreach my $find (@re_find) {
59 my $replace = $rules->{$find};
60 $ris_result &= ($string =~ s/$find/$replace/g);
61 }
62 }
63
64 if (ref $rules eq 'ARRAY') {
65 foreach my $rule (@{$rules}) {
66 my $find = $rule->[0];
67 my $replace = $rule->[1];
68 $ris_result &= ($string =~ s/$find/$replace/g);
69 }
70 }
71
72 return $string;
73 }
74
75 sub rif {
76 my $filename = shift;
77 my $rules = shift;
78 my $out_suffix = shift;
79
80 my $outfile = $filename;
81 $outfile .= '.' . $out_suffix if ($out_suffix);
82
83 my $buf = f2s($filename);
84 $buf = ris($buf, $rules);
85 s2f($outfile, $buf);
86 }
87
88 sub findKeyEntries {
89 my $string = shift;
90 my $pattern = shift;
91 my @arr = split("\n", $string);
92 my @entries;
93 foreach (@arr) {
94 chomp;
95 #print "l: ", $_, "\n";
96 if (m/$pattern/) {
97 push @entries, $1;
98 }
99 }
100 return \@entries;
101 }
102
103 # ---------------------------------
104 # is a context-entry in a file?
105 # a "context-entry" is an entry identified
106 # by a certain keystring, which itself
107 # is detected dynamically
108 sub isEntryInFile {
109
110 my $chk = shift;
111 my $content_current = f2s($chk->{filename});
112
113 # try to find all key-entries via patterns which are "entry-identifiers"
114 if (my @keys = @{ findKeyEntries($chk->{'out'}, $chk->{'pattern'}{'EntryIdent'}) }) {
115 # iterate through all "entry-identifiers"
116 foreach (@keys) {
117 my $pattern = $chk->{'pattern'}{'EntryCheck'};
118 $pattern =~ s/\@\@KEY\@\@/$_/;
119 my $bool_AlreadyThere = ($content_current =~ m/$pattern/);
120 if ($bool_AlreadyThere) {
121 $chk->{'EntryFound'} = $_;
122 return 1;
123 }
124 }
125 }
126
127 }
128
129 1;

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed