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

Last change on this file since 17 was 4, checked in by sjboddie, 26 years ago

Initial revision

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 1.6 KB
Line 
1# reads in configuration files
2
3package cfgread;
4
5
6sub read_cfg_line {
7 my ($handle) = @_;
8 my $line = "";
9
10 while (defined($line = <$handle>)) {
11 $line =~ s/^\#.*$//; # remove comments
12 $line =~ s/\cM|\cJ//g; # remove end-of-line characters
13 if ($line =~ /\w/) {
14 my @line = split(/\t/, $line);
15 return \@line;
16 }
17 }
18 return undef;
19}
20
21sub write_cfg_line {
22 my ($handle, $line) = @_;
23 print $handle join ("\t", @$line), "\n";
24}
25
26
27# stringexp and arrayexp should be something like
28# '^(this|that)$'
29sub read_cfg_file {
30 my ($filename, $stringexp, $arrayexp) = @_;
31 my ($line);
32 my $data = {};
33
34 if (open (COLCFG, $filename)) {
35 while (defined ($line = &read_cfg_line('COLCFG'))) {
36 if (scalar(@$line) >= 2) {
37 my $key = shift (@$line);
38 if ($key =~ /$stringexp/) {
39 $data->{$key} = shift (@$line);
40
41 } elsif ($key =~ /$arrayexp/) {
42 push (@{$data->{$key}}, @$line);
43 }
44 }
45 }
46 close (COLCFG);
47 } else {
48 print STDERR "cfgread::read_cfg_file couldn't read the cfg file $filename\n";
49 }
50
51 return $data;
52}
53
54
55# stringexp and arrayexp should be something like
56# '^(this|that)$'
57sub write_cfg_file {
58 my ($filename, $data, $stringexp, $arrayexp) = @_;
59
60 if (open (COLCFG, ">$filename")) {
61 foreach $key (sort(keys(%$data))) {
62 if ($key =~ /$stringexp/) {
63 &write_cfg_line ('COLCFG', [$key, $data->{$key}]);
64 } elsif ($key =~ /$arrayexp/) {
65 &write_cfg_line ('COLCFG', [$key, @{$data->{$key}}]);
66 }
67 }
68 close (COLCFG);
69 } else {
70 print STDERR "cfgread::write_cfg_file couldn't write the cfg file $filename\n";
71 }
72}
73
74
751;
Note: See TracBrowser for help on using the repository browser.