| 1 | package YATT::XHF; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | YATT::XHF - Extended Header Fields format. | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | =cut | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 3 | ▶ | use warnings qw(FATAL all NONFATAL misc); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 3 | ▶ | use base qw(YATT::Class::Configurable); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | 3 | ▶ | use YATT::Fields qw(cf_FH cf_filename cf_tokens); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 3 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 3 | ▶ | use YATT::Util::Enum -prefix => '_', qw(NAME VALUE SIGIL); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 3 | our $cc_name = qr{\w|[\.\-%/]}; | |||||||||||||||||||||||||||
| 19 | 3 | our $cc_sigil = qr{[:\#,\-\[\]\{\}]}; | |||||||||||||||||||||||||||
| 20 | 3 | our $cc_tabsp = qr{[\ \t]}; | |||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 3 | our %OPN = qw([ array { hash); | |||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 15 | ▶ | sub configure_filename { | ||||||||||||||||||||||||||
|
Subroutine
configure_filename: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 25 | 15 | (my MY $self, my ($fn)) = @_; | |||||||||||||||||||||||||||
| 26 | 15 | ▶ | open $self->{cf_FH}, '<', $fn | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 27 | or croak "Can't open file '$fn': $!"; | ||||||||||||||||||||||||||||
| 28 | 15 | $self->{cf_filename} = $fn; | |||||||||||||||||||||||||||
| 29 | 15 | $self; | |||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 7 | ▶ | sub configure_string { | ||||||||||||||||||||||||||
|
Subroutine
configure_string: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 33 | 7 | (my MY $self, my ($string)) = @_; | |||||||||||||||||||||||||||
| 34 | 7 | ▶ | open $self->{cf_FH}, '<', \$string | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | or croak "Can't create string stream: $!"; | ||||||||||||||||||||||||||||
| 36 | 7 | $self; | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 1 | ▶ | sub read_as_hashlist { | ||||||||||||||||||||||||||
|
Subroutine
read_as_hashlist: called CC=4 SCAR=14.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 40 | 1 | my MY $reader = shift; | |||||||||||||||||||||||||||
| 41 | 1 | local $/ = ""; | |||||||||||||||||||||||||||
| 42 | 1 | my $fh = $$reader{cf_FH}; | |||||||||||||||||||||||||||
| 43 | 1 | my @result; | |||||||||||||||||||||||||||
| 44 | 1 | ▶ | while (defined (my $paragraph = <$fh>)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 45 | 2 | ▶ | @{$$reader{cf_tokens}} = $reader->tokenize($paragraph) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | or next; | ||||||||||||||||||||||||||||
| 47 | 2 | push @result, $reader->organize_as_hash($reader->{cf_tokens}); | |||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | 1 | ▶ | wantarray ? @result : \@result; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 51 | } | ||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | 203 | ▶ | sub read_as_hash { | ||||||||||||||||||||||||||
|
Subroutine
read_as_hash: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 54 | 203 | shift->read_as(hash => @_); | |||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 205 | ▶ | sub read_as { | ||||||||||||||||||||||||||
|
Subroutine
read_as: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 58 | 205 | (my MY $reader, my ($type)) = @_; | |||||||||||||||||||||||||||
| 59 | 205 | ▶ | my $sub = $reader->can("organize_as_$type") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | or croak "Unknown read_as type: $type"; | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 205 | local $/ = ""; | |||||||||||||||||||||||||||
| 63 | 205 | my $fh = $$reader{cf_FH}; | |||||||||||||||||||||||||||
| 64 | 205 | ▶ | until ($$reader{cf_tokens} && @{$$reader{cf_tokens}}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$reader->{"cf_tokens"} and @{$reader->{"cf_tokens"};}
MC/DC100%2/2
$reader->{"cf_tokens"} and @{$reader->{"cf_tokens"};}
$reader->{"cf_tokens"}
@{$reader->{"cf_tokens"};}
Truth table100%3/3
A and B
A= $reader->{"cf_tokens"}B= @{$reader->{"cf_tokens"};}
| |||||||||||||||||||||||||||||
| 65 | 219 | ▶ | defined (my $paragraph = <$fh>) or last; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 66 | 206 | @{$$reader{cf_tokens}} = $reader->tokenize($paragraph) | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | 205 | ▶ | return unless $$reader{cf_tokens} && @{$$reader{cf_tokens}}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$reader->{"cf_tokens"} and @{$reader->{"cf_tokens"};}
MC/DC50%1/2
$reader->{"cf_tokens"} and @{$reader->{"cf_tokens"};}
$reader->{"cf_tokens"}
@{$reader->{"cf_tokens"};}
Truth table66%2/3
A and B
A= $reader->{"cf_tokens"}B= @{$reader->{"cf_tokens"};}
| |||||||||||||||||||||||||||||
| 69 | 192 | $sub->($reader, $reader->{cf_tokens}); | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | 2 | ▶ | sub organize_as_pairlist { | ||||||||||||||||||||||||||
|
Subroutine
organize_as_pairlist: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 73 | 2 | (my MY $reader, my ($tokens)) = @_; | |||||||||||||||||||||||||||
| 74 | 2 | my $hash = $reader->organize_as_hash($tokens); | |||||||||||||||||||||||||||
| 75 | 2 | %$hash; | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 198 | ▶ | sub organize_as_hash { | ||||||||||||||||||||||||||
|
Subroutine
organize_as_hash: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 79 | 198 | (my MY $reader, my ($tokens)) = @_; | |||||||||||||||||||||||||||
| 80 | 198 | my %result; | |||||||||||||||||||||||||||
| 81 | 198 | ▶ | while (@$tokens) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 639 | my $desc = shift @$tokens; | |||||||||||||||||||||||||||
| 83 | 639 | my $sigil = pop @$desc; | |||||||||||||||||||||||||||
| 84 | 639 | ▶ | if (my $type = $OPN{$sigil}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 85 | 94 | $desc->[_VALUE] = $reader->can("organize_as_$type") | |||||||||||||||||||||||||||
| 86 | ->($reader, $tokens); | ||||||||||||||||||||||||||||
| 87 | } elsif ($sigil eq '}') { | ||||||||||||||||||||||||||||
| 88 | 4 | last; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | 635 | $reader->add_value($result{$reader->decode_name($desc->[_NAME])} | |||||||||||||||||||||||||||
| 91 | , $desc->[_VALUE]); | ||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||
| 93 | 198 | \%result; | |||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 114 | ▶ | sub organize_as_array { | ||||||||||||||||||||||||||
|
Subroutine
organize_as_array: called CC=5 SCAR=16.3
Pod
undocumented
| |||||||||||||||||||||||||||||
| 97 | 114 | (my MY $reader, my ($tokens)) = @_; | |||||||||||||||||||||||||||
| 98 | 114 | my @result; | |||||||||||||||||||||||||||
| 99 | 114 | ▶ | while (@$tokens) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 100 | 313 | my $desc = shift @$tokens; | |||||||||||||||||||||||||||
| 101 | 313 | my $sigil = pop @$desc; | |||||||||||||||||||||||||||
| 102 | 313 | ▶ | unless ($desc->[_NAME] eq '') { | ||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||
| 103 | 0 | croak "Array can not have name: $desc->[_NAME]"; | |||||||||||||||||||||||||||
| 104 | } elsif (my $type = $OPN{$sigil}) { | ||||||||||||||||||||||||||||
| 105 | 24 | $desc->[_VALUE] = $reader->can("organize_as_$type") | |||||||||||||||||||||||||||
| 106 | ->($reader, $tokens); | ||||||||||||||||||||||||||||
| 107 | } elsif ($sigil eq ']') { | ||||||||||||||||||||||||||||
| 108 | 114 | last; | |||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||
| 110 | 199 | push @result, $desc->[_VALUE]; | |||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | 114 | \@result; | |||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 635 | ▶ | sub add_value { | ||||||||||||||||||||||||||
|
Subroutine
add_value: called CC=3 SCAR=11.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 116 | 635 | my MY $reader = shift; | |||||||||||||||||||||||||||
| 117 | 635 | ▶ | unless (defined $_[0]) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 118 | 634 | $_[0] = $_[1]; | |||||||||||||||||||||||||||
| 119 | } elsif (ref $_[0] ne 'ARRAY') { | ||||||||||||||||||||||||||||
| 120 | 1 | $_[0] = [$_[0], $_[1]]; | |||||||||||||||||||||||||||
| 121 | } else { | ||||||||||||||||||||||||||||
| 122 | 0 | push @{$_[0]}, $_[1]; | |||||||||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 212 | ▶ | sub tokenize { | ||||||||||||||||||||||||||
|
Subroutine
tokenize: called CC=13 SCAR=26.9
Pod
undocumented
| |||||||||||||||||||||||||||||
| 127 | 212 | my MY $reader = shift; | |||||||||||||||||||||||||||
| 128 | 212 | my ($ncomments, @result); | |||||||||||||||||||||||||||
| 129 | 212 | foreach my $token ($reader->split(my $record = shift)) { | |||||||||||||||||||||||||||
| 130 | 1027 | ▶ | if ($token =~ s{^(?:\#[^\n]*(?:\n|$))+}{}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 131 | 49 | $ncomments++; | |||||||||||||||||||||||||||
| 132 | 49 | ▶ | next if $token eq ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 978 | ▶ | unless ($token =~ s{^($cc_name*) ($cc_sigil) (?:($cc_tabsp)|(\n|$))}{}x) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | croak "Invalid XHF token: $token" | ||||||||||||||||||||||||||||
| 137 | 0 | ▶ | . (defined $reader->{cf_filename} ? " in $reader->{cf_filename}" : ""); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | 978 | my ($name, $sigil, $tabsp, $eol) = ($1, $2, $3, $4); | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | # Comment fields are ignored. | ||||||||||||||||||||||||||||
| 142 | 978 | ▶ | $ncomments++, next if $sigil eq "#"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | # Line continuation. | ||||||||||||||||||||||||||||
| 145 | 978 | $token =~ s/\n[\ \t]/\n/g; | |||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||
| 147 | 978 | ▶ | unless (defined $eol) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 148 | # Values are trimmed unless $eol | ||||||||||||||||||||||||||||
| 149 | 462 | $token =~ s/^\s+|\s+$//gs; | |||||||||||||||||||||||||||
| 150 | } elsif ($OPN{$sigil}) { | ||||||||||||||||||||||||||||
| 151 | # Prohibit: | ||||||||||||||||||||||||||||
| 152 | # name{ foo | ||||||||||||||||||||||||||||
| 153 | # name[ foo | ||||||||||||||||||||||||||||
| 154 | croak "Invalid XHF token(container with value): " | ||||||||||||||||||||||||||||
| 155 | 0 | ▶ | . join("", grep {defined $_} $name, $sigil, $tabsp, $token) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 156 | if $token ne ""; | ||||||||||||||||||||||||||||
| 157 | } else { | ||||||||||||||||||||||||||||
| 158 | # Trim leading space for $tabsp eq "\n". | ||||||||||||||||||||||||||||
| 159 | 394 | $token =~ s/^[\ \t]//; | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | 978 | push @result, [$name, $token, $sigil]; | |||||||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | # Comment only paragraph should return nothing. | ||||||||||||||||||||||||||||
| 165 | 212 | ▶ | return if $ncomments && !@result; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$ncomments and not @result
MC/DC100%2/2
$ncomments and not @result
$ncomments
not @result
Truth table100%3/3
A and not(B)
A= $ncommentsB= not @result
| |||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 198 | ▶ | wantarray ? @result : \@result; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 212 | ▶ | sub split { | ||||||||||||||||||||||||||
|
Subroutine
split: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 171 | 212 | (my MY $reader, my ($record)) = @_; | |||||||||||||||||||||||||||
| 172 | # XXX: Can avoid copy. | ||||||||||||||||||||||||||||
| 173 | 212 | $record =~ s{\n+$}{\n}s; | |||||||||||||||||||||||||||
| 174 | 212 | split /(?<=\n)(?=[^\ \t])/, $record; | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 635 | ▶ | sub decode_name { | ||||||||||||||||||||||||||
|
Subroutine
decode_name: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 178 | 635 | (my MY $reader, my ($name)) = @_; | |||||||||||||||||||||||||||
| 179 | 635 | $name =~ s{%([\da-f]{2})}{pack("C", hex($1))}egxi; | |||||||||||||||||||||||||||
| 180 | 635 | $name; | |||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | 3 | 1; | |||||||||||||||||||||||||||
| 184 | __END__ | ||||||||||||||||||||||||||||