X-Git-Url: http://git.freeside.biz/gitweb/?p=freeside.git;a=blobdiff_plain;f=site_perl%2FConf.pm;fp=site_perl%2FConf.pm;h=d3ef307c0d7c48eeb72b16561f6a8a24a92e1a14;hp=0000000000000000000000000000000000000000;hb=cad4eadf964cb65841d7cb6f0bcf804f1d39ae2c;hpb=3175087f33550fd68e8a4dcf2e33c45f41034eaa diff --git a/site_perl/Conf.pm b/site_perl/Conf.pm new file mode 100644 index 000000000..d3ef307c0 --- /dev/null +++ b/site_perl/Conf.pm @@ -0,0 +1,113 @@ +package FS::Conf; + +use vars qw($default_dir); +use IO::File; + +$default_dir='/var/spool/freeside/conf'; + +=head1 NAME + +FS::Conf - Read access to Freeside configuration values + +=head1 SYNOPSIS + + use FS::Conf; + + $conf = new FS::Conf; + $conf = new FS::Conf "/non/standard/config/directory"; + + $dir = $conf->dir; + + $value = $conf->config('key'); + @list = $conf->config('key'); + $bool = $conf->exists('key'); + +=head1 DESCRIPTION + +Read access to Freeside configuration values. Keys currently map to filenames, +but this may change in the future. + +=head1 METHODS + +=over 4 + +=item new [ DIRECTORY ] + +Create a new configuration object. Optionally, a non-default directory may +be specified. + +=cut + +sub new { + my($proto,$dir) = @_; + my($class) = ref($proto) || $proto; + my($self) = { 'dir' => $dir || $default_dir } ; + bless ($self, $class); +} + +=item dir + +Returns the directory. + +=cut + +sub dir { + my($self) = @_; + $self->{dir}; +} + +=item config + +Returns the configuration value or values (depending on context) for key. + +=cut + +sub config { + my($self,$file)=@_; + my($dir)=$self->dir; + my $fh = new IO::File "<$dir/$file" or return; + if ( wantarray ) { + map { + /^(.*)$/ or die "Illegal line in $dir/$file:\n$_\n"; + $1; + } <$fh>; + } else { + <$fh> =~ /^(.*)$/ or die "Illegal line in $dir/$file:\n$_\n"; + $1; + } +} + +=item exists + +Returns true if the specified key exists, even if the corresponding value +is undefined. + +=cut + +sub exists { + my($self,$file)=@_; + my($dir) = $self->dir; + -e "$dir/$file"; +} + +=back + +=head1 BUGS + +The option to specify a non-default directory should probably be removed. + +Write access (with locking) should be implemented. + +=head1 SEE ALSO + +config.html from the base documentation contains a list of configuration files. + +=head1 HISTORY + +Ivan Kohler 98-sep-6 + +sub exists forgot to fetch $dir ivan@sisd.com 98-sep-27 + +=cut + +1;