2 * A Swiss-Army Knife for CSV-like Files
4 * (c) 2012 Martin Mares <mj@ucw.cz>
12 /*** Memory allocation ***/
14 static void *xmalloc(size_t bytes)
16 void *p = malloc(bytes);
18 fprintf(stderr, "xsv: Out of memory (cannot allocate %zu bytes)\n", bytes);
24 static void *xrealloc(void *old, size_t bytes)
26 void *p = realloc(old, bytes);
28 fprintf(stderr, "xsv: Out of memory (cannot allocate %zu bytes)\n", bytes);
34 #define DECLARE_BUF(name, type) \
35 typedef struct { type *start; int count; int max; } name##_t; \
36 static inline void name##_init(name##_t *b) { b->start = NULL; b->count = b->max = 0; } \
37 static inline void name##_reset(name##_t *b) { b->count = 0; } \
38 static inline int name##_count(name##_t *b) { return b->count; } \
39 static void name##_extend(name##_t *b) { \
40 b->max = b->max ? 2*b->max : 16; \
41 b->start = xrealloc(b->start, b->max * sizeof(type)); \
43 static inline type *name##_push(name##_t *b) { \
44 if (b->count >= b->max) name##_extend(b); \
45 return &b->start[b->count++]; \
47 static inline type *name##_nth(name##_t *b, int n) { return &b->start[n]; } \
50 /*** Formats and their parameters ***/
63 int (*read_line)(void);
64 void (*write_line)(void);
67 static struct format *in_format, *out_format;
74 DECLARE_BUF(fields, struct field);
75 DECLARE_BUF(line, unsigned char);
77 static fields_t in_fields, out_fields;
78 static struct field *in_field;
79 static line_t in_line;
81 static void new_field(void)
83 in_field = fields_push(&in_fields);
84 in_field->start_pos = line_count(&in_line);
88 static void ensure_field(void)
94 static int csv_read(void)
97 // FIXME: Complain if closing quote is missing?
102 return !!fields_count(&in_fields);
108 if (c == in_format->quote) {
110 if (c != in_format->quote) {
114 // Two quotes assimilate to one
116 // Fall through to pushing the character
117 } else if (c == in_format->quote) {
120 } else if (c == in_format->fs && !quoted) {
126 *line_push(&in_line) = c;
131 static int is_ws(int c)
133 return (c == ' ' || c == '\t' || c == '\f');
136 static void csv_write(void)
138 unsigned char *line = line_nth(&in_line, 0);
139 int n = fields_count(&out_fields);
140 for (int i=0; i<n; i++) {
141 struct field *f = fields_nth(&out_fields, i);
143 if (out_format->quote >= 0) {
144 for (int j=0; j < f->len; j++) {
145 int c = line[f->start_pos + j];
146 if (c == out_format->fs || c == out_format->quote) {
153 putchar(out_format->fs);
155 putchar(out_format->quote);
156 for (int j=0; j < f->len; j++) {
157 int c = line[f->start_pos + j];
158 if (c == out_format->quote)
163 putchar(out_format->quote);
168 static int ws_read(void)
174 return !!fields_count(&in_fields);
186 *line_push(&in_line) = c;
195 static void trim_fields(void)
197 unsigned char *line = line_nth(&in_line, 0);
198 for (int i = 0; i < fields_count(&in_fields); i++) {
199 struct field *f = fields_nth(&in_fields, i);
200 while (f->len && is_ws(line[f->start_pos]))
201 f->start_pos++, f->len--;
202 while (f->len && is_ws(line[f->start_pos + f->len - 1]))
207 /*** Field selection ***/
210 int first_field, last_field;
213 DECLARE_BUF(selectors, struct selector);
214 static selectors_t selectors;
216 static char *parse_selector(char *str)
218 char buf[strlen(str) + 1];
221 struct selector *s = selectors_push(&selectors);
222 char *sep = strchr(buf, '-');
225 s->first_field = atoi(buf);
226 s->last_field = atoi(sep);
228 s->first_field = s->last_field = atoi(buf);
233 static void finish_parse_selectors(void)
235 if (!selectors_count(&selectors))
239 static void select_fields(void)
241 for (int i = 0; i < selectors_count(&selectors); i++) {
242 struct selector *s = selectors_nth(&selectors, i);
243 int first = s->first_field;
246 int last = s->last_field;
248 last = fields_count(&in_fields);
249 for (int j = first; j <= last; j++) {
250 struct field *f = fields_push(&out_fields);
251 if (j >= 1 && j <= fields_count(&in_fields))
252 *f = *fields_nth(&in_fields, j-1);
254 f->start_pos = f->len = 0;
259 /*** Parsing of arguments ***/
261 static void usage(void)
264 Usage: xsv <in-format> [<out-format>] <options> [<fields>]\n\
267 -t, --tsv TAB-separated values (default)\n\
268 -c, --csv Comma-separated values\n\
269 -w, --ws Values separated by arbitrary whitespace\n\
271 Format parameters:\n\
272 -d, --fs=<char> Delimiter of fields\n\
275 --trim Trim leading and trailing whitespaces in fields\n\
280 static void bad_args(char *msg)
283 fprintf(stderr, "xsv: %s\n", msg);
284 fprintf(stderr, "Try `xsv --help' for more information.\n");
288 static const char short_options[] = "cd:tw";
295 static const struct option long_options[] = {
296 { "csv", 0, NULL, 'c' },
297 { "fs", 1, NULL, 'd' },
298 { "trim", 0, NULL, OPT_TRIM },
299 { "tsv", 0, NULL, 't' },
300 { "ws", 0, NULL, 'w' },
301 { "help", 0, NULL, OPT_HELP },
302 { NULL, 0, NULL, 0 },
305 static void set_format(int format_id)
307 struct format *f = xmalloc(sizeof(*f));
308 memset(f, 0, sizeof(*f));
315 f->read_line = csv_read;
316 f->write_line = csv_write;
321 f->read_line = csv_read;
322 f->write_line = csv_write;
327 f->read_line = ws_read;
328 f->write_line = csv_write;
334 else if (!out_format)
337 bad_args("At most two format may be given.");
340 static struct format *current_format(void)
346 set_format(FORM_TSV);
350 int main(int argc, char **argv)
355 while ((opt = getopt_long(argc, argv, short_options, long_options, NULL)) >= 0)
358 set_format(FORM_CSV);
362 current_format()->fs = optarg[0];
364 bad_args("No field delimiter given.");
367 set_format(FORM_TSV);
383 out_format = in_format;
385 for (int i = optind; i < argc; i++) {
386 char *err = parse_selector(argv[i]);
390 finish_parse_selectors();
392 fields_init(&in_fields);
393 fields_init(&out_fields);
397 fields_reset(&in_fields);
398 line_reset(&in_line);
400 if (!in_format->read_line())
406 fields_reset(&out_fields);
409 out_format->write_line();