ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/ActiveDoc/PreProcessedFile.pm
Revision: 1.5
Committed: Fri Feb 18 10:32:16 2000 UTC (25 years, 2 months ago) by williamc
Content type: text/plain
Branch: MAIN
Changes since 1.4: +11 -6 lines
Log Message:
Line tracing fixed

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 williamc 1.4 # realline(number): Return the line and fileobj corresponding to number in
19 williamc 1.1 # 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 williamc 1.4 sub realline {
40 williamc 1.1 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 williamc 1.5 last if ( $line <= $startline );
49     if ( $line >= ($inc->lines()+$startline+2) ) {
50     $line=$line-($inc->lines()+$startline-$inc->endline())-2;
51     #+($inc->endline()-$startline);
52 williamc 1.1 # n lines in original map to m lines in expanded
53 williamc 1.5 print "Included file lines = ".$inc->lines()." +2 \n";
54     print "Start tag= $startline , endtag=".$inc->endline()."\n";
55 williamc 1.1 }
56     else { # must be in the include file
57 williamc 1.5 print "Passing $line-$startline \n";
58 williamc 1.3 ($line, $fileob)=$self->{includes}[$i]->
59 williamc 1.5 realline($line-$startline);
60 williamc 1.1 }
61     }
62    
63     return ($line, $fileob);
64 williamc 1.4 }
65    
66     sub line {
67     my $self=shift;
68     my $line=$self->{currentparser}->line();
69     return $line, $self;
70 williamc 1.1 }
71    
72     sub url {
73     my $self=shift;
74    
75     if ( @_ ) {
76     my $url=shift;
77     ($self->{url}, $file)=$self->{urlhandler}->get($url);
78     }
79     else {
80     $self->{url};
81     }
82     }
83    
84     sub ProcessedFile {
85     my $self=shift;
86     $self->config()->cache()->file("_preprocess_".$self->url());
87     }
88    
89     sub file {
90     my $self=shift;
91    
92     my ($url, $file)=$self->{urlhandler}->get($self->url());
93     return $file;
94     }
95    
96    
97     sub update {
98     my $self=shift;
99    
100     my $rv=0;
101 williamc 1.3 my $outfile="_preprocess_".$self->url();
102     my $fileobj;
103     my $sn;
104     @{$self->{updatedfiles}}=();
105 williamc 1.1
106 williamc 1.3 # -- check the input file snd output sequence numbers are in sync
107     my $basefilenumber=$self->config()->cache()->updatenumber($self->url());
108     if ( $basefilenumber != $self->{lastsequence} ) {
109     $rv=1;
110     }
111     else {
112     # -- update dependencies
113     for (my $i=0; $i<=$#{$self->{includes}}; $i++ ) {
114     $fileobj=$self->{includes}[$i];
115     $rv=$rv+$fileobj->update(); # make sure it up to date
116    
117     # -- has it changed since last time we built this object
118     $sn=$self->config()->sequence($fileobj->url());
119     if ( $self->{includesdesc}[$i]->lastsequence() != $sn ) {
120     $rv++;
121     push @{$self->{updatedfiles}},$fileobj->url(); # record for test
122     }
123     }
124 williamc 1.1 }
125 williamc 1.3
126     if ( $rv != 0 ) {
127 williamc 1.5 $self->verbose(" Need to Update ".$self->url());
128 williamc 1.1 # ---- sort out the preprocessed file in the cache
129     my $newfile=$self->config()->cache()->
130 williamc 1.3 filename($outfile);
131     $self->config()->cache()->store($outfile,$newfile);
132 williamc 1.1
133     $self->process($self->config()->cache()->file($self->url()),$newfile);
134 williamc 1.3 $self->{lastsequence}=$basefilenumber;
135     # store self in the objectstore by url
136     $self->config->store($self,$self->url());
137     }
138     else {
139 williamc 1.5 $self->verbose("No Need to Update ".$self->url());
140 williamc 1.1 }
141     return $rv;
142     }
143    
144 williamc 1.3 sub updatedfiles {
145     my $self=shift;
146     return @{$self->{updatedfiles}};
147     }
148    
149 williamc 1.1 sub process {
150     my $self=shift;
151     my $filein=shift;
152     my $fileout=shift;
153    
154     #-- create a new file in the url cache
155     $self->_cleanup();
156     $self->{fileout}=FileHandle->new();
157     $self->{fileout}->open(">".$fileout) or die "Unable to open $newfile\n"
158     ."$!\n";
159    
160     # turn on the switch streamer
161     $self->parse("include", $self->{fileout}, "include_starttag");
162    
163     $self->{fileout}->close();
164 williamc 1.5 $self->verbose("$fileout Created");
165 williamc 1.1 }
166    
167     sub store {
168     my $self=shift;
169     my $location=shift;
170    
171     my $fh=$self->openfile(">".$location);
172    
173     # get all include objects to store themselves
174     print $fh $self->{lastsequence}."\n";
175     print $fh ($self->url()?$self->url():"");
176     print $fh "\n";
177 williamc 1.3 foreach $inc ( @{$self->{includesdesc}} ) {
178 williamc 1.1 print $fh ">\n";
179     $inc->store($fh);
180     }
181     close $fh;
182     }
183    
184     sub restore {
185     my $self=shift;
186     my $location=shift;
187    
188 williamc 1.3 my $fh=$self->openfile("<".$location);
189 williamc 1.1
190     $self->{lastsequence}=<$fh>;
191     chomp $self->{lastsequence};
192     $self->{url}=<$fh>;
193     chomp $self->{url};
194     while ( <$fh> ) {
195 williamc 1.3 if ( $_ eq ">\n") {
196 williamc 1.1 $inc=IncFile->new();
197     $inc->restore($fh);
198 williamc 1.3 push @{$self->{includesdesc}}, $inc;
199     # resurrect the appropriate object ID
200     push @{$self->{includes}}, $self->getfile($inc->file());
201 williamc 1.1 }
202     }
203     $fh->close();
204     }
205    
206     sub _cleanup {
207     my $self=shift;
208 williamc 1.3 foreach $inc ( @{$self->{includesdesc}} ) {
209 williamc 1.1 undef $inc;
210     }
211 williamc 1.3 undef @{$self->{includesdesc}};
212 williamc 1.1 undef @{$self->{includes}};
213     }
214    
215 williamc 1.3 sub _includefile {
216     use ActiveDoc::IncFile;
217     my $self=shift;
218     my $fileobj=shift;
219     my $startline=shift;
220     my $endline=shift;
221     my $lines=shift;
222    
223     my $obj=IncFile->new();
224     my $url=$fileobj->url();
225     my $sn=$self->config()->sequence($url);
226     $obj->init($url,$startline,$endline,$lines, $sn);
227     push @{$self->{includes}}, $fileobj;
228     push @{$self->{includesdesc}}, $obj;
229     }
230    
231 williamc 1.1 # ------------------------ Tag Routines -------------------------------
232    
233     #
234     # Include tag
235    
236     sub Include_Start {
237     my $self=shift;
238     my $name=shift;
239     my $hashref=shift;
240    
241     $self->checktag( $name,$hashref, "url");
242     print "Including ".$$hashref{'url'}."\n";
243     my $fileObj=$self->getfile($$hashref{'url'});
244     if ( defined $fileObj ) {
245     # dump out to our file in construction
246     my $fh=FileHandle->new();
247     my $outfilename=$fileObj->ProcessedFile();
248     $fh->open("<".$outfilename) or die "Unable to open $outfilename\n";
249     print {$self->{fileout}} "\n";# always start an include on a new line
250 williamc 1.3 my $linecount=0;
251 williamc 1.1 while ( <$fh> ) {
252 williamc 1.3 $linecount++;
253 williamc 1.1 print {$self->{fileout}} $_;
254     }
255     print {$self->{fileout}} "\n";# always end include with new line
256     undef $fh;
257 williamc 1.3 $self->_includefile($fileObj, $self->{currentparser}->tagstartline(),
258     $self->{currentparser}->line(), $linecount);
259 williamc 1.1 }
260     }
261