blib/lib/blx/xsdsql/ios/istream.pm untested

filter: Statementstmt 0.0% 0 / 201 Branchbran 0.0% 0 / 114 Conditioncond 0.0% 0 / 30 MC/DCmcdc 0.0% 0 / 20 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 387 CC 45 SCAR 53.5
CC
45
Cov
0%
get_chars
420.0
get_line
210.0
_init_input_stream
72.0
CRAP 211.1
1 package blx::xsdsql::ios::istream;
2 use strict; # use strict is for PBP
3 use Filter::Include;
4 include blx::xsdsql::include;
5 #line 6
6 use base qw(Exporter);
7  
8 use overload "<>" => \&get_line;
9 #warning - exist a bug into package overload
10 #calling @y=<$x> the operator <> return a one line (wantarray is false)
11  
12 my %t=( overload => [ ( "<>" ) ]);
13 our %EXPORT_TAGS=( all => [ map { @{$t{$_}} } keys %t ],%t);
14 our @EXPORT_OK=( @{$EXPORT_TAGS{all}} );
15 our @EXPORT=qw( );
16  
17 sub _pop_str { #pop n chars from str pointer
18 my ($p,$n)=@_;
19 my $l=length($$p);
20 my $s=substr($$p,$l - $n);
21 $$p=substr($$p,0,$l - $n);
22 return defined wantarray ? $s : undef;
23 }
24  
25 sub _push_str { # push s to str pointer
26 my ($p,$s,$maxsize)=@_;
27 my $r=$$p.$s;
28 $$p=substr($r,length($r) - $maxsize);
29 return defined wantarray ? $$p : undef;
30 }
31  
32 sub _init_input_stream {
33 my ($self,%params)=@_;
34 return $self unless defined $self->{INPUT_STREAM};
35 my $r=ref($self->{INPUT_STREAM});
36 if ($r eq '') { #string
37 $self->{I}->{P}=0; #current position
38 }
39 elsif ($r eq 'ARRAY') {
40 $self->{I}={ R => 0,P => 0}; #current index + current position
41 }
42 elsif ($r eq 'SCALAR') { #reference to scalar
43 $self->{I}->{P}=0; #current index
44 }
45 elsif ($self->{INPUT_STREAM} eq *STDIN || $r eq 'GLOB') {
46 $params{MAX_PUSHBACK_SIZE}=0 unless defined $params{MAX_PUSHBACK_SIZE};
47 affirm { $params{MAX_PUSHBACK_SIZE}=~/^\d+$/ } $params{MAX_PUSHBACK_SIZE}.': invalid param value MAX_PUSHBACK_SIZE';
48 my $s='';
49 $self->{BUFFER}=\$s;
50 $self->{PUSHBACK_N}=0;
51 $self->{MAX_PUSHBACK_SIZE}=$params{MAX_PUSHBACK_SIZE};
52 }
53 elsif ($r eq 'CODE') {
54 #empty
55 }
56 else {
57 croak $r.': type non implemented';
58 }
59 return $self;
60 }
61  
62  
63 sub new {
64 my ($class,%params)=@_;
65 my $max_pushback_size=delete $params{MAX_PUSHBACK_SIZE};
66 my $self=bless(\%params,$class);
67 return $self->_init_input_stream(MAX_PUSHBACK_SIZE => $max_pushback_size);
68 }
69  
70  
71 sub set_input_descriptor {
72 my ($self,$fd,%params)=@_;
73 $self->{INPUT_STREAM}=$fd;
74 return $self->_init_input_stream(%params);
75 }
76  
77  
78 sub get_chars {
79 my ($self,$n,%params)=@_;
80 $n=1 unless defined $n;
81 my $stream=$self->{INPUT_STREAM};
82 affirm { defined $stream } "INPUT_STREAM non set";
83 affirm { $n=~/^\d+$/ } "$n: invalid first param value";
84 return '' if $n == 0;
85 my $r=ref($stream);
86 if ($stream eq *STDIN || ref($stream) eq 'GLOB') {
87 my $outs='';
88 if ($self->{PUSHBACK_N}) {
89 my $m=$n > $self->{PUSHBACK_N} ? $self->{PUSHBACK_N} : $n;
90 $outs=_pop_str($self->{BUFFER},$m);
91 $n -= $m;
92 $self->{PUSHBACK_N}-=$m;
93 }
94 if ($n) {
95 my $s=undef;
96 my $r=read $stream,$s,$n;
97 croak "$!" unless defined $r;
98 $s='' if $r == 0;
99 if ($self->{MAX_PUSHBACK_SIZE} && length($s) > 0) {
100 _push_str($self->{BUFFER},$s,$self->{MAX_PUSHBACK_SIZE});
101 }
102 $outs.=$s;
103 }
104 return $outs;
105 }
106 elsif ($r eq '') { #string
107 return '' if $self->{I}->{P} >= length($stream);
108 my $s=substr($stream,$self->{I}->{P},$n);
109 $self->{I}->{P} += $n;
110 return $s;
111 }
112 elsif ($r eq 'ARRAY') {
113 return '' if $self->{I}->{R} >= scalar(@{$self->{INPUT_STREAM}});
114 my $s='';
115 while($self->{I}->{R} < scalar(@$stream) && length($s) < $n) {
116 my $e=$self->{INPUT_STREAM}->[$self->{I}->{R}];
117 $e='' unless defined $e;
118 $s .= "\n" if $self->{I}->{P} == 0 && $self->{I}->{R} > 0;
119 my $m=$n - length($s);
120 $s .= substr($e,$self->{I}->{P},$m);
121 $self->{I}->{P} += $m;
122 if ($self->{I}->{P} >= length($e)) {
123 $self->{I}->{P} = 0;
124 ++$self->{I}->{R};
125 }
126 }
127 return $s;
128 }
129 elsif ($r eq 'CODE') {
130 return $stream->($self,@_);
131 }
132 elsif ($r eq 'SCALAR') {
133 return '' if $self->{I}->{P} >= length($$stream);
134 my $s=substr($$stream,$self->{I}->{P},$n);
135 $self->{I}->{P} += $n;
136 return $s;
137 }
138 else {
139 croak $r.': type non implemented';
140 }
141 undef;
142 }
143  
144 sub get_char {
145 my ($self,%params)=@_;
146 return $self->get_chars(1,%params);
147 }
148  
149 sub get_line {
150 my $self=shift;
151 return <$self> if ref($self) ne __PACKAGE__;
152 my $stream=$self->{INPUT_STREAM};
153 affirm { defined $stream } "INPUT_STREAM non set";
154  
155 my $r=ref($stream);
156 if (wantarray) {
157 if ($stream eq *STDIN || $r eq 'GLOB') { #use the optimized version for file descriptor
158 affirm { ! $self->{PUSHBACK_N} } "push back not implemented for common streams";
159 return <$stream>;
160 }
161 my @s=();
162 while(my $s=$self->get_line) {
163 push @s,$s;
164 }
165 return @s;
166 }
167  
168 if ($stream eq *STDIN || $r eq 'GLOB') {
169 affirm { ! $self->{PUSHBACK_N} } "push back not implemented for common streams";
170 my $s=<$stream>;
171 return $s;
172 }
173 elsif ($r eq '') { #string
174 return if $self->{I}->{P} >= length($stream);
175 my $s='';
176 while($self->{I}->{P} < length($stream)) {
177 my $c=substr($stream,$self->{I}->{P}++,1);
178 $s .= $c;
179 last if $c eq "\n";
180 }
181 return $s;
182 }
183 elsif ($r eq 'ARRAY') {
184 return if $self->{I}->{R} >= scalar(@$stream);
185 return $stream->[$self->{I}->{R}++]."\n";
186 }
187 elsif ($r eq 'CODE') {
188 affirm { ! $self->{PUSHBACK_N} } "push back not implemented for code streamer";
189 return $stream->($self,@_);
190 }
191 elsif ($r eq 'SCALAR') {
192 return if $self->{I}->{P} >= length($$stream);
193 my $s='';
194 while($self->{I}->{P} < length($$stream)) {
195 my $c=substr($$stream,$self->{I}->{P}++,1);
196 $s .= $c;
197 last if $c eq "\n";
198 }
199 return $s;
200 }
201 else {
202 croak $r.': type non implemented';
203 }
204 undef;
205 }
206  
207 sub push_back {
208 my ($self,$n,%params)=@_;
209 my $stream=$self->{INPUT_STREAM};
210 affirm { defined $stream } "INPUT_STREAM non set";
211 $n=1 unless defined $n;
212 affirm { $n=~/^\d+$/ } "$n: invalid first param value";
213 my $r=ref($stream);
214 if ($stream eq *STDIN || ref($stream) eq 'GLOB') {
215 $self->{PUSHBACK_N} += $n;
216 affirm {
217 $self->{PUSHBACK_N} <= $self->{MAX_PUSHBACK_SIZE}
218 &&
219 $self->{PUSHBACK_N} <= length(${$self->{BUFFER}})
220 } "pushback oveflow ";
221 }
222 elsif ($r eq '' || $r eq 'SCALAR') {
223 $self->{I}->{P} -= $n;
224 affirm { $self->{I}->{P} >= 0 } "$n: invalid value for push_back";
225 }
226 else {
227 croak "$r: push_back not implemented for this type";
228 }
229 return $self;
230 }
231  
232 if (__FILE__ eq $0) {
233  
234 my @arr=();
235 my $streamer=__PACKAGE__->new(INPUT_STREAM => \@arr);
236 my $s=$streamer->get_chars(1);
237 confess "check failed " if length($s);
238  
239 @arr= qw(pippo pluto paperino);
240 $streamer=__PACKAGE__->new(INPUT_STREAM => \@arr);
241 $s=$streamer->get_chars(10);
242 confess "check failed " unless $s eq "pippo\nplut";
243 $s=$streamer->get_chars(3);
244 confess "check failed " unless $s eq "o\np";
245  
246 $s=$streamer->get_chars(3000);
247 confess "check failed " unless $s eq "aperino";
248  
249 $s=$streamer->get_chars(3000);
250 confess "check failed " if length($s);
251
252  
253  
254 my $src=join("\n",@arr);
255 $streamer=__PACKAGE__->new(INPUT_STREAM => \$src);
256 $s=$streamer->get_chars(10);
257 confess "check failed " unless $s eq "pippo\nplut";
258 $s=$streamer->get_chars(3);
259 confess "check failed " unless $s eq "o\np";
260  
261 $s=$streamer->get_chars(3000);
262 confess "check failed " unless $s eq "aperino";
263  
264 $s=$streamer->get_chars(3000);
265 confess "check failed " if length($s);
266  
267  
268 $streamer->set_input_descriptor(\@arr);
269 my @t=$streamer->get_line;
270 confess "check failed " if @t ne @arr;
271  
272 $streamer->set_input_descriptor(\@arr);
273 @t=<$streamer>;
274 print STDERR "(W) check failed - overload bug in ",__PACKAGE__," line ",__LINE__," - continue \n" if @t ne @arr;
275  
276 $streamer->set_input_descriptor(\$src);
277 @t=$streamer->get_line;
278 confess "check failed " if @t ne @arr;
279  
280 }
281  
282 1;
283  
284 __END__