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 |
|
---|
31 | sub 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 |
|
---|
67 | sub write_cfg_line {
|
---|
68 | my ($handle, $line) = @_;
|
---|
69 | print $handle join ("\t", @$line), "\n";
|
---|
70 | }
|
---|
71 |
|
---|
72 |
|
---|
73 | # stringexp, arrayexp, hashexp,arrayarrayexp and hashhashexp
|
---|
74 | # should be something like '^(this|that)$'
|
---|
75 | sub read_cfg_file {
|
---|
76 | my ($filename, $stringexp, $arrayexp, $hashexp, $arrayarrayexp,
|
---|
77 | $hashhashexp) = @_;
|
---|
78 | my ($line);
|
---|
79 | my $data = {};
|
---|
80 |
|
---|
81 | if (open (COLCFG, $filename)) {
|
---|
82 | while (defined ($line = &read_cfg_line('COLCFG'))) {
|
---|
83 | if (scalar(@$line) >= 2) {
|
---|
84 | my $key = shift (@$line);
|
---|
85 | if (defined $stringexp && $key =~ /$stringexp/) {
|
---|
86 | $data->{$key} = shift (@$line);
|
---|
87 |
|
---|
88 | } elsif (defined $arrayexp && $key =~ /$arrayexp/) {
|
---|
89 | push (@{$data->{$key}}, @$line);
|
---|
90 |
|
---|
91 | } elsif (defined $hashexp && $key =~ /$hashexp/) {
|
---|
92 | my $k = shift @$line;
|
---|
93 | my $v = shift @$line;
|
---|
94 | $data->{$key}->{$k} = $v;
|
---|
95 | } elsif (defined $arrayarrayexp && $key =~ /$arrayarrayexp/) {
|
---|
96 | if (!defined $data->{$key}) {
|
---|
97 | $data->{$key} = [];
|
---|
98 | }
|
---|
99 | push (@{$data->{$key}}, $line);
|
---|
100 | }
|
---|
101 | elsif (defined $hashhashexp && $key =~ /$hashhashexp/) {
|
---|
102 | my $k = shift @$line;
|
---|
103 | my $p = shift @$line;
|
---|
104 | my $v = shift @$line;
|
---|
105 | if (!defined $v) {
|
---|
106 | $v = $p;
|
---|
107 | $p = 'default';
|
---|
108 | }
|
---|
109 | $data->{$key}->{$k}->{$p} = $v;
|
---|
110 | }
|
---|
111 | }
|
---|
112 | }
|
---|
113 | close (COLCFG);
|
---|
114 |
|
---|
115 | } else {
|
---|
116 | print STDERR "cfgread::read_cfg_file couldn't read the cfg file $filename\n";
|
---|
117 | }
|
---|
118 |
|
---|
119 | return $data;
|
---|
120 | }
|
---|
121 |
|
---|
122 |
|
---|
123 | # stringexp, arrayexp, hashexp and arrayarrayexp
|
---|
124 | # should be something like '^(this|that)$'
|
---|
125 | sub write_cfg_file {
|
---|
126 | my ($filename, $data, $stringexp, $arrayexp, $hashexp, $arrayarrayexp,
|
---|
127 | $hashhashexp) = @_;
|
---|
128 |
|
---|
129 | if (open (COLCFG, ">$filename")) {
|
---|
130 | foreach $key (sort(keys(%$data))) {
|
---|
131 | if ($key =~ /$stringexp/) {
|
---|
132 | &write_cfg_line ('COLCFG', [$key, $data->{$key}]);
|
---|
133 | } elsif ($key =~ /$arrayexp/) {
|
---|
134 | &write_cfg_line ('COLCFG', [$key, @{$data->{$key}}]);
|
---|
135 | } elsif ($key =~ /$hashexp/) {
|
---|
136 | foreach $k (keys (%{$data->{$key}})) {
|
---|
137 | &write_cfg_line ('COLCFG', [$key, $k, $data->{$key}->{$k}]);
|
---|
138 | }
|
---|
139 | } elsif ($key =~ /$arrayarrayexp/) {
|
---|
140 | foreach $k (@{$data->{$key}}) {
|
---|
141 | &write_cfg_line ('COLCFG', [$key, @$k]);
|
---|
142 | }
|
---|
143 | } elsif ($key =~ /$hashhashexp/) {
|
---|
144 | foreach $k (keys (%{$data->{$key}})) {
|
---|
145 | foreach $p (keys (%{$data->{$key}->{$k}})) {
|
---|
146 | if ($p =~ /default/) {
|
---|
147 | &write_cfg_line ('COLCFG',
|
---|
148 | [$key, $k, $data->{$key}->{$k}]);
|
---|
149 | }
|
---|
150 | else {
|
---|
151 | &write_cfg_line ('COLCFG',
|
---|
152 | [$key, $k, $p, $data->{$key}->{$k}->{$p}]);
|
---|
153 | }
|
---|
154 | }
|
---|
155 | }
|
---|
156 | }
|
---|
157 | }
|
---|
158 | close (COLCFG);
|
---|
159 | } else {
|
---|
160 | print STDERR "cfgread::write_cfg_file couldn't write the cfg file $filename\n";
|
---|
161 | }
|
---|
162 | }
|
---|
163 |
|
---|
164 |
|
---|
165 | 1;
|
---|