summaryrefslogtreecommitdiffstats
path: root/test/parsecsv.c
blob: 82f3deebe9cbd3219d990305997612d2cf47fa08 (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
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
/* parsecsv.c

   Copyright (c) 2003-2015 HandBrake Team
   This file is part of the HandBrake source code
   Homepage: <http://handbrake.fr/>.
   It may be used under the terms of the GNU General Public License v2.
   For full terms see the file COPYING file or visit http://www.gnu.org/licenses/gpl-2.0.html
 */

#include <fcntl.h>
#include "hb.h"
#include "parsecsv.h"

/* Internal declarations */
#define is_newline(_x)      ( (_x) == 13 || \
                              (_x) == 11 || \
                              (_x) == 10 )

#define is_white(_x)        ( (_x) == '\t' || \
                              (_x) == ' '  || \
                              is_newline(_x) )

#define is_sep(_x)          ( (_x) == ',' )

#define is_esc(_x)          ( (_x) == '\\' )

#define CSV_CHAR_ERROR      0x8000
#define CSV_CHAR_EOF        0x4000
#define CSV_CHAR_ROWSEP     0x2000
#define CSV_CHAR_COLSEP     0x1000

#define CSV_PARSE_NORMAL    0x0000
#define CSV_PARSE_SEEK      0x0001
#define CSV_PARSE_ESC       0x0002

static uint16_t hb_parse_character( hb_csv_file_t * file );
static void hb_trim_end( char *text );

/* Open a CSV File */
hb_csv_file_t *hb_open_csv_file( const char *filepath )
{
    hb_csv_file_t *file = NULL;
    FILE * fileref;

    if( filepath == NULL )
    {
        return file;
    }

    fileref = hb_fopen(filepath, "r");
    if( fileref == NULL )
    {
        return file;
    }

    file = malloc( sizeof( hb_csv_file_t ) );
    if( file == NULL )
    {
        return file;
    }

    file->fileref       = fileref;
    file->eof           = 0;
    file->parse_state   = CSV_PARSE_SEEK;
    file->curr_col      = 0;
    file->curr_row      = 0;
    return file;
}

void hb_close_csv_file( hb_csv_file_t *file )
{
    if( file == NULL )
    {
        return;
    }

    fclose( file->fileref );
    free( file );
}

/* Parse CSV Cells */
hb_csv_cell_t *hb_read_next_cell( hb_csv_file_t *file )
{
    hb_csv_cell_t *cell = NULL;
    uint16_t c;
    int index;

    if( file == NULL  )
    {
        return cell;
    }

    if( file->eof )
    {
        return cell;
    }

    cell = malloc( sizeof( hb_csv_cell_t ) );
    if( cell == NULL )
    {
        return cell;
    }

    cell->cell_row = file->curr_row;
    cell->cell_col = file->curr_col;
    index = 0;
    while( CSV_CHAR_EOF != (c = hb_parse_character( file ) ) )
    {
        if( c == CSV_CHAR_ROWSEP )
        {
            file->curr_row++;
            file->curr_col = 0;
            break;
        }
        else if( c == CSV_CHAR_COLSEP )
        {
            file->curr_col++;
            break;
        }
        else
        {
            if( index < 1023 )
            {
                cell->cell_text[index] = (char)c;
                index++;
            }
        }
    }

    if( c == CSV_CHAR_EOF )
    {
        file->eof = 1;
    }

    /* Terminate the cell text */
    cell->cell_text[index] = '\0';
    hb_trim_end( cell->cell_text );
    return cell;
}

void hb_dispose_cell( hb_csv_cell_t *cell )
{
    if( cell == NULL )
    {
        return;
    }

    free( cell );
}

/* Raw parsing */
static uint16_t hb_parse_character( hb_csv_file_t * file )
{
    int byte;
    uint16_t c = 0;
    int need_char = 1;

    if( file == NULL )
    {
        return CSV_CHAR_ERROR;
    }

    while( need_char )
    {
        byte = fgetc( file->fileref );
        if( feof( file->fileref ) )
        {
            return CSV_CHAR_EOF;
        }
        if( ferror( file->fileref ) )
        {
            return CSV_CHAR_ERROR;
        }

        if( file->parse_state == CSV_PARSE_SEEK && is_white(byte) )
        {
            continue;
        }
        else if( file->parse_state != CSV_PARSE_ESC && is_esc(byte) )
        {
            file->parse_state = CSV_PARSE_ESC;
            continue;
        }
        else if( file->parse_state != CSV_PARSE_ESC && is_sep(byte) )
        {
            file->parse_state = CSV_PARSE_SEEK;
            need_char = 0;
            c = CSV_CHAR_COLSEP;
        }
        else if( file->parse_state == CSV_PARSE_ESC )
        {
            file->parse_state = CSV_PARSE_NORMAL;
            need_char = 0;
            c = (uint16_t)byte;
        }
        else if( is_newline(byte) )
        {
            file->parse_state = CSV_PARSE_SEEK;
            need_char = 0;
            c = CSV_CHAR_ROWSEP;
        }
        else
        {
            file->parse_state = CSV_PARSE_NORMAL;
            need_char = 0;
            c = (uint16_t)byte;
        }
    }

    return c;
}

static void hb_trim_end( char *text )
{
    if( text == NULL )
    {
        return;
    }

    int i;

    for( i = strlen(text) - 1; i >= 0 && is_white(text[i]) ; i-- )
    {
        text[i] = '\0';
    }
}