| 1 | # | ||||||||||||||||||||
| 2 | # (c) Jan Gehring <jan.gehring@gmail.com> | ||||||||||||||||||||
| 3 | # | ||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | Rex::FS::File - File Class | ||||||||||||||||||||
| 8 | |||||||||||||||||||||
| 9 | =head1 DESCRIPTION | ||||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | This is the File Class used by I<file_write> and I<file_read>. | ||||||||||||||||||||
| 12 | |||||||||||||||||||||
| 13 | =head1 SYNOPSIS | ||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | use Rex::Interface::File; | ||||||||||||||||||||
| 16 | my $fh = Rex::Interface::File->create('Local'); | ||||||||||||||||||||
| 17 | $fh->open( '<', 'filename' ); | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | my $file = Rex::FS::File->new(fh => $fh); | ||||||||||||||||||||
| 20 | $file->read($len); | ||||||||||||||||||||
| 21 | $file->read_all; | ||||||||||||||||||||
| 22 | $file->write($buf); | ||||||||||||||||||||
| 23 | $file->close; | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | =head1 CLASS METHODS | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | =cut | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | package Rex::FS::File; | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | 49 | ▶ | use v5.14.4; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 32 | 49 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 33 | 49 | ▶ | use Rex::Interface::File; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | 49 | our $VERSION = '1.16.1'; # VERSION | |||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | 49 | ▶ | use constant DEFAULT_READ_LEN => 64; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | =head2 new | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | This is the constructor. You need to set the filehandle which the object should work on | ||||||||||||||||||||
| 42 | or pass a filename. If you pass a filehandle, it has to be a C<Rex::Interface::File::*> | ||||||||||||||||||||
| 43 | object | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | my $fh = Rex::Interface::File->create('Local'); | ||||||||||||||||||||
| 46 | $fh->open( '<', 'filename' ); | ||||||||||||||||||||
| 47 | |||||||||||||||||||||
| 48 | my $file = Rex::FS::File->new(fh => $fh); | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | Create a C<Rex::FS::File> object with a filename | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | # open a local file in read mode | ||||||||||||||||||||
| 53 | my $file = Rex::FS::File->new( | ||||||||||||||||||||
| 54 | filename => 'filename', | ||||||||||||||||||||
| 55 | mode => 'r', # or '<' | ||||||||||||||||||||
| 56 | type => 'Local', | ||||||||||||||||||||
| 57 | ); | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | # or shorter | ||||||||||||||||||||
| 60 | my $file = Rex::FS::File->new( filename => 'filename' ); | ||||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | # open a local file in write mode | ||||||||||||||||||||
| 63 | my $file = Rex::FS::File->new( | ||||||||||||||||||||
| 64 | filename => 'filename', | ||||||||||||||||||||
| 65 | mode => 'w', # or '>' | ||||||||||||||||||||
| 66 | ); | ||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | Allowed modes: | ||||||||||||||||||||
| 69 | |||||||||||||||||||||
| 70 | < read | ||||||||||||||||||||
| 71 | r read | ||||||||||||||||||||
| 72 | > write | ||||||||||||||||||||
| 73 | w write | ||||||||||||||||||||
| 74 | >> append | ||||||||||||||||||||
| 75 | a append | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | For allowed C<types> see documentation of L<Rex::Interface::File>. | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | =cut | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | 195 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=7 SCAR=20.3
Pod
documented
| |||||||||||||||||||||
| 82 | 195 | my $that = shift; | |||||||||||||||||||
| 83 | 195 | ▶ | my $proto = ref($that) || $that; | ||||||||||||||||||
|
Condition33%1/3
ref $that || $that
MC/DC0%0/2
ref $that || $that
ref $that
$that
Truth table33%1/3
A || B
A= ref $thatB= $that
| |||||||||||||||||||||
| 84 | 195 | my $self = {@_}; | |||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | 195 | my %modes = ( | |||||||||||||||||||
| 87 | 'w' => '>', | ||||||||||||||||||||
| 88 | 'r' => '<', | ||||||||||||||||||||
| 89 | 'a' => '>>', | ||||||||||||||||||||
| 90 | '<' => '<', | ||||||||||||||||||||
| 91 | '>' => '>', | ||||||||||||||||||||
| 92 | '>>' => '>>', | ||||||||||||||||||||
| 93 | ); | ||||||||||||||||||||
| 94 | |||||||||||||||||||||
| 95 | 195 | ▶ | if ( $self->{filename} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 96 | 5 | ▶ | $self->{mode} ||= '<'; | ||||||||||||||||||
|
Condition100%2/2
$self->{"mode"} ||= "<"
MC/DC100%1/1
$self->{"mode"} ||= "<"
$self->{"mode"}
Truth table100%2/2
A
A= $self->{"mode"}
| |||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | 5 | ▶ | my $mode = $modes{ $self->{mode} } || '<'; | ||||||||||||||||||
|
Condition50%1/2
$modes{$self->{'mode'}} || '<'
MC/DC0%0/1
$modes{$self->{'mode'}} || '<'
$modes{$self->{'mode'}}
Truth table50%1/2
A
A= $modes{$self->{'mode'}}
| |||||||||||||||||||||
| 99 | 5 | ▶ | $self->{fh} = Rex::Interface::File->create( $self->{type} || 'Local' ); | ||||||||||||||||||
|
Condition50%1/2
$self->{'type'} || 'Local'
MC/DC0%0/1
$self->{'type'} || 'Local'
$self->{'type'}
Truth table50%1/2
A
A= $self->{'type'}
| |||||||||||||||||||||
| 100 | 5 | $self->{fh}->open( $mode, $self->{filename} ); | |||||||||||||||||||
| 101 | } | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | 195 | bless( $self, $proto ); | |||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | 195 | ▶ | if ( ref $self->{fh} !~ m{Rex::Interface::File} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 106 | 0 | die "Need an Rex::Interface::File object"; | |||||||||||||||||||
| 107 | } | ||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | 195 | return $self; | |||||||||||||||||||
| 110 | } | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | 195 | ▶ | sub DESTROY { | ||||||||||||||||||
|
Subroutine
DESTROY: called CC=4 SCAR=17.9
| |||||||||||||||||||||
| 113 | 195 | my ($self) = @_; | |||||||||||||||||||
| 114 | 195 | ▶ | if ( ref $self->{'fh'} =~ m/^Rex::Interface::File/ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 115 | 0 | ▶ | $self->close if ( $self->{'fh'}->{'fh'} ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 116 | } | ||||||||||||||||||||
| 117 | else { | ||||||||||||||||||||
| 118 | 195 | ▶ | $self->close if ( $self->{'fh'} ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 119 | } | ||||||||||||||||||||
| 120 | } | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | =head2 write($buf) | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | Write $buf into the filehandle. | ||||||||||||||||||||
| 125 | |||||||||||||||||||||
| 126 | $file->write("Hello World"); | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | =cut | ||||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | 253 | ▶ | sub write { | ||||||||||||||||||
|
Subroutine
write: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 131 | |||||||||||||||||||||
| 132 | 253 | my ( $self, @buf ) = @_; | |||||||||||||||||||
| 133 | 253 | my $fh = $self->{fh}; | |||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | 253 | ▶ | if ( scalar(@buf) > 1 ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 136 | 3 | for my $line (@buf) { | |||||||||||||||||||
| 137 | 12 | $fh->write($line); | |||||||||||||||||||
| 138 | 12 | $fh->write($/); | |||||||||||||||||||
| 139 | } | ||||||||||||||||||||
| 140 | } | ||||||||||||||||||||
| 141 | else { | ||||||||||||||||||||
| 142 | 250 | $fh->write( $buf[0] ); | |||||||||||||||||||
| 143 | } | ||||||||||||||||||||
| 144 | } | ||||||||||||||||||||
| 145 | |||||||||||||||||||||
| 146 | =head2 seek($offset) | ||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | Seek to the file position $offset. | ||||||||||||||||||||
| 149 | |||||||||||||||||||||
| 150 | Set the file pointer to the 5th byte. | ||||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | $file->seek(5); | ||||||||||||||||||||
| 153 | |||||||||||||||||||||
| 154 | =cut | ||||||||||||||||||||
| 155 | |||||||||||||||||||||
| 156 | 0 | ▶ | sub seek { | ||||||||||||||||||
|
Subroutine
seek: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 157 | 0 | my ( $self, $offset ) = @_; | |||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | 0 | my $fh = $self->{'fh'}; | |||||||||||||||||||
| 160 | 0 | $fh->seek($offset); | |||||||||||||||||||
| 161 | } | ||||||||||||||||||||
| 162 | |||||||||||||||||||||
| 163 | =head2 read($len) | ||||||||||||||||||||
| 164 | |||||||||||||||||||||
| 165 | Read $len bytes out of the filehandle. | ||||||||||||||||||||
| 166 | |||||||||||||||||||||
| 167 | my $content = $file->read(1024); | ||||||||||||||||||||
| 168 | |||||||||||||||||||||
| 169 | =cut | ||||||||||||||||||||
| 170 | |||||||||||||||||||||
| 171 | 778 | ▶ | sub read { | ||||||||||||||||||
|
Subroutine
read: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||
| 172 | 778 | my ( $self, $len ) = @_; | |||||||||||||||||||
| 173 | 778 | ▶ | $len = DEFAULT_READ_LEN if ( !$len ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 174 | |||||||||||||||||||||
| 175 | 778 | my $fh = $self->{'fh'}; | |||||||||||||||||||
| 176 | 778 | return $fh->read($len); | |||||||||||||||||||
| 177 | } | ||||||||||||||||||||
| 178 | |||||||||||||||||||||
| 179 | =head2 read_all | ||||||||||||||||||||
| 180 | |||||||||||||||||||||
| 181 | Read everything out of the filehandle. | ||||||||||||||||||||
| 182 | |||||||||||||||||||||
| 183 | my $content = $file->read_all; | ||||||||||||||||||||
| 184 | |||||||||||||||||||||
| 185 | =cut | ||||||||||||||||||||
| 186 | |||||||||||||||||||||
| 187 | 143 | ▶ | sub read_all { | ||||||||||||||||||
|
Subroutine
read_all: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||
| 188 | 143 | my ($self) = @_; | |||||||||||||||||||
| 189 | |||||||||||||||||||||
| 190 | 143 | my $all = ''; | |||||||||||||||||||
| 191 | 143 | ▶ | while ( my $in = $self->read() ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 192 | 635 | $all .= $in; | |||||||||||||||||||
| 193 | } | ||||||||||||||||||||
| 194 | 143 | ▶ | if (wantarray) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 195 | 6 | return split( /\n/, $all ); | |||||||||||||||||||
| 196 | } | ||||||||||||||||||||
| 197 | 137 | return $all; | |||||||||||||||||||
| 198 | } | ||||||||||||||||||||
| 199 | |||||||||||||||||||||
| 200 | =head2 close | ||||||||||||||||||||
| 201 | |||||||||||||||||||||
| 202 | Close the file. | ||||||||||||||||||||
| 203 | |||||||||||||||||||||
| 204 | $file->close; | ||||||||||||||||||||
| 205 | |||||||||||||||||||||
| 206 | =cut | ||||||||||||||||||||
| 207 | |||||||||||||||||||||
| 208 | 384 | ▶ | sub close { | ||||||||||||||||||
|
Subroutine
close: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||
| 209 | 384 | my ($self) = @_; | |||||||||||||||||||
| 210 | 384 | my $fh = $self->{'fh'}; | |||||||||||||||||||
| 211 | 384 | $fh->close; | |||||||||||||||||||
| 212 | } | ||||||||||||||||||||
| 213 | |||||||||||||||||||||
| 214 | 49 | 1; | |||||||||||||||||||