File/Text.pm

filter: Statementstmt 100% 9 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 100% 12 / 12 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« File/Readelf.pm ↑ Summary File/Tsv.pm »
1 #
2 # $Id$
3 #
4 # file::text Brik
5 #
6 package Metabrik::File::Text;
7 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 5 ▶use base qw(Metabrik::File::Write);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub brik_properties {
13 return {
14 revision => '$Revision$',
15 tags => [ qw(unstable read write) ],
16 author => 'GomoR <GomoR[at]metabrik.org>',
17 license => 'http://opensource.org/licenses/BSD-3-Clause',
18 attributes => {
19 input => [ qw(file) ],
20 output => [ qw(file) ],
21 as_array => [ qw(0|1) ],
22 strip_crlf => [ qw(0|1) ],
23 encoding => [ qw(utf8|ascii) ], # Inherited
24 _fr => [ qw(INTERNAL) ],
25 },
26 # encoding: see `perldoc Encode::Supported' for other types
27 attributes_default => {
28 encoding => 'utf8',
29 as_array => 0,
30 strip_crlf => 0,
31 },
32 commands => {
33 read => [ qw(input) ],
34 read_line => [ qw(input count|OPTIONAL) ],
35 read_split_by_blank_line => [ qw(input) ],
36 read_split_by_ini_block => [ qw(input) ],
37 write => [ qw($data|$data_ref|$data_list output) ],
38 is_eof => [ qw(ret) ],
39 },
40 require_modules => {
41 'Metabrik::File::Read' => [ ],
42 },
43 };
44 }
45  
46 sub _open {
47 my $self = shift;
48 my ($input) = @_;
49  
50 my $fr = Metabrik::File::Read->new_from_brik_init($self) or return;
51 $fr->input($input);
52 $fr->encoding($self->encoding);
53 $fr->as_array($self->as_array);
54 $fr->strip_crlf($self->strip_crlf);
55  
56 $fr->open or return;
57  
58 return $fr;
59 }
60  
61 #
62 # Read everything available
63 #
64 sub read {
65 my $self = shift;
66 my ($input) = @_;
67  
68 $input ||= $self->input;
69 $self->brik_help_run_undef_arg('read', $input) or return;
70  
71 my $fr = $self->_open($input) or return;
72 my $data = $fr->read or return;
73 $fr->close;
74 $self->_fr(undef);
75  
76 return $data;
77 }
78  
79 #
80 # Just return next available line
81 # Returns 0 on EOF.
82 #
83 sub read_line {
84 my $self = shift;
85 my ($input, $count) = @_;
86  
87 $input ||= $self->input;
88 $count ||= 1;
89 $self->brik_help_run_undef_arg('read_line', $input) or return;
90 $self->brik_help_run_file_not_found('read_line', $input) or return;
91  
92 my $fr = $self->_fr;
93 if (! $fr) {
94 $fr = $self->_open($input) or return;
95 $self->_fr($fr);
96 }
97  
98 if ($fr->eof) {
99 $fr->close;
100 $self->_fr(undef);
101 return 0;
102 }
103  
104 my $data;
105 my @lines = ();
106 if ($count > 1) {
107 for (1..$count) {
108 $data = $fr->read_line;
109 push @lines, $data;
110 }
111 }
112 else {
113 $data = $fr->read_line;
114 }
115  
116 return $count > 1 ? \@lines : $data;
117 }
118  
119 #
120 # Will read everything until eof
121 #
122 sub read_split_by_blank_line {
123 my $self = shift;
124 my ($input) = @_;
125  
126 $input ||= $self->input;
127 $self->brik_help_run_undef_arg('read_split_by_blank_line', $input) or return;
128  
129 my $fr = $self->_open($input) or return;
130  
131 my @chunks = ();
132 while (my $this = $fr->read_until_blank_line) {
133 push @chunks, $this;
134 last if $fr->eof;
135 }
136  
137 $fr->close;
138 $self->_fr(undef);
139  
140 return \@chunks;
141 }
142  
143 sub read_split_by_ini_block {
144 my $self = shift;
145 my ($input) = @_;
146  
147 $input ||= $self->input;
148 $self->brik_help_run_undef_arg('read_split_by_ini_block', $input) or return;
149  
150 my $fr = $self->_open($input) or return;
151 $fr->skip_comment(1);
152 $fr->skip_blank_line(1);
153  
154 my @chunks = ();
155 while (my $this = $fr->read_until_ini_block) {
156 push @chunks, $this;
157 last if $fr->eof;
158 }
159  
160 $fr->close;
161 $self->_fr(undef);
162  
163 return \@chunks;
164 }
165  
166 sub write {
167 my $self = shift;
168 my ($data, $output) = @_;
169  
170 $output ||= $self->output;
171 $self->brik_help_run_undef_arg('write', $data) or return;
172 $self->brik_help_run_undef_arg('write', $output) or return;
173  
174 $self->open($output) or return;
175 # We check definedness because if we write 0 byte write will return 0
176 my $r = $self->SUPER::write($data);
177 if (! defined($r)) {
178 return;
179 }
180 $self->close;
181  
182 return 1;
183 }
184  
185 sub is_eof {
186 my $self = shift;
187 my ($r) = @_;
188  
189 if (defined($r) && $r == 0 && !defined($self->_fr)) {
190 return 1;
191 }
192  
193 return 0;
194 }
195  
196 1;
197  
198 __END__
« File/Readelf.pm ↑ Summary File/Tsv.pm »