-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathCSV_XS.xs
2770 lines (2455 loc) · 75.7 KB
/
CSV_XS.xs
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
/* Copyright (c) 2007-2025 H.Merijn Brand. All rights reserved.
* Copyright (c) 1998-2001 Jochen Wiedmann. All rights reserved.
* This program is free software; you can redistribute it and/or
* modify it under the same terms as Perl itself.
*/
#define PERL_NO_GET_CONTEXT
#include <EXTERN.h>
#include <perl.h>
#include <XSUB.h>
#define DPPP_PL_parser_NO_DUMMY
#define NEED_utf8_to_uvchr_buf
#define NEED_my_snprintf
#define NEED_pv_escape
#define NEED_pv_pretty
#ifndef PERLIO_F_UTF8
# define PERLIO_F_UTF8 0x00008000
# endif
#ifndef MAXINT
# define MAXINT ((int)(~(unsigned)0 >> 1))
# endif
#include "ppport.h"
#define is_utf8_sv(s) is_utf8_string ((U8 *)SvPV_nolen (s), SvCUR (s))
#define MAINT_DEBUG 0
#define MAINT_DEBUG_EOL 0
#define BUFFER_SIZE 1024
#define CSV_XS_TYPE_WARN 1
#define CSV_XS_TYPE_PV 0
#define CSV_XS_TYPE_IV 1
#define CSV_XS_TYPE_NV 2
/* maximum length for EOL, SEP, and QUOTE - keep in sync with .pm */
#define MAX_ATTR_LEN 16
#define CSV_FLAGS_QUO 0x0001
#define CSV_FLAGS_BIN 0x0002
#define CSV_FLAGS_EIF 0x0004
#define CSV_FLAGS_MIS 0x0010
#define HOOK_ERROR 0x0001
#define HOOK_AFTER_PARSE 0x0002
#define HOOK_BEFORE_PRINT 0x0004
#ifdef __THW_370__
/* EBCDIC on os390 z/OS: IS_EBCDIC reads better than __THW_370__ */
#define IS_EBCDIC
#endif
#define CH_TAB '\t'
#define CH_NL '\n'
#define CH_CR '\r'
#define CH_SPACE ' '
#define CH_QUO '"'
#ifdef IS_EBCDIC
#define CH_DEL '\007'
static unsigned char ec, ebcdic2ascii[256] = {
0x00, 0x01, 0x02, 0x03, 0x9c, 0x09, 0x86, 0x7f,
0x97, 0x8d, 0x8e, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
0x10, 0x11, 0x12, 0x13, 0x9d, 0x0a, 0x08, 0x87,
0x18, 0x19, 0x92, 0x8f, 0x1c, 0x1d, 0x1e, 0x1f,
0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x17, 0x1b,
0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x05, 0x06, 0x07,
0x90, 0x91, 0x16, 0x93, 0x94, 0x95, 0x96, 0x04,
0x98, 0x99, 0x9a, 0x9b, 0x14, 0x15, 0x9e, 0x1a,
0x20, 0xa0, 0xe2, 0xe4, 0xe0, 0xe1, 0xe3, 0xe5,
0xe7, 0xf1, 0xa2, 0x2e, 0x3c, 0x28, 0x2b, 0x7c,
0x26, 0xe9, 0xea, 0xeb, 0xe8, 0xed, 0xee, 0xef,
0xec, 0xdf, 0x21, 0x24, 0x2a, 0x29, 0x3b, 0x5e,
0x2d, 0x2f, 0xc2, 0xc4, 0xc0, 0xc1, 0xc3, 0xc5,
0xc7, 0xd1, 0xa6, 0x2c, 0x25, 0x5f, 0x3e, 0x3f,
0xf8, 0xc9, 0xca, 0xcb, 0xc8, 0xcd, 0xce, 0xcf,
0xcc, 0x60, 0x3a, 0x23, 0x40, 0x27, 0x3d, 0x22,
0xd8, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
0x68, 0x69, 0xab, 0xbb, 0xf0, 0xfd, 0xfe, 0xb1,
0xb0, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70,
0x71, 0x72, 0xaa, 0xba, 0xe6, 0xb8, 0xc6, 0xa4,
0xb5, 0x7e, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
0x79, 0x7a, 0xa1, 0xbf, 0xd0, 0x5b, 0xde, 0xae,
0xac, 0xa3, 0xa5, 0xb7, 0xa9, 0xa7, 0xb6, 0xbc,
0xbd, 0xbe, 0xdd, 0xa8, 0xaf, 0x5d, 0xb4, 0xd7,
0x7b, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47,
/* v this 0xa0 really should be 0xad. Needed for UTF = binary */
0x48, 0x49, 0xa0, 0xf4, 0xf6, 0xf2, 0xf3, 0xf5,
0x7d, 0x4a, 0x4b, 0x4c, 0x4d, 0x4e, 0x4f, 0x50,
0x51, 0x52, 0xb9, 0xfb, 0xfc, 0xf9, 0xfa, 0xff,
0x5c, 0xf7, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
0x59, 0x5a, 0xb2, 0xd4, 0xd6, 0xd2, 0xd3, 0xd5,
0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37,
0x38, 0x39, 0xb3, 0xdb, 0xdc, 0xd9, 0xda, 0x9f
};
#define is_csv_binary(ch) ((((ec = ebcdic2ascii[ch]) < 0x20 || ec >= 0x7f) && ch != CH_TAB) || ch == EOF)
#else
#define CH_DEL '\177'
#define is_csv_binary(ch) ((ch < CH_SPACE || ch >= CH_DEL) && ch != CH_TAB)
#endif
#define CH_EOLX 1215
#define CH_EOL *csv->eol
#define CH_SEPX 8888
#define CH_SEP *csv->sep
#define CH_QUOTEX 8889
#define CH_QUOTE *csv->quo
#define useIO_EOF 0x10
#define unless(expr) if (!(expr))
#define _is_reftype(f,x) \
(f && ((SvGMAGICAL (f) && mg_get (f)) || 1) && SvROK (f) && SvTYPE (SvRV (f)) == x)
#define _is_arrayref(f) _is_reftype (f, SVt_PVAV)
#define _is_hashref(f) _is_reftype (f, SVt_PVHV)
#define _is_coderef(f) _is_reftype (f, SVt_PVCV)
#define SvSetUndef(sv) sv_setpvn (sv, NULL, 0)
#define SvSetEmpty(sv) sv_setpvn_mg (sv, "", 0)
#define CSV_XS_SELF \
if (!self || !SvOK (self) || !SvROK (self) || \
SvTYPE (SvRV (self)) != SVt_PVHV) \
croak ("self is not a hash ref"); \
hv = (HV *)SvRV (self)
/* Keep in sync with .pm! */
#define CACHE_ID_quote_char 0
#define CACHE_ID_escape_char 1
#define CACHE_ID_sep_char 2
#define CACHE_ID_always_quote 4
#define CACHE_ID_quote_empty 5
#define CACHE_ID_quote_space 6
#define CACHE_ID_quote_binary 7
#define CACHE_ID_allow_loose_quotes 8
#define CACHE_ID_allow_loose_escapes 9
#define CACHE_ID_allow_unquoted_escape 10
#define CACHE_ID_allow_whitespace 11
#define CACHE_ID_blank_is_undef 12
#define CACHE_ID_empty_is_undef 13
#define CACHE_ID_auto_diag 14
#define CACHE_ID_diag_verbose 15
#define CACHE_ID_escape_null 16
#define CACHE_ID_formula 18
#define CACHE_ID_has_error_input 20
#define CACHE_ID_decode_utf8 21
#define CACHE_ID_verbatim 23
#define CACHE_ID_strict_eol 24
#define CACHE_ID_eol_is_cr 26
#define CACHE_ID_eol_type 27
#define CACHE_ID_strict 28
#define CACHE_ID_skip_empty_rows 29
#define CACHE_ID_binary 30
#define CACHE_ID_keep_meta_info 31
#define CACHE_ID__has_hooks 32
#define CACHE_ID__has_ahead 33
#define CACHE_ID_eol_len 36
#define CACHE_ID_sep_len 37
#define CACHE_ID_quo_len 38
#define CACHE_ID__is_bound 44
#define CACHE_ID_types 92
#define CACHE_ID_eol 100
#define CACHE_ID_sep 116
#define CACHE_ID_quo 132
#define CACHE_ID_undef_str 148
#define CACHE_ID_comment_str 156
#define EOL_TYPE_UNDEF 0
#define EOL_TYPE_NL 1
#define EOL_TYPE_CR 2
#define EOL_TYPE_CRNL 3
#define EOL_TYPE_OTHER 4
#define EOL_TYPE(c) ((((char)c) == CH_NL) ? EOL_TYPE_NL : (((char)c) == CH_CR) ? EOL_TYPE_CR : EOL_TYPE_OTHER)
#define SET_EOL_TYPE(c,t) { \
unless (c->eol_type) { \
c->eol_type = t; \
c->cache[CACHE_ID_eol_type] = t;\
} \
}
#define byte unsigned char
#define ulng unsigned long
typedef struct {
byte quote_char; /* 0 */
byte escape_char; /* 1 */
byte _sep_char; /* 2 : reserved for sep_char */
byte fld_idx; /* 3 */
byte always_quote; /* 4 */
byte quote_empty; /* 5 */
byte quote_space; /* 6 */
byte quote_binary; /* 7 */
byte allow_loose_quotes; /* 8 */
byte allow_loose_escapes; /* 9 */
byte allow_unquoted_escape; /* 10 */
byte allow_whitespace; /* 11 */
byte blank_is_undef; /* 12 */
byte empty_is_undef; /* 13 */
byte auto_diag; /* 14 */
byte diag_verbose; /* 15 */
byte escape_null; /* 16 */
byte first_safe_char; /* 17 */
byte formula; /* 18 */
byte utf8; /* 19 */
byte has_error_input; /* 20 */
byte decode_utf8; /* 21 */
byte useIO; /* 22: Also used to indicate EOF */
byte verbatim; /* 23 */
byte strict_eol; /* 24 */
byte eolx; /* 25 */
byte eol_is_cr; /* 26 */
byte eol_type; /* 27 */
byte strict; /* 28 */
byte skip_empty_rows; /* 29 */
byte binary; /* 30 */
byte keep_meta_info; /* 31 */
byte has_hooks; /* 32 */
byte has_ahead; /* 33 */
byte nyi_1; /* 34 : free */
byte nyi_2; /* 35 : free */
byte eol_len; /* 36 */
byte sep_len; /* 37 */
byte quo_len; /* 38 */
byte types_len; /* 39 */
short strict_n; /* 40.. */
long is_bound; /* 44.. */
ulng recno; /* 52.. */
byte * cache; /* 60.. */
SV * pself; /* 68.. PL_self, for error_diag */
HV * self; /* 76.. */
SV * bound; /* 84.. */
char * types; /* 92.. */
byte eol[MAX_ATTR_LEN]; /* 100..115 */
byte sep[MAX_ATTR_LEN]; /* 116..131 */
byte quo[MAX_ATTR_LEN]; /* 132..147 */
byte * undef_str; /* 148.. */
byte * comment_str; /* 156.. */
char * bptr;
SV * tmp;
int eol_pos;
STRLEN size;
STRLEN used;
byte undef_flg;
char buffer[BUFFER_SIZE];
/* Likely 1240 bytes */
} csv_t;
#define bool_opt_def(o,d) \
(((svp = hv_fetchs (self, o, FALSE)) && *svp) ? SvTRUE (*svp) : d)
#define bool_opt(o) bool_opt_def (o, 0)
#define num_opt_def(o,d) \
(((svp = hv_fetchs (self, o, FALSE)) && *svp) ? SvIV (*svp) : d)
#define num_opt(o) num_opt_def (o, 0)
typedef struct {
int xs_errno;
const char *xs_errstr;
} xs_error_t;
static const xs_error_t xs_errors[] = {
/* Generic errors */
{ 1000, "INI - constructor failed" },
{ 1001, "INI - sep_char is equal to quote_char or escape_char" },
{ 1002, "INI - allow_whitespace with escape_char or quote_char SP or TAB" },
{ 1003, "INI - \\r or \\n in main attr not allowed" },
{ 1004, "INI - callbacks should be undef or a hashref" },
{ 1005, "INI - EOL too long" },
{ 1006, "INI - SEP too long" },
{ 1007, "INI - QUOTE too long" },
{ 1008, "INI - SEP undefined" },
{ 1010, "INI - the header is empty" },
{ 1011, "INI - the header contains more than one valid separator" },
{ 1012, "INI - the header contains an empty field" },
{ 1013, "INI - the header contains nun-unique fields" },
{ 1014, "INI - header called on undefined stream" },
/* Syntax errors */
{ 1500, "PRM - Invalid/unsupported argument(s)" },
{ 1501, "PRM - The key attribute is passed as an unsupported type" },
{ 1502, "PRM - The value attribute is passed without the key attribute" },
{ 1503, "PRM - The value attribute is passed as an unsupported type" },
/* Parse errors */
{ 2010, "ECR - QUO char inside quotes followed by CR not part of EOL" },
{ 2011, "ECR - Characters after end of quoted field" },
{ 2012, "EOF - End of data in parsing input stream" },
{ 2013, "ESP - Specification error for fragments RFC7111" },
{ 2014, "ENF - Inconsistent number of fields" },
{ 2015, "ERW - Empty row" },
{ 2016, "EOL - Inconsistent EOL" },
/* EIQ - Error Inside Quotes */
{ 2021, "EIQ - NL char inside quotes, binary off" },
{ 2022, "EIQ - CR char inside quotes, binary off" },
{ 2023, "EIQ - QUO character not allowed" },
{ 2024, "EIQ - EOF cannot be escaped, not even inside quotes" },
{ 2025, "EIQ - Loose unescaped escape" },
{ 2026, "EIQ - Binary character inside quoted field, binary off" },
{ 2027, "EIQ - Quoted field not terminated" },
/* EIF - Error Inside Field */
{ 2030, "EIF - NL char inside unquoted verbatim, binary off" },
{ 2031, "EIF - CR char is first char of field, not part of EOL" },
{ 2032, "EIF - CR char inside unquoted, not part of EOL" },
{ 2034, "EIF - Loose unescaped quote" },
{ 2035, "EIF - Escaped EOF in unquoted field" },
{ 2036, "EIF - ESC error" },
{ 2037, "EIF - Binary character in unquoted field, binary off" },
/* Combine errors */
{ 2110, "ECB - Binary character in Combine, binary off" },
/* IO errors */
{ 2200, "EIO - print to IO failed. See errno" },
/* Hash-Ref errors */
{ 3001, "EHR - Unsupported syntax for column_names ()" },
{ 3002, "EHR - getline_hr () called before column_names ()" },
{ 3003, "EHR - bind_columns () and column_names () fields count mismatch" },
{ 3004, "EHR - bind_columns () only accepts refs to scalars" },
{ 3006, "EHR - bind_columns () did not pass enough refs for parsed fields" },
{ 3007, "EHR - bind_columns needs refs to writable scalars" },
{ 3008, "EHR - unexpected error in bound fields" },
{ 3009, "EHR - print_hr () called before column_names ()" },
{ 3010, "EHR - print_hr () called with invalid arguments" },
{ 4001, "PRM - The key does not exist as field in the data" },
{ 5001, "PRM - The result does not match the output to append to" },
{ 5002, "PRM - Unsupported output" },
{ 0, "" },
};
static int last_error = 0;
static SV *m_getline, *m_print;
#define is_EOL(c) (c == CH_EOLX)
#define __is_SEPX(c) (c == CH_SEP && (csv->sep_len == 0 || (\
csv->size - csv->used >= (STRLEN)csv->sep_len - 1 &&\
!memcmp (csv->bptr + csv->used, csv->sep + 1, csv->sep_len - 1) &&\
(csv->used += csv->sep_len - 1) &&\
(c = CH_SEPX))))
#if MAINT_DEBUG > 1
static byte _is_SEPX (unsigned int c, csv_t *csv, int line) {
unsigned int b = __is_SEPX (c);
(void)fprintf (stderr, "# %4d - is_SEPX:\t%d (%d)\n", line, b, csv->sep_len);
if (csv->sep_len)
(void)fprintf (stderr,
"# len: %d, siz: %d, usd: %d, c: %03x, *sep: %03x\n",
csv->sep_len, csv->size, csv->used, c, CH_SEP);
return b;
} /* _is_SEPX */
#define is_SEP(c) _is_SEPX (c, csv, __LINE__)
#else
#define is_SEP(c) __is_SEPX (c)
#endif
#define __is_QUOTEX(c) (CH_QUOTE && c == CH_QUOTE && (csv->quo_len == 0 || (\
csv->size - csv->used >= (STRLEN)csv->quo_len - 1 &&\
!memcmp (csv->bptr + csv->used, csv->quo + 1, csv->quo_len - 1) &&\
(csv->used += csv->quo_len - 1) &&\
(c = CH_QUOTEX))))
#if MAINT_DEBUG > 1
static byte _is_QUOTEX (unsigned int c, csv_t *csv, int line) {
unsigned int b = __is_QUOTEX (c);
(void)fprintf (stderr, "# %4d - is_QUOTEX:\t%d (%d)\n", line, b, csv->quo_len);
if (csv->quo_len)
(void)fprintf (stderr,
"# len: %d, siz: %d, usd: %d, c: %03x, *quo: %03x\n",
csv->quo_len, csv->size, csv->used, c, CH_QUOTE);
return b;
} /* _is_QUOTEX */
#define is_QUOTE(c) _is_QUOTEX (c, csv, __LINE__)
#else
#define is_QUOTE(c) __is_QUOTEX (c)
#endif
#define is_whitespace(ch) \
( (ch) != CH_SEP && \
(ch) != CH_QUOTE && \
(ch) != csv->escape_char && \
( (ch) == CH_SPACE || \
(ch) == CH_TAB \
) \
)
#define _pretty_strl(cp) cx_pretty_str (aTHX_ cp, strlen (cp))
#define _pretty_str(cp,xse) cx_pretty_str (aTHX_ cp, xse)
static char *cx_pretty_str (pTHX_ byte *s, STRLEN l) {
SV *dsv = newSVpvs_flags ("", SVs_TEMP);
return (pv_pretty (dsv, (char *)s, l, 0, NULL, NULL,
(PERL_PV_PRETTY_DUMP | PERL_PV_ESCAPE_UNI_DETECT)));
} /* _pretty_str */
#if MAINT_DEBUG > 4
#define _pretty_sv(cp) cx_pretty_sv (aTHX_ cp)
static char *cx_pretty_sv (pTHX_ SV *sv) {
if (SvOK (sv) && SvPOK (sv)) {
STRLEN l;
char *s = SvPV (sv, l);
return _pretty_str ((byte *)s, l);
}
return ("");
} /* _pretty_sv */
#endif
#define SvDiag(xse) cx_SvDiag (aTHX_ xse)
static SV *cx_SvDiag (pTHX_ int xse) {
int i = 0;
SV *err;
while (xs_errors[i].xs_errno && xs_errors[i].xs_errno != xse) i++;
if ((err = newSVpv (xs_errors[i].xs_errstr, 0))) {
(void)SvUPGRADE (err, SVt_PVIV);
SvIV_set (err, xse);
SvIOK_on (err);
}
return (err);
} /* SvDiag */
/* This function should be altered to deal with the optional extra argument
* that holds the replacement message */
#define SetDiag(csv,xse) cx_SetDiag (aTHX_ csv, xse, __LINE__)
#define SetDiagL(csv,xse,line) cx_SetDiag (aTHX_ csv, xse, line)
static SV *cx_SetDiag (pTHX_ csv_t *csv, int xse, int line) {
dSP;
SV *err = SvDiag (xse);
SV *pself = csv->pself;
last_error = xse;
(void)hv_store (csv->self, "_ERROR_DIAG", 11, err, 0);
if (xse == 0) {
(void)hv_store (csv->self, "_ERROR_POS", 10, newSViv (0), 0);
(void)hv_store (csv->self, "_ERROR_FLD", 10, newSViv (0), 0);
(void)hv_store (csv->self, "_ERROR_INPUT", 12, &PL_sv_undef, 0);
csv->has_error_input = 0;
}
if (line)
(void)hv_store (csv->self, "_ERROR_SRC", 10, newSViv (line), 0);
if (xse == 2012) /* EOF */
(void)hv_store (csv->self, "_EOF", 4, &PL_sv_yes, 0);
if (csv->auto_diag) {
unless (_is_hashref (pself))
pself = newRV_inc ((SV *)csv->self);
ENTER;
PUSHMARK (SP);
XPUSHs (pself);
PUTBACK;
call_pv ("Text::CSV_XS::error_diag", G_VOID | G_DISCARD);
LEAVE;
unless (pself == csv->pself)
sv_free (pself);
}
return (err);
} /* SetDiag */
#define xs_cache_get_eolt(hv) cx_xs_cache_get_eolt (aTHX_ hv)
static char *cx_xs_cache_get_eolt (pTHX_ HV *hv) {
SV **svp;
csv_t *csvs;
unless ((svp = hv_fetchs (hv, "_CACHE", FALSE)) && *svp)
return NULL;
csvs = (csv_t *)SvPV_nolen (*svp);
if (csvs->eol_type == EOL_TYPE_NL) return "\n";
if (csvs->eol_type == EOL_TYPE_CR) return "\r";
if (csvs->eol_type == EOL_TYPE_CRNL) return "\r\n";
if (csvs->eol_type == EOL_TYPE_OTHER) return (char *)(csvs->eol);
return NULL;
} /* cx_xs_cache_get_eolt */
#define xs_cache_set(hv,idx,val) cx_xs_cache_set (aTHX_ hv, idx, val)
static void cx_xs_cache_set (pTHX_ HV *hv, int idx, SV *val) {
SV **svp;
byte *cache;
csv_t csvs;
csv_t *csv = &csvs;
IV iv;
byte bv;
char *cp = "\0";
STRLEN len = 0;
unless ((svp = hv_fetchs (hv, "_CACHE", FALSE)) && *svp)
return;
cache = (byte *)SvPV_nolen (*svp);
(void)memcpy (csv, cache, sizeof (csv_t));
if (SvPOK (val))
cp = SvPV (val, len);
if (SvIOK (val))
iv = SvIV (val);
else if (SvNOK (val)) /* Needed for 5.6.x but safe for 5.8.x+ */
iv = (IV)SvNV (val); /* uncoverable statement ancient perl required */
else
iv = *cp;
bv = (unsigned)iv & 0xff;
switch (idx) {
/* single char/byte */
case CACHE_ID_sep_char:
CH_SEP = *cp;
csv->sep_len = 0;
break;
case CACHE_ID_quote_char:
CH_QUOTE = *cp;
csv->quo_len = 0;
break;
case CACHE_ID_escape_char: csv->escape_char = *cp; break;
/* boolean/numeric */
case CACHE_ID_binary: csv->binary = bv; break;
case CACHE_ID_keep_meta_info: csv->keep_meta_info = bv; break;
case CACHE_ID_always_quote: csv->always_quote = bv; break;
case CACHE_ID_quote_empty: csv->quote_empty = bv; break;
case CACHE_ID_quote_space: csv->quote_space = bv; break;
case CACHE_ID_escape_null: csv->escape_null = bv; break;
case CACHE_ID_quote_binary: csv->quote_binary = bv; break;
case CACHE_ID_decode_utf8: csv->decode_utf8 = bv; break;
case CACHE_ID_allow_loose_escapes: csv->allow_loose_escapes = bv; break;
case CACHE_ID_allow_loose_quotes: csv->allow_loose_quotes = bv; break;
case CACHE_ID_allow_unquoted_escape: csv->allow_unquoted_escape = bv; break;
case CACHE_ID_allow_whitespace: csv->allow_whitespace = bv; break;
case CACHE_ID_blank_is_undef: csv->blank_is_undef = bv; break;
case CACHE_ID_empty_is_undef: csv->empty_is_undef = bv; break;
case CACHE_ID_formula: csv->formula = bv; break;
case CACHE_ID_strict: csv->strict = bv; break;
case CACHE_ID_verbatim: csv->verbatim = bv; break;
case CACHE_ID_strict_eol: csv->strict_eol = bv; break;
case CACHE_ID_eol_type: csv->eol_type = bv; break;
case CACHE_ID_skip_empty_rows: csv->skip_empty_rows = bv; break;
case CACHE_ID_auto_diag: csv->auto_diag = bv; break;
case CACHE_ID_diag_verbose: csv->diag_verbose = bv; break;
case CACHE_ID__has_ahead: csv->has_ahead = bv; break;
case CACHE_ID__has_hooks: csv->has_hooks = bv; break;
case CACHE_ID_has_error_input: csv->has_error_input = bv; break;
/* a 4-byte IV */
case CACHE_ID__is_bound: csv->is_bound = iv; break;
/* string */
case CACHE_ID_sep:
(void)memcpy (csv->sep, cp, len);
csv->sep_len = len == 1 ? 0 : len;
break;
case CACHE_ID_quo:
(void)memcpy (csv->quo, cp, len);
csv->quo_len = len == 1 ? 0 : len;
break;
case CACHE_ID_eol:
(void)memcpy (csv->eol, cp, len);
csv->eol_len = len;
csv->eol_type = len == 0 ? EOL_TYPE_UNDEF
: len == 1 && *cp == CH_NL ? EOL_TYPE_NL
: len == 1 && *cp == CH_CR ? EOL_TYPE_CR
: len == 2 && *cp == CH_CR
&& cp[1] == CH_NL ? EOL_TYPE_CRNL
: EOL_TYPE_OTHER;
csv->strict_eol &= 0x3F;
csv->eol_is_cr = csv->eol_type == EOL_TYPE_CR ? 1 : 0;
#if MAINT_DEBUG_EOL > 0
(void)fprintf (stderr, "# %04d cache set eol: '%s'\t(len: %d, is_cr: %d, tp: %02x)\n",
__LINE__, _pretty_str (cp, len), len, csv->eol_is_cr, csv->eol_type);
#endif
break;
case CACHE_ID_undef_str:
if (*cp) {
csv->undef_str = (byte *)cp;
if (SvUTF8 (val))
csv->undef_flg = 3;
}
else {
csv->undef_str = NULL;
csv->undef_flg = 0;
}
break;
case CACHE_ID_comment_str:
csv->comment_str = *cp ? (byte *)cp : NULL;
break;
case CACHE_ID_types:
if (cp && len) {
csv->types = cp;
csv->types_len = len;
}
else {
csv->types = NULL;
csv->types_len = 0;
}
break;
default:
warn ("Unknown cache index %d ignored\n", idx);
}
csv->cache = cache;
(void)memcpy (cache, csv, sizeof (csv_t));
} /* cache_set */
#define _cache_show_byte(trim,c) \
warn (" %-21s %02x:%3d\n", trim, c, c)
#define _cache_show_char(trim,c) \
warn (" %-21s %02x:%s\n", trim, c, _pretty_str (&c, 1))
#define _cache_show_str(trim,l,str) \
warn (" %-21s %3d:%s\n", trim, l, _pretty_str (str, l))
#define _csv_diag(csv) _xs_csv_diag (aTHX_ csv)
static void _xs_csv_diag (pTHX_ csv_t *csv) {
warn ("CACHE:\n");
_cache_show_char ("quote_char", CH_QUOTE);
_cache_show_char ("escape_char", csv->escape_char);
_cache_show_char ("sep_char", CH_SEP);
_cache_show_byte ("binary", csv->binary);
_cache_show_byte ("decode_utf8", csv->decode_utf8);
_cache_show_byte ("allow_loose_escapes", csv->allow_loose_escapes);
_cache_show_byte ("allow_loose_quotes", csv->allow_loose_quotes);
_cache_show_byte ("allow_unquoted_escape", csv->allow_unquoted_escape);
_cache_show_byte ("allow_whitespace", csv->allow_whitespace);
_cache_show_byte ("always_quote", csv->always_quote);
_cache_show_byte ("quote_empty", csv->quote_empty);
_cache_show_byte ("quote_space", csv->quote_space);
_cache_show_byte ("escape_null", csv->escape_null);
_cache_show_byte ("quote_binary", csv->quote_binary);
_cache_show_byte ("auto_diag", csv->auto_diag);
_cache_show_byte ("diag_verbose", csv->diag_verbose);
_cache_show_byte ("formula", csv->formula);
_cache_show_byte ("strict", csv->strict);
_cache_show_byte ("strict_n", csv->strict_n);
_cache_show_byte ("strict_eol", csv->strict_eol);
_cache_show_byte ("eol_type", csv->eol_type);
_cache_show_byte ("skip_empty_rows", csv->skip_empty_rows);
_cache_show_byte ("has_error_input", csv->has_error_input);
_cache_show_byte ("blank_is_undef", csv->blank_is_undef);
_cache_show_byte ("empty_is_undef", csv->empty_is_undef);
_cache_show_byte ("has_ahead", csv->has_ahead);
_cache_show_byte ("keep_meta_info", csv->keep_meta_info);
_cache_show_byte ("verbatim", csv->verbatim);
_cache_show_byte ("useIO", csv->useIO);
_cache_show_byte ("has_hooks", csv->has_hooks);
_cache_show_byte ("eol_is_cr", csv->eol_is_cr);
_cache_show_byte ("eol_len", csv->eol_len);
_cache_show_str ("eol", csv->eol_len, csv->eol);
_cache_show_byte ("sep_len", csv->sep_len);
if (csv->sep_len > 1)
_cache_show_str ("sep", csv->sep_len, csv->sep);
_cache_show_byte ("quo_len", csv->quo_len);
if (csv->quo_len > 1)
_cache_show_str ("quote", csv->quo_len, csv->quo);
if (csv->types_len)
_cache_show_str ("types", csv->types_len, (byte *)csv->types);
else
_cache_show_str ("types", 0, (byte *)"");
if (csv->bptr)
_cache_show_str ("bptr", (int)strlen (csv->bptr), (byte *)csv->bptr);
if (csv->tmp && SvPOK (csv->tmp)) {
char *s = SvPV_nolen (csv->tmp);
_cache_show_str ("tmp", (int)strlen (s), (byte *)s);
}
if (csv->cache)
warn (" %-20s %4d:0x%08lx\n", "cache", (int)sizeof (csv_t), (unsigned long)csv->cache);
else
warn (" %-22s --:no cache yet\n", "cache");
} /* _csv_diag */
#define xs_cache_diag(hv) cx_xs_cache_diag (aTHX_ hv)
static void cx_xs_cache_diag (pTHX_ HV *hv) {
SV **svp;
byte *cache;
csv_t csvs;
csv_t *csv = &csvs;
unless ((svp = hv_fetchs (hv, "_CACHE", FALSE)) && *svp) {
warn ("CACHE: invalid\n");
return;
}
cache = (byte *)SvPV_nolen (*svp);
(void)memcpy (csv, cache, sizeof (csv_t));
_csv_diag (csv);
} /* xs_cache_diag */
#define set_eol_is_cr(csv) cx_set_eol_is_cr (aTHX_ csv)
static void cx_set_eol_is_cr (pTHX_ csv_t *csv) {
csv->eol_is_cr = 1;
csv->eol_len = 1;
csv->eol[0] = CH_CR;
csv->eol_type = EOL_TYPE_CR;
(void)memcpy (csv->cache, csv, sizeof (csv_t));
(void)hv_store (csv->self, "eol", 3, newSVpvn ((char *)csv->eol, 1), 0);
#if MAINT_DEBUG_EOL > 0
(void)fprintf (stderr, "# %04d set eol is CR: '%s'\t(len: %d, is_cr: %d, tp: %02x)\n",
__LINE__, _pretty_str (csv->eol, csv->eol_len), csv->eol_len, csv->eol_is_cr, csv->eol_type);
#endif
} /* set_eol_is_cr */
#define SetupCsv(csv,self,pself) cx_SetupCsv (aTHX_ csv, self, pself)
static void cx_SetupCsv (pTHX_ csv_t *csv, HV *self, SV *pself) {
SV **svp;
STRLEN len;
char *ptr;
last_error = 0;
if ((svp = hv_fetchs (self, "_CACHE", FALSE)) && *svp) {
byte *cache = (byte *)SvPVX (*svp);
(void)memcpy (csv, cache, sizeof (csv_t));
}
else {
SV *sv_cache;
(void)memset (csv, 0, sizeof (csv_t)); /* Reset everything */
csv->self = self;
csv->pself = pself;
CH_SEP = ',';
if ((svp = hv_fetchs (self, "sep_char", FALSE)) && *svp && SvOK (*svp))
CH_SEP = *SvPV (*svp, len);
if ((svp = hv_fetchs (self, "sep", FALSE)) && *svp && SvOK (*svp)) {
ptr = SvPV (*svp, len);
(void)memcpy (csv->sep, ptr, len);
if (len > 1)
csv->sep_len = len;
}
CH_QUOTE = '"';
if ((svp = hv_fetchs (self, "quote_char", FALSE)) && *svp) {
if (SvOK (*svp)) {
ptr = SvPV (*svp, len);
CH_QUOTE = len ? *ptr : (char)0;
}
else
CH_QUOTE = (char)0;
}
if ((svp = hv_fetchs (self, "quote", FALSE)) && *svp && SvOK (*svp)) {
ptr = SvPV (*svp, len);
(void)memcpy (csv->quo, ptr, len);
if (len > 1)
csv->quo_len = len;
}
csv->escape_char = '"';
if ((svp = hv_fetchs (self, "escape_char", FALSE)) && *svp) {
if (SvOK (*svp)) {
ptr = SvPV (*svp, len);
csv->escape_char = len ? *ptr : (char)0;
}
else
csv->escape_char = (char)0;
}
if ((svp = hv_fetchs (self, "eol", FALSE)) && *svp && SvOK (*svp)) {
char *eol = SvPV (*svp, len);
(void)memcpy (csv->eol, eol, len);
csv->eol_len = len;
if (len == 1 && *eol == CH_CR) {
csv->eol_is_cr = 1;
csv->eol_type = EOL_TYPE_CR;
}
else if (len == 1 && *eol == CH_NL)
csv->eol_type = EOL_TYPE_NL;
else if (len == 2 && *eol == CH_CR && eol[1] == CH_NL)
csv->eol_type = EOL_TYPE_CRNL;
}
csv->undef_flg = 0;
if ((svp = hv_fetchs (self, "undef_str", FALSE)) && *svp && SvOK (*svp)) {
/*if (sv && (SvOK (sv) || (
(SvGMAGICAL (sv) && (mg_get (sv), 1) && SvOK (sv))))) {*/
csv->undef_str = (byte *)SvPV_nolen (*svp);
if (SvUTF8 (*svp))
csv->undef_flg = 3;
}
else
csv->undef_str = NULL;
if ((svp = hv_fetchs (self, "comment_str", FALSE)) && *svp && SvOK (*svp))
csv->comment_str = (byte *)SvPV_nolen (*svp);
else
csv->comment_str = NULL;
if ((svp = hv_fetchs (self, "_types", FALSE)) && *svp && SvOK (*svp)) {
csv->types = SvPV (*svp, len);
csv->types_len = len;
}
if ((svp = hv_fetchs (self, "_is_bound", FALSE)) && *svp && SvOK (*svp))
csv->is_bound = SvIV (*svp);
if ((svp = hv_fetchs (self, "callbacks", FALSE)) && _is_hashref (*svp)) {
HV *cb = (HV *)SvRV (*svp);
if ((svp = hv_fetchs (cb, "after_parse", FALSE)) && _is_coderef (*svp))
csv->has_hooks |= HOOK_AFTER_PARSE;
if ((svp = hv_fetchs (cb, "before_print", FALSE)) && _is_coderef (*svp))
csv->has_hooks |= HOOK_BEFORE_PRINT;
}
csv->binary = bool_opt ("binary");
csv->decode_utf8 = bool_opt ("decode_utf8");
csv->always_quote = bool_opt ("always_quote");
csv->strict = bool_opt ("strict");
csv->strict_eol = num_opt ("strict_eol");
csv->quote_empty = bool_opt ("quote_empty");
csv->quote_space = bool_opt_def ("quote_space", 1);
csv->escape_null = bool_opt_def ("escape_null", 1);
csv->quote_binary = bool_opt_def ("quote_binary", 1);
csv->allow_loose_quotes = bool_opt ("allow_loose_quotes");
csv->allow_loose_escapes = bool_opt ("allow_loose_escapes");
csv->allow_unquoted_escape = bool_opt ("allow_unquoted_escape");
csv->allow_whitespace = bool_opt ("allow_whitespace");
csv->blank_is_undef = bool_opt ("blank_is_undef");
csv->empty_is_undef = bool_opt ("empty_is_undef");
csv->verbatim = bool_opt ("verbatim");
csv->auto_diag = num_opt ("auto_diag");
csv->diag_verbose = num_opt ("diag_verbose");
csv->keep_meta_info = num_opt ("keep_meta_info");
csv->skip_empty_rows = num_opt ("skip_empty_rows");
csv->formula = num_opt ("formula");
unless (csv->escape_char) csv->escape_null = 0;
sv_cache = newSVpvn ((char *)csv, sizeof (csv_t));
csv->cache = (byte *)SvPVX (sv_cache);
SvREADONLY_on (sv_cache);
(void)memcpy (csv->cache, csv, sizeof (csv_t));
(void)hv_store (self, "_CACHE", 6, sv_cache, 0);
}
csv->utf8 = 0;
csv->size = 0;
csv->used = 0;
/* This is EBCDIC-safe, as it is used after translation */
csv->first_safe_char = csv->quote_space ? 0x21 : 0x20;
if (csv->is_bound) {
if ((svp = hv_fetchs (self, "_BOUND_COLUMNS", FALSE)) && _is_arrayref (*svp))
csv->bound = *svp;
else
csv->is_bound = 0;
}
csv->eol_pos = -1;
csv->eolx = csv->eol_len
? csv->verbatim || csv->eol_len >= 2
? 1
: csv->eol[0] == CH_CR || csv->eol[0] == CH_NL
? 0
: 1
: 0;
if (csv->eol_type > 0 && csv->strict_eol > 0 && !*csv->eol)
csv->eol_is_cr = 0;
#if MAINT_DEBUG_EOL > 0
(void)fprintf (stderr, "# %04d setup eol: '%s'\t(len: %d, is_cr: %d, x: %d, pos: %d, tp: %02x)\n",
__LINE__, _pretty_str (csv->eol, csv->eol_len), csv->eol_len, csv->eol_is_cr, csv->eolx, csv->eol_pos, csv->eol_type);
#endif
if (csv->sep_len > 1 && is_utf8_string ((U8 *)(csv->sep), csv->sep_len))
csv->utf8 = 1;
if (csv->quo_len > 1 && is_utf8_string ((U8 *)(csv->quo), csv->quo_len))
csv->utf8 = 1;
if (csv->strict
&& !csv->strict_n
&& (svp = hv_fetchs (self, "_COLUMN_NAMES", FALSE))
&& _is_arrayref (*svp))
csv->strict_n = av_len ((AV *)(SvRV (*svp)));
} /* SetupCsv */
#define Print(csv,dst) cx_Print (aTHX_ csv, dst)
static int cx_Print (pTHX_ csv_t *csv, SV *dst) {
int result;
int keep = 0;
if (csv->useIO) {
SV *tmp = newSVpvn_flags (csv->buffer, csv->used, SVs_TEMP);
dSP;
PUSHMARK (sp);
EXTEND (sp, 2);
PUSHs ((dst));
if (csv->utf8) {
STRLEN len;
char *ptr;
int j;
ptr = SvPV (tmp, len);
while (len > 0 && !is_utf8_sv (tmp) && keep < 16) {
ptr[--len] = (char)0;
SvCUR_set (tmp, len);
keep++;
}
for (j = 0; j < keep; j++)
csv->buffer[j] = csv->buffer[csv->used - keep + j];
SvUTF8_on (tmp);
}
PUSHs (tmp);
PUTBACK;
result = call_sv (m_print, G_METHOD);
SPAGAIN;
if (result) {
result = POPi;
unless (result)
(void)SetDiag (csv, 2200);
}
PUTBACK;
}
else {
sv_catpvn (SvRV (dst), csv->buffer, csv->used);
result = TRUE;
}
if (csv->utf8 && !csv->useIO && csv->decode_utf8
&& SvROK (dst) && is_utf8_sv (SvRV (dst)))
SvUTF8_on (SvRV (dst));
csv->used = keep;
return result;
} /* Print */
#define CSV_PUT(csv,dst,c) { \
if ((csv)->used == sizeof ((csv)->buffer) - 1) { \
unless (Print ((csv), (dst))) \
return FALSE; \
} \
(csv)->buffer[(csv)->used++] = (c); \
}
#define bound_field(csv,i,keep) cx_bound_field (aTHX_ csv, i, keep)
static SV *cx_bound_field (pTHX_ csv_t *csv, SSize_t i, int keep) {
SV *sv = csv->bound;
AV *av;
/* fprintf (stderr, "# New bind %d/%d\n", i, csv->is_bound);\ */
if (i >= csv->is_bound) {
(void)SetDiag (csv, 3006);
return (NULL);
}
if (sv && SvROK (sv)) {
av = (AV *)(SvRV (sv));
/* fprintf (stderr, "# Bind %d/%d/%d\n", i, csv->is_bound, av_len (av)); */
sv = *av_fetch (av, i, FALSE);
if (sv && SvROK (sv)) {
sv = SvRV (sv);
if (keep)
return (sv);
unless (SvREADONLY (sv)) {
SvSetEmpty (sv);
return (sv);
}
}
}
(void)SetDiag (csv, 3008);
return (NULL);
} /* bound_field */
#define was_quoted(mf,idx) cx_was_quoted (aTHX_ mf, idx)
static int cx_was_quoted (pTHX_ AV *mf, int idx) {
SV **x = av_fetch (mf, idx, FALSE);
return (x && SvIOK (*x) && SvIV (*x) & CSV_FLAGS_QUO ? 1 : 0);
} /* was_quoted */
#define _formula(csv,sv,len,f) cx_formula (aTHX_ csv, sv, len, f)
static char *cx_formula (pTHX_ csv_t *csv, SV *sv, STRLEN *len, int f) {
int fa = csv->formula;
if (fa == 1) die ("Formulas are forbidden\n");
if (fa == 2) croak ("Formulas are forbidden\n");
if (fa == 3) {
char *ptr = SvPV_nolen (sv);
char rec[40];