[537] | 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 |
|
---|
[4] | 26 | # reads in configuration files
|
---|
| 27 |
|
---|
| 28 | package cfgread;
|
---|
| 29 |
|
---|
| 30 |
|
---|
| 31 | sub read_cfg_line {
|
---|
| 32 | my ($handle) = @_;
|
---|
| 33 | my $line = "";
|
---|
[132] | 34 | my @line = ();
|
---|
| 35 | my $linecontinues = 0;
|
---|
[4] | 36 |
|
---|
| 37 | while (defined($line = <$handle>)) {
|
---|
[132] | 38 | $line =~ s/^\#.*$//; # remove comments
|
---|
[4] | 39 | $line =~ s/\cM|\cJ//g; # remove end-of-line characters
|
---|
[132] | 40 | $line =~ s/^\s+//; # remove initial white space
|
---|
[3418] | 41 | # Merge with following line if a quoted phrase is left un-closed.
|
---|
[3507] | 42 | if ($line =~ m/[^\\]([\"\'])/) {
|
---|
| 43 | my $quote=$1;
|
---|
[7692] | 44 | while ($line !~ m/$quote(.*?[^\\])?(\\\\)*$quote/) {
|
---|
[3418] | 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 | }
|
---|
[132] | 54 | $linecontinues = $line =~ s/\\$//;
|
---|
[3418] | 55 |
|
---|
[7692] | 56 | while ($line =~ s/\s*(\".*?[^\\](\\\\)*\"|\'.*?[^\\](\\\\)*\'|\S+)\s*//) {
|
---|
[132] | 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";
|
---|
[4] | 73 | return \@line;
|
---|
| 74 | }
|
---|
| 75 | }
|
---|
[132] | 76 |
|
---|
[4] | 77 | return undef;
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | sub write_cfg_line {
|
---|
| 81 | my ($handle, $line) = @_;
|
---|
| 82 | print $handle join ("\t", @$line), "\n";
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 |
|
---|
[2772] | 86 | # stringexp, arrayexp, hashexp,arrayarrayexp and hashhashexp
|
---|
[813] | 87 | # should be something like '^(this|that)$'
|
---|
[4] | 88 | sub read_cfg_file {
|
---|
[2772] | 89 | my ($filename, $stringexp, $arrayexp, $hashexp, $arrayarrayexp,
|
---|
| 90 | $hashhashexp) = @_;
|
---|
[4] | 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);
|
---|
[91] | 98 | if (defined $stringexp && $key =~ /$stringexp/) {
|
---|
[4] | 99 | $data->{$key} = shift (@$line);
|
---|
| 100 |
|
---|
[91] | 101 | } elsif (defined $arrayexp && $key =~ /$arrayexp/) {
|
---|
[4] | 102 | push (@{$data->{$key}}, @$line);
|
---|
[69] | 103 |
|
---|
[91] | 104 | } elsif (defined $hashexp && $key =~ /$hashexp/) {
|
---|
[69] | 105 | my $k = shift @$line;
|
---|
| 106 | my $v = shift @$line;
|
---|
| 107 | $data->{$key}->{$k} = $v;
|
---|
[813] | 108 | } elsif (defined $arrayarrayexp && $key =~ /$arrayarrayexp/) {
|
---|
| 109 | if (!defined $data->{$key}) {
|
---|
| 110 | $data->{$key} = [];
|
---|
| 111 | }
|
---|
| 112 | push (@{$data->{$key}}, $line);
|
---|
[4] | 113 | }
|
---|
[2772] | 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 | }
|
---|
[4] | 124 | }
|
---|
| 125 | }
|
---|
| 126 | close (COLCFG);
|
---|
[2772] | 127 |
|
---|
[4] | 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 |
|
---|
[813] | 136 | # stringexp, arrayexp, hashexp and arrayarrayexp
|
---|
| 137 | # should be something like '^(this|that)$'
|
---|
[4] | 138 | sub write_cfg_file {
|
---|
[2772] | 139 | my ($filename, $data, $stringexp, $arrayexp, $hashexp, $arrayarrayexp,
|
---|
| 140 | $hashhashexp) = @_;
|
---|
[813] | 141 |
|
---|
[4] | 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}}]);
|
---|
[69] | 148 | } elsif ($key =~ /$hashexp/) {
|
---|
| 149 | foreach $k (keys (%{$data->{$key}})) {
|
---|
| 150 | &write_cfg_line ('COLCFG', [$key, $k, $data->{$key}->{$k}]);
|
---|
| 151 | }
|
---|
[813] | 152 | } elsif ($key =~ /$arrayarrayexp/) {
|
---|
| 153 | foreach $k (@{$data->{$key}}) {
|
---|
| 154 | &write_cfg_line ('COLCFG', [$key, @$k]);
|
---|
| 155 | }
|
---|
[2772] | 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 | }
|
---|
[4] | 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 |
|
---|
| 178 | 1;
|
---|