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 /*** Field selection ***/
166 int first_field, last_field;
169 DECLARE_BUF(selectors, struct selector);
170 static selectors_t selectors;
172 static char *parse_selector(char *str)
174 char buf[strlen(str) + 1];
177 struct selector *s = selectors_push(&selectors);
178 char *sep = strchr(buf, '-');
181 s->first_field = atoi(buf);
182 s->last_field = atoi(sep);
184 s->first_field = s->last_field = atoi(buf);
189 static void finish_parse_selectors(void)
191 if (!selectors_count(&selectors))
195 static void select_fields(void)
197 for (int i = 0; i < selectors_count(&selectors); i++) {
198 struct selector *s = selectors_nth(&selectors, i);
199 int first = s->first_field;
202 int last = s->last_field;
204 last = fields_count(&in_fields);
205 for (int j = first; j <= last; j++) {
206 struct field *f = fields_push(&out_fields);
207 if (j >= 1 && j <= fields_count(&in_fields))
208 *f = *fields_nth(&in_fields, j-1);
210 f->start_pos = f->len = 0;
215 /*** Parsing of arguments ***/
217 static void usage(void)
220 Usage: xsv <in-format> [<out-format>] <options> [<fields>]\n\
223 -t, --tsv TAB-separated values (default)\n\
224 -c, --csv Comma-separated values\n\
225 -w, --ws Values separated by arbitrary whitespace\n\
227 Format parameters:\n\
228 -d, --fs=<char> Delimiter of fields\n\
236 static void bad_args(char *msg)
239 fprintf(stderr, "xsv: %s\n", msg);
240 fprintf(stderr, "Try `xsv --help' for more information.\n");
244 static const char short_options[] = "cd:tw";
250 static const struct option long_options[] = {
251 { "csv", 0, NULL, 'c' },
252 { "fs", 1, NULL, 'd' },
253 { "tsv", 0, NULL, 't' },
254 { "ws", 0, NULL, 'w' },
255 { "help", 0, NULL, OPT_HELP },
256 { NULL, 0, NULL, 0 },
259 static void set_format(int format_id)
261 struct format *f = xmalloc(sizeof(*f));
262 memset(f, 0, sizeof(*f));
269 f->read_line = csv_read;
270 f->write_line = csv_write;
275 f->read_line = csv_read;
276 f->write_line = csv_write;
284 else if (!out_format)
287 bad_args("At most two format may be given.");
290 static struct format *current_format(void)
296 set_format(FORM_TSV);
300 int main(int argc, char **argv)
304 while ((opt = getopt_long(argc, argv, short_options, long_options, NULL)) >= 0)
307 set_format(FORM_CSV);
311 current_format()->fs = optarg[0];
313 bad_args("No field delimiter given.");
316 set_format(FORM_TSV);
329 out_format = in_format;
331 for (int i = optind; i < argc; i++) {
332 char *err = parse_selector(argv[i]);
336 finish_parse_selectors();
338 fields_init(&in_fields);
339 fields_init(&out_fields);
343 fields_reset(&in_fields);
344 line_reset(&in_line);
346 if (!in_format->read_line())
349 fields_reset(&out_fields);
352 out_format->write_line();