12 |
|
# to the desired routines |
13 |
|
# usegroupchecker(groupchecker) : Set a groupchecker |
14 |
|
# parse() : Parse the file |
15 |
– |
# checkparam($name,$par) : Exit with an error message if parameter |
16 |
– |
# is undefined in tag $name |
15 |
|
# line() : return the current line number of the parse |
16 |
< |
|
17 |
< |
package Switcher; |
16 |
> |
# stream(filehandle) : stream output to the filehandle if not handled |
17 |
> |
# in any other way |
18 |
> |
package ActiveDoc::Switcher; |
19 |
|
require 5.001; |
20 |
|
use Carp; |
21 |
|
|
25 |
|
my $objectname=shift; |
26 |
|
my $groupchecker=shift; |
27 |
|
|
28 |
< |
my $self = {}; |
28 |
> |
$self = {}; |
29 |
|
$self->{allw}=$objectname; |
30 |
|
bless $self, $class; |
31 |
|
$self->_initialise($file); |
32 |
|
return $self; |
33 |
|
} |
34 |
|
|
35 |
+ |
sub stream { |
36 |
+ |
my $self=shift; |
37 |
+ |
|
38 |
+ |
$self->{stream}=shift; |
39 |
+ |
} |
40 |
+ |
|
41 |
+ |
sub streamexclude { |
42 |
+ |
my $self=shift; |
43 |
+ |
my $tag=shift; |
44 |
+ |
|
45 |
+ |
$tag=~tr/A-Z/a-z/; |
46 |
+ |
$self->{streamexclude}{$tag}=1; |
47 |
+ |
} |
48 |
+ |
|
49 |
|
sub _initialise (hash1) { |
50 |
|
my $self=shift; |
51 |
|
$self->{filename}=shift; |
57 |
|
|
58 |
|
# Add a default TagContainer |
59 |
|
use ActiveDoc::TagContainer; |
60 |
< |
$self->{tagcontainer}=TagContainer->new(); |
60 |
> |
$self->{tagcontainer}=ActiveDoc::TagContainer->new(); |
61 |
|
|
62 |
|
} |
63 |
|
|
78 |
|
sub parse { |
79 |
|
my $self=shift; |
80 |
|
my $char; |
81 |
+ |
my $buf; |
82 |
|
$self->{linecount}=0; |
83 |
|
$self->_resetvars(); |
84 |
+ |
$self->{streamstore}=""; |
85 |
+ |
$self->{streamtmp}=""; |
86 |
|
|
87 |
|
# Open the file |
88 |
|
use FileHandle; |
89 |
< |
my $filehandle=FileHandle->new(); |
90 |
< |
open( $filehandle , "$self->{filename}" ) |
91 |
< |
or carp "Switcher: Cannot open $self->{filename} $! \n"; |
89 |
> |
local $filehandle; |
90 |
> |
$filehandle=FileHandle->new(); |
91 |
> |
$filehandle->open("<".$self->{filename}) |
92 |
> |
or return 1; |
93 |
> |
# The buffering seems all messed up - best not to use it |
94 |
> |
$filehandle->setvbuf($buf, _IONBF, 3000); |
95 |
|
|
96 |
|
# Start file processing |
97 |
< |
while ( <$filehandle> ) { |
97 |
> |
while ( ($_=<$filehandle>) ) { |
98 |
|
$self->{linecount}++; |
99 |
|
$self->{currentline}=$_; |
100 |
|
$self->{stringpos}=0; |
102 |
|
$self->_checkchar($char); |
103 |
|
} # end char while |
104 |
|
} # End String while loop |
105 |
< |
close $filehandle; |
106 |
< |
} |
88 |
< |
|
89 |
< |
sub checkparam($name, $key) { |
90 |
< |
my $self=shift; |
91 |
< |
my $name=shift; |
92 |
< |
my $key=shift; |
93 |
< |
|
94 |
< |
if ( ! defined $self->{tagvar}{$key} ) { |
95 |
< |
print "Switcher: Badly formed $name tag -". |
96 |
< |
" undefined $key parameter\n"; |
97 |
< |
exit 1; |
98 |
< |
} |
105 |
> |
undef $filehandle; |
106 |
> |
$self->_printstream(); |
107 |
|
} |
108 |
|
|
109 |
|
# |
135 |
|
my $char=shift; |
136 |
|
my $string; |
137 |
|
|
138 |
+ |
|
139 |
|
# ---- In a tag |
140 |
|
if ( $self->{tagcontext}=~/tag/ ) { |
141 |
|
if ( ! $self->_quotetest($char) ) { |
161 |
|
my $char; |
162 |
|
$char=substr($self->{currentline},$self->{stringpos}++,1); |
163 |
|
# print "Debug : Fetching character $char\n"; |
164 |
+ |
|
165 |
+ |
# Keep a record for any stream processes |
166 |
+ |
$self->{streamstore}=$self->{streamstore}.$char; |
167 |
+ |
|
168 |
|
return $char; |
169 |
|
} |
170 |
|
|
173 |
|
my $char; |
174 |
|
|
175 |
|
# Close the last text segment |
176 |
+ |
$self->{streamtmp}=$self->_popstream(); |
177 |
|
$self->_calltag($self->{textcontext}, $self->{textcontext}, |
178 |
|
$self->_getstore()); |
179 |
|
$self->_resetstore(); |
237 |
|
my $tagroutine=shift; |
238 |
|
my @args=@_; |
239 |
|
my $rt; |
240 |
+ |
my $found=0; |
241 |
|
|
242 |
|
if ( $self->{groupchecker}->status() || |
243 |
|
( $self->{tagcontainer}->inquiregroup($tagroutine)) ) { |
244 |
< |
$rt=$self->{tagcontainer}->getroutine($tagroutine); |
244 |
> |
($rt,$obj)=$self->{tagcontainer}->getroutine($tagroutine); |
245 |
|
if ( $rt ne "" ) { |
246 |
+ |
if ( ! defined $obj ) { |
247 |
|
&{$rt}( $self->{allw},@_); |
248 |
+ |
} |
249 |
+ |
else { |
250 |
+ |
&{$rt}( $obj,@_); |
251 |
+ |
} |
252 |
+ |
$found=1; |
253 |
|
} |
254 |
|
} |
255 |
+ |
|
256 |
+ |
# stream function |
257 |
+ |
if ( ! exists $self->{streamexclude}{$tagroutine} ) { |
258 |
+ |
$self->_printstream(); |
259 |
+ |
} |
260 |
+ |
$self->_clearstream(); |
261 |
+ |
} |
262 |
+ |
|
263 |
+ |
sub _clearstream { |
264 |
+ |
my $self=shift; |
265 |
+ |
$self->{streamstore}=(($self->{streamtmp} ne "")?$self->{streamtmp}:""); |
266 |
+ |
$self->{streamtmp}=""; |
267 |
+ |
} |
268 |
+ |
|
269 |
+ |
sub _popstream { |
270 |
+ |
my $self=shift; |
271 |
+ |
$self->{streamstore}=~s/(.*)(.)$/$1/; |
272 |
+ |
return $2; |
273 |
+ |
} |
274 |
+ |
|
275 |
+ |
sub _printstream { |
276 |
+ |
|
277 |
+ |
my $self=shift; |
278 |
+ |
|
279 |
+ |
# Stream output functionality |
280 |
+ |
if ( defined $self->{stream} ) { |
281 |
+ |
print {$self->{stream}} "$self->{streamstore}"; |
282 |
+ |
} |
283 |
|
} |
284 |
|
|
285 |
|
sub _removefromstack { |
354 |
|
sub _resetlabels { |
355 |
|
my $self=shift; |
356 |
|
undef $self->{tagvar}; |
357 |
+ |
undef $self->{tagname}; |
358 |
|
} |
359 |
|
|
360 |
|
sub _closelabel { |
366 |
|
$self->{lastlabel}=""; |
367 |
|
} |
368 |
|
elsif ( $self->_getstore() ne "") { |
369 |
< |
#Then it must be the tag name |
370 |
< |
($self->{tagname}=$self->_getstore())=~tr/A-Z/a-z/; |
369 |
> |
# Then it must be the tag name |
370 |
> |
if ( ! defined $self->{tagname} ) { |
371 |
> |
($self->{tagname}=$self->_getstore())=~tr/A-Z/a-z/; |
372 |
> |
} |
373 |
> |
else { |
374 |
> |
die "Tag syntax error in $self->{tagname} on ".$self->line()."\n". |
375 |
> |
"of file $self->{filename}"; |
376 |
> |
} |
377 |
|
} |
378 |
|
$self->_resetstore(); |
379 |
|
} |