-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathCSV_XS.pm
5348 lines (4014 loc) · 152 KB
/
CSV_XS.pm
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package Text::CSV_XS;
# Copyright (c) 2007-2025 H.Merijn Brand. All rights reserved.
# Copyright (c) 1998-2001 Jochen Wiedmann. All rights reserved.
# Copyright (c) 1997 Alan Citterman. All rights reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.
# HISTORY
#
# 0.24 - H.Merijn Brand <[email protected]>
# 0.10 - 0.23 Jochen Wiedmann <[email protected]>
# Based on (the original) Text::CSV by Alan Citterman <[email protected]>
require 5.006001;
use strict;
use warnings;
require Exporter;
use XSLoader;
use Carp;
use vars qw( $VERSION @ISA @EXPORT_OK %EXPORT_TAGS );
$VERSION = "1.61";
@ISA = qw( Exporter );
XSLoader::load ("Text::CSV_XS", $VERSION);
sub PV { 0 } sub CSV_TYPE_PV { PV }
sub IV { 1 } sub CSV_TYPE_IV { IV }
sub NV { 2 } sub CSV_TYPE_NV { NV }
sub CSV_FLAGS_IS_QUOTED { 0x0001 }
sub CSV_FLAGS_IS_BINARY { 0x0002 }
sub CSV_FLAGS_ERROR_IN_FIELD { 0x0004 }
sub CSV_FLAGS_IS_MISSING { 0x0010 }
%EXPORT_TAGS = (
CONSTANTS => [qw(
CSV_FLAGS_IS_QUOTED
CSV_FLAGS_IS_BINARY
CSV_FLAGS_ERROR_IN_FIELD
CSV_FLAGS_IS_MISSING
CSV_TYPE_PV
CSV_TYPE_IV
CSV_TYPE_NV
)],
);
@EXPORT_OK = (qw( csv PV IV NV ), @{$EXPORT_TAGS{'CONSTANTS'}});
if ($] < 5.008002) {
no warnings "redefine";
*utf8::decode = sub {};
}
# version
#
# class/object method expecting no arguments and returning the version
# number of Text::CSV. there are no side-effects.
sub version {
return $VERSION;
} # version
# new
#
# class/object method expecting no arguments and returning a reference to
# a newly created Text::CSV object.
my %def_attr = (
'eol' => '',
'sep_char' => ',',
'quote_char' => '"',
'escape_char' => '"',
'binary' => 0,
'decode_utf8' => 1,
'auto_diag' => 0,
'diag_verbose' => 0,
'strict' => 0,
'strict_eol' => 0,
'blank_is_undef' => 0,
'empty_is_undef' => 0,
'allow_whitespace' => 0,
'allow_loose_quotes' => 0,
'allow_loose_escapes' => 0,
'allow_unquoted_escape' => 0,
'always_quote' => 0,
'quote_empty' => 0,
'quote_space' => 1,
'quote_binary' => 1,
'escape_null' => 1,
'keep_meta_info' => 0,
'verbatim' => 0,
'formula' => 0,
'skip_empty_rows' => 0,
'undef_str' => undef,
'comment_str' => undef,
'types' => undef,
'callbacks' => undef,
'_EOF' => "",
'_RECNO' => 0,
'_STATUS' => undef,
'_FIELDS' => undef,
'_FFLAGS' => undef,
'_STRING' => undef,
'_ERROR_INPUT' => undef,
'_COLUMN_NAMES' => undef,
'_BOUND_COLUMNS' => undef,
'_AHEAD' => undef,
'_FORMULA_CB' => undef,
'_EMPTROW_CB' => undef,
'ENCODING' => undef,
);
my %attr_alias = (
'quote_always' => "always_quote",
'verbose_diag' => "diag_verbose",
'quote_null' => "escape_null",
'escape' => "escape_char",
'comment' => "comment_str",
);
my $last_err = Text::CSV_XS->SetDiag (0);
my $ebcdic = ord ("A") == 0xC1; # Faster than $Config{'ebcdic'}
my @internal_kh;
# NOT a method: is also used before bless
sub _unhealthy_whitespace {
my ($self, $aw) = @_;
$aw or return 0; # no checks needed without allow_whitespace
my $quo = $self->{'quote'};
defined $quo && length ($quo) or $quo = $self->{'quote_char'};
my $esc = $self->{'escape_char'};
defined $quo && $quo =~ m/^[ \t]/ and return 1002;
defined $esc && $esc =~ m/^[ \t]/ and return 1002;
return 0;
} # _unhealty_whitespace
sub _check_sanity {
my $self = shift;
my $eol = $self->{'eol'};
my $sep = $self->{'sep'};
defined $sep && length ($sep) or $sep = $self->{'sep_char'};
my $quo = $self->{'quote'};
defined $quo && length ($quo) or $quo = $self->{'quote_char'};
my $esc = $self->{'escape_char'};
# use DP;::diag ("SEP: '", DPeek ($sep),
# "', QUO: '", DPeek ($quo),
# "', ESC: '", DPeek ($esc),"'");
# sep_char should not be undefined
$sep ne "" or return 1008;
length ($sep) > 16 and return 1006;
$sep =~ m/[\r\n]/ and return 1003;
if (defined $quo) {
$quo eq $sep and return 1001;
length ($quo) > 16 and return 1007;
$quo =~ m/[\r\n]/ and return 1003;
}
if (defined $esc) {
$esc eq $sep and return 1001;
$esc =~ m/[\r\n]/ and return 1003;
}
if (defined $eol) {
length ($eol) > 16 and return 1005;
}
return _unhealthy_whitespace ($self, $self->{'allow_whitespace'});
} # _check_sanity
sub known_attributes {
sort grep !m/^_/ => "sep", "quote", keys %def_attr;
} # known_attributes
sub new {
$last_err = Text::CSV_XS->SetDiag (1000,
"usage: my \$csv = Text::CSV_XS->new ([{ option => value, ... }]);");
my $proto = shift;
my $class = ref $proto || $proto or return;
@_ > 0 && ref $_[0] ne "HASH" and return;
my $attr = shift || {};
my %attr = map {
my $k = m/^[a-zA-Z]\w+$/ ? lc $_ : $_;
exists $attr_alias{$k} and $k = $attr_alias{$k};
($k => $attr->{$_});
} keys %{$attr};
my $sep_aliased = 0;
if (exists $attr{'sep'}) {
$attr{'sep_char'} = delete $attr{'sep'};
$sep_aliased = 1;
}
my $quote_aliased = 0;
if (exists $attr{'quote'}) {
$attr{'quote_char'} = delete $attr{'quote'};
$quote_aliased = 1;
}
exists $attr{'formula_handling'} and
$attr{'formula'} = delete $attr{'formula_handling'};
my $attr_formula = delete $attr{'formula'};
for (keys %attr) {
if (m/^[a-z]/ && exists $def_attr{$_}) {
# uncoverable condition false
defined $attr{$_} && m/_char$/ and utf8::decode ($attr{$_});
next;
}
# croak?
$last_err = Text::CSV_XS->SetDiag (1000, "INI - Unknown attribute '$_'");
$attr{'auto_diag'} and error_diag ();
return;
}
if ($sep_aliased) {
my @b = unpack "U0C*", $attr{'sep_char'};
if (@b > 1) {
$attr{'sep'} = $attr{'sep_char'};
$attr{'sep_char'} = "\0";
}
else {
$attr{'sep'} = undef;
}
}
if ($quote_aliased and defined $attr{'quote_char'}) {
my @b = unpack "U0C*", $attr{'quote_char'};
if (@b > 1) {
$attr{'quote'} = $attr{'quote_char'};
$attr{'quote_char'} = "\0";
}
else {
$attr{'quote'} = undef;
}
}
my $self = { %def_attr, %attr };
if (my $ec = _check_sanity ($self)) {
$last_err = Text::CSV_XS->SetDiag ($ec);
$attr{'auto_diag'} and error_diag ();
return;
}
if (defined $self->{'callbacks'} && ref $self->{'callbacks'} ne "HASH") {
carp ("The 'callbacks' attribute is set but is not a hash: ignored\n");
$self->{'callbacks'} = undef;
}
$last_err = Text::CSV_XS->SetDiag (0);
defined $\ && !exists $attr{'eol'} and $self->{'eol'} = $\;
bless $self, $class;
defined $self->{'types'} and $self->types ($self->{'types'});
defined $self->{'skip_empty_rows'} and $self->{'skip_empty_rows'} = _supported_skip_empty_rows ($self, $self->{'skip_empty_rows'});
defined $attr_formula and $self->{'formula'} = _supported_formula ($self, $attr_formula);
$self;
} # new
# Keep in sync with XS!
my %_cache_id = ( # Only expose what is accessed from within PM
'quote_char' => 0,
'escape_char' => 1,
'sep_char' => 2,
'always_quote' => 4,
'quote_empty' => 5,
'quote_space' => 6,
'quote_binary' => 7,
'allow_loose_quotes' => 8,
'allow_loose_escapes' => 9,
'allow_unquoted_escape' => 10,
'allow_whitespace' => 11,
'blank_is_undef' => 12,
'empty_is_undef' => 13,
'auto_diag' => 14,
'diag_verbose' => 15,
'escape_null' => 16,
'formula' => 18,
'decode_utf8' => 21,
'verbatim' => 23,
'strict_eol' => 24,
'strict' => 28,
'skip_empty_rows' => 29,
'binary' => 30,
'keep_meta_info' => 31,
'_has_hooks' => 32,
'_has_ahead' => 33,
'_is_bound' => 44,
'eol' => 100,
'sep' => 116,
'quote' => 132,
'undef_str' => 148,
'comment_str' => 156,
'types' => 92,
);
# A `character'
sub _set_attr_C {
my ($self, $name, $val, $ec) = @_;
defined $val and utf8::decode ($val);
$self->{$name} = $val;
$ec = _check_sanity ($self) and croak ($self->SetDiag ($ec));
$self->_cache_set ($_cache_id{$name}, $val);
} # _set_attr_C
# A flag
sub _set_attr_X {
my ($self, $name, $val) = @_;
defined $val or $val = 0;
$self->{$name} = $val;
$self->_cache_set ($_cache_id{$name}, 0 + $val);
} # _set_attr_X
# A number
sub _set_attr_N {
my ($self, $name, $val) = @_;
$self->{$name} = $val;
$self->_cache_set ($_cache_id{$name}, 0 + $val);
} # _set_attr_N
# Accessor methods.
# It is unwise to change them halfway through a single file!
sub quote_char {
my $self = shift;
if (@_) {
$self->_set_attr_C ("quote_char", shift);
$self->_cache_set ($_cache_id{'quote'}, "");
}
$self->{'quote_char'};
} # quote_char
sub quote {
my $self = shift;
if (@_) {
my $quote = shift;
defined $quote or $quote = "";
utf8::decode ($quote);
my @b = unpack "U0C*", $quote;
if (@b > 1) {
@b > 16 and croak ($self->SetDiag (1007));
$self->quote_char ("\0");
}
else {
$self->quote_char ($quote);
$quote = "";
}
$self->{'quote'} = $quote;
my $ec = _check_sanity ($self);
$ec and croak ($self->SetDiag ($ec));
$self->_cache_set ($_cache_id{'quote'}, $quote);
}
my $quote = $self->{'quote'};
defined $quote && length ($quote) ? $quote : $self->{'quote_char'};
} # quote
sub escape_char {
my $self = shift;
if (@_) {
my $ec = shift;
$self->_set_attr_C ("escape_char", $ec);
$ec or $self->_set_attr_X ("escape_null", 0);
}
$self->{'escape_char'};
} # escape_char
sub sep_char {
my $self = shift;
if (@_) {
$self->_set_attr_C ("sep_char", shift);
$self->_cache_set ($_cache_id{'sep'}, "");
}
$self->{'sep_char'};
} # sep_char
sub sep {
my $self = shift;
if (@_) {
my $sep = shift;
defined $sep or $sep = "";
utf8::decode ($sep);
my @b = unpack "U0C*", $sep;
if (@b > 1) {
@b > 16 and croak ($self->SetDiag (1006));
$self->sep_char ("\0");
}
else {
$self->sep_char ($sep);
$sep = "";
}
$self->{'sep'} = $sep;
my $ec = _check_sanity ($self);
$ec and croak ($self->SetDiag ($ec));
$self->_cache_set ($_cache_id{'sep'}, $sep);
}
my $sep = $self->{'sep'};
defined $sep && length ($sep) ? $sep : $self->{'sep_char'};
} # sep
sub eol {
my $self = shift;
if (@_) {
my $eol = shift;
defined $eol or $eol = ""; # Also reset strict_eol?
length ($eol) > 16 and croak ($self->SetDiag (1005));
$self->{'eol'} = $eol;
$self->_cache_set ($_cache_id{'eol'}, $eol);
}
$self->{'eol'};
} # eol
sub eol_type {
my $self = shift;
$self->_cache_get_eolt;
} # eol_type
sub always_quote {
my $self = shift;
@_ and $self->_set_attr_X ("always_quote", shift);
$self->{'always_quote'};
} # always_quote
sub quote_space {
my $self = shift;
@_ and $self->_set_attr_X ("quote_space", shift);
$self->{'quote_space'};
} # quote_space
sub quote_empty {
my $self = shift;
@_ and $self->_set_attr_X ("quote_empty", shift);
$self->{'quote_empty'};
} # quote_empty
sub escape_null {
my $self = shift;
@_ and $self->_set_attr_X ("escape_null", shift);
$self->{'escape_null'};
} # escape_null
sub quote_null { goto &escape_null; }
sub quote_binary {
my $self = shift;
@_ and $self->_set_attr_X ("quote_binary", shift);
$self->{'quote_binary'};
} # quote_binary
sub binary {
my $self = shift;
@_ and $self->_set_attr_X ("binary", shift);
$self->{'binary'};
} # binary
sub strict {
my $self = shift;
@_ and $self->_set_attr_X ("strict", shift);
$self->{'strict'};
} # strict
sub strict_eol {
my $self = shift;
@_ and $self->_set_attr_X ("strict_eol", shift);
$self->{'strict_eol'};
} # strict_eol
sub _supported_skip_empty_rows {
my ($self, $f) = @_;
defined $f or return 0;
if ($self && $f && ref $f && ref $f eq "CODE") {
$self->{'_EMPTROW_CB'} = $f;
return 6;
}
$f =~ m/^(?: 0 | undef )$/xi ? 0 :
$f =~ m/^(?: 1 | skip )$/xi ? 1 :
$f =~ m/^(?: 2 | eof | stop )$/xi ? 2 :
$f =~ m/^(?: 3 | die )$/xi ? 3 :
$f =~ m/^(?: 4 | croak )$/xi ? 4 :
$f =~ m/^(?: 5 | error )$/xi ? 5 :
$f =~ m/^(?: 6 | cb )$/xi ? 6 : do {
$self ||= "Text::CSV_XS";
croak ($self->_SetDiagInfo (1500, "skip_empty_rows '$f' is not supported"));
};
} # _supported_skip_empty_rows
sub skip_empty_rows {
my $self = shift;
@_ and $self->_set_attr_N ("skip_empty_rows", _supported_skip_empty_rows ($self, shift));
my $ser = $self->{'skip_empty_rows'};
$ser == 6 or $self->{'_EMPTROW_CB'} = undef;
$ser <= 1 ? $ser : $ser == 2 ? "eof" : $ser == 3 ? "die" :
$ser == 4 ? "croak" : $ser == 5 ? "error" :
$self->{'_EMPTROW_CB'};
} # skip_empty_rows
sub _SetDiagInfo {
my ($self, $err, $msg) = @_;
$self->SetDiag ($err);
my $em = $self->error_diag ();
$em =~ s/^\d+$// and $msg =~ s/^/# /;
my $sep = $em =~ m/[;\n]$/ ? "\n\t" : ": ";
join $sep => grep m/\S\S\S/ => $em, $msg;
} # _SetDiagInfo
sub _supported_formula {
my ($self, $f) = @_;
defined $f or return 5;
if ($self && $f && ref $f && ref $f eq "CODE") {
$self->{'_FORMULA_CB'} = $f;
return 6;
}
$f =~ m/^(?: 0 | none )$/xi ? 0 :
$f =~ m/^(?: 1 | die )$/xi ? 1 :
$f =~ m/^(?: 2 | croak )$/xi ? 2 :
$f =~ m/^(?: 3 | diag )$/xi ? 3 :
$f =~ m/^(?: 4 | empty | )$/xi ? 4 :
$f =~ m/^(?: 5 | undef )$/xi ? 5 :
$f =~ m/^(?: 6 | cb )$/xi ? 6 : do {
$self ||= "Text::CSV_XS";
croak ($self->_SetDiagInfo (1500, "formula-handling '$f' is not supported"));
};
} # _supported_formula
sub formula {
my $self = shift;
@_ and $self->_set_attr_N ("formula", _supported_formula ($self, shift));
$self->{'formula'} == 6 or $self->{'_FORMULA_CB'} = undef;
[qw( none die croak diag empty undef cb )]->[_supported_formula ($self, $self->{'formula'})];
} # formula
sub formula_handling {
my $self = shift;
$self->formula (@_);
} # formula_handling
sub decode_utf8 {
my $self = shift;
@_ and $self->_set_attr_X ("decode_utf8", shift);
$self->{'decode_utf8'};
} # decode_utf8
sub keep_meta_info {
my $self = shift;
if (@_) {
my $v = shift;
!defined $v || $v eq "" and $v = 0;
$v =~ m/^[0-9]/ or $v = lc $v eq "false" ? 0 : 1; # true/truth = 1
$self->_set_attr_X ("keep_meta_info", $v);
}
$self->{'keep_meta_info'};
} # keep_meta_info
sub allow_loose_quotes {
my $self = shift;
@_ and $self->_set_attr_X ("allow_loose_quotes", shift);
$self->{'allow_loose_quotes'};
} # allow_loose_quotes
sub allow_loose_escapes {
my $self = shift;
@_ and $self->_set_attr_X ("allow_loose_escapes", shift);
$self->{'allow_loose_escapes'};
} # allow_loose_escapes
sub allow_whitespace {
my $self = shift;
if (@_) {
my $aw = shift;
_unhealthy_whitespace ($self, $aw) and
croak ($self->SetDiag (1002));
$self->_set_attr_X ("allow_whitespace", $aw);
}
$self->{'allow_whitespace'};
} # allow_whitespace
sub allow_unquoted_escape {
my $self = shift;
@_ and $self->_set_attr_X ("allow_unquoted_escape", shift);
$self->{'allow_unquoted_escape'};
} # allow_unquoted_escape
sub blank_is_undef {
my $self = shift;
@_ and $self->_set_attr_X ("blank_is_undef", shift);
$self->{'blank_is_undef'};
} # blank_is_undef
sub empty_is_undef {
my $self = shift;
@_ and $self->_set_attr_X ("empty_is_undef", shift);
$self->{'empty_is_undef'};
} # empty_is_undef
sub verbatim {
my $self = shift;
@_ and $self->_set_attr_X ("verbatim", shift);
$self->{'verbatim'};
} # verbatim
sub undef_str {
my $self = shift;
if (@_) {
my $v = shift;
$self->{'undef_str'} = defined $v ? "$v" : undef;
$self->_cache_set ($_cache_id{'undef_str'}, $self->{'undef_str'});
}
$self->{'undef_str'};
} # undef_str
sub comment_str {
my $self = shift;
if (@_) {
my $v = shift;
$self->{'comment_str'} = defined $v ? "$v" : undef;
$self->_cache_set ($_cache_id{'comment_str'}, $self->{'comment_str'});
}
$self->{'comment_str'};
} # comment_str
sub auto_diag {
my $self = shift;
if (@_) {
my $v = shift;
!defined $v || $v eq "" and $v = 0;
$v =~ m/^[0-9]/ or $v = lc $v eq "false" ? 0 : 1; # true/truth = 1
$self->_set_attr_X ("auto_diag", $v);
}
$self->{'auto_diag'};
} # auto_diag
sub diag_verbose {
my $self = shift;
if (@_) {
my $v = shift;
!defined $v || $v eq "" and $v = 0;
$v =~ m/^[0-9]/ or $v = lc $v eq "false" ? 0 : 1; # true/truth = 1
$self->_set_attr_X ("diag_verbose", $v);
}
$self->{'diag_verbose'};
} # diag_verbose
# status
#
# object method returning the success or failure of the most recent
# combine () or parse (). there are no side-effects.
sub status {
my $self = shift;
return $self->{'_STATUS'};
} # status
sub eof {
my $self = shift;
return $self->{'_EOF'};
} # eof
sub types {
my $self = shift;
if (@_) {
if (my $types = shift) {
$self->{'_types'} = join "", map { chr } @{$types};
$self->{'types'} = $types;
$self->_cache_set ($_cache_id{'types'}, $self->{'_types'});
}
else {
delete $self->{'types'};
delete $self->{'_types'};
$self->_cache_set ($_cache_id{'types'}, undef);
undef;
}
}
else {
$self->{'types'};
}
} # types
sub callbacks {
my $self = shift;
if (@_) {
my $cb;
my $hf = 0x00;
if (defined $_[0]) {
grep { !defined } @_ and croak ($self->SetDiag (1004));
$cb = @_ == 1 && ref $_[0] eq "HASH" ? shift
: @_ % 2 == 0 ? { @_ }
: croak ($self->SetDiag (1004));
foreach my $cbk (keys %{$cb}) {
# A key cannot be a ref. That would be stored as the *string
# 'SCALAR(0x1f3e710)' or 'ARRAY(0x1a5ae18)'
$cbk =~ m/^[\w.]+$/ && ref $cb->{$cbk} eq "CODE" or
croak ($self->SetDiag (1004));
}
exists $cb->{'error'} and $hf |= 0x01;
exists $cb->{'after_parse'} and $hf |= 0x02;
exists $cb->{'before_print'} and $hf |= 0x04;
}
elsif (@_ > 1) {
# (undef, whatever)
croak ($self->SetDiag (1004));
}
$self->_set_attr_X ("_has_hooks", $hf);
$self->{'callbacks'} = $cb;
}
$self->{'callbacks'};
} # callbacks
# error_diag
#
# If (and only if) an error occurred, this function returns a code that
# indicates the reason of failure
sub error_diag {
my $self = shift;
my @diag = (0 + $last_err, $last_err, 0, 0, 0, 0);
# Docs state to NEVER use UNIVERSAL::isa, because it will *never* call an
# overridden isa method in any class. Well, that is exacly what I want here
if ($self && ref $self and # Not a class method or direct call
UNIVERSAL::isa ($self, __PACKAGE__) && exists $self->{'_ERROR_DIAG'}) {
$diag[0] = 0 + $self->{'_ERROR_DIAG'};
$diag[1] = $self->{'_ERROR_DIAG'};
$diag[2] = 1 + $self->{'_ERROR_POS'} if exists $self->{'_ERROR_POS'};
$diag[3] = $self->{'_RECNO'};
$diag[4] = $self->{'_ERROR_FLD'} if exists $self->{'_ERROR_FLD'};
$diag[5] = $self->{'_ERROR_SRC'} if exists $self->{'_ERROR_SRC'} && $self->{'diag_verbose'};
$diag[0] && $self->{'callbacks'} && $self->{'callbacks'}{'error'} and
return $self->{'callbacks'}{'error'}->(@diag);
}
my $context = wantarray;
unless (defined $context) { # Void context, auto-diag
if ($diag[0] && $diag[0] != 2012) {
my $msg = "# CSV_XS ERROR: $diag[0] - $diag[1] \@ rec $diag[3] pos $diag[2]\n";
$diag[4] and $msg =~ s/$/ field $diag[4]/;
$diag[5] and $msg =~ s/$/ (XS#$diag[5])/;
unless ($self && ref $self) { # auto_diag
# called without args in void context
warn $msg;
return;
}
$self->{'diag_verbose'} && $self->{'_ERROR_INPUT'} and
$msg .= $self->{'_ERROR_INPUT'}."\n".
(" " x ($diag[2] - 1))."^\n";
my $lvl = $self->{'auto_diag'};
if ($lvl < 2) {
my @c = caller (2);
if (@c >= 11 && $c[10] && ref $c[10] eq "HASH") {
my $hints = $c[10];
(exists $hints->{'autodie'} && $hints->{'autodie'} or
exists $hints->{'guard Fatal'} &&
!exists $hints->{'no Fatal'}) and
$lvl++;
# Future releases of autodie will probably set $^H{autodie}
# to "autodie @args", like "autodie :all" or "autodie open"
# so we can/should check for "open" or "new"
}
}
$lvl > 1 ? die $msg : warn $msg;
}
return;
}
return $context ? @diag : $diag[1];
} # error_diag
sub record_number {
my $self = shift;
return $self->{'_RECNO'};
} # record_number
# string
#
# object method returning the result of the most recent combine () or the
# input to the most recent parse (), whichever is more recent. there are
# no side-effects.
sub string {
my $self = shift;
return ref $self->{'_STRING'} ? ${$self->{'_STRING'}} : undef;
} # string
# fields
#
# object method returning the result of the most recent parse () or the
# input to the most recent combine (), whichever is more recent. there
# are no side-effects.
sub fields {
my $self = shift;
return ref $self->{'_FIELDS'} ? @{$self->{'_FIELDS'}} : undef;
} # fields
# meta_info
#
# object method returning the result of the most recent parse () or the
# input to the most recent combine (), whichever is more recent. there
# are no side-effects. meta_info () returns (if available) some of the
# field's properties
sub meta_info {
my $self = shift;
return ref $self->{'_FFLAGS'} ? @{$self->{'_FFLAGS'}} : undef;
} # meta_info
sub is_quoted {
my ($self, $idx) = @_;
ref $self->{'_FFLAGS'} &&
$idx >= 0 && $idx < @{$self->{'_FFLAGS'}} or return;
$self->{'_FFLAGS'}[$idx] & CSV_FLAGS_IS_QUOTED () ? 1 : 0;
} # is_quoted
sub is_binary {
my ($self, $idx) = @_;
ref $self->{'_FFLAGS'} &&
$idx >= 0 && $idx < @{$self->{'_FFLAGS'}} or return;
$self->{'_FFLAGS'}[$idx] & CSV_FLAGS_IS_BINARY () ? 1 : 0;
} # is_binary
sub is_missing {
my ($self, $idx) = @_;
$idx < 0 || !ref $self->{'_FFLAGS'} and return;
$idx >= @{$self->{'_FFLAGS'}} and return 1;
$self->{'_FFLAGS'}[$idx] & CSV_FLAGS_IS_MISSING () ? 1 : 0;
} # is_missing
# combine
#
# Object method returning success or failure. The given arguments are
# combined into a single comma-separated value. Failure can be the
# result of no arguments or an argument containing an invalid character.
# side-effects include:
# setting status ()
# setting fields ()
# setting string ()
# setting error_input ()
sub combine {
my $self = shift;
my $str = "";
$self->{'_FIELDS'} = \@_;
$self->{'_STATUS'} = (@_ > 0) && $self->Combine (\$str, \@_, 0);
$self->{'_STRING'} = \$str;
$self->{'_STATUS'};
} # combine
# parse
#
# Object method returning success or failure. The given argument is
# expected to be a valid comma-separated value. Failure can be the
# result of no arguments or an argument containing an invalid sequence
# of characters. Side-effects include:
# setting status ()
# setting fields ()
# setting meta_info ()
# setting string ()
# setting error_input ()
sub parse {
my ($self, $str) = @_;
ref $str and croak ($self->SetDiag (1500));
my $fields = [];
my $fflags = [];
$self->{'_STRING'} = \$str;
if (defined $str && $self->Parse ($str, $fields, $fflags)) {
$self->{'_FIELDS'} = $fields;
$self->{'_FFLAGS'} = $fflags;
$self->{'_STATUS'} = 1;
}
else {
$self->{'_FIELDS'} = undef;
$self->{'_FFLAGS'} = undef;
$self->{'_STATUS'} = 0;
}
$self->{'_STATUS'};
} # parse
sub column_names {
my ($self, @keys) = @_;
@keys or
return defined $self->{'_COLUMN_NAMES'} ? @{$self->{'_COLUMN_NAMES'}} : ();
@keys == 1 && ! defined $keys[0] and
return $self->{'_COLUMN_NAMES'} = undef;
if (@keys == 1 && ref $keys[0] eq "ARRAY") {
@keys = @{$keys[0]};
}
elsif (join "", map { defined $_ ? ref $_ : "" } @keys) {
croak ($self->SetDiag (3001));
}
$self->{'_BOUND_COLUMNS'} && @keys != @{$self->{'_BOUND_COLUMNS'}} and
croak ($self->SetDiag (3003));
$self->{'_COLUMN_NAMES'} = [ map { defined $_ ? $_ : "\cAUNDEF\cA" } @keys ];
@{$self->{'_COLUMN_NAMES'}};
} # column_names
sub header {
my ($self, $fh, @args) = @_;
$fh or croak ($self->SetDiag (1014));
my (@seps, %args);
for (@args) {
if (ref $_ eq "ARRAY") {
push @seps, @{$_};
next;
}
if (ref $_ eq "HASH") {
%args = %{$_};
next;
}
croak ('usage: $csv->header ($fh, [ seps ], { options })');
}
defined $args{'munge'} && !defined $args{'munge_column_names'} and
$args{'munge_column_names'} = $args{'munge'}; # munge as alias
defined $args{'detect_bom'} or $args{'detect_bom'} = 1;
defined $args{'set_column_names'} or $args{'set_column_names'} = 1;
defined $args{'munge_column_names'} or $args{'munge_column_names'} = "lc";
# Reset any previous leftovers
$self->{'_RECNO'} = 0;
$self->{'_AHEAD'} = undef;
$self->{'_COLUMN_NAMES'} = undef if $args{'set_column_names'};
$self->{'_BOUND_COLUMNS'} = undef if $args{'set_column_names'};
if (defined $args{'sep_set'}) {
ref $args{'sep_set'} eq "ARRAY" or
croak ($self->_SetDiagInfo (1500, "sep_set should be an array ref"));
@seps = @{$args{'sep_set'}};
}
$^O eq "MSWin32" and binmode $fh;
my $hdr = <$fh>;
# check if $hdr can be empty here, I don't think so
defined $hdr && $hdr ne "" or croak ($self->SetDiag (1010));
my %sep;
@seps or @seps = (",", ";");
foreach my $sep (@seps) {
index ($hdr, $sep) >= 0 and $sep{$sep}++;
}
keys %sep >= 2 and croak ($self->SetDiag (1011));
$self->sep (keys %sep);
my $enc = "";
if ($args{'detect_bom'}) { # UTF-7 is not supported
if ($hdr =~ s/^\x00\x00\xfe\xff//) { $enc = "utf-32be" }
elsif ($hdr =~ s/^\xff\xfe\x00\x00//) { $enc = "utf-32le" }
elsif ($hdr =~ s/^\xfe\xff//) { $enc = "utf-16be" }
elsif ($hdr =~ s/^\xff\xfe//) { $enc = "utf-16le" }
elsif ($hdr =~ s/^\xef\xbb\xbf//) { $enc = "utf-8" }
elsif ($hdr =~ s/^\xf7\x64\x4c//) { $enc = "utf-1" }
elsif ($hdr =~ s/^\xdd\x73\x66\x73//) { $enc = "utf-ebcdic" }
elsif ($hdr =~ s/^\x0e\xfe\xff//) { $enc = "scsu" }
elsif ($hdr =~ s/^\xfb\xee\x28//) { $enc = "bocu-1" }
elsif ($hdr =~ s/^\x84\x31\x95\x33//) { $enc = "gb-18030" }
elsif ($hdr =~ s/^\x{feff}//) { $enc = "" }
$self->{'ENCODING'} = $enc ? uc $enc : undef;
$hdr eq "" and croak ($self->SetDiag (1010));
if ($enc) {
$ebcdic && $enc eq "utf-ebcdic" and $enc = "";
if ($enc =~ m/([13]).le$/) {
my $l = 0 + $1;
my $x;
$hdr .= "\0" x $l;
read $fh, $x, $l;
}
if ($enc) {
if ($enc ne "utf-8") {
require Encode;
$hdr = Encode::decode ($enc, $hdr);
}
binmode $fh, ":encoding($enc)";
}
}
}
my ($ahead, $eol);
if ($hdr and $hdr =~ s/\Asep=(\S)([\r\n]+)//i) { # Also look in xs:Parse
$self->sep ($1);
length $hdr or $hdr = <$fh>;
}
if ($hdr =~ s/^([^\r\n]+)([\r\n]+)([^\r\n].+)\z/$1/s) {