Moves the filters' logging info to work.c, adds parameter info. I also changed the...
[HandBrake.git] / test / parsecsv.c
blobbfc6a2783c5217b3a5b5be9c07db0e95a74dd0c5
1 /* $Id: parsecsv.c $
3 This file is part of the HandBrake source code.
4 Homepage: <http://handbrake.m0k.org/>.
5 It may be used under the terms of the GNU General Public License. */
7 #include <fcntl.h>
8 #include "hb.h"
9 #include "parsecsv.h"
11 /* Internal declarations */
12 #define is_newline(_x) ( (_x) == 13 || \
13 (_x) == 11 || \
14 (_x) == 10 )
16 #define is_white(_x) ( (_x) == '\t' || \
17 (_x) == ' ' || \
18 is_newline(_x) )
20 #define is_sep(_x) ( (_x) == ',' )
22 #define is_esc(_x) ( (_x) == '\\' )
24 #define CSV_CHAR_ERROR 0x8000
25 #define CSV_CHAR_EOF 0x4000
26 #define CSV_CHAR_ROWSEP 0x2000
27 #define CSV_CHAR_COLSEP 0x1000
29 #define CSV_PARSE_NORMAL 0x0000
30 #define CSV_PARSE_SEEK 0x0001
31 #define CSV_PARSE_ESC 0x0002
33 static uint16_t hb_parse_character( hb_csv_file_t * file );
34 static void hb_trim_end( char *text );
36 /* Open a CSV File */
37 hb_csv_file_t *hb_open_csv_file( const char *filepath )
39 hb_csv_file_t *file = NULL;
40 FILE * fileref;
42 if( filepath == NULL )
44 return file;
47 fileref = fopen( filepath, "r" );
48 if( fileref == NULL )
50 return file;
53 file = malloc( sizeof( hb_csv_file_t ) );
54 file->fileref = fileref;
55 file->eof = 0;
56 file->parse_state = CSV_PARSE_SEEK;
57 file->curr_col = 0;
58 file->curr_row = 0;
59 return file;
62 void hb_close_csv_file( hb_csv_file_t *file )
64 if( file == NULL )
66 return;
69 fclose( file->fileref );
70 free( file );
73 /* Parse CSV Cells */
74 hb_csv_cell_t *hb_read_next_cell( hb_csv_file_t *file )
76 hb_csv_cell_t *cell = NULL;
77 uint16_t c;
78 int index;
80 if( file == NULL )
82 return cell;
85 if( file->eof )
87 return cell;
90 cell = malloc( sizeof( hb_csv_cell_t ) );
91 cell->cell_row = file->curr_row;
92 cell->cell_col = file->curr_col;
93 index = 0;
94 while( CSV_CHAR_EOF != (c = hb_parse_character( file ) ) )
96 if( c == CSV_CHAR_ROWSEP )
98 file->curr_row++;
99 file->curr_col = 0;
100 break;
102 else if( c == CSV_CHAR_COLSEP )
104 file->curr_col++;
105 break;
107 else
109 if( index < 1023 )
111 cell->cell_text[index] = (char)c;
112 index++;
117 if( c == CSV_CHAR_EOF )
119 file->eof = 1;
122 /* Terminate the cell text */
123 cell->cell_text[index] = '\0';
124 hb_trim_end( cell->cell_text );
125 return cell;
128 void hb_dispose_cell( hb_csv_cell_t *cell )
130 if( cell == NULL )
132 return;
135 free( cell );
138 /* Raw parsing */
139 static uint16_t hb_parse_character( hb_csv_file_t * file )
141 int byte;
142 uint16_t c;
143 int need_char = 1;
145 if( file == NULL )
147 return CSV_CHAR_ERROR;
150 while( need_char )
152 byte = fgetc( file->fileref );
153 if( feof( file->fileref ) )
155 return CSV_CHAR_EOF;
157 if( ferror( file->fileref ) )
159 return CSV_CHAR_ERROR;
162 if( file->parse_state == CSV_PARSE_SEEK && is_white(byte) )
164 continue;
166 else if( file->parse_state != CSV_PARSE_ESC && is_esc(byte) )
168 file->parse_state = CSV_PARSE_ESC;
169 continue;
171 else if( file->parse_state != CSV_PARSE_ESC && is_sep(byte) )
173 file->parse_state = CSV_PARSE_SEEK;
174 need_char = 0;
175 c = CSV_CHAR_COLSEP;
177 else if( file->parse_state == CSV_PARSE_ESC )
179 file->parse_state = CSV_PARSE_NORMAL;
180 need_char = 0;
181 c = (uint16_t)byte;
183 else if( is_newline(byte) )
185 file->parse_state = CSV_PARSE_SEEK;
186 need_char = 0;
187 c = CSV_CHAR_ROWSEP;
189 else
191 file->parse_state = CSV_PARSE_NORMAL;
192 need_char = 0;
193 c = (uint16_t)byte;
197 return c;
200 static void hb_trim_end( char *text )
202 if( text == NULL )
204 return;
207 int i = strlen(text) - 1;
209 for( i = strlen(text) - 1; i >= 0 && is_white(text[i]) ; i-- )
211 text[i] = '\0';