ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/ActiveDoc/PreProcessedFile.pm
Revision: 1.3
Committed: Tue Nov 23 17:20:40 1999 UTC (25 years, 5 months ago) by williamc
Content type: text/plain
Branch: MAIN
Changes since 1.2: +70 -43 lines
Log Message:
Changes to get PreProcessor Working

File Contents

# User Rev Content
1 williamc 1.1 #
2     # PreProcessedFile.pm
3     #
4     # Originally Written by Christopher Williams
5     #
6     # Description
7     # -----------
8     # Handle Preprocessed file information
9     #
10     # Interface
11     # ---------
12     # new() : A new PreProcessedFile object
13     # url([url]) : return the full url. With an argument will take the given
14     # url and expand it in the context of the current url base.
15     # file() : return the filename corresponding to url of the document
16     # ProcessedFile() : return the filename corresponding to processed url
17     # of the document
18     # line(number) : Return the line and fileobj corresponding to number in
19     # processed file
20     # update() : update the preprocessed file as required.
21     # store(filename) :
22     # restore(filename) :
23    
24     package ActiveDoc::PreProcessedFile;
25     use ActiveDoc::ActiveDoc;
26     require 5.004;
27     @ISA=qw(ActiveDoc::ActiveDoc);
28    
29     sub init {
30     my $self=shift;
31     $self->{lastsequence}=-1;
32     # Specific Tags
33     $self->newparse("include");
34     $self->addurltags("include");
35     $self->addtag("include","Include", \&Include_Start, $self,
36     "", $self, "", $self);
37     }
38    
39     sub line {
40     my $self=shift;
41     my $origline=shift;
42    
43     my $fileob=$self;
44     my $line=$origline;
45 williamc 1.3 for(my $i=0; $i<=$#{$self->{includesdesc}}; $i++ ) {
46     $inc=$self->{includesdesc}[$i];
47 williamc 1.1 $startline=$inc->startline();
48     last if ( $line < $startline );
49     if ( $line > ($inc->lines()+$startline) ) {
50     $line=$line-$inc->lines()+1+($inc->endline()-$startline);
51     # n lines in original map to m lines in expanded
52     }
53     else { # must be in the include file
54 williamc 1.3 ($line, $fileob)=$self->{includes}[$i]->
55     line($line-$startline+1);
56 williamc 1.1 }
57     }
58    
59     return ($line, $fileob);
60     }
61    
62     sub url {
63     my $self=shift;
64    
65     if ( @_ ) {
66     my $url=shift;
67     ($self->{url}, $file)=$self->{urlhandler}->get($url);
68     }
69     else {
70     $self->{url};
71     }
72     }
73    
74     sub ProcessedFile {
75     my $self=shift;
76     $self->config()->cache()->file("_preprocess_".$self->url());
77     }
78    
79     sub file {
80     my $self=shift;
81    
82     my ($url, $file)=$self->{urlhandler}->get($self->url());
83     return $file;
84     }
85    
86    
87     sub update {
88     my $self=shift;
89    
90     my $rv=0;
91 williamc 1.3 my $outfile="_preprocess_".$self->url();
92     my $fileobj;
93     my $sn;
94     @{$self->{updatedfiles}}=();
95 williamc 1.1
96 williamc 1.3 # -- check the input file snd output sequence numbers are in sync
97     my $basefilenumber=$self->config()->cache()->updatenumber($self->url());
98     if ( $basefilenumber != $self->{lastsequence} ) {
99     $rv=1;
100     }
101     else {
102     # -- update dependencies
103     for (my $i=0; $i<=$#{$self->{includes}}; $i++ ) {
104     $fileobj=$self->{includes}[$i];
105     $rv=$rv+$fileobj->update(); # make sure it up to date
106    
107     # -- has it changed since last time we built this object
108     $sn=$self->config()->sequence($fileobj->url());
109     if ( $self->{includesdesc}[$i]->lastsequence() != $sn ) {
110     $rv++;
111     push @{$self->{updatedfiles}},$fileobj->url(); # record for test
112     }
113     }
114 williamc 1.1 }
115 williamc 1.3
116     if ( $rv != 0 ) {
117     print " Need to Update ".$self->url()."\n";
118 williamc 1.1 # ---- sort out the preprocessed file in the cache
119     my $newfile=$self->config()->cache()->
120 williamc 1.3 filename($outfile);
121     $self->config()->cache()->store($outfile,$newfile);
122 williamc 1.1
123     $self->process($self->config()->cache()->file($self->url()),$newfile);
124 williamc 1.3 $self->{lastsequence}=$basefilenumber;
125     # store self in the objectstore by url
126     $self->config->store($self,$self->url());
127     }
128     else {
129     print "No Need to Update ".$self->url()."\n";
130 williamc 1.1 }
131     return $rv;
132     }
133    
134 williamc 1.3 sub updatedfiles {
135     my $self=shift;
136     return @{$self->{updatedfiles}};
137     }
138    
139 williamc 1.1 sub process {
140     my $self=shift;
141     my $filein=shift;
142     my $fileout=shift;
143    
144     #-- create a new file in the url cache
145     $self->_cleanup();
146     $self->{fileout}=FileHandle->new();
147     $self->{fileout}->open(">".$fileout) or die "Unable to open $newfile\n"
148     ."$!\n";
149    
150     # turn on the switch streamer
151     $self->parse("include", $self->{fileout}, "include_starttag");
152    
153     $self->{fileout}->close();
154     }
155    
156     sub store {
157     my $self=shift;
158     my $location=shift;
159    
160     my $fh=$self->openfile(">".$location);
161    
162     # get all include objects to store themselves
163     print $fh $self->{lastsequence}."\n";
164     print $fh ($self->url()?$self->url():"");
165     print $fh "\n";
166 williamc 1.3 foreach $inc ( @{$self->{includesdesc}} ) {
167 williamc 1.1 print $fh ">\n";
168     $inc->store($fh);
169     }
170     close $fh;
171     }
172    
173     sub restore {
174     my $self=shift;
175     my $location=shift;
176    
177 williamc 1.3 my $fh=$self->openfile("<".$location);
178 williamc 1.1
179     $self->{lastsequence}=<$fh>;
180     chomp $self->{lastsequence};
181     $self->{url}=<$fh>;
182     chomp $self->{url};
183     while ( <$fh> ) {
184 williamc 1.3 if ( $_ eq ">\n") {
185 williamc 1.1 $inc=IncFile->new();
186     $inc->restore($fh);
187 williamc 1.3 push @{$self->{includesdesc}}, $inc;
188     # resurrect the appropriate object ID
189     push @{$self->{includes}}, $self->getfile($inc->file());
190 williamc 1.1 }
191     }
192     $fh->close();
193     }
194    
195     sub _cleanup {
196     my $self=shift;
197 williamc 1.3 foreach $inc ( @{$self->{includesdesc}} ) {
198 williamc 1.1 undef $inc;
199     }
200 williamc 1.3 undef @{$self->{includesdesc}};
201 williamc 1.1 undef @{$self->{includes}};
202     }
203    
204 williamc 1.3 sub _includefile {
205     use ActiveDoc::IncFile;
206     my $self=shift;
207     my $fileobj=shift;
208     my $startline=shift;
209     my $endline=shift;
210     my $lines=shift;
211    
212     my $obj=IncFile->new();
213     my $url=$fileobj->url();
214     my $sn=$self->config()->sequence($url);
215     $obj->init($url,$startline,$endline,$lines, $sn);
216     push @{$self->{includes}}, $fileobj;
217     push @{$self->{includesdesc}}, $obj;
218     }
219    
220 williamc 1.1 # ------------------------ Tag Routines -------------------------------
221    
222     #
223     # Include tag
224    
225     sub Include_Start {
226     my $self=shift;
227     my $name=shift;
228     my $hashref=shift;
229    
230     $self->checktag( $name,$hashref, "url");
231     print "Including ".$$hashref{'url'}."\n";
232     my $fileObj=$self->getfile($$hashref{'url'});
233     if ( defined $fileObj ) {
234     # dump out to our file in construction
235     my $fh=FileHandle->new();
236     my $outfilename=$fileObj->ProcessedFile();
237     $fh->open("<".$outfilename) or die "Unable to open $outfilename\n";
238     print {$self->{fileout}} "\n";# always start an include on a new line
239 williamc 1.3 my $linecount=0;
240 williamc 1.1 while ( <$fh> ) {
241 williamc 1.3 $linecount++;
242 williamc 1.1 print {$self->{fileout}} $_;
243     }
244     print {$self->{fileout}} "\n";# always end include with new line
245     undef $fh;
246 williamc 1.3 $self->_includefile($fileObj, $self->{currentparser}->tagstartline(),
247     $self->{currentparser}->line(), $linecount);
248 williamc 1.1 }
249     }
250