File/Read.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/Raw.pm ↑ Summary File/Readelf.pm »
1 #
2 # $Id$
3 #
4 # file::read Brik
5 #
6 package Metabrik::File::Read;
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use base qw(Metabrik);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub brik_properties {
13 return {
14 revision => '$Revision$',
15 tags => [ qw(unstable) ],
16 author => 'GomoR <GomoR[at]metabrik.org>',
17 license => 'http://opensource.org/licenses/BSD-3-Clause',
18 attributes => {
19 input => [ qw(file) ],
20 encoding => [ qw(utf8|ascii) ],
21 fd => [ qw(file_descriptor) ],
22 as_array => [ qw(0|1) ],
23 eof => [ qw(0|1) ],
24 count => [ qw(count) ],
25 strip_crlf => [ qw(0|1) ],
26 skip_comment => [ qw(0|1) ],
27 skip_blank_line => [ qw(0|1) ],
28 },
29 attributes_default => {
30 as_array => 0,
31 eof => 0,
32 count => 1,
33 strip_crlf => 1,
34 skip_comment => 0,
35 skip_blank_line => 0,
36 },
37 commands => {
38 open => [ qw(file|OPTIONAL) ],
39 close => [ ],
40 offset => [ ],
41 seek => [ qw(offset) ],
42 read => [ ],
43 read_until_blank_line => [ ],
44 read_until_ini_block => [ ],
45 read_line => [ qw(count|OPTIONAL) ],
46 is_eof => [ ],
47 },
48 };
49 }
50  
51 sub brik_use_properties {
52 my $self = shift;
53  
54 return {
55 attributes_default => {
56 encoding => defined($self->global) && $self->global->encoding || 'utf8',
57 },
58 };
59 }
60  
61 sub open {
62 my $self = shift;
63 my ($input) = @_;
64  
65 $input ||= $self->input;
66 $self->brik_help_run_undef_arg('open', $input) or return;
67  
68 my $r;
69 my $out;
70 my $encoding = $self->encoding || 'ascii';
71 if ($encoding eq 'ascii') {
72 $r = open($out, '<', $input);
73 }
74 else {
75 $r = open($out, "<$encoding", $input);
76 }
77 if (! defined($r)) {
78 return $self->log->error("open: open: file [$input]: $!");
79 }
80  
81 return $self->fd($out);
82 }
83  
84 sub close {
85 my $self = shift;
86  
87 if (defined($self->fd)) {
88 close($self->fd);
89 $self->eof(0);
90 }
91  
92 return 1;
93 }
94  
95 sub offset {
96 my $self = shift;
97  
98 my $fd = $self->fd;
99 $self->brik_help_run_undef_arg('open', $fd) or return;
100  
101 my $r = CORE::tell($fd);
102 if (! defined($r)) {
103 return $self->log->error("offset: unable to get offset: [$!]");
104 }
105  
106 return $r;
107 }
108  
109 sub seek {
110 my $self = shift;
111 my ($offset) = @_;
112  
113 my $fd = $self->fd;
114 $self->brik_help_run_undef_arg('open', $fd) or return;
115 $self->brik_help_run_undef_arg('seek', $offset) or return;
116  
117 my $r = CORE::seek($fd, $offset, 0);
118 if (! $r) {
119 return $self->log->error("seek: unable to seek to offset [$offset]: [$!]");
120 }
121  
122 return $offset;
123 }
124  
125 sub read {
126 my $self = shift;
127  
128 my $fd = $self->fd;
129 $self->brik_help_run_undef_arg('open', $fd) or return;
130  
131 my $strip_crlf = $self->strip_crlf;
132 my $skip_comment = $self->skip_comment;
133 my $skip_blank_line = $self->skip_blank_line;
134  
135 if ($self->as_array) {
136 my @out = ();
137 # Don't play with $_, it may be tricky:
138 # https://www.perlmonks.org/bare/?node_id=570088
139 while (my $line = <$fd>) {
140 if ($skip_comment) {
141 next if $line =~ m{^\s*#};
142 }
143 if ($skip_blank_line) {
144 next if $line =~ m{^\s*$};
145 }
146 if ($strip_crlf) {
147 $line =~ s/[\r\n]*$//;
148 }
149 push @out, $line;
150 }
151 $self->eof(1);
152 return \@out;
153 }
154 else {
155 my $out = '';
156 while (my $line = <$fd>) {
157 if ($skip_comment) {
158 next if $line =~ m{^\s*#};
159 }
160 if ($skip_blank_line) {
161 next if $line =~ m{^\s*$};
162 }
163 $out .= $line;
164 }
165 $self->eof(1);
166 if ($strip_crlf) {
167 $out =~ s/[\r\n]*$//;
168 }
169 return $out;
170 }
171  
172 return;
173 }
174  
175 sub read_until_blank_line {
176 my $self = shift;
177  
178 my $fd = $self->fd;
179 $self->brik_help_run_undef_arg('open', $fd) or return;
180  
181 my $strip_crlf = $self->strip_crlf;
182 my $skip_comment = $self->skip_comment;
183  
184 if ($self->as_array) {
185 my @out = ();
186 while (my $line = <$fd>) {
187 if ($skip_comment) {
188 next if $line =~ m{^\s*#};
189 }
190 last if $line =~ /^\s*$/;
191 if ($strip_crlf) {
192 $line =~ s/[\r\n]*$//;
193 }
194 push @out, $line;
195 }
196 if (eof($fd)) {
197 $self->eof(1);
198 }
199 return \@out;
200 }
201 else {
202 my $out = '';
203 while (my $line = <$fd>) {
204 if ($skip_comment) {
205 next if $line =~ m{^\s*#};
206 }
207 last if $line =~ /^\s*$/;
208 $out .= $line;
209 }
210 if (eof($fd)) {
211 $self->eof(1);
212 }
213 if ($strip_crlf) {
214 $out =~ s/[\r\n]*$//;
215 }
216 return $out;
217 }
218  
219 return;
220 }
221  
222 sub read_until_ini_block {
223 my $self = shift;
224  
225 my $fd = $self->fd;
226 $self->brik_help_run_undef_arg('open', $fd) or return;
227  
228 my $strip_crlf = $self->strip_crlf;
229 my $skip_comment = $self->skip_comment;
230 my $skip_blank_line = $self->skip_blank_line;
231  
232 my $block = undef;
233 my $offset = 0;
234 if ($self->as_array) {
235 my @out = ();
236 while (my $line = <$fd>) {
237 if ($skip_comment) {
238 next if $line =~ m{^\s*#};
239 }
240 if ($skip_blank_line) {
241 next if $line =~ m{^\s*$};
242 }
243 if ($strip_crlf) {
244 $line =~ s/[\r\n]*$//;
245 }
246 if ($line =~ /^\s*\[\s*\S+\s*\]\s*$/) {
247 if (!defined($block)) {
248 $block = $line;
249 }
250 else {
251 $self->seek($offset); # New block starting, restore to previous offset
252 # for next Command call.
253 last;
254 }
255 }
256 push @out, $line;
257 $offset = $self->offset($fd) or return;
258 }
259 if (eof($fd)) {
260 $self->eof(1);
261 }
262 return \@out;
263 }
264 else {
265 my $out = '';
266 while (my $line = <$fd>) {
267 if ($skip_comment) {
268 next if $line =~ m{^\s*#};
269 }
270 if ($skip_blank_line) {
271 next if $line =~ m{^\s*$};
272 }
273 if ($line =~ /^\s*\[\s*\S+\s*\]\s*$/) {
274 if (!defined($block)) {
275 $block = $line;
276 }
277 else {
278 $self->seek($offset); # New block starting, restore to previous offset
279 # for next Command call.
280 last;
281 }
282 }
283 $out .= $line;
284 $offset = $self->offset($fd) or return;
285 }
286 if (eof($fd)) {
287 $self->eof(1);
288 }
289 if ($strip_crlf) {
290 $out =~ s/[\r\n]*$//;
291 }
292 return $out;
293 }
294  
295 return;
296 }
297  
298 sub read_line {
299 my $self = shift;
300 my ($count) = @_;
301  
302 my $fd = $self->fd;
303 $self->brik_help_run_undef_arg('open', $fd) or return;
304  
305 $count ||= $self->count;
306  
307 my $strip_crlf = $self->strip_crlf;
308 my $skip_comment = $self->skip_comment;
309 my $skip_blank_line = $self->skip_blank_line;
310  
311 if ($self->as_array) {
312 my @out = ();
313 my $this = 1;
314 while (my $line = <$fd>) {
315 if ($skip_comment) {
316 next if $line =~ m{^\s*#};
317 }
318 if ($skip_blank_line) {
319 next if $line =~ m{^\s*$};
320 }
321 if ($strip_crlf) {
322 $line =~ s/[\r\n]*$//;
323 }
324 push @out, $line;
325 last if $this == $count;
326 $count++;
327 }
328 if (eof($fd)) {
329 $self->eof(1);
330 }
331 return \@out;
332 }
333 else {
334 my $out = '';
335 my $this = 1;
336 while (my $line = <$fd>) {
337 if ($skip_comment) {
338 next if $line =~ m{^\s*#};
339 }
340 if ($skip_blank_line) {
341 next if $line =~ m{^\s*$};
342 }
343 last if $line =~ /^\s*$/;
344 $out .= $line;
345 last if $this == $count;
346 $count++;
347 }
348 if (eof($fd)) {
349 $self->eof(1);
350 }
351 if ($strip_crlf) {
352 $out =~ s/[\r\n]*$//;
353 }
354 return $out;
355 }
356  
357 return;
358 }
359  
360 sub is_eof {
361 my $self = shift;
362  
363 return $self->eof;
364 }
365  
366 1;
367  
368 __END__
« File/Raw.pm ↑ Summary File/Readelf.pm »