-
Notifications
You must be signed in to change notification settings - Fork 36
/
csvtable.c
1683 lines (1556 loc) · 34.3 KB
/
csvtable.c
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
/**
* @file csvtable.c
* SQLite extension module for mapping a CSV file as
* a read-only SQLite virtual table plus extension
* function to import a CSV file as a real table.
*
* 2012 July 27
*
* The author disclaims copyright to this source code. In place of
* a legal notice, here is a blessing:
*
* May you do good and not evil.
* May you find forgiveness for yourself and forgive others.
* May you share freely, never taking more than you give.
*
********************************************************************
*/
#ifdef STANDALONE
#include <sqlite3.h>
#else
#include <sqlite3ext.h>
static SQLITE_EXTENSION_INIT1
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#ifdef _WIN32
#include <windows.h>
#define strcasecmp _stricmp
#define strncasecmp _strnicmp
#endif
/**
* @typedef csv_file
* @struct csv_file
* Structure to implement CSV file handle.
*/
typedef struct csv_file {
FILE *f; /**< CSV file */
char *sep; /**< column separator characters */
char *quot; /**< text quoting characters */
int isdos; /**< true, when DOS format detected */
int maxl; /**< max. capacity of line buffer */
char *line; /**< line buffer */
long pos0; /**< file position for rewind */
int maxc; /**< max. capacity of column buffer */
int ncols; /**< number of columns */
char **cols; /**< column buffer */
} csv_file;
/**
* @typedef csv_guess_fmt
* @struct csv_guess_fmt
* Info to guess CSV layout.
*/
typedef struct csv_guess_fmt {
int nlines;
int hist[256];
} csv_guess_fmt;
/**
* @typedef csv_vtab
* @struct csv_vtab
* Structure to describe a CSV virtual table.
*/
typedef struct csv_vtab {
sqlite3_vtab vtab; /**< SQLite virtual table */
csv_file *csv; /**< CSV file handle */
int convert; /**< convert flags */
char coltypes[1]; /**< column types */
} csv_vtab;
/**
* @typedef csv_cursor
* @struct csv_cursor
* Structure to describe CSV virtual table cursor.
*/
typedef struct {
sqlite3_vtab_cursor cursor; /**< SQLite virtual table cursor */
long pos; /**< CSV file position */
} csv_cursor;
/**
* Free dynamically allocated string buffer
* @param in input string pointer
*/
static void
append_free(char **in)
{
long *p = (long *) *in;
if (p) {
p -= 2;
sqlite3_free(p);
*in = 0;
}
}
/**
* Append a string to dynamically allocated string buffer
* with optional quoting
* @param in input string pointer
* @param append string to append
* @param quote quote character or NUL
* @result new string to be free'd with append_free()
*/
static char *
append(char **in, char const *append, char quote)
{
long *p = (long *) *in;
long len, maxlen, actlen;
int i;
char *pp;
int nappend = append ? strlen(append) : 0;
if (p) {
p -= 2;
maxlen = p[0];
actlen = p[1];
} else {
maxlen = actlen = 0;
}
len = nappend + actlen;
if (quote) {
len += 2;
for (i = 0; i < nappend; i++) {
if (append[i] == quote) {
len++;
}
}
} else if (!nappend) {
return *in;
}
if (len >= maxlen - 1) {
long *q;
maxlen = (len + 0x03ff) & (~0x3ff);
q = (long *) sqlite3_realloc(p, maxlen + 1 + 2 * sizeof (long));
if (!q) {
return 0;
}
if (!p) {
q[1] = 0;
}
p = q;
p[0] = maxlen;
*in = (char *) (p + 2);
}
pp = *in + actlen;
if (quote) {
*pp++ = quote;
for (i = 0; i < nappend; i++) {
*pp++ = append[i];
if (append[i] == quote) {
*pp++ = quote;
}
}
*pp++ = quote;
*pp = '\0';
} else {
if (nappend) {
memcpy(pp, append, nappend);
pp += nappend;
*pp = '\0';
}
}
p[1] = pp - *in;
return *in;
}
/**
* Strip off quotes given string.
* @param in string to be processed
* @result new string to be free'd with sqlite3_free()
*/
static char *
unquote(char const *in)
{
char c, *ret;
int i;
ret = sqlite3_malloc(strlen(in) + 1);
if (ret) {
c = in[0];
if ((c == '"') || (c == '\'')) {
i = strlen(in + 1);
if ((i > 0) && (in[i] == c)) {
strcpy(ret, in + 1);
ret[i - 1] = '\0';
return ret;
}
}
strcpy(ret, in);
}
return ret;
}
/**
* Map string to SQLite data type.
* @param type string to be mapped
* @result SQLITE_TEXT et.al.
*/
static int
maptype(char const *type)
{
int typelen = type ? strlen(type) : 0;
if ((typelen >= 3) &&
(strncasecmp(type, "integer", 7) == 0)) {
return SQLITE_INTEGER;
}
if ((typelen >= 6) &&
(strncasecmp(type, "double", 6) == 0)) {
return SQLITE_FLOAT;
}
if ((typelen >= 5) &&
(strncasecmp(type, "float", 5) == 0)) {
return SQLITE_FLOAT;
}
if ((typelen >= 4) &&
(strncasecmp(type, "real", 4) == 0)) {
return SQLITE_FLOAT;
}
return SQLITE_TEXT;
}
/**
* Convert and collapse white space in column names to underscore
* @param names string vector of column names
* @param ncols number of columns
*/
static void
conv_names(char **names, int ncols)
{
int i;
char *p, *q;
static const char ws[] = "\n\t\r\b\v ";
if (!names || ncols <= 0) {
return;
}
for (i = 0; i < ncols; i++) {
p = names[i];
while (*p) {
if (strchr(ws, *p)) {
*p++ = '_';
q = p;
while (*q && strchr(ws, *q)) {
++q;
}
if (*q && q > p) {
strcpy(p, q);
}
continue;
}
++p;
}
}
}
/**
* Make result data or parameter binding accoring to type
* @param ctx SQLite function context or NULL
* @param stmt SQLite statement or NULL
* @param idx parameter number, 1-based
* @param data string data
* @param len string length
* @param type SQLite type
*/
static void
result_or_bind(sqlite3_context *ctx, sqlite3_stmt *stmt, int idx,
char *data, int len, int type)
{
char *endp;
if (!data) {
if (ctx) {
sqlite3_result_null(ctx);
} else {
sqlite3_bind_null(stmt, idx);
}
return;
}
if (type == SQLITE_INTEGER) {
sqlite_int64 val;
#if defined(_WIN32) || defined(_WIN64)
char endc;
if (sscanf(data, "%I64d%c", &val, &endc) == 1) {
if (ctx) {
sqlite3_result_int64(ctx, val);
} else {
sqlite3_bind_int64(stmt, idx, val);
}
return;
}
#else
endp = 0;
#ifdef __osf__
val = strtol(data, &endp, 0);
#else
val = strtoll(data, &endp, 0);
#endif
if (endp && (endp != data) && !*endp) {
if (ctx) {
sqlite3_result_int64(ctx, val);
} else {
sqlite3_bind_int64(stmt, idx, val);
}
return;
}
#endif
} else if (type == SQLITE_FLOAT) {
double val;
endp = 0;
val = strtod(data, &endp);
if (endp && (endp != data) && !*endp) {
if (ctx) {
sqlite3_result_double(ctx, val);
} else {
sqlite3_bind_double(stmt, idx, val);
}
return;
}
}
if (ctx) {
sqlite3_result_text(ctx, data, len, SQLITE_TRANSIENT);
} else {
sqlite3_bind_text(stmt, idx, data, len, SQLITE_TRANSIENT);
}
}
/**
* Process one column of the current row
* @param ctx SQLite function context or NULL
* @param stmt SQLite statement or NULL
* @param idx parameter index, 1-based
* @param data string data
* @param type SQLite type
* @param conv conversion flags
*/
static int
process_col(sqlite3_context *ctx, sqlite3_stmt *stmt, int idx,
char *data, int type, int conv)
{
char c, *p;
const char flchars[] = "Ee+-.,0123456789";
if (!data) {
goto putdata;
}
/*
* Floating point number test,
* converts single comma to dot.
*/
c = data[0];
if ((c != '\0') && strchr(flchars + 2, c)) {
p = data + 1;
while (*p && strchr(flchars, *p)) {
++p;
}
if (*p == '\0') {
char *first = 0;
int n = 0;
p = data;
while (p) {
p = strchr(p, ',');
if (!p) {
break;
}
if (++n == 1) {
first = p;
}
++p;
}
if (first) {
*first = '.';
goto putdata;
}
}
}
if (conv) {
char *utf = sqlite3_malloc(strlen(data) * 2 + 2);
if (utf) {
p = utf;
while ((c = *data) != '\0') {
if (((conv & 10) == 10) && (c == '\\')) {
if (data[1] == 'q') {
*p++ = '\'';
data += 2;
continue;
}
}
if ((conv & 2) && (c == '\\')) {
char c2 = data[1];
switch (c2) {
case '\0':
goto convdone;
case 'n':
*p = '\n';
break;
case 't':
*p = '\t';
break;
case 'r':
*p = '\r';
break;
case 'f':
*p = '\f';
break;
case 'v':
*p = '\v';
break;
case 'b':
*p = '\b';
break;
case 'a':
*p = '\a';
break;
case '?':
*p = '\?';
break;
case '\'':
*p = '\'';
break;
case '"':
*p = '\"';
break;
case '\\':
*p = '\\';
break;
default:
*p++ = c;
*p = c2;
break;
}
p++;
data += 2;
continue;
}
if ((conv & 1) && (c & 0x80)) {
*p++ = 0xc0 | ((c >> 6) & 0x1f);
*p++ = 0x80 | (c & 0x3f);
} else {
*p++ = c;
}
data++;
}
convdone:
*p = '\0';
result_or_bind(ctx, stmt, idx, utf, p - utf, type);
sqlite3_free(utf);
return SQLITE_OK;
} else {
if (ctx) {
sqlite3_result_error(ctx, "out of memory", -1);
}
return SQLITE_NOMEM;
}
}
putdata:
result_or_bind(ctx, stmt, idx, data, -1, type);
return SQLITE_OK;
}
/**
* Open CSV file for reading and return handle to it.
* @param filename name of CSV file
* @param sep column separator characters or NULL
* @param quot string quote characters or NULL
* @result CSV file handle
*/
static csv_file *
csv_open(const char *filename, const char *sep, const char *quot)
{
FILE *f;
csv_file *csv;
#ifdef _WIN32
f = fopen(filename, "rb");
#else
f = fopen(filename, "r");
#endif
if (!f) {
return 0;
}
csv = sqlite3_malloc(sizeof (csv_file));
if (!csv) {
error0:
fclose(f);
return 0;
}
csv->f = f;
if (sep && sep[0]) {
csv->sep = sqlite3_malloc(strlen(sep) + 1);
if (!csv->sep) {
error1:
sqlite3_free(csv);
goto error0;
}
strcpy(csv->sep, sep);
} else {
csv->sep = 0;
}
if (quot && quot[0]) {
csv->quot = sqlite3_malloc(strlen(quot) + 1);
if (!csv->quot) {
if (csv->sep) {
sqlite3_free(csv->sep);
}
goto error1;
}
strcpy(csv->quot, quot);
} else {
csv->quot = 0;
}
csv->isdos = 0;
csv->maxl = 0;
csv->line = 0;
csv->pos0 = 0;
csv->maxc = 0;
csv->ncols = 0;
csv->cols = 0;
return csv;
}
/**
* Close CSV file handle.
* @param csv CSV file handle
*/
static void
csv_close(csv_file *csv)
{
if (csv) {
if (csv->sep) {
sqlite3_free(csv->sep);
}
if (csv->quot) {
sqlite3_free(csv->quot);
}
if (csv->line) {
sqlite3_free(csv->line);
}
if (csv->cols) {
sqlite3_free(csv->cols);
}
if (csv->f) {
fclose(csv->f);
}
sqlite3_free(csv);
}
}
/**
* Test EOF on CSV file handle.
* @param csv CSV file handle
* @result true when file position is at EOF
*/
static int
csv_eof(csv_file *csv)
{
if (csv && csv->f) {
return feof(csv->f);
}
return 1;
}
/**
* Position CSV file handle.
* @param csv CSV file handle
* @param pos position to seek
* @result 0 on success, EOF on error
*/
static long
csv_seek(csv_file *csv, long pos)
{
if (csv && csv->f) {
return fseek(csv->f, pos, SEEK_SET);
}
return EOF;
}
/**
* Rewind CSV file handle.
* @param csv CSV file handle
*/
static void
csv_rewind(csv_file *csv)
{
if (csv && csv->f) {
csv_seek(csv, csv->pos0);
}
}
/**
* Return current position of CSV file handle.
* @param csv CSV file handle
* @result current file position
*/
static long
csv_tell(csv_file *csv)
{
if (csv && csv->f) {
return ftell(csv->f);
}
return EOF;
}
/**
* Read and process one line of CSV file handle.
* @param csv CSV file handle
* @param guess NULL or buffer for guessing file format
* @result number of columns on success, EOF on error
*/
static int
csv_getline(csv_file *csv, csv_guess_fmt *guess)
{
int i, index = 0, inq = 0, c, col;
char *p, *sep;
if (!csv || !csv->f) {
return EOF;
}
while (1) {
c = fgetc(csv->f);
if (c == EOF) {
if (index > 0) {
break;
}
return EOF;
}
if (c == '\0') {
continue;
}
if (c == '\r') {
int c2 = fgetc(csv->f);
c = '\n';
if (c2 == '\n') {
csv->isdos = 1;
} else if (c2 != EOF) {
ungetc(c2, csv->f);
}
}
/* check for DOS EOF (Ctrl-Z) */
if (csv->isdos && (c == '\032')) {
int c2 = fgetc(csv->f);
if (c2 == EOF) {
if (index > 0) {
break;
}
return EOF;
}
ungetc(c2, csv->f);
}
if (index >= csv->maxl - 1) {
int n = csv->maxl * 2;
char *line;
if (n <= 0) {
n = 4096;
}
line = sqlite3_malloc(n);
if (!line) {
return EOF;
}
if (csv->line) {
memcpy(line, csv->line, index);
sqlite3_free(csv->line);
}
csv->maxl = n;
csv->line = line;
}
csv->line[index++] = c;
if (csv->quot && (p = strchr(csv->quot, c))) {
if (inq) {
if (*p == inq) {
inq = 0;
}
} else {
inq = *p;
}
}
if (!inq && (c == '\n')) {
break;
}
}
if (guess) {
for (i = 0; i < index; i++) {
guess->hist[csv->line[i] & 0xFF] += 1;
}
guess->nlines += 1;
csv->ncols = 0;
return 0;
}
for (i = index - 1; i >= 0; i--) {
if (csv->line[i] != '\n') {
break;
}
}
index = i + 1;
csv->line[index] = '\0';
i = inq = col = 0;
sep = csv->sep ? csv->sep : ";";
if (!csv->cols) {
int n = 128;
csv->cols = sqlite3_malloc(sizeof (char *) * n);
if (!csv->cols) {
return EOF;
}
csv->maxc = n;
}
csv->cols[col++] = csv->line;
while (i < index) {
if (csv->quot && (p = strchr(csv->quot, csv->line[i]))) {
if (inq) {
if (*p == inq) {
inq = 0;
}
} else {
inq = *p;
}
}
if (!inq && (p = strchr(sep, csv->line[i]))) {
p = csv->line + i;
*p = '\0';
if (col >= csv->maxc) {
int n = csv->maxc * 2;
char **cols;
cols = sqlite3_realloc(csv->cols, sizeof (char *) * n);
if (!cols) {
return EOF;
}
csv->cols = cols;
csv->maxc = n;
}
csv->cols[col++] = p + 1;
}
++i;
}
csv->ncols = col;
/* strip off quotes */
if (csv->quot) {
for (i = 0; i < col; i++) {
if (*csv->cols[i]) {
p = strchr(csv->quot, *csv->cols[i]);
if (p) {
char *src, *dst;
c = *p;
csv->cols[i] += 1;
sep = csv->cols[i] + strlen(csv->cols[i]) - 1;
if ((sep >= csv->cols[i]) && (*sep == c)) {
*sep = '\0';
}
/* collapse quote escape sequences */
src = csv->cols[i];
dst = 0;
while (*src) {
if ((*src == c) && (src[1] == c)) {
if (!dst) {
dst = src;
}
src++;
while (*src) {
*dst++ = *src++;
if (*src == c) {
--src;
break;
}
}
}
++src;
}
if (dst) {
*dst++ = '\0';
}
}
}
}
}
return col;
}
/**
* Return number of columns of current row in CSV file.
* @param csv CSV file handle
* @result number of columns of current row
*/
static int
csv_ncols(csv_file *csv)
{
if (csv && csv->cols) {
return csv->ncols;
}
return 0;
}
/**
* Return nth column of current row in CSV file.
* @param csv CSV file handle
* @param n column number
* @result string pointer or NULL
*/
static char *
csv_coldata(csv_file *csv, int n)
{
if (csv && csv->cols && (n >= 0) && (n < csv->ncols)) {
return csv->cols[n];
}
return 0;
}
/**
* Guess CSV layout of CSV file handle.
* @param csv CSV file handle
* @result 0 on succes, EOF on error
*/
static int
csv_guess(csv_file *csv)
{
csv_guess_fmt guess;
int i, n;
char *p, sep[32], quot[4];
const struct {
int c;
int min;
} sep_test[] = {
{ ',', 2 },
{ ';', 2 },
{ '\t', 2 },
{ ' ', 4 },
{ '|', 2 }
};
if (!csv) {
return EOF;
}
memset(&guess, 0, sizeof (guess));
csv->pos0 = 0;
csv_rewind(csv);
for (i = n = 0; i < 10; i++) {
n = csv_getline(csv, &guess);
if (n == EOF) {
break;
}
}
csv_rewind(csv);
if (n && !i) {
return EOF;
}
p = quot;
n = '"';
if (guess.hist[n] > 1) {
*p++ = n;
}
n = '\'';
if (guess.hist[n] > 1) {
*p++ = n;
}
*p = '\0';
p = sep;
for (i = 0; i < sizeof (sep_test) / sizeof (sep_test[0]); i++) {
if (guess.hist[sep_test[i].c] > sep_test[i].min * guess.nlines) {
*p++ = sep_test[i].c;
}
}
*p = '\0';
if (quot[0]) {
p = sqlite3_malloc(strlen(quot) + 1);
if (p) {
strcpy(p, quot);
if (csv->quot) {
sqlite3_free(csv->quot);
}
csv->quot = p;
} else {
return EOF;
}
}
if (sep[0]) {
p = sqlite3_malloc(strlen(sep) + 1);
if (p) {
strcpy(p, sep);
if (csv->sep) {
sqlite3_free(csv->sep);
}
csv->sep = p;
} else {
return EOF;
}
}
return 0;
}
/**
* Connect to virtual table
* @param db SQLite database pointer
* @param aux user specific pointer (unused)
* @param argc argument count
* @param argv argument vector
* @param vtabp pointer receiving virtual table pointer
* @param errp pointer receiving error messag
* @result SQLite error code
*
* Argument vector contains:
*
* argv[0] - module name<br>
* argv[1] - database name<br>
* argv[2] - table name (virtual table)<br>
* argv[3] - filename (required)<br>
* argv[4] - number, when non-zero use first line as column names,
* when negative use given type names (optional)<br>
* argv[5] - number, when non-zero, translate data (optional, see below)<br>
* argv[6] - column separator characters (optional)<br>
* argv[7] - string quoting characters (optional)<br>
* argv[8] - column/type name for first column (optional)<br>
* ..<br>
* argv[X] - column/type name for last column (optional)<br><br>
*
* Translation flags:
*
* 1 - convert ISO-8859-1 to UTF-8<br>
* 2 - perform backslash substitution<br>
* 4 - convert and collapse white-space in column names to underscore<br>
* 10 - convert \q to single quote, in addition to backslash substitution<br>
*/
static int
csv_vtab_connect(sqlite3* db, void *aux, int argc, const char * const *argv,
sqlite3_vtab **vtabp, char **errp)
{
csv_file *csv;
int rc = SQLITE_ERROR, i, k, ncnames, row1, *colmap = 0;
char **cnames, *schema = 0, **nargv;
csv_vtab *vtab = 0;
if (argc < 4) {
*errp = sqlite3_mprintf("input file name missing");
return SQLITE_ERROR;
}
nargv = sqlite3_malloc(sizeof (char *) * argc);
memset(nargv, 0, sizeof (char *) * argc);
for (i = 3; i < argc; i++) {
nargv[i] = unquote(argv[i]);
}
csv = csv_open(nargv[3], (argc > 6) ? nargv[6] : 0,
(argc > 7) ? nargv[7] : 0);
if (!csv) {
*errp = sqlite3_mprintf("unable to open input file");
cleanup:
append_free(&schema);
for (i = 3; i < argc; i++) {
if (nargv[i]) {
sqlite3_free(nargv[i]);
}
}
if (vtab) {
sqlite3_free(vtab);
}
if (colmap) {
sqlite3_free(colmap);
}
return rc;