File Coverage

blib/lib/OpenCA/Configuration.pm
Criterion Covered Total %
statement 3 102 2.9
branch 0 30 0.0
condition 0 21 0.0
subroutine 1 9 11.1
pod 0 8 0.0
total 4 170 2.3


line stmt bran cond sub pod time code
1             ## OpenCA::Configuration.pm
2             ##
3             ## Copyright (C) 1998-1999 Massimiliano Pala (madwolf@openca.org)
4             ## All rights reserved.
5             ##
6             ## This library is free for commercial and non-commercial use as long as
7             ## the following conditions are aheared to. The following conditions
8             ## apply to all code found in this distribution, be it the RC4, RSA,
9             ## lhash, DES, etc., code; not just the SSL code. The documentation
10             ## included with this distribution is covered by the same copyright terms
11             ##
12             ## Copyright remains Massimiliano Pala's, and as such any Copyright notices
13             ## in the code are not to be removed.
14             ## If this package is used in a product, Massimiliano Pala should be given
15             ## attribution as the author of the parts of the library used.
16             ## This can be in the form of a textual message at program startup or
17             ## in documentation (online or textual) provided with the package.
18             ##
19             ## Redistribution and use in source and binary forms, with or without
20             ## modification, are permitted provided that the following conditions
21             ## are met:
22             ## 1. Redistributions of source code must retain the copyright
23             ## notice, this list of conditions and the following disclaimer.
24             ## 2. Redistributions in binary form must reproduce the above copyright
25             ## notice, this list of conditions and the following disclaimer in the
26             ## documentation and/or other materials provided with the distribution.
27             ## 3. All advertising materials mentioning features or use of this software
28             ## must display the following acknowledgement:
29             ## "This product includes OpenCA software written by Massimiliano Pala
30             ## (madwolf@openca.org) and the OpenCA Group (www.openca.org)"
31             ## 4. If you include any Windows specific code (or a derivative thereof) from
32             ## some directory (application code) you must include an acknowledgement:
33             ## "This product includes OpenCA software (www.openca.org)"
34             ##
35             ## THIS SOFTWARE IS PROVIDED BY OPENCA DEVELOPERS ``AS IS'' AND
36             ## ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37             ## IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38             ## ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
39             ## FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40             ## DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41             ## OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42             ## HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43             ## LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44             ## OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45             ## SUCH DAMAGE.
46             ##
47             ## The licence and distribution terms for any publically available version or
48             ## derivative of this code cannot be changed. i.e. this code cannot simply be
49             ## copied and put under another distribution licence
50             ## [including the GNU Public Licence.]
51             ##
52             ## Porpouse:
53             ## =========
54             ##
55             ## Get easily configuration parameters passed into a config file
56             ##
57             ## Status:
58             ## =======
59             ##
60             ## Started: 10/11/1998
61             ## Last Modified: 28/04/1999
62             ##
63              
64 1     1   593 use strict;
  1         1  
  1         1187  
65              
66             package OpenCA::Configuration;
67              
68             $OpenCA::Configuration::VERSION = '1.5.3';
69              
70             # Preloaded methods go here.
71              
72             ## Define Error Messages for the Configuration Manager Errors
73             my %params = {
74             cnfLines => undef,
75             cnfDB => undef,
76             };
77              
78             ## Create an instance of the Class
79             sub new {
80 0     0 0   my $that = shift;
81 0   0       my $class = ref($that) || $that;
82              
83 0           my $self = {
84             %params,
85             };
86              
87 0           bless $self, $class;
88              
89 0           my @keys = @_ ;
90 0           my $fileName = $keys[0];
91              
92 0 0 0       if( defined $fileName and $fileName ne "" ) {
93 0           my $ret = $self->loadCfg ( $fileName );
94 0 0         return undef if ( not $ret );
95             }
96              
97 0           return $self;
98             }
99              
100             ## Configuration Manager Functions
101             sub loadCfg {
102 0     0 0   my $self = shift;
103 0           my @keys = @_ ;
104              
105 0           my $temp;
106             my $temp2;
107 0           my @configLines;
108 0           my $sameLine;
109              
110 0           my $fileName = $keys[0];
111              
112 0           $sameLine = 0;
113 0           $temp2 = "";
114 0 0         open( FD, "$fileName" ) || return undef;
115 0           while( $temp = ) {
116 0 0         if( $temp =~ /\\\n$/ ) {
117 0           $temp =~ s/\\\n$//;
118 0           $temp2 .= $temp;
119 0           $sameLine = 1;
120             } else {
121 0 0         if( $sameLine == 1 ) {
122 0           $temp2 .= $temp;
123 0           $temp = $temp2;
124 0           $sameLine = 0;
125 0           $temp2 = "";
126             }
127              
128 0           $sameLine = 0;
129 0           push ( @configLines, $temp);
130             }
131             }
132 0           close(FD);
133              
134 0 0         if( $self->parsecfg( @configLines ) ) {
135 0           $self->{cnfLines} = [ @configLines ];
136 0           return 1;
137             } else {
138 0           return 0;
139             }
140             }
141              
142             ## Parsing Function
143             sub parsecfg {
144 0     0 0   my $self = shift;
145 0           my @keys = @_;
146              
147 0           my @configDB = ();
148 0           my $num = -1;
149 0           my $line;
150            
151 0           foreach $line (@keys) {
152 0           my $paramName;
153             my %par;
154 0           my @values;
155              
156             ## Take count of Config Line Number
157 0           $num++;
158              
159             ## Trial line and discard Comments
160 0           chop($line);
161             ## next if ($line =~ /\#.*/)||($line eq "")||($line =~ /HASH.*/);
162 0 0 0       next if ($line =~ /\#.*/)||($line =~ /^\s*[\n\r]*$/)||($line =~ /HASH.*/);
      0        
163 0           $line =~ s/#.*//;
164 0           $line =~ s/^[\s]*//;
165 0           $line =~ s/(\r|\n)//g;
166              
167             ## Get the Parameter Name
168 0           ( $paramName ) =
169             ( $line =~ /([\S]+).*/ );
170              
171             ## Erase the parameter Name from the Line
172 0           $line =~ s/$paramName// ;
173              
174 0           @values = ();
175              
176             ## Start displacing command
177 0           while ( length($line) > 0 ) {
178 0           my ( $param, $match );
179              
180             ## Delete remaining Spaces
181 0           $line =~ s/^[\s]*//;
182              
183 0 0         if ( $line =~ /^\"/ ) {
184 0           ( $param ) = ( $line =~ /^\"(.*?)\"/ );
185 0           $line =~ s/^\".*?\"//;
186             } else {
187 0           ( $param ) = ( $line =~ /^([\S]+)/ );
188 0           $line =~ s/^([\S]+)//;
189             };
190              
191 0           @values = ( @values, $param );
192            
193             }
194              
195             ## Get the parameter set up
196 0           my $par = { NAME=>$paramName,
197             LINE_NUMBER=>$num,
198             VALUES=>[ @values ] };
199              
200 0           push ( @configDB, $par);
201             }
202              
203 0           $self->{cnfDB} = [ @configDB ];
204 0           return 1;
205             }
206              
207             ## Get Single Parameter
208             sub getParam {
209 0     0 0   my $self = shift;
210 0           my @keys = @_;
211              
212 0           return $self->getNextParam( NAME=>$keys[0],
213             LINE_NUMBER=>-1 );
214             };
215              
216             ## Get next Parameter
217             sub getNextParam {
218 0     0 0   my $self = shift;
219 0           my $k = { @_ };
220 0           my $par;
221              
222 0 0         return if( not ( $k->{NAME} ) );
223              
224 0           foreach $par ( @{$self->{cnfDB}} ) {
  0            
225 0           my $tmp = $par->{NAME};
226              
227 0 0 0       if( (lc( $tmp ) eq lc($k->{NAME})) and
228             ( $par->{LINE_NUMBER} > $k->{LINE_NUMBER}) ) {
229 0           return $par;
230             };
231             };
232              
233 0           return undef;
234             }
235              
236             sub checkParam {
237 0     0 0   my $self = shift;
238 0           my $k = { @_ };
239 0           my ( $par, $pnum );
240              
241 0 0         return unless ( exists $k->{NAME} );
242              
243 0           $par = $self->getParam( $k->{NAME} );
244 0 0         return unless ( not ( keys %$par ));
245              
246             ## $pnum = $#($par->{VALUES});
247              
248 0 0 0       if( ($k->{MIN}) && ($pnum < $k->{MIN}) ) {
249 0           return $par->{LINE_NUMBER};
250             }
251              
252 0 0 0       if( ($k->{MAX}) && ($pnum > $k->{MAX}) ) {
253 0           return $par->{LINE_NUMBER};
254             }
255              
256 0           return 0;
257             }
258              
259             sub checkConfig {
260 0     0 0   my $self = shift;
261 0           my @keys = @_;
262 0           my ( $ret, $par );
263              
264 0           foreach $par ( @keys ) {
265 0           $ret = $self->ceckParam( $par );
266 0 0         return if ( not $ret);
267             }
268              
269 0           return 0;
270             }
271              
272             sub getVersion {
273 0     0 0   my $self = shift;
274              
275 0           return $OpenCA::Configuration::VERSION;
276             }
277              
278             1;