source: trunk/gsdl/perllib/arcinfo.pm@ 14

Last change on this file since 14 was 14, checked in by rjmcnab, 25 years ago

modified internal representation so that order is preserved (and
therefore sorting is possible).

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 2.4 KB
Line 
1# This module stores information about the archives. At the moment
2# this information just consists of the file name (relative to the
3# directory the archives information file is in) and its OID.
4
5# This module assumes there is a one to one correspondance between
6# a file in the archives directory and an OID.
7
8
9package arcinfo;
10
11sub new {
12 my ($class) = @_;
13 my $self = {'info'=>{},
14 'order'=>[]};
15
16 return bless $self, $class;
17}
18
19sub load_info {
20 my $self = shift (@_);
21 my ($filename) = @_;
22
23 $self->{'info'} = {};
24
25 if (-e $filename) {
26 open (INFILE, $filename) ||
27 die "arcinfo::load_info couldn't read $filename\n";
28
29 my ($line, @line);
30 while (defined ($line = <INFILE>)) {
31 $line =~ s/\cM|\cJ//g; # remove end-of-line characters
32 @line = split ("\t", $line); # filename,
33 if (scalar(@line) >= 2) {
34 $self->add_info (@line);
35 }
36 }
37
38 close (INFILE);
39 }
40}
41
42sub save_info {
43 my $self = shift (@_);
44 my ($filename) = @_;
45
46 my ($OID, $info);
47
48 open (OUTFILE, ">$filename") ||
49 die "arcinfo::save_info couldn't write $filename\n";
50
51 foreach $info (@{$self->get_OID_list()}) {
52 if (defined $info) {
53 print OUTFILE join("\t", @$info), "\n";
54 }
55 }
56
57 close (OUTFILE);
58}
59
60sub delete_info {
61 my $self = shift (@_);
62 my ($OID) = @_;
63
64 if (defined $self->{'info'}->{$OID}) {
65 delete $self->{'info'}->{$OID};
66
67 my $i = 0;
68 while ($i < scalar (@{$self->{'order'}})) {
69 if ($self->{'order'}->[$i] eq $OID) {
70 splice (@{$self->{'order'}}, $i, 1);
71 last;
72 }
73
74 $i ++;
75 }
76 }
77}
78
79sub add_info {
80 my $self = shift (@_);
81 my ($OID, $doc_file) = @_;
82
83 $self->delete_info ($OID);
84 $self->{'info'}->{$OID} = [$doc_file];
85 push (@{$self->{'order'}}, $OID);
86}
87
88# returns a list of the form [[OID, doc_file], ...]
89sub get_OID_list {
90 my $self = shift (@_);
91
92 my ($OID);
93 my @list = ();
94
95 foreach $OID (@{$self->{'order'}}) {
96 push (@list, [$OID, $self->{'info'}->{$OID}->[0]]);
97 }
98
99 return \@list;
100}
101
102# returns a list of the form [[doc_file, OID], ...]
103sub get_file_list {
104 my $self = shift (@_);
105
106 my ($OID);
107 my @list = ();
108
109 foreach $OID (@{$self->{'order'}}) {
110 push (@list, [$self->{'info'}->{$OID}->[0], $OID]);
111 }
112
113 return \@list;
114}
115
116
117# returns a list of the form [doc_file]
118sub get_info {
119 my $self = shift (@_);
120 my ($OID) = @_;
121
122 if (defined $self->{'info'}->{$OID}) {
123 return $self->{'info'}->{$OID};
124 }
125
126 return undef;
127}
128
129
1301;
131
Note: See TracBrowser for help on using the repository browser.