line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Text::NumericData; |
2
|
|
|
|
|
|
|
|
3
|
14
|
|
|
14
|
|
69745
|
use strict; |
|
14
|
|
|
|
|
32
|
|
|
14
|
|
|
|
|
486
|
|
4
|
14
|
|
|
14
|
|
9779
|
use Storable qw(dclone); |
|
14
|
|
|
|
|
48482
|
|
|
14
|
|
|
|
|
49767
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
# TODO: optimize those regexes, compile once in constructor |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
# major.minor.bugfix, the latter two with 3 digits each |
9
|
|
|
|
|
|
|
# It's not pretty, but I gave up on 1.2.3 style. |
10
|
|
|
|
|
|
|
our $VERSION = '2.004000'; |
11
|
|
|
|
|
|
|
our $version = $VERSION; |
12
|
|
|
|
|
|
|
$VERSION = eval $VERSION; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our $years = '2005-2023'; |
15
|
|
|
|
|
|
|
our $copyright = 'Copyright (c) '.$years.' Thomas Orgis, Free Software licensed under the same terms as Perl 5.10'; |
16
|
|
|
|
|
|
|
our $author = 'Thomas Orgis '; |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
# TODO: More smarts in separator search. |
19
|
|
|
|
|
|
|
# One should find ', ' as separator in |
20
|
|
|
|
|
|
|
# a / c, d /e |
21
|
|
|
|
|
|
|
my $newhite = '[^\S\015\012]'; # whitespace that is no line end character |
22
|
|
|
|
|
|
|
my $trenner = $newhite.'+|,'.$newhite.'*|;'.$newhite.'*'; |
23
|
|
|
|
|
|
|
my $ntrenner = '[^\s,;]+'; # also excludes CR LF |
24
|
|
|
|
|
|
|
my $lend = '[\012\015]+'; |
25
|
|
|
|
|
|
|
my $nlend = '[^\012\015]'; |
26
|
|
|
|
|
|
|
my $quotematch = "['\"]"; |
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
my %endname = ("\015\012"=>'DOS', "\012"=>'UNIX', "\015"=>'MAC'); |
29
|
|
|
|
|
|
|
my %endstring = reverse %endname; |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
# Fallback defaults if anything else fails. |
32
|
|
|
|
|
|
|
our $default_sep = "\t"; |
33
|
|
|
|
|
|
|
our $default_eol = $/; |
34
|
|
|
|
|
|
|
our $default_comchar = '#'; |
35
|
|
|
|
|
|
|
our $default_quote = 1; |
36
|
|
|
|
|
|
|
our $default_quotechar = '"'; |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
our %help = |
39
|
|
|
|
|
|
|
( |
40
|
|
|
|
|
|
|
separator=>'use this separator for input (otherwise deduce from data; TAB is another way to say "tabulator", fallback is'.$default_sep.')' |
41
|
|
|
|
|
|
|
,outsep=>'use this separator for output (leave undefined to use input separator, fallback to '.($default_sep eq "\t" ? 'TAB' : $default_sep).')' |
42
|
|
|
|
|
|
|
,lineend=>'line ending to use: ('.join(', ', sort keys %endstring).' or be explicit if you can, taken from data if undefined, finally resorting to '.(defined $endname{$default_eol} ? $endname{$default_eol} : $default_eol).')' |
43
|
|
|
|
|
|
|
,comchar=>'comment character (if not set, deduce from data or use '.$default_comchar.')' |
44
|
|
|
|
|
|
|
,numregex=>'regex for matching numbers' |
45
|
|
|
|
|
|
|
,numformat=>'printf formats to use (if there is no "%" present at all, one will be prepended)' |
46
|
|
|
|
|
|
|
,comregex=>'regex for matching comments' |
47
|
|
|
|
|
|
|
,quote=>'quote titles' |
48
|
|
|
|
|
|
|
,quotechar=>'quote character to use (derived from input or '.$default_quotechar.')' |
49
|
|
|
|
|
|
|
,strict=>'strictly split data lines at configured separator (otherwise more fuzzy logic is involved)' |
50
|
|
|
|
|
|
|
,text=>'allow text as data (not first column)' |
51
|
|
|
|
|
|
|
,fill=>'fill value for undefined data' |
52
|
|
|
|
|
|
|
,black=>'ignore whitespace at beginning and end of line (disables strict mode)' |
53
|
|
|
|
|
|
|
,empty=>'treat empty lines as empty data sets, preserving them in output' |
54
|
|
|
|
|
|
|
); |
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
# These are defaults for user settings. |
57
|
|
|
|
|
|
|
our %defaults = |
58
|
|
|
|
|
|
|
( |
59
|
|
|
|
|
|
|
'separator',undef, |
60
|
|
|
|
|
|
|
'outsep', undef, |
61
|
|
|
|
|
|
|
'lineend', undef, |
62
|
|
|
|
|
|
|
'comchar', undef, |
63
|
|
|
|
|
|
|
'numregex', '[\+\-]?\d*\.?\d*[eE]?\+?\-?\d*', |
64
|
|
|
|
|
|
|
'numformat',[], |
65
|
|
|
|
|
|
|
'comregex','[#%]*'.$newhite.'*', |
66
|
|
|
|
|
|
|
'quote',undef, |
67
|
|
|
|
|
|
|
'quotechar',undef, |
68
|
|
|
|
|
|
|
'strict', 0, |
69
|
|
|
|
|
|
|
'text', 1 |
70
|
|
|
|
|
|
|
,'fill',undef # a value to fill in for non-existent but still demanded data |
71
|
|
|
|
|
|
|
,'black', 0 |
72
|
|
|
|
|
|
|
,'empty',0 |
73
|
|
|
|
|
|
|
); |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
sub new |
76
|
|
|
|
|
|
|
{ |
77
|
60
|
|
|
60
|
0
|
160
|
my $class = shift; |
78
|
60
|
|
|
|
|
124
|
my $self = {}; |
79
|
60
|
|
|
|
|
144
|
bless $self, $class; |
80
|
|
|
|
|
|
|
# Only pick the parts of the config hash that are of interest here. |
81
|
60
|
|
|
|
|
104
|
my $gotconf = shift; |
82
|
60
|
|
|
|
|
198
|
$self->{gotconfig} = {}; |
83
|
60
|
|
|
|
|
330
|
for my $n (keys %defaults) |
84
|
|
|
|
|
|
|
{ |
85
|
840
|
100
|
|
|
|
1615
|
if(ref $gotconf->{$n}) |
86
|
|
|
|
|
|
|
{ |
87
|
58
|
|
|
|
|
2789
|
$self->{gotconfig}{$n} = dclone($gotconf->{$n}); |
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
else |
90
|
|
|
|
|
|
|
{ |
91
|
782
|
|
|
|
|
1522
|
$self->{gotconfig}{$n} = $gotconf->{$n}; |
92
|
|
|
|
|
|
|
} |
93
|
|
|
|
|
|
|
} |
94
|
60
|
|
|
|
|
179
|
foreach my $n (@{$self->{gotconfig}->{numformat}}) |
|
60
|
|
|
|
|
222
|
|
95
|
|
|
|
|
|
|
{ |
96
|
41
|
100
|
|
|
|
217
|
$n = '%'.$n unless $n =~ /\%/; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
# Expand named special characters for line ending. |
99
|
60
|
100
|
|
|
|
225
|
if(defined $gotconf->{lineend}) |
100
|
|
|
|
|
|
|
{ |
101
|
|
|
|
|
|
|
$self->{gotconfig}{lineend} = defined $endstring{$gotconf->{lineend}} |
102
|
|
|
|
|
|
|
? $endstring{$gotconf->{lineend}} |
103
|
37
|
100
|
|
|
|
195
|
: $gotconf->{lineend}; |
104
|
|
|
|
|
|
|
} |
105
|
60
|
|
|
|
|
168
|
for (qw(separator outsep)) |
106
|
|
|
|
|
|
|
{ |
107
|
120
|
100
|
100
|
|
|
437
|
if(defined $gotconf->{$_} and $gotconf->{$_} eq 'TAB'){ $self->{gotconfig}{$_} = "\t"; } |
|
2
|
|
|
|
|
9
|
|
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
$self->{gotconfig}{strict} = 0 |
111
|
60
|
100
|
|
|
|
171
|
if $self->{gotconfig}{black}; |
112
|
60
|
|
|
|
|
222
|
$self->init(); |
113
|
60
|
|
|
|
|
349
|
return $self; |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
sub init |
117
|
|
|
|
|
|
|
{ |
118
|
77
|
|
|
77
|
0
|
151
|
my $self = shift; |
119
|
77
|
|
|
|
|
131
|
%{$self->{config}} = %{$self->{gotconfig}}; |
|
77
|
|
|
|
|
495
|
|
|
77
|
|
|
|
|
338
|
|
120
|
77
|
|
|
|
|
292
|
$self->{comments} = []; #some comment in header |
121
|
77
|
|
|
|
|
170
|
$self->{guessquote} = undef; |
122
|
77
|
|
|
|
|
164
|
$self->{titles} = []; #column titles |
123
|
77
|
|
|
|
|
148
|
$self->{title} = undef; #file title |
124
|
77
|
|
|
|
|
165
|
foreach my $k ('numregex','numformat','comregex','fill') |
125
|
|
|
|
|
|
|
{ |
126
|
308
|
100
|
|
|
|
785
|
$self->{config}{$k} = $defaults{$k} unless defined $self->{config}{$k}; |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
# Strict mode needs some set separator. |
129
|
77
|
100
|
100
|
|
|
277
|
if($self->{config}{strict} and not defined $self->{config}{separator}) |
130
|
|
|
|
|
|
|
{ |
131
|
1
|
|
|
|
|
3
|
$self->{config}{separator} = $default_sep; |
132
|
|
|
|
|
|
|
} |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
#line_check($line, $onlycheck) |
136
|
|
|
|
|
|
|
#$onlycheck: 0/undef: do full search for file/line titles and line end, etc. |
137
|
|
|
|
|
|
|
# 1: only determine if data or not |
138
|
|
|
|
|
|
|
sub line_check #return 1 and set separator and line ending if data line and 0 otherwise |
139
|
|
|
|
|
|
|
{ |
140
|
101
|
|
|
101
|
1
|
177
|
my $self = shift; |
141
|
|
|
|
|
|
|
# temporary hack until fully switching to value instead of ref, which is a ref anyway |
142
|
101
|
100
|
|
|
|
273
|
my $lr = ref $_[0] ? $_[0] : \$_[0]; |
143
|
101
|
|
|
|
|
166
|
my $oc = $_[1]; |
144
|
101
|
|
|
|
|
195
|
my $zahl = $self->{config}{numregex}; |
145
|
101
|
|
|
|
|
166
|
my $seppl = $trenner; |
146
|
101
|
100
|
|
|
|
241
|
$seppl = $self->{config}{separator} if $self->{config}{strict}; |
147
|
|
|
|
|
|
|
#the leading whitespace is a workaround for TISEAN |
148
|
|
|
|
|
|
|
#good or bad? It should not break any files that worked before... |
149
|
101
|
100
|
66
|
|
|
159
|
if( ${$lr} =~ /^\s*$/ and ${$lr} =~ /^($nlend*)($lend)$/o ) |
|
101
|
|
|
|
|
506
|
|
|
2
|
|
|
|
|
31
|
|
150
|
|
|
|
|
|
|
{ |
151
|
2
|
50
|
|
|
|
12
|
$self->{config}{lineend} = $2 unless defined $self->{config}{lineend}; |
152
|
|
|
|
|
|
|
# An empty line counts as comment when it comes after a title. |
153
|
2
|
|
|
|
|
9
|
push(@{$self->{comments}},$1) |
154
|
2
|
50
|
|
|
|
5
|
if defined $self->{title}; |
155
|
2
|
|
|
|
|
10
|
return 0; |
156
|
|
|
|
|
|
|
} |
157
|
99
|
100
|
|
|
|
167
|
if(${$lr} =~ /^\s*($zahl)(($seppl)$nlend*|)($lend)$/) |
|
99
|
50
|
|
|
|
2149
|
|
158
|
|
|
|
|
|
|
{ |
159
|
41
|
|
|
|
|
200
|
my ($num, $end, $sep) = ($1, $4, $3); |
160
|
41
|
|
|
|
|
135
|
my $piece = $1.$2; |
161
|
41
|
100
|
66
|
|
|
268
|
unless(not defined $end or defined $self->{config}{lineend}) |
162
|
|
|
|
|
|
|
{ |
163
|
1
|
|
|
|
|
3
|
$self->{config}{lineend} = $end; |
164
|
|
|
|
|
|
|
} |
165
|
41
|
100
|
|
|
|
177
|
unless($self->{config}{text}) |
166
|
|
|
|
|
|
|
{ |
167
|
|
|
|
|
|
|
# If text is not allowed, we strictly only want |
168
|
|
|
|
|
|
|
# numbers and separators and line end. |
169
|
|
|
|
|
|
|
# Let's get expensive: Remove everything we know. if there is something |
170
|
|
|
|
|
|
|
# left, we got text. |
171
|
2
|
|
|
|
|
3
|
my $linecopy = ${$lr}; |
|
2
|
|
|
|
|
6
|
|
172
|
2
|
|
|
|
|
58
|
$linecopy =~ s/$seppl//g; |
173
|
2
|
|
|
|
|
96
|
$linecopy =~ s/($zahl|\s+|$lend)//g; |
174
|
2
|
100
|
|
|
|
15
|
if($linecopy ne '') |
175
|
|
|
|
|
|
|
{ |
176
|
1
|
50
|
|
|
|
6
|
if( defined $self->{title} ){ push(@{$self->{comments}},$piece); } |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
4
|
|
177
|
0
|
|
|
|
|
0
|
else{ $self->{title} = $piece; } |
178
|
1
|
|
|
|
|
7
|
return 0; |
179
|
|
|
|
|
|
|
} |
180
|
|
|
|
|
|
|
} |
181
|
|
|
|
|
|
|
# sanity check for loosened definition of number... at least one digit shall be there |
182
|
40
|
50
|
|
|
|
237
|
if($num =~ /\d/) |
183
|
|
|
|
|
|
|
{ |
184
|
40
|
100
|
100
|
|
|
221
|
unless(not defined $sep or defined $self->{config}{separator}) |
185
|
|
|
|
|
|
|
{ |
186
|
33
|
|
|
|
|
92
|
$self->{config}{separator} = $sep; |
187
|
|
|
|
|
|
|
} |
188
|
40
|
100
|
66
|
|
|
58
|
if($#{$self->{comments}} > -1 and $#{$self->{titles}} > -1) |
|
40
|
|
|
|
|
175
|
|
|
23
|
|
|
|
|
98
|
|
189
|
|
|
|
|
|
|
{ |
190
|
23
|
|
|
|
|
47
|
pop(@{$self->{comments}}); |
|
23
|
|
|
|
|
43
|
|
191
|
|
|
|
|
|
|
} |
192
|
40
|
|
|
|
|
209
|
return 1; # Yeah, found a number line. |
193
|
|
|
|
|
|
|
} |
194
|
|
|
|
|
|
|
} |
195
|
0
|
|
|
|
|
0
|
elsif($oc){ return 0; } |
196
|
|
|
|
|
|
|
else |
197
|
|
|
|
|
|
|
{ |
198
|
58
|
50
|
|
|
|
123
|
if(${$lr} =~ /^($self->{config}{comregex})($lend)$/) |
|
58
|
|
|
|
|
696
|
|
199
|
|
|
|
|
|
|
{ |
200
|
|
|
|
|
|
|
$self->{config}{comchar} = $1 |
201
|
0
|
0
|
|
|
|
0
|
unless defined $self->{config}{comchar}; |
202
|
|
|
|
|
|
|
$self->{config}{lineend} = $2 |
203
|
0
|
0
|
|
|
|
0
|
unless defined $self->{config}{lineend}; |
204
|
0
|
|
|
|
|
0
|
return 0; |
205
|
|
|
|
|
|
|
} |
206
|
|
|
|
|
|
|
#first non-empty line is some kind of title or comment |
207
|
|
|
|
|
|
|
#first means: we didn't have content up to now |
208
|
58
|
50
|
|
|
|
128
|
if(${$lr} =~ /^($self->{config}{comregex})($nlend+)($lend)$/) |
|
58
|
|
|
|
|
668
|
|
209
|
|
|
|
|
|
|
{ |
210
|
58
|
100
|
|
|
|
179
|
if( defined $self->{title} ){ push(@{$self->{comments}},$2); } |
|
33
|
|
|
|
|
77
|
|
|
33
|
|
|
|
|
124
|
|
211
|
25
|
|
|
|
|
86
|
else{ $self->{title} = $2; } |
212
|
|
|
|
|
|
|
$self->{config}{lineend} = $3 |
213
|
58
|
100
|
|
|
|
162
|
unless defined $self->{config}{lineend}; |
214
|
|
|
|
|
|
|
$self->{config}{comchar} = $1 |
215
|
58
|
100
|
|
|
|
166
|
unless defined $self->{config}{comchar}; |
216
|
|
|
|
|
|
|
} |
217
|
|
|
|
|
|
|
#attention: I take " or ' just as quotes, do distinction! |
218
|
58
|
|
|
|
|
122
|
my $quote = $self->{config}{quotechar}; |
219
|
58
|
50
|
|
|
|
162
|
$quote = $quotematch |
220
|
|
|
|
|
|
|
unless defined $quote; |
221
|
58
|
100
|
|
|
|
78
|
if(${$lr} =~ /^($self->{config}{comregex})($quote)($nlend*\2($seppl)\2*$nlend*)\2*($lend)$/) |
|
58
|
50
|
|
|
|
1263
|
|
222
|
|
|
|
|
|
|
{ |
223
|
|
|
|
|
|
|
$self->{config}{quote} = 1 |
224
|
20
|
50
|
|
|
|
90
|
unless defined $self->{config}{quote}; |
225
|
|
|
|
|
|
|
$self->{config}{quotechar} = $2 |
226
|
20
|
50
|
|
|
|
98
|
unless defined $self->{config}{quotechar}; |
227
|
|
|
|
|
|
|
# "axis title"\t"axis title"\t"..." |
228
|
|
|
|
|
|
|
# allow flexible space in separator |
229
|
20
|
|
|
|
|
57
|
my $sep = $4; |
230
|
20
|
|
|
|
|
45
|
my $q = $2; |
231
|
20
|
|
|
|
|
44
|
my $rest = $3; |
232
|
20
|
|
|
|
|
150
|
$rest =~ s:$q$::; |
233
|
|
|
|
|
|
|
$sep =~ s:\s+$:\\s+: |
234
|
20
|
50
|
|
|
|
148
|
unless($self->{config}{strict}); |
235
|
20
|
|
|
|
|
314
|
my @ax = split($q.$sep.$q,$rest); |
236
|
20
|
|
|
|
|
92
|
$self->{titles} = \@ax; |
237
|
|
|
|
|
|
|
$self->{config}{lineend} = $5 |
238
|
20
|
50
|
|
|
|
77
|
unless defined $self->{config}{lineend}; |
239
|
|
|
|
|
|
|
$self->{config}{comchar} = $1 |
240
|
20
|
50
|
|
|
|
75
|
unless defined $self->{config}{comchar}; |
241
|
|
|
|
|
|
|
} |
242
|
|
|
|
|
|
|
#either no quotes at all or maybe quotes but single item without separator |
243
|
38
|
|
|
|
|
758
|
elsif(${$lr} =~ /^($self->{config}{comregex})($quote?)($nlend*)($lend)$/) |
244
|
|
|
|
|
|
|
{ |
245
|
38
|
50
|
|
|
|
136
|
if($2 ne '') |
246
|
|
|
|
|
|
|
{ |
247
|
|
|
|
|
|
|
$self->{config}{quotechar} = $2 |
248
|
0
|
0
|
|
|
|
0
|
unless defined $self->{config}{quotechar}; |
249
|
|
|
|
|
|
|
$self->{config}{quote} = 1 |
250
|
0
|
0
|
|
|
|
0
|
unless defined $self->{config}{quote}; |
251
|
|
|
|
|
|
|
} |
252
|
|
|
|
|
|
|
else |
253
|
|
|
|
|
|
|
{ |
254
|
38
|
|
|
|
|
118
|
$self->{guessquote} = 0 |
255
|
|
|
|
|
|
|
} |
256
|
|
|
|
|
|
|
$self->{config}{lineend} = $4 |
257
|
38
|
50
|
|
|
|
112
|
unless defined $self->{config}{lineend}; |
258
|
|
|
|
|
|
|
$self->{config}{comchar} = $1 |
259
|
38
|
50
|
|
|
|
100
|
unless defined $self->{config}{comchar}; |
260
|
38
|
|
|
|
|
103
|
my $d = $3; |
261
|
38
|
|
|
|
|
226
|
$d =~ s/$quote$//; |
262
|
38
|
|
|
|
|
109
|
my @ax = (); |
263
|
38
|
100
|
|
|
|
715
|
if($d =~ /($seppl)/) |
264
|
|
|
|
|
|
|
{ |
265
|
24
|
|
|
|
|
342
|
@ax = split($1, $d); |
266
|
|
|
|
|
|
|
} |
267
|
14
|
|
|
|
|
36
|
else{ @ax = ($d); } |
268
|
38
|
|
|
|
|
155
|
$self->{titles} = \@ax; |
269
|
|
|
|
|
|
|
} |
270
|
58
|
|
|
|
|
308
|
return 0; |
271
|
|
|
|
|
|
|
} |
272
|
|
|
|
|
|
|
} |
273
|
|
|
|
|
|
|
|
274
|
|
|
|
|
|
|
sub get_insep |
275
|
|
|
|
|
|
|
{ |
276
|
3
|
|
|
3
|
0
|
6
|
my $self = shift; |
277
|
|
|
|
|
|
|
return defined $self->{config}{separator} |
278
|
|
|
|
|
|
|
? $self->{config}{separator} |
279
|
3
|
50
|
|
|
|
29
|
: $default_sep; |
280
|
|
|
|
|
|
|
} |
281
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
sub get_outsep |
283
|
|
|
|
|
|
|
{ |
284
|
1922
|
|
|
1922
|
0
|
2563
|
my $self = shift; |
285
|
|
|
|
|
|
|
return defined $self->{config}{outsep} |
286
|
|
|
|
|
|
|
? $self->{config}{outsep} |
287
|
|
|
|
|
|
|
: ( |
288
|
|
|
|
|
|
|
defined $self->{config}{separator} |
289
|
|
|
|
|
|
|
? $self->{config}{separator} |
290
|
1922
|
100
|
|
|
|
5324
|
: $default_sep |
|
|
100
|
|
|
|
|
|
291
|
|
|
|
|
|
|
); |
292
|
|
|
|
|
|
|
} |
293
|
|
|
|
|
|
|
|
294
|
|
|
|
|
|
|
sub get_end |
295
|
|
|
|
|
|
|
{ |
296
|
2709
|
|
|
2709
|
0
|
3807
|
my $self = shift; |
297
|
|
|
|
|
|
|
return defined $self->{config}{lineend} |
298
|
|
|
|
|
|
|
? $self->{config}{lineend} |
299
|
2709
|
100
|
|
|
|
6838
|
: $default_eol; |
300
|
|
|
|
|
|
|
} |
301
|
|
|
|
|
|
|
|
302
|
|
|
|
|
|
|
sub get_quote |
303
|
|
|
|
|
|
|
{ |
304
|
25
|
|
|
25
|
0
|
42
|
my $self = shift; |
305
|
|
|
|
|
|
|
my $want = defined $self->{config}{quote} |
306
|
|
|
|
|
|
|
? $self->{config}{quote} |
307
|
|
|
|
|
|
|
: ( defined $self->{guessquote} |
308
|
|
|
|
|
|
|
? $self->{guessquote} |
309
|
25
|
100
|
|
|
|
89
|
: $default_quote ); |
|
|
100
|
|
|
|
|
|
310
|
|
|
|
|
|
|
return $want |
311
|
|
|
|
|
|
|
? ( defined $self->{config}{quotechar} |
312
|
|
|
|
|
|
|
? $self->{config}{quotechar} |
313
|
25
|
100
|
|
|
|
108
|
: $default_quotechar ) |
|
|
100
|
|
|
|
|
|
314
|
|
|
|
|
|
|
: ''; |
315
|
|
|
|
|
|
|
} |
316
|
|
|
|
|
|
|
|
317
|
|
|
|
|
|
|
sub get_comchar |
318
|
|
|
|
|
|
|
{ |
319
|
809
|
|
|
809
|
0
|
1090
|
my $self = shift; |
320
|
|
|
|
|
|
|
return defined $self->{config}{comchar} |
321
|
|
|
|
|
|
|
? $self->{config}{comchar} |
322
|
809
|
100
|
|
|
|
1742
|
: $default_comchar; |
323
|
|
|
|
|
|
|
} |
324
|
|
|
|
|
|
|
|
325
|
|
|
|
|
|
|
sub line_data |
326
|
|
|
|
|
|
|
{ |
327
|
1456
|
|
|
1456
|
1
|
2148
|
my $self = shift; |
328
|
1456
|
50
|
|
|
|
2969
|
my $lr = ref $_[0] ? $_[0] : \$_[0]; |
329
|
1456
|
|
|
|
|
2352
|
my @ar = (); |
330
|
1456
|
|
|
|
|
2387
|
my $zahl = $self->{config}{numregex}; |
331
|
|
|
|
|
|
|
# empty lines |
332
|
1456
|
0
|
|
|
|
1961
|
return ($self->{config}{empty} ? [] : undef) if(${$lr} =~ /^$lend$/); |
|
1456
|
50
|
|
|
|
5895
|
|
333
|
1456
|
100
|
|
|
|
3170
|
if($self->{config}{strict}) |
334
|
|
|
|
|
|
|
{ |
335
|
|
|
|
|
|
|
#just split with defined or found separator |
336
|
3
|
|
|
|
|
10
|
@ar = split($self->get_insep(), ${$lr}); |
|
3
|
|
|
|
|
16
|
|
337
|
|
|
|
|
|
|
#remove line end |
338
|
3
|
50
|
|
|
|
12
|
if($#ar > -1){ $ar[$#ar] =~ s/$lend//o; } |
|
3
|
|
|
|
|
22
|
|
339
|
|
|
|
|
|
|
} |
340
|
|
|
|
|
|
|
else |
341
|
|
|
|
|
|
|
{ |
342
|
1453
|
|
|
|
|
1858
|
my $l = ${$lr}; |
|
1453
|
|
|
|
|
2809
|
|
343
|
1453
|
100
|
|
|
|
3112
|
if($self->{config}{black}) |
344
|
|
|
|
|
|
|
{ |
345
|
3
|
|
|
|
|
16
|
$l =~ s/^\s*//; |
346
|
|
|
|
|
|
|
# s/\s*$// deletes the line end -- no problem here |
347
|
3
|
|
|
|
|
28
|
$l =~ s/\s*$//; |
348
|
|
|
|
|
|
|
} |
349
|
1453
|
50
|
|
|
|
7879
|
if($l =~ /^($zahl)(.*)$/){ push(@ar, $1); $l = $2; }else{ return undef; } |
|
1453
|
|
|
|
|
3785
|
|
|
1453
|
|
|
|
|
2642
|
|
|
0
|
|
|
|
|
0
|
|
350
|
1453
|
100
|
|
|
|
2746
|
unless($self->{config}{text}) |
351
|
|
|
|
|
|
|
{ |
352
|
3
|
|
|
|
|
66
|
while($l =~ /^($trenner)($zahl)(.*)$/o) |
353
|
|
|
|
|
|
|
{ |
354
|
6
|
|
|
|
|
16
|
push(@ar, $2); |
355
|
6
|
|
|
|
|
30
|
$l = $3; |
356
|
|
|
|
|
|
|
} |
357
|
|
|
|
|
|
|
} |
358
|
|
|
|
|
|
|
else |
359
|
|
|
|
|
|
|
{ |
360
|
1450
|
|
|
|
|
5371
|
while($l =~ /^($trenner)($ntrenner)(.*)$/o) |
361
|
|
|
|
|
|
|
{ |
362
|
2004
|
|
|
|
|
4052
|
push(@ar, $2); |
363
|
2004
|
|
|
|
|
6199
|
$l = $3; |
364
|
|
|
|
|
|
|
} |
365
|
|
|
|
|
|
|
} |
366
|
|
|
|
|
|
|
} |
367
|
1456
|
|
|
|
|
3976
|
return \@ar; |
368
|
|
|
|
|
|
|
} |
369
|
|
|
|
|
|
|
|
370
|
|
|
|
|
|
|
sub data_line |
371
|
|
|
|
|
|
|
{ |
372
|
1897
|
|
|
1897
|
1
|
2930
|
my $self = shift; |
373
|
1897
|
|
|
|
|
2458
|
my $ar = shift; |
374
|
|
|
|
|
|
|
|
375
|
1897
|
|
|
|
|
2569
|
my $cols_include = shift; |
376
|
1897
|
|
|
|
|
2575
|
my $cols_exclude = shift; |
377
|
1897
|
|
|
|
|
2881
|
my $l = ''; |
378
|
1897
|
|
|
|
|
2849
|
my $zahl = $self->{config}{numregex}; |
379
|
1897
|
|
|
|
|
3202
|
my $end = $self->get_end(); |
380
|
1897
|
|
|
|
|
3521
|
my $sep = $self->get_outsep(); |
381
|
1897
|
|
|
|
|
2972
|
my @vals; |
382
|
|
|
|
|
|
|
my @cols; |
383
|
1897
|
|
|
|
|
2605
|
my $i = -1; |
384
|
|
|
|
|
|
|
|
385
|
1897
|
100
|
|
|
|
3024
|
unless(defined $cols_include) |
386
|
|
|
|
|
|
|
{ |
387
|
1885
|
|
|
|
|
2477
|
@vals = @{$ar}; |
|
1885
|
|
|
|
|
5572
|
|
388
|
1885
|
|
|
|
|
3997
|
@cols = (0..$#vals); |
389
|
|
|
|
|
|
|
} |
390
|
|
|
|
|
|
|
else |
391
|
|
|
|
|
|
|
{ |
392
|
12
|
|
|
|
|
16
|
for my $k (@{$cols_include}) |
|
12
|
|
|
|
|
24
|
|
393
|
|
|
|
|
|
|
{ |
394
|
|
|
|
|
|
|
push(@vals, ($k > -1 and $k < @{$ar}) |
395
|
|
|
|
|
|
|
? $ar->[$k] |
396
|
28
|
50
|
33
|
|
|
56
|
: $self->{config}{fill}); |
397
|
28
|
50
|
|
|
|
62
|
push(@cols, $k > -1 ? $k : 0); # ... for numerformat ... arrg |
398
|
|
|
|
|
|
|
} |
399
|
|
|
|
|
|
|
} |
400
|
|
|
|
|
|
|
|
401
|
1897
|
50
|
|
|
|
3801
|
if(defined $cols_exclude) |
402
|
|
|
|
|
|
|
{ |
403
|
0
|
|
|
|
|
0
|
my @oldvals = @vals; |
404
|
0
|
|
|
|
|
0
|
my @oldcols = @cols; |
405
|
0
|
|
|
|
|
0
|
@vals = (); |
406
|
0
|
|
|
|
|
0
|
@cols = (); |
407
|
0
|
|
|
|
|
0
|
for(my $i=0; $i<=$#oldvals; ++$i) |
408
|
|
|
|
|
|
|
{ |
409
|
0
|
0
|
|
|
|
0
|
if(not grep {$_ == $i} @{$cols_exclude}) |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
410
|
|
|
|
|
|
|
{ |
411
|
0
|
|
|
|
|
0
|
push(@vals, $oldvals[$i]); |
412
|
0
|
|
|
|
|
0
|
push(@cols, $oldcols[$i]); |
413
|
|
|
|
|
|
|
} |
414
|
|
|
|
|
|
|
} |
415
|
|
|
|
|
|
|
} |
416
|
|
|
|
|
|
|
|
417
|
1897
|
100
|
|
|
|
3617
|
if(defined $self->{config}{numformat}->[0]) |
418
|
|
|
|
|
|
|
{ |
419
|
1433
|
|
|
|
|
3124
|
foreach my $i (0..$#vals) |
420
|
|
|
|
|
|
|
{ |
421
|
3586
|
|
|
|
|
6788
|
my $v = $vals[$i]; |
422
|
3586
|
|
|
|
|
4700
|
my $c = $cols[$i]; |
423
|
3586
|
50
|
|
|
|
6402
|
unless(defined $v){ $l .= $sep; next; } |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
424
|
|
|
|
|
|
|
|
425
|
3586
|
|
|
|
|
5842
|
my $numform = $self->{config}{numformat}->[$c]; |
426
|
3586
|
100
|
|
|
|
6610
|
$numform = $self->{config}{numformat}->[0] unless defined $numform; |
427
|
3586
|
50
|
|
|
|
5865
|
if($numform ne '') |
428
|
|
|
|
|
|
|
{ |
429
|
3586
|
|
33
|
|
|
41167
|
$l .= ($v ne '' and $v =~ /^$zahl$/ ? sprintf($numform, $v) : $v).$sep; |
430
|
|
|
|
|
|
|
} |
431
|
0
|
|
|
|
|
0
|
else{ $l .= $v.$sep; } |
432
|
|
|
|
|
|
|
} |
433
|
1433
|
|
|
|
|
7967
|
$l =~ s/$sep$/$end/; |
434
|
|
|
|
|
|
|
} |
435
|
|
|
|
|
|
|
else |
436
|
|
|
|
|
|
|
{ |
437
|
|
|
|
|
|
|
# do I want to care for undefs? |
438
|
|
|
|
|
|
|
# not here ... failure is not communicated from here, you shall handle bad columns externally |
439
|
464
|
|
|
|
|
2551
|
$l = join($sep, @vals).$end; |
440
|
|
|
|
|
|
|
} |
441
|
1897
|
|
|
|
|
9553
|
return \$l; |
442
|
|
|
|
|
|
|
} |
443
|
|
|
|
|
|
|
|
444
|
|
|
|
|
|
|
sub title_line |
445
|
|
|
|
|
|
|
{ |
446
|
25
|
|
|
25
|
1
|
55
|
my $self = shift; |
447
|
25
|
|
|
|
|
48
|
my $cols_include = shift; |
448
|
25
|
|
|
|
|
76
|
my $cols_exclude = shift; |
449
|
|
|
|
|
|
|
|
450
|
25
|
100
|
|
|
|
81
|
my @cols = defined $cols_include ? @{$cols_include} : (0..$#{$self->{titles}}); |
|
3
|
|
|
|
|
8
|
|
|
22
|
|
|
|
|
73
|
|
451
|
25
|
50
|
|
|
|
63
|
if(defined $cols_exclude) |
452
|
|
|
|
|
|
|
{ |
453
|
0
|
|
|
|
|
0
|
my @oldcols = @cols; |
454
|
0
|
|
|
|
|
0
|
@cols = (); |
455
|
0
|
|
|
|
|
0
|
for(my $i=0; $i<=$#oldcols; ++$i) |
456
|
|
|
|
|
|
|
{ |
457
|
0
|
0
|
|
|
|
0
|
if(not grep {$_ == $i} @{$cols_exclude}) |
|
0
|
|
|
|
|
0
|
|
|
0
|
|
|
|
|
0
|
|
458
|
|
|
|
|
|
|
{ |
459
|
0
|
|
|
|
|
0
|
push(@cols, $oldcols[$i]); |
460
|
|
|
|
|
|
|
} |
461
|
|
|
|
|
|
|
} |
462
|
|
|
|
|
|
|
} |
463
|
|
|
|
|
|
|
|
464
|
25
|
|
|
|
|
65
|
my $end = $self->get_end(); |
465
|
25
|
|
|
|
|
71
|
my $sep = $self->get_outsep(); |
466
|
25
|
|
|
|
|
55
|
my $com = $self->get_comchar(); |
467
|
25
|
|
|
|
|
74
|
my $q = $self->get_quote(); |
468
|
25
|
|
|
|
|
64
|
my $l = $com.$q; |
469
|
|
|
|
|
|
|
#print STDERR "titles: @{$self->{titles}}\n"; |
470
|
|
|
|
|
|
|
#print STDERR "titles for @{$ar}\n" if defined $ar; |
471
|
25
|
|
|
|
|
55
|
foreach my $k (@cols) |
472
|
|
|
|
|
|
|
{ |
473
|
|
|
|
|
|
|
# should match for title containing $q |
474
|
172
|
50
|
|
|
|
397
|
my $t = $k > -1 ? $self->{titles}->[$k] : undef; |
475
|
172
|
50
|
|
|
|
339
|
$t = "" unless defined $t; |
476
|
172
|
|
|
|
|
507
|
$l .= $t.$q.$sep.$q; |
477
|
|
|
|
|
|
|
} |
478
|
25
|
|
|
|
|
261
|
$l =~ s/$q$//; |
479
|
25
|
|
|
|
|
215
|
$l =~ s/$sep$/$end/; |
480
|
25
|
|
|
|
|
152
|
return \$l; |
481
|
|
|
|
|
|
|
} |
482
|
|
|
|
|
|
|
|
483
|
|
|
|
|
|
|
sub comment_line |
484
|
|
|
|
|
|
|
{ |
485
|
784
|
|
|
784
|
1
|
1039
|
my $self = shift; |
486
|
784
|
100
|
|
|
|
1428
|
my $line = ref $_[0] ? $_[0] : \$_[0]; |
487
|
784
|
|
|
|
|
1267
|
my $cline = $self->get_comchar().${$line}.$self->get_end(); |
|
784
|
|
|
|
|
1741
|
|
488
|
784
|
|
|
|
|
2669
|
return \$cline; |
489
|
|
|
|
|
|
|
} |
490
|
|
|
|
|
|
|
|
491
|
|
|
|
|
|
|
sub chomp_line |
492
|
|
|
|
|
|
|
{ |
493
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
494
|
0
|
0
|
|
|
|
0
|
my $string = ref $_[0] ? $_[0] : \$_[0]; |
495
|
0
|
0
|
|
|
|
0
|
if(defined $string) |
496
|
|
|
|
|
|
|
{ |
497
|
0
|
|
|
|
|
0
|
${$string} =~ s/$lend$//; |
|
0
|
|
|
|
|
0
|
|
498
|
|
|
|
|
|
|
} |
499
|
|
|
|
|
|
|
} |
500
|
|
|
|
|
|
|
|
501
|
|
|
|
|
|
|
sub make_naked |
502
|
|
|
|
|
|
|
{ |
503
|
35
|
|
|
35
|
1
|
62
|
my $self = shift; |
504
|
35
|
50
|
|
|
|
155
|
my $string = ref $_[0] ? $_[0] : \$_[0]; |
505
|
35
|
50
|
|
|
|
87
|
if(defined $string) |
506
|
|
|
|
|
|
|
{ |
507
|
35
|
|
|
|
|
53
|
${$string} =~ s/$lend$//; |
|
35
|
|
|
|
|
259
|
|
508
|
35
|
|
|
|
|
71
|
${$string} =~ s/^$self->{config}{comregex}//; |
|
35
|
|
|
|
|
299
|
|
509
|
|
|
|
|
|
|
} |
510
|
|
|
|
|
|
|
} |
511
|
|
|
|
|
|
|
|
512
|
|
|
|
|
|
|
# Not well supported, but possible: Text in between numeric data. |
513
|
|
|
|
|
|
|
# To make it a bit safer, this filter will replace everything that would count as separator. |
514
|
|
|
|
|
|
|
# It's only a bit safer... supsequent parsers are supposed to work in strict mode if we're in strict mode here. |
515
|
|
|
|
|
|
|
sub filter_text |
516
|
|
|
|
|
|
|
{ |
517
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
518
|
0
|
|
|
|
|
|
my $match; |
519
|
0
|
0
|
|
|
|
|
if($self->{config}{strict}) |
520
|
|
|
|
|
|
|
{ |
521
|
0
|
|
|
|
|
|
my $sep = $self->get_outsep(); |
522
|
0
|
|
|
|
|
|
$match = qr/$sep/; |
523
|
|
|
|
|
|
|
} |
524
|
|
|
|
|
|
|
else |
525
|
|
|
|
|
|
|
{ |
526
|
0
|
|
|
|
|
|
$match = qr/$trenner/; |
527
|
|
|
|
|
|
|
} |
528
|
0
|
|
|
|
|
|
for(@_){ s:$match:_:g; } |
|
0
|
|
|
|
|
|
|
529
|
|
|
|
|
|
|
} |
530
|
|
|
|
|
|
|
|
531
|
|
|
|
|
|
|
1; |
532
|
|
|
|
|
|
|
|
533
|
|
|
|
|
|
|
__END__ |