source: trunk/gsdl/perllib/cfgread.pm@ 740

Last change on this file since 740 was 537, checked in by sjboddie, 25 years ago

added GPL headers

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 3.5 KB
Line 
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
28package cfgread;
29
30
31sub 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
67sub write_cfg_line {
68 my ($handle, $line) = @_;
69 print $handle join ("\t", @$line), "\n";
70}
71
72
73# stringexp and arrayexp should be something like
74# '^(this|that)$'
75sub read_cfg_file {
76 my ($filename, $stringexp, $arrayexp, $hashexp) = @_;
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);
84 if (defined $stringexp && $key =~ /$stringexp/) {
85 $data->{$key} = shift (@$line);
86
87 } elsif (defined $arrayexp && $key =~ /$arrayexp/) {
88 push (@{$data->{$key}}, @$line);
89
90 } elsif (defined $hashexp && $key =~ /$hashexp/) {
91 my $k = shift @$line;
92 my $v = shift @$line;
93 $data->{$key}->{$k} = $v;
94 }
95 }
96 }
97 close (COLCFG);
98 } else {
99 print STDERR "cfgread::read_cfg_file couldn't read the cfg file $filename\n";
100 }
101
102 return $data;
103}
104
105
106# stringexp and arrayexp should be something like
107# '^(this|that)$'
108sub write_cfg_file {
109 my ($filename, $data, $stringexp, $arrayexp, $hashexp) = @_;
110
111 if (open (COLCFG, ">$filename")) {
112 foreach $key (sort(keys(%$data))) {
113 if ($key =~ /$stringexp/) {
114 &write_cfg_line ('COLCFG', [$key, $data->{$key}]);
115 } elsif ($key =~ /$arrayexp/) {
116 &write_cfg_line ('COLCFG', [$key, @{$data->{$key}}]);
117 } elsif ($key =~ /$hashexp/) {
118 foreach $k (keys (%{$data->{$key}})) {
119 &write_cfg_line ('COLCFG', [$key, $k, $data->{$key}->{$k}]);
120 }
121 }
122 }
123 close (COLCFG);
124 } else {
125 print STDERR "cfgread::write_cfg_file couldn't write the cfg file $filename\n";
126 }
127}
128
129
1301;
Note: See TracBrowser for help on using the repository browser.