File Coverage

lib/Kephra/File/IO.pm
Criterion Covered Total %
statement 6 54 11.1
branch 0 24 0.0
condition 0 21 0.0
subroutine 2 5 40.0
pod 0 3 0.0
total 8 107 7.4


line stmt bran cond sub pod time code
1             package Kephra::File::IO;
2             our $VERSION = '0.20';
3              
4 1     1   1241 use strict;
  1         3  
  1         35  
5 1     1   6 use warnings;
  1         2  
  1         720  
6              
7             # read a file into a scintilla buffer, is much faster then open_buffer
8             sub open_buffer {
9 0     0 0   my $doc_nr = shift;
10 0   0       my $file = shift || Kephra::Document::Data::get_file_path($doc_nr);
11 0   0       my $ep = shift || Kephra::Document::Data::_ep($doc_nr);
12 0           my $err_txt= Kephra::API::localisation->{dialog}{error};
13 0           my $input;
14 0 0         unless ($file) {
15 0           Kephra::Dialog::warning_box("file_read " . $err_txt->{no_param}, $err_txt->{general} );
16             } else {
17 0 0         unless ( -r $file ) {
18 0           Kephra::Dialog::warning_box( $err_txt->{file_read} . " " . $file, $err_txt->{file} );
19             } else {
20 0           my $did_open = open my $FH,'<', $file;
21 0 0         unless ($did_open){
22 0           Kephra::Dialog::warning_box($err_txt->{file_read} . " $file", $err_txt->{file});
23 0           return 0;
24             }
25 0           my $codepage = Kephra::Document::Data::get_attribute('codepage', $doc_nr);
26 0 0         if ($codepage eq 'auto'){
27 0           binmode $FH;
28 0           read $FH, my $probe, 20000;
29 0 0         if ($probe){
30 0           my $enc = Encode::Guess::guess_encoding( $probe, 'latin1' );
31 0           seek $FH, 0, 0;
32 0 0         $codepage = $enc =~ /utf8/ ? 'utf8' : '8bit';
33 0           Kephra::Document::Data::set_attribute('codepage', $codepage, $doc_nr);
34             } else {
35 0           $codepage = Kephra::File::_config->{defaultsettings}{new}{codepage};
36             }
37 0           Kephra::Document::Data::set_attribute('codepage', $codepage, $doc_nr);
38             }
39 0 0         binmode $FH, $codepage eq 'utf8' ? ":utf8" : ":raw"; # ":encoding(utf8)"
40 0           Kephra::EventTable::freeze('document.text.change');
41 0           my $content = do { local $/; <$FH> };
  0            
  0            
42 0 0         $ep->AddText( $content ) if defined $content;
43 0           Kephra::EventTable::thaw('document.text.change');
44 0           return 1;
45             }
46             }
47 0           return 0;
48             }
49              
50             # wite into file from buffer variable
51             sub write_buffer {
52 0   0 0 0   my $doc_nr = shift || Kephra::Document::Data::current_nr();
53 0   0       my $file = shift || Kephra::Document::Data::get_file_path($doc_nr);
54 0   0       my $ep = shift || Kephra::Document::Data::_ep($doc_nr);
55 0           my $err_txt = Kephra::API::localisation->{dialog}{error};
56             # check if there is a name or if file that you overwrite is locked
57 0 0 0       if ( not $file or (-e $file and not -w $file) ) {
      0        
58 0           Kephra::Dialog::warning_box
59             ("file_write " . $err_txt->{'no_param'}, $err_txt->{general} );
60             } else {
61 0           my $codepage = Kephra::Document::Data::get_attribute('codepage', $doc_nr);
62 0           my $did_open = open my $FH, '>', $file;
63 0 0         unless ($did_open){
64 0           Kephra::Dialog::warning_box($err_txt->{file_write} . " $file", $err_txt->{file} );
65 0           return 0;
66             }
67 0 0         binmode $FH, $codepage eq 'utf8' ? ":utf8" : ":raw"; # ":encoding(utf8)"
68 0           print $FH $ep->GetText();
69             }
70             }
71              
72              
73             sub get_age {
74 0     0 0   my $file = shift;
75 0 0         return (stat $file)[9] if -e $file;
76             }
77              
78             1;