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

Last change on this file since 14926 was 8890, checked in by davidb, 19 years ago

Reading of config files has support for environment variables, however
this conficts with a user trying to legitimately include a dollar symbol
to denote currency. Since there were two ways an enivorment variable
can be specified, I have removed the one that conflicts with a collection
designer specifying a literal dollar symbol.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 4.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 # Merge with following line if a quoted phrase is left un-closed.
42 if ($line =~ m/[^\\]([\"\'])/) {
43 my $quote=$1;
44 while ($line !~ m/$quote(.*?[^\\])?(\\\\)*$quote/) {
45 my $nextline = <$handle>;
46 if (defined($nextline)) {
47 $nextline =~ s/\r?\n//; # remove end-of-line
48 $line .= " " . $nextline;
49 } else {
50 return undef; # parse error?
51 }
52 }
53 }
54 $linecontinues = $line =~ s/\\$//;
55
56 while ($line =~ s/\s*(\".*?[^\\](\\\\)*\"|\'.*?[^\\](\\\\)*\'|\S+)\s*//) {
57 if (defined $1) {
58 # remove any enclosing quotes
59 my $entry = $1;
60 $entry =~ s/^([\"\'])(.*)\1$/$2/;
61
62 # substitute an environment variables
63## $entry =~ s/\$(\w+)/$ENV{$1}/g;
64 $entry =~ s/\$\{(\w+)\}/$ENV{$1}/g;
65 push (@line, $entry);
66 } else {
67 push (@line, "");
68 }
69 }
70
71 if (scalar(@line) > 0 && !$linecontinues) {
72# print STDERR "line: \"" . join ("\" \"", @line) . "\"\n";
73 return \@line;
74 }
75 }
76
77 return undef;
78}
79
80sub write_cfg_line {
81 my ($handle, $line) = @_;
82 print $handle join ("\t", @$line), "\n";
83}
84
85
86# stringexp, arrayexp, hashexp,arrayarrayexp and hashhashexp
87# should be something like '^(this|that)$'
88sub read_cfg_file {
89 my ($filename, $stringexp, $arrayexp, $hashexp, $arrayarrayexp,
90 $hashhashexp) = @_;
91 my ($line);
92 my $data = {};
93
94 if (open (COLCFG, $filename)) {
95 while (defined ($line = &read_cfg_line('COLCFG'))) {
96 if (scalar(@$line) >= 2) {
97 my $key = shift (@$line);
98 if (defined $stringexp && $key =~ /$stringexp/) {
99 $data->{$key} = shift (@$line);
100
101 } elsif (defined $arrayexp && $key =~ /$arrayexp/) {
102 push (@{$data->{$key}}, @$line);
103
104 } elsif (defined $hashexp && $key =~ /$hashexp/) {
105 my $k = shift @$line;
106 my $v = shift @$line;
107 $data->{$key}->{$k} = $v;
108 } elsif (defined $arrayarrayexp && $key =~ /$arrayarrayexp/) {
109 if (!defined $data->{$key}) {
110 $data->{$key} = [];
111 }
112 push (@{$data->{$key}}, $line);
113 }
114 elsif (defined $hashhashexp && $key =~ /$hashhashexp/) {
115 my $k = shift @$line;
116 my $p = shift @$line;
117 my $v = shift @$line;
118 if (!defined $v) {
119 $v = $p;
120 $p = 'default';
121 }
122 $data->{$key}->{$k}->{$p} = $v;
123 }
124 }
125 }
126 close (COLCFG);
127
128 } else {
129 print STDERR "cfgread::read_cfg_file couldn't read the cfg file $filename\n";
130 }
131
132 return $data;
133}
134
135
136# stringexp, arrayexp, hashexp and arrayarrayexp
137# should be something like '^(this|that)$'
138sub write_cfg_file {
139 my ($filename, $data, $stringexp, $arrayexp, $hashexp, $arrayarrayexp,
140 $hashhashexp) = @_;
141
142 if (open (COLCFG, ">$filename")) {
143 foreach $key (sort(keys(%$data))) {
144 if ($key =~ /$stringexp/) {
145 &write_cfg_line ('COLCFG', [$key, $data->{$key}]);
146 } elsif ($key =~ /$arrayexp/) {
147 &write_cfg_line ('COLCFG', [$key, @{$data->{$key}}]);
148 } elsif ($key =~ /$hashexp/) {
149 foreach $k (keys (%{$data->{$key}})) {
150 &write_cfg_line ('COLCFG', [$key, $k, $data->{$key}->{$k}]);
151 }
152 } elsif ($key =~ /$arrayarrayexp/) {
153 foreach $k (@{$data->{$key}}) {
154 &write_cfg_line ('COLCFG', [$key, @$k]);
155 }
156 } elsif ($key =~ /$hashhashexp/) {
157 foreach $k (keys (%{$data->{$key}})) {
158 foreach $p (keys (%{$data->{$key}->{$k}})) {
159 if ($p =~ /default/) {
160 &write_cfg_line ('COLCFG',
161 [$key, $k, $data->{$key}->{$k}]);
162 }
163 else {
164 &write_cfg_line ('COLCFG',
165 [$key, $k, $p, $data->{$key}->{$k}->{$p}]);
166 }
167 }
168 }
169 }
170 }
171 close (COLCFG);
172 } else {
173 print STDERR "cfgread::write_cfg_file couldn't write the cfg file $filename\n";
174 }
175}
176
177
1781;
Note: See TracBrowser for help on using the repository browser.