forked from chip-moore/sc-audit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclean68.pl
More file actions
206 lines (170 loc) · 6.14 KB
/
clean68.pl
File metadata and controls
206 lines (170 loc) · 6.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# perl code
# clean68.pl
# Copyright (c) 2011 Chip Moore
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to the
# following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software. THE SOFTWARE IS
# PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
# a script for rewriting an el68.lst ES&S audit file to make it easier to
# parse. The el68.lst file contains the details of manual adjustments to
# the vote tally.
# BEWARE: problems are created by odd chars in file, such as
# Morgan Bruce Reeves (0x0a)
# 63MANUAL (0x0d 0x0c)
use strict;
require utl;
require rgex;
# Use base class Exporter in order to export method "help" so that these can
# be called and passed around in objects including this package without need
# of namespace qualifier.
use base 'Exporter';
our @EXPORT = ( 'help' );
my $args = utl::parseCommandLine( \@ARGV, 4, "-b_-i_-o_-t", \&help );
my $in = $args->{ "-i" };
my $out = $args->{ "-o" };
my $bad = $args->{ "-b" };
my $trns = $args->{ "-t" };
# open input and output files
open( IN, "< $in" ) || die "Unable to open $in\n";
open( OUT, "> $out" ) || die "Unable to create $out\n";
# translation table
my %name2name;
# Build translation table if fixing bad names in file.
if ( $trns )
{
open( TRNS, "< $trns" ) || die "Unable to open $trns\n";
utl::getTranslations( \%name2name, \*TRNS );
}
# string of bad characters to remove
my $bc;
if ( $bad )
{
open( BAD, "< $bad" ) || die "Unable to open $bad\n";
$bc = <BAD>;
chomp $bc;
$bc = "[" . $bc . "]";
}
else
{
$bc = ' ';
}
my $precinctNumber;
my $precinctName;
my $candidate;
while ( <IN> )
{
if ( $trns )
{
# remove commas from numbers because they screw-up parsing
$_ =~ s/,//g;
# replace bad characters with a space character.
$_ =~ s/$bc/ /g;
# precinct name and candidate name
if ( /^($rgex::prc)\s($rgex::str)\s\s\s+?($rgex::str)\s\s\s+($rgex::date)\s($rgex::time)\s($rgex::ampm)\s\s(\d{0,1})\s\s+(\d{1,5})\s+(\d{1,5})/ )
{
$precinctNumber = $1;
$precinctName = ( defined( $name2name{ $2 } ) ? $name2name{ $2 } : $2 );
$candidate = ( defined( $name2name{ $3 } ) ? $name2name{ $3 } : $3 );
printf OUT "%4s %-35s%-45s%5s %5s %2s %2s %6d %8d\n", $precinctNumber,
$precinctName, $candidate, $4, $5, $6, $7, $8, $9;
}
# precinct name
elsif ( /^($rgex::prc)\s($rgex::str)\s\s\s+?($rgex::date)\s($rgex::time)\s($rgex::ampm)\s\s(\d{0,1})\s\s+(\d{1,5})\s+(\d{1,5})/ )
{
$precinctNumber = $1;
$precinctName = ( defined( $name2name{ $2 } ) ? $name2name{ $2 } : $2 );
printf OUT "%4s %-35s%-45s%5s %5s %2s %2s %6d %8d\n", $precinctNumber,
$precinctName, $candidate, $3, $4, $5, $6, $7, $8;
}
# candidate name
elsif ( /^\s\s\s+?($rgex::str)\s\s\s+($rgex::date)\s($rgex::time)\s($rgex::ampm)\s\s(\d{0,1})\s\s+(\d{1,5})\s+(\d{1,5})/ )
{
$candidate = ( defined( $name2name{ $1 } ) ? $name2name{ $1 } : $1 );
printf OUT "%4s %-35s%-45s%5s %5s %2s %2s %6d %8d\n", $precinctNumber,
$precinctName, $candidate, $2, $3, $4, $5, $6, $7;
}
# neither candidate name nor precinct name
elsif ( /\s+($rgex::date)\s($rgex::time)\s($rgex::ampm)\s\s(\d{0,1})\s\s+(\d{1,5})\s+(\d{1,5})/ )
{
printf OUT "%4s %-35s%-45s%5s %5s %2s %2s %6d %8d\n", $precinctNumber,
$precinctName, $candidate, $1, $2, $3, $4, $5, $6;
}
# reinsert comma in header date that is stripped out at top of
# loop.
elsif ( /^(\s+)(\w{3,9})\s(\d{1,2})\s(\d{4})/ )
{
print OUT "$1$2 $3, $4\n";;
}
else
{
print OUT $_;
}
}
# reading the file for a list of candidate and precinct names.
else
{
if ( /^($rgex::prc)\s($rgex::str)\s\s\s+?($rgex::str)\s\s\s+($rgex::date)\s($rgex::time)\s($rgex::ampm).*/ )
{
# precinct name
if ( !defined( $name2name{ $2 } ) )
{
$name2name{ $2 } = 1;
}
# candidate name
if ( !defined( $name2name{ $3 } ) )
{
$name2name{ $3 } = 1;
}
}
elsif ( /^($rgex::prc)\s($rgex::str)\s\s\s+?($rgex::date)\s($rgex::time)\s($rgex::ampm).*/ )
{
# precinct name
if ( !defined( $name2name{ $2 } ) )
{
$name2name{ $2 } = 1;
}
}
elsif ( /^\s\s\s+?($rgex::str)\s\s\s+($rgex::date)\s($rgex::time)\s($rgex::ampm).*/ )
{
# candidate name
if ( !defined( $name2name{ $1 } ) )
{
$name2name{ $1 } = 1;
}
}
}
}
# print the list of names in the manual adjustment file if building a
# translation table.
if ( !$trns )
{
utl::printHashKeys( \*OUT, "names", \%name2name );
}
#################################
sub help
{
print "\nperl clean68ab.pl -i [input file] -o [output file]\n";
print " -b [bad characters] -t [translation table]\n\n";
print "1) Declare -i and -o only to build list of names in manual\n";
print " adjustment file from which a translation table is created\n\n";
print "2) Declare -i, -o and -t to use translation table to\n";
print " fix naming problems in manual adjustment file\n\n";
print "The -b parameter is optional. If the users need to globally remove certain\n";
print "characters, create a file with all the bad characters as a single string\n";
print "on a single line at the top of the file. The illegal characters are\n";
print "replaced by a space character\n\n";
exit();
}