]> sigrok.org Git - libsigrok.git/blob - src/input/csv.c
input/csv: Fix a false negative after successful import
[libsigrok.git] / src / input / csv.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2013 Marc Schink <sigrok-dev@marcschink.de>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <config.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <glib.h>
24 #include <libsigrok/libsigrok.h>
25 #include "libsigrok-internal.h"
26
27 #define LOG_PREFIX "input/csv"
28
29 /*
30  * The CSV input module has the following options:
31  *
32  * single-column: Specifies the column number which stores the sample data for
33  *                single column mode and enables single column mode. Multi
34  *                column mode is used if this parameter is omitted.
35  *
36  * numchannels:   Specifies the number of channels to use. In multi column mode
37  *                the number of channels are the number of columns and in single
38  *                column mode the number of bits (LSB first) beginning at
39  *                'first-channel'.
40  *
41  * delimiter:     Specifies the delimiter for columns. Must be at least one
42  *                character. Comma is used as default delimiter.
43  *
44  * format:        Specifies the format of the sample data in single column mode.
45  *                Available formats are: 'bin', 'hex' and 'oct'. The binary
46  *                format is used by default. This option has no effect in multi
47  *                column mode.
48  *
49  * comment:       Specifies the prefix character(s) for comments. No prefix
50  *                characters are used by default which disables removing of
51  *                comments.
52  *
53  * samplerate:    Samplerate which the sample data was captured with. Default
54  *                value is 0.
55  *
56  * first-channel: Column number of the first channel in multi column mode and
57  *                position of the bit for the first channel in single column mode.
58  *                Default value is 0.
59  *
60  * header:        Determines if the first line should be treated as header
61  *                and used for channel names in multi column mode. Empty header
62  *                names will be replaced by the channel number. If enabled in
63  *                single column mode the first line will be skipped. Usage of
64  *                header is disabled by default.
65  *
66  * startline:     Line number to start processing sample data. Must be greater
67  *                than 0. The default line number to start processing is 1.
68  */
69
70 /* Single column formats. */
71 enum {
72         FORMAT_BIN,
73         FORMAT_HEX,
74         FORMAT_OCT
75 };
76
77 struct context {
78         gboolean started;
79
80         /* Current selected samplerate. */
81         uint64_t samplerate;
82
83         /* Number of channels. */
84         unsigned int num_channels;
85
86         /* Column delimiter character(s). */
87         GString *delimiter;
88
89         /* Comment prefix character(s). */
90         GString *comment;
91
92         /* Termination character(s) used in current stream. */
93         char *termination;
94
95         /* Determines if sample data is stored in multiple columns. */
96         gboolean multi_column_mode;
97
98         /* Column number of the sample data in single column mode. */
99         unsigned int single_column;
100
101         /*
102          * Number of the first column to parse. Equivalent to the number of the
103          * first channel in multi column mode and the single column number in
104          * single column mode.
105          */
106         unsigned int first_column;
107
108         /*
109          * Column number of the first channel in multi column mode and position of
110          * the bit for the first channel in single column mode.
111          */
112         unsigned int first_channel;
113
114         /* Line number to start processing. */
115         size_t start_line;
116
117         /*
118          * Determines if the first line should be treated as header and used for
119          * channel names in multi column mode.
120          */
121         gboolean header;
122
123         /* Format sample data is stored in single column mode. */
124         int format;
125
126         /* Size of the sample buffer. */
127         size_t sample_buffer_size;
128
129         /* Buffer to store sample data. */
130         uint8_t *sample_buffer;
131
132         /* Current line number. */
133         size_t line_number;
134 };
135
136 static void strip_comment(char *buf, const GString *prefix)
137 {
138         char *ptr;
139
140         if (!prefix->len)
141                 return;
142
143         if ((ptr = strstr(buf, prefix->str)))
144                 *ptr = '\0';
145 }
146
147 static int parse_binstr(const char *str, struct context *inc)
148 {
149         gsize i, j, length;
150
151         length = strlen(str);
152
153         if (!length) {
154                 sr_err("Column %u in line %zu is empty.", inc->single_column,
155                         inc->line_number);
156                 return SR_ERR;
157         }
158
159         /* Clear buffer in order to set bits only. */
160         memset(inc->sample_buffer, 0, (inc->num_channels + 7) >> 3);
161
162         i = inc->first_channel;
163
164         for (j = 0; i < length && j < inc->num_channels; i++, j++) {
165                 if (str[length - i - 1] == '1') {
166                         inc->sample_buffer[j / 8] |= (1 << (j % 8));
167                 } else if (str[length - i - 1] != '0') {
168                         sr_err("Invalid value '%s' in column %u in line %zu.",
169                                 str, inc->single_column, inc->line_number);
170                         return SR_ERR;
171                 }
172         }
173
174         return SR_OK;
175 }
176
177 static int parse_hexstr(const char *str, struct context *inc)
178 {
179         gsize i, j, k, length;
180         uint8_t value;
181         char c;
182
183         length = strlen(str);
184
185         if (!length) {
186                 sr_err("Column %u in line %zu is empty.", inc->single_column,
187                         inc->line_number);
188                 return SR_ERR;
189         }
190
191         /* Clear buffer in order to set bits only. */
192         memset(inc->sample_buffer, 0, (inc->num_channels + 7) >> 3);
193
194         /* Calculate the position of the first hexadecimal digit. */
195         i = inc->first_channel / 4;
196
197         for (j = 0; i < length && j < inc->num_channels; i++) {
198                 c = str[length - i - 1];
199
200                 if (!g_ascii_isxdigit(c)) {
201                         sr_err("Invalid value '%s' in column %u in line %zu.",
202                                 str, inc->single_column, inc->line_number);
203                         return SR_ERR;
204                 }
205
206                 value = g_ascii_xdigit_value(c);
207
208                 k = (inc->first_channel + j) % 4;
209
210                 for (; j < inc->num_channels && k < 4; k++) {
211                         if (value & (1 << k))
212                                 inc->sample_buffer[j / 8] |= (1 << (j % 8));
213
214                         j++;
215                 }
216         }
217
218         return SR_OK;
219 }
220
221 static int parse_octstr(const char *str, struct context *inc)
222 {
223         gsize i, j, k, length;
224         uint8_t value;
225         char c;
226
227         length = strlen(str);
228
229         if (!length) {
230                 sr_err("Column %u in line %zu is empty.", inc->single_column,
231                         inc->line_number);
232                 return SR_ERR;
233         }
234
235         /* Clear buffer in order to set bits only. */
236         memset(inc->sample_buffer, 0, (inc->num_channels + 7) >> 3);
237
238         /* Calculate the position of the first octal digit. */
239         i = inc->first_channel / 3;
240
241         for (j = 0; i < length && j < inc->num_channels; i++) {
242                 c = str[length - i - 1];
243
244                 if (c < '0' || c > '7') {
245                         sr_err("Invalid value '%s' in column %u in line %zu.",
246                                 str, inc->single_column, inc->line_number);
247                         return SR_ERR;
248                 }
249
250                 value = g_ascii_xdigit_value(c);
251
252                 k = (inc->first_channel + j) % 3;
253
254                 for (; j < inc->num_channels && k < 3; k++) {
255                         if (value & (1 << k))
256                                 inc->sample_buffer[j / 8] |= (1 << (j % 8));
257
258                         j++;
259                 }
260         }
261
262         return SR_OK;
263 }
264
265 static char **parse_line(char *buf, struct context *inc, int max_columns)
266 {
267         const char *str, *remainder;
268         GSList *list, *l;
269         char **columns;
270         char *column;
271         gsize n, k;
272
273         n = 0;
274         k = 0;
275         list = NULL;
276
277         remainder = buf;
278         str = strstr(remainder, inc->delimiter->str);
279
280         while (str && max_columns) {
281                 if (n >= inc->first_column) {
282                         column = g_strndup(remainder, str - remainder);
283                         list = g_slist_prepend(list, g_strstrip(column));
284
285                         max_columns--;
286                         k++;
287                 }
288
289                 remainder = str + inc->delimiter->len;
290                 str = strstr(remainder, inc->delimiter->str);
291                 n++;
292         }
293
294         if (buf[0] && max_columns && n >= inc->first_column) {
295                 column = g_strdup(remainder);
296                 list = g_slist_prepend(list, g_strstrip(column));
297                 k++;
298         }
299
300         if (!(columns = g_try_new(char *, k + 1)))
301                 return NULL;
302
303         columns[k--] = NULL;
304
305         for (l = list; l; l = l->next)
306                 columns[k--] = l->data;
307
308         g_slist_free(list);
309
310         return columns;
311 }
312
313 static int parse_multi_columns(char **columns, struct context *inc)
314 {
315         gsize i;
316         char *column;
317
318         /* Clear buffer in order to set bits only. */
319         memset(inc->sample_buffer, 0, (inc->num_channels + 7) >> 3);
320
321         for (i = 0; i < inc->num_channels; i++) {
322                 column = columns[i];
323                 if (column[0] == '1') {
324                         inc->sample_buffer[i / 8] |= (1 << (i % 8));
325                 } else if (!strlen(column)) {
326                         sr_err("Column %zu in line %zu is empty.",
327                                 inc->first_channel + i, inc->line_number);
328                         return SR_ERR;
329                 } else if (column[0] != '0') {
330                         sr_err("Invalid value '%s' in column %zu in line %zu.",
331                                 column, inc->first_channel + i,
332                                 inc->line_number);
333                         return SR_ERR;
334                 }
335         }
336
337         return SR_OK;
338 }
339
340 static int parse_single_column(const char *column, struct context *inc)
341 {
342         int res;
343
344         res = SR_ERR;
345
346         switch (inc->format) {
347         case FORMAT_BIN:
348                 res = parse_binstr(column, inc);
349                 break;
350         case FORMAT_HEX:
351                 res = parse_hexstr(column, inc);
352                 break;
353         case FORMAT_OCT:
354                 res = parse_octstr(column, inc);
355                 break;
356         }
357
358         return res;
359 }
360
361 static int send_samples(const struct sr_dev_inst *sdi, uint8_t *buffer,
362                 gsize buffer_size, gsize count)
363 {
364         struct sr_datafeed_packet packet;
365         struct sr_datafeed_logic logic;
366         int res;
367         gsize i;
368
369         packet.type = SR_DF_LOGIC;
370         packet.payload = &logic;
371         logic.unitsize = buffer_size;
372         logic.length = buffer_size;
373         logic.data = buffer;
374
375         for (i = 0; i < count; i++) {
376                 res = sr_session_send(sdi, &packet);
377                 if (res != SR_OK)
378                         return res;
379         }
380
381         return SR_OK;
382 }
383
384 static int init(struct sr_input *in, GHashTable *options)
385 {
386         struct context *inc;
387         const char *s;
388
389         in->sdi = g_malloc0(sizeof(struct sr_dev_inst));
390         in->priv = inc = g_malloc0(sizeof(struct context));
391
392         inc->single_column = g_variant_get_int32(g_hash_table_lookup(options, "single-column"));
393         inc->multi_column_mode = inc->single_column == 0;
394
395         inc->num_channels = g_variant_get_int32(g_hash_table_lookup(options, "numchannels"));
396
397         inc->delimiter = g_string_new(g_variant_get_string(
398                         g_hash_table_lookup(options, "delimiter"), NULL));
399         if (inc->delimiter->len == 0) {
400                 sr_err("Delimiter must be at least one character.");
401                 return SR_ERR_ARG;
402         }
403
404         s = g_variant_get_string(g_hash_table_lookup(options, "format"), NULL);
405         if (!g_ascii_strncasecmp(s, "bin", 3)) {
406                 inc->format = FORMAT_BIN;
407         } else if (!g_ascii_strncasecmp(s, "hex", 3)) {
408                 inc->format = FORMAT_HEX;
409         } else if (!g_ascii_strncasecmp(s, "oct", 3)) {
410                 inc->format = FORMAT_OCT;
411         } else {
412                 sr_err("Invalid format: '%s'", s);
413                 return SR_ERR_ARG;
414         }
415
416         inc->comment = g_string_new(g_variant_get_string(
417                         g_hash_table_lookup(options, "comment"), NULL));
418         if (g_string_equal(inc->comment, inc->delimiter)) {
419                 /* That's never going to work. Likely the result of the user
420                  * setting the delimiter to ; -- the default comment. Clearing
421                  * the comment setting will work in that case. */
422                 g_string_truncate(inc->comment, 0);
423         }
424
425         inc->samplerate = g_variant_get_uint64(g_hash_table_lookup(options, "samplerate"));
426
427         inc->first_channel = g_variant_get_int32(g_hash_table_lookup(options, "first-channel"));
428
429         inc->header = g_variant_get_boolean(g_hash_table_lookup(options, "header"));
430
431         inc->start_line = g_variant_get_int32(g_hash_table_lookup(options, "startline"));
432         if (inc->start_line < 1) {
433                 sr_err("Invalid start line %zu.", inc->start_line);
434                 return SR_ERR_ARG;
435         }
436
437         if (inc->multi_column_mode)
438                 inc->first_column = inc->first_channel;
439         else
440                 inc->first_column = inc->single_column;
441
442         if (!inc->multi_column_mode && !inc->num_channels) {
443                 sr_err("Number of channels needs to be specified in single column mode.");
444                 return SR_ERR_ARG;
445         }
446
447         return SR_OK;
448 }
449
450 static const char *delim_set = "\r\n";
451
452 static const char *get_line_termination(GString *buf)
453 {
454         const char *term;
455
456         term = NULL;
457         if (g_strstr_len(buf->str, buf->len, "\r\n"))
458                 term = "\r\n";
459         else if (memchr(buf->str, '\n', buf->len))
460                 term = "\n";
461         else if (memchr(buf->str, '\r', buf->len))
462                 term = "\r";
463
464         return term;
465 }
466
467 static int initial_parse(const struct sr_input *in, GString *buf)
468 {
469         struct context *inc;
470         GString *channel_name;
471         unsigned int num_columns, i;
472         size_t line_number, l;
473         int ret;
474         char **lines, *line, **columns, *column;
475
476         ret = SR_OK;
477         inc = in->priv;
478         columns = NULL;
479
480         line_number = 0;
481         lines = g_strsplit_set(buf->str, delim_set, 0);
482         for (l = 0; lines[l]; l++) {
483                 line_number++;
484                 line = lines[l];
485                 if (inc->start_line > line_number) {
486                         sr_spew("Line %zu skipped.", line_number);
487                         continue;
488                 }
489                 if (line[0] == '\0') {
490                         sr_spew("Blank line %zu skipped.", line_number);
491                         continue;
492                 }
493                 strip_comment(line, inc->comment);
494                 if (line[0] == '\0') {
495                         sr_spew("Comment-only line %zu skipped.", line_number);
496                         continue;
497                 }
498
499                 /* Reached first proper line. */
500                 break;
501         }
502         if (!lines[l]) {
503                 /* Not enough data for a proper line yet. */
504                 ret = SR_ERR_NA;
505                 goto out;
506         }
507
508         /*
509          * In order to determine the number of columns parse the current line
510          * without limiting the number of columns.
511          */
512         columns = parse_line(line, inc, -1);
513         if (!columns) {
514                 sr_err("Error while parsing line %zu.", line_number);
515                 ret = SR_ERR;
516                 goto out;
517         }
518         num_columns = g_strv_length(columns);
519
520         /* Ensure that the first column is not out of bounds. */
521         if (!num_columns) {
522                 sr_err("Column %u in line %zu is out of bounds.",
523                         inc->first_column, line_number);
524                 ret = SR_ERR;
525                 goto out;
526         }
527
528         if (inc->multi_column_mode) {
529                 /*
530                  * Detect the number of channels in multi column mode
531                  * automatically if not specified.
532                  */
533                 if (!inc->num_channels) {
534                         inc->num_channels = num_columns;
535                         sr_dbg("Number of auto-detected channels: %u.",
536                                 inc->num_channels);
537                 }
538
539                 /*
540                  * Ensure that the number of channels does not exceed the number
541                  * of columns in multi column mode.
542                  */
543                 if (num_columns < inc->num_channels) {
544                         sr_err("Not enough columns for desired number of channels in line %zu.",
545                                 line_number);
546                         ret = SR_ERR;
547                         goto out;
548                 }
549         }
550
551         channel_name = g_string_sized_new(64);
552         for (i = 0; i < inc->num_channels; i++) {
553                 column = columns[i];
554                 if (inc->header && inc->multi_column_mode && column[0] != '\0')
555                         g_string_assign(channel_name, column);
556                 else
557                         g_string_printf(channel_name, "%u", i);
558                 sr_channel_new(in->sdi, i, SR_CHANNEL_LOGIC, TRUE, channel_name->str);
559         }
560         g_string_free(channel_name, TRUE);
561
562         /*
563          * Calculate the minimum buffer size to store the sample data of the
564          * channels.
565          */
566         inc->sample_buffer_size = (inc->num_channels + 7) >> 3;
567         inc->sample_buffer = g_malloc(inc->sample_buffer_size);
568
569 out:
570         if (columns)
571                 g_strfreev(columns);
572         g_strfreev(lines);
573
574         return ret;
575 }
576
577 static int initial_receive(const struct sr_input *in)
578 {
579         struct context *inc;
580         GString *new_buf;
581         int len, ret;
582         char *p;
583         const char *termination;
584
585         inc = in->priv;
586
587         termination = get_line_termination(in->buf);
588         if (!termination)
589                 /* Don't have a full line yet. */
590                 return SR_ERR_NA;
591
592         p = g_strrstr_len(in->buf->str, in->buf->len, termination);
593         if (!p)
594                 /* Don't have a full line yet. */
595                 return SR_ERR_NA;
596         len = p - in->buf->str - 1;
597         new_buf = g_string_new_len(in->buf->str, len);
598         g_string_append_c(new_buf, '\0');
599
600         inc->termination = g_strdup(termination);
601
602         if (in->buf->str[0] != '\0')
603                 ret = initial_parse(in, new_buf);
604         else
605                 ret = SR_OK;
606
607         g_string_free(new_buf, TRUE);
608
609         return ret;
610 }
611
612 static int process_buffer(struct sr_input *in)
613 {
614         struct sr_datafeed_packet packet;
615         struct sr_datafeed_meta meta;
616         struct sr_config *src;
617         struct context *inc;
618         gsize num_columns;
619         uint64_t samplerate;
620         int max_columns, ret, l;
621         char *p, **lines, *line, **columns;
622
623         inc = in->priv;
624         if (!inc->started) {
625                 std_session_send_df_header(in->sdi);
626
627                 if (inc->samplerate) {
628                         packet.type = SR_DF_META;
629                         packet.payload = &meta;
630                         samplerate = inc->samplerate;
631                         src = sr_config_new(SR_CONF_SAMPLERATE, g_variant_new_uint64(samplerate));
632                         meta.config = g_slist_append(NULL, src);
633                         sr_session_send(in->sdi, &packet);
634                         g_slist_free(meta.config);
635                         sr_config_free(src);
636                 }
637
638                 inc->started = TRUE;
639         }
640
641         /* Limit the number of columns to parse. */
642         if (inc->multi_column_mode)
643                 max_columns = inc->num_channels;
644         else
645                 max_columns = 1;
646
647         /*
648          * Consider empty input non-fatal. Keep accumulating input until
649          * at least one full text line has become available. Grab the
650          * maximum amount of accumulated data that consists of full text
651          * lines, and process what has been received so far, leaving not
652          * yet complete lines for the next invocation.
653          */
654         if (!in->buf->len)
655                 return SR_OK;
656         p = g_strrstr_len(in->buf->str, in->buf->len, inc->termination);
657         if (!p)
658                 return SR_ERR;
659         *p = '\0';
660         g_strstrip(in->buf->str);
661
662         ret = SR_OK;
663         lines = g_strsplit_set(in->buf->str, delim_set, 0);
664         for (l = 0; lines[l]; l++) {
665                 inc->line_number++;
666                 line = lines[l];
667                 if (line[0] == '\0') {
668                         sr_spew("Blank line %zu skipped.", inc->line_number);
669                         continue;
670                 }
671
672                 /* Remove trailing comment. */
673                 strip_comment(line, inc->comment);
674                 if (line[0] == '\0') {
675                         sr_spew("Comment-only line %zu skipped.", inc->line_number);
676                         continue;
677                 }
678
679                 /* Skip the header line, its content was used as the channel names. */
680                 if (inc->header) {
681                         sr_spew("Header line %zu skipped.", inc->line_number);
682                         inc->header = FALSE;
683                         continue;
684                 }
685
686                 columns = parse_line(line, inc, max_columns);
687                 if (!columns) {
688                         sr_err("Error while parsing line %zu.", inc->line_number);
689                         return SR_ERR;
690                 }
691                 num_columns = g_strv_length(columns);
692                 if (!num_columns) {
693                         sr_err("Column %u in line %zu is out of bounds.",
694                                 inc->first_column, inc->line_number);
695                         g_strfreev(columns);
696                         return SR_ERR;
697                 }
698                 /*
699                  * Ensure that the number of channels does not exceed the number
700                  * of columns in multi column mode.
701                  */
702                 if (inc->multi_column_mode && num_columns < inc->num_channels) {
703                         sr_err("Not enough columns for desired number of channels in line %zu.",
704                                 inc->line_number);
705                         g_strfreev(columns);
706                         return SR_ERR;
707                 }
708
709                 if (inc->multi_column_mode)
710                         ret = parse_multi_columns(columns, inc);
711                 else
712                         ret = parse_single_column(columns[0], inc);
713                 if (ret != SR_OK) {
714                         g_strfreev(columns);
715                         return SR_ERR;
716                 }
717
718                 /* Send sample data to the session bus. */
719                 ret = send_samples(in->sdi, inc->sample_buffer,
720                         inc->sample_buffer_size, 1);
721                 if (ret != SR_OK) {
722                         sr_err("Sending samples failed.");
723                         return SR_ERR;
724                 }
725                 g_strfreev(columns);
726         }
727         g_strfreev(lines);
728         g_string_erase(in->buf, 0, p - in->buf->str + 1);
729
730         return ret;
731 }
732
733 static int receive(struct sr_input *in, GString *buf)
734 {
735         struct context *inc;
736         int ret;
737
738         g_string_append_len(in->buf, buf->str, buf->len);
739
740         inc = in->priv;
741         if (!inc->termination) {
742                 ret = initial_receive(in);
743                 if (ret == SR_ERR_NA)
744                         /* Not enough data yet. */
745                         return SR_OK;
746                 else if (ret != SR_OK)
747                         return SR_ERR;
748
749                 /* sdi is ready, notify frontend. */
750                 in->sdi_ready = TRUE;
751                 return SR_OK;
752         }
753
754         ret = process_buffer(in);
755
756         return ret;
757 }
758
759 static int end(struct sr_input *in)
760 {
761         struct context *inc;
762         int ret;
763
764         if (in->sdi_ready)
765                 ret = process_buffer(in);
766         else
767                 ret = SR_OK;
768
769         inc = in->priv;
770         if (inc->started)
771                 std_session_send_df_end(in->sdi);
772
773         return ret;
774 }
775
776 static void cleanup(struct sr_input *in)
777 {
778         struct context *inc;
779
780         inc = in->priv;
781
782         if (inc->delimiter)
783                 g_string_free(inc->delimiter, TRUE);
784
785         if (inc->comment)
786                 g_string_free(inc->comment, TRUE);
787
788         g_free(inc->termination);
789         g_free(inc->sample_buffer);
790 }
791
792 static int reset(struct sr_input *in)
793 {
794         struct context *inc = in->priv;
795
796         cleanup(in);
797         inc->started = FALSE;
798         g_string_truncate(in->buf, 0);
799
800         return SR_OK;
801 }
802
803 static struct sr_option options[] = {
804         { "single-column", "Single column", "Enable/specify single column", NULL, NULL },
805         { "numchannels", "Max channels", "Number of channels", NULL, NULL },
806         { "delimiter", "Delimiter", "Column delimiter", NULL, NULL },
807         { "format", "Format", "Numeric format", NULL, NULL },
808         { "comment", "Comment", "Comment prefix character", NULL, NULL },
809         { "samplerate", "Samplerate", "Samplerate used during capture", NULL, NULL },
810         { "first-channel", "First channel", "Column number of first channel", NULL, NULL },
811         { "header", "Header", "Treat first line as header with channel names", NULL, NULL },
812         { "startline", "Start line", "Line number at which to start processing samples", NULL, NULL },
813         ALL_ZERO
814 };
815
816 static const struct sr_option *get_options(void)
817 {
818         if (!options[0].def) {
819                 options[0].def = g_variant_ref_sink(g_variant_new_int32(0));
820                 options[1].def = g_variant_ref_sink(g_variant_new_int32(0));
821                 options[2].def = g_variant_ref_sink(g_variant_new_string(","));
822                 options[3].def = g_variant_ref_sink(g_variant_new_string("bin"));
823                 options[4].def = g_variant_ref_sink(g_variant_new_string(";"));
824                 options[5].def = g_variant_ref_sink(g_variant_new_uint64(0));
825                 options[6].def = g_variant_ref_sink(g_variant_new_int32(0));
826                 options[7].def = g_variant_ref_sink(g_variant_new_boolean(FALSE));
827                 options[8].def = g_variant_ref_sink(g_variant_new_int32(1));
828         }
829
830         return options;
831 }
832
833 SR_PRIV struct sr_input_module input_csv = {
834         .id = "csv",
835         .name = "CSV",
836         .desc = "Comma-separated values",
837         .exts = (const char*[]){"csv", NULL},
838         .options = get_options,
839         .init = init,
840         .receive = receive,
841         .end = end,
842         .cleanup = cleanup,
843         .reset = reset,
844 };