blib/lib/Uplug/IO/Text.pm untested

filter: Statementstmt 0.0% 0 / 183 Branchbran 0.0% 0 / 96 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 324 CC 49 SCAR 52.2
CC
49
Cov
0%
OpenStreamFile
506.0
FindDataFile
72.0
init
42.0
CRAP 185.2
1 #-*-perl-*-
2 ###########################################################################
3 # Copyright (C) 2004 Jörg Tiedemann <joerg@stp.ling.uu.se>
4 #
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 2 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 #
19 # simple text stream (reading line by line)
20 # (derived from Uplug::IO)
21 #
22 # TextStream::open($AccessMode,\%OptionHash)
23 # $AccessMode - read|write|overwrite|append
24 # %OptionHash - FileName => '/path/to/file' (required)
25 # pipe command => pipe_command (optional)
26 # input pipe command => input_pipe (optional)
27 # output pipe command => output_pipe (optional)
28 #
29 ###########################################################################
30  
31  
32 package Uplug::IO::Text;
33  
34 # use bytes; #### ????????? Do WE NEED THIS ?????????????
35 use strict;
36 use vars qw(@ISA $COMPRESS $DECOMPRESS);
37 use FindBin qw($Bin);
38 use lib "$Bin/../lib";
39 use Data::Dumper;
40 use File::Basename;
41 use FileHandle;
42  
43 use Uplug::IO;
44  
45 @ISA = qw( Uplug::IO );
46  
47 $COMPRESS='gzip -c';
48 $DECOMPRESS='gzip -cd';
49  
50  
51 sub init{
52 my $self = shift;
53 my $OptionHash = shift;
54  
55 $self->SUPER::init($OptionHash);
56  
57 if ((not defined $self->{StreamOptions}->{FileName}) and
58 (defined $self->{StreamOptions}->{file})){
59 $self->{StreamOptions}->{FileName}=$self->{StreamOptions}->{file};
60 }
61 $self->{FileName}=$self->{StreamOptions}->{FileName};
62 $self->{StreamOptions}->{file}=$self->{StreamOptions}->{FileName};
63  
64 my $ret;
65 if (not defined $self->{FileName}){
66 if (not defined $self->{FileHandle}){
67 if ($self->{AccessMode} eq 'read'){
68 $self->{FileHandle}=*STDIN;
69 }
70 else{
71 $self->{FileHandle}=*STDOUT;
72 }
73 }
74 $ret=1;
75 }
76 else{
77 $ret=$self->{'FileHandle'}=&OpenStreamFile($self->{FileName},
78 $self->{AccessMode},
79 $OptionHash);
80 }
81 if ($]>=5.008){ # for Perl version >= 5.8
82 binmode($self->{FileHandle}, # set PerlIO layer
83 ':encoding('.$self->getEncoding.')'); # according to char enc.
84 }
85 return $ret;
86 }
87  
88 sub close{
89 my $self=shift;
90 return $self->SUPER::close;
91 if (defined $self->{'FileHandle'}){
92 $self->{'FileHandle'}->close;
93 }
94 }
95  
96 sub getFileHandle{return $_[0]->{FileHandle};} # return file-handle!
97  
98  
99 sub read{
100 my $self=shift;
101 my $data=shift;
102 if (not ref($data)){return 0;}
103 $data->init;
104  
105 my $fh=$self->{'FileHandle'};
106 # my $content=<$fh>;
107 my $content=$self->readFromHandle($fh);
108 chomp $content;
109 $data->setContent($content);
110 $self->SUPER::read($data);
111 return (defined $content);
112 }
113  
114  
115 sub write{
116 my $self=shift;
117 my ($data)=@_;
118 my $content=$data;
119 if (ref($data)){
120 $self->SUPER::write($data);
121 $content=$data->content;
122 }
123 my $fh=$self->{'FileHandle'};
124 $self->writeToHandle($fh,$content."\n");
125 return 1;
126 }
127  
128  
129  
130  
131 sub readheader{
132 my $self=shift;
133 $self->SUPER::readheader;
134 my $fh=$self->{'FileHandle'};
135 # print STDERR "---$fh--\n";
136 if ($fh=~/STDIN/){return;}
137 my $CurrentPos;
138 my %HeaderHash=();
139 my $DataLine;
140 do {
141 $CurrentPos=tell $fh;
142 if (not $DataLine=$self->readFromHandle($fh)){
143 $self->addheader(\%HeaderHash);
144 return 0;
145 }
146 chomp $DataLine;
147 if ($DataLine=~/^\# ([^:]+):\s*(\S.*)\s*$/){
148 $HeaderHash{$1}=eval $2;
149 # $HeaderHash{$1}=$2;
150 }
151 }
152 until ($DataLine!~/^\#/);
153 seek $fh,$CurrentPos,0;
154 if (tell $fh != $CurrentPos){$self->{READBUFFER}=$DataLine;}
155 $self->addheader(\%HeaderHash);
156 }
157  
158  
159  
160 sub writeheader{
161 my $self=shift;
162 if (ref($self->{StreamHeader}) eq 'HASH'){
163 my $fh=$self->{'FileHandle'};
164 foreach my $k (sort keys %{$self->{StreamHeader}}){
165  
166 $Data::Dumper::Terse = 1;
167 $Data::Dumper::Indent = 0;
168 $Data::Dumper::Purity=1;
169 my $string=Dumper($self->{StreamHeader}->{$k});
170 $self->writeToHandle($fh,"# $k: $string\n");
171 }
172 }
173 }
174  
175  
176 sub files{
177 my $self=shift;
178 if (defined $self->{file}){
179 return wantarray ? ($self->{file}) : $self->{file};
180 }
181 if (defined $self->{FileName}){
182 return wantarray ? ($self->{FileName}) : $self->{FileName};
183 }
184 }
185  
186 sub delete{
187 my $self=shift;
188 my $file;
189 if (defined $self->{file}){$file=$self->{file};}
190 if (defined $self->{FileName}){$file=$self->{FileName};}
191 if (-e $file){
192 print STDERR "# LiuStream.pm: remove file $file!\n";
193 unlink $file;
194 }
195 }
196  
197  
198  
199  
200  
201  
202  
203 ##########################################################################
204  
205  
206 sub OpenStreamFile{
207  
208 my $FileName=shift;
209 my $AccessMode=shift;
210 my $OptionHash=shift;
211  
212 print STDERR "open $FileName ($AccessMode)\n";
213  
214 my $fh = new FileHandle;
215  
216 #---------------------------------------------------------------------------
217 # initialize pipe command according to the access mode
218 #---------------------------------------------------------------------------
219  
220 if ($AccessMode eq 'read'){
221 $FileName=&FindDataFile($FileName);
222 if (defined $$OptionHash{'input pipe command'}){
223 $$OptionHash{'pipe command'}=$$OptionHash{'input pipe command'};
224 }
225 }
226 if ($AccessMode ne 'read'){
227 if (defined $$OptionHash{'output pipe command'}){
228 $$OptionHash{'pipe command'}=$$OptionHash{'output pipe command'};
229 }
230 }
231  
232 #---------------------------------------------------------------------------
233 # gzipped files: add gzip command in pipe
234 #---------------------------------------------------------------------------
235  
236 if ($FileName=~/\.gz$/){
237 my $compress;
238 if ($AccessMode eq 'read'){
239 $compress=$Uplug::IO::Text::DECOMPRESS;
240 }
241 else{
242 $compress=$Uplug::IO::Text::COMPRESS;
243 }
244 if (not defined $$OptionHash{'pipe command'}){
245 $$OptionHash{'pipe command'}=$compress;
246 }
247 else{
248 if ($$OptionHash{'pipe command'}!~/$compress/){
249 if ($AccessMode eq 'read'){
250 $$OptionHash{'pipe command'}=
251 "$compress | $$OptionHash{'pipe command'}";
252 }
253 else{
254 $$OptionHash{'pipe command'}=
255 "$$OptionHash{'pipe command'} | $compress";
256 }
257 }
258 }
259 }
260  
261 #---------------------------------------------------------------------------
262 # access_mode == read --> open files for reading
263 #---------------------------------------------------------------------------
264  
265 if ($AccessMode eq 'read'){
266 if (-e $FileName){ # if file exists
267 my $str="<$FileName";
268 if ($$OptionHash{'pipe command'}){
269 $str="$$OptionHash{'pipe command'} $str |";
270 }
271 if (not $fh->open($str)){
272 warn "failed to open $FileName";
273 return 0;
274 }
275 }
276 }
277  
278 #---------------------------------------------------------------------------
279 # access_mode <> read --> open files for writing
280 #---------------------------------------------------------------------------
281  
282 if ($AccessMode ne 'read'){
283 my $f;
284 my $pipe='';
285 if ($$OptionHash{'pipe command'}){
286 $pipe="| $$OptionHash{'pipe command'} ";
287 }
288  
289 #---------------------------------------------------------------------------
290 # ... file exists and is non-empty
291 #---------------------------------------------------------------------------
292  
293 if ((-s $FileName) and
294 ($AccessMode eq 'write')){
295 warn "failed to open $FileName, data exist!";
296 return 0;
297 }
298  
299 #---------------------------------------------------------------------------
300 # ... file exists and access_mode is 'overwrite'
301 #---------------------------------------------------------------------------
302  
303 elsif ((-e $FileName) and
304 ($AccessMode eq 'overwrite')){
305 # print STDERR "# Uplug::IO::Text.pm: Data exist in $FileName! (overwriting!)\n";
306  
307 if (not $fh->open("$pipe>$FileName")){
308 warn "failed to open $FileName";
309 return 0;
310 }
311 }
312  
313 #---------------------------------------------------------------------------
314 # ... access_mode is 'append'
315 #---------------------------------------------------------------------------
316  
317 elsif ($AccessMode eq 'append'){
318 if (not $fh->open("$pipe>>$FileName")){
319 warn "failed to open $FileName";
320 return 0;
321 }
322 }
323  
324 #---------------------------------------------------------------------------
325 # ... (file is empty or does not exist) and (access_mode is not 'append')
326 #---------------------------------------------------------------------------
327  
328 else{
329 if (not $fh->open("$pipe>$FileName")){
330 warn "failed to open $FileName in $$OptionHash{'id'}!";
331 return 0;
332 }
333 }
334 }
335 # binmode($fh);
336 return $fh;
337 }
338  
339  
340 ############################################################################
341  
342 sub FindDataFile{
343 my ($file)=@_;
344 if (-f $file){return $file;}
345 if ($file!~/\.gz$/){
346 my $new=&FindDataFile("$file.gz");
347 if (-f $new){return $new;}
348 }
349 if (-f "data/$file"){return "data/$file";}
350 if (-f "$ENV{UPLUGHOME}/$file"){return "$ENV{UPLUGHOME}/$file";}
351 $file=basename($file);
352 if (-f "data/$file"){return "data/$file";}
353 if (-f "$ENV{UPLUGHOME}/$file"){return "$ENV{UPLUGHOME}/$file";}
354 return $file;
355 }