| 1 | # BW::Config.pm | ||||||||||||||||||||
| 2 | # by Bill Weinman Support for config files | ||||||||||||||||||||
| 3 | # Based upon bwConfig by Bill Weinman | ||||||||||||||||||||
| 4 | # | ||||||||||||||||||||
| 5 | # Copyright (c) 1995-2010 The BearHeart Group, LLC | ||||||||||||||||||||
| 6 | # | ||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | package BW::Config; | ||||||||||||||||||||
| 9 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 1 | ▶ | use IO::File; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 1 | ▶ | use BW::Constants; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 1 | ▶ | use base qw( BW::Base ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 1 | our $VERSION = 1.0; | |||||||||||||||||||
| 17 | 0 | ▶ | sub version { $VERSION } | ||||||||||||||||||
|
Subroutine
version: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | sub _init | ||||||||||||||||||||
| 20 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
_init: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 21 | 0 | my $self = shift; | |||||||||||||||||||
| 22 | 0 | $self->SUPER::_init(@_); | |||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | # arrange the room | ||||||||||||||||||||
| 25 | 0 | $self->{raw_lines} = []; | |||||||||||||||||||
| 26 | 0 | $self->{keywords} = []; | |||||||||||||||||||
| 27 | 0 | $self->{values} = {}; | |||||||||||||||||||
| 28 | 0 | $self->{arrays} = {}; | |||||||||||||||||||
| 29 | 0 | $self->{me} = ref($self); | |||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | # shall we parse? | ||||||||||||||||||||
| 32 | 0 | ▶ | $self->parse() if $self->{filename}; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | 0 | return SUCCESS; | |||||||||||||||||||
| 35 | } | ||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | # _setter_getter entry points | ||||||||||||||||||||
| 38 | 0 | ▶ | sub filename { BW::Base::_setter_getter(@_); } | ||||||||||||||||||
|
Subroutine
filename: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | ### getters only | ||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | # get the keywords arrayref | ||||||||||||||||||||
| 43 | sub keywords | ||||||||||||||||||||
| 44 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
keywords: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 45 | 0 | my $self = shift; | |||||||||||||||||||
| 46 | 0 | ▶ | return $self->{keywords} || []; | ||||||||||||||||||
|
Condition0%0/2
$self->{'keywords'} || []
MC/DC0%0/1
$self->{'keywords'} || []
$self->{'keywords'}
Truth table0%0/2
A
A= $self->{'keywords'}
| |||||||||||||||||||||
| 47 | } | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | # get the values hash | ||||||||||||||||||||
| 50 | sub values | ||||||||||||||||||||
| 51 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
values: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 52 | 0 | my $self = shift; | |||||||||||||||||||
| 53 | 0 | ▶ | return $self->{values} || {}; | ||||||||||||||||||
|
Condition0%0/2
$self->{'values'} || {}
MC/DC0%0/1
$self->{'values'} || {}
$self->{'values'}
Truth table0%0/2
A
A= $self->{'values'}
| |||||||||||||||||||||
| 54 | } | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | # get the arrays hash | ||||||||||||||||||||
| 57 | sub arrays | ||||||||||||||||||||
| 58 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
arrays: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||
| 59 | 0 | my $self = shift; | |||||||||||||||||||
| 60 | 0 | ▶ | return $self->{arrays} || {}; | ||||||||||||||||||
|
Condition0%0/2
$self->{'arrays'} || {}
MC/DC0%0/1
$self->{'arrays'} || {}
$self->{'arrays'}
Truth table0%0/2
A
A= $self->{'arrays'}
| |||||||||||||||||||||
| 61 | } | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | ### worker bees | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | # parse the file | ||||||||||||||||||||
| 66 | sub parse | ||||||||||||||||||||
| 67 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
parse: not called CC=7 SCAR=40.3
Pod
undocumented
| |||||||||||||||||||||
| 68 | 0 | my $sn = 'parse'; | |||||||||||||||||||
| 69 | 0 | my $self = shift; | |||||||||||||||||||
| 70 | 0 | my $args = shift; | |||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | # find the filename | ||||||||||||||||||||
| 73 | 0 | ▶ | my $filename = $args->{filename} || $self->filename; | ||||||||||||||||||
|
Condition0%0/3
$args->{'filename'} || $self->filename
MC/DC0%0/2
$args->{'filename'} || $self->filename
$args->{'filename'}
$self->filename
Truth table0%0/3
A || B
A= $args->{'filename'}B= $self->filename
| |||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | 0 | ▶ | return $self->_error("$sn: No filename.") unless $filename; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | # set the filename property while opening the file | ||||||||||||||||||||
| 78 | 0 | ▶ | my $fh = new IO::File( $filename, 'r' ) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 79 | or return $self->_error("$sn: Cannot open config file ($filename): $!"); | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | 0 | ▶ | while (<$fh>) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 82 | 0 | s/#.*//; # trim end-of-line comments | |||||||||||||||||||
| 83 | 0 | $_ = $self->_trim($_); # trim surrounding whitespace | |||||||||||||||||||
| 84 | 0 | ▶ | next if /^$/; # skip semantically empty lines | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | 0 | push @{ $self->{raw_lines} }, $_; | |||||||||||||||||||
| 87 | 0 | my ( $lh, $rh ) = split( /=/, $_, 2 ); | |||||||||||||||||||
| 88 | 0 | $lh = $self->_trim($lh); # trim them up | |||||||||||||||||||
| 89 | 0 | $rh = $self->_trim($rh); | |||||||||||||||||||
| 90 | 0 | push @{ $self->{keywords} }, $lh; | |||||||||||||||||||
| 91 | 0 | ▶ | $rh = '' unless $rh; # avoid undef | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 92 | 0 | $self->{values}{$lh} = $rh; | |||||||||||||||||||
| 93 | 0 | @{ $self->{arrays}{$lh} } = split( /:/, $rh ); | |||||||||||||||||||
| 94 | } | ||||||||||||||||||||
| 95 | 0 | return TRUE; | |||||||||||||||||||
| 96 | } | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | # trim leading/trailing space from a string | ||||||||||||||||||||
| 99 | sub _trim | ||||||||||||||||||||
| 100 | 0 | ▶ | { | ||||||||||||||||||
|
Subroutine
_trim: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 101 | 0 | my $self = shift; | |||||||||||||||||||
| 102 | 0 | ▶ | my $string = shift or return ''; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 103 | |||||||||||||||||||||
| 104 | 0 | $string =~ /\s*(.*?)\s*$/; | |||||||||||||||||||
| 105 | 0 | return $1; | |||||||||||||||||||
| 106 | } | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 1 | 1; | |||||||||||||||||||
| 109 | |||||||||||||||||||||
| 110 | __END__ | ||||||||||||||||||||