| 1 | ##################################################################### | ||
| 2 | # Copyright (C) 2004 Jörg Tiedemann <joerg@stp.ling.uu.se> | ||
| 3 | # | ||
| 4 | # This program is free software; you can redistribute it and/or modify | ||
| 5 | # it under the terms of the GNU General Public License as published by | ||
| 6 | # the Free Software Foundation; either version 2 of the License, or | ||
| 7 | # (at your option) any later version. | ||
| 8 | # | ||
| 9 | # This program is distributed in the hope that it will be useful, | ||
| 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 12 | # GNU General Public License for more details. | ||
| 13 | # | ||
| 14 | # You should have received a copy of the GNU General Public License | ||
| 15 | # along with this program; if not, write to the Free Software | ||
| 16 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
| 17 | # | ||
| 18 | # StreamCollection - | ||
| 19 | # | ||
| 20 | # | ||
| 21 | ##################################################################### | ||
| 22 | # $Author$ | ||
| 23 | # $Id$ | ||
| 24 | |||
| 25 | |||
| 26 | package Uplug::IO::Collection; | ||
| 27 | |||
| 28 | use strict; | ||
| 29 | use vars qw(@ISA); | ||
| 30 | use Uplug::IO; | ||
| 31 | use Uplug::IO::Any; | ||
| 32 | |||
| 33 | @ISA = qw( Uplug::IO ); | ||
| 34 | |||
| 35 | |||
| 36 | sub new{ | ||
| 37 | my $class=shift; | ||
| 38 | my $options=shift; | ||
| 39 | my $self=$class->SUPER::new($options); | ||
| 40 | $self->{StreamHandles}=[]; | ||
| 41 | $self->{StreamID}={}; | ||
| 42 | return $self; | ||
| 43 | } | ||
| 44 | |||
| 45 | sub open{ | ||
| 46 | my $self = shift; | ||
| 47 | $self->{AccessMode} = shift; | ||
| 48 | my $HeaderHash = $_[0]; | ||
| 49 | |||
| 50 | $self->SUPER::open($self->{AccessMode},$HeaderHash); | ||
| 51 | |||
| 52 | $self->AddStream($self->{StreamOptions}); | ||
| 53 | |||
| 54 | if ($#{$self->{StreamHandles}}){ | ||
| 55 | if (not defined $self->{CurrentStreamID}){ | ||
| 56 | $self->{CurrentStreamID}=0; | ||
| 57 | $self->{CurrentStream}=$self->{StreamHandles}->[0]; | ||
| 58 | } | ||
| 59 | return $self->{CurrentStream}->open($self->{AccessMode},$HeaderHash); | ||
| 60 | } | ||
| 61 | } | ||
| 62 | |||
| 63 | sub AddStream{ | ||
| 64 | my $self = shift; | ||
| 65 | my $OptionHash = $_[0]; | ||
| 66 | |||
| 67 | if (ref($OptionHash) ne 'HASH'){return 0;} | ||
| 68 | my @streams=(); | ||
| 69 | if (ref($OptionHash->{'stream names'}) eq 'ARRAY'){ | ||
| 70 | @streams=@{$OptionHash->{'stream names'}}; | ||
| 71 | } | ||
| 72 | if (ref($OptionHash->{'streams'}) eq 'ARRAY'){ | ||
| 73 | @streams=@{$OptionHash->{'streams'}}; | ||
| 74 | } | ||
| 75 | if (@streams){ | ||
| 76 | foreach (@streams){ | ||
| 77 | my %options=(format => $_); | ||
| 78 | $self->AddStream(\%options); | ||
| 79 | } | ||
| 80 | return 1; | ||
| 81 | } | ||
| 82 | |||
| 83 | my $StreamID=scalar @{$self->{StreamHandles}}; | ||
| 84 | $self->{StreamHandles}->[$StreamID]=Uplug::IO::Any->new($OptionHash); | ||
| 85 | if (not defined $OptionHash->{'stream name'}){ | ||
| 86 | $OptionHash->{'stream name'}="stream $StreamID"; | ||
| 87 | } | ||
| 88 | &Uplug::IO::AddHash2Hash($self->{StreamHandles}->[$StreamID]->{StreamOptions},$OptionHash); | ||
| 89 | $self->{StreamID}->{$OptionHash->{'stream name'}}=$StreamID; | ||
| 90 | } | ||
| 91 | |||
| 92 | sub close{ | ||
| 93 | my $self=shift; | ||
| 94 | return $self->SUPER::close; | ||
| 95 | # return $self->{CurrentStream}->close; | ||
| 96 | } | ||
| 97 | |||
| 98 | |||
| 99 | sub read{ | ||
| 100 | my $self=shift; | ||
| 101 | my $data=shift; | ||
| 102 | my $doc=''; | ||
| 103 | while (not $self->{CurrentStream}->read($data)){ | ||
| 104 | if (not $self->NextStream){ | ||
| 105 | delete $self->{openDoc}; | ||
| 106 | return 0; | ||
| 107 | } | ||
| 108 | else{ # save the doc-name of a new stream | ||
| 109 | |||
| 110 | if (defined $self->{CurrentStream}->{StreamOptions}->{FileName}){ | ||
| 111 | $doc=$self->{CurrentStream}->{StreamOptions}->{FileName}; | ||
| 112 | } | ||
| 113 | elsif (defined $self->{CurrentStream}->{StreamOptions}->{file}){ | ||
| 114 | $doc=$self->{CurrentStream}->{StreamOptions}->{file}; | ||
| 115 | } | ||
| 116 | elsif (defined | ||
| 117 | $self->{CurrentStream}->{StreamOptions}->{'stream name'}){ | ||
| 118 | $doc=$self->{CurrentStream}->{StreamOptions}->{'stream name'}; | ||
| 119 | } | ||
| 120 | } | ||
| 121 | } | ||
| 122 | # $data->addHeader($doc); # add the document name to the data header | ||
| 123 | if ($doc){ | ||
| 124 | if ($self->{openDoc}){ | ||
| 125 | $data->addHeader("\n</text>\n"); | ||
| 126 | } | ||
| 127 | $self->{openDoc}=$doc; | ||
| 128 | $data->addHeader("\n<text file=\"$doc\">\n"); | ||
| 129 | } | ||
| 130 | |||
| 131 | return 1; | ||
| 132 | } | ||
| 133 | |||
| 134 | sub write{ | ||
| 135 | my $self=shift; | ||
| 136 | my $data=shift; | ||
| 137 | return $self->{CurrentStream}->write($data); | ||
| 138 | } | ||
| 139 | |||
| 140 | |||
| 141 | sub select{ | ||
| 142 | my $self=shift; | ||
| 143 | my ($PatternHash, | ||
| 144 | $ListOfAttr, | ||
| 145 | $CmpOperator)=@_; | ||
| 146 | my $data; | ||
| 147 | while (not ($data=$self->{CurrentStream}->select($PatternHash, | ||
| 148 | $ListOfAttr, | ||
| 149 | $CmpOperator))){ | ||
| 150 | if (not $self->NextStream){ | ||
| 151 | return undef; | ||
| 152 | } | ||
| 153 | } | ||
| 154 | return $data; | ||
| 155 | } | ||
| 156 | |||
| 157 | |||
| 158 | sub NextStream{ | ||
| 159 | |||
| 160 | my $self=shift; | ||
| 161 | $self->{CurrentStream}->close; | ||
| 162 | while (1) { | ||
| 163 | if ($self->{CurrentStreamID}<$#{$self->{StreamHandles}}){ | ||
| 164 | $self->{CurrentStreamID}++; | ||
| 165 | my $streamID=$self->{CurrentStreamID}; | ||
| 166 | $self->{CurrentStream}= | ||
| 167 | $self->{StreamHandles}->[$self->{CurrentStreamID}]; | ||
| 168 | if ($self->{CurrentStream}->open($self->{AccessMode})){ | ||
| 169 | return 1; | ||
| 170 | } | ||
| 171 | } | ||
| 172 | else{ | ||
| 173 | $self->{CurrentStreamID}=-1; | ||
| 174 | $self->{CurrentStream}=undef; | ||
| 175 | return 0; | ||
| 176 | } | ||
| 177 | } | ||
| 178 | return 1; | ||
| 179 | } | ||
| 180 | |||
| 181 | |||
| 182 | sub GotoStream{ | ||
| 183 | |||
| 184 | my $self=shift; | ||
| 185 | my $StreamName=shift; | ||
| 186 | |||
| 187 | my $StreamID; | ||
| 188 | if (not defined $self->{StreamID}->{$StreamName}){ | ||
| 189 | if ($StreamName=~/^[0-9]+$/){ | ||
| 190 | $StreamID=$StreamName; | ||
| 191 | } | ||
| 192 | else{return 0;} | ||
| 193 | } | ||
| 194 | else{ | ||
| 195 | $StreamID=$self->{StreamID}->{$StreamName}; | ||
| 196 | } | ||
| 197 | |||
| 198 | $self->{CurrentStream}->close; | ||
| 199 | $self->{CurrentStreamID}=$StreamID; | ||
| 200 | $self->{CurrentStream}= | ||
| 201 | $self->{StreamHandles}->[$self->{CurrentStreamID}]; | ||
| 202 | if ($self->{CurrentStream}->open($self->{AccessMode})){ | ||
| 203 | return 1; | ||
| 204 | } | ||
| 205 | else{ | ||
| 206 | return 0; | ||
| 207 | } | ||
| 208 | } | ||
| 209 | |||
| 210 | |||
| 211 | 1; | ||
| 212 |