1 |
sashby |
1.4 |
=head1 NAME
|
2 |
williamc |
1.1 |
|
3 |
sashby |
1.4 |
Utilities::DataItem - A data container with a list of lookup keys.
|
4 |
|
|
|
5 |
|
|
=head1 SYNOPSIS
|
6 |
|
|
|
7 |
|
|
my $obj = Utilities::DataItem->new();
|
8 |
|
|
|
9 |
|
|
=head1 DESCRIPTION
|
10 |
|
|
|
11 |
|
|
A data container with a list of lookup keys.
|
12 |
|
|
|
13 |
|
|
=head1 METHODS
|
14 |
|
|
|
15 |
|
|
=over
|
16 |
|
|
|
17 |
|
|
=cut
|
18 |
|
|
|
19 |
|
|
=item C<new(data,@key)
|
20 |
|
|
A new DataItem object with the data matched to a keylist (See also restore).
|
21 |
|
|
|
22 |
|
|
=item C<keys()>
|
23 |
|
|
Return a list of the fundamental keys for the data object.
|
24 |
|
|
|
25 |
|
|
=item C<data()>
|
26 |
|
|
Return the data for the object.
|
27 |
|
|
|
28 |
|
|
=item C<match(@keys)>
|
29 |
|
|
Return 1 if @keys matches those of the data item, else 0.
|
30 |
|
|
|
31 |
|
|
=item C<alias(@keys)>
|
32 |
|
|
Provide an alternative set of keys for matching.
|
33 |
|
|
|
34 |
|
|
=item C<unalias(@keys)>
|
35 |
|
|
Remove any aliases. Original keys cannot be removed.
|
36 |
|
|
Returns 1 if successful, 0 otherwise.
|
37 |
|
|
|
38 |
|
|
=item C<store(fh)>
|
39 |
|
|
Store to the given stream.
|
40 |
|
|
|
41 |
|
|
=item C<restore(fh)>
|
42 |
|
|
Returns a new dataitem, initialised to the data found in fh.
|
43 |
|
|
|
44 |
|
|
=back
|
45 |
|
|
|
46 |
|
|
=head1 AUTHOR
|
47 |
|
|
|
48 |
|
|
Originally Written by Christopher Williams.
|
49 |
|
|
|
50 |
|
|
=head1 MAINTAINER
|
51 |
|
|
|
52 |
sashby |
1.5 |
Shaun ASHBY
|
53 |
sashby |
1.4 |
|
54 |
|
|
=cut
|
55 |
|
|
|
56 |
williamc |
1.1 |
package Utilities::DataItem;
|
57 |
|
|
require 5.001;
|
58 |
|
|
|
59 |
|
|
sub new {
|
60 |
|
|
my $class=shift;
|
61 |
|
|
my $data=shift;
|
62 |
|
|
my @keys=@_;
|
63 |
|
|
|
64 |
|
|
$self={};
|
65 |
|
|
bless $self, $class;
|
66 |
|
|
$self->{data}=$data;
|
67 |
williamc |
1.3 |
push @{$self->{keys}[0]},@keys; # fundamental keys
|
68 |
williamc |
1.1 |
return $self;
|
69 |
|
|
}
|
70 |
|
|
|
71 |
|
|
sub keys {
|
72 |
|
|
my $self=shift;
|
73 |
williamc |
1.3 |
return @{$self->{keys}[0]};
|
74 |
williamc |
1.1 |
}
|
75 |
|
|
|
76 |
|
|
sub data {
|
77 |
|
|
my $self=shift;
|
78 |
|
|
return $self->{data};
|
79 |
|
|
}
|
80 |
|
|
|
81 |
|
|
sub match {
|
82 |
|
|
my $self=shift;
|
83 |
|
|
my @keys=@_;
|
84 |
|
|
|
85 |
williamc |
1.3 |
my $nm;
|
86 |
williamc |
1.1 |
|
87 |
williamc |
1.3 |
# search over all key aliases
|
88 |
|
|
foreach $keyset ( @{$self->{keys}} ) {
|
89 |
|
|
$nm=0;
|
90 |
|
|
for ( $i=0; (($i <= $#keys) && ($i <=$#{$keyset} )); $i++ ) {
|
91 |
|
|
if ( $$keyset[$i] eq $keys[$i] ) {
|
92 |
williamc |
1.1 |
$nm++;
|
93 |
williamc |
1.3 |
}
|
94 |
|
|
}
|
95 |
|
|
return 1 , if ( $nm > $#keys ); # Succesful match
|
96 |
|
|
}
|
97 |
|
|
return 0;
|
98 |
|
|
}
|
99 |
|
|
|
100 |
|
|
sub alias {
|
101 |
|
|
my $self=shift;
|
102 |
|
|
my @keys=@_;
|
103 |
|
|
|
104 |
|
|
if ( $#keys<0 ) {
|
105 |
|
|
die "Unable to set an alias with no keys\n";
|
106 |
|
|
}
|
107 |
|
|
push @{$self->{keys}},[@keys];
|
108 |
|
|
}
|
109 |
|
|
|
110 |
|
|
sub unalias {
|
111 |
|
|
my $self=shift;
|
112 |
|
|
my @keys=@_;
|
113 |
|
|
|
114 |
|
|
my $rv=0;
|
115 |
|
|
for ( my $i=1; $i<=$#{$self->{keys}}; $i++ ) {
|
116 |
|
|
if ( "@{$self->{keys}[$i]}" eq "@keys" ) {
|
117 |
|
|
undef $self->{keys}[$i];
|
118 |
|
|
splice( @{$self->{keys}}, $i, 1);
|
119 |
|
|
$rv=1;
|
120 |
|
|
}
|
121 |
|
|
}
|
122 |
|
|
return $rv;
|
123 |
|
|
}
|
124 |
|
|
|
125 |
|
|
sub store {
|
126 |
|
|
my $self=shift;
|
127 |
|
|
my $fh=shift;
|
128 |
|
|
|
129 |
|
|
# print the keys first, aliases, then the data
|
130 |
|
|
foreach $keyset ( @{$self->{keys}} ) {
|
131 |
|
|
foreach $key ( @{$keyset} ) {
|
132 |
|
|
print $fh "#".$key."\n";
|
133 |
|
|
}
|
134 |
|
|
print $fh "#\n"; # alias marker
|
135 |
|
|
}
|
136 |
|
|
print $fh ">".$self->data()."\n";
|
137 |
|
|
}
|
138 |
|
|
|
139 |
|
|
sub restore {
|
140 |
|
|
my $class=shift;
|
141 |
|
|
my $fh=shift;
|
142 |
|
|
|
143 |
|
|
$self={};
|
144 |
|
|
bless $self, $class;
|
145 |
|
|
|
146 |
|
|
my @arr=();
|
147 |
|
|
while ( <$fh> ) {
|
148 |
|
|
chomp;
|
149 |
|
|
if ( $_ eq "#") { # set aliases
|
150 |
|
|
$self->alias(@arr);
|
151 |
|
|
@arr=();
|
152 |
|
|
}
|
153 |
|
|
elsif ( $_=~/^#(.*)/ ) {
|
154 |
|
|
push @arr, $1;
|
155 |
|
|
}
|
156 |
|
|
elsif ( $_=~/^>(.*)/ ) { # load in data
|
157 |
|
|
$data=$1;
|
158 |
|
|
$self->{data}=$data;
|
159 |
|
|
last;
|
160 |
|
|
}
|
161 |
|
|
else {
|
162 |
|
|
print "Data corruption detected\n";
|
163 |
|
|
last;
|
164 |
williamc |
1.1 |
}
|
165 |
|
|
}
|
166 |
williamc |
1.3 |
return $self;
|
167 |
williamc |
1.1 |
}
|