aboutsummaryrefslogtreecommitdiff
path: root/src/csv.c
blob: 979e97a6496b708477d16b75abca48f07799ae72 (plain)
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
#include "globals.h"

/* takes a pointer to a pointer to a csv string */
/* the pointer pointed to will be modified, but the data it points to will not */

char *csv_read(char **ptr)
{
    if(!ptr)
        return NULL;

    char *start = *ptr;
    bool quoted = false;

    while(**ptr)
    {
        char c = **ptr;

        if(c == '"')
        {
            quoted = !quoted;
        }

        else if((c == ',' && !quoted)  ||
                c == '\0'              ||
                c == '\n')
        {
            char *ret = malloc(*ptr - start + 1);
            ret[*ptr - start] = '\0';
            memcpy(ret, start, *ptr - start);
            (*ptr)++;

            return ret;
        }

        (*ptr)++;
    }

    return NULL;
}