source: trunk/gsdl/perllib/expinfo.pm@ 8518

Last change on this file since 8518 was 8518, checked in by chi, 19 years ago

A new program to deal with export.pl function.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 3.9 KB
Line 
1###########################################################################
2#
3# expinfo.pm --
4# A component of the Greenstone digital library software
5# from the New Zealand Digital Library Project at the
6# University of Waikato, New Zealand.
7#
8# Copyright (C) 1999 New Zealand Digital Library Project
9#
10# This program is free software; you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 2 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program; if not, write to the Free Software
22# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23#
24###########################################################################
25
26# modified by: Chi-Yu Huang
27# This module stores information about the export directory. At the moment
28# this information just consists of the file name (relative to the
29# directory the export information file is in) and its OID.
30
31# This module assumes there is a one to one correspondance between
32# a file in the export directory and an OID.
33
34
35package expinfo;
36
37sub new {
38 my ($class) = @_;
39 my $self = {'info'=>{},
40 'order'=>[]};
41
42 return bless $self, $class;
43}
44
45sub load_info {
46 my $self = shift (@_);
47 my ($filename) = @_;
48
49 $self->{'info'} = {};
50
51 if (-e $filename) {
52 open (INFILE, $filename) ||
53 die "expinfo::load_info couldn't read $filename\n";
54
55 my ($line, @line);
56 while (defined ($line = <INFILE>)) {
57 $line =~ s/\cM|\cJ//g; # remove end-of-line characters
58 @line = split ("\t", $line); # filename,
59 if (scalar(@line) >= 2) {
60 $self->add_info (@line);
61 }
62 }
63 close (INFILE);
64 }
65}
66
67sub save_info {
68 my $self = shift (@_);
69 my ($filename) = @_;
70
71 my ($OID, $info);
72
73 open (OUTFILE, ">$filename") ||
74 die "expinfo::save_info couldn't write $filename\n";
75
76 foreach $info (@{$self->get_OID_list()}) {
77 if (defined $info) {
78 print OUTFILE join("\t", @$info), "\n";
79 }
80 }
81 close (OUTFILE);
82}
83
84sub delete_info {
85 my $self = shift (@_);
86 my ($OID) = @_;
87
88 if (defined $self->{'info'}->{$OID}) {
89 delete $self->{'info'}->{$OID};
90
91 my $i = 0;
92 while ($i < scalar (@{$self->{'order'}})) {
93 if ($self->{'order'}->[$i]->[0] eq $OID) {
94 splice (@{$self->{'order'}}, $i, 1);
95 last;
96 }
97 $i ++;
98 }
99 }
100}
101
102sub add_info {
103 my $self = shift (@_);
104 my ($OID, $doc_file, $sortmeta) = @_;
105 $sortmeta = "" unless defined $sortmeta;
106
107 if (! defined($OID)) {
108 # only happens when no files can be processed?
109 return undef;
110 }
111 $self->delete_info ($OID);
112 $self->{'info'}->{$OID} = [$doc_file];
113 push (@{$self->{'order'}}, [$OID, $sortmeta]);
114}
115
116# returns a list of the form [[OID, doc_file], ...]
117sub get_OID_list {
118 my $self = shift (@_);
119
120 my ($OID);
121 my @list = ();
122
123 foreach $OID (sort {$a->[1] cmp $b->[1]} @{$self->{'order'}}) {
124 push (@list, [$OID->[0], $self->{'info'}->{$OID->[0]}->[0]]);
125 }
126 return \@list;
127}
128
129# returns a list of the form [[doc_file, OID], ...]
130sub get_file_list {
131 my $self = shift (@_);
132
133 my ($OID);
134 my @list = ();
135
136 foreach $OID (sort {$a->[1] cmp $b->[1]} @{$self->{'order'}}) {
137 push (@list, [$self->{'info'}->{$OID->[0]}->[0], $OID->[0]]);
138 }
139 return \@list;
140}
141
142
143# returns a list of the form [doc_file]
144sub get_info {
145 my $self = shift (@_);
146 my ($OID) = @_;
147
148 if (defined $self->{'info'}->{$OID}) {
149 return $self->{'info'}->{$OID};
150 }
151
152 return undef;
153}
154
155
156# returns the number of documents so far
157sub size {
158 my $self = shift (@_);
159 return (scalar(@{$self->{'order'}}));
160}
161
1621;
163
Note: See TracBrowser for help on using the repository browser.