]>
git.datanom.net - gtk_lensdatabase.git/blob - src/csv-parser/csv-parser.c
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:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
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.
26 #include "csv-parser.h"
28 static int count_fields( const char *line
) {
32 for ( cnt
= 1, fQuote
= 0, ptr
= line
; *ptr
; ptr
++ ) {
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.
64 char **parse_csv( const char *line
) {
65 char **buf
, **bptr
, *tmp
, *tptr
;
67 int fieldcnt
, fQuote
, fEnd
;
73 fieldcnt
= count_fields( line
);
75 if ( fieldcnt
== -1 ) {
79 buf
= malloc( sizeof(char*) * (fieldcnt
+1) );
85 tmp
= malloc( strlen(line
) + 1 );
94 for ( ptr
= line
, fQuote
= 0, *tmp
= '\0', tptr
= tmp
, fEnd
= 0; ; ptr
++ ) {
100 if ( *ptr
== '\"' ) {
101 if ( ptr
[1] == '\"' ) {
123 *bptr
= strdup( tmp
);
126 for ( bptr
--; bptr
>= buf
; bptr
-- ) {
This page took 0.708567 seconds and 6 git commands to generate.