| 1 | #-*-perl-*- | ||
| 2 | ##################################################################### | ||
| 3 | # Copyright (C) 2004 Jörg Tiedemann <joerg@stp.ling.uu.se> | ||
| 4 | # | ||
| 5 | # This program is free software; you can redistribute it and/or modify | ||
| 6 | # it under the terms of the GNU General Public License as published by | ||
| 7 | # the Free Software Foundation; either version 2 of the License, or | ||
| 8 | # (at your option) any later version. | ||
| 9 | # | ||
| 10 | # This program is distributed in the hope that it will be useful, | ||
| 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 13 | # GNU General Public License for more details. | ||
| 14 | # | ||
| 15 | # You should have received a copy of the GNU General Public License | ||
| 16 | # along with this program; if not, write to the Free Software | ||
| 17 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
| 18 | ##################################################################### | ||
| 19 | # | ||
| 20 | # access modes - read|write|overwrite|append | ||
| 21 | # | ||
| 22 | ########################################################################### | ||
| 23 | |||
| 24 | |||
| 25 | package Uplug::IO; | ||
| 26 | |||
| 27 | require 5.004; | ||
| 28 | |||
| 29 | use vars qw($DEFAULTENCODING); | ||
| 30 | use strict; | ||
| 31 | use Uplug::Encoding; | ||
| 32 | |||
| 33 | $DEFAULTENCODING='utf-8'; | ||
| 34 | |||
| 35 | my $PerlVersion=$]; | ||
| 36 | |||
| 37 | sub new{ | ||
| 38 | my $class=shift; | ||
| 39 | my $self={}; | ||
| 40 | bless $self,$class; | ||
| 41 | $self->{StreamOptions}={} ; | ||
| 42 | $self->{StreamOptions}->{encoding}=$DEFAULTENCODING; | ||
| 43 | return $self; | ||
| 44 | } | ||
| 45 | |||
| 46 | |||
| 47 | |||
| 48 | sub init{ | ||
| 49 | my $self=shift; | ||
| 50 | my $OptionHash=shift; | ||
| 51 | &AddHash2Hash($self->{StreamOptions},$OptionHash); | ||
| 52 | if ($self->{AccessMode} eq 'write'){ | ||
| 53 | if (defined $OptionHash->{'write_mode'}){ | ||
| 54 | $self->{AccessMode}=$OptionHash->{'write_mode'}; | ||
| 55 | } | ||
| 56 | } | ||
| 57 | $self->{DATACOUNTER}=0; | ||
| 58 | return 1; | ||
| 59 | } | ||
| 60 | |||
| 61 | |||
| 62 | sub open{ | ||
| 63 | |||
| 64 | my $self = shift; | ||
| 65 | $self->{AccessMode} = shift; | ||
| 66 | my $OptionHash = shift; | ||
| 67 | |||
| 68 | if (not $self->{AccessMode}){$self->{AccessMode}='read';} | ||
| 69 | |||
| 70 | my $ret; | ||
| 71 | if ($ret=$self->init($OptionHash)){ | ||
| 72 | $self->{StreamStatus}='open'; | ||
| 73 | if ($self->{AccessMode} eq 'read'){ | ||
| 74 | $self->readheader; | ||
| 75 | } | ||
| 76 | else{ | ||
| 77 | if (defined $OptionHash->{'write_mode'}){ | ||
| 78 | $self->{AccessMode}=$OptionHash->{'write_mode'}; | ||
| 79 | } | ||
| 80 | $self->writeheader; | ||
| 81 | } | ||
| 82 | return 1; | ||
| 83 | } | ||
| 84 | return 0; | ||
| 85 | } | ||
| 86 | |||
| 87 | sub reopen{ | ||
| 88 | my $self=shift; | ||
| 89 | $self->close; | ||
| 90 | return $self->open($self->{AccessMode}); | ||
| 91 | } | ||
| 92 | |||
| 93 | |||
| 94 | sub close{ | ||
| 95 | |||
| 96 | my $self = shift; | ||
| 97 | my ($TailHash) = @_; | ||
| 98 | $self->{StreamStatus} = 'close'; | ||
| 99 | |||
| 100 | if ($self->{AccessMode} eq 'read'){ | ||
| 101 | $self->readtail; | ||
| 102 | } | ||
| 103 | else{ | ||
| 104 | $self->addtail($TailHash); | ||
| 105 | $self->writetail($TailHash); | ||
| 106 | } | ||
| 107 | return 1; | ||
| 108 | } | ||
| 109 | |||
| 110 | #---------------------------------------------------------------- | ||
| 111 | |||
| 112 | sub read{ | ||
| 113 | my $self = shift; | ||
| 114 | $self->{DATACOUNTER}++; | ||
| 115 | return 1; | ||
| 116 | } | ||
| 117 | |||
| 118 | |||
| 119 | sub write{ | ||
| 120 | my $self = shift; | ||
| 121 | $self->{DATACOUNTER}++; | ||
| 122 | return 1; | ||
| 123 | } | ||
| 124 | |||
| 125 | #---------------------------------------------------------------- | ||
| 126 | |||
| 127 | sub select{ | ||
| 128 | my $self=shift; | ||
| 129 | my ($data, | ||
| 130 | $SelectPatternHash, | ||
| 131 | $ListOfAttributes, | ||
| 132 | $CmpOperator)=@_; | ||
| 133 | |||
| 134 | while ($self->read($data)){ | ||
| 135 | |||
| 136 | if ($data->matchData($SelectPatternHash,$CmpOperator)){ | ||
| 137 | if (ref($ListOfAttributes) eq 'ARRAY'){ | ||
| 138 | $data->keepAttributes($ListOfAttributes); | ||
| 139 | } | ||
| 140 | return 1; | ||
| 141 | } | ||
| 142 | } | ||
| 143 | return 0; | ||
| 144 | } | ||
| 145 | |||
| 146 | #---------------------------------------------------------------- | ||
| 147 | # update(oldData,newData,cmpOperator) | ||
| 148 | |||
| 149 | sub update{return;} | ||
| 150 | |||
| 151 | #---------------------------------------------------------------- | ||
| 152 | # delete(dataPattern,cmpOperator) | ||
| 153 | |||
| 154 | sub delete{return;} | ||
| 155 | |||
| 156 | #---------------------------------------------------------------- | ||
| 157 | |||
| 158 | sub count{ | ||
| 159 | my $self=shift; | ||
| 160 | |||
| 161 | |||
| 162 | return $self->{DATACOUNTER} if (defined $self->{DATACOUNTER}); | ||
| 163 | |||
| 164 | if ($self->{AccessMode} eq 'read'){ | ||
| 165 | $self->init(); | ||
| 166 | $self->{DATACOUNTER}=0; | ||
| 167 | while ($self->read){$self->{DATACOUNTER}++} | ||
| 168 | return $self->{DATACOUNTER}; | ||
| 169 | } | ||
| 170 | } | ||
| 171 | |||
| 172 | |||
| 173 | #---------------------------------------------------------------- | ||
| 174 | |||
| 175 | sub options{ | ||
| 176 | my $self=shift; | ||
| 177 | return $self->{StreamOptions}; | ||
| 178 | } | ||
| 179 | |||
| 180 | sub option{ | ||
| 181 | my $self=shift; | ||
| 182 | if (ref($self->{StreamOptions}) eq 'HASH'){ | ||
| 183 | return $self->{StreamOptions}->{$_[0]}; | ||
| 184 | } | ||
| 185 | return undef; | ||
| 186 | } | ||
| 187 | |||
| 188 | sub setOption{ | ||
| 189 | my $self=shift; | ||
| 190 | while (@_){ | ||
| 191 | my $attr=shift; | ||
| 192 | my $val=shift; | ||
| 193 | $self->{StreamOptions}->{$attr}=$val; | ||
| 194 | } | ||
| 195 | } | ||
| 196 | |||
| 197 | sub SetOption{ | ||
| 198 | my $self=shift; | ||
| 199 | return $self->setOption(@_); | ||
| 200 | } | ||
| 201 | |||
| 202 | #---------------------------------------------------------------- | ||
| 203 | |||
| 204 | sub header{ | ||
| 205 | my $self=shift; | ||
| 206 | return $self->{StreamHeader}; | ||
| 207 | } | ||
| 208 | |||
| 209 | sub tail{ | ||
| 210 | my $self=shift; | ||
| 211 | return $self->{StreamTail}; | ||
| 212 | } | ||
| 213 | |||
| 214 | #---------------------------------------------------------------- | ||
| 215 | |||
| 216 | sub readheader{ | ||
| 217 | my $self=shift; | ||
| 218 | if (not defined $self->{StreamHeader}){$self->{StreamHeader} = {};} | ||
| 219 | return 0; | ||
| 220 | } | ||
| 221 | |||
| 222 | sub addheader{ | ||
| 223 | my $self=shift; | ||
| 224 | my $HeaderHash=shift; | ||
| 225 | |||
| 226 | if (not defined $self->{StreamHeader}){$self->{StreamHeader}={};} | ||
| 227 | if (not defined $self->{StreamOptions}){$self->{StreamOptions}={};} | ||
| 228 | |||
| 229 | &AddHash2Hash($self->{StreamHeader},$HeaderHash); # stream options can | ||
| 230 | &AddHash2Hash($self->{StreamOptions},$HeaderHash); # be stored in header! | ||
| 231 | } | ||
| 232 | |||
| 233 | #---------------------------------------------------------------- | ||
| 234 | |||
| 235 | sub writeheader{ | ||
| 236 | my $self=shift; | ||
| 237 | return 0; | ||
| 238 | } | ||
| 239 | |||
| 240 | #---------------------------------------------------------------- | ||
| 241 | |||
| 242 | sub readtail{ | ||
| 243 | my $self=shift; | ||
| 244 | $self->{'StreamTail'} = {}; | ||
| 245 | return 0; | ||
| 246 | } | ||
| 247 | |||
| 248 | sub addtail{ | ||
| 249 | my $self=shift; | ||
| 250 | my $TailHash=shift; | ||
| 251 | &AddHash2Hash($self->{StreamTail},$TailHash); | ||
| 252 | } | ||
| 253 | |||
| 254 | #---------------------------------------------------------------- | ||
| 255 | |||
| 256 | sub writetail{ | ||
| 257 | my $self=shift; | ||
| 258 | return 0; | ||
| 259 | } | ||
| 260 | |||
| 261 | sub files{return undef;} | ||
| 262 | |||
| 263 | |||
| 264 | |||
| 265 | ###################################################################### | ||
| 266 | # | ||
| 267 | # encoding determines the EXTERNAL encoding of data streams | ||
| 268 | # internal encoding is somewhat depreciated with perl >= 5.8 | ||
| 269 | # | ||
| 270 | |||
| 271 | sub getEncoding{ | ||
| 272 | my $self=shift; | ||
| 273 | if (ref($self->{StreamOptions}) eq 'HASH'){ | ||
| 274 | if (defined $self->{StreamOptions}->{encoding}){ | ||
| 275 | return $self->{StreamOptions}->{encoding}; | ||
| 276 | } | ||
| 277 | } | ||
| 278 | if (ref($self->{StreamHeader}) eq 'HASH'){ | ||
| 279 | if (defined $self->{StreamHeader}->{encoding}){ | ||
| 280 | return $self->{StreamHeader}->{encoding}; | ||
| 281 | } | ||
| 282 | } | ||
| 283 | return $DEFAULTENCODING; | ||
| 284 | } | ||
| 285 | |||
| 286 | sub getInternalEncoding{return $DEFAULTENCODING;} # internal encoding | ||
| 287 | sub getExternalEncoding{return $_[0]->getEncoding();} # external encoding | ||
| 288 | |||
| 289 | ###################################################################### | ||
| 290 | #---------------------------------------------------------------- | ||
| 291 | |||
| 292 | sub readFromHandle{ | ||
| 293 | my $self=shift; | ||
| 294 | my ($fh,$encoding)=@_; | ||
| 295 | if (defined $self->{READBUFFER}){ # check if there's | ||
| 296 | my $content=$self->{READBUFFER}; # something in the buffer | ||
| 297 | delete $self->{READBUFFER}; | ||
| 298 | return $content; | ||
| 299 | } | ||
| 300 | if (not defined $encoding){ | ||
| 301 | $encoding=$self->getEncoding; | ||
| 302 | } | ||
| 303 | my $content=<$fh>; # otherwise: read from handle | ||
| 304 | if (not $content){return $content;} | ||
| 305 | if ($PerlVersion<5.008){ | ||
| 306 | if ($encoding ne $DEFAULTENCODING){ | ||
| 307 | $content=Uplug::Encoding::decode($content,$DEFAULTENCODING, | ||
| 308 | $encoding); | ||
| 309 | # $content=$self->decode($content,$encoding,$DEFAULTENCODING); | ||
| 310 | } | ||
| 311 | } | ||
| 312 | return $content; | ||
| 313 | } | ||
| 314 | |||
| 315 | #---------------------------------------------------------------- | ||
| 316 | |||
| 317 | sub writeToHandle{ | ||
| 318 | my $self=shift; | ||
| 319 | my ($fh,$content,$encoding)=@_; | ||
| 320 | if (not defined $encoding){ | ||
| 321 | $encoding=$self->getEncoding; | ||
| 322 | } | ||
| 323 | if ($PerlVersion<5.008){ | ||
| 324 | if ($encoding ne $DEFAULTENCODING){ | ||
| 325 | $content=$self->encode($content,$DEFAULTENCODING,$encoding); | ||
| 326 | } | ||
| 327 | } | ||
| 328 | print $fh $content; | ||
| 329 | } | ||
| 330 | |||
| 331 | |||
| 332 | #---------------------------------------------------------------- | ||
| 333 | |||
| 334 | |||
| 335 | |||
| 336 | sub AddHash2Hash{ | ||
| 337 | my $base=shift; | ||
| 338 | my $hash=shift; | ||
| 339 | if (ref($base) ne 'HASH'){return;} | ||
| 340 | foreach (keys %{$hash}){ | ||
| 341 | eval {$base->{$_}=$hash->{$_} }; | ||
| 342 | } | ||
| 343 | } | ||
| 344 | |||
| 345 | |||
| 346 | |||
| 347 | #------------------------------------------------------------------------- | ||
| 348 | |||
| 349 | sub encode{ | ||
| 350 | my $self=shift; | ||
| 351 | return &Uplug::Encoding::encode(@_); | ||
| 352 | } | ||
| 353 | |||
| 354 | |||
| 355 | #------------------------------------------------------------------------- | ||
| 356 | # return a true value | ||
| 357 | # | ||
| 358 | |||
| 359 | |||
| 360 | 1; |