ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvsroot/COMP/SCRAM/src/Utilities/DataItem.pm
Revision: 1.3
Committed: Tue Feb 8 13:53:22 2000 UTC (25 years, 3 months ago) by williamc
Content type: text/plain
Branch: MAIN
CVS Tags: v102p1, V1_0_1, V1_0_0, V1_pre0, SCRAM_V1, SCRAMV1_IMPORT, V0_19_7, V0_19_6, V0_19_6p1, V0_19_5, SFATEST, V0_19_4, V0_19_4_pre3, V0_19_4_pre2, V0_19_4_pre1, V0_19_3, V0_19_2, V0_19_1, V0_19_0, V0_18_5, V0_18_4, V_18_3_TEST, VO_18_3, V0_18_2, V0_18_1, ProtoEnd
Branch point for: V1_pre1, SCRAM_V1_BRANCH, V0_19_4_B, HPWbranch, V0_9branch
Changes since 1.2: +88 -8 lines
Log Message:
Add aliasing mechanism and make dataItem store its own data

File Contents

# Content
1 #
2 # DataItem.pm
3 #
4 # Originally Written by Christopher Williams
5 #
6 # Description
7 # -----------
8 # A data container with a list of lookup keys
9 #
10 # Interface
11 # ---------
12 # new(data,@key) : A new DataItem object with the data matched to a keylist
13 # (See also restore)
14 # keys() : return a list of the fundamental keys for the data object
15 # data() : return the data for the object
16 # match(@keys) : return 1 if @keys matches those of the data item, else 0
17 # alias(@keys) : provide an alternative set of keys for matching
18 # unalias(@keys) : remove any aliases - original keys cannot be removed
19 # returns 1 if successful, 0 otherwise
20 # store(fh) : store to the given stream
21 # restore(fh) : Returns a new dataitem, initialised to the data found in
22 # fh
23
24 package Utilities::DataItem;
25 require 5.001;
26
27 sub new {
28 my $class=shift;
29 my $data=shift;
30 my @keys=@_;
31
32 $self={};
33 bless $self, $class;
34 $self->{data}=$data;
35 push @{$self->{keys}[0]},@keys; # fundamental keys
36 return $self;
37 }
38
39 sub keys {
40 my $self=shift;
41 return @{$self->{keys}[0]};
42 }
43
44 sub data {
45 my $self=shift;
46 return $self->{data};
47 }
48
49 sub match {
50 my $self=shift;
51 my @keys=@_;
52
53 my $nm;
54
55 # search over all key aliases
56 foreach $keyset ( @{$self->{keys}} ) {
57 $nm=0;
58 for ( $i=0; (($i <= $#keys) && ($i <=$#{$keyset} )); $i++ ) {
59 if ( $$keyset[$i] eq $keys[$i] ) {
60 $nm++;
61 }
62 }
63 return 1 , if ( $nm > $#keys ); # Succesful match
64 }
65 return 0;
66 }
67
68 sub alias {
69 my $self=shift;
70 my @keys=@_;
71
72 if ( $#keys<0 ) {
73 die "Unable to set an alias with no keys\n";
74 }
75 push @{$self->{keys}},[@keys];
76 }
77
78 sub unalias {
79 my $self=shift;
80 my @keys=@_;
81
82 my $rv=0;
83 for ( my $i=1; $i<=$#{$self->{keys}}; $i++ ) {
84 if ( "@{$self->{keys}[$i]}" eq "@keys" ) {
85 undef $self->{keys}[$i];
86 splice( @{$self->{keys}}, $i, 1);
87 $rv=1;
88 }
89 }
90 return $rv;
91 }
92
93 sub store {
94 my $self=shift;
95 my $fh=shift;
96
97 # print the keys first, aliases, then the data
98 foreach $keyset ( @{$self->{keys}} ) {
99 foreach $key ( @{$keyset} ) {
100 print $fh "#".$key."\n";
101 }
102 print $fh "#\n"; # alias marker
103 }
104 print $fh ">".$self->data()."\n";
105 }
106
107 sub restore {
108 my $class=shift;
109 my $fh=shift;
110
111 $self={};
112 bless $self, $class;
113
114 my @arr=();
115 while ( <$fh> ) {
116 chomp;
117 if ( $_ eq "#") { # set aliases
118 $self->alias(@arr);
119 @arr=();
120 }
121 elsif ( $_=~/^#(.*)/ ) {
122 push @arr, $1;
123 }
124 elsif ( $_=~/^>(.*)/ ) { # load in data
125 $data=$1;
126 $self->{data}=$data;
127 last;
128 }
129 else {
130 print "Data corruption detected\n";
131 last;
132 }
133 }
134 return $self;
135 }