aboutsummaryrefslogtreecommitdiff
path: root/csv-tutorial.c
diff options
context:
space:
mode:
Diffstat (limited to 'csv-tutorial.c')
-rw-r--r--csv-tutorial.c36
1 files changed, 12 insertions, 24 deletions
diff --git a/csv-tutorial.c b/csv-tutorial.c
index 17b95c8..9bdb7e6 100644
--- a/csv-tutorial.c
+++ b/csv-tutorial.c
@@ -8,9 +8,9 @@
8bool 8bool
9processFile(const char *filename) 9processFile(const char *filename)
10{ 10{
11 FILE *in; 11 FILE *in = fopen(filename, "r");
12 12
13 if ( (in = fopen(filename, "r")) == NULL ) { 13 if ( in == NULL ) {
14 return false; 14 return false;
15 } 15 }
16 16
@@ -18,22 +18,21 @@ processFile(const char *filename)
18 csv_init(&csv); 18 csv_init(&csv);
19 19
20 while ( csv_read(&csv, in) ) { 20 while ( csv_read(&csv, in) ) {
21 const size_t nf = csv_nfields(&csv); 21 const size_t nfields = csv_nfields(&csv);
22 (void) nf;
23 // ... 22 // ...
24 } 23 }
25 csv_cleanup(&csv); 24 csv_cleanup(&csv);
26 25
27 fclose(in); 26 (void) fclose(in);
28 return true; 27 return true;
29} 28}
30 29
31bool 30bool
32processFile_options(const char *filename) 31processFile_options(const char *filename)
33{ 32{
34 FILE *in; 33 FILE *in = fopen(filename, "r");
35 34
36 if ( (in = fopen(filename, "r")) == NULL ) { 35 if ( in == NULL ) {
37 return false; 36 return false;
38 } 37 }
39 38
@@ -45,13 +44,12 @@ processFile_options(const char *filename)
45 csv_init_opt(&csv, &csv_options); 44 csv_init_opt(&csv, &csv_options);
46 45
47 while ( csv_read(&csv, in) ) { 46 while ( csv_read(&csv, in) ) {
48 const size_t nf = csv_nfields(&csv); 47 const size_t nfields = csv_nfields(&csv);
49 (void) nf;
50 // ... 48 // ...
51 } 49 }
52 csv_cleanup(&csv); 50 csv_cleanup(&csv);
53 51
54 fclose(in); 52 (void) fclose(in);
55 return true; 53 return true;
56} 54}
57 55
@@ -64,9 +62,9 @@ csvErrorHandler(csv_err_t err, void *cb_arg)
64bool 62bool
65processFile_error(const char *filename) 63processFile_error(const char *filename)
66{ 64{
67 FILE *in; 65 FILE *in = fopen(filename, "r");
68 66
69 if ( (in = fopen(filename, "r")) == NULL ) { 67 if ( in == NULL ) {
70 return false; 68 return false;
71 } 69 }
72 70
@@ -84,30 +82,20 @@ processFile_error(const char *filename)
84 switch ( setjmp(env) ) { 82 switch ( setjmp(env) ) {
85 case CSV_ERR_OK: 83 case CSV_ERR_OK:
86 while ( csv_read(&csv, in) ) { 84 while ( csv_read(&csv, in) ) {
87 const size_t nf = csv_nfields(&csv); 85 const size_t nfields = csv_nfields(&csv);
88 (void) nf;
89 // ... 86 // ...
90 } 87 }
91 break; 88 break;
92 89
93 case CSV_ERR_OUT_OF_MEMORY: 90 case CSV_ERR_OUT_OF_MEMORY:
94 csv_cleanup(&csv);
95 break;
96
97 case CSV_ERR_OUT_OF_RANGE: 91 case CSV_ERR_OUT_OF_RANGE:
98 csv_cleanup(&csv);
99 break;
100
101 case CSV_ERR_IO_READ: 92 case CSV_ERR_IO_READ:
102 csv_cleanup(&csv);
103 break;
104
105 case CSV_ERR_IO_WRITE: 93 case CSV_ERR_IO_WRITE:
106 csv_cleanup(&csv); 94 csv_cleanup(&csv);
107 break; 95 break;
108 } 96 }
109 97
110 fclose(in); 98 (void) fclose(in);
111 return true; 99 return true;
112} 100}
113 101