CAB/Format/Text.pm untested

filter: Statementstmt 0.0% 0 / 50 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 85 CC 6 SCAR 27.8
CC
6
Cov
0%
blockScanBody
42.0
BEGIN
2.0
BEGIN
2.0
CRAP 16.1
1 ## -*- Mode: CPerl -*-
2 ##
3 ## File: DTA::CAB::Format::Text.pm
4 ## Author: Bryan Jurish <moocow@cpan.org>
5 ## Description: Datum parser: verbose human-readable text
6  
7 package DTA::CAB::Format::Text;
8 use DTA::CAB::Format;
9 use DTA::CAB::Format::TT;
10 use DTA::CAB::Datum ':all';
11 use IO::File;
12 use Carp;
13 use strict;
14  
15 ##==============================================================================
16 ## Globals
17 ##==============================================================================
18  
19 our @ISA = qw(DTA::CAB::Format::TT);
20  
21 BEGIN {
22 DTA::CAB::Format->registerFormat(name=>__PACKAGE__, filenameRegex=>qr/\.(?i:txt|text|cab\-txt|cab\-text)$/);
23 DTA::CAB::Format->registerFormat(name=>__PACKAGE__, short=>'txt');
24 }
25  
26 ##==============================================================================
27 ## Constructors etc.
28 ##==============================================================================
29  
30 ## $fmt = CLASS_OR_OBJ->new(%args)
31 ## + object structure: assumed HASH
32 ## (
33 ## ##---- Input
34 ## doc => $doc, ##-- buffered input document
35 ##
36 ## ##---- Output
37 ## #level => $formatLevel, ##-- output formatting level: n/a
38 ## #outbuf => $stringBuffer, ##-- buffered output
39 ##
40 ## ##---- Common
41 ## utf8 => $bool, ##-- default: 1
42 ## defaultFieldName => $name, ##-- default name for unnamed fields; parsed into @{$tok->{other}{$name}}; default=''
43 ## )
44 ## + inherited from DTA::CAB::Format::TT
45  
46 ##==============================================================================
47 ## Methods: Persistence
48 ##==============================================================================
49  
50 ##==============================================================================
51 ## Methods: I/O: Block-wise
52 ##==============================================================================
53  
54 ## \@blocks = $fmt->blockScanBody(\$buf,\%opts)
55 ## + scans $filename for block boundaries according to \%opts
56 sub blockScanBody {
57 my ($fmt,$bufr,$opts) = @_;
58  
59 ##-- scan blocks into head, body, foot
60 my $bsize = $opts->{bsize} // $opts->{size} // 1048576;
61 my $fsize = $opts->{ifsize};
62 my $eob = $opts->{eob} =~ /^s/i ? 's' : 'w';
63 my $blocks = [];
64  
65 my ($off0,$off1,$blk);
66 for ($off0=$opts->{ihead}[0]+$opts->{ihead}[1]; $off0 < $fsize; $off0=$off1) {
67 push(@$blocks, $blk={ioff=>$off0});
68 pos($$bufr) = ($off0+$bsize < $fsize ? $off0+$bsize : $fsize);
69 if ($eob eq 's' ? $$bufr=~m/\n{2,}/sg : $$bufr=~m/\n{1,}(?!\t)/sg) {
70 $off1 = $+[0];
71 $blk->{eos} = $+[0]-$-[0] > 1 ? 1 : 0;
72 } else {
73 $off1 = $fsize;
74 $blk->{eos} = 1;
75 }
76 $blk->{ilen} = $off1-$off0;
77 }
78  
79 return $blocks;
80 }
81  
82 ##==============================================================================
83 ## Methods: Input
84 ##==============================================================================
85  
86 ##--------------------------------------------------------------
87 ## Methods: Input: Local
88  
89 ## $fmt = $fmt->parseTextString(\$string)
90 BEGIN { *parseTTString = \&parseTextString; }
91 sub parseTextString {
92 my ($fmt,$src) = @_;
93 $$src =~ s/\r?\n\t\+?/\t/sg;
94 return DTA::CAB::Format::TT::parseTTString($fmt,$src);
95 }
96  
97 ##--------------------------------------------------------------
98 ## Methods: Input: Generic API
99  
100  
101 ##==============================================================================
102 ## Methods: Output
103 ##==============================================================================
104  
105 ##--------------------------------------------------------------
106 ## Methods: Output: Generic
107  
108 ## $ext = $fmt->defaultExtension()
109 ## + returns default filename extension for this format
110 sub defaultExtension { return '.txt'; }
111  
112 ##--------------------------------------------------------------
113 ## Methods: Output: output selection
114  
115 ## \$buf = $fmt->token2buf($tok,\$buf)
116 ## + buffer output for a single token
117 ## + override converts from TT->Text
118 sub token2buf {
119 my $bufr = DTA::CAB::Format::TT::token2buf(@_);
120 $$bufr =~ s/(?<!\n)\t/\n\t/sg;
121 $$bufr =~ s/^\t\[/\t+\[/mg;
122 return $bufr;
123 }
124  
125 ##--------------------------------------------------------------
126 ## Methods: Output: Generic API
127  
128 ## $fmt = $fmt->putToken($tok)
129 ## + appends $tok to output buffer
130 ## + INHERITED from Format::TT
131  
132 ## $fmt = $fmt->putSentence($sent)
133 ## + concatenates formatted tokens, adding sentence-id comment if available
134 ## + inherited from DTA::CAB::Format::TT
135  
136 ## $out = $fmt->formatDocument($doc)
137 ## + concatenates formatted sentences, adding document 'xmlbase' comment if available
138 ## + inherited from DTA::CAB::Format::TT
139  
140 1; ##-- be happy
141  
142 __END__