Quiq/LineProcessor.pm

filter: Statementstmt 75.4% 83 / 110 Branchbran 42.1% 16 / 38 Conditioncond 30.0% 6 / 20 MC/DCmcdc 7.6% 1 / 13 Subroutinesub 85.7% 18 / 21 Podpod 100% 8 / 8 totaltotal 62.8% 132 / 210 CC 28 SCAR 34.3
CC
28
Cov
62%
new
67.3
shiftLineIfEq
12.0
removeEmptyLines
3.0
CRAP 31.0
« Quiq/TimeLapse/Sequence.pm ↑ Summary Quiq/JQuery/MenuBar.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::LineProcessor - Verarbeite Datei als Array von Zeilen
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Hash>
12  
13 =head1 DESCRIPTION
14  
15 Ein Objekt der Klasse repräsentiert eine Datei als ein Array von
16 Zeilen. Die Zeilen sind ihrerseits Objekte (per Default Objekte
17 der Klasse Quiq::LineProcessor::Line). Die Klasse stellt
18 Methoden zur Manipulation des Arrays von Zeilen zur Verfügung.
19  
20 =head2 Fehlerbehandlung
21  
22 Für eine Fehlerbehandlung können die Methoden $par->input()
23 und $line->number() genutzt werden:
24  
25 $class->throw(
26 'SDOC-00001: K\{} and k\{} are not supported anymore',
27 Input => ''.$par->input,
28 Line => $line->number,
29 );
30  
31 produziert (z.B.)
32  
33 Exception:
34 SDOC-00001: K{} and k{} is not supported anymore
35 Input:
36 /tmp/test.sdoc
37 Line:
38 20
39 Stacktrace:
40 ...
41  
42 =cut
43  
44 # -----------------------------------------------------------------------------
45  
46 package Quiq::LineProcessor;
47 1 ▶use base qw/Quiq::Hash/;
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 1 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 1 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
53  
54 1 our $VERSION = '1.240';
55  
56 1 ▶use Quiq::Option;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 1 ▶use Quiq::FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
58 1 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
59  
60 # -----------------------------------------------------------------------------
61  
62 =head1 METHODS
63  
64 =head2 Konstruktor
65  
66 =head3 new() - Instantiiere Textdatei-Objekt
67  
68 =head4 Synopsis
69  
70 $par = $class->new($file,@opt);
71 $par = $class->new(\$str,@opt);
72 $par = $class->new(\@lines,@opt);
73  
74 =head4 Options
75  
76 =over 4
77  
78 =item -lineClass => $class (Default: 'Quiq::LineProcessor::Line')
79  
80 Klasse, auf die die Zeilen des Dokuments geblesst werden.
81  
82 =item -lineContinuation => $type (Default: undef)
83  
84 Art der Zeilenfortsetzung. Mögliche Werte:
85  
86 =over 4
87  
88 =item undef
89  
90 Keine Zeilenfortsetzung.
91  
92 =item 'backslash'
93  
94 Endet eine Zeile mit einem Backslash, entferne Whitespace am
95 Anfang der Folgezeile und füge den Rest zur Zeile hinzu.
96  
97 Dies kann für eine Zeile unterdrückt werden, indem der Backslash am
98 Ende der Zeile durch einen davorgestellten Backslash maskiert wird.
99 In dem Fall wird statt einer Fortsetzung der Zeile der maskierende
100 Backslash entfernt.
101  
102 =item 'whitespace'
103  
104 Beginnt eine Zeile mit einem oder mehreren Leerzeichen oder TABs, wird
105 sie zur vorhergehenden Zeile hinzugefügt. Die Leerzeichen und TABs am
106 Zeilenanfang werden entfernt. Die Teile werden mit \n als Trenner
107 zusammengefügt.
108  
109 =back
110  
111 =item -skip => $regex (Default: undef)
112  
113 Überlies Zeilen, die Regex $regex erfüllen.
114  
115 =back
116  
117 =head4 Description
118  
119 Instantiiere ein Dokument-Objekt aus Datei $file, aus Text
120 $text oder aus den Zeilen @lines und liefere eine Referenz
121 auf dieses Objekt zurück.
122  
123 =cut
124  
125 # -----------------------------------------------------------------------------
126  
127 5 ▶sub new {
Subroutine new: called CC=21 SCAR=42.1
Pod documented
128 5 my $class = shift;
129 5 my $input = shift;
130 # @_: @opt
131  
132 # Optionen
133  
134 5 my $encoding;
135 5 my $lineClass = 'Quiq::LineProcessor::Line';
136 5 my $lineContinuation = undef;
137 5 my $skip = undef;
138  
139 5 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 5 5
140 0 Quiq::Option->extract(\@_,
141 -encoding => \$encoding,
142 -lineClass => \$lineClass,
143 -lineContinuation => \$lineContinuation,
144 -skip => \$skip,
145 );
146 }
147  
148 # Lade Zeilenklasse
149  
150 1 ▶ eval "use $lineClass";
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
151 5 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 5 5
152 0 $class->throw(
153 'TEXT-00001: Kann Zeilenklasse nicht laden',
154 LineClass => $lineClass,
155 InternalError => $@,
156 );
157 }
158  
159 5 my @lines;
160 5 my $inputAsString = "$input"; # Wir wollen die Bezeichnung
161 5 ▶ if (ref($input) eq 'ARRAY') { # Zeilen in neues Dokument
Branch: 1 branch
BranchTrueFalseTotal
if (ref $input eq 'ARRAY') { } 0 5 5
162 0 @lines = @$input;
163 }
164 else { # Zeilen aus Datei oder String lesen
165 5 ▶ if (ref($input) && $encoding) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $input and $encoding) 0 5 5
Condition33%1/3
ref $input and $encoding
!ll&&!rl&&r
050
MC/DC0%0/2
ref $input and $encoding
ref $input $encoding
Truth table33%1/3
A and B
A= ref $inputB= $encoding
ABresult
0X0
100
111
166 0 $$input = Encode::encode($encoding,$$input);
167 }
168 5 my $fh = Quiq::FileHandle->new('<',$input);
169 5 ▶ if ($encoding) {
Branch: 1 branch
BranchTrueFalseTotal
if ($encoding) 0 5 5
170 0 $fh->binmode(":encoding($encoding)");
171 }
172 5 ▶ while (<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 22 5 27
173 22 chomp;
174 22 push @lines,$lineClass->new($_,$.,\$inputAsString);
175 }
176 5 $fh->close;
177 }
178  
179 # Kommentarzeilen entfernen und Fortsetzungszeilen zusammenfassen
180  
181 5 ▶ for (my $i = 0; $i < @lines; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @lines) 22 5 27
182 22 my $text = $lines[$i]->text;
183 22 ▶ if ($skip && $text =~ /$skip/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($skip and $text =~ /$skip/) 0 22 22
Condition33%1/3
$skip and $text =~ /$skip/
!ll&&!rl&&r
2200
MC/DC0%0/2
$skip and $text =~ /$skip/
$skip $text =~ /$skip/
Truth table33%1/3
A and B
A= $skipB= $text =~ /$skip/
ABresult
0X0
100
111
184 # Kommentarzeile entfernen
185 0 splice @lines,$i--,1;
186 0 next;
187 }
188  
189 22 ▶ if ($lineContinuation) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lineContinuation) 0 22 22
190 0 ▶ if ($lineContinuation eq 'backslash') {
Branch: 2 branches
BranchTrueFalseTotal
if ($lineContinuation eq 'backslash') { } 0 0 0
elsif ($lineContinuation eq 'whitespace') { } 0 0 0
191 0 my $modified = 0;
192 0 ▶ while ($text =~ s/\\$// && $i+1 < @lines) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ s/\\$// and $i + 1 < @lines) 0 0 0
Condition0%0/3
$text =~ s/\\$// and $i + 1 < @lines
!ll&&!rl&&r
000
MC/DC0%0/2
$text =~ s/\\$// and $i + 1 < @lines
$text =~ s/\\$// $i + 1 < @lines
Truth table0%0/3
A and B
A= $text =~ s/\\$//B= $i + 1 < @lines
ABresult
0X0
100
111
193 0 $modified++;
194 0 ▶ last if $text =~ /\\$/;
Branch: 1 branch
BranchTrueFalseTotal
if $text =~ /\\$/ 0 0 0
195 0 my $text2 = $lines[$i+1]->text;
196 0 $text2 =~ s/^\s+//; # WS am Anfang entfernen
197 0 $text .= $text2;
198 0 splice @lines,$i+1,1;
199 }
200 0 ▶ $lines[$i]->text($text) if $modified;
Branch: 1 branch
BranchTrueFalseTotal
if $modified 0 0 0
201 }
202 elsif ($lineContinuation eq 'whitespace') {
203 0 ▶ if ($text =~ s/^[ \t]+/\n/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($text =~ s/^[ \t]+/\n/) 0 0 0
204 0 $lines[$i-1]->append($text);
205 # Fortsetzungszeile entfernen
206 0 splice @lines,$i--,1;
207 }
208 }
209 else {
210 0 $class->throw(
211 'TEXT-00002: Ungüliger Wert für Option -lineContinuation',
212 Value => $lineContinuation,
213 );
214 }
215 }
216 }
217  
218 # Leerzeilen am Ende entfernen
219 5 ▶ pop @lines while @lines && $lines[-1]->isEmpty;
Branch: 1 branch
BranchTrueFalseTotal
@lines and $lines[-1]->isEmpty and do { pop @lines } 0 5 5
Condition33%1/3
@lines and $lines[-1]->isEmpty
!ll&&!rl&&r
050
MC/DC0%0/2
@lines and $lines[-1]->isEmpty
@lines $lines[-1]->isEmpty
Truth table33%1/3
A and B
A= @linesB= $lines[-1]->isEmpty
ABresult
0X0
100
111
220  
221 5 my $self = $class->SUPER::new(
222 input => $inputAsString, # Wir wollen die Bezeichnung
223 lineClass => $lineClass,
224 lineA => \@lines,
225 );
226  
227 5 return $self;
228 }
229  
230 # -----------------------------------------------------------------------------
231  
232 =head2 Akzessoren
233  
234 =head3 input() - Bezeichnung Eingabequelle
235  
236 =head4 Synopsis
237  
238 $input = $par->input;
239  
240 =head4 Description
241  
242 Liefere die Bezeichnung der Eingabequelle. Dies kann ein Dateiname
243 oder eine stringifizierte String- oder Arrayreferenz sein.
244  
245 =cut
246  
247 # -----------------------------------------------------------------------------
248  
249 0 ▶sub input {
Subroutine input: not called CC=1 SCAR=6.9
Pod documented
250 0 return shift->{'input'};
251 }
252  
253 # -----------------------------------------------------------------------------
254  
255 =head3 lineClass() - Zeilen-Klasse
256  
257 =head4 Synopsis
258  
259 $lineClass = $par->lineClass;
260  
261 =head4 Description
262  
263 Liefere die Zeilen-Klasse.
264  
265 =cut
266  
267 # -----------------------------------------------------------------------------
268  
269 0 ▶sub lineClass {
Subroutine lineClass: not called CC=1 SCAR=6.9
Pod documented
270 0 return shift->{'lineClass'};
271 }
272  
273 # -----------------------------------------------------------------------------
274  
275 =head3 lines() - Liste der Zeilen
276  
277 =head4 Synopsis
278  
279 @lines | $lineA = $par->lines(\@lines);
280  
281 =head4 Description
282  
283 Liefere die Liste der Zeilen der Datei. Im Skalarkontext liefere
284 eine Referenz auf die Liste.
285  
286 =cut
287  
288 # -----------------------------------------------------------------------------
289  
290 7 ▶sub lines {
Subroutine lines: called CC=2 SCAR=6.9
Pod documented
291 7 my $self = shift;
292 7 my $lineA = $self->{'lineA'};
293 7 ▶ return wantarray? @$lineA: $lineA;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 5 7
294 }
295  
296 # -----------------------------------------------------------------------------
297  
298 =head2 Operationen
299  
300 =head3 shiftLine() - Entferne und liefere erste Zeile
301  
302 =head4 Synopsis
303  
304 $line = $par->shiftLine;
305  
306 =head4 Description
307  
308 Entferne die erste Zeile aus dem Dokument und liefere diese zurück.
309  
310 =cut
311  
312 # -----------------------------------------------------------------------------
313  
314 2 ▶sub shiftLine {
Subroutine shiftLine: called CC=1 SCAR=0.0
Pod documented
315 2 my $self = shift;
316 2 return shift @{$self->lines};
317 }
318  
319 # -----------------------------------------------------------------------------
320  
321 =head3 shiftLineIfEq() - Entferne erste Zeile, wenn bestimmter Inhalt
322  
323 =head4 Synopsis
324  
325 $line = $par->shiftLineIfEq($str);
326  
327 =head4 Description
328  
329 Entferne die erste Zeile aus dem Dokument und liefere diese zurück,
330 sofern ihr Inhalt eq $str ist.
331  
332 =cut
333  
334 # -----------------------------------------------------------------------------
335  
336 0 ▶sub shiftLineIfEq {
Subroutine shiftLineIfEq: not called CC=3 SCAR=24.8
Pod documented
337 0 my ($self,$str) = @_;
338 0 my $lineA = $self->lines;
339 0 ▶ return @$lineA && $lineA->[0]->text eq $str? shift @{$self->lines}: undef;
Branch: 1 branch
BranchTrueFalseTotal
@$lineA && $lineA->[0]->text eq $str ? : 0 0 0
Condition0%0/3
@$lineA and $lineA->[0]->text eq $str
!ll&&!rl&&r
000
MC/DC0%0/2
@$lineA and $lineA->[0]->text eq $str
@$lineA $lineA->[0]->text eq $str
Truth table0%0/3
A and B
A= @$lineAB= $lineA->[0]->text eq $str
ABresult
0X0
100
111
340 }
341  
342 # -----------------------------------------------------------------------------
343  
344 =head3 removeEmptyLines() - Entferne Leerzeilen am Anfang
345  
346 =head4 Synopsis
347  
348 $par->removeEmptyLines;
349  
350 =head4 Description
351  
352 Entferne Leerzeilen am Anfang. Die Methode liefert
353 keinen Wert zurück.
354  
355 =cut
356  
357 # -----------------------------------------------------------------------------
358  
359 1 ▶sub removeEmptyLines {
Subroutine removeEmptyLines: called CC=3 SCAR=11.0
Pod documented
360 1 my $self = shift;
361 1 my $lines = $self->lines;
362 1 ▶ shift @$lines while @$lines && $lines->[0]->isEmpty;
Branch: 1 branch
BranchTrueFalseTotal
@$lines and $lines->[0]->isEmpty and do { shift @$lines } 2 1 3
Condition66%2/3
@$lines and $lines->[0]->isEmpty
!ll&&!rl&&r
012
MC/DC50%1/2
@$lines and $lines->[0]->isEmpty
@$lines $lines->[0]->isEmpty
Truth table66%2/3
A and B
A= @$linesB= $lines->[0]->isEmpty
ABresult
0X0
100
111
363 1 return;
364 }
365  
366 # -----------------------------------------------------------------------------
367  
368 =head2 Externe Repräsentation
369  
370 =head3 dump() - Erzeuge externe Repräsentation
371  
372 =head4 Synopsis
373  
374 $str = $par->dump($format);
375 $str = $par->dump;
376  
377 =head4 Description
378  
379 Erzeuge eine externe Dokumentrepräsentation in Format $format
380 für das gesamte Dokument und liefere diese zurück.
381  
382 B<Formate>
383  
384 Siehe $ln->dump()
385  
386 =cut
387  
388 # -----------------------------------------------------------------------------
389  
390 1 ▶sub dump {
Subroutine dump: called CC=3 SCAR=11.0
Pod documented
391 1 my $self = shift;
392 1 ▶ my $format = shift || 0;
Condition50%1/2
shift() || 0
l!l
01
MC/DC0%0/1
shift() || 0
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
393  
394 1 my $str = '';
395 1 for my $line (@{$self->lines}) {
396 4 $str .= $line->dump($format);
397 }
398  
399 1 return $str;
400 }
401  
402 # -----------------------------------------------------------------------------
403  
404 =head1 VERSION
405  
406 1.240
407  
408 =head1 AUTHOR
409  
410 Frank Seitz, L<http://fseitz.de/>
411  
412 =head1 COPYRIGHT
413  
414 Copyright (C) 2026 Frank Seitz
415  
416 =head1 LICENSE
417  
418 This code is free software; you can redistribute it and/or modify
419 it under the same terms as Perl itself.
420  
421 =cut
422  
423 # -----------------------------------------------------------------------------
424  
425 1 1;
426  
427 # eof
« Quiq/TimeLapse/Sequence.pm ↑ Summary Quiq/JQuery/MenuBar.pm »