Storage/Std.pm untested

filter: Statementstmt 0.0% 0 / 165 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 52 Podpod 0.0% 0 / 50 totaltotal 0.0% 0 / 294 CC 12 SCAR 13.5
CC
12
Cov
0%
get_zstr
12.0
get_zwstr
12.0
get_nbyte
6.0
CRAP 3.8
1 package OLE::Storage::Std;
2 no strict;
3 my $VERSION=do{my@R=('$Revision: 1.2 $'=~/\d+/g);sprintf"%d."."%d"x$#R,@R};
4  
5 #
6 # I decided to put read_* functions here and not in package Storage::Io
7 # to bind them close to get_* etc. functions...
8 #
9  
10 use Exporter;
11 @ISA = (Exporter);
12 @EXPORT = qw(
13 packpar basename
14 byte nbyte get_byte get_nbyte read_byte read_nbyte
15 word nword get_word get_nword read_word read_nword
16 long nlong get_long get_nlong read_long read_nlong
17 struct get_struct
18 get_str
19 get_zstr
20 wstr nwstr
21 get_zwstr
22 get_rzwstr
23 real nreal get_real get_nreal read_real read_nreal
24 double ndouble get_double get_ndouble read_double read_ndouble
25 );
26  
27 sub B () { "C" } sub BS () { 1 }
28 sub W () { "v" } sub WS () { 2 }
29 sub L () { "V" } sub LS () { 4 }
30 sub R () { "f" } sub RS () { 4 }
31 sub D () { "d" } sub DS () { 8 }
32  
33 ##
34 ## EXPORT functions, will be exported by default.
35 ##
36  
37 # thing ($number)
38 sub byte { pack (B, $_[0]) }
39 sub word { pack (W, $_[0]) }
40 sub long { pack (L, $_[0]) }
41 sub real { pack (R, $_[0]) }
42 sub double { pack (D, $_[0]) }
43  
44 # nthing (\@list)
45 sub nbyte { pack (B.($#{$_[0]}+1), @{$_[0]}) }
46 sub nword { pack (W.($#{$_[0]}+1), @{$_[0]}) }
47 sub nlong { pack (L.($#{$_[0]}+1), @{$_[0]}) }
48 sub nreal { pack (R.($#{$_[0]}+1), @{$_[0]}) }
49 sub ndouble { pack (D.($#{$_[0]}+1), @{$_[0]}) }
50  
51 # struct ($struct, \@list)
52 sub struct { pack ((packpar($_[0]))[0], @{$_[1]}) }
53  
54 # $wstr = wstr(perlstr)
55 sub wstr { join("\0",split(//, $_[0]))."\0" }
56 sub nwstr { map (wstr($_), @_) }
57  
58 sub packpar {
59 #
60 # ($packstr, $varsize) = packpar ($str)
61 #
62 my $str = shift;
63 my $F; my $len = 0;
64 $F = B(); $len += ($str =~ s/B/$F/g) * BS;
65 $F = W(); $len += ($str =~ s/W/$F/g) * WS;
66 $F = L(); $len += ($str =~ s/L/$F/g) * LS;
67 $F = R(); $len += ($str =~ s/R/$F/g) * RS;
68 $F = D(); $len += ($str =~ s/D/$F/g) * DS;
69 ($str, $len);
70 }
71  
72 # get_thing (\$buf, $offset);
73 sub get_byte { get_nbyte(1, @_) }
74 sub get_word { get_nword(1, @_) }
75 sub get_long { get_nlong(1, @_) }
76 sub get_real { get_nreal(1, @_) }
77 sub get_double { get_ndouble(1, @_) }
78  
79 # get_nthing ($n, \$buf, $o||\$o);
80 sub get_nbyte {
81 if (ref($_[2])) {
82 ${$_[2]}+=$_[0]*BS;
83 unpack (B."$_[0]", substr(${$_[1]}, ${$_[2]}-$_[0]*BS, $_[0]*BS))
84 } else {
85 unpack (B."$_[0]", substr(${$_[1]}, $_[2], $_[0]*BS))
86 }
87 }
88 sub get_nword {
89 if (ref($_[2])) {
90 ${$_[2]}+=$_[0]*WS;
91 unpack (W."$_[0]", substr(${$_[1]}, ${$_[2]}-$_[0]*WS, $_[0]*WS))
92 } else {
93 unpack (W."$_[0]", substr(${$_[1]}, $_[2], $_[0]*WS))
94 }
95 }
96 sub get_nlong {
97 if (ref($_[2])) {
98 ${$_[2]}+=$_[0]*LS;
99 unpack (L."$_[0]", substr(${$_[1]}, ${$_[2]}-$_[0]*LS, $_[0]*LS))
100 } else {
101 unpack (L."$_[0]", substr(${$_[1]}, $_[2], $_[0]*LS))
102 }
103 }
104 sub get_nreal {
105 if (ref($_[2])) {
106 ${$_[2]}+=$_[0]*RS;
107 unpack (R."$_[0]", substr(${$_[1]}, ${$_[2]}-$_[0]*RS, $_[0]*RS))
108 } else {
109 unpack (R."$_[0]", substr(${$_[1]}, $_[2], $_[0]*RS))
110 }
111 }
112 sub get_ndouble {
113 if (ref($_[2])) {
114 ${$_[2]}+=$_[0]*DS;
115 unpack (D."$_[0]", substr(${$_[1]}, ${$_[2]}-$_[0]*DS, $_[0]*DS))
116 } else {
117 unpack (D."$_[0]", substr(${$_[1]}, $_[2], $_[0]*DS))
118 }
119 }
120  
121 # get_struct ($struct, \$buf, $o||\$o)
122 sub get_struct {
123 my @PV = packpar(shift);
124 if (ref($_[1])) {
125 ${$_[1]} += $PV[1];
126 unpack ($PV[0], substr(${$_[0]}, ${$_[1]}-$PV[1], $PV[1]));
127 } else {
128 unpack ($PV[0], substr(${$_[0]}, $_[1], $PV[1]));
129 }
130 }
131  
132 # get_str (\$buf, $o||\$o, $len)
133 sub get_str {
134 if (ref($_[1])) {
135 ${$_[1]}+=$_[2];
136 substr(${$_[0]}, ${$_[1]}-$_[2], $_[2])
137 } else {
138 substr(${$_[0]}, $_[1], $_[2])
139 }
140 }
141 sub get_zstr {
142 return "" if !$_[2];
143 if (ref($_[1])) {
144 ${$_[1]}+=$_[2];
145 substr(${$_[0]}, ${$_[1]}-$_[2], $_[2]-1)
146 } else {
147 substr(${$_[0]}, $_[1], $_[2]-1)
148 }
149 }
150 sub get_zwstr {
151 return "" if !$_[2];
152 if (ref($_[1])) {
153 ${$_[1]}+=$_[2];
154 substr(${$_[0]}, ${$_[1]}-$_[2], $_[2]-2);
155 } else {
156 substr(${$_[0]}, $_[1], $_[2]-2);
157 }
158 }
159 sub get_rzwstr {
160 my $tmp = get_zwstr(@_);
161 reverse_unicode Unicode::Map ($tmp);
162 $tmp;
163 }
164  
165 # read_thing ($Io, $offset);
166 sub read_byte { read_nbyte(1, @_) }
167 sub read_word { read_nword(1, @_) }
168 sub read_long { read_nlong(1, @_) }
169 sub read_real { read_nreal(1, @_) }
170 sub read_double { read_ndouble(1, @_) }
171  
172 # read_thing ($n, $Io, $offset);
173 sub read_nbyte {
174 my $l=shift; my $b=""; $_[0]->read($_[1], $l*BS, \$b); unpack (B."$l", $b)
175 }
176 sub read_nword {
177 my $l=shift; my $b=""; $_[0]->read($_[1], $l*WS, \$b); unpack (W."$l", $b)
178 }
179 sub read_nlong {
180 my $l=shift; my $b=""; $_[0]->read($_[1], $l*LS, \$b); unpack (L."$l", $b)
181 }
182 sub read_nreal {
183 my $l=shift; my $b=""; $_[0]->read($_[1], $l*RS, \$b); unpack (R."$l", $b)
184 }
185 sub read_ndouble {
186 my $l=shift; my $b=""; $_[0]->read($_[1], $l*DS, \$b); unpack (D."$l", $b)
187 }
188  
189 sub basename {
190 #
191 # $basename = basename($filepath)
192 #
193 (substr($_[0], rindex($_[0],'/')+1) =~ /(^[^.]*)/) && $1;
194 }
195  
196 "Atomkraft? Nein, danke!"
197