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 void csv_write(void)
133 unsigned char *line = line_nth(&in_line, 0);
134 int n = fields_count(&out_fields);
135 for (int i=0; i<n; i++) {
136 struct field *f = fields_nth(&out_fields, i);
138 if (out_format->quote >= 0) {
139 for (int j=0; j < f->len; j++) {
140 int c = line[f->start_pos + j];
141 if (c == out_format->fs || c == out_format->quote) {
148 putchar(out_format->fs);
150 putchar(out_format->quote);
151 for (int j=0; j < f->len; j++) {
152 int c = line[f->start_pos + j];
153 if (c == out_format->quote)
158 putchar(out_format->quote);
163 static int ws_read(void)
169 return !!fields_count(&in_fields);
174 if (c == ' ' || c == '\t' || c == '\f') {
181 *line_push(&in_line) = c;
188 /*** Field selection ***/
191 int first_field, last_field;
194 DECLARE_BUF(selectors, struct selector);
195 static selectors_t selectors;
197 static char *parse_selector(char *str)
199 char buf[strlen(str) + 1];
202 struct selector *s = selectors_push(&selectors);
203 char *sep = strchr(buf, '-');
206 s->first_field = atoi(buf);
207 s->last_field = atoi(sep);
209 s->first_field = s->last_field = atoi(buf);
214 static void finish_parse_selectors(void)
216 if (!selectors_count(&selectors))
220 static void select_fields(void)
222 for (int i = 0; i < selectors_count(&selectors); i++) {
223 struct selector *s = selectors_nth(&selectors, i);
224 int first = s->first_field;
227 int last = s->last_field;
229 last = fields_count(&in_fields);
230 for (int j = first; j <= last; j++) {
231 struct field *f = fields_push(&out_fields);
232 if (j >= 1 && j <= fields_count(&in_fields))
233 *f = *fields_nth(&in_fields, j-1);
235 f->start_pos = f->len = 0;
240 /*** Parsing of arguments ***/
242 static void usage(void)
245 Usage: xsv <in-format> [<out-format>] <options> [<fields>]\n\
248 -t, --tsv TAB-separated values (default)\n\
249 -c, --csv Comma-separated values\n\
250 -w, --ws Values separated by arbitrary whitespace\n\
252 Format parameters:\n\
253 -d, --fs=<char> Delimiter of fields\n\
261 static void bad_args(char *msg)
264 fprintf(stderr, "xsv: %s\n", msg);
265 fprintf(stderr, "Try `xsv --help' for more information.\n");
269 static const char short_options[] = "cd:tw";
275 static const struct option long_options[] = {
276 { "csv", 0, NULL, 'c' },
277 { "fs", 1, NULL, 'd' },
278 { "tsv", 0, NULL, 't' },
279 { "ws", 0, NULL, 'w' },
280 { "help", 0, NULL, OPT_HELP },
281 { NULL, 0, NULL, 0 },
284 static void set_format(int format_id)
286 struct format *f = xmalloc(sizeof(*f));
287 memset(f, 0, sizeof(*f));
294 f->read_line = csv_read;
295 f->write_line = csv_write;
300 f->read_line = csv_read;
301 f->write_line = csv_write;
306 f->read_line = ws_read;
307 f->write_line = csv_write;
313 else if (!out_format)
316 bad_args("At most two format may be given.");
319 static struct format *current_format(void)
325 set_format(FORM_TSV);
329 int main(int argc, char **argv)
333 while ((opt = getopt_long(argc, argv, short_options, long_options, NULL)) >= 0)
336 set_format(FORM_CSV);
340 current_format()->fs = optarg[0];
342 bad_args("No field delimiter given.");
345 set_format(FORM_TSV);
358 out_format = in_format;
360 for (int i = optind; i < argc; i++) {
361 char *err = parse_selector(argv[i]);
365 finish_parse_selectors();
367 fields_init(&in_fields);
368 fields_init(&out_fields);
372 fields_reset(&in_fields);
373 line_reset(&in_line);
375 if (!in_format->read_line())
378 fields_reset(&out_fields);
381 out_format->write_line();