[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.
|
---|
[16929] | 44 | if ($line =~ m/^([\"\'])/ || $line =~ m/[^\\]([\"\'])/) {
|
---|
[3507] | 45 | my $quote=$1;
|
---|
[20390] | 46 |
|
---|
| 47 | # Improve speed substantially by not doing the regular expression on $line in the while loop
|
---|
| 48 | # (since $line gets longer each iteration, the regular expression gets slower and slower)
|
---|
| 49 | # Instead we just check each new line to see if it finishes the quoted multi-line value
|
---|
| 50 | if ($line !~ m/$quote(.*?[^\\])?(\\\\)*$quote/)
|
---|
| 51 | {
|
---|
| 52 | my $nextline;
|
---|
| 53 | while (defined($nextline = <$handle>))
|
---|
| 54 | {
|
---|
[3418] | 55 | $nextline =~ s/\r?\n//; # remove end-of-line
|
---|
| 56 | $line .= " " . $nextline;
|
---|
[20390] | 57 |
|
---|
| 58 | # Break out of the while loop if we've found the end of the multi-line value
|
---|
| 59 | last if ($nextline =~ m/^(.*?[^\\])?(\\\\)*$quote/);
|
---|
[3418] | 60 | }
|
---|
| 61 | }
|
---|
| 62 | }
|
---|
[132] | 63 | $linecontinues = $line =~ s/\\$//;
|
---|
[3418] | 64 |
|
---|
[7692] | 65 | while ($line =~ s/\s*(\".*?[^\\](\\\\)*\"|\'.*?[^\\](\\\\)*\'|\S+)\s*//) {
|
---|
[132] | 66 | if (defined $1) {
|
---|
| 67 | # remove any enclosing quotes
|
---|
| 68 | my $entry = $1;
|
---|
| 69 | $entry =~ s/^([\"\'])(.*)\1$/$2/;
|
---|
| 70 |
|
---|
| 71 | # substitute an environment variables
|
---|
[8890] | 72 | ## $entry =~ s/\$(\w+)/$ENV{$1}/g;
|
---|
[132] | 73 | $entry =~ s/\$\{(\w+)\}/$ENV{$1}/g;
|
---|
| 74 | push (@line, $entry);
|
---|
| 75 | } else {
|
---|
| 76 | push (@line, "");
|
---|
| 77 | }
|
---|
| 78 | }
|
---|
| 79 |
|
---|
| 80 | if (scalar(@line) > 0 && !$linecontinues) {
|
---|
| 81 | # print STDERR "line: \"" . join ("\" \"", @line) . "\"\n";
|
---|
[4] | 82 | return \@line;
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
[132] | 85 |
|
---|
[4] | 86 | return undef;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | sub write_cfg_line {
|
---|
| 90 | my ($handle, $line) = @_;
|
---|
| 91 | print $handle join ("\t", @$line), "\n";
|
---|
| 92 | }
|
---|
| 93 |
|
---|
| 94 |
|
---|
[2772] | 95 | # stringexp, arrayexp, hashexp,arrayarrayexp and hashhashexp
|
---|
[813] | 96 | # should be something like '^(this|that)$'
|
---|
[4] | 97 | sub read_cfg_file {
|
---|
[2772] | 98 | my ($filename, $stringexp, $arrayexp, $hashexp, $arrayarrayexp,
|
---|
| 99 | $hashhashexp) = @_;
|
---|
[4] | 100 | my ($line);
|
---|
| 101 | my $data = {};
|
---|
| 102 |
|
---|
| 103 | if (open (COLCFG, $filename)) {
|
---|
| 104 | while (defined ($line = &read_cfg_line('COLCFG'))) {
|
---|
| 105 | if (scalar(@$line) >= 2) {
|
---|
| 106 | my $key = shift (@$line);
|
---|
[91] | 107 | if (defined $stringexp && $key =~ /$stringexp/) {
|
---|
[4] | 108 | $data->{$key} = shift (@$line);
|
---|
| 109 |
|
---|
[91] | 110 | } elsif (defined $arrayexp && $key =~ /$arrayexp/) {
|
---|
[4] | 111 | push (@{$data->{$key}}, @$line);
|
---|
[69] | 112 |
|
---|
[91] | 113 | } elsif (defined $hashexp && $key =~ /$hashexp/) {
|
---|
[69] | 114 | my $k = shift @$line;
|
---|
| 115 | my $v = shift @$line;
|
---|
| 116 | $data->{$key}->{$k} = $v;
|
---|
[813] | 117 | } elsif (defined $arrayarrayexp && $key =~ /$arrayarrayexp/) {
|
---|
| 118 | if (!defined $data->{$key}) {
|
---|
| 119 | $data->{$key} = [];
|
---|
| 120 | }
|
---|
| 121 | push (@{$data->{$key}}, $line);
|
---|
[4] | 122 | }
|
---|
[2772] | 123 | elsif (defined $hashhashexp && $key =~ /$hashhashexp/) {
|
---|
| 124 | my $k = shift @$line;
|
---|
| 125 | my $p = shift @$line;
|
---|
| 126 | my $v = shift @$line;
|
---|
| 127 | if (!defined $v) {
|
---|
| 128 | $v = $p;
|
---|
| 129 | $p = 'default';
|
---|
| 130 | }
|
---|
| 131 | $data->{$key}->{$k}->{$p} = $v;
|
---|
| 132 | }
|
---|
[4] | 133 | }
|
---|
| 134 | }
|
---|
| 135 | close (COLCFG);
|
---|
[2772] | 136 |
|
---|
[4] | 137 | } else {
|
---|
| 138 | print STDERR "cfgread::read_cfg_file couldn't read the cfg file $filename\n";
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | return $data;
|
---|
| 142 | }
|
---|
| 143 |
|
---|
[24586] | 144 | # If the cfg file contains unicode characters, use this method to read from it
|
---|
| 145 | # Used by HFileHierarchy classifier, since an HFile is read as a cfg file, but
|
---|
| 146 | # can contain unicode characters.
|
---|
| 147 | sub read_cfg_file_unicode {
|
---|
| 148 | my ($filename, $stringexp, $arrayexp, $hashexp, $arrayarrayexp,
|
---|
| 149 | $hashhashexp) = @_;
|
---|
| 150 | my ($line);
|
---|
| 151 | my $data = {};
|
---|
[4] | 152 |
|
---|
[24586] | 153 | if (open (COLCFG, $filename)) {
|
---|
| 154 | binmode(COLCFG,":utf8");
|
---|
| 155 | while (defined ($line = &read_cfg_line('COLCFG'))) {
|
---|
| 156 | if (scalar(@$line) >= 2) {
|
---|
| 157 |
|
---|
| 158 | #map { decode("utf8",$_) } @$line; #use Encode;
|
---|
| 159 |
|
---|
| 160 | my $key = shift (@$line);
|
---|
[31396] | 161 |
|
---|
| 162 | # OIDtype and OIDmetadata may be present in collect.cfg as "oidtype" and "oidmetadata"
|
---|
| 163 | # but rest of perl code expects it to be OIDtype/OIDmetadata and uses these as indexes into hashes
|
---|
| 164 | # so convert any lowercase version to uppercase here
|
---|
| 165 | if($key =~ m/^oid(type|metadata)/) {
|
---|
| 166 | $key =~ s/^oid/OID/;
|
---|
| 167 | }
|
---|
[24586] | 168 | if (defined $stringexp && $key =~ /$stringexp/) {
|
---|
| 169 | $data->{$key} = shift (@$line);
|
---|
| 170 |
|
---|
| 171 | } elsif (defined $arrayexp && $key =~ /$arrayexp/) {
|
---|
| 172 | push (@{$data->{$key}}, @$line);
|
---|
| 173 |
|
---|
| 174 | } elsif (defined $hashexp && $key =~ /$hashexp/) {
|
---|
| 175 | my $k = shift @$line;
|
---|
| 176 | my $v = shift @$line;
|
---|
| 177 | $data->{$key}->{$k} = $v;
|
---|
| 178 | } elsif (defined $arrayarrayexp && $key =~ /$arrayarrayexp/) {
|
---|
| 179 | if (!defined $data->{$key}) {
|
---|
| 180 | $data->{$key} = [];
|
---|
| 181 | }
|
---|
| 182 | push (@{$data->{$key}}, $line);
|
---|
| 183 | }
|
---|
| 184 | elsif (defined $hashhashexp && $key =~ /$hashhashexp/) {
|
---|
| 185 | my $k = shift @$line;
|
---|
| 186 | my $p = shift @$line;
|
---|
| 187 | my $v = shift @$line;
|
---|
| 188 | if (!defined $v) {
|
---|
| 189 | $v = $p;
|
---|
| 190 | $p = 'default';
|
---|
| 191 | }
|
---|
| 192 | $data->{$key}->{$k}->{$p} = $v;
|
---|
| 193 | }
|
---|
| 194 | }
|
---|
| 195 | }
|
---|
| 196 | close (COLCFG);
|
---|
| 197 |
|
---|
| 198 | } else {
|
---|
| 199 | print STDERR "cfgread::read_cfg_file_unicode couldn't read the cfg file $filename\n";
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | return $data;
|
---|
| 203 | }
|
---|
| 204 |
|
---|
| 205 |
|
---|
[813] | 206 | # stringexp, arrayexp, hashexp and arrayarrayexp
|
---|
| 207 | # should be something like '^(this|that)$'
|
---|
[4] | 208 | sub write_cfg_file {
|
---|
[2772] | 209 | my ($filename, $data, $stringexp, $arrayexp, $hashexp, $arrayarrayexp,
|
---|
| 210 | $hashhashexp) = @_;
|
---|
[18441] | 211 |
|
---|
[4] | 212 | if (open (COLCFG, ">$filename")) {
|
---|
[15889] | 213 | foreach my $key (sort(keys(%$data))) {
|
---|
[4] | 214 | if ($key =~ /$stringexp/) {
|
---|
| 215 | &write_cfg_line ('COLCFG', [$key, $data->{$key}]);
|
---|
| 216 | } elsif ($key =~ /$arrayexp/) {
|
---|
| 217 | &write_cfg_line ('COLCFG', [$key, @{$data->{$key}}]);
|
---|
[26449] | 218 | } elsif (defined $hashexp && $key =~ /$hashexp/) {
|
---|
[15889] | 219 | foreach my $k (keys (%{$data->{$key}})) {
|
---|
[69] | 220 | &write_cfg_line ('COLCFG', [$key, $k, $data->{$key}->{$k}]);
|
---|
| 221 | }
|
---|
[26449] | 222 | } elsif (defined $arrayarrayexp && $key =~ /$arrayarrayexp/) {
|
---|
[15889] | 223 | foreach my $k (@{$data->{$key}}) {
|
---|
[813] | 224 | &write_cfg_line ('COLCFG', [$key, @$k]);
|
---|
| 225 | }
|
---|
[26449] | 226 | } elsif (defined $hashhashexp && $key =~ /$hashhashexp/) {
|
---|
[15889] | 227 | foreach my $k (keys (%{$data->{$key}})) {
|
---|
| 228 | foreach my $p (keys (%{$data->{$key}->{$k}})) {
|
---|
[2772] | 229 | if ($p =~ /default/) {
|
---|
| 230 | &write_cfg_line ('COLCFG',
|
---|
| 231 | [$key, $k, $data->{$key}->{$k}]);
|
---|
| 232 | }
|
---|
| 233 | else {
|
---|
| 234 | &write_cfg_line ('COLCFG',
|
---|
| 235 | [$key, $k, $p, $data->{$key}->{$k}->{$p}]);
|
---|
| 236 | }
|
---|
| 237 | }
|
---|
| 238 | }
|
---|
[4] | 239 | }
|
---|
| 240 | }
|
---|
| 241 | close (COLCFG);
|
---|
| 242 | } else {
|
---|
| 243 | print STDERR "cfgread::write_cfg_file couldn't write the cfg file $filename\n";
|
---|
| 244 | }
|
---|
| 245 | }
|
---|
| 246 |
|
---|
| 247 |
|
---|
| 248 | 1;
|
---|