-
Notifications
You must be signed in to change notification settings - Fork 1
/
itj_csv.h
777 lines (656 loc) · 21.8 KB
/
itj_csv.h
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
/*
* This is a streaming CSV reader, with AVX and AVX2 support, written in a stb header style
* It only supports x86 and x86_64
*
* LICENSE available at the bottom
*
* USAGE: Look up example_usage.c or test.c
* but here's a summary
* itj_csv_open();
* itj_csv_pump_stdio();
* for (;;) {
* value = itj_csv_get_next_value();
* if (value.need_data) {
* itj_csv_pump_stdio();
* }
*
* USE "value" here
* }
*
* KNOWN ISSUES: It expects an ending newline, and not just end of file
*/
#ifndef ITJ_CSV_NO_STD
#include <stdlib.h>
#include <stdio.h>
#endif
#ifdef ITJ_CSV_IMPLEMENTATION_AVX2
#define ITJ_CSV_IMPLEMENTATION_AVX
#endif
#ifdef ITJ_CSV_IMPLEMENTATION_AVX
#define ITJ_CSV_IMPLEMENTATION
#endif
#if defined(ITJ_CSV_IMPLEMENTATION_AVX) || defined(ITJ_CSV_IMPLEMENTATION_AVX2)
#include <immintrin.h>
#ifdef _MSC_VER
#include <intrin.h>
#endif
#endif
#if !(ITJ_CSV_32BIT || ITJ_CSV_64BIT)
#if defined(__x86_64__) || defined(_M_X64) || defined(__ppc64__)
#define ITJ_CSV_64BIT
#else
#define ITJ_CSV_32BIT
#endif
#endif
typedef unsigned char itj_csv_u8;
typedef unsigned int itj_csv_u32;
typedef int itj_csv_s32;
typedef itj_csv_u8 itj_csv_bool;
#ifdef ITJ_CSV_64BIT
typedef long long itj_csv_s64;
typedef unsigned long long itj_csv_u64;
typedef itj_csv_s64 itj_csv_smax;
typedef itj_csv_u64 itj_csv_umax;
#else
typedef itj_csv_s32 itj_csv_smax;
typedef itj_csv_u32 itj_csv_umax;
#endif
#define ITJ_CSV_TRUE 1
#define ITJ_CSV_FALSE 0
#define ITJ_CSV_FAILED 0
#ifndef ITJ_CSV_NO_STD
#define ITJ_CSV_ALLOC(user_ptr, num_bytes) calloc(1, num_bytes)
#define ITJ_CSV_FREE(user_ptr, ptr) free(ptr)
#endif
#ifndef ITJ_CSV_MEMCPY
#include <string.h>
#define ITJ_CSV_MEMCPY(dst, src, size) memcpy(dst, src, size)
#endif
#define ITJ_CSV_DELIM_COMMA ','
#define ITJ_CSV_DELIM_COLON ';'
typedef enum itj_csv_state_name {
ITJ_CSV_STATE_NORMAL,
ITJ_CSV_STATE_NEWLINE,
ITJ_CSV_STATE_EOF,
} itj_csv_state_name_t;
typedef struct itj_csv_string {
itj_csv_u8 *base;
itj_csv_u32 len;
} itj_csv_string_t;
typedef struct itj_csv_value {
struct itj_csv_string data;
itj_csv_bool is_end_of_line;
itj_csv_bool need_data;
itj_csv_umax idx;
} itj_csv_value_t;
typedef struct itj_csv {
itj_csv_u8 delimiter;
itj_csv_umax read_iter;
itj_csv_umax prev_read_iter;
itj_csv_u8 *read_base;
itj_csv_umax read_max;
itj_csv_umax read_used;
itj_csv_umax idx;
void *user_mem_ptr;
#ifndef ITJ_CSV_NO_STD
FILE *fh;
#endif
} itj_csv_t;
void itj_csv_ignore_newlines(struct itj_csv *csv) {
itj_csv_umax i;
itj_csv_umax max = csv->read_used;
for (i = csv->read_iter; i < max; ++i) {
itj_csv_u8 ch = csv->read_base[i];
if (ch == '\r') {
if (i + 1 < max) {
itj_csv_u8 ch_next = csv->read_base[i + 1];
if (ch_next == '\n') {
i = i + 1;
}
}
} else if (ch == '\n') {
i = i + 1;
} else {
break;
}
}
csv->read_iter = i;
}
itj_csv_umax itj_csv_contract_double_quotes(itj_csv_u8 *start, itj_csv_umax max) {
itj_csv_umax new_len = max;
for (itj_csv_umax i = 0; i < new_len; ++i) {
itj_csv_u8 ch = start[i];
if (ch == '\"') {
for (itj_csv_umax j = i + 1; j < max - 1; ++j) {
start[j] = start[j + 1];
}
--new_len;
}
}
return new_len;
}
#if defined(ITJ_CSV_IMPLEMENTATION_AVX) || defined(ITJ_CSV_IMPLEMENTATION_AVX2)
#ifdef _MSC_VER
inline itj_csv_u32 itj_csv_ffs(itj_csv_u32 value) {
unsigned long pos;
_BitScanForward(&pos, value);
return pos + 1;
}
#else
#define itj_csv_ffs(value) __builtin_ffs(value)
#endif
#endif
#ifdef ITJ_CSV_IMPLEMENTATION
struct itj_csv_value itj_csv_parse_quotes(struct itj_csv *csv, itj_csv_umax i) {
csv->prev_read_iter = csv->read_iter;
struct itj_csv_value rv;
i += 1; // Expecting " to be the first character
rv.data.base = &csv->read_base[i];
rv.data.len = 0;
rv.is_end_of_line = ITJ_CSV_FALSE;
rv.need_data = ITJ_CSV_FALSE;
rv.idx = csv->idx;
itj_csv_bool got_doubles = ITJ_CSV_FALSE;
itj_csv_umax max = csv->read_used;
for (; i < max; ++i) {
itj_csv_u8 ch = csv->read_base[i];
if (ch == '"') {
if (i + 1 == max) {
goto skip_max_test;
} else {
itj_csv_u8 ch_next = csv->read_base[i + 1];
if (ch_next == '"') {
got_doubles = ITJ_CSV_TRUE;
rv.data.len += 2;
i += 1;
} else {
i += 1;
goto skip_max_test;
}
}
} else {
rv.data.len += 1;
}
}
if (i >= max) {
rv.need_data = ITJ_CSV_TRUE;
return rv;
}
skip_max_test:
for (; i < max; ++i) {
itj_csv_u8 ch = csv->read_base[i];
if (ch == '\n') {
rv.is_end_of_line = ITJ_CSV_TRUE;
i += 1;
break;
}
if (ch == csv->delimiter) {
++i;
break;
}
}
if (got_doubles) {
rv.data.len = itj_csv_contract_double_quotes(rv.data.base, rv.data.len);
}
csv->read_iter = i;
return rv;
}
struct itj_csv_value itj_csv_parse_value(struct itj_csv *csv, itj_csv_umax i) {
csv->prev_read_iter = csv->read_iter;
struct itj_csv_value rv;
rv.data.base = &csv->read_base[i];
rv.data.len = 0;
rv.is_end_of_line = ITJ_CSV_FALSE;
rv.need_data = ITJ_CSV_FALSE;
rv.idx = csv->idx;
itj_csv_umax max = csv->read_used;
for (; i < max; ++i) {
itj_csv_u8 ch = csv->read_base[i];
if (ch == '"') {
return itj_csv_parse_quotes(csv, i);
} else if (ch == csv->delimiter) {
i += 1;
csv->read_iter = i;
return rv;
} else if (ch == '\r') {
if (i + 1 == max) {
rv.need_data = ITJ_CSV_TRUE;
return rv;
}
itj_csv_u8 ch_next = csv->read_base[i + 1];
if (ch_next == '\n') {
i += 2;
rv.is_end_of_line = ITJ_CSV_TRUE;
csv->read_iter = i;
return rv;
} else {
rv.data.len += 1;
}
} else if (ch == '\n') {
rv.is_end_of_line = ITJ_CSV_TRUE;
i += 1;
csv->read_iter = i;
return rv;
} else {
rv.data.len += 1;
}
}
rv.need_data = ITJ_CSV_TRUE;
return rv;
}
struct itj_csv_value itj_csv_get_next_value(struct itj_csv *csv) {
struct itj_csv_value rv = itj_csv_parse_value(csv, csv->read_iter);
if (!rv.need_data) {
csv->idx += 1;
}
return rv;
}
#endif // ITJ_CSV_IMPLEMENTATION
#ifdef ITJ_CSV_IMPLEMENTATION_AVX
struct itj_csv_value itj_csv_parse_quotes_avx(struct itj_csv *csv, itj_csv_umax i) {
csv->prev_read_iter = csv->read_iter;
struct itj_csv_value rv;
rv.data.base = NULL;
rv.data.len = 0;
rv.is_end_of_line = ITJ_CSV_FALSE;
rv.need_data = ITJ_CSV_FALSE;
rv.idx = csv->idx;
__m128i Q = _mm_set1_epi8('\"');
// We assume that i starts by pointing to a quotation mark
i += 1;
itj_csv_umax start = i;
itj_csv_bool got_doubles = ITJ_CSV_FALSE;
itj_csv_bool quote_at_boundary = ITJ_CSV_FALSE;
itj_csv_umax max = csv->read_used;
while (i < max) {
__m128i b = _mm_loadu_si128((__m128i *)(csv->read_base + i));
__m128i q = _mm_cmpeq_epi8(b, Q);
itj_csv_u32 quotes_mask = _mm_movemask_epi8(q);
if (quote_at_boundary) {
if ((quotes_mask >> 15) & 0x1 != 0x1) {
goto get_out;
}
got_doubles = ITJ_CSV_TRUE;
quote_at_boundary = false;
}
if (quotes_mask == 0) {
i += 16;
} else {
itj_csv_u32 acc = 0;
do {
itj_csv_u32 j = itj_csv_ffs(quotes_mask) - 1;
quotes_mask = quotes_mask >> j;
if (quotes_mask & 0x1) {
if (acc == 15) {
quote_at_boundary = true;
i += j;
quotes_mask = 0;
} else {
if ((quotes_mask & 0x3) == 3) {
j += 2;
quotes_mask = quotes_mask >> 2;
i += j;
got_doubles = ITJ_CSV_TRUE;
} else {
i += j;
goto get_out;
}
}
}
} while (quotes_mask);
}
}
get_out:
if (i >= max) {
rv.need_data = ITJ_CSV_TRUE;
return rv;
}
rv.data.base = &csv->read_base[start];
rv.data.len = i - start;
for (; i < max; ++i) {
itj_csv_u8 c = csv->read_base[i];
if (c == '\r' || c == '\n') {
if (i < max && c + csv->read_base[i] == '\r' + '\n') {
i += 2;
rv.is_end_of_line = ITJ_CSV_TRUE;
break;
} else if (c == '\n') {
i += 1;
rv.is_end_of_line = ITJ_CSV_TRUE;
break;
}
} else if (c == csv->delimiter) {
i++;
break;
}
}
if (got_doubles) {
rv.data.len = itj_csv_contract_double_quotes(rv.data.base, rv.data.len);
}
csv->read_iter = i;
csv->idx += 1;
return rv;
}
struct itj_csv_value itj_csv_parse_value_avx(struct itj_csv *csv, itj_csv_umax i) {
csv->prev_read_iter = csv->read_iter;
struct itj_csv_value rv;
rv.data.base = NULL;
rv.data.len = 0;
rv.is_end_of_line = ITJ_CSV_FALSE;
rv.need_data = ITJ_CSV_FALSE;
rv.idx = csv->idx;
__m128i Q = _mm_set1_epi8('\"');
__m128i delim = _mm_set1_epi8(csv->delimiter);
__m128i R = _mm_set1_epi8('\r');
__m128i N = _mm_set1_epi8('\n');
itj_csv_umax start = i;
itj_csv_umax max = csv->read_used;
while (i < max) {
__m128i b = _mm_loadu_si128((__m128i *)(csv->read_base + i));
__m128i q = _mm_cmpeq_epi8(b, Q);
itj_csv_u32 quote_mask = _mm_movemask_epi8(q);
__m128i m = _mm_cmpeq_epi8(b, delim);
__m128i n = _mm_cmpeq_epi8(b, N);
__m128i r = _mm_cmpeq_epi8(b, R);
m = _mm_or_si128(m, r);
m = _mm_or_si128(m, n);
itj_csv_u32 mask = _mm_movemask_epi8(m);
if (mask == 0) {
if (quote_mask != 0) {
i += itj_csv_ffs(quote_mask) - 1;
return itj_csv_parse_quotes_avx(csv, i);
}
i += 16;
} else {
itj_csv_umax first_delim = itj_csv_ffs(mask) - 1;
if (quote_mask != 0) {
itj_csv_umax first_quote = itj_csv_ffs(quote_mask) - 1;
if (first_quote < first_delim) {
return itj_csv_parse_quotes_avx(csv, i);
}
}
i += first_delim;
break;
}
}
if (i >= max) {
rv.need_data = ITJ_CSV_TRUE;
return rv;
}
rv.data.base = &csv->read_base[start];
rv.data.len = i - start;
itj_csv_u8 c = csv->read_base[i++];
if (c == '\r' || c == '\n') {
if (i < max && c + csv->read_base[i] == '\r' + '\n') {
i += 1;
rv.is_end_of_line = ITJ_CSV_TRUE;
} else if (c == '\n') {
rv.is_end_of_line = ITJ_CSV_TRUE;
}
}
csv->read_iter = i;
csv->idx += 1;
return rv;
}
struct itj_csv_value itj_csv_get_next_value_avx(struct itj_csv *csv) {
return itj_csv_parse_value_avx(csv, csv->read_iter);
}
#endif // ITJ_CSV_IMPLEMENTATION_AVX
#ifdef ITJ_CSV_IMPLEMENTATION_AVX2
struct itj_csv_value itj_csv_parse_quotes_avx2(struct itj_csv *csv, itj_csv_umax i) {
csv->prev_read_iter = csv->read_iter;
struct itj_csv_value rv;
rv.data.base = NULL;
rv.data.len = 0;
rv.is_end_of_line = ITJ_CSV_FALSE;
rv.need_data = ITJ_CSV_FALSE;
rv.idx = csv->idx;
__m256i Q = _mm256_set1_epi8('\"');
// We assume that i starts by pointing to a quotation mark
i += 1;
itj_csv_umax start = i;
itj_csv_bool got_doubles = ITJ_CSV_FALSE;
itj_csv_bool quote_at_boundary = ITJ_CSV_FALSE;
itj_csv_umax max = csv->read_used;
while (i < max) {
__m256i b = _mm256_loadu_si256((__m256i *)(csv->read_base + i));
__m256i q = _mm256_cmpeq_epi8(b, Q);
itj_csv_u32 quotes_mask = _mm256_movemask_epi8(q);
if (quote_at_boundary) {
if ((quotes_mask >> 31) & 0x1 != 0x1) {
goto get_out;
}
got_doubles = ITJ_CSV_TRUE;
quote_at_boundary = false;
}
if (quotes_mask == 0) {
i += 32;
} else {
itj_csv_u32 acc = 0;
do {
itj_csv_u32 j = itj_csv_ffs(quotes_mask) - 1;
quotes_mask = quotes_mask >> j;
acc += j;
if (quotes_mask & 0x1) {
if (acc == 31) {
quote_at_boundary = true;
i += j;
quotes_mask = 0;
} else {
if ((quotes_mask & 0x3) == 3) {
j += 2;
quotes_mask = quotes_mask >> 2;
i += j;
got_doubles = ITJ_CSV_TRUE;
} else {
i += j;
goto get_out;
}
}
}
} while (quotes_mask);
}
}
get_out:
if (i >= max) {
rv.need_data = ITJ_CSV_TRUE;
return rv;
}
rv.data.base = &csv->read_base[start];
rv.data.len = i - start;
for (; i < max; ++i) {
itj_csv_u8 c = csv->read_base[i];
if (c == '\r' || c == '\n') {
if (i < max && c + csv->read_base[i] == '\r' + '\n') {
i += 2;
rv.is_end_of_line = ITJ_CSV_TRUE;
break;
} else if (c == '\n') {
i += 1;
rv.is_end_of_line = ITJ_CSV_TRUE;
break;
}
} else if (c == csv->delimiter) {
i++;
break;
}
}
if (got_doubles) {
rv.data.len = itj_csv_contract_double_quotes(rv.data.base, rv.data.len);
}
csv->read_iter = i;
csv->idx += 1;
return rv;
}
struct itj_csv_value itj_csv_parse_value_avx2(struct itj_csv *csv, itj_csv_umax i) {
csv->prev_read_iter = csv->read_iter;
struct itj_csv_value rv;
rv.data.base = NULL;
rv.data.len = 0;
rv.is_end_of_line = ITJ_CSV_FALSE;
rv.need_data = ITJ_CSV_FALSE;
rv.idx = csv->idx;
__m256i Q = _mm256_set1_epi8('\"');
__m256i delim = _mm256_set1_epi8(csv->delimiter);
__m256i R = _mm256_set1_epi8('\r');
__m256i N = _mm256_set1_epi8('\n');
itj_csv_umax start = i;
itj_csv_umax max = csv->read_used;
while (i < max) {
__m256i b = _mm256_loadu_si256((__m256i *)(csv->read_base + i));
__m256i q = _mm256_cmpeq_epi8(b, Q);
itj_csv_u32 quote_mask = _mm256_movemask_epi8(q);
__m256i m = _mm256_cmpeq_epi8(b, delim);
__m256i n = _mm256_cmpeq_epi8(b, N);
__m256i r = _mm256_cmpeq_epi8(b, R);
m = _mm256_or_si256(m, r);
m = _mm256_or_si256(m, n);
itj_csv_u32 mask = _mm256_movemask_epi8(m);
if (mask == 0) {
if (quote_mask != 0) {
i += itj_csv_ffs(quote_mask) - 1;
return itj_csv_parse_quotes_avx2(csv, i);
}
i += 32;
} else {
itj_csv_umax first_delim = itj_csv_ffs(mask) - 1;
if (quote_mask != 0) {
itj_csv_umax first_quote = itj_csv_ffs(quote_mask) - 1;
if (first_quote < first_delim) {
i += first_quote;
return itj_csv_parse_quotes_avx2(csv, i);
}
}
i += first_delim;
break;
}
}
if (i >= max) {
rv.need_data = ITJ_CSV_TRUE;
return rv;
}
rv.data.base = &csv->read_base[start];
rv.data.len = i - start;
itj_csv_u8 c = csv->read_base[i++];
if (c == '\r' || c == '\n') {
if (i < max && c + csv->read_base[i] == '\r' + '\n') {
i += 1;
rv.is_end_of_line = ITJ_CSV_TRUE;
} else if (c == '\n') {
rv.is_end_of_line = ITJ_CSV_TRUE;
}
}
csv->read_iter = i;
csv->idx += 1;
return rv;
}
struct itj_csv_value itj_csv_get_next_value_avx2(struct itj_csv *csv) {
return itj_csv_parse_value_avx2(csv, csv->read_iter);
}
#endif // ITJ_CSV_IMPLEMENTATION_AVX2
#ifndef ITJ_CSV_NO_STD
void itj_csv_close_fh(struct itj_csv *csv) {
if (csv->fh) {
fclose(csv->fh);
}
}
itj_csv_umax itj_csv_pump_stdio(struct itj_csv *csv) {
itj_csv_umax total_read = 0;
itj_csv_umax diff = 0;
itj_csv_smax ret;
if (csv->read_iter != 0) {
if (csv->read_iter == csv->prev_read_iter) {
if (csv->read_used > csv->read_iter) {
diff = csv->read_used - csv->read_iter;
if (diff <= csv->read_max) {
ITJ_CSV_MEMCPY(csv->read_base, csv->read_base + csv->read_iter, diff);
} else {
return 0;
}
}
}
}
csv->read_iter = 0;
csv->prev_read_iter = 0;
do {
ret = fread(csv->read_base + diff + total_read, 1, csv->read_max - diff - total_read, csv->fh);
if (ret < 0) {
return 0;
}
total_read += ret;
} while (ret != 0 && total_read < csv->read_max - diff);
csv->read_used = total_read + diff;
return total_read;
}
void itj_csv_open_fp(struct itj_csv *csv_out, FILE *fh, void *mem_buf, itj_csv_umax mem_buf_size, itj_csv_u8 delimiter, void *user_mem_ptr) {
csv_out->read_iter = 0;
csv_out->read_used = 0;
csv_out->read_base = (itj_csv_u8 *)mem_buf;
csv_out->read_max = mem_buf_size;
csv_out->delimiter = delimiter;
csv_out->fh = fh;
csv_out->user_mem_ptr = user_mem_ptr;
csv_out->idx = 0;
}
itj_csv_bool itj_csv_open(struct itj_csv *csv_out, const char *filepath, itj_csv_u32 filepath_len, void *mem_buf, itj_csv_umax mem_buf_size, itj_csv_u8 delimiter, void *user_mem_ptr) {
char *null_terminated = ITJ_CSV_ALLOC(user_mem_ptr, filepath_len + 1);
ITJ_CSV_MEMCPY(null_terminated, filepath, filepath_len);
null_terminated[filepath_len] = '\0';
FILE *fh = fopen(null_terminated, "rb");
if (!fh) {
return ITJ_CSV_FALSE;
}
itj_csv_open_fp(csv_out, fh, mem_buf, mem_buf_size, delimiter, user_mem_ptr);
return ITJ_CSV_TRUE;
}
#endif // ITJ_CSV_NO_STD
void itj_csv_open_memory(struct itj_csv *csv_out, void *mem_buf, itj_csv_umax mem_buf_size, itj_csv_u8 delimiter, void *user_mem_ptr) {
csv_out->read_iter = 0;
csv_out->read_used = mem_buf_size;
csv_out->read_base = (itj_csv_u8 *)mem_buf;
csv_out->read_max = mem_buf_size;
csv_out->delimiter = delimiter;
csv_out->user_mem_ptr = user_mem_ptr;
csv_out->idx = 0;
}
/*
------------------------------------------------------------------------------
This software is available under 2 licenses -- choose whichever you prefer.
------------------------------------------------------------------------------
ALTERNATIVE A - MIT License
Copyright (c) 2017 Ian Thomassen Jacobsen
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
------------------------------------------------------------------------------
ALTERNATIVE B - Public Domain (www.unlicense.org)
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
software, either in source code form or as a compiled binary, for any purpose,
commercial or non-commercial, and by any means.
In jurisdictions that recognize copyright laws, the author or authors of this
software dedicate any and all copyright interest in the software to the public
domain. We make this dedication for the benefit of the public at large and to
the detriment of our heirs and successors. We intend this dedication to be an
overt act of relinquishment in perpetuity of all present and future rights to
this software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
------------------------------------------------------------------------------
*/