summaryrefslogtreecommitdiff
path: root/csv-test.c
diff options
context:
space:
mode:
Diffstat (limited to 'csv-test.c')
-rw-r--r--csv-test.c139
1 files changed, 0 insertions, 139 deletions
diff --git a/csv-test.c b/csv-test.c
deleted file mode 100644
index efd2155..0000000
--- a/csv-test.c
+++ /dev/null
@@ -1,139 +0,0 @@
1#include <stdio.h>
2#include <stdlib.h>
3#include <setjmp.h>
4#include <signal.h>
5#include <time.h>
6#include <assert.h>
7
8#include "csv.h"
9
10static int
11test(FILE *f)
12{
13 int line = 0;
14 csv_t csv = { 0 };
15
16 while ( csv_read(&csv, f) != -1 ) {
17 ++line;
18 }
19
20 return line;
21}
22
23static int
24test_exception(FILE *f)
25{
26 int line = 0;
27 csv_t csv[1];
28
29 csv_init(csv);
30 for ( int n; (n = csv_read(csv, f)) != -1; ) {
31 if (++line == 500000) csv_field(csv, -1);
32 }
33
34 return line;
35}
36
37static void
38my_signal_handler(int sig)
39{
40 if ( sig == SIGABRT ) {
41 fprintf(stderr, "my_signal_handler!\n");
42 exit(1);
43 }
44}
45
46static int
47test_exception_with_signal(FILE *f)
48{
49 void (*old_handler)(int) = signal(SIGABRT, my_signal_handler);
50
51 int line = 0;
52 csv_t csv[1];
53
54 csv_init(csv);
55 for ( int n; (n = csv_read(csv, f)) != -1; ) {
56 if (++line == 500000) csv_field(csv, -1);
57 }
58
59 signal(SIGABRT, old_handler);
60
61 return line;
62}
63
64static void
65my_error_handler(csv_err_t csv_err, void *env)
66{
67 assert(env != NULL);
68
69 fprintf(stderr, "my_error_handler: %s\n", csv_err_str(csv_err));
70 longjmp(*((jmp_buf *) env), 1);
71}
72
73static int
74test_exception_longjmp(FILE *f)
75{
76 /* start with default options */
77 csv_options_t csv_options = csv_default_options;
78
79 /* setup error handler */
80 jmp_buf env;
81 csv_options.cb_error = my_error_handler;
82 csv_options.cb_error_arg = &env;
83
84 /* setup csv-Object with custom options */
85 csv_t csv[1];
86 csv_init_opt(csv, &csv_options);
87
88 if ( setjmp(env) == 0 ) {
89 int line = 0;
90
91 for ( int n; (n = csv_read(csv, f)) != -1; ) {
92 // process data...
93 if (++line == 500000) csv_field(csv, -1);
94 }
95 return line;
96 }
97 else {
98 csv_cleanup(csv);
99 return -1;
100 }
101}
102
103int
104main(void)
105{
106#if 0
107 const char *names[] = { "Thomas \"DG\" Schmucker", "Andrea", "Markus", "Marion", NULL };
108
109 csv_write(stdout, -1, names, NULL);
110 csv_write(stdout, 0, names, NULL);
111 csv_write(stdout, 1, names, NULL);
112 csv_write(stdout, 2, names, NULL);
113 csv_write(stdout, 3, names, NULL);
114 csv_write(stdout, 4, names, NULL);
115 csv_write(stdout, 5, names, NULL);
116 return 0;
117#endif
118
119 FILE *in = fopen("500000 Records.csv", "r");
120 if ( in == NULL ) {
121 fprintf(stderr, "failed to open testfile...\n");
122 return EXIT_FAILURE;
123 }
124
125 clock_t start = clock();
126
127 int lines = test(in);
128 (void) test_exception;
129 (void) test_exception_with_signal;
130 (void) test_exception_longjmp;
131
132 clock_t end = clock();
133
134 fclose(in);
135
136 printf("%d lines processed, duration: %.3lf sec\n", lines, ((double)(end - start)) / CLOCKS_PER_SEC);
137
138 return EXIT_SUCCESS;
139}