[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 |
|
---|
[15894] | 30 | use strict; no strict 'refs';
|
---|
[4] | 31 |
|
---|
[15889] | 32 |
|
---|
[4] | 33 | sub read_cfg_line {
|
---|
| 34 | my ($handle) = @_;
|
---|
| 35 | my $line = "";
|
---|
[132] | 36 | my @line = ();
|
---|
| 37 | my $linecontinues = 0;
|
---|
[4] | 38 |
|
---|
| 39 | while (defined($line = <$handle>)) {
|
---|
[132] | 40 | $line =~ s/^\#.*$//; # remove comments
|
---|
[4] | 41 | $line =~ s/\cM|\cJ//g; # remove end-of-line characters
|
---|
[132] | 42 | $line =~ s/^\s+//; # remove initial white space
|
---|
[3418] | 43 | # Merge with following line if a quoted phrase is left un-closed.
|
---|
[3507] | 44 | if ($line =~ m/[^\\]([\"\'])/) {
|
---|
| 45 | my $quote=$1;
|
---|
[7692] | 46 | while ($line !~ m/$quote(.*?[^\\])?(\\\\)*$quote/) {
|
---|
[3418] | 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 | }
|
---|
[132] | 56 | $linecontinues = $line =~ s/\\$//;
|
---|
[3418] | 57 |
|
---|
[7692] | 58 | while ($line =~ s/\s*(\".*?[^\\](\\\\)*\"|\'.*?[^\\](\\\\)*\'|\S+)\s*//) {
|
---|
[132] | 59 | if (defined $1) {
|
---|
| 60 | # remove any enclosing quotes
|
---|
| 61 | my $entry = $1;
|
---|
| 62 | $entry =~ s/^([\"\'])(.*)\1$/$2/;
|
---|
| 63 |
|
---|
| 64 | # substitute an environment variables
|
---|
[8890] | 65 | ## $entry =~ s/\$(\w+)/$ENV{$1}/g;
|
---|
[132] | 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";
|
---|
[4] | 75 | return \@line;
|
---|
| 76 | }
|
---|
| 77 | }
|
---|
[132] | 78 |
|
---|
[4] | 79 | return undef;
|
---|
| 80 | }
|
---|
| 81 |
|
---|
| 82 | sub write_cfg_line {
|
---|
| 83 | my ($handle, $line) = @_;
|
---|
| 84 | print $handle join ("\t", @$line), "\n";
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 |
|
---|
[2772] | 88 | # stringexp, arrayexp, hashexp,arrayarrayexp and hashhashexp
|
---|
[813] | 89 | # should be something like '^(this|that)$'
|
---|
[4] | 90 | sub read_cfg_file {
|
---|
[2772] | 91 | my ($filename, $stringexp, $arrayexp, $hashexp, $arrayarrayexp,
|
---|
| 92 | $hashhashexp) = @_;
|
---|
[4] | 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);
|
---|
[91] | 100 | if (defined $stringexp && $key =~ /$stringexp/) {
|
---|
[4] | 101 | $data->{$key} = shift (@$line);
|
---|
| 102 |
|
---|
[91] | 103 | } elsif (defined $arrayexp && $key =~ /$arrayexp/) {
|
---|
[4] | 104 | push (@{$data->{$key}}, @$line);
|
---|
[69] | 105 |
|
---|
[91] | 106 | } elsif (defined $hashexp && $key =~ /$hashexp/) {
|
---|
[69] | 107 | my $k = shift @$line;
|
---|
| 108 | my $v = shift @$line;
|
---|
| 109 | $data->{$key}->{$k} = $v;
|
---|
[813] | 110 | } elsif (defined $arrayarrayexp && $key =~ /$arrayarrayexp/) {
|
---|
| 111 | if (!defined $data->{$key}) {
|
---|
| 112 | $data->{$key} = [];
|
---|
| 113 | }
|
---|
| 114 | push (@{$data->{$key}}, $line);
|
---|
[4] | 115 | }
|
---|
[2772] | 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 | }
|
---|
[4] | 126 | }
|
---|
| 127 | }
|
---|
| 128 | close (COLCFG);
|
---|
[2772] | 129 |
|
---|
[4] | 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 |
|
---|
[813] | 138 | # stringexp, arrayexp, hashexp and arrayarrayexp
|
---|
| 139 | # should be something like '^(this|that)$'
|
---|
[4] | 140 | sub write_cfg_file {
|
---|
[2772] | 141 | my ($filename, $data, $stringexp, $arrayexp, $hashexp, $arrayarrayexp,
|
---|
| 142 | $hashhashexp) = @_;
|
---|
[813] | 143 |
|
---|
[4] | 144 | if (open (COLCFG, ">$filename")) {
|
---|
[15889] | 145 | foreach my $key (sort(keys(%$data))) {
|
---|
[4] | 146 | if ($key =~ /$stringexp/) {
|
---|
| 147 | &write_cfg_line ('COLCFG', [$key, $data->{$key}]);
|
---|
| 148 | } elsif ($key =~ /$arrayexp/) {
|
---|
| 149 | &write_cfg_line ('COLCFG', [$key, @{$data->{$key}}]);
|
---|
[69] | 150 | } elsif ($key =~ /$hashexp/) {
|
---|
[15889] | 151 | foreach my $k (keys (%{$data->{$key}})) {
|
---|
[69] | 152 | &write_cfg_line ('COLCFG', [$key, $k, $data->{$key}->{$k}]);
|
---|
| 153 | }
|
---|
[813] | 154 | } elsif ($key =~ /$arrayarrayexp/) {
|
---|
[15889] | 155 | foreach my $k (@{$data->{$key}}) {
|
---|
[813] | 156 | &write_cfg_line ('COLCFG', [$key, @$k]);
|
---|
| 157 | }
|
---|
[2772] | 158 | } elsif ($key =~ /$hashhashexp/) {
|
---|
[15889] | 159 | foreach my $k (keys (%{$data->{$key}})) {
|
---|
| 160 | foreach my $p (keys (%{$data->{$key}->{$k}})) {
|
---|
[2772] | 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 | }
|
---|
[4] | 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;
|
---|