-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathlibcsv_demo.c
More file actions
181 lines (156 loc) · 4.24 KB
/
Copy pathlibcsv_demo.c
File metadata and controls
181 lines (156 loc) · 4.24 KB
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
#include <rtthread.h>
#include <rtdevice.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <limits.h>
/* libcsv */
#include "libcsv.h"
/* optparse */
#include "optparse.h"
#define PROGRAM_NAME "csvcheck"
#define AUTHORS "Robert Gamble"
static struct optparse_long longopts[] =
{
{"delimiter", 'd', OPTPARSE_REQUIRED},
{"quote", 'q', OPTPARSE_REQUIRED},
{"help", CHAR_MAX + 1, OPTPARSE_NONE},
{NULL, 0, OPTPARSE_NONE}
};
/* The name this program was called with */
char *program_name;
/* The delimiter character */
char delimiter = CSV_COMMA;
/* The delimiter argument */
char *delimiter_name;
/* The quote character */
char quote = CSV_QUOTE;
/* The quote argument */
char *quote_name;
void usage(int status)
{
if (status != RT_EOK)
fprintf (stderr, "Try `%s --help for more information.\n", program_name);
else {
printf("\
Usage: %s [OPTION]... [FILE]...\n\
Determine if file(s) are properly formed CSV files and display the position\n\
of the first offending byte if not.\n\
\n\
", program_name);
printf("\
-d, --delimiter=DELIM use DELIM as the delimiter instead of comma\n\
-q, --quote=QUOTE_CHAR use QUOTE_CHAR as the quote character instead of\n\
double quote\n\
--help display this help and exit\n\
");
}
}
void check_file(char *filename)
{
size_t pos = 0;
char buf[1024];
struct csv_parser p;
FILE *fp;
size_t bytes_read;
size_t retval;
if (csv_init(&p, CSV_STRICT | CSV_STRICT_FINI) != 0)
{
fprintf(stderr, "Failed to initialize csv parser\n");
return;
}
csv_set_delim(&p, delimiter);
csv_set_quote(&p, quote);
if (filename == NULL || !strcmp(filename, "-"))
{
fp = stdin;
}
else
{
fp = fopen(filename, "rb");
if (fp == NULL)
{
fprintf(stderr, "Failed to open file %s: %s\n", filename, strerror(errno));
csv_free(&p);
return;
}
}
while ((bytes_read = fread(buf, 1, 1024, fp)) > 0)
{
if ((retval = csv_parse(&p, buf, bytes_read, NULL, NULL, NULL)) != bytes_read)
{
if (csv_error(&p) == CSV_EPARSE)
{
printf("%s: malformed at byte %lu\n", filename ? filename : "stdin", (unsigned long)pos + retval + 1);
goto end;
}
else
{
printf("Error while processing %s: %s\n", filename ? filename : "stdin", csv_strerror(csv_error(&p)));
goto end;
}
}
pos += 1024;
}
if (csv_fini(&p, NULL, NULL, NULL) != 0)
printf("%s: missing closing quote at end of input\n", filename ? filename : "stdin");
else
printf("%s well-formed\n", filename ? filename : "data is");
end:
fclose(fp);
}
int csvcheck(int argc, char *argv[])
{
int optc;
int option_index;
program_name = argv[0];
struct optparse options;
if(argc == 1)
{
usage(RT_EOK);
return RT_EOK;
}
optparse_init(&options, argv);
while ((optc = optparse_long(&options, longopts, &option_index)) != -1)
{
switch (optc)
{
case 'd':
delimiter_name = options.optarg;
if (strlen(delimiter_name) > 1)
printf("delimiter must be exactly one byte long\n");
else
delimiter = delimiter_name[0];
break;
case 'q':
quote_name = options.optarg;
if (strlen(quote_name) > 1)
printf("delimiter must be exactly one byte long\n");
else
quote = quote_name[0];
break;
case CHAR_MAX + 1:
/* --help */
usage(RT_EOK);
return RT_EOK;
default:
usage(RT_ERROR);
return RT_ERROR;
}
}
if (options.optind < argc)
{
while (options.optind < argc)
{
check_file(argv[options.optind++]);
}
}
else
{
/* Process from stdin */
check_file(NULL);
}
return RT_EOK;
}
MSH_CMD_EXPORT(csvcheck, The command to check CSV files format based on the libcsv.);