| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
#!/usr/bin/perl -w |
|
2
|
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# |
|
4
|
|
|
|
|
|
|
# db_to_csv.pm |
|
5
|
|
|
|
|
|
|
# Copyright (C) 2007 by John Heidemann |
|
6
|
|
|
|
|
|
|
# $Id: 2fc5da9ab90db77e3b4b2adfdf26f9fe040194df $ |
|
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::Filter::db_to_csv; |
|
14
|
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
=head1 NAME |
|
16
|
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
db_to_csv - convert fsdb to the comma-separated-value file-format |
|
18
|
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
20
|
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
db_to_csv [-C] |
|
22
|
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
Covert an existing fsdb file to comma-separated value format. |
|
26
|
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
Input is fsdb format. |
|
28
|
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
Output is CSV-format plain text (I fsdb). |
|
30
|
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
=head1 OPTIONS |
|
32
|
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
=over 4 |
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
=item B<-C> or <--omit-comments> |
|
36
|
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
Also strip all comments. |
|
38
|
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
=back |
|
40
|
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=for comment |
|
43
|
|
|
|
|
|
|
begin_standard_fsdb_options |
|
44
|
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
This module also supports the standard fsdb options: |
|
46
|
|
|
|
|
|
|
|
|
47
|
|
|
|
|
|
|
=over 4 |
|
48
|
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
=item B<-d> |
|
50
|
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
Enable debugging output. |
|
52
|
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
=item B<-i> or B<--input> InputSource |
|
54
|
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
Read from InputSource, typically a file name, or C<-> for standard input, |
|
56
|
|
|
|
|
|
|
or (if in Perl) a IO::Handle, Fsdb::IO or Fsdb::BoundedQueue objects. |
|
57
|
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
=item B<-o> or B<--output> OutputDestination |
|
59
|
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
Write to OutputDestination, typically a file name, or C<-> for standard output, |
|
61
|
|
|
|
|
|
|
or (if in Perl) a IO::Handle, Fsdb::IO or Fsdb::BoundedQueue objects. |
|
62
|
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
=item B<--autorun> or B<--noautorun> |
|
64
|
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
By default, programs process automatically, |
|
66
|
|
|
|
|
|
|
but Fsdb::Filter objects in Perl do not run until you invoke |
|
67
|
|
|
|
|
|
|
the run() method. |
|
68
|
|
|
|
|
|
|
The C<--(no)autorun> option controls that behavior within Perl. |
|
69
|
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
=item B<--help> |
|
71
|
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
Show help. |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
=item B<--man> |
|
75
|
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
Show full manual. |
|
77
|
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
=back |
|
79
|
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
=for comment |
|
81
|
|
|
|
|
|
|
end_standard_fsdb_options |
|
82
|
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
=head1 SAMPLE USAGE |
|
85
|
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
=head2 Input: |
|
87
|
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
#fsdb -F S paper papertitle reviewer reviewername score1 score2 score3 score4 score5 |
|
89
|
|
|
|
|
|
|
1 test, paper 2 Smith 4 4 - - - |
|
90
|
|
|
|
|
|
|
2 other paper 3 Jones 3 3 - - - |
|
91
|
|
|
|
|
|
|
2 input double space 3 Jones 3 3 - - - |
|
92
|
|
|
|
|
|
|
# | csv_to_db |
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
=head2 Command: |
|
95
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
cat data.fsdb | db_to_csv |
|
97
|
|
|
|
|
|
|
|
|
98
|
|
|
|
|
|
|
=head2 Output: |
|
99
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
paper,papertitle,reviewer,reviewername,score1,score2,score3,score4,score5 |
|
101
|
|
|
|
|
|
|
1,"test, paper",2,Smith,4,4,-,-,- |
|
102
|
|
|
|
|
|
|
2,"other paper",3,Jones,3,3,-,-,- |
|
103
|
|
|
|
|
|
|
2,"input double space",3,Jones,3,3,-,-,- |
|
104
|
|
|
|
|
|
|
# | csv_to_db |
|
105
|
|
|
|
|
|
|
# | db_to_csv |
|
106
|
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
109
|
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
L. |
|
111
|
|
|
|
|
|
|
L. |
|
112
|
|
|
|
|
|
|
L |
|
113
|
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=head1 CLASS FUNCTIONS |
|
116
|
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
=cut |
|
118
|
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
@ISA = qw(Fsdb::Filter); |
|
120
|
|
|
|
|
|
|
$VERSION = 2.0; |
|
121
|
|
|
|
|
|
|
|
|
122
|
1
|
|
|
1
|
|
6038
|
use strict; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
39
|
|
|
123
|
1
|
|
|
1
|
|
9
|
use Pod::Usage; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
133
|
|
|
124
|
1
|
|
|
1
|
|
8
|
use Carp; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
68
|
|
|
125
|
1
|
|
|
1
|
|
727
|
use Text::CSV_XS; |
|
|
1
|
|
|
|
|
11080
|
|
|
|
1
|
|
|
|
|
76
|
|
|
126
|
|
|
|
|
|
|
|
|
127
|
1
|
|
|
1
|
|
10
|
use Fsdb::Filter; |
|
|
1
|
|
|
|
|
4
|
|
|
|
1
|
|
|
|
|
29
|
|
|
128
|
1
|
|
|
1
|
|
7
|
use Fsdb::IO::Reader; |
|
|
1
|
|
|
|
|
3
|
|
|
|
1
|
|
|
|
|
656
|
|
|
129
|
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=head2 new |
|
132
|
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
$filter = new Fsdb::Filter::db_to_csv(@arguments); |
|
134
|
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
Create a new db_to_csv object, taking command-line arguments. |
|
136
|
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=cut |
|
138
|
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
sub new ($@) { |
|
140
|
0
|
|
|
0
|
1
|
|
my $class = shift @_; |
|
141
|
0
|
|
|
|
|
|
my $self = $class->SUPER::new(@_); |
|
142
|
0
|
|
|
|
|
|
bless $self, $class; |
|
143
|
0
|
|
|
|
|
|
$self->set_defaults; |
|
144
|
0
|
|
|
|
|
|
$self->parse_options(@_); |
|
145
|
0
|
|
|
|
|
|
$self->SUPER::post_new(); |
|
146
|
0
|
|
|
|
|
|
return $self; |
|
147
|
|
|
|
|
|
|
} |
|
148
|
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
=head2 set_defaults |
|
151
|
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
$filter->set_defaults(); |
|
153
|
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
Internal: set up defaults. |
|
155
|
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
=cut |
|
157
|
|
|
|
|
|
|
|
|
158
|
|
|
|
|
|
|
sub set_defaults ($) { |
|
159
|
0
|
|
|
0
|
1
|
|
my($self) = @_; |
|
160
|
0
|
|
|
|
|
|
$self->SUPER::set_defaults(); |
|
161
|
0
|
|
|
|
|
|
$self->{_omit_comments} = undef; |
|
162
|
|
|
|
|
|
|
} |
|
163
|
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
=head2 parse_options |
|
165
|
|
|
|
|
|
|
|
|
166
|
|
|
|
|
|
|
$filter->parse_options(@ARGV); |
|
167
|
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
Internal: parse command-line arguments. |
|
169
|
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
=cut |
|
171
|
|
|
|
|
|
|
|
|
172
|
|
|
|
|
|
|
sub parse_options ($@) { |
|
173
|
0
|
|
|
0
|
1
|
|
my $self = shift @_; |
|
174
|
|
|
|
|
|
|
|
|
175
|
0
|
|
|
|
|
|
my(@argv) = @_; |
|
176
|
|
|
|
|
|
|
$self->get_options( |
|
177
|
|
|
|
|
|
|
\@argv, |
|
178
|
0
|
|
|
0
|
|
|
'help|?' => sub { pod2usage(1); }, |
|
179
|
0
|
|
|
0
|
|
|
'man' => sub { pod2usage(-verbose => 2); }, |
|
180
|
|
|
|
|
|
|
'autorun!' => \$self->{_autorun}, |
|
181
|
|
|
|
|
|
|
'C|omit-comments!' => \$self->{_omit_comments}, |
|
182
|
|
|
|
|
|
|
'd|debug+' => \$self->{_debug}, |
|
183
|
0
|
|
|
0
|
|
|
'i|input=s' => sub { $self->parse_io_option('input', @_); }, |
|
184
|
|
|
|
|
|
|
'log!' => \$self->{_logprog}, |
|
185
|
0
|
|
|
0
|
|
|
'o|output=s' => sub { $self->parse_io_option('output', @_); }, |
|
186
|
0
|
0
|
|
|
|
|
) or pod2usage(2); |
|
187
|
0
|
0
|
|
|
|
|
pod2usage(2) if ($#argv != -1); |
|
188
|
|
|
|
|
|
|
} |
|
189
|
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
=head2 setup |
|
191
|
|
|
|
|
|
|
|
|
192
|
|
|
|
|
|
|
$filter->setup(); |
|
193
|
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
Internal: setup, parse headers. |
|
195
|
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
=cut |
|
197
|
|
|
|
|
|
|
|
|
198
|
|
|
|
|
|
|
sub setup ($) { |
|
199
|
0
|
|
|
0
|
1
|
|
my($self) = @_; |
|
200
|
|
|
|
|
|
|
|
|
201
|
0
|
|
|
|
|
|
my $comment_sub; |
|
202
|
0
|
0
|
|
|
|
|
if ($self->{_omit_comments}) { |
|
203
|
0
|
|
|
0
|
|
|
$comment_sub = sub {}; |
|
204
|
|
|
|
|
|
|
} else { |
|
205
|
0
|
|
|
0
|
|
|
$comment_sub = sub { $self->{_out}->print(join("\n", @_)); }; |
|
|
0
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
}; |
|
207
|
|
|
|
|
|
|
|
|
208
|
0
|
|
|
|
|
|
$self->finish_io_option('input', -comment_handler => $comment_sub); |
|
209
|
|
|
|
|
|
|
|
|
210
|
0
|
|
|
|
|
|
$self->finish_fh_io_option('output'); |
|
211
|
0
|
0
|
|
|
|
|
$self->{_logprog} = undef if ($self->{_omit_comments}); |
|
212
|
|
|
|
|
|
|
|
|
213
|
|
|
|
|
|
|
# write out the header as the first line |
|
214
|
|
|
|
|
|
|
# xxx: perhaps have an option to suppress this? |
|
215
|
0
|
|
|
|
|
|
my $csv = $self->{_csv} = new Text::CSV_XS; |
|
216
|
0
|
|
|
|
|
|
$csv->combine(@{$self->{_in}->cols}) |
|
217
|
0
|
0
|
|
|
|
|
or croak $self->{_prog} . ": cannot generate column names.\n"; |
|
218
|
0
|
|
|
|
|
|
$self->{_out}->print($csv->string . "\n"); |
|
219
|
|
|
|
|
|
|
} |
|
220
|
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
=head2 run |
|
222
|
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
$filter->run(); |
|
224
|
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
Internal: run over each row. |
|
226
|
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
=cut |
|
228
|
|
|
|
|
|
|
sub run ($) { |
|
229
|
0
|
|
|
0
|
1
|
|
my($self) = @_; |
|
230
|
|
|
|
|
|
|
|
|
231
|
0
|
|
|
|
|
|
my $read_fastpath_sub = $self->{_in}->fastpath_sub(); |
|
232
|
0
|
|
|
|
|
|
my $fref; |
|
233
|
0
|
|
|
|
|
|
my $csv = $self->{_csv}; |
|
234
|
0
|
|
|
|
|
|
my $out_fh = $self->{_out}; |
|
235
|
0
|
|
|
|
|
|
while ($fref = &{$read_fastpath_sub}()) { |
|
|
0
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
$csv->combine(@$fref) |
|
237
|
0
|
0
|
|
|
|
|
or croak $self->{_prog} . ": failed on this line: " . join(" ", @$fref) . ".\n"; |
|
238
|
0
|
|
|
|
|
|
$out_fh->print($csv->string . "\n"); |
|
239
|
|
|
|
|
|
|
}; |
|
240
|
|
|
|
|
|
|
} |
|
241
|
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
|
|
243
|
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
=head1 AUTHOR and COPYRIGHT |
|
246
|
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
Copyright (C) 1991-2008 by John Heidemann |
|
248
|
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
This program is distributed under terms of the GNU general |
|
250
|
|
|
|
|
|
|
public license, version 2. See the file COPYING |
|
251
|
|
|
|
|
|
|
with the distribution for details. |
|
252
|
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
=cut |
|
254
|
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
1; |