ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/ActiveDoc/TreeMonkey.pm
Revision: 1.3
Committed: Mon Sep 20 16:27:57 1999 UTC (25 years, 7 months ago) by williamc
Content type: text/plain
Branch: MAIN
CVS Tags: ProtoEnd
Changes since 1.2: +1 -1 lines
Log Message:
Basic Working Level

File Contents

# User Rev Content
1 williamc 1.1 #
2     # TreeMonkey - Climb around the tree as directed by a search string
3     #
4     # Interface
5     # -----------
6     # new() : A new monkey is born
7     # find(string) : Tell the monkey to search the tree for the node at the
8     # location indicated by the string, relative to the current
9     # tree node.
10     # goto(TreeNode) : Tell the monkey to go to a particular node in the tree
11    
12 williamc 1.3 package ActiveDoc::TreeMonkey;
13 williamc 1.2 require 5.001;
14     use Utilities::ListIterator;
15 williamc 1.1
16     sub new {
17     my $class=shift;
18     $self={};
19     bless $self, $class;
20     return $self;
21     }
22    
23     sub goto {
24     my $self=shift;
25 williamc 1.2 my $node=shift;
26    
27     $self->{currentnode}=$node;
28 williamc 1.1 }
29    
30     sub find {
31     my $self=shift;
32     my $string=shift;
33     my @words;
34     my $first;
35     my $found=0;
36 williamc 1.2 my $branch="";
37 williamc 1.1
38     # Split string
39     ($first, @words)=split '/', $string;
40    
41     # Search towards root for first word or until we reach root
42 williamc 1.2 while ( ( $self->{currentnode}->parent() ne '/' ) &&
43     ( $self->{currentnode}->name() ne $first ) ) {
44     $self->{currentnode}=$self->{currentnode}->parent();
45 williamc 1.1 }
46    
47     # Check the case of just a single word
48 williamc 1.2 if (( $#words < 0 )) {
49     if (( $self->{currentnode}->name() eq $first )) {
50 williamc 1.1 $found=1;
51 williamc 1.2 }
52     }
53     else { #if we have some more words...
54     $f=1;
55     $n=0;
56     while ( ($f!=0) && ( $n < $#words ) ) {
57     my $branchlist=$self->{currentnode}->nodeList();
58     $f=$self->_checklist($words[$n],$branchlist);
59     $n++;
60     print $n." ".$f."\n";
61     }
62     $found=$f;
63 williamc 1.1 }
64    
65     # Now Search the child branches for the pattern
66     # Now return the object ID of the found node or null
67     if ( $found==1 ) {
68     return $self->{currentnode};
69     }
70     else {
71     return "";
72     }
73     }
74 williamc 1.2
75     # ------------------------ Support Routines ------------------------------
76    
77     sub _checklist {
78     my $self=shift;
79     my $word=shift;
80     my $branchlist=shift;
81     my $bit;
82     my $branch;
83     my $found=0;
84    
85     $bit=ListIterator->new($branchlist);
86    
87     while ( ($found==0) && ( ! $bit->last() ) ) {
88     $branch=$bit->next();
89     if ( $branch->name() eq $word) {
90     $self->{currentnode}=$branch;
91     $found=1;
92     }
93     } # end while
94     return $found;
95     }
96    
97