VBTK/AdminLog.pm untested

filter: Statementstmt 0.0% 0 / 78 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 123 CC 7 SCAR 21.4
CC
7
Cov
0%
getEntryList
20.0
delEntry
12.0
new
6.0
CRAP 8.5
1 #! /bin/perl
2 #############################################################################
3 #
4 # NOTE: This file under revision control using RCS
5 # Any changes made without RCS will be lost
6 #
7 # $Source: /usr/local/cvsroot/vbtk/VBTK/AdminLog.pm,v $
8 # $Revision: 1.5 $
9 # $Date: 2002/03/04 20:53:06 $
10 # $Author: bhenry $
11 # $Locker: $
12 # $State: Exp $
13 #
14 # Purpose: Methods to handle maintenance of an administration
15 # log file.
16 #
17 # Copyright (C) 1996 - 2002 Brent Henry
18 #
19 # This program is free software; you can redistribute it and/or
20 # modify it under the terms of version 2 of the GNU General Public
21 # License as published by the Free Software Foundation available at:
22 # http://www.gnu.org/copyleft/gpl.html
23 #
24 # This program is distributed in the hope that it will be useful,
25 # but WITHOUT ANY WARRANTY; without even the implied warranty of
26 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 # GNU General Public License for more details.
28 #
29 #############################################################################
30 #
31 #
32 # REVISION HISTORY:
33 #
34 # $Log: AdminLog.pm,v $
35 # Revision 1.5 2002/03/04 20:53:06 bhenry
36 # *** empty log message ***
37 #
38 # Revision 1.4 2002/03/04 16:49:08 bhenry
39 # Changed requirement back to perl 5.6.0
40 #
41 # Revision 1.3 2002/03/02 00:53:54 bhenry
42 # Documentation updates
43 #
44 # Revision 1.2 2002/01/28 18:13:19 bhenry
45 # *** empty log message ***
46 #
47 # Revision 1.1 2002/01/23 20:32:43 bhenry
48 # *** empty log message ***
49 #
50 #
51  
52 package VBTK::AdminLog;
53  
54 use 5.6.0;
55 use strict;
56 use warnings;
57 # I like using undef as a value so I'm turning off the uninitialized warnings
58 no warnings qw(uninitialized);
59  
60 use VBTK::Common;
61 use Storable qw(store retrieve);
62  
63 our $VERBOSE=$ENV{VERBOSE};
64  
65 #-------------------------------------------------------------------------------
66 # Function: new
67 # Description: Object constructor. Allocates memory for all class members
68 # Input Parms: Configuration filename
69 # Output Parms: Pointer to class
70 #-------------------------------------------------------------------------------
71 sub new
72 {
73 my $type = shift;
74 my $fileName = shift;
75  
76 my $self = retrieve($fileName) if (-f $fileName);
77 $self ||= {};
78 bless $self, $type;
79  
80 $self->{fileName} = $fileName;
81  
82 # If nothing was loaded, then just initialize it to an empty array
83 $self->{entryList} ||= [];
84  
85 $self;
86 }
87  
88 #-------------------------------------------------------------------------------
89 # Function: write
90 # Description: Write the admin log to the specified file
91 # Input Parms: None
92 # Output Parms: Return value
93 #-------------------------------------------------------------------------------
94 sub write
95 {
96 my $self = shift;
97 my $fileName = $self->{fileName};
98
99 $self->store($fileName) || return undef;
100
101 1;
102 }
103  
104 #-------------------------------------------------------------------------------
105 # Function: addEntry
106 # Description: Add an entry to the adminLog and write it out to the file
107 # Input Parms: None
108 # Output Parms: Return Value
109 #-------------------------------------------------------------------------------
110 sub addEntry
111 {
112 my $self = shift;
113 my $fileName = $self->{fileName};
114 my $entryList = $self->{entryList};
115
116 my $entry = [ @_ ];
117 push(@{$entryList},$entry);
118
119 $self->write;
120 }
121  
122 #-------------------------------------------------------------------------------
123 # Function: delEntry
124 # Description: Delete the specified entry number
125 # Input Parms: None
126 # Output Parms: Return Value
127 #-------------------------------------------------------------------------------
128 sub delEntry
129 {
130 my $self = shift;
131 my $fileName = $self->{fileName};
132 my $entryList = $self->{entryList};
133 my @delPos = sort @_;
134 my $delCount = 0;
135
136 return undef if (@delPos < 1);
137
138 foreach my $pos (@delPos)
139 {
140 $pos -= $delCount;
141 next if (($pos < 0)||($pos >= @{$entryList}));
142 splice(@{$entryList},$pos,1);
143 $delCount++;
144 }
145
146 $self->write;
147 }
148  
149 #-------------------------------------------------------------------------------
150 # Function: getEntryList
151 # Description: Retrieve the list of log entries sorted by the specified column.
152 # Input Parms: None
153 # Output Parms: Return Value
154 #-------------------------------------------------------------------------------
155 sub getEntryList
156 {
157 my $self = shift;
158 my $sortCol = shift || 0;
159 my $entryList = $self->{entryList};
160 my (@posMappedList,$x,$reverse,@retList);
161
162 # If a negative number is specified, then reverse the sort order
163 if ($sortCol =~ /^-/)
164 {
165 $reverse = 1;
166 $sortCol = abs($sortCol);
167 }
168
169 # Add a 'position' column onto the beginning of all the data sets
170 for($x = 0; $x < @{$entryList}; $x++)
171 {
172 push(@posMappedList, [ $x, @{$entryList->[$x]} ]);
173 }
174
175 # If there are any non-numeric entries in the column we're trying to sort,
176 # then use an alpha-numeric sort. Otherwise use a numeric sort
177 if(grep(/[^\d]/, map { $_->[$sortCol] } @posMappedList))
178 {
179 @retList = sort { $a->[$sortCol] cmp $b->[$sortCol]; } @posMappedList;
180 }
181 else
182 {
183 @retList = sort { $a->[$sortCol] <=> $b->[$sortCol]; } @posMappedList;
184 }
185
186 if($reverse) { reverse @retList; }
187 else { @retList; }
188 }
189  
190  
191 1;
192 __END__