| 1 | package AnyData::Storage::File; | ||||||||||||||||||||||||||||||||||
| 2 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use IO::File; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use Fcntl qw(:flock); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 6 | 5 | ▶ | use File::Basename; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use constant HAS_FLOCK => eval { flock STDOUT, 0; 1 }; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 8 | 5 | ▶ | use constant HAS_FILE_SPEC => eval { require File::Spec }; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use vars qw($DEBUG); | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 10 | 5 | $DEBUG = 0; | |||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||
| 13 | 4 | ▶ | sub new { | ||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 14 | 4 | my $class = shift; | |||||||||||||||||||||||||||||||||
| 15 | 4 | ▶ | my $self = shift || {}; | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
shift() || {}
MC/DC100%1/1
shift() || {}
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||
| 16 | #$self->{f_dir} ||= './'; | ||||||||||||||||||||||||||||||||||
| 17 | 4 | return bless $self, $class; | |||||||||||||||||||||||||||||||||
| 18 | } | ||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||
| 20 | 8 | ▶ | sub seek_first_record { | ||||||||||||||||||||||||||||||||
|
Subroutine
seek_first_record: called CC=4 SCAR=14.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 21 | 8 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 22 | 8 | my $fh = $self->{fh}; | |||||||||||||||||||||||||||||||||
| 23 | 8 | my $start = $self->{first_row_pos}; | |||||||||||||||||||||||||||||||||
| 24 | 8 | ▶ | $start | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%2/4
$fh->seek($start, 0) or die $!
$fh->seek(0, 0) or die $!
MC/DC0%0/1
$fh->seek($start, 0) or die $!
$fh->seek($start, 0)
MC/DC0%0/1
$fh->seek(0, 0) or die $!
$fh->seek(0, 0)
Truth table50%1/2
A
A= $fh->seek($start, 0)
Truth table50%1/2
A
A= $fh->seek(0, 0)
| |||||||||||||||||||||||||||||||||||
| 25 | ? $fh->seek($start,0) || die $! | ||||||||||||||||||||||||||||||||||
| 26 | : $fh->seek(0,0) || die $!; | ||||||||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||||||||
| 28 | 0 | ▶ | sub get_pos { return shift->{fh}->tell } | ||||||||||||||||||||||||||||||||
|
Subroutine
get_pos: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 29 | 0 | ▶ | sub go_pos { my($s,$pos)=@_; $s->{fh}->seek($pos,0); } | ||||||||||||||||||||||||||||||||
|
Subroutine
go_pos: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 30 | 5 | my $open_table_re = | |||||||||||||||||||||||||||||||||
| 31 | HAS_FILE_SPEC ? | ||||||||||||||||||||||||||||||||||
| 32 | sprintf('(?:%s|%s|%s)', | ||||||||||||||||||||||||||||||||||
| 33 | quotemeta(File::Spec->curdir()), | ||||||||||||||||||||||||||||||||||
| 34 | quotemeta(File::Spec->updir()), | ||||||||||||||||||||||||||||||||||
| 35 | quotemeta(File::Spec->rootdir())) | ||||||||||||||||||||||||||||||||||
| 36 | : '(?:\.?\.)?\/'; | ||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||
| 39 | 3 | ▶ | sub open_local_file { | ||||||||||||||||||||||||||||||||
|
Subroutine
open_local_file: called CC=22 SCAR=38.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 40 | 3 | my( $self,$file, $open_mode ) = @_; | |||||||||||||||||||||||||||||||||
| 41 | 3 | ▶ | my $dir = $self->{f_dir} || './'; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->{'f_dir'} || './'
MC/DC0%0/1
$self->{'f_dir'} || './'
$self->{'f_dir'}
Truth table50%1/2
A
A= $self->{'f_dir'}
| |||||||||||||||||||||||||||||||||||
| 42 | 3 | my($fname,$path) = fileparse($file); | |||||||||||||||||||||||||||||||||
| 43 | 3 | my($foo2,$os_cur_dir) = fileparse(''); | |||||||||||||||||||||||||||||||||
| 44 | 3 | ▶ | my $haspath = 1 if $path and $path ne $os_cur_dir; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$path and $path ne $os_cur_dir
MC/DC0%0/2
$path and $path ne $os_cur_dir
$path
$path ne $os_cur_dir
Truth table33%1/3
A and B
A= $pathB= $path ne $os_cur_dir
| |||||||||||||||||||||||||||||||||||
| 45 | 3 | ▶ | if (!$haspath && $file !~ /^$open_table_re/o) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$haspath or $file =~ /^$open_table_re/o
MC/DC0%0/2
$haspath or $file =~ /^$open_table_re/o
$haspath
$file =~ /^$open_table_re/o
Truth table33%1/3
A or B
A= $haspathB= $file =~ /^$open_table_re/o
| |||||||||||||||||||||||||||||||||||
| 46 | 0 | $file = HAS_FILE_SPEC | |||||||||||||||||||||||||||||||||
| 47 | ? File::Spec->catfile($dir, $file) | ||||||||||||||||||||||||||||||||||
| 48 | : $dir . "/$file"; | ||||||||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||||||||
| 50 | 3 | my $fh; | |||||||||||||||||||||||||||||||||
| 51 | 3 | ▶ | $open_mode ||= 'r'; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$open_mode ||= "r"
MC/DC0%0/1
$open_mode ||= "r"
$open_mode
Truth table50%1/2
A
A= $open_mode
| |||||||||||||||||||||||||||||||||||
| 52 | 3 | my %valid_mode = ( | |||||||||||||||||||||||||||||||||
| 53 | r => q/read read an existing file, fail if already exists/, | ||||||||||||||||||||||||||||||||||
| 54 | u => q/update read & modify an existing file, fail if already exists/, | ||||||||||||||||||||||||||||||||||
| 55 | c => q/create create a new file, fail if it already exists/, | ||||||||||||||||||||||||||||||||||
| 56 | o => q/overwrite create a new file, overwrite if it already exists/, | ||||||||||||||||||||||||||||||||||
| 57 | ); | ||||||||||||||||||||||||||||||||||
| 58 | 3 | my %mode = ( | |||||||||||||||||||||||||||||||||
| 59 | r => O_RDONLY, | ||||||||||||||||||||||||||||||||||
| 60 | u => O_RDWR, | ||||||||||||||||||||||||||||||||||
| 61 | c => O_CREAT | O_RDWR | O_EXCL, | ||||||||||||||||||||||||||||||||||
| 62 | o => O_CREAT | O_RDWR | O_TRUNC | ||||||||||||||||||||||||||||||||||
| 63 | ); | ||||||||||||||||||||||||||||||||||
| 64 | 3 | my $help = qq( | |||||||||||||||||||||||||||||||||
| 65 | r if file exists, get shared lock | ||||||||||||||||||||||||||||||||||
| 66 | u if file exists, get exclusive lock | ||||||||||||||||||||||||||||||||||
| 67 | c if file doesn't exist, get exclusive lock | ||||||||||||||||||||||||||||||||||
| 68 | o truncate if file exists, else create; get exclusive lock | ||||||||||||||||||||||||||||||||||
| 69 | ); | ||||||||||||||||||||||||||||||||||
| 70 | 3 | ▶ | if ( !$valid_mode{$open_mode} ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 71 | 0 | print "\nBad open_mode '$open_mode'\nValid modes are :\n"; | |||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||
| 73 | 0 | for ('r','u','c','o'){ | |||||||||||||||||||||||||||||||||
| 74 | 0 | print " $_ = $valid_mode{$_}\n"; | |||||||||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||||||||
| 76 | 0 | exit; | |||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||
| 78 | 3 | ▶ | if ($open_mode eq 'c') { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 79 | 0 | ▶ | if (-f $file) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 80 | 0 | die "Cannot create '$file': Already exists"; | |||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||||||||
| 83 | 3 | ▶ | if ($open_mode =~ /[co]/ ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 84 | 1 | ▶ | if (!($fh = IO::File->new( $file, $mode{$open_mode} ))) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 85 | 0 | die "Cannot open '$file': $!"; | |||||||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||||||||
| 87 | 1 | ▶ | if (!$fh->seek(0, 0)) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 88 | 0 | die " Error while seeking back: $!"; | |||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||
| 91 | 3 | ▶ | if ($open_mode =~ /[ru]/) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 92 | 2 | ▶ | die "Cannot read file '$file': doesn't exist!" unless -f $file; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 93 | 2 | ▶ | if (!($fh = IO::File->new($file, $mode{$open_mode}))) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 94 | 0 | die " Cannot open '$file': $!"; | |||||||||||||||||||||||||||||||||
| 95 | } | ||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||
| 97 | 3 | binmode($fh); | |||||||||||||||||||||||||||||||||
| 98 | 3 | $fh->autoflush(1); | |||||||||||||||||||||||||||||||||
| 99 | 3 | if ( HAS_FLOCK ) { | |||||||||||||||||||||||||||||||||
| 100 | 3 | ▶ | if ( $open_mode eq 'r') { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 101 | 2 | ▶ | if (!flock($fh, LOCK_SH)) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 102 | 0 | die "Cannot obtain shared lock on '$file': $!"; | |||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||
| 104 | } else { | ||||||||||||||||||||||||||||||||||
| 105 | 1 | ▶ | if (!flock($fh, LOCK_EX)) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 106 | 0 | die " Cannot obtain exclusive lock on '$file': $!"; | |||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||
| 110 | 3 | ▶ | print "OPENING $file, mode = '$open_mode'\n" if $DEBUG; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 111 | 3 | ▶ | return( $file, $fh, $open_mode) if wantarray; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 112 | 1 | return( $fh ); | |||||||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||
| 115 | 0 | ▶ | sub print_col_names { | ||||||||||||||||||||||||||||||||
|
Subroutine
print_col_names: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 116 | 0 | my($self,$parser,$col_names) = @_; | |||||||||||||||||||||||||||||||||
| 117 | 0 | ▶ | my $fields = $col_names || $self->{col_names} || $parser->{col_names}; | ||||||||||||||||||||||||||||||||
|
Condition0%0/6
$col_names || $self->{'col_names'} || $parser->{'col_names'}
$col_names or $self->{"col_names"}
MC/DC0%0/3
$col_names || $self->{'col_names'} || $parser->{'col_names'}
$col_names
$self->{"col_names"}
$parser->{'col_names'}
Truth table0%0/4
A or B || C
A= $col_namesB= $self->{"col_names"}C= $parser->{'col_names'}
| |||||||||||||||||||||||||||||||||||
| 118 | 0 | ▶ | return undef unless scalar @$fields; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 119 | 0 | $self->{col_names} = $fields; | |||||||||||||||||||||||||||||||||
| 120 | 0 | ▶ | return $fields if $parser->{keep_first_line}; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 121 | 0 | my $first_line = $self->get_record(); | |||||||||||||||||||||||||||||||||
| 122 | 0 | my $fh = $self->{fh}; | |||||||||||||||||||||||||||||||||
| 123 | 0 | $self->seek_first_record; | |||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||
| 125 | 0 | ▶ | my $end = $parser->{record_sep} || "\n"; | ||||||||||||||||||||||||||||||||
|
Condition0%0/2
$parser->{'record_sep'} || "\n"
MC/DC0%0/1
$parser->{'record_sep'} || "\n"
$parser->{'record_sep'}
Truth table0%0/2
A
A= $parser->{'record_sep'}
| |||||||||||||||||||||||||||||||||||
| 126 | 0 | my $colStr = $parser->write_fields(@$fields); | |||||||||||||||||||||||||||||||||
| 127 | 0 | ▶ | $colStr = join( ',',@$fields) . $end if ref($parser) =~ /Fixed/; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 128 | 0 | $fh->write($colStr,length $colStr); | |||||||||||||||||||||||||||||||||
| 129 | 0 | $self->{first_row_pos} = $fh->tell(); | |||||||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||
| 132 | 2 | ▶ | sub get_col_names { | ||||||||||||||||||||||||||||||||
|
Subroutine
get_col_names: called CC=9 SCAR=23.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 133 | 2 | my($self,$parser) = @_; | |||||||||||||||||||||||||||||||||
| 134 | 2 | my @fields = (); | |||||||||||||||||||||||||||||||||
| 135 | 2 | ▶ | if ($parser->{keep_first_line}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 136 | 1 | my $cols = $parser->{col_names}; | |||||||||||||||||||||||||||||||||
| 137 | 1 | ▶ | return undef unless $cols; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 138 | 1 | ▶ | return $cols if ref $cols eq 'ARRAY'; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 139 | 0 | @fields = split ',',$cols; | |||||||||||||||||||||||||||||||||
| 140 | #die "@fields"; | ||||||||||||||||||||||||||||||||||
| 141 | return scalar @fields | ||||||||||||||||||||||||||||||||||
| 142 | 0 | ▶ | ? \@fields | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 143 | : undef; | ||||||||||||||||||||||||||||||||||
| 144 | } | ||||||||||||||||||||||||||||||||||
| 145 | 1 | my $fh = $self->{fh}; | |||||||||||||||||||||||||||||||||
| 146 | 1 | ▶ | $fh->seek(0,0) if $fh; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 147 | 1 | my $first_line = $self->get_record($parser); | |||||||||||||||||||||||||||||||||
| 148 | #print $first_line; | ||||||||||||||||||||||||||||||||||
| 149 | 1 | ▶ | if ( $first_line ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 150 | 1 | ▶ | @fields = ref($parser) =~ /Fixed/ | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 151 | ? split /,/,$first_line | ||||||||||||||||||||||||||||||||||
| 152 | : $parser->read_fields($first_line); | ||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||
| 154 | # my @fields = $first_line | ||||||||||||||||||||||||||||||||||
| 155 | # ? $parser->read_fields($first_line) | ||||||||||||||||||||||||||||||||||
| 156 | # : (); | ||||||||||||||||||||||||||||||||||
| 157 | #print "<$_>" for @fields; print "\n"; | ||||||||||||||||||||||||||||||||||
| 158 | return "CAN'T FIND COLUMN NAMES ON FIRST LINE OF '" | ||||||||||||||||||||||||||||||||||
| 159 | . $self->{file} | ||||||||||||||||||||||||||||||||||
| 160 | 1 | ▶ | . "' : '@fields'" if "@fields" =~ /[^ a-zA-Z0-9_]/; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 161 | 1 | $parser->{col_names} = \@fields; | |||||||||||||||||||||||||||||||||
| 162 | 1 | $self->{col_names} = \@fields; | |||||||||||||||||||||||||||||||||
| 163 | 1 | $self->{col_nums} = $self->set_col_nums; | |||||||||||||||||||||||||||||||||
| 164 | 1 | $self->{first_row_pos} = $fh->tell(); | |||||||||||||||||||||||||||||||||
| 165 | 1 | return( \@fields); | |||||||||||||||||||||||||||||||||
| 166 | } | ||||||||||||||||||||||||||||||||||
| 167 | 2 | ▶ | sub open_table { | ||||||||||||||||||||||||||||||||
|
Subroutine
open_table: called CC=13 SCAR=28.7
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 168 | 2 | my( $self, $parser, $file, $open_mode ) = @_; | |||||||||||||||||||||||||||||||||
| 169 | 2 | my($newfile, $fh); | |||||||||||||||||||||||||||||||||
| 170 | 2 | ▶ | $file ||= ''; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$file ||= ""
MC/DC0%0/1
$file ||= ""
$file
Truth table50%1/2
A
A= $file
| |||||||||||||||||||||||||||||||||||
| 171 | 2 | ▶ | if ( $file =~ m'http://|ftp://' ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 172 | # die "wrong storage!"; | ||||||||||||||||||||||||||||||||||
| 173 | 0 | $newfile = $file; | |||||||||||||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||||||||||||||
| 175 | else { | ||||||||||||||||||||||||||||||||||
| 176 | 2 | ▶ | ($newfile,$fh) = | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$file and not ref $file
MC/DC0%0/2
$file and not ref $file
$file
not ref $file
Truth table33%1/3
A and not(B)
A= $fileB= not ref $file
| |||||||||||||||||||||||||||||||||||
| 177 | $self->open_local_file($file,$open_mode) if $file && !(ref $file); | ||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||||||||
| 180 | 2 | ▶ | $newfile ||= $file; | ||||||||||||||||||||||||||||||||
|
Condition33%1/3
$newfile ||= $file
MC/DC0%0/2
$newfile ||= $file
$newfile
$file
Truth table33%1/3
A ||= B
A= $newfileB= $file
| |||||||||||||||||||||||||||||||||||
| 181 | #die AnyData::dump($parser); | ||||||||||||||||||||||||||||||||||
| 182 | 2 | ▶ | my $col_names = $parser->{col_names} || ''; | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
$parser->{'col_names'} || ''
MC/DC100%1/1
$parser->{'col_names'} || ''
$parser->{'col_names'}
Truth table100%2/2
A
A= $parser->{'col_names'}
| |||||||||||||||||||||||||||||||||||
| 183 | # my @array = split(/,/,$col_names); | ||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||
| 185 | 2 | my @array; | |||||||||||||||||||||||||||||||||
| 186 | 2 | ▶ | @array = ref $col_names eq 'ARRAY' | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 187 | ? @$col_names | ||||||||||||||||||||||||||||||||||
| 188 | : split ',',$col_names; | ||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||
| 190 | 2 | ▶ | my $pos = $fh->tell() if $fh; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 191 | 2 | my %table = ( | |||||||||||||||||||||||||||||||||
| 192 | file => $newfile, | ||||||||||||||||||||||||||||||||||
| 193 | open_mode => $open_mode, | ||||||||||||||||||||||||||||||||||
| 194 | fh => $fh, | ||||||||||||||||||||||||||||||||||
| 195 | col_nums => {}, | ||||||||||||||||||||||||||||||||||
| 196 | col_names => \@array, | ||||||||||||||||||||||||||||||||||
| 197 | first_row_pos => $pos | ||||||||||||||||||||||||||||||||||
| 198 | ); | ||||||||||||||||||||||||||||||||||
| 199 | 2 | for my $key(keys %table) { | |||||||||||||||||||||||||||||||||
| 200 | 12 | $self->{$key}=$table{$key}; | |||||||||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||||||||
| 202 | 2 | my $skip = $parser->init_parser($self); | |||||||||||||||||||||||||||||||||
| 203 | 2 | ▶ | if (!$skip && defined $newfile) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not $skip and defined $newfile
MC/DC0%0/2
not $skip and defined $newfile
not $skip
defined $newfile
Truth table33%1/3
not(A) and B
A= not $skipB= defined $newfile
| |||||||||||||||||||||||||||||||||||
| 204 | 2 | ▶ | $open_mode =~ /[co]/ | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 205 | ? $self->print_col_names($parser) | ||||||||||||||||||||||||||||||||||
| 206 | : $self->get_col_names($parser); | ||||||||||||||||||||||||||||||||||
| 207 | } | ||||||||||||||||||||||||||||||||||
| 208 | 2 | $self->{col_nums} = $self->set_col_nums(); | |||||||||||||||||||||||||||||||||
| 209 | # use Data::Dumper; die Dumper $self; | ||||||||||||||||||||||||||||||||||
| 210 | } | ||||||||||||||||||||||||||||||||||
| 211 | 0 | ▶ | sub get_file_handle { return shift->{fh} } | ||||||||||||||||||||||||||||||||
|
Subroutine
get_file_handle: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 212 | 0 | ▶ | sub get_file_name { return shift->{file} } | ||||||||||||||||||||||||||||||||
|
Subroutine
get_file_name: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 213 | 0 | ▶ | sub get_file_open_mode { return shift->{open_mode} } | ||||||||||||||||||||||||||||||||
|
Subroutine
get_file_open_mode: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||
| 215 | 33 | ▶ | sub file2str { return shift->get_record(@_) } | ||||||||||||||||||||||||||||||||
|
Subroutine
file2str: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 216 | 34 | ▶ | sub get_record { | ||||||||||||||||||||||||||||||||
|
Subroutine
get_record: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 217 | 34 | my($self,$parser)=@_; | |||||||||||||||||||||||||||||||||
| 218 | 34 | ▶ | local $/ = $parser->{record_sep} || "\n"; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$parser->{'record_sep'} || "\n"
MC/DC0%0/1
$parser->{'record_sep'} || "\n"
$parser->{'record_sep'}
Truth table50%1/2
A
A= $parser->{'record_sep'}
| |||||||||||||||||||||||||||||||||||
| 219 | 34 | my $fh = $self->{fh} ; | |||||||||||||||||||||||||||||||||
| 220 | 34 | ▶ | my $record = $fh->getline || return undef; | ||||||||||||||||||||||||||||||||
|
Condition100%2/2
$fh->getline || (return undef)
MC/DC100%1/1
$fh->getline || (return undef)
$fh->getline
Truth table100%2/2
A
A= $fh->getline
| |||||||||||||||||||||||||||||||||||
| 221 | 31 | $record =~ s/\015$//g; | |||||||||||||||||||||||||||||||||
| 222 | 31 | $record =~ s/\012$//g; | |||||||||||||||||||||||||||||||||
| 223 | 31 | return $record; | |||||||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||
| 226 | 3 | ▶ | sub set_col_nums { | ||||||||||||||||||||||||||||||||
|
Subroutine
set_col_nums: called CC=4 SCAR=14.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 227 | 3 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 228 | 3 | my $col_names = $self->{col_names}; | |||||||||||||||||||||||||||||||||
| 229 | 3 | ▶ | return {} unless $col_names; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 230 | 3 | my $col_nums={}; my $i=0; | |||||||||||||||||||||||||||||||||
| 231 | 3 | for (@$col_names) { | |||||||||||||||||||||||||||||||||
| 232 | 13 | ▶ | next unless $_; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 233 | 13 | $col_nums->{$col_names->[$i]} = $i; | |||||||||||||||||||||||||||||||||
| 234 | 13 | $i++; | |||||||||||||||||||||||||||||||||
| 235 | } | ||||||||||||||||||||||||||||||||||
| 236 | 3 | return $col_nums; | |||||||||||||||||||||||||||||||||
| 237 | } | ||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||
| 239 | 0 | ▶ | sub truncate { | ||||||||||||||||||||||||||||||||
|
Subroutine
truncate: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 240 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 241 | 0 | ▶ | if (!$self->{fh}->truncate($self->{fh}->tell())) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 242 | 0 | die "Error while truncating " . $self->{file} . ": $!"; | |||||||||||||||||||||||||||||||||
| 243 | } | ||||||||||||||||||||||||||||||||||
| 244 | } | ||||||||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||||||||
| 246 | 0 | ▶ | sub drop ($) { | ||||||||||||||||||||||||||||||||
|
Subroutine
drop: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 247 | 0 | my($self) = @_; | |||||||||||||||||||||||||||||||||
| 248 | # We have to close the file before unlinking it: Some OS'es will | ||||||||||||||||||||||||||||||||||
| 249 | # refuse the unlink otherwise. | ||||||||||||||||||||||||||||||||||
| 250 | 0 | ▶ | $self->{'fh'}->close() || die $!; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 251 | 0 | ▶ | unlink($self->{'file'}) || die $!; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 252 | 0 | return 1; | |||||||||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||||||||
| 254 | 0 | ▶ | sub close{ shift->{'fh'}->close() || die $!; } | ||||||||||||||||||||||||||||||||
|
Subroutine
close: not called CC=2 SCAR=17.9
Pod
undocumented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||
| 256 | 0 | ▶ | sub push_row { | ||||||||||||||||||||||||||||||||
|
Subroutine
push_row: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 257 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 258 | 0 | my $rec = shift; | |||||||||||||||||||||||||||||||||
| 259 | 0 | my $fh = $self->{fh}; | |||||||||||||||||||||||||||||||||
| 260 | #####!!!! DON'T USE THIS #### $fh->seek(0,2) or die $!; | ||||||||||||||||||||||||||||||||||
| 261 | 0 | ▶ | $fh->write($rec,length $rec) | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 262 | || die "Couldn't write to file: $!\n"; | ||||||||||||||||||||||||||||||||||
| 263 | } | ||||||||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||||||||
| 265 | 0 | ▶ | sub delete_record { | ||||||||||||||||||||||||||||||||
|
Subroutine
delete_record: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 266 | 0 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 267 | 0 | ▶ | my $parser = shift || {}; | ||||||||||||||||||||||||||||||||
|
Condition0%0/2
shift() || {}
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||
| 268 | 0 | my $fh = $self->{fh}; | |||||||||||||||||||||||||||||||||
| 269 | 0 | ▶ | my $travel = length($parser->{record_sep}) || 0; | ||||||||||||||||||||||||||||||||
|
Condition0%0/2
length $parser->{'record_sep'} || 0
MC/DC0%0/1
length $parser->{'record_sep'} || 0
length $parser->{'record_sep'}
Truth table0%0/2
A
A= length $parser->{'record_sep'}
| |||||||||||||||||||||||||||||||||||
| 270 | 0 | my $pos = $fh->tell - $travel; | |||||||||||||||||||||||||||||||||
| 271 | 0 | $self->{deleted}->{$pos}++; | |||||||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||||||||
| 273 | 30 | ▶ | sub is_deleted { | ||||||||||||||||||||||||||||||||
|
Subroutine
is_deleted: called CC=3 SCAR=11.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 274 | 30 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 275 | 30 | ▶ | my $parser = shift || {}; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
shift() || {}
MC/DC0%0/1
shift() || {}
shift()
Truth table50%1/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||
| 276 | 30 | my $fh = $self->{fh}; | |||||||||||||||||||||||||||||||||
| 277 | 30 | ▶ | my $travel = length($parser->{record_sep}) || 0; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
length $parser->{'record_sep'} || 0
MC/DC0%0/1
length $parser->{'record_sep'} || 0
length $parser->{'record_sep'}
Truth table50%1/2
A
A= length $parser->{'record_sep'}
| |||||||||||||||||||||||||||||||||||
| 278 | 30 | my $pos = $fh->tell - $travel; | |||||||||||||||||||||||||||||||||
| 279 | 30 | return $self->{deleted}->{$pos}; | |||||||||||||||||||||||||||||||||
| 280 | } | ||||||||||||||||||||||||||||||||||
| 281 | 0 | ▶ | sub seek { | ||||||||||||||||||||||||||||||||
|
Subroutine
seek: not called CC=6 SCAR=37.4
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 282 | 0 | my($self, $pos, $whence) = @_; | |||||||||||||||||||||||||||||||||
| 283 | 0 | ▶ | if ($whence == 0 && $pos == 0) { | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition0%0/3
$whence == 0 and $pos == 0
MC/DC0%0/2
$whence == 0 and $pos == 0
$whence == 0
$pos == 0
Truth table0%0/3
A and B
A= $whence == 0B= $pos == 0
| |||||||||||||||||||||||||||||||||||
| 284 | 0 | $pos = $self->{first_row_pos}; | |||||||||||||||||||||||||||||||||
| 285 | ▶ | } elsif ($whence != 2 || $pos != 0) { | |||||||||||||||||||||||||||||||||
|
Condition0%0/3
$whence != 2 or $pos != 0
MC/DC0%0/2
$whence != 2 or $pos != 0
$whence != 2
$pos != 0
Truth table0%0/3
A or B
A= $whence != 2B= $pos != 0
| |||||||||||||||||||||||||||||||||||
| 286 | 0 | die "Illegal seek position: pos = $pos, whence = $whence"; | |||||||||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||||||||
| 288 | 0 | ▶ | if (!$self->{fh}->seek($pos, $whence)) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 289 | 0 | die "Error while seeking in " . $self->{'file'} . ": $!"; | |||||||||||||||||||||||||||||||||
| 290 | } | ||||||||||||||||||||||||||||||||||
| 291 | #print "<$pos-$whence>"; | ||||||||||||||||||||||||||||||||||
| 292 | } | ||||||||||||||||||||||||||||||||||
| 293 | |||||||||||||||||||||||||||||||||||
| 294 | 4 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=4 SCAR=14.1
| |||||||||||||||||||||||||||||||||||
| 295 | 4 | my $self = shift; | |||||||||||||||||||||||||||||||||
| 296 | 4 | my $fh = $self->{fh}; | |||||||||||||||||||||||||||||||||
| 297 | 4 | ▶ | print "CLOSING ", $self->get_file_name, "\n" if $fh && $DEBUG; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$fh and $AnyData::Storage::File::DEBUG
MC/DC0%0/2
$fh and $AnyData::Storage::File::DEBUG
$fh
$AnyData::Storage::File::DEBUG
Truth table66%2/3
A and B
A= $fhB= $AnyData::Storage::File::DEBUG
| |||||||||||||||||||||||||||||||||||
| 298 | 4 | ▶ | $fh->close if $fh; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||||||||
| 300 | __END__ | ||||||||||||||||||||||||||||||||||