« blib/lib/blx/xsdsql/connection/sql/connection.pm
↑ Summary
blib/lib/blx/xsdsql/schema_repository/sql/oracle/cursor.pm »
| 1 | package blx::xsdsql::ios::ostream; | ||
| 2 | use strict; # use strict is for PBP | ||
| 3 | use Filter::Include; | ||
| 4 | include blx::xsdsql::include; | ||
| 5 | #line 6 | ||
| 6 | |||
| 7 | use base qw(Exporter); | ||
| 8 | use subs qw(print say binmode); | ||
| 9 | |||
| 10 | my %t=( overload => [ qw ( print say binmode) ]); | ||
| 11 | our %EXPORT_TAGS=( all => [ map { @{$t{$_}} } keys %t ],%t); | ||
| 12 | our @EXPORT_OK=( @{$EXPORT_TAGS{all}} ); | ||
| 13 | our @EXPORT=qw( ); | ||
| 14 | |||
| 15 | sub _init_output_stream { | ||
| 16 | my ($self,%params)=@_; | ||
| 17 | $self->{LINE_SEPARATOR}=$params{LINE_SEPARATOR} if defined $params{LINE_SEPARATOR}; | ||
| 18 | return $self unless defined $self->{OUTPUT_STREAM}; | ||
| 19 | my $r=ref($self->{OUTPUT_STREAM}); | ||
| 20 | if ($r eq 'ARRAY') { | ||
| 21 | $self->{O}->{I}=0; | ||
| 22 | $self->{LINE_SEPARATOR}="\n" unless defined $self->{LINE_SEPARATOR}; | ||
| 23 | } | ||
| 24 | elsif ($r eq 'CODE') { | ||
| 25 | $self->{_OUTPUT_BUFFER}=""; | ||
| 26 | } | ||
| 27 | return $self; | ||
| 28 | } | ||
| 29 | |||
| 30 | |||
| 31 | sub new { | ||
| 32 | my ($class,%params)=@_; | ||
| 33 | my $s=bless(\%params,$class); | ||
| 34 | return $s->_init_output_stream(%params); | ||
| 35 | } | ||
| 36 | |||
| 37 | sub set_output_descriptor { | ||
| 38 | my ($self,$fd,%params)=@_; | ||
| 39 | $self->{OUTPUT_STREAM}=$fd; | ||
| 40 | return $self->_init_output_stream(%params); | ||
| 41 | } | ||
| 42 | |||
| 43 | sub mode { | ||
| 44 | my ($self,$value,%params)=@_; | ||
| 45 | my $stream = $self->{OUTPUT_STREAM}; | ||
| 46 | affirm { defined $stream } "OUTPUT_STREAM not set"; | ||
| 47 | |||
| 48 | if ($stream eq *STDOUT || $stream eq *STDERR || ref($stream) eq 'GLOB') { | ||
| 49 | my $r=CORE::binmode($stream,$value); | ||
| 50 | croak "error: $!" unless defined $r; | ||
| 51 | } | ||
| 52 | $self->{BINMODE}=$value; | ||
| 53 | return $self; | ||
| 54 | } | ||
| 55 | |||
| 56 | sub put_chars { | ||
| 57 | my $self=shift; | ||
| 58 | my $stream = $self->{OUTPUT_STREAM}; | ||
| 59 | affirm { defined $stream } "OUTPUT_STREAM not set"; | ||
| 60 | |||
| 61 | if ($stream eq *STDOUT || $stream eq *STDERR || ref($stream) eq 'GLOB') { | ||
| 62 | my $r=CORE::print $stream @_; | ||
| 63 | croak "error: $!" unless defined $r; | ||
| 64 | } | ||
| 65 | elsif (ref($stream) eq '') { #string | ||
| 66 | $self->{OUTPUT_STREAM} .= join('',@_); | ||
| 67 | } | ||
| 68 | elsif (ref($stream) eq 'ARRAY') { | ||
| 69 | for my $i(0..scalar(@_) - 1) { | ||
| 70 | my $s=$_[$i]; | ||
| 71 | affirm { defined $s } "element $i is not defined"; | ||
| 72 | my $n=index($s,$self->{LINE_SEPARATOR}); | ||
| 73 | while($n >= 0) { | ||
| 74 | my $p=substr($s,0,$n); | ||
| 75 | $stream->[$self->{O}->{I}]='' | ||
| 76 | unless defined $self->{OUTPUT_STREAM}->[$self->{O}->{I}]; | ||
| 77 | $stream->[$self->{O}->{I}].=$p; | ||
| 78 | $s=substr($s,$n + length($self->{LINE_SEPARATOR})); | ||
| 79 | $n=index($s,$self->{LINE_SEPARATOR}); | ||
| 80 | $stream->[++$self->{O}->{I}]=''; | ||
| 81 | } | ||
| 82 | $stream->[$self->{O}->{I}].=$s; | ||
| 83 | } | ||
| 84 | } | ||
| 85 | elsif (ref($stream) eq 'CODE') { | ||
| 86 | if (defined $self->{LINE_SEPARATOR} && length($self->{LINE_SEPARATOR})) { | ||
| 87 | for my $i(0..scalar(@_) - 1) { | ||
| 88 | my $s=$_[$i]; | ||
| 89 | affirm { defined $s } "element $i is not defined"; | ||
| 90 | $s=$self->{_OUTPUT_BUFFER}.$s; | ||
| 91 | my $n=index($s,$self->{LINE_SEPARATOR}); | ||
| 92 | while($n >= 0) { | ||
| 93 | my $p=substr($s,0,$n + length($self->{LINE_SEPARATOR})); | ||
| 94 | $stream->($self,$p); | ||
| 95 | if (($n + length($self->{LINE_SEPARATOR})) > length($s)) { | ||
| 96 | $s=''; | ||
| 97 | $n=-1; | ||
| 98 | } | ||
| 99 | else { | ||
| 100 | $s=substr($s,$n + length($self->{LINE_SEPARATOR})); | ||
| 101 | $n=index($s,$self->{LINE_SEPARATOR}); | ||
| 102 | } | ||
| 103 | } | ||
| 104 | $self->{_OUTPUT_BUFFER}=$s; | ||
| 105 | } | ||
| 106 | } | ||
| 107 | else { | ||
| 108 | $stream->($self,@_); | ||
| 109 | } | ||
| 110 | } | ||
| 111 | elsif (ref($stream) eq 'SCALAR') { #reference to scalar | ||
| 112 | $$stream .= join('',@_); | ||
| 113 | } | ||
| 114 | else { | ||
| 115 | croak ref($stream).': type non implemented'; | ||
| 116 | } | ||
| 117 | return $self; | ||
| 118 | } | ||
| 119 | |||
| 120 | |||
| 121 | sub print { | ||
| 122 | my $self=shift; | ||
| 123 | return CORE::print($self,@_) unless ref($self) =~/::/; #if $self is not a class use CORE::print | ||
| 124 | return $self->put_chars(@_); | ||
| 125 | } | ||
| 126 | |||
| 127 | |||
| 128 | |||
| 129 | sub put_line { my $self=shift; return $self->put_chars(@_,"\n"); } | ||
| 130 | |||
| 131 | sub say { | ||
| 132 | my $self=shift; | ||
| 133 | return $self->put_line(@_) if ref($self) =~/::/; #if $self is not a class use CORE::say if is implemented | ||
| 134 | local $@; | ||
| 135 | my $r=eval("CORE::say($self,@_)"); ## no critic | ||
| 136 | return print STDOUT $self,@_,"\n" if $@; | ||
| 137 | return $r; | ||
| 138 | } | ||
| 139 | |||
| 140 | sub binmode { | ||
| 141 | my $self=shift; | ||
| 142 | return CORE::binmode($self,@_) unless ref($self) =~/::/; #if $self is not a class use CORE::binmode | ||
| 143 | return $self->mode(@_); | ||
| 144 | } | ||
| 145 | |||
| 146 | sub flush { | ||
| 147 | my ($self,%params)=@_; | ||
| 148 | my $stream = $self->{OUTPUT_STREAM}; | ||
| 149 | affirm { defined $stream } "OUTPUT_STREAM not set"; | ||
| 150 | if (ref($stream) eq 'CODE') { | ||
| 151 | $stream->($self,$self->{_OUTPUT_BUFFER}) if length($self->{_OUTPUT_BUFFER}); | ||
| 152 | } | ||
| 153 | return $self; | ||
| 154 | } | ||
| 155 | |||
| 156 | if (__FILE__ eq $0) { | ||
| 157 | |||
| 158 | use constant { | ||
| 159 | STR => 'this is a string' | ||
| 160 | ,K => 'this is a key' | ||
| 161 | ,STRM => "this is\na multi\nline\nstring\n" | ||
| 162 | }; | ||
| 163 | |||
| 164 | my $out=*STDOUT; | ||
| 165 | my $streamer=__PACKAGE__->new(OUTPUT_STREAM => \$out); | ||
| 166 | binmode($streamer,':encoding(UTF-8)'); | ||
| 167 | $streamer->put_line(STR); | ||
| 168 | |||
| 169 | $out=''; | ||
| 170 | $streamer=__PACKAGE__->new(OUTPUT_STREAM => \$out); | ||
| 171 | $streamer->put_line(STR); | ||
| 172 | croak "check failed " if $out ne STR."\n"; | ||
| 173 | |||
| 174 | my @arr=(); | ||
| 175 | $streamer=__PACKAGE__->new(OUTPUT_STREAM => \@arr); | ||
| 176 | $streamer->put_line(STR); | ||
| 177 | croak "check failed " if scalar(@arr) != 2; | ||
| 178 | croak "check failed " if join('',@arr) ne STR; | ||
| 179 | $streamer->put_chars(STR,STR,"\n",STR); | ||
| 180 | croak "check failed " if scalar(@arr) != 3; | ||
| 181 | croak "check failed " if join("\n",@arr) ne STR."\n".STR.STR."\n".STR; | ||
| 182 | |||
| 183 | |||
| 184 | @arr=(); | ||
| 185 | $streamer=__PACKAGE__->new(OUTPUT_STREAM => sub { push @arr,$_[1]},LINE_SEPARATOR => "\n" ); | ||
| 186 | $streamer->put_line(STRM); | ||
| 187 | $streamer->flush; | ||
| 188 | croak "check failed " if join('',@arr) ne STRM."\n"; | ||
| 189 | croak "check failed " if scalar(@arr) != 5; | ||
| 190 | @arr=(); | ||
| 191 | $streamer=__PACKAGE__->new(OUTPUT_STREAM => sub { push @arr,$_[1] },LINE_SEPARATOR => "\n" ); | ||
| 192 | $streamer->put_line(STRM,STRM); | ||
| 193 | $streamer->flush; | ||
| 194 | croak "check failed " if join('',@arr) ne STRM.STRM."\n"; | ||
| 195 | croak "check failed " if scalar(@arr) != 9; | ||
| 196 | |||
| 197 | @arr=(); | ||
| 198 | $streamer=__PACKAGE__->new(OUTPUT_STREAM => sub { push @arr,$_[1] },LINE_SEPARATOR => 's' ); | ||
| 199 | $streamer->put_chars(K,'s'); | ||
| 200 | $streamer->flush; | ||
| 201 | croak "check failed " if join('',@arr) ne K."s"; | ||
| 202 | croak "check failed " if scalar(@arr) != 3; | ||
| 203 | |||
| 204 | @arr=(); | ||
| 205 | $streamer=__PACKAGE__->new(OUTPUT_STREAM => sub { push @arr,$_[1] },LINE_SEPARATOR => 's' ); | ||
| 206 | $streamer->put_chars(K); | ||
| 207 | $streamer->flush; | ||
| 208 | croak "check failed " if join('',@arr) ne K; | ||
| 209 | croak "check failed " if scalar(@arr) != 3; | ||
| 210 | |||
| 211 | $streamer=__PACKAGE__->new(OUTPUT_STREAM => ''); | ||
| 212 | $streamer->put_line(STR); | ||
| 213 | croak "check failed " if $streamer->{OUTPUT_STREAM} ne STR."\n"; | ||
| 214 | |||
| 215 | } | ||
| 216 | |||
| 217 | 1; | ||
| 218 | |||
| 219 | __END__ |