source: trunk/gsdl/perllib/cfgread.pm@ 2018

Last change on this file since 2018 was 813, checked in by sjboddie, 24 years ago

plugins now take options and classifiers are handled properly

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 3.9 KB
Line 
1###########################################################################
2#
3# cfgread.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# reads in configuration files
27
28package cfgread;
29
30
31sub read_cfg_line {
32 my ($handle) = @_;
33 my $line = "";
34 my @line = ();
35 my $linecontinues = 0;
36
37 while (defined($line = <$handle>)) {
38 $line =~ s/^\#.*$//; # remove comments
39 $line =~ s/\cM|\cJ//g; # remove end-of-line characters
40 $line =~ s/^\s+//; # remove initial white space
41 $linecontinues = $line =~ s/\\$//;
42 while ($line =~ s/\s*(\"[^\"]*\"|\'[^\']*\'|\S+)\s*//) {
43 if (defined $1) {
44 # remove any enclosing quotes
45 my $entry = $1;
46 $entry =~ s/^([\"\'])(.*)\1$/$2/;
47
48 # substitute an environment variables
49 $entry =~ s/\$(\w+)/$ENV{$1}/g;
50 $entry =~ s/\$\{(\w+)\}/$ENV{$1}/g;
51
52 push (@line, $entry);
53 } else {
54 push (@line, "");
55 }
56 }
57
58 if (scalar(@line) > 0 && !$linecontinues) {
59# print STDERR "line: \"" . join ("\" \"", @line) . "\"\n";
60 return \@line;
61 }
62 }
63
64 return undef;
65}
66
67sub write_cfg_line {
68 my ($handle, $line) = @_;
69 print $handle join ("\t", @$line), "\n";
70}
71
72
73# stringexp, arrayexp, hashexp and arrayarrayexp
74# should be something like '^(this|that)$'
75sub read_cfg_file {
76 my ($filename, $stringexp, $arrayexp, $hashexp, $arrayarrayexp) = @_;
77 my ($line);
78 my $data = {};
79
80 if (open (COLCFG, $filename)) {
81 while (defined ($line = &read_cfg_line('COLCFG'))) {
82 if (scalar(@$line) >= 2) {
83 my $key = shift (@$line);
84 if (defined $stringexp && $key =~ /$stringexp/) {
85 $data->{$key} = shift (@$line);
86
87 } elsif (defined $arrayexp && $key =~ /$arrayexp/) {
88 push (@{$data->{$key}}, @$line);
89
90 } elsif (defined $hashexp && $key =~ /$hashexp/) {
91 my $k = shift @$line;
92 my $v = shift @$line;
93 $data->{$key}->{$k} = $v;
94 } elsif (defined $arrayarrayexp && $key =~ /$arrayarrayexp/) {
95 if (!defined $data->{$key}) {
96 $data->{$key} = [];
97 }
98 push (@{$data->{$key}}, $line);
99 }
100 }
101 }
102 close (COLCFG);
103 } else {
104 print STDERR "cfgread::read_cfg_file couldn't read the cfg file $filename\n";
105 }
106
107 return $data;
108}
109
110
111# stringexp, arrayexp, hashexp and arrayarrayexp
112# should be something like '^(this|that)$'
113sub write_cfg_file {
114 my ($filename, $data, $stringexp, $arrayexp, $hashexp, $arrayarrayexp) = @_;
115
116 if (open (COLCFG, ">$filename")) {
117 foreach $key (sort(keys(%$data))) {
118 if ($key =~ /$stringexp/) {
119 &write_cfg_line ('COLCFG', [$key, $data->{$key}]);
120 } elsif ($key =~ /$arrayexp/) {
121 &write_cfg_line ('COLCFG', [$key, @{$data->{$key}}]);
122 } elsif ($key =~ /$hashexp/) {
123 foreach $k (keys (%{$data->{$key}})) {
124 &write_cfg_line ('COLCFG', [$key, $k, $data->{$key}->{$k}]);
125 }
126 } elsif ($key =~ /$arrayarrayexp/) {
127 foreach $k (@{$data->{$key}}) {
128 &write_cfg_line ('COLCFG', [$key, @$k]);
129 }
130 }
131 }
132 close (COLCFG);
133 } else {
134 print STDERR "cfgread::write_cfg_file couldn't write the cfg file $filename\n";
135 }
136}
137
138
1391;
Note: See TracBrowser for help on using the repository browser.