lib/File/Access/Driver.pm

filter: Statementstmt 54.1% 255 / 471 Branchbran 32.6% 98 / 300 Conditioncond 35.7% 5 / 14 MC/DCmcdc 12.5% 1 / 8 Subroutinesub 71.7% 33 / 46 Podpod 25.0% 9 / 36 totaltotal 45.8% 401 / 875 SCAR 83.2
CC
157
Cov
46%
_openaFile
240.0
appendLine
240.0
changeFileName
132.0
CRAP 4124.1
↑ Summary
1 #
2 # @author Bodo (Hugo) Barwich
3 # @version 2026-03-21
4 # @package File Access Driver
5 # @subpackage lib/File/Access/Driver.pm
6  
7 # This module defines the a class to interact with files
8 #
9 #---------------------------------
10 # Requirements:
11 #
12 #---------------------------------
13 # Extensions:
14 #
15 #---------------------------------
16 # Configurations:
17 #
18 #---------------------------------
19 # Features:
20  
21 #==============================================================================
22 # The File::Access::Driver Package
23  
24 =head1 NAME
25  
26 File::Access::Driver - Convenient File Access with "Batteries included"
27  
28 =cut
29  
30 package File::Access::Driver;
31  
32 1 our $VERSION = '1.0.2';
33  
34 #----------------------------------------------------------------------------
35 #Dependencies
36  
37 1 ▶use Fcntl ':flock'; # import LOCK_* constants
Subroutine BEGIN: called CC=1 SCAR=0.0
38 1 ▶use File::Path qw(make_path);
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 =head1 DESCRIPTION
41  
42 C<File::Access::Driver> is a class for convenient file access designed to reduce
43 the code needed to interact with files.
44  
45 It has grown to a "I<Batteries included>" solution covering the most file access
46 use cases.
47  
48 It will not produce exceptions but instead report the errors over the C<getErrorCode()>
49 and the C<getErrorString()> methods.
50  
51 =head1 SYNOPSIS
52  
53 The C<File::Access::Driver> can be used as seen in the "I<File Write>" test:
54  
55 use File::Access::Driver;
56  
57 my $driver = File::Access::Driver->new( 'filepath' => $spath . 'files/out/testfile_out.txt' );
58  
59 # Make sure the file does not exist
60 is( $driver->Delete(), 1, "File Delete: Delete operation 1 correct" );
61 is( $driver->Exists(), 0, "File Exist: File does not exist anymore" );
62  
63 $driver->writeContent(q(This is the multi line content for the test file.
64  
65 It will be written into the test file.
66 The file should only contain this text.
67 Also the file should be created.
68 ));
69  
70 printf(
71 "Test File Exists - File '%s': Write finished with [%d]\n",
72 $driver->getFileName(),
73 $driver->getErrorCode()
74 );
75 printf(
76 "Test File Exists - File '%s': Write Report:\n'%s'\n",
77 $driver->getFileName(),
78 ${ $driver->getReportString() }
79 );
80 printf(
81 "Test File Exists - File '%s': Write Error:\n'%s'\n",
82 $driver->getFileName(),
83 ${ $driver->getErrorString() }
84 );
85  
86 is( $driver->getErrorCode(), 0, "Write Error Code: No errors have occurred" );
87 is( ${ $driver->getErrorString() }, '', "Write Error Message: No errors are reported" );
88  
89 is( $driver->Exists(), 1, "File Exist: File does exist now" );
90 isnt( $driver->getFileSize(), 0, "File Size: File is not empty anymore" );
91  
92 =cut
93  
94 #----------------------------------------------------------------------------
95 #Constructors
96  
97 =head1 METHODS
98  
99 =head2 Constructor
100  
101 =head3 new ( [ CONFIGURATIONS ] )
102  
103 This is the constructor for a new C<File::Access::Driver> object.
104  
105 B<Parameters:>
106  
107 =over 4
108  
109 =item C<CONFIGURATIONS>
110  
111 Key and value pairs containing the configurations.
112  
113 B<Recognized fields:>
114  
115 C<filedirectory> - The directory where the file is located.
116  
117 C<filename> - The base name of the file.
118  
119 C<filepath> - The complete path with directory and file base name.
120  
121 =back
122  
123 See L<Method C<setFileDirectory()>|/"setFileDirectory ( DIRECTORY )">
124  
125 See L<Method C<setFileName()>|/"setFileName ( NAME )">
126  
127 =cut
128  
129 sub new {
130  
131 #Take the Method Parameters
132 6 ▶ my ( $invocant, %hshprms ) = @_;
Subroutine new: called CC=6 SCAR=18.0
Pod documented
133 6 ▶ my $class = ref($invocant) || $invocant;
Condition33%1/3
ref $invocant || $invocant
l!l&&r!l&&!r
060
MC/DC0%0/2
ref $invocant || $invocant
ref $invocant $invocant
Truth table33%1/3
A || B
A= ref $invocantB= $invocant
ABresult
1X1
011
000
134 6 my $self = undef;
135  
136 # Set the Default Attributes and assign the initial Values
137 6 $self = {
138 '_file' => undef,
139 '_directory_name' => '',
140 '_file_name' => '',
141  
142 # A Reference to the Content Text
143 '_file_content' => undef,
144 '_file_content_lines' => undef,
145 '_file_time' => -1,
146 '_file_access_time' => -1,
147 '_file_size' => -1,
148 '_package_size' => 32768,
149 '_buffered' => 0,
150 '_persistent' => 0,
151 '_locked' => 0,
152 '_writable' => 0,
153 '_appendable' => 0,
154 '_report' => '',
155 '_error_message' => '',
156 '_error_code' => 0
157 };
158  
159 6 bless $self, $class;
160  
161 6 ▶ if ( scalar( keys %hshprms ) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %hshprms > 0) 5 1 6
162 5 ▶ $self->setFileDirectory( $hshprms{'filedirectory'} ) if ( defined $hshprms{'filedirectory'} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $hshprms{"filedirectory"} 1 4 5
163 5 ▶ $self->setFileName( $hshprms{'filename'} ) if ( defined $hshprms{'filename'} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $hshprms{"filename"} 1 4 5
164 5 ▶ $self->setFilePath( $hshprms{'filepath'} ) if ( defined $hshprms{'filepath'} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $hshprms{"filepath"} 4 1 5
165 } #if(scalar(keys %hshprms) > 0)
166  
167 #Give the Object back
168 6 return $self;
169 }
170  
171 sub DESTROY {
172 6 ▶ my $self = $_[0];
Subroutine DESTROY: called CC=1 SCAR=0.0
173  
174 # Free the System Resources
175 6 $self->freeResources;
176 }
177  
178 #----------------------------------------------------------------------------
179 #Administration Methods
180  
181 =head3 setFileDirectory ( DIRECTORY )
182  
183 This method sets the directory where the file is located.
184  
185 B<Parameters:>
186  
187 =over 4
188  
189 =item C<DIRECTORY>
190  
191 The directory where the file is located.
192  
193 If the directory does not end on a slash C< / > it will be appended.
194  
195 =back
196  
197 =cut
198  
199 sub setFileDirectory {
200 5 ▶ my $self = $_[0];
Subroutine setFileDirectory: called CC=5 SCAR=16.8
Pod documented
201  
202 5 ▶ if ( scalar(@_) > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ > 1) { } 5 0 5
203 5 $self->{'_directory_name'} = $_[1];
204 }
205 else #No Parameter given
206 {
207 0 $self->{'_directory_name'} = '';
208 }
209  
210 5 ▶ $self->{'_directory_name'} = '' unless ( defined $self->{'_directory_name'} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"_directory_name"} 0 5 5
211  
212 5 ▶ if ( $self->{'_directory_name'} ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_directory_name"} ne "") 5 0 5
213 5 ▶ $self->{'_directory_name'} .= '/' unless ( $self->{'_directory_name'} =~ qr#/$# );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_directory_name"} =~ qr"/$" 1 4 5
214 }
215  
216 #Clear the File Object
217 5 $self->Clear;
218 }
219  
220 =head3 setFileName ( NAME )
221  
222 This method sets the base name of the file.
223  
224 B<Parameters:>
225  
226 =over 4
227  
228 =item C<NAME>
229  
230 The base name of the file.
231  
232 This will also close open file handles and free in-memory cache.
233  
234 =back
235  
236 See L<Method C<Clear()>|/"Clear ()">
237  
238 =cut
239  
240 sub setFileName {
241 5 ▶ my $self = $_[0];
Subroutine setFileName: called CC=3 SCAR=11.8
Pod documented
242  
243 5 ▶ if ( scalar(@_) > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ > 1) { } 5 0 5
244 5 $self->{'_file_name'} = $_[1];
245 }
246 else #No Parameter given
247 {
248 0 $self->{'_file_name'} = '';
249 }
250  
251 5 ▶ $self->{'_file_name'} = '' unless ( defined $self->{'_file_name'} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"_file_name"} 0 5 5
252  
253 # Clear the File Object
254 5 $self->Clear;
255 }
256  
257 =head3 setFilePath ( PATH )
258  
259 This method sets the complete path of the file.
260  
261 B<Parameters:>
262  
263 =over 4
264  
265 =item C<PATH>
266  
267 The complete path of the file.
268  
269 This will split the C<PATH> and call C<setFileDirectory()> with the directory
270 and C<setFileName()> with the file base name.
271  
272 =back
273  
274 See L<Method C<setFileDirectory()>|/"setFileDirectory ( DIRECTORY )">
275  
276 See L<Method C<setFileName()>|/"setFileName ( NAME )">
277  
278 =cut
279  
280 sub setFilePath {
281 4 ▶ my $self = $_[0];
Subroutine setFilePath: called CC=5 SCAR=17.4
Pod documented
282 4 ▶ my $sdirnm = $_[1] || '';
Condition50%1/2
$_[1] || ''
l!l
40
MC/DC0%0/1
$_[1] || ''
$_[1]
Truth table50%1/2
A
A= $_[1]
Aresult
11
00
283 4 my $sflnm = '';
284  
285 4 ▶ if ( $sdirnm ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sdirnm ne "") 4 0 4
286 4 ▶ if ( index( $sdirnm, '/' ) > -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($sdirnm, '/') != -1) { } 4 0 4
287 4 ▶ if ( $sdirnm =~ qr#(.*/)([^/]+)$# ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sdirnm =~ qr"(.*/)([^/]+)$") 4 0 4
288 4 $sdirnm = $1;
289 4 $sflnm = $2;
290 }
291 }
292 else #The Path does not include a Slash Sign
293 {
294 0 $sflnm = $sdirnm;
295 0 $sdirnm = '';
296 }
297 }
298  
299 #Set the Parsed Values
300 4 $self->setFileDirectory($sdirnm);
301 4 $self->setFileName($sflnm);
302 }
303  
304 sub changeFileName {
305 0 ▶ my $self = $_[0];
Subroutine changeFileName: not called CC=11 SCAR=48.8
Pod undocumented
306 0 my $sdirnm = '';
307 0 ▶ my $sflnm = $_[1] || '';
Condition0%0/2
$_[1] || ''
l!l
00
MC/DC0%0/1
$_[1] || ''
$_[1]
Truth table0%0/2
A
A= $_[1]
Aresult
11
00
308 0 my $irs = 0;
309  
310 0 ▶ if ( $sflnm ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sflnm ne '') { } 0 0 0
311  
312 #If File Name contains Directory Information
313 0 ▶ if ( index( $sflnm, '/' ) > -1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($sflnm, "/") != -1) 0 0 0
314 0 ▶ if ( $sflnm =~ qr#(.*/)([^/]+)$# ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sflnm =~ qr"(.*/)([^/]+)$") 0 0 0
315 0 $sdirnm = $1;
316 0 $sflnm = $2;
317 }
318 }
319  
320 #Assume the same Directory
321 0 ▶ $sdirnm = $self->{'_directory_name'}
Branch: 1 branch
BranchTrueFalseTotal
if $sdirnm eq "" 0 0 0
322 if ( $sdirnm eq '' );
323  
324 0 ▶ if ( $self->Exists ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->Exists) { } 0 0 0
325 0 $irs = rename $self->{'_directory_name'} . $self->{'_file_name'}, $sdirnm . $sflnm;
326  
327 0 ▶ if ($irs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($irs) { } 0 0 0
328  
329 #Update the File Access Object accordingly but keep the Report History
330  
331 0 $self->{'_directory_name'} = $sdirnm;
332 0 $self->{'_file_name'} = $sflnm;
333 }
334 else #Change File Name failed
335 {
336 0 $irs = 0 + $!;
337  
338 $self->{'_error_message'} .=
339 "File '"
340 . $self->{'_directory_name'}
341 0 . $self->{'_file_name'}
342 . "': Change File Name failed with [$irs]!"
343 . "Message: '$!'\n";
344 0 ▶ $self->{'_error_code'} = 1 if ( $self->{'_error_code'} < 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_error_code"} < 1 0 0 0
345  
346 0 $irs = 0;
347 }
348 }
349 else #File does not exist
350 {
351 0 $self->{'_error_message'} .= "File does not exist!\n";
352 0 ▶ $self->{'_error_code'} = 3 if ( $self->{'_error_code'} < 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_error_code"} < 1 0 0 0
353 }
354 }
355 else #New File Name was not given
356 {
357 0 $self->{'_error_message'} .= "New File Name is not set!\n";
358 0 ▶ $self->{'_error_code'} = 3 if ( $self->{'_error_code'} < 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_error_code"} < 1 0 0 0
359 }
360  
361 0 return $irs;
362 }
363  
364 sub setContent {
365 3 ▶ my $self = $_[0];
Subroutine setContent: called CC=5 SCAR=17.1
Pod undocumented
366  
367 3 $self->{'_file_content'} = undef;
368 3 $self->{'_file_content_lines'} = undef;
369 3 ▶ $self->{'_buffered'} ||= 1;
Condition100%2/2
$self->{"_buffered"} ||= 1
l!l
12
MC/DC100%1/1
$self->{"_buffered"} ||= 1
$self->{"_buffered"}
Truth table100%2/2
A
A= $self->{"_buffered"}
Aresult
11
00
370  
371 3 ▶ if ( scalar(@_) > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ > 1) 3 0 3
372 3 ▶ if ( ref( $_[1] ) eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[1] eq '') { } 3 0 3
373 3 $self->{'_file_content'} = \$_[1];
374 }
375 else {
376 0 $self->{'_file_content'} = $_[1];
377 }
378 }
379  
380 0 ${ $self->{'_file_content'} } = ''
381 3 ▶ unless ( defined $self->{'_file_content'} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"_file_content"} 0 3 3
382  
383 }
384  
385 sub setContentArray {
386 1 ▶ my $self = $_[0];
Subroutine setContentArray: called CC=4 SCAR=14.8
Pod undocumented
387  
388 1 $self->{'_file_content'} = \'';
389 1 $self->{'_file_content_lines'} = undef;
390 1 ▶ $self->{'_buffered'} ||= 1;
Condition50%1/2
$self->{"_buffered"} ||= 1
l!l
10
MC/DC0%0/1
$self->{"_buffered"} ||= 1
$self->{"_buffered"}
Truth table50%1/2
A
A= $self->{"_buffered"}
Aresult
11
00
391  
392 1 ▶ if ( scalar(@_) > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ > 1) 1 0 1
393 1 ▶ if ( ref( $_[1] ) eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[1] eq '') { } 0 1 1
394 0 $self->{'_file_content_lines'} = \$_[1];
395 }
396 else {
397 1 $self->{'_file_content_lines'} = $_[1];
398 }
399 }
400 }
401  
402 sub setFileTime {
403 0 ▶ my $self = $_[0];
Subroutine setFileTime: not called CC=6 SCAR=37.4
Pod undocumented
404 0 ▶ my $itmfl = $_[1] || time;
Condition0%0/3
$_[1] || time
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[1] || time
$_[1] time
Truth table0%0/3
A || B
A= $_[1]B= time
ABresult
1X1
011
000
405 0 my $irs = 0;
406  
407 0 ▶ if ( $self->Exists ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->Exists) { } 0 0 0
408 0 $self->getFileTime;
409  
410 0 $irs = utime $self->{'_file_access_time'}, $itmfl, $self->{'_directory_name'} . $self->{'_file_name'};
411  
412 0 ▶ if ( $irs < 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($irs < 1) 0 0 0
413 0 $irs = 0 + $!;
414  
415 $self->{'_error_message'} .=
416 "File '"
417 . $self->{'_directory_name'}
418 0 . $self->{'_file_name'}
419 . "': Set File Time failed with [$irs]!"
420 . "Message: '$!'\n";
421 0 ▶ $self->{'_error_code'} = 1 if ( $self->{'_error_code'} < 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_error_code"} < 1 0 0 0
422  
423 0 $irs = 0;
424 }
425 }
426 else #File does not exist
427 {
428 0 $self->{'_error_message'} .= "File does not exist!\n";
429 0 ▶ $self->{'_error_code'} = 3 if ( $self->{'_error_code'} < 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_error_code"} < 1 0 0 0
430 }
431  
432 0 return $irs;
433 }
434  
435 sub setBuffered {
436 0 ▶ my $self = $_[0];
Subroutine setBuffered: not called CC=4 SCAR=30.0
Pod undocumented
437  
438 0 ▶ if ( scalar(@_) > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ > 1) { } 0 0 0
439 0 ▶ if ( $_[1] =~ qr/^\d+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[1] =~ qr/^\d+$/) { } 0 0 0
440  
441 # The parameter is an unsigned whole number
442  
443 0 ▶ if ( $_[1] != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[1] != 0) { } 0 0 0
444 0 $self->{'_buffered'} = 1;
445 }
446 else {
447 0 $self->{'_buffered'} = 0;
448 }
449 }
450 else #The Parameter is not a Number
451 {
452 0 $self->{'_buffered'} = 0;
453 }
454 }
455 else #No Parameter was given
456 {
457 0 $self->{'_buffered'} = 1;
458 }
459 }
460  
461 sub setPersistent {
462 0 ▶ my $self = $_[0];
Subroutine setPersistent: not called CC=4 SCAR=30.0
Pod undocumented
463  
464 0 ▶ if ( scalar(@_) > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ > 1) { } 0 0 0
465 0 ▶ if ( $_[1] =~ qr/^\d+$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[1] =~ qr/^\d+$/) { } 0 0 0
466  
467 # The parameter is an unsigned whole number
468  
469 0 ▶ if ( $_[1] != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[1] != 0) { } 0 0 0
470 0 $self->{'_persistent'} = 1;
471 }
472 else {
473 0 $self->{'_persistent'} = 0;
474 }
475 }
476 else #The Parameter is not a Number
477 {
478 0 $self->{'_persistent'} = 0;
479 }
480 }
481 else #No Parameter was given
482 {
483 0 $self->{'_persistent'} = 1;
484 }
485 }
486  
487 =head3 Create ()
488  
489 This method will create an empty file.
490  
491 If the file already exists it will be truncated.
492  
493 =cut
494  
495 sub Create {
496 1 ▶ my $self = $_[0];
Subroutine Create: called CC=1 SCAR=0.0
Pod documented
497  
498 1 return $self->writeContent('');
499 }
500  
501 sub _openrFile {
502 1 ▶ my $self = $_[0];
Subroutine _openrFile: called CC=9 SCAR=30.0
503 1 my $irs = 0;
504  
505 1 ▶ if ( $self->_isOpen() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_isOpen) 0 1 1
506  
507 #Reopen the File in shared Reading Mode
508 0 ▶ $self->_closeFile() if ( $self->_isWritable() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_isWritable 0 0 0
509 } #if($self->_isOpen())
510  
511 1 ▶ unless ( $self->_isOpen() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_isOpen) { } 0 1 1
512 1 ▶ if ( $self->Exists() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->Exists) { } 1 0 1
513  
514 #Open the File
515 1 $irs = open $self->{'_file'}, '<', $self->{'_directory_name'} . $self->{'_file_name'};
516  
517 1 ▶ if ( defined $irs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $irs) { } 1 0 1
518 1 $irs = flock( $self->{'_file'}, LOCK_SH );
519  
520 1 ▶ if ($irs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($irs) 1 0 1
521 1 $self->{'_locked'} = 1;
522  
523 1 $irs = 1;
524 } #if($irs)
525 }
526 else #The File could not be opened
527 {
528 $self->{'_error_message'} .=
529 "File '"
530 . $self->{'_directory_name'}
531 0 . $self->{'_file_name'} . "': "
532 . "Open Read failed!\n"
533 . "Message: '$!'\n";
534 0 ▶ $self->{'_error_code'} = 1 if ( $self->{'_error_code'} < 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_error_code"} < 1 0 0 0
535  
536 0 $irs = 0;
537 } #unless(defined $irs)
538 }
539 else #The File does not exist
540 {
541 $self->{'_error_message'} .=
542 "File '"
543 . $self->{'_directory_name'}
544 0 . $self->{'_file_name'} . "': "
545 . "Open Read failed!\n"
546 . "Message: 'File does not exist.'\n";
547 0 ▶ $self->{'_error_code'} = 3 if ( $self->{'_error_code'} < 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_error_code"} < 1 0 0 0
548 }
549 }
550 else #The File is open and in Reading Mode
551 {
552 0 $irs = 1;
553 }
554  
555 1 return $irs;
556 }
557  
558 sub _openwFile {
559 4 ▶ my $self = $_[0];
Subroutine _openwFile: called CC=14 SCAR=33.7
560 4 my $irs = 0;
561  
562 4 ▶ unless ( $self->_isWritable() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_isWritable) { } 0 4 4
563 4 ▶ $self->_closeFile() if ( $self->_isOpen() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_isOpen 0 4 4
564  
565 4 ▶ if ( $self->{'_directory_name'} ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_directory_name'} ne '') { } 4 0 4
566 4 ▶ unless ( -d $self->{'_directory_name'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $self->{'_directory_name'}) { } 3 1 4
567 1 my $idircnt = -1;
568  
569 $self->{"_report"} .=
570 "Directory '"
571 1 . $self->{'_directory_name'} . "': "
572 . "Directory does not exist. Directory creating ...\n";
573  
574 #Clear any previous Error Messages
575 1 $@ = '';
576  
577 1 eval {
578 #Create the Directory
579 1 $idircnt = make_path( $self->{'_directory_name'}, { mode => 0775 } );
580 };
581  
582 1 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 1 1
583 $self->{'_error_message'} .=
584 "Directory '"
585 0 . $self->{'_directory_name'} . "': "
586 . "Create Directory failed.\n"
587 . "Message (Code '"
588 . ( $! + 0 )
589 . "'): '$@'\n";
590  
591 0 ▶ $self->{'_error_code'} = 1 if ( $self->{'_error_code'} < 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_error_code"} < 1 0 0 0
592  
593 0 $idircnt = -1;
594 }
595  
596 1 ▶ $irs = 1 if ( $idircnt != -1 );
Branch: 1 branch
BranchTrueFalseTotal
if $idircnt != -1 1 0 1
597 }
598 else # Directory does exist
599 {
600 3 $irs = 1;
601 }
602 }
603 else #The File Directory is not set
604 {
605 0 $irs = 1;
606 }
607  
608 4 ▶ if ($irs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($irs) 4 0 4
609 4 ▶ if ( $self->{'_file_name'} ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_file_name'} ne '') { } 4 0 4
610  
611 #Open the File
612 4 $irs = open $self->{'_file'}, ">", $self->{'_directory_name'} . $self->{'_file_name'};
613  
614 4 ▶ if ( defined $irs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $irs) { } 4 0 4
615 4 $self->{'_writable'} = 1;
616  
617 4 $irs = flock( $self->{'_file'}, LOCK_EX );
618  
619 4 ▶ if ($irs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($irs) 4 0 4
620 4 $self->{'_locked'} = 1;
621  
622 4 $irs = 1;
623 }
624 }
625 else #The File could not be opened
626 {
627 $self->{'_error_message'} .=
628 "File '"
629 . $self->{'_directory_name'}
630 0 . $self->{'_file_name'}
631 . "': Open Write failed!\n"
632 . "Message: '$!'\n";
633 0 ▶ $self->{'_error_code'} = 1 if ( $self->{'_error_code'} < 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_error_code"} < 1 0 0 0
634  
635 0 $irs = 0;
636 }
637 }
638 else #The File Name isnt set
639 {
640 0 $self->{'_error_message'} .= "File Name isn't set!\n";
641 0 ▶ $self->{'_error_code'} = 3 if ( $self->{'_error_code'} < 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_error_code"} < 1 0 0 0
642  
643 0 $irs = 0;
644 }
645 }
646 }
647 else #The File is already open in Appending Mode
648 {
649 0 $irs = 1;
650 } #unless($self->_isWritable())
651  
652 4 return $irs;
653 }
654  
655 sub _openaFile {
656 0 ▶ my $self = $_[0];
Subroutine _openaFile: not called CC=15 SCAR=54.8
657 0 my $irs = 0;
658  
659 0 ▶ unless ( $self->_isAppendable() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_isAppendable) { } 0 0 0
660 0 ▶ $self->_closeFile() if ( $self->_isOpen() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_isOpen 0 0 0
661  
662 0 ▶ if ( $self->{'_directory_name'} ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_directory_name'} ne '') { } 0 0 0
663 0 ▶ unless ( -d $self->{'_directory_name'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-d $self->{'_directory_name'}) { } 0 0 0
664 0 my $idircnt = -1;
665  
666 #Clear any previous Error Messages
667 0 $@ = '';
668  
669 0 eval {
670 #Create the Directory
671 0 $idircnt = make_path( $self->{'_directory_name'}, { mode => 0775 } );
672 };
673  
674 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
675 $self->{'_error_message'} .=
676 "Directory '"
677 0 . $self->{'_directory_name'} . "': "
678 . "Create Directory failed.\n"
679 . "Message (Code '"
680 . ( 0 + $! )
681 . "'): '$@'\n";
682  
683 0 ▶ $self->{'_error_code'} = 1 if ( $self->{'_error_code'} < 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_error_code"} < 1 0 0 0
684  
685 0 $idircnt = -1;
686 }
687  
688 0 ▶ $irs = 1 if ( $idircnt != -1 );
Branch: 1 branch
BranchTrueFalseTotal
if $idircnt != -1 0 0 0
689 }
690 else # Directory does exist
691 {
692 0 $irs = 1;
693 }
694 }
695 else #The File Directory is not set
696 {
697 0 $irs = 1;
698 }
699  
700 0 ▶ if ($irs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($irs) 0 0 0
701 0 ▶ if ( $self->{'_file_name'} ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_file_name'} ne '') { } 0 0 0
702  
703 #Open the File
704 0 $irs = open $self->{'_file'}, ">>", $self->{'_directory_name'} . $self->{'_file_name'};
705  
706 0 ▶ if ( defined $irs ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $irs) { } 0 0 0
707 0 $self->{'_writable'} = 1;
708 0 $self->{'_appendable'} = 1;
709  
710 0 $irs = flock( $self->{'_file'}, LOCK_EX );
711  
712 0 ▶ $irs = seek( $self->{'_file'}, 0, SEEK_END ) if ($irs);
Branch: 1 branch
BranchTrueFalseTotal
if $irs 0 0 0
713  
714 0 ▶ if ($irs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($irs) 0 0 0
715 0 $self->{'_locked'} = 1;
716  
717 0 $irs = 1;
718 }
719 }
720 else #The File could not be opened
721 {
722 $self->{'_error_message'} .=
723 "File '"
724 . $self->{'_directory_name'}
725 0 . $self->{'_file_name'}
726 . "': Open Append failed!\n"
727 . "Message: '$!'\n";
728 0 ▶ $self->{'_error_code'} = 1 if ( $self->{'_error_code'} < 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_error_code"} < 1 0 0 0
729  
730 0 $irs = 0;
731 }
732 }
733 else #The File Name isnt set
734 {
735 0 $self->{'_error_message'} .= "File Name isn't set!\n";
736 0 ▶ $self->{'_error_code'} = 3 if ( $self->{'_error_code'} < 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_error_code"} < 1 0 0 0
737 }
738 }
739 }
740 else #The File is already open in Appending Mode
741 {
742 0 $irs = 1;
743 } #unless($self->_isAppendable())
744  
745 0 return $irs;
746 }
747  
748 sub Read {
749 1 ▶ my $self = $_[0];
Subroutine Read: called CC=14 SCAR=30.2
Pod undocumented
750 1 my $irs = 0;
751  
752 1 ${ $self->{'_file_content'} } = '';
753 1 $self->{'_file_content_lines'} = undef;
754 1 ▶ $self->{'_buffered'} = 1 unless ( $self->{'_buffered'} );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_buffered"} 1 0 1
755 1 $self->{'_file_time'} = -1;
756 1 $self->{'_file_size'} = -1;
757  
758 1 ▶ $self->_openrFile() if ( $self->_isWritable() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_isWritable 0 1 1
759  
760 1 ▶ $self->_openrFile() unless ( $self->_isOpen() );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_isOpen 1 0 1
761  
762 1 ▶ if ( $self->_isOpen() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_isOpen) 1 0 1
763 1 my $scntntln = '';
764 1 my $irdcnt = -1;
765  
766 do #while($irdcnt);
767 1 ▶ {
Branch: 1 branch
BranchTrueFalseTotal
$irdcnt and do { do { $irdcnt = sysread($self->{"_file"}, $scntntln, $self->{"_package_size"}); if (defined $irdcnt) { ${$$self{"_file_content"};} .= $scntntln if $irdcnt > 0; } else { $self->{"_error_message"} .= q[File '] . $self->{"_directory_name"} . $self->{"_file_name"} . q(': Read File failed with [) . (0 + $!) . ']!!' . "Message: '$!'\n"; $self->{"_error_code"} = 1 if $self->{"_error_code"} < 1; } } } 1 1 2
768 2 $irdcnt = sysread( $self->{'_file'}, $scntntln, $self->{"_package_size"} );
769  
770 2 ▶ if ( defined $irdcnt ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $irdcnt) { } 2 0 2
771 2 ▶ ${ $self->{'_file_content'} } .= $scntntln if ( $irdcnt > 0 );
Branch: 1 branch
BranchTrueFalseTotal
if $irdcnt > 0 1 1 2
772 }
773 else #An Error has ocurred
774 {
775 $self->{'_error_message'} .=
776 "File '"
777 . $self->{'_directory_name'}
778 0 . $self->{'_file_name'}
779 . "': Read File failed with ["
780 . ( 0 + $! ) . "]!!"
781 . "Message: '$!'\n";
782 0 ▶ $self->{'_error_code'} = 1 if ( $self->{'_error_code'} < 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_error_code"} < 1 0 0 0
783 }
784 } while ($irdcnt);
785  
786 1 ▶ $irs = 1 unless ($!);
Branch: 1 branch
BranchTrueFalseTotal
unless $! 1 0 1
787  
788 1 ▶ if ($irs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($irs) 1 0 1
789 1 my @arrflstt = stat( $self->{'_file'} );
790  
791 1 ▶ if ( scalar(@arrflstt) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @arrflstt > 0) { } 1 0 1
792 1 $self->{'_file_time'} = $arrflstt[9];
793 1 $self->{'_file_size'} = $arrflstt[7];
794 }
795 else #The File Attributes are empty
796 {
797 $self->{'_error_message'} .=
798 0 "File '" . $self->{'_directory_name'} . $self->{'_file_name'} . "': File Attributes failed!\n";
799 }
800 }
801  
802 1 ▶ if ( $self->{'_persistent'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_persistent'}) { } 0 1 1
803 0 ▶ unless ($irs) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($irs) 0 0 0
804 $self->{'_error_message'} .=
805 "File '"
806 . $self->{'_directory_name'}
807 0 . $self->{'_file_name'} . "': "
808 . "File closing because of Reading Error ...\n";
809  
810 #Close the File
811 0 $self->_closeFile();
812 }
813 }
814 else #File is not persistent
815 {
816 #Close the File
817 1 $self->_closeFile();
818 }
819 }
820  
821 1 return $irs;
822 }
823  
824 sub readContent {
825 0 ▶ my $self = $_[0];
Subroutine readContent: not called CC=1 SCAR=6.9
Pod undocumented
826  
827 0 $self->Read();
828  
829 0 return $self->getContent;
830 }
831  
832 sub readContentArray {
833 0 ▶ my $self = $_[0];
Subroutine readContentArray: not called CC=1 SCAR=6.9
Pod undocumented
834  
835 0 File::Access::Driver::Read $self;
836  
837 0 return $self->getContentArray;
838 }
839  
840 =head3 Truncate ()
841  
842 This method will empty an existing file.
843  
844 If the file does not exist it will be created.
845  
846 =cut
847  
848 sub Truncate {
849 1 ▶ my $self = $_[0];
Subroutine Truncate: called CC=1 SCAR=0.0
Pod documented
850  
851 1 return $self->writeContent('');
852 }
853  
854 sub Write {
855 4 ▶ my $self = $_[0];
Subroutine Write: called CC=12 SCAR=28.9
Pod undocumented
856 4 my $irs = 0;
857  
858 4 ▶ $self->{'_buffered'} = 1 unless ( $self->{'_buffered'} );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"_buffered"} 0 4 4
859 4 $self->{'_file_time'} = -1;
860 4 $self->{'_file_access_time'} = -1;
861 4 $self->{'_file_size'} = -1;
862  
863 4 ▶ unless ( $self->_isWritable() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_isWritable) 4 0 4
864 4 ▶ $self->_closeFile() if ( $self->_isOpen() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_isOpen 0 4 4
865  
866 #Open the File in Write Mode
867 4 $self->_openwFile();
868 }
869  
870 4 ▶ if ( $self->_isWritable() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_isWritable) 4 0 4
871 4 my $iwrtcnt = -1;
872 4 my $content = $self->getContent();
873 4 my $icntntlen = length( ${$content} );
874  
875 4 $irs = 0;
876  
877 # Write the Content Line to the File
878 4 $iwrtcnt = syswrite( $self->{'_file'}, ${$content} );
879  
880 4 ▶ if ( defined $iwrtcnt ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $iwrtcnt) { } 4 0 4
881 4 $irs = 1;
882  
883 4 ▶ if ( $iwrtcnt != $icntntlen ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($iwrtcnt != $icntntlen) 0 4 4
884 $self->{'_error_message'} .=
885 "File '"
886 . $self->{'_directory_name'}
887 0 . $self->{'_file_name'}
888 . "': '$iwrtcnt' from '$icntntlen' Bytes written.\n";
889 }
890 }
891 else # An Error has ocurred
892 {
893 $self->{'_error_message'} .=
894 "File '"
895 . $self->{'_directory_name'}
896 0 . $self->{'_file_name'}
897 . "': File Write failed!\n"
898 . "Message: '$!'\n";
899 0 ▶ $self->{'_error_code'} = 1 if ( $self->{'_error_code'} < 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_error_code"} < 1 0 0 0
900 }
901  
902 4 ▶ if ($irs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($irs) 4 0 4
903 4 my @arrflstt = stat( $self->{'_file'} );
904  
905 4 ▶ if ( scalar(@arrflstt) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @arrflstt > 0) { } 4 0 4
906 4 $self->{'_file_time'} = $arrflstt[9];
907 4 $self->{'_file_access_time'} = $arrflstt[8];
908 4 $self->{'_file_size'} = $arrflstt[7];
909 }
910 else #The File Attributes are empty
911 {
912 $self->{'_error_message'} .=
913 0 "File '" . $self->{'_directory_name'} . $self->{'_file_name'} . "': File Attributes failed!\n";
914 }
915 }
916  
917 4 ▶ if ( $self->{'_persistent'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_persistent'}) { } 0 4 4
918 0 ▶ unless ($irs) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($irs) 0 0 0
919 $self->{'_error_message'} .=
920 "File '"
921 . $self->{'_directory_name'}
922 0 . $self->{'_file_name'} . "': "
923 . "File closing because of Writing Error ...\n";
924  
925 #Close the File
926 0 $self->_closeFile();
927 }
928 }
929 else #File is not persistent
930 {
931 #Close the File
932 4 $self->_closeFile();
933 }
934 }
935  
936 4 return $irs;
937 }
938  
939 sub writeContent {
940 3 ▶ my $self = $_[0];
Subroutine writeContent: called CC=1 SCAR=0.0
Pod undocumented
941  
942 3 $self->setContent( $_[1] );
943  
944 3 return File::Access::Driver::Write $self;
945 }
946  
947 sub appendLine {
948 0 ▶ my $self = $_[0];
Subroutine appendLine: not called CC=15 SCAR=54.8
Pod undocumented
949 0 my $rcntntln = undef;
950 0 my $irs = 0;
951  
952 0 $self->{'_file_time'} = -1;
953 0 $self->{'_file_size'} = -1;
954  
955 0 ▶ if ( scalar(@_) > 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ > 1) 0 0 0
956 0 ▶ if ( ref( $_[1] ) ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[1] ne '') { } 0 0 0
957 0 $rcntntln = $_[1];
958 }
959 else {
960 0 $rcntntln = \$_[1];
961 }
962  
963 0 ▶ if ( $$rcntntln ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($$rcntntln ne "") 0 0 0
964 0 ▶ if ( $self->{'_buffered'} > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_buffered"} > 0) 0 0 0
965 0 ${ $self->{'_file_content'} } .= $$rcntntln;
966 }
967 }
968 }
969  
970 0 ▶ unless ( $self->_isAppendable() ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_isAppendable) 0 0 0
971 0 ▶ $self->_closeFile() if ( $self->_isOpen() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_isOpen 0 0 0
972  
973 #Open the File in Appending Mode
974 0 $self->_openaFile();
975 }
976  
977 0 ▶ if ( $self->_isAppendable() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_isAppendable) 0 0 0
978 0 my $iwrtcnt = -1;
979 0 my $icntntlen = length($$rcntntln);
980  
981 0 $irs = 0;
982  
983 #Write the Content Line to the File
984 0 $iwrtcnt = syswrite( $self->{'_file'}, $$rcntntln );
985  
986 0 ▶ if ( defined $iwrtcnt ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $iwrtcnt) { } 0 0 0
987 0 $irs = 1;
988  
989 0 ▶ if ( $iwrtcnt != $icntntlen ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($iwrtcnt != $icntntlen) 0 0 0
990 $self->{'_error_message'} .=
991 "File '"
992 . $self->{'_directory_name'}
993 0 . $self->{'_file_name'} . "': "
994 . "'$iwrtcnt' from '$icntntlen' Bytes written.\n";
995 }
996 }
997 else #An Error has ocurred
998 {
999 $self->{'_error_message'} .=
1000 "File '"
1001 . $self->{'_directory_name'}
1002 0 . $self->{'_file_name'}
1003 . "': File Write failed!\n"
1004 . "Message: '$!'\n";
1005 0 ▶ $self->{'_error_code'} = 1 if ( $self->{'_error_code'} < 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_error_code"} < 1 0 0 0
1006 }
1007  
1008 0 ▶ if ($irs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($irs) 0 0 0
1009 0 my @arrflstt = stat( $self->{'_file'} );
1010  
1011 0 ▶ if ( scalar(@arrflstt) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @arrflstt > 0) { } 0 0 0
1012 0 $self->{'_file_time'} = $arrflstt[9];
1013 0 $self->{'_file_size'} = $arrflstt[7];
1014 }
1015 else #The File Attributes are empty
1016 {
1017 $self->{'_error_message'} .=
1018 0 "File '" . $self->{'_directory_name'} . $self->{'_file_name'} . "': File Attributes failed!\n";
1019 }
1020 }
1021  
1022 0 ▶ if ( $self->{'_persistent'} > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_persistent'} > 0) { } 0 0 0
1023 0 ▶ unless ( $irs > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($irs > 0) 0 0 0
1024 $self->{'_error_message'} .=
1025 "File '"
1026 . $self->{'_directory_name'}
1027 0 . $self->{'_file_name'} . "': "
1028 . "File closing because of Writing Error ...\n";
1029  
1030 #Close the File
1031 0 $self->_closeFile();
1032 }
1033 }
1034 else #File is not persistent
1035 {
1036 #Close the File
1037 0 $self->_closeFile();
1038 }
1039 } #if($self->_isAppendable())
1040  
1041 0 return $irs;
1042 }
1043  
1044 sub writeLine {
1045 0 ▶ my $self = $_[0];
Subroutine writeLine: not called CC=1 SCAR=6.9
Pod undocumented
1046  
1047 0 return $self->appendLine( $_[1] );
1048 }
1049  
1050 sub Delete {
1051 4 ▶ my $self = $_[0];
Subroutine Delete: called CC=5 SCAR=18.3
Pod undocumented
1052 4 my $irs = 0;
1053  
1054 #Close the Open File
1055 4 ▶ $self->_closeFile() if ( $self->_isOpen );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_isOpen 0 4 4
1056  
1057 4 ▶ if ( $self->Exists ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->Exists) { } 2 2 4
1058 2 $irs = unlink $self->{'_directory_name'} . $self->{'_file_name'};
1059  
1060 2 ▶ if ( $irs < 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($irs < 1) 0 2 2
1061 $self->{'_error_message'} .=
1062 "File '"
1063 . $self->{'_directory_name'}
1064 0 . $self->{'_file_name'}
1065 . "': Delete File failed with ["
1066 . ( 0 + $! ) . "]!"
1067 . "Message: '$!'\n";
1068 0 ▶ $self->{'_error_code'} = 1 if ( $self->{'_error_code'} < 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_error_code"} < 1 0 0 0
1069  
1070 0 $irs = 0;
1071 }
1072 }
1073 else #The File does not exist
1074 {
1075 2 $irs = 1;
1076 }
1077  
1078 4 return $irs;
1079 }
1080  
1081 sub _closeFile {
1082 5 ▶ my $self = $_[0];
Subroutine _closeFile: called CC=7 SCAR=23.0
1083 5 my $irs = 0;
1084  
1085 5 ▶ if ( $self->_isOpen() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_isOpen) { } 5 0 5
1086 5 ▶ if ( $self->{'_locked'} > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_locked"} > 0) 5 0 5
1087 5 $irs = flock( $self->{'_file'}, LOCK_UN );
1088  
1089 5 ▶ unless ($irs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($irs) { } 5 0 5
1090 $self->{'_error_message'} .=
1091 "File '"
1092 . $self->{'_directory_name'}
1093 0 . $self->{'_file_name'} . "': "
1094 . "Lock Release failed!\n"
1095 . "Message: '$!'\n";
1096 0 ▶ $self->{'_error_code'} = 1 if ( $self->{'_error_code'} < 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_error_code"} < 1 0 0 0
1097 }
1098 else #File Lock was released
1099 {
1100 5 $self->{'_locked'} = 0;
1101 }
1102 }
1103  
1104 5 $irs = close $self->{'_file'};
1105  
1106 5 ▶ unless ( $irs > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($irs > 0) 0 5 5
1107 $self->{'_error_message'} .=
1108 "File '"
1109 . $self->{'_directory_name'}
1110 0 . $self->{'_file_name'} . "': "
1111 . "Close failed!\n"
1112 . "Message: '$!'\n";
1113 0 ▶ $self->{'_error_code'} = 1 if ( $self->{'_error_code'} < 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_error_code"} < 1 0 0 0
1114 } #unless($irs > 0)
1115  
1116 5 $self->{'_file'} = undef;
1117 5 $self->{'_writable'} = 0;
1118 5 $self->{'_appendable'} = 0;
1119  
1120 #To Refresh on next Request
1121 5 $self->{'_file_access_time'} = -1;
1122 5 $self->{'_file_time'} = -1;
1123 5 $self->{'_file_size'} = -1;
1124  
1125 }
1126 else #The File is not open
1127 {
1128 0 $irs = 1;
1129 }
1130  
1131 5 return $irs;
1132 }
1133  
1134 =head3 Clear ()
1135  
1136 This method closed the file handle and frees the in-memory cache
1137 and resets also the in-memory file attributes.
1138  
1139 =cut
1140  
1141 sub Clear {
1142 10 ▶ my $self = $_[0];
Subroutine Clear: called CC=2 SCAR=7.0
Pod documented
1143  
1144 #Close the Open File
1145 10 ▶ $self->_closeFile() if ( $self->_isOpen() );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_isOpen 0 10 10
1146  
1147 #Clear Content
1148 10 $self->{'_file_content'} = undef;
1149 10 $self->{'_file_content_lines'} = undef;
1150  
1151 10 $self->clearErrors;
1152  
1153 #To Refresh on next Request
1154 10 $self->{'_file_access_time'} = -1;
1155 10 $self->{'_file_time'} = -1;
1156 10 $self->{'_file_size'} = -1;
1157  
1158 }
1159  
1160 =head3 clearErrors ()
1161  
1162 Clear the error and activity report and reset the error code.
1163  
1164 =cut
1165  
1166 sub clearErrors {
1167 11 ▶ my $self = $_[0];
Subroutine clearErrors: called CC=1 SCAR=0.0
Pod documented
1168  
1169 11 $self->{"_report"} = '';
1170 11 $self->{'_error_message'} = '';
1171 11 $self->{'_error_code'} = 0;
1172 }
1173  
1174 =head3 freeResources ()
1175  
1176 This method closed the file handle and frees the in-memory cache.
1177  
1178 =cut
1179  
1180 sub freeResources {
1181 6 ▶ my $self = $_[0];
Subroutine freeResources: called CC=2 SCAR=7.4
Pod documented
1182  
1183 6 ▶ if ( $self->_isOpen ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_isOpen) 0 6 6
1184  
1185 # Close the Open File
1186 0 $self->_closeFile();
1187 }
1188  
1189 #Clear Content
1190 6 $self->{'_file_content'} = undef;
1191 6 $self->{'_file_content_lines'} = undef;
1192 }
1193  
1194 #----------------------------------------------------------------------------
1195 #Consultation Methods
1196  
1197 sub getFileDirectory {
1198 3 ▶ return $_[0]->{'_directory_name'};
Subroutine getFileDirectory: called CC=1 SCAR=0.0
Pod undocumented
1199 }
1200  
1201 sub getFileName {
1202 14 ▶ return $_[0]->{'_file_name'};
Subroutine getFileName: called CC=1 SCAR=0.0
Pod undocumented
1203 }
1204  
1205 sub getFilePath {
1206 3 ▶ return $_[0]->{'_directory_name'} . $_[0]->{'_file_name'};
Subroutine getFilePath: called CC=1 SCAR=0.0
Pod undocumented
1207 }
1208  
1209 sub getContent {
1210 5 ▶ my $self = $_[0];
Subroutine getContent: called CC=4 SCAR=13.9
Pod undocumented
1211  
1212 $self->{'_file_content'} = \''
1213 5 ▶ unless ( defined $self->{'_file_content'} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"_file_content"} 0 5 5
1214  
1215 5 ▶ if ( ${ $self->{'_file_content'} } eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (${$self->{"_file_content"};} eq "") 3 2 5
1216 3 ▶ if ( scalar( @{ $self->{'_file_content_lines'} } ) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->{"_file_content_lines"};} > 0) 1 2 3
1217 1 my $content = join( "\n", @{ $self->{'_file_content_lines'} } );
1218  
1219 1 $self->{'_file_content'} = \$content;
1220 }
1221 }
1222  
1223 5 return $self->{'_file_content'};
1224 }
1225  
1226 sub getContentArray {
1227 1 ▶ my $self = $_[0];
Subroutine getContentArray: called CC=4 SCAR=14.1
Pod undocumented
1228  
1229 1 @{ $self->{'_file_content_lines'} } = ()
1230 1 ▶ unless ( defined $self->{'_file_content_lines'} );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"_file_content_lines"} 1 0 1
1231  
1232 1 ▶ if ( scalar( @{ $self->{'_file_content_lines'} } ) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->{"_file_content_lines"};} == 0) 1 0 1
1233 1 ▶ if ( ${ $self->{'_file_content'} } ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (${$self->{"_file_content"};} ne "") 1 0 1
1234 1 @{ $self->{'_file_content_lines'} } = split( "\n", ${ $self->{'_file_content'} } );
1235 }
1236 }
1237  
1238 1 return $self->{'_file_content_lines'};
1239 }
1240  
1241 sub getFileTime {
1242 0 ▶ my $self = $_[0];
Subroutine getFileTime: not called CC=5 SCAR=34.0
Pod undocumented
1243  
1244 0 ▶ if ( $self->Exists() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->Exists) { } 0 0 0
1245 0 ▶ if ( $self->{'_file_time'} < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_file_time"} < 0) 0 0 0
1246 0 my @arrflstt = stat( $self->{'_directory_name'} . $self->{'_file_name'} );
1247  
1248 0 $self->{'_file_time'} = -1;
1249 0 $self->{'_file_access_time'} = -1;
1250 0 $self->{'_file_size'} = -1;
1251  
1252 0 ▶ if ( scalar(@arrflstt) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @arrflstt > 0) { } 0 0 0
1253 0 $self->{'_file_time'} = $arrflstt[9];
1254 0 $self->{'_file_access_time'} = $arrflstt[8];
1255 0 $self->{'_file_size'} = $arrflstt[7];
1256 }
1257 else #The File Attributes are empty
1258 {
1259 $self->{'_error_message'} .=
1260 0 "File '" . $self->{'_directory_name'} . $self->{'_file_name'} . "': File Attributes failed!\n";
1261 0 ▶ $self->{'_error_code'} = 1 if ( $self->{'_error_code'} < 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_error_code"} < 1 0 0 0
1262 }
1263 }
1264 }
1265 else #The File does not exist
1266 {
1267 0 $self->{'_file_time'} = -1;
1268 0 $self->{'_file_access_time'} = -1;
1269 0 $self->{'_file_size'} = -1;
1270 }
1271  
1272 0 return $self->{'_file_time'};
1273 }
1274  
1275 sub getFileSize {
1276 5 ▶ my $self = $_[0];
Subroutine getFileSize: called CC=5 SCAR=18.9
Pod undocumented
1277  
1278 5 ▶ if ( $self->Exists() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->Exists) { } 5 0 5
1279 5 ▶ if ( $self->{'_file_size'} < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"_file_size"} < 0) 5 0 5
1280 5 my @arrflstt = stat( $self->{'_directory_name'} . $self->{'_file_name'} );
1281  
1282 5 $self->{'_file_time'} = -1;
1283 5 $self->{'_file_access_time'} = -1;
1284 5 $self->{'_file_size'} = -1;
1285  
1286 5 ▶ if ( scalar(@arrflstt) > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @arrflstt > 0) { } 5 0 5
1287 5 $self->{'_file_time'} = $arrflstt[9];
1288 5 $self->{'_file_access_time'} = $arrflstt[8];
1289 5 $self->{'_file_size'} = $arrflstt[7];
1290 }
1291 else #The File Attributes are empty
1292 {
1293 $self->{'_error_message'} .=
1294 0 "File '" . $self->{'_directory_name'} . $self->{'_file_name'} . "': File Attributes failed!\n";
1295 0 ▶ $self->{'_error_code'} = 1 if ( $self->{'_error_code'} < 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_error_code"} < 1 0 0 0
1296 }
1297 }
1298 }
1299 else #The File does not exist
1300 {
1301 0 $self->{'_file_time'} = -1;
1302 0 $self->{'_file_access_time'} = -1;
1303 0 $self->{'_file_size'} = -1;
1304 }
1305  
1306 5 return $self->{'_file_size'};
1307 }
1308  
1309 sub Exists {
1310 17 ▶ my $self = $_[0];
Subroutine Exists: called CC=8 SCAR=23.4
Pod undocumented
1311 17 my $irs = 0;
1312  
1313 17 ▶ unless ( $self->_isOpen() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_isOpen) { } 0 17 17
1314 17 ▶ if ( $self->{'_file_name'} ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_file_name'} ne '') { } 17 0 17
1315 17 ▶ if ( $self->{'_directory_name'} ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'_directory_name'} ne '') { } 17 0 17
1316 17 ▶ $irs = 1 if ( -d $self->{'_directory_name'} );
Branch: 1 branch
BranchTrueFalseTotal
if -d $self->{"_directory_name"} 15 2 17
1317 }
1318 else #The Directory Name isnt set
1319 {
1320 0 $irs = 1;
1321 }
1322  
1323 17 ▶ if ($irs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($irs) 15 2 17
1324 15 ▶ $irs = 0 unless ( -e $self->{'_directory_name'} . $self->{'_file_name'} );
Branch: 1 branch
BranchTrueFalseTotal
unless -e $self->{"_directory_name"} . $self->{"_file_name"} 3 12 15
1325 }
1326 }
1327 else #The File Name isnt set
1328 {
1329 0 $self->{'_error_message'} .= "File Name isn't set!\n";
1330 0 ▶ $self->{'_error_code'} = 3 if ( $self->{'_error_code'} < 1 );
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_error_code"} < 1 0 0 0
1331 }
1332 }
1333 else #The File is already open
1334 {
1335 0 $irs = 1;
1336 }
1337  
1338 17 return $irs;
1339 }
1340  
1341 sub _isOpen {
1342 67 ▶ my $self = $_[0];
Subroutine _isOpen: called CC=4 SCAR=14.0
1343 67 my $iopn = 0;
1344  
1345 67 ▶ if ( defined $self->{'_file'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'_file'}) { } 10 57 67
1346 10 ▶ $iopn = 1 if ( fileno( $self->{'_file'} ) );
Branch: 1 branch
BranchTrueFalseTotal
if fileno $self->{"_file"} 10 0 10
1347 }
1348 else #The File Handle is not set
1349 {
1350 57 ▶ $self->{'_file'} = undef unless ( exists $self->{'_file'} );
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"_file"} 0 57 57
1351 }
1352  
1353 67 return $iopn;
1354 }
1355  
1356 sub _isWritable {
1357 13 ▶ my $self = $_[0];
Subroutine _isWritable: called CC=3 SCAR=11.0
1358 13 my $iopn = 0;
1359  
1360 13 ▶ if ( $self->_isOpen() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_isOpen) 4 9 13
1361 4 ▶ $iopn = $self->{'_writable'} if ( defined $self->{'_writable'} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_writable"} 4 0 4
1362 }
1363  
1364 13 return $iopn;
1365 }
1366  
1367 sub _isAppendable {
1368 0 ▶ my $self = $_[0];
Subroutine _isAppendable: not called CC=3 SCAR=24.8
1369 0 my $iopn = 0;
1370  
1371 0 ▶ if ( $self->_isOpen() ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_isOpen) 0 0 0
1372 0 ▶ $iopn = $self->{'_appendable'} if ( defined $self->{'_appendable'} );
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"_appendable"} 0 0 0
1373 }
1374  
1375 0 return $iopn;
1376 }
1377  
1378 sub isBuffered {
1379 0 ▶ return $_[0]->{'_buffered'};
Subroutine isBuffered: not called CC=1 SCAR=6.9
Pod undocumented
1380 }
1381  
1382 sub isPersistent {
1383 0 ▶ return $_[0]->{'_persistent'};
Subroutine isPersistent: not called CC=1 SCAR=6.9
Pod undocumented
1384 }
1385  
1386 sub getReportString {
1387 3 ▶ return \$_[0]->{"_report"};
Subroutine getReportString: called CC=1 SCAR=0.0
Pod undocumented
1388 }
1389  
1390 sub getErrorString {
1391 6 ▶ return \$_[0]->{'_error_message'};
Subroutine getErrorString: called CC=1 SCAR=0.0
Pod undocumented
1392 }
1393  
1394 sub getErrorCode {
1395 6 ▶ return $_[0]->{'_error_code'};
Subroutine getErrorCode: called CC=1 SCAR=0.0
Pod undocumented
1396 }
1397  
1398 1 return 1;
↑ Summary