summaryrefslogtreecommitdiff
path: root/csv-test.c
diff options
context:
space:
mode:
authorThomas Schmucker <ts@its1.de>2020-06-17 15:30:24 +0200
committerThomas Schmucker <ts@its1.de>2020-06-17 15:30:24 +0200
commit062053677f578b273110372d5c8f6778077e4feb (patch)
treedc1fbe7aaf1f63721366d83cb6a3429a1bf57def /csv-test.c
parent2873f39e5e1f41d79f689994948196a77da47671 (diff)
downloadlibcsv-062053677f578b273110372d5c8f6778077e4feb.tar.gz
libcsv-062053677f578b273110372d5c8f6778077e4feb.tar.bz2
libcsv-062053677f578b273110372d5c8f6778077e4feb.zip
aufräumen...
Diffstat (limited to 'csv-test.c')
-rw-r--r--csv-test.c117
1 files changed, 27 insertions, 90 deletions
diff --git a/csv-test.c b/csv-test.c
index 55a41bc..e16091c 100644
--- a/csv-test.c
+++ b/csv-test.c
@@ -149,75 +149,32 @@ static csv_options_t default_csv_options = {
149}; 149};
150 150
151typedef struct { 151typedef struct {
152 FILE *in;
153 csv_options_t *csv_options; 152 csv_options_t *csv_options;
154 csv_string_t csv_string; 153 csv_string_t csv_string;
155 csv_field_t csv_field; 154 csv_field_t csv_field;
156 jmp_buf env;
157} csv_t; 155} csv_t;
158 156
159int csv_init(csv_t *csv, FILE *in); 157int csv_init(csv_t *csv);
160int csv_init_opt(csv_t *csv, FILE *in, csv_options_t *csv_options); 158int csv_init_opt(csv_t *csv, csv_options_t *csv_options);
161void csv_free(csv_t *csv); 159void csv_free(csv_t *csv);
162int csv_nfields(csv_t *csv); 160int csv_nfields(csv_t *csv);
163const char * csv_field(csv_t *csv, int idx); 161const char * csv_field(csv_t *csv, int idx);
164int csv_read(csv_t *csv); 162int csv_read(csv_t *csv, FILE *in);
165void csv_write(csv_t *csv, int n, const char *fields[], FILE *out); 163void csv_write(csv_t *csv, int n, const char *fields[], FILE *out);
166 164
167int csv_try(csv_t *csv, FILE *in);
168void csv_throw(csv_t *csv, int x);
169
170#define CSV_NO_MEMORY_EXCEPTION (1)
171#define CSV_OUT_OF_BOUND_EXCEPTION (2)
172
173
174#if 0
175#define TRY do { jmp_buf ex_buf__; switch( setjmp(ex_buf__) ) { case 0: while(1) {
176#define CATCH(x) break; case x:
177#define FINALLY break; } default: {
178#define ETRY break; } } }while(0)
179#define THROW(x) longjmp(ex_buf__, x)
180#endif
181
182#define CSV_TRY(csv, in) do{ switch( csv_try(csv, in) ){ case 0: while(1) {
183#define CSV_CATCH(x) break; case x:
184#define CSV_FINALLY break; } default: {
185#define CSV_ETRY break; } } } while(0)
186#define CSV_THROW(csv, x) csv_throw(csv, x)
187
188int
189csv_try(csv_t *csv, FILE *in)
190{
191 int err = csv_init(csv, in);
192 if ( err ) {
193 return err;
194 }
195 err = setjmp(csv->env);
196 return err;
197}
198
199void
200csv_throw(csv_t *csv, int x)
201{
202 longjmp(csv->env, x);
203 printf("d\n");
204}
205
206int 165int
207csv_init(csv_t *csv, FILE *in) 166csv_init(csv_t *csv)
208{ 167{
209 assert(csv != NULL); 168 assert(csv != NULL);
210 169
211 return csv_init_opt(csv, in, &default_csv_options); 170 return csv_init_opt(csv, &default_csv_options);
212} 171}
213 172
214int 173int
215csv_init_opt(csv_t *csv, FILE *in, csv_options_t *csv_options) 174csv_init_opt(csv_t *csv, csv_options_t *csv_options)
216{ 175{
217 assert(csv != NULL); 176 assert(csv != NULL);
218 assert(in != NULL);
219 177
220 csv->in = in;
221 csv->csv_options = ( csv_options != NULL ) ? csv_options : &default_csv_options; 178 csv->csv_options = ( csv_options != NULL ) ? csv_options : &default_csv_options;
222 179
223 if ( !csv_string_init(&csv->csv_string) ) { 180 if ( !csv_string_init(&csv->csv_string) ) {
@@ -255,20 +212,19 @@ const char *
255csv_field(csv_t *csv, int idx) 212csv_field(csv_t *csv, int idx)
256{ 213{
257 assert(csv != NULL); 214 assert(csv != NULL);
258 //assert(idx >= 0 && idx < csv->csv_field.pos); 215 assert(idx >= 0 && idx < csv->csv_field.pos);
259 216
260 if ( idx < 0 || idx >= csv->csv_field.pos) { 217 if ( idx >= 0 && idx < csv->csv_field.pos) {
261 printf("idx: %d\n", idx); 218 return &csv->csv_string.str[csv->csv_field.fields[idx]];
262 CSV_THROW(csv, CSV_OUT_OF_BOUND_EXCEPTION);
263 return NULL;
264 } 219 }
265 else { 220 else {
266 return &csv->csv_string.str[csv->csv_field.fields[idx]]; 221 /* TODO: error handling */
222 return NULL;
267 } 223 }
268} 224}
269 225
270int 226int
271csv_read(csv_t *csv) 227csv_read(csv_t *csv, FILE *in)
272{ 228{
273 assert(csv != NULL); 229 assert(csv != NULL);
274 230
@@ -294,14 +250,14 @@ csv_read(csv_t *csv)
294 case STATE_START_FIELD: 250 case STATE_START_FIELD:
295 csv_field_append(&csv->csv_field, csv->csv_string.pos); 251 csv_field_append(&csv->csv_field, csv->csv_string.pos);
296 252
297 ch = getc(csv->in); 253 ch = getc(in);
298 if ( ch == EOF ) { 254 if ( ch == EOF ) {
299 state = STATE_END_FILE; 255 state = STATE_END_FILE;
300 } 256 }
301 else if ( ch == '\r' ) { /* Teste auf CR.. */ 257 else if ( ch == '\r' ) { /* Teste auf CR.. */
302 ch = getc(csv->in); 258 ch = getc(in);
303 if ( ch != '\n' ) { /* ... LF */ 259 if ( ch != '\n' ) { /* ... LF */
304 ungetc(ch, csv->in); 260 ungetc(ch, in);
305 } 261 }
306 state = STATE_END_LINE; 262 state = STATE_END_LINE;
307 } 263 }
@@ -322,12 +278,12 @@ csv_read(csv_t *csv)
322 278
323 case STATE_QUOTED_FIELD: 279 case STATE_QUOTED_FIELD:
324 do { 280 do {
325 ch = getc(csv->in); 281 ch = getc(in);
326 if ( ch == EOF ) { 282 if ( ch == EOF ) {
327 state = STATE_END_FILE; 283 state = STATE_END_FILE;
328 } 284 }
329 else if ( ch == QUOTE ) { 285 else if ( ch == QUOTE ) {
330 ch = getc(csv->in); 286 ch = getc(in);
331 if ( ch == EOF ) { 287 if ( ch == EOF ) {
332 state = STATE_END_FILE; 288 state = STATE_END_FILE;
333 } 289 }
@@ -338,9 +294,9 @@ csv_read(csv_t *csv)
338 state = STATE_END_FIELD; 294 state = STATE_END_FIELD;
339 } 295 }
340 else if ( ch == '\r' ) { 296 else if ( ch == '\r' ) {
341 ch = getc(csv->in); 297 ch = getc(in);
342 if ( ch != '\n' ) { 298 if ( ch != '\n' ) {
343 ungetc(ch, csv->in); 299 ungetc(ch, in);
344 } 300 }
345 state = STATE_END_LINE; 301 state = STATE_END_LINE;
346 } 302 }
@@ -349,7 +305,7 @@ csv_read(csv_t *csv)
349 } 305 }
350 else { 306 else {
351 csv_string_append(&csv->csv_string, QUOTE); 307 csv_string_append(&csv->csv_string, QUOTE);
352 ungetc(ch, csv->in); /* zuviel gelesenes Zeichen zurückstellen */ 308 ungetc(ch, in); /* zuviel gelesenes Zeichen zurückstellen */
353 } 309 }
354 } 310 }
355 else { 311 else {
@@ -360,7 +316,7 @@ csv_read(csv_t *csv)
360 316
361 case STATE_SIMPLE_FIELD: 317 case STATE_SIMPLE_FIELD:
362 do { 318 do {
363 ch = getc(csv->in); 319 ch = getc(in);
364 if ( ch == EOF ) { 320 if ( ch == EOF ) {
365 state = STATE_END_FILE; 321 state = STATE_END_FILE;
366 } 322 }
@@ -368,9 +324,9 @@ csv_read(csv_t *csv)
368 state = STATE_END_FIELD; 324 state = STATE_END_FIELD;
369 } 325 }
370 else if ( ch == '\r' ) { 326 else if ( ch == '\r' ) {
371 ch = getc(csv->in); 327 ch = getc(in);
372 if ( ch != '\n' ) { 328 if ( ch != '\n' ) {
373 ungetc(ch, csv->in); 329 ungetc(ch, in);
374 } 330 }
375 state = STATE_END_LINE; 331 state = STATE_END_LINE;
376 } 332 }
@@ -464,33 +420,14 @@ main(void)
464 420
465 clock_t start = clock(); 421 clock_t start = clock();
466 422
467 CSV_TRY(csv, stdin) 423 int err = csv_init(csv);
468 { 424 if ( !err ) {
469 while ( (n = csv_read(csv)) != -1 ) { 425 while ( (n = csv_read(csv, stdin)) != -1 ) {
470 csv_field(csv, -1); 426 //csv_field(csv, -1);
471 ++line; 427 ++line;
472 } 428 }
473 }
474 CSV_CATCH( CSV_NO_MEMORY_EXCEPTION )
475 {
476 fprintf(stderr, "out of memory!");
477 csv_free(csv);
478 return EXIT_FAILURE;
479 }
480 CSV_CATCH( CSV_OUT_OF_BOUND_EXCEPTION )
481 {
482 fprintf(stderr, "out of bound!!\n");
483 csv_free(csv); 429 csv_free(csv);
484 return EXIT_FAILURE;
485 } 430 }
486 CSV_FINALLY
487 {
488 printf("finally...\n");
489 csv_free(csv);
490 }
491 CSV_ETRY;
492
493 // http://groups.di.unipi.it/~nids/docs/longjump_try_trow_catch.html
494 431
495 clock_t end = clock(); 432 clock_t end = clock();
496 433