line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
#!/usr/bin/perl |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# Support.pm |
5
|
|
|
|
|
|
|
# Copyright (C) 1991-2007 by John Heidemann |
6
|
|
|
|
|
|
|
# $Id: 88483b6ffcd50120552f971d8e96d3f2e82f71dd $ |
7
|
|
|
|
|
|
|
# |
8
|
|
|
|
|
|
|
# This program is distributed under terms of the GNU general |
9
|
|
|
|
|
|
|
# public license, version 2. See the file COPYING |
10
|
|
|
|
|
|
|
# in $dblibdir for details. |
11
|
|
|
|
|
|
|
# |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
package Fsdb::Support; |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 NAME |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
Fsdb::Support - support routines for Fsdb |
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
=head1 SYNOPSIS |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
This class contains the bits of Fsdb::Old that needed to be kept. |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
=head1 FUNCTIONS |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
=cut |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
@ISA = (); |
28
|
|
|
|
|
|
|
($VERSION) = 1.0; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
## Module import. |
31
|
2
|
|
|
2
|
|
15
|
use Exporter 'import'; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
181
|
|
32
|
|
|
|
|
|
|
@EXPORT = qw(); |
33
|
|
|
|
|
|
|
@EXPORT_OK = qw( |
34
|
|
|
|
|
|
|
code_prettify |
35
|
|
|
|
|
|
|
force_numeric |
36
|
|
|
|
|
|
|
fullname_to_sortkey |
37
|
|
|
|
|
|
|
progname |
38
|
|
|
|
|
|
|
$is_numeric_regexp |
39
|
|
|
|
|
|
|
ddmmmyy_to_iso |
40
|
|
|
|
|
|
|
); |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
# |
43
|
|
|
|
|
|
|
# our libaries |
44
|
|
|
|
|
|
|
# |
45
|
2
|
|
|
2
|
|
12
|
use IO::Handle; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
174
|
|
46
|
2
|
|
|
2
|
|
12
|
use IO::File; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
359
|
|
47
|
2
|
|
|
2
|
|
10
|
use Carp qw(croak); |
|
2
|
|
|
|
|
42
|
|
|
2
|
|
|
|
|
167
|
|
48
|
|
|
|
|
|
|
|
49
|
2
|
|
|
2
|
|
20
|
use Fsdb::IO::Reader; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
76
|
|
50
|
2
|
|
|
2
|
|
16
|
use Fsdb::IO::Writer; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
2046
|
|
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
=head1 LOGGING REALTED FUNCTIONS |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
=head2 progname |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
Generate the name of our program for error messages. |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=cut |
59
|
|
|
|
|
|
|
sub progname () { |
60
|
0
|
|
|
0
|
1
|
|
my($prog) = ($0); |
61
|
0
|
|
|
|
|
|
$prog =~ s@^.*/@@g; |
62
|
0
|
|
|
|
|
|
return $prog; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
=head1 IO SETUP FUNCTIONS |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
=head2 default_in(@READER_OPTIONS) |
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
Generate a default Fsdb::Reader object with the given READER_OPTIONS |
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
=cut |
72
|
|
|
|
|
|
|
sub default_in ($@) { |
73
|
0
|
|
|
0
|
1
|
|
my $in_fh = new IO::Handle; |
74
|
0
|
0
|
|
|
|
|
$in_fh->fdopen(fileno(STDIN), "r") or croak progname . ": cannot open input as fsdb.\n"; |
75
|
0
|
|
|
|
|
|
my $in = new Fsdb::IO::Reader(-fh => $in_fh, @_); |
76
|
0
|
|
|
|
|
|
return $in; |
77
|
|
|
|
|
|
|
# $in->error and croak progname . ": cannot open input as fsdb.\n"; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=head2 default_out(@WRITER_OPTIONS) |
81
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
Generate a default Fsdb::Writer object with the given READER_OPTIONS |
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=cut |
85
|
|
|
|
|
|
|
sub default_out ($@) { |
86
|
0
|
|
|
0
|
1
|
|
my $out_fh = new IO::Handle; |
87
|
0
|
0
|
|
|
|
|
$out_fh->fdopen(fileno(STDOUT), "w+") or croak progname . ": cannot open stdout.\n"; |
88
|
0
|
|
|
|
|
|
my $out = new Fsdb::IO::Writer(-fh => $out_fh, @_); |
89
|
0
|
|
|
|
|
|
return $out; |
90
|
|
|
|
|
|
|
# $out->error and croak progname . ": cannot open STDOUT as fsdb.\n"; |
91
|
|
|
|
|
|
|
} |
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
=head1 CONVERSION FUNCTIONS |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
=head2 code_prettify |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
Convert db-code into "pretty code". |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=cut |
100
|
|
|
|
|
|
|
sub code_prettify (@) { |
101
|
0
|
|
|
0
|
1
|
|
my($prettycode) = join(";", @_); |
102
|
0
|
|
|
|
|
|
$prettycode =~ s/\n/ /g; # newlines will break commenting |
103
|
0
|
|
|
|
|
|
return $prettycode; |
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
=head1 CONVERSION FUNCTIONS |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=head2 number_prettify |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
Add-thousands-separators to numbers. |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
xxx: should consider locale. |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
(This code is from F, |
115
|
|
|
|
|
|
|
contributed by Andrew Johnson from University of Alberta.) |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=cut |
118
|
|
|
|
|
|
|
sub number_prettify($) { |
119
|
0
|
|
|
0
|
1
|
|
my $input = shift; |
120
|
0
|
|
|
|
|
|
$input = reverse $input; |
121
|
0
|
|
|
|
|
|
$input =~ s<(\d\d\d)(?=\d)(?!\d*\.)><$1,>g; |
122
|
0
|
|
|
|
|
|
return reverse $input; |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
|
|
|
|
|
|
=head2 force_numeric |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
my $x = force_numeric($s, $include_non_numeric) |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
Return C<$S> if it's numeric, or C if not. |
130
|
|
|
|
|
|
|
If C<$INCLUDE_NON_NUMERIC>, then non-numeric values register as zero. |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=cut |
133
|
|
|
|
|
|
|
# note that we tolerate spaces before and after, |
134
|
|
|
|
|
|
|
# since field splitting doesn't always kill them |
135
|
|
|
|
|
|
|
# (see TEST/dbcolstats_trailing_spaces.in) |
136
|
|
|
|
|
|
|
our $is_numeric_regexp = '^\s*[+-]?(\d+\.\d+|\d+\.|\.\d+|\d+)([eE][+-]?\d+)?\s*$'; |
137
|
|
|
|
|
|
|
sub force_numeric { |
138
|
0
|
|
|
0
|
1
|
|
my($value, $zero_non_numeric) = @_; |
139
|
|
|
|
|
|
|
# next re is almost copied from L |
140
|
0
|
0
|
|
|
|
|
if ($value =~ /$is_numeric_regexp/) { |
141
|
0
|
|
|
|
|
|
return $value + 0.0; # force numeric |
142
|
|
|
|
|
|
|
} else { |
143
|
0
|
0
|
|
|
|
|
if ($ignore_non_numeric) { |
144
|
0
|
|
|
|
|
|
return undef; |
145
|
0
|
|
|
|
|
|
next; |
146
|
|
|
|
|
|
|
} else { |
147
|
0
|
|
|
|
|
|
return 0.0; |
148
|
|
|
|
|
|
|
}; |
149
|
|
|
|
|
|
|
}; |
150
|
|
|
|
|
|
|
} |
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=head2 fullname_to_sortkey |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
my $sortkey = fullname_to_sortkey("John Smith"); |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
Convert "Firstname Lastname" to sort key "lastname, firstname". |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=cut |
160
|
|
|
|
|
|
|
sub fullname_to_sortkey { |
161
|
0
|
|
|
0
|
1
|
|
my($sort) = @_; |
162
|
0
|
|
|
|
|
|
$sort = lc($sort); |
163
|
0
|
|
|
|
|
|
my($first, $last) = ($sort =~ /^(.*)\s+(\S+)$/); |
164
|
0
|
0
|
|
|
|
|
$last = $sort if (!defined($last)); |
165
|
0
|
0
|
|
|
|
|
$first = '' if (!defined($first)); |
166
|
0
|
|
|
|
|
|
return "$last, $first"; |
167
|
|
|
|
|
|
|
} |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=head2 ddmmmyy_to_iso |
171
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
my $iso_date = ddmmmyy_to_iso('1-Jan-10') |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
Converts a date in the form dd-mmm-yy to ISO-style yyyy-mm-dd. |
175
|
|
|
|
|
|
|
Examples: |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
2-Jan-70 to 1970-01-02 |
178
|
|
|
|
|
|
|
2-Jan-99 to 1999-01-02 |
179
|
|
|
|
|
|
|
2-Jan-10 to 2010-01-02 |
180
|
|
|
|
|
|
|
2-Jan-69 to 2069-01-02 |
181
|
|
|
|
|
|
|
Jan-10 to 2010-01-00 |
182
|
|
|
|
|
|
|
99 to 1999-00-00 |
183
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
=cut |
185
|
|
|
|
|
|
|
sub ddmmmyy_to_iso { |
186
|
0
|
|
|
0
|
1
|
|
my($orig) = @_; |
187
|
0
|
0
|
|
|
|
|
return $orig if ($orig eq '-'); |
188
|
0
|
|
|
|
|
|
my(@parts) = split('-', $orig); |
189
|
0
|
0
|
|
|
|
|
unshift(@parts, '00') if ($#parts == 0); |
190
|
0
|
0
|
|
|
|
|
unshift(@parts, '00') if ($#parts == 1); |
191
|
0
|
|
|
|
|
|
my($dd, $mm, $yyyy) = @parts; |
192
|
0
|
0
|
|
|
|
|
$dd = '0' if ($dd eq '?'); |
193
|
0
|
|
|
|
|
|
my(%map) = qw(jan 1 feb 2 mar 3 apr 4 may 5 jun 6 jul 7 aug 8 sep 9 oct 10 nov 11 dec 12); |
194
|
0
|
0
|
|
|
|
|
$mm = $map{lc($mm)}; $mm = 0 if (!defined($mm)); # sigh, for 5.008 |
|
0
|
|
|
|
|
|
|
195
|
0
|
0
|
0
|
|
|
|
$yyyy += 1900 if ($yyyy >= 70 && $yyyy < 100); |
196
|
0
|
0
|
|
|
|
|
$yyyy += 2000 if ($yyyy < 70); |
197
|
0
|
|
|
|
|
|
return sprintf("%04d-%02d-%02d", $yyyy, $mm, $dd); |
198
|
|
|
|
|
|
|
} |
199
|
|
|
|
|
|
|
|
200
|
|
|
|
|
|
|
1; |