ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/ActiveDoc/PreProcessedFile.pm
Revision: 1.4
Committed: Mon Nov 29 17:32:48 1999 UTC (25 years, 5 months ago) by williamc
Content type: text/plain
Branch: MAIN
Changes since 1.3: +8 -2 lines
Log Message:
realline and line seperated functions

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