blib/lib/Data/Buffer.pm

filter: Statementstmt 99.1% 110 / 111 Branchbran 71.0% 27 / 38 Conditioncond 100% 2 / 2 MC/DCmcdc 100% 1 / 1 Subroutinesub 100% 28 / 28 Podpod 88.4% 23 / 26 totaltotal 92.7% 191 / 206 CC 24 SCAR 12.6
CC
24
Cov
92%
get_all
10.1
bytes
4.0
put_bytes
3.1
CRAP 3.5
↑ Summary
1 # $Id: Buffer.pm,v 1.9 2001/07/28 06:36:50 btrott Exp $
2  
3 package Data::Buffer;
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 our $VERSION = '0.06'; # VERSION
8  
9 5 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
10 5 my $class = shift;
11 5 my %arg = @_;
12 5 bless { buf => "", offset => 0, template => "" }, $class;
13 }
14  
15 2 ▶sub new_with_init {
Subroutine new_with_init: called CC=2 SCAR=6.9
Pod documented
16 2 my $class = shift;
17 2 my $buf = $class->new;
18 2 $buf->append($_) for @_;
19 2 $buf;
20 }
21  
22 1 ▶sub extract {
Subroutine extract: called CC=1 SCAR=0.0
Pod documented
23 1 my $buf = shift;
24 1 my($nbytes) = @_;
25 1 my $new = ref($buf)->new;
26 1 $new->append( $buf->get_bytes($nbytes) );
27 1 $new;
28 }
29  
30 2 ▶sub empty {
Subroutine empty: called CC=1 SCAR=0.0
Pod documented
31 2 my $buf = shift;
32 2 $buf->{buf} = "";
33 2 $buf->{offset} = 0;
34 2 $buf->{template} = "";
35 }
36  
37 2 ▶sub set_offset { $_[0]->{offset} = $_[1] }
Subroutine set_offset: called CC=1 SCAR=0.0
Pod documented
38 1 ▶sub reset_offset { $_[0]->set_offset(0) }
Subroutine reset_offset: called CC=1 SCAR=0.0
Pod documented
39  
40 1 ▶sub insert_template {
Subroutine insert_template: called CC=1 SCAR=0.0
Pod undocumented
41 1 my $buf = shift;
42 1 $buf->bytes(0, 0, $buf->{template} . chr(0));
43 }
44  
45 6 ▶sub append {
Subroutine append: called CC=1 SCAR=0.0
Pod documented
46 6 my $buf = shift;
47 6 $buf->{buf} .= $_[0];
48 }
49  
50 41 ▶sub bytes {
Subroutine bytes: called CC=4 SCAR=13.9
Pod documented
51 41 my $buf = shift;
52 41 my($off, $len, $rep) = @_;
53 41 ▶ $off ||= 0;
Condition100%2/2
$off ||= 0
l!l
2912
MC/DC100%1/1
$off ||= 0
$off
Truth table100%2/2
A
A= $off
Aresult
11
00
54 41 ▶ $len = length $buf->{buf} unless defined $len;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $len 7 34 41
55 return defined $rep ?
56 substr($buf->{buf}, $off, $len, $rep) :
57 41 ▶ substr($buf->{buf}, $off, $len);
Branch: 1 branch
BranchTrueFalseTotal
defined $rep ? : 1 40 41
58 }
59  
60 9 ▶sub length { length $_[0]->{buf} }
Subroutine length: called CC=1 SCAR=0.0
Pod documented
61 7 ▶sub offset { $_[0]->{offset} }
Subroutine offset: called CC=1 SCAR=0.0
Pod documented
62 1 ▶sub template { $_[0]->{template} }
Subroutine template: called CC=1 SCAR=0.0
Pod undocumented
63  
64 3 ▶sub dump {
Subroutine dump: called CC=3 SCAR=11.0
Pod documented
65 3 my $buf = shift;
66 3 my @r;
67 3 for my $c (split //, $buf->bytes(@_)) {
68 3 push @r, sprintf "%02x", ord $c;
69 3 ▶ push @r, "\n" unless @r % 24;
Branch: 1 branch
BranchTrueFalseTotal
unless @r % 24 0 3 3
70 }
71 3 join ' ', @r
72 }
73  
74 1 ▶sub get_all {
Subroutine get_all: called CC=10 SCAR=23.1
Pod undocumented
75 1 my $buf = shift;
76 1 ▶ my($tmpl, $data) = $buf->{buf} =~ /^([NYaCn\d]+)\0(.+)$/s or
Branch: 1 branch
BranchTrueFalseTotal
() = $buf->{"buf"} =~ /^([NYaCn\d]+)\0(.+)$/s or die "Buffer $buf does not appear to contain a template" 1 0 1
77 die "Buffer $buf does not appear to contain a template";
78 1 my $b = __PACKAGE__->new;
79 1 $b->append($data);
80 1 my @tmpl = split //, $tmpl;
81 1 my @data;
82 1 ▶ while (@tmpl) {
Branch: 1 branch
BranchTrueFalseTotal
if (@tmpl) 10 1 11
83 10 my $el = shift @tmpl;
84 10 ▶ if ($el eq "N") {
Branch: 5 branches
BranchTrueFalseTotal
if ($el eq 'N') { } 3 7 10
elsif ($el eq 'n') { } 1 6 7
elsif ($el eq 'C') { } 1 5 6
elsif ($el eq 'a') { } 3 2 5
elsif ($el eq 'Y') { } 2 0 2
85 3 ▶ next if $tmpl[0] eq "Y"; ## Peek ahead: is it a string?
Branch: 1 branch
BranchTrueFalseTotal
if $tmpl[0] eq "Y" 2 1 3
86 1 push @data, $b->get_int32;
87 }
88 elsif ($el eq "n") {
89 1 push @data, $b->get_int16;
90 }
91 elsif ($el eq "C") {
92 1 push @data, $b->get_int8;
93 }
94 elsif ($el eq "a") {
95 3 my $len = shift @tmpl;
96 3 push @data, $b->get_char for 1..$len;
97 }
98 elsif ($el eq "Y") {
99 2 push @data, $b->get_str;
100 }
101 else {
102 0 die "Unrecognized template token: $el";
103 }
104 }
105 1 @data;
106 }
107  
108 2 ▶sub get_int8 {
Subroutine get_int8: called CC=2 SCAR=7.0
Pod documented
109 2 my $buf = shift;
110 2 ▶ my $off = defined $_[0] ? shift : $buf->{offset};
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 0 2 2
111 2 $buf->{offset} += 1;
112 2 unpack "C", $buf->bytes($off, 1);
113 }
114  
115 1 ▶sub put_int8 {
Subroutine put_int8: called CC=1 SCAR=0.0
Pod documented
116 1 my $buf = shift;
117 1 $buf->{buf} .= pack "C", $_[0];
118 1 $buf->{template} .= "C";
119 }
120  
121 4 ▶sub get_int16 {
Subroutine get_int16: called CC=2 SCAR=7.0
Pod documented
122 4 my $buf = shift;
123 4 ▶ my $off = defined $_[0] ? shift : $buf->{offset};
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 0 4 4
124 4 $buf->{offset} += 2;
125 4 unpack "n", $buf->bytes($off, 2);
126 }
127  
128 2 ▶sub put_int16 {
Subroutine put_int16: called CC=1 SCAR=0.0
Pod documented
129 2 my $buf = shift;
130 2 $buf->{buf} .= pack "n", $_[0];
131 2 $buf->{template} .= "n";
132 }
133  
134 6 ▶sub get_int32 {
Subroutine get_int32: called CC=2 SCAR=7.0
Pod documented
135 6 my $buf = shift;
136 6 ▶ my $off = defined $_[0] ? shift : $buf->{offset};
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 0 6 6
137 6 $buf->{offset} += 4;
138 6 unpack "N", $buf->bytes($off, 4);
139 }
140  
141 3 ▶sub put_int32 {
Subroutine put_int32: called CC=1 SCAR=0.0
Pod documented
142 3 my $buf = shift;
143 3 $buf->{buf} .= pack "N", $_[0];
144 3 $buf->{template} .= "N";
145 }
146  
147 13 ▶sub get_char {
Subroutine get_char: called CC=2 SCAR=7.0
Pod documented
148 13 my $buf = shift;
149 13 ▶ my $off = defined $_[0] ? shift : $buf->{offset};
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 0 13 13
150 13 $buf->{offset}++;
151 13 $buf->bytes($off, 1);
152 }
153  
154 2 ▶sub put_char {
Subroutine put_char: called CC=1 SCAR=0.0
Pod documented
155 2 my $buf = shift;
156 2 $buf->{buf} .= $_[0];
157 2 $buf->{template} .= "a" . CORE::length($_[0]);
158 }
159  
160 4 ▶sub get_bytes {
Subroutine get_bytes: called CC=1 SCAR=0.0
Pod documented
161 4 my $buf = shift;
162 4 my($nbytes) = @_;
163 4 my $d = $buf->bytes($buf->{offset}, $nbytes);
164 4 $buf->{offset} += $nbytes;
165 4 $d;
166 }
167  
168 1 ▶sub put_bytes {
Subroutine put_bytes: called CC=3 SCAR=11.4
Pod documented
169 1 my $buf = shift;
170 1 my($str, $nbytes) = @_;
171 1 ▶ $buf->{buf} .= $nbytes ? substr($str, 0, $nbytes) : $str;
Branch: 1 branch
BranchTrueFalseTotal
$nbytes ? : 1 0 1
172 1 ▶ $buf->{template} .= "a" . ($nbytes ? $nbytes : CORE::length($str));
Branch: 1 branch
BranchTrueFalseTotal
$nbytes ? : 1 0 1
173 }
174  
175 1 *put_chars = \&put_char;
176  
177 4 ▶sub get_str {
Subroutine get_str: called CC=2 SCAR=7.0
Pod documented
178 4 my $buf = shift;
179 4 ▶ my $off = defined $_[0] ? shift : $buf->{offset};
Branch: 1 branch
BranchTrueFalseTotal
defined $_[0] ? : 0 4 4
180 4 my $len = $buf->get_int32;
181 4 $buf->{offset} += $len;
182 4 $buf->bytes($off+4, $len);
183 }
184  
185 2 ▶sub put_str {
Subroutine put_str: called CC=2 SCAR=7.0
Pod documented
186 2 my $buf = shift;
187 2 my $str = shift;
188 2 ▶ $str = "" unless defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 0 2 2
189 2 $buf->put_int32(CORE::length($str));
190 2 $buf->{buf} .= $str;
191 2 $buf->{template} .= "Y";
192 }
193  
194 1 1;
195 __END__
↑ Summary