]> mj.ucw.cz Git - xsv.git/blob - xsv.c
Added --ws mode
[xsv.git] / xsv.c
1 /*
2  *      A Swiss-Army Knife for CSV-like Files
3  *
4  *      (c) 2012 Martin Mares <mj@ucw.cz>
5  */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <getopt.h>
11
12 /*** Memory allocation ***/
13
14 static void *xmalloc(size_t bytes)
15 {
16         void *p = malloc(bytes);
17         if (!p) {
18                 fprintf(stderr, "xsv: Out of memory (cannot allocate %zu bytes)\n", bytes);
19                 exit(1);
20         }
21         return p;
22 }
23
24 static void *xrealloc(void *old, size_t bytes)
25 {
26         void *p = realloc(old, bytes);
27         if (!p) {
28                 fprintf(stderr, "xsv: Out of memory (cannot allocate %zu bytes)\n", bytes);
29                 exit(1);
30         }
31         return p;
32 }
33
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));                           \
42         }                                                                                       \
43         static inline type *name##_push(name##_t *b) {                                          \
44                 if (b->count >= b->max) name##_extend(b);                                       \
45                 return &b->start[b->count++];                                                   \
46         }                                                                                       \
47         static inline type *name##_nth(name##_t *b, int n) { return &b->start[n]; }             \
48         // end
49
50 /*** Formats and their parameters ***/
51
52 enum format_id {
53         FORM_UNSPEC,
54         FORM_TSV,
55         FORM_CSV,
56         FORM_WS,
57 };
58
59 struct format {
60         enum format_id id;
61         int fs;
62         int quote;
63         int (*read_line)(void);
64         void (*write_line)(void);
65 };
66
67 static struct format *in_format, *out_format;
68
69 struct field {
70         int start_pos;
71         int len;
72 };
73
74 DECLARE_BUF(fields, struct field);
75 DECLARE_BUF(line, unsigned char);
76
77 static fields_t in_fields, out_fields;
78 static struct field *in_field;
79 static line_t in_line;
80
81 static void new_field(void)
82 {
83         in_field = fields_push(&in_fields);
84         in_field->start_pos = line_count(&in_line);
85         in_field->len = 0;
86 }
87
88 static void ensure_field(void)
89 {
90         if (!in_field)
91                 new_field();
92 }
93
94 static int csv_read(void)
95 {
96         int quoted = 0;
97         // FIXME: Complain if closing quote is missing?
98         for (;;) {
99                 int c = getchar();
100 restart:
101                 if (c < 0)
102                         return !!fields_count(&in_fields);
103                 if (c == '\r')
104                         continue;
105                 if (c == '\n')
106                         return 1;
107                 if (quoted) {
108                         if (c == in_format->quote) {
109                                 c = getchar();
110                                 if (c != in_format->quote) {
111                                         quoted = 0;
112                                         goto restart;
113                                 }
114                                 // Two quotes assimilate to one
115                         }
116                         // Fall through to pushing the character
117                 } else if (c == in_format->quote) {
118                         quoted = 1;
119                         continue;
120                 } else if (c == in_format->fs && !quoted) {
121                         ensure_field();
122                         new_field();
123                         continue;
124                 }
125                 ensure_field();
126                 *line_push(&in_line) = c;
127                 in_field->len++;
128         }
129 }
130
131 static void csv_write(void)
132 {
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);
137                 int need_quotes = 0;
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) {
142                                         need_quotes = 1;
143                                         break;
144                                 }
145                         }
146                 }
147                 if (i)
148                         putchar(out_format->fs);
149                 if (need_quotes)
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)
154                                 putchar(c);
155                         putchar(c);
156                 }
157                 if (need_quotes)
158                         putchar(out_format->quote);
159         }
160         putchar('\n');
161 }
162
163 static int ws_read(void)
164 {
165         int ws = 0;
166         for (;;) {
167                 int c = getchar();
168                 if (c < 0)
169                         return !!fields_count(&in_fields);
170                 if (c == '\r')
171                         continue;
172                 if (c == '\n')
173                         return 1;
174                 if (c == ' ' || c == '\t' || c == '\f') {
175                         ensure_field();
176                         if (!ws)
177                                 new_field();
178                         ws++;
179                 } else {
180                         ensure_field();
181                         *line_push(&in_line) = c;
182                         in_field->len++;
183                         ws = 0;
184                 }
185         }
186 }
187
188 /*** Field selection ***/
189
190 struct selector {
191         int first_field, last_field;
192 };
193
194 DECLARE_BUF(selectors, struct selector);
195 static selectors_t selectors;
196
197 static char *parse_selector(char *str)
198 {
199         char buf[strlen(str) + 1];
200         strcpy(buf, str);
201
202         struct selector *s = selectors_push(&selectors);
203         char *sep = strchr(buf, '-');
204         if (sep) {
205                 *sep++ = 0;
206                 s->first_field = atoi(buf);
207                 s->last_field = atoi(sep);
208         } else
209                 s->first_field = s->last_field = atoi(buf);
210
211         return NULL;
212 }
213
214 static void finish_parse_selectors(void)
215 {
216         if (!selectors_count(&selectors))
217                 parse_selector("-");
218 }
219
220 static void select_fields(void)
221 {
222         for (int i = 0; i < selectors_count(&selectors); i++) {
223                 struct selector *s = selectors_nth(&selectors, i);
224                 int first = s->first_field;
225                 if (first <= 0)
226                         first = 1;
227                 int last = s->last_field;
228                 if (last <= 0)
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);
234                         else
235                                 f->start_pos = f->len = 0;
236                 }
237         }
238 }
239
240 /*** Parsing of arguments ***/
241
242 static void usage(void)
243 {
244         printf("\
245 Usage: xsv <in-format> [<out-format>] <options> [<fields>]\n\
246 \n\
247 Formats:\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\
251 \n\
252 Format parameters:\n\
253 -d, --fs=<char> Delimiter of fields\n\
254 \n\
255 Other options:\n\
256 (so far none)\n\
257 ");
258         exit(0);
259 }
260
261 static void bad_args(char *msg)
262 {
263         if (msg)
264                 fprintf(stderr, "xsv: %s\n", msg);
265         fprintf(stderr, "Try `xsv --help' for more information.\n");
266         exit(1);
267 }
268
269 static const char short_options[] = "cd:tw";
270
271 enum long_options {
272         OPT_HELP = 256,
273 };
274
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 },
282 };
283
284 static void set_format(int format_id)
285 {
286         struct format *f = xmalloc(sizeof(*f));
287         memset(f, 0, sizeof(*f));
288         f->id = format_id;
289
290         switch (format_id) {
291                 case FORM_TSV:
292                         f->fs = '\t';
293                         f->quote = -1;
294                         f->read_line = csv_read;
295                         f->write_line = csv_write;
296                         break;
297                 case FORM_CSV:
298                         f->fs = ',';
299                         f->quote = '"';
300                         f->read_line = csv_read;
301                         f->write_line = csv_write;
302                         break;
303                 case FORM_WS:
304                         f->fs = ' ';
305                         f->quote = -1;
306                         f->read_line = ws_read;
307                         f->write_line = csv_write;
308                         break;
309         }
310
311         if (!in_format)
312                 in_format = f;
313         else if (!out_format)
314                 out_format = f;
315         else
316                 bad_args("At most two format may be given.");
317 }
318
319 static struct format *current_format(void)
320 {
321         if (out_format)
322                 return out_format;
323         if (in_format)
324                 return in_format;
325         set_format(FORM_TSV);
326         return in_format;
327 }
328
329 int main(int argc, char **argv)
330 {
331         int opt;
332
333         while ((opt = getopt_long(argc, argv, short_options, long_options, NULL)) >= 0)
334                 switch (opt) {
335                         case 'c':
336                                 set_format(FORM_CSV);
337                                 break;
338                         case 'd':
339                                 if (optarg[0])
340                                         current_format()->fs = optarg[0];
341                                 else
342                                         bad_args("No field delimiter given.");
343                                 break;
344                         case 't':
345                                 set_format(FORM_TSV);
346                                 break;
347                         case 'w':
348                                 set_format(FORM_WS);
349                                 break;
350                         case OPT_HELP:
351                                 usage();
352                         default:
353                                 bad_args(NULL);
354                 }
355
356         current_format();
357         if (!out_format)
358                 out_format = in_format;
359
360         for (int i = optind; i < argc; i++) {
361                 char *err = parse_selector(argv[i]);
362                 if (err)
363                         bad_args(err);
364         }
365         finish_parse_selectors();
366
367         fields_init(&in_fields);
368         fields_init(&out_fields);
369         line_init(&in_line);
370
371         for (;;) {
372                 fields_reset(&in_fields);
373                 line_reset(&in_line);
374                 in_field = NULL;
375                 if (!in_format->read_line())
376                         break;
377
378                 fields_reset(&out_fields);
379                 select_fields();
380
381                 out_format->write_line();
382         }
383
384         return 0;
385 }