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