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