[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
|
---|
| 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";
|
---|
[4] | 60 | return \@line;
|
---|
| 61 | }
|
---|
| 62 | }
|
---|
[132] | 63 |
|
---|
[4] | 64 | return undef;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | sub write_cfg_line {
|
---|
| 68 | my ($handle, $line) = @_;
|
---|
| 69 | print $handle join ("\t", @$line), "\n";
|
---|
| 70 | }
|
---|
| 71 |
|
---|
| 72 |
|
---|
[813] | 73 | # stringexp, arrayexp, hashexp and arrayarrayexp
|
---|
| 74 | # should be something like '^(this|that)$'
|
---|
[4] | 75 | sub read_cfg_file {
|
---|
[813] | 76 | my ($filename, $stringexp, $arrayexp, $hashexp, $arrayarrayexp) = @_;
|
---|
[4] | 77 | my ($line);
|
---|
| 78 | my $data = {};
|
---|
| 79 |
|
---|
| 80 | if (open (COLCFG, $filename)) {
|
---|
| 81 | while (defined ($line = &read_cfg_line('COLCFG'))) {
|
---|
| 82 | if (scalar(@$line) >= 2) {
|
---|
| 83 | my $key = shift (@$line);
|
---|
[91] | 84 | if (defined $stringexp && $key =~ /$stringexp/) {
|
---|
[4] | 85 | $data->{$key} = shift (@$line);
|
---|
| 86 |
|
---|
[91] | 87 | } elsif (defined $arrayexp && $key =~ /$arrayexp/) {
|
---|
[4] | 88 | push (@{$data->{$key}}, @$line);
|
---|
[69] | 89 |
|
---|
[91] | 90 | } elsif (defined $hashexp && $key =~ /$hashexp/) {
|
---|
[69] | 91 | my $k = shift @$line;
|
---|
| 92 | my $v = shift @$line;
|
---|
| 93 | $data->{$key}->{$k} = $v;
|
---|
[813] | 94 | } elsif (defined $arrayarrayexp && $key =~ /$arrayarrayexp/) {
|
---|
| 95 | if (!defined $data->{$key}) {
|
---|
| 96 | $data->{$key} = [];
|
---|
| 97 | }
|
---|
| 98 | push (@{$data->{$key}}, $line);
|
---|
[4] | 99 | }
|
---|
| 100 | }
|
---|
| 101 | }
|
---|
| 102 | close (COLCFG);
|
---|
| 103 | } else {
|
---|
| 104 | print STDERR "cfgread::read_cfg_file couldn't read the cfg file $filename\n";
|
---|
| 105 | }
|
---|
| 106 |
|
---|
| 107 | return $data;
|
---|
| 108 | }
|
---|
| 109 |
|
---|
| 110 |
|
---|
[813] | 111 | # stringexp, arrayexp, hashexp and arrayarrayexp
|
---|
| 112 | # should be something like '^(this|that)$'
|
---|
[4] | 113 | sub write_cfg_file {
|
---|
[813] | 114 | my ($filename, $data, $stringexp, $arrayexp, $hashexp, $arrayarrayexp) = @_;
|
---|
| 115 |
|
---|
[4] | 116 | if (open (COLCFG, ">$filename")) {
|
---|
| 117 | foreach $key (sort(keys(%$data))) {
|
---|
| 118 | if ($key =~ /$stringexp/) {
|
---|
| 119 | &write_cfg_line ('COLCFG', [$key, $data->{$key}]);
|
---|
| 120 | } elsif ($key =~ /$arrayexp/) {
|
---|
| 121 | &write_cfg_line ('COLCFG', [$key, @{$data->{$key}}]);
|
---|
[69] | 122 | } elsif ($key =~ /$hashexp/) {
|
---|
| 123 | foreach $k (keys (%{$data->{$key}})) {
|
---|
| 124 | &write_cfg_line ('COLCFG', [$key, $k, $data->{$key}->{$k}]);
|
---|
| 125 | }
|
---|
[813] | 126 | } elsif ($key =~ /$arrayarrayexp/) {
|
---|
| 127 | foreach $k (@{$data->{$key}}) {
|
---|
| 128 | &write_cfg_line ('COLCFG', [$key, @$k]);
|
---|
| 129 | }
|
---|
[4] | 130 | }
|
---|
| 131 | }
|
---|
| 132 | close (COLCFG);
|
---|
| 133 | } else {
|
---|
| 134 | print STDERR "cfgread::write_cfg_file couldn't write the cfg file $filename\n";
|
---|
| 135 | }
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 |
|
---|
| 139 | 1;
|
---|