]> git.datanom.net - lensdb.git/blob - src/csv-parser/csv-parser.c
before transition to proper ide
[lensdb.git] / src / csv-parser / csv-parser.c
1 /*
2 * csv.c
3 *
4 * Copyright 2015--2021 Samuel Alexander
5 * Permission is hereby granted, free of charge, to any person
6 * obtaining a copy of this software and associated documentation files
7 * (the "Software"), to deal in the Software without restriction,
8 * including without limitation the rights to use, copy, modify, merge,
9 * publish, distribute, sublicense, and/or sell copies of the Software,
10 * and to permit persons to whom the Software is furnished to do so,
11 * subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *
24 */
25
26 #include "csv-parser.h"
27
28 static int count_fields( const char *line ) {
29 const char *ptr;
30 int cnt, fQuote;
31
32 for ( cnt = 1, fQuote = 0, ptr = line; *ptr; ptr++ ) {
33 if ( fQuote ) {
34 if ( *ptr == '\"' ) {
35 fQuote = 0;
36 }
37 continue;
38 }
39
40 switch( *ptr ) {
41 case '\"':
42 fQuote = 1;
43 continue;
44 case ',':
45 cnt++;
46 continue;
47 default:
48 continue;
49 }
50 }
51
52 if ( fQuote ) {
53 return -1;
54 }
55
56 return cnt;
57 }
58
59 /*
60 * Given a string containing no linebreaks, or containing line breaks
61 * which are escaped by "double quotes", extract a NULL-terminated
62 * array of strings, one for every cell in the row.
63 */
64 char **parse_csv( const char *line ) {
65 char **buf, **bptr, *tmp, *tptr;
66 const char *ptr;
67 int fieldcnt, fQuote, fEnd;
68
69 if ( line == NULL ) {
70 return NULL;
71 }
72
73 fieldcnt = count_fields( line );
74
75 if ( fieldcnt == -1 ) {
76 return NULL;
77 }
78
79 buf = malloc( sizeof(char*) * (fieldcnt+1) );
80
81 if ( !buf ) {
82 return NULL;
83 }
84
85 tmp = malloc( strlen(line) + 1 );
86
87 if ( !tmp ) {
88 free( buf );
89 return NULL;
90 }
91
92 bptr = buf;
93
94 for ( ptr = line, fQuote = 0, *tmp = '\0', tptr = tmp, fEnd = 0; ; ptr++ ) {
95 if ( fQuote ) {
96 if ( !*ptr ) {
97 break;
98 }
99
100 if ( *ptr == '\"' ) {
101 if ( ptr[1] == '\"' ) {
102 *tptr++ = '\"';
103 ptr++;
104 continue;
105 }
106 fQuote = 0;
107 }
108 else {
109 *tptr++ = *ptr;
110 }
111
112 continue;
113 }
114
115 switch( *ptr ) {
116 case '\"':
117 fQuote = 1;
118 continue;
119 case '\0':
120 fEnd = 1;
121 case ',':
122 *tptr = '\0';
123 *bptr = strdup( tmp );
124
125 if ( !*bptr ) {
126 for ( bptr--; bptr >= buf; bptr-- ) {
127 free( *bptr );
128 }
129 free( buf );
130 free( tmp );
131
132 return NULL;
133 }
134
135 bptr++;
136 tptr = tmp;
137
138 if ( fEnd ) {
139 break;
140 } else {
141 continue;
142 }
143
144 default:
145 *tptr++ = *ptr;
146 continue;
147 }
148
149 if ( fEnd ) {
150 break;
151 }
152 }
153
154 *bptr = NULL;
155 free( tmp );
156 return buf;
157 }
This page took 0.39705 seconds and 6 git commands to generate.