]> sigrok.org Git - libsigrok.git/blame - src/input/csv.c
input/csv: Fix a false negative after successful import
[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
6ec6c43b 20#include <config.h>
4a35548b
MS
21#include <stdlib.h>
22#include <string.h>
23#include <glib.h>
c1aae900 24#include <libsigrok/libsigrok.h>
4a35548b
MS
25#include "libsigrok-internal.h"
26
3544f848 27#define LOG_PREFIX "input/csv"
4a35548b
MS
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 *
ba7dd8bb
UH
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
4a35548b 38 * column mode the number of bits (LSB first) beginning at
ba7dd8bb 39 * 'first-channel'.
4a35548b
MS
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 *
ba7dd8bb
UH
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.
4a35548b
MS
58 * Default value is 0.
59 *
60 * header: Determines if the first line should be treated as header
ba7dd8bb
UH
61 * and used for channel names in multi column mode. Empty header
62 * names will be replaced by the channel number. If enabled in
4a35548b
MS
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. */
71enum {
72 FORMAT_BIN,
73 FORMAT_HEX,
74 FORMAT_OCT
75};
76
77struct context {
41d214f6
BV
78 gboolean started;
79
4a35548b
MS
80 /* Current selected samplerate. */
81 uint64_t samplerate;
82
ba7dd8bb 83 /* Number of channels. */
6433156c 84 unsigned int num_channels;
4a35548b
MS
85
86 /* Column delimiter character(s). */
87 GString *delimiter;
88
89 /* Comment prefix character(s). */
90 GString *comment;
91
d9251a2c 92 /* Termination character(s) used in current stream. */
41d214f6
BV
93 char *termination;
94
4a35548b
MS
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. */
6433156c 99 unsigned int single_column;
4a35548b
MS
100
101 /*
102 * Number of the first column to parse. Equivalent to the number of the
ba7dd8bb 103 * first channel in multi column mode and the single column number in
4a35548b
MS
104 * single column mode.
105 */
6433156c 106 unsigned int first_column;
4a35548b
MS
107
108 /*
ba7dd8bb
UH
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.
4a35548b 111 */
6433156c 112 unsigned int first_channel;
4a35548b
MS
113
114 /* Line number to start processing. */
6433156c 115 size_t start_line;
4a35548b
MS
116
117 /*
118 * Determines if the first line should be treated as header and used for
ba7dd8bb 119 * channel names in multi column mode.
4a35548b
MS
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. */
6433156c 127 size_t sample_buffer_size;
4a35548b
MS
128
129 /* Buffer to store sample data. */
130 uint8_t *sample_buffer;
131
4a35548b 132 /* Current line number. */
6433156c 133 size_t line_number;
4a35548b
MS
134};
135
41d214f6 136static void strip_comment(char *buf, const GString *prefix)
4a35548b
MS
137{
138 char *ptr;
139
140 if (!prefix->len)
141 return;
142
41d214f6
BV
143 if ((ptr = strstr(buf, prefix->str)))
144 *ptr = '\0';
4a35548b
MS
145}
146
41d214f6 147static int parse_binstr(const char *str, struct context *inc)
4a35548b
MS
148{
149 gsize i, j, length;
150
151 length = strlen(str);
152
153 if (!length) {
6433156c 154 sr_err("Column %u in line %zu is empty.", inc->single_column,
41d214f6 155 inc->line_number);
4a35548b
MS
156 return SR_ERR;
157 }
158
159 /* Clear buffer in order to set bits only. */
41d214f6 160 memset(inc->sample_buffer, 0, (inc->num_channels + 7) >> 3);
4a35548b 161
41d214f6 162 i = inc->first_channel;
4a35548b 163
41d214f6 164 for (j = 0; i < length && j < inc->num_channels; i++, j++) {
4a35548b 165 if (str[length - i - 1] == '1') {
41d214f6 166 inc->sample_buffer[j / 8] |= (1 << (j % 8));
4a35548b 167 } else if (str[length - i - 1] != '0') {
6433156c 168 sr_err("Invalid value '%s' in column %u in line %zu.",
41d214f6 169 str, inc->single_column, inc->line_number);
4a35548b
MS
170 return SR_ERR;
171 }
172 }
173
174 return SR_OK;
175}
176
41d214f6 177static int parse_hexstr(const char *str, struct context *inc)
4a35548b
MS
178{
179 gsize i, j, k, length;
180 uint8_t value;
181 char c;
182
183 length = strlen(str);
184
185 if (!length) {
6433156c 186 sr_err("Column %u in line %zu is empty.", inc->single_column,
41d214f6 187 inc->line_number);
4a35548b
MS
188 return SR_ERR;
189 }
190
191 /* Clear buffer in order to set bits only. */
41d214f6 192 memset(inc->sample_buffer, 0, (inc->num_channels + 7) >> 3);
4a35548b
MS
193
194 /* Calculate the position of the first hexadecimal digit. */
41d214f6 195 i = inc->first_channel / 4;
4a35548b 196
41d214f6 197 for (j = 0; i < length && j < inc->num_channels; i++) {
4a35548b
MS
198 c = str[length - i - 1];
199
200 if (!g_ascii_isxdigit(c)) {
6433156c 201 sr_err("Invalid value '%s' in column %u in line %zu.",
41d214f6 202 str, inc->single_column, inc->line_number);
4a35548b
MS
203 return SR_ERR;
204 }
205
206 value = g_ascii_xdigit_value(c);
207
41d214f6 208 k = (inc->first_channel + j) % 4;
4a35548b 209
41d214f6 210 for (; j < inc->num_channels && k < 4; k++) {
4a35548b 211 if (value & (1 << k))
41d214f6 212 inc->sample_buffer[j / 8] |= (1 << (j % 8));
4a35548b
MS
213
214 j++;
215 }
216 }
217
218 return SR_OK;
219}
220
41d214f6 221static int parse_octstr(const char *str, struct context *inc)
4a35548b
MS
222{
223 gsize i, j, k, length;
224 uint8_t value;
225 char c;
226
227 length = strlen(str);
228
229 if (!length) {
6433156c 230 sr_err("Column %u in line %zu is empty.", inc->single_column,
41d214f6 231 inc->line_number);
4a35548b
MS
232 return SR_ERR;
233 }
234
235 /* Clear buffer in order to set bits only. */
41d214f6 236 memset(inc->sample_buffer, 0, (inc->num_channels + 7) >> 3);
4a35548b
MS
237
238 /* Calculate the position of the first octal digit. */
41d214f6 239 i = inc->first_channel / 3;
4a35548b 240
41d214f6 241 for (j = 0; i < length && j < inc->num_channels; i++) {
4a35548b
MS
242 c = str[length - i - 1];
243
244 if (c < '0' || c > '7') {
6433156c 245 sr_err("Invalid value '%s' in column %u in line %zu.",
41d214f6 246 str, inc->single_column, inc->line_number);
4a35548b
MS
247 return SR_ERR;
248 }
249
250 value = g_ascii_xdigit_value(c);
251
41d214f6 252 k = (inc->first_channel + j) % 3;
4a35548b 253
41d214f6 254 for (; j < inc->num_channels && k < 3; k++) {
4a35548b 255 if (value & (1 << k))
41d214f6 256 inc->sample_buffer[j / 8] |= (1 << (j % 8));
4a35548b
MS
257
258 j++;
259 }
260 }
261
262 return SR_OK;
263}
264
41d214f6 265static char **parse_line(char *buf, struct context *inc, int max_columns)
4a35548b
MS
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
41d214f6
BV
277 remainder = buf;
278 str = strstr(remainder, inc->delimiter->str);
4a35548b
MS
279
280 while (str && max_columns) {
41d214f6 281 if (n >= inc->first_column) {
4a35548b
MS
282 column = g_strndup(remainder, str - remainder);
283 list = g_slist_prepend(list, g_strstrip(column));
284
285 max_columns--;
286 k++;
287 }
288
41d214f6
BV
289 remainder = str + inc->delimiter->len;
290 str = strstr(remainder, inc->delimiter->str);
4a35548b
MS
291 n++;
292 }
293
41d214f6 294 if (buf[0] && max_columns && n >= inc->first_column) {
4a35548b
MS
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
41d214f6 313static int parse_multi_columns(char **columns, struct context *inc)
4a35548b
MS
314{
315 gsize i;
df0db9fd 316 char *column;
4a35548b
MS
317
318 /* Clear buffer in order to set bits only. */
41d214f6 319 memset(inc->sample_buffer, 0, (inc->num_channels + 7) >> 3);
4a35548b 320
41d214f6 321 for (i = 0; i < inc->num_channels; i++) {
df0db9fd
GS
322 column = columns[i];
323 if (column[0] == '1') {
41d214f6 324 inc->sample_buffer[i / 8] |= (1 << (i % 8));
df0db9fd 325 } else if (!strlen(column)) {
4a35548b 326 sr_err("Column %zu in line %zu is empty.",
41d214f6 327 inc->first_channel + i, inc->line_number);
4a35548b 328 return SR_ERR;
df0db9fd 329 } else if (column[0] != '0') {
4a35548b 330 sr_err("Invalid value '%s' in column %zu in line %zu.",
df0db9fd 331 column, inc->first_channel + i,
41d214f6 332 inc->line_number);
4a35548b
MS
333 return SR_ERR;
334 }
335 }
336
337 return SR_OK;
338}
339
41d214f6 340static int parse_single_column(const char *column, struct context *inc)
4a35548b
MS
341{
342 int res;
343
344 res = SR_ERR;
345
0c5f2abc 346 switch (inc->format) {
4a35548b 347 case FORMAT_BIN:
41d214f6 348 res = parse_binstr(column, inc);
4a35548b
MS
349 break;
350 case FORMAT_HEX:
41d214f6 351 res = parse_hexstr(column, inc);
4a35548b
MS
352 break;
353 case FORMAT_OCT:
41d214f6 354 res = parse_octstr(column, inc);
4a35548b
MS
355 break;
356 }
357
358 return res;
359}
360
361static int send_samples(const struct sr_dev_inst *sdi, uint8_t *buffer,
41d214f6 362 gsize buffer_size, gsize count)
4a35548b 363{
4a35548b
MS
364 struct sr_datafeed_packet packet;
365 struct sr_datafeed_logic logic;
41d214f6 366 int res;
4a35548b
MS
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++) {
df0db9fd
GS
376 res = sr_session_send(sdi, &packet);
377 if (res != SR_OK)
4a35548b
MS
378 return res;
379 }
380
381 return SR_OK;
382}
383
41d214f6 384static int init(struct sr_input *in, GHashTable *options)
4a35548b 385{
41d214f6
BV
386 struct context *inc;
387 const char *s;
4a35548b 388
aac29cc1 389 in->sdi = g_malloc0(sizeof(struct sr_dev_inst));
41d214f6 390 in->priv = inc = g_malloc0(sizeof(struct context));
4a35548b 391
41d214f6
BV
392 inc->single_column = g_variant_get_int32(g_hash_table_lookup(options, "single-column"));
393 inc->multi_column_mode = inc->single_column == 0;
4a35548b 394
41d214f6 395 inc->num_channels = g_variant_get_int32(g_hash_table_lookup(options, "numchannels"));
4a35548b 396
41d214f6
BV
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;
4a35548b
MS
402 }
403
41d214f6
BV
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;
4a35548b
MS
414 }
415
41d214f6
BV
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);
4a35548b
MS
423 }
424
6e8d95a5 425 inc->samplerate = g_variant_get_uint64(g_hash_table_lookup(options, "samplerate"));
4a35548b 426
41d214f6 427 inc->first_channel = g_variant_get_int32(g_hash_table_lookup(options, "first-channel"));
4a35548b 428
41d214f6 429 inc->header = g_variant_get_boolean(g_hash_table_lookup(options, "header"));
4a35548b 430
41d214f6
BV
431 inc->start_line = g_variant_get_int32(g_hash_table_lookup(options, "startline"));
432 if (inc->start_line < 1) {
6433156c 433 sr_err("Invalid start line %zu.", inc->start_line);
41d214f6 434 return SR_ERR_ARG;
4a35548b
MS
435 }
436
41d214f6
BV
437 if (inc->multi_column_mode)
438 inc->first_column = inc->first_channel;
4a35548b 439 else
41d214f6 440 inc->first_column = inc->single_column;
4a35548b 441
41d214f6 442 if (!inc->multi_column_mode && !inc->num_channels) {
ba7dd8bb 443 sr_err("Number of channels needs to be specified in single column mode.");
41d214f6 444 return SR_ERR_ARG;
4a35548b
MS
445 }
446
41d214f6
BV
447 return SR_OK;
448}
4a35548b 449
492dfa90
GS
450static const char *delim_set = "\r\n";
451
329733d9 452static const char *get_line_termination(GString *buf)
41d214f6 453{
329733d9 454 const char *term;
4a35548b 455
41d214f6
BV
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";
4a35548b 463
41d214f6
BV
464 return term;
465}
4a35548b 466
41d214f6
BV
467static int initial_parse(const struct sr_input *in, GString *buf)
468{
469 struct context *inc;
41d214f6 470 GString *channel_name;
6433156c
DE
471 unsigned int num_columns, i;
472 size_t line_number, l;
41d214f6 473 int ret;
df0db9fd 474 char **lines, *line, **columns, *column;
41d214f6
BV
475
476 ret = SR_OK;
477 inc = in->priv;
478 columns = NULL;
479
480 line_number = 0;
492dfa90 481 lines = g_strsplit_set(buf->str, delim_set, 0);
41d214f6
BV
482 for (l = 0; lines[l]; l++) {
483 line_number++;
df0db9fd 484 line = lines[l];
41d214f6
BV
485 if (inc->start_line > line_number) {
486 sr_spew("Line %zu skipped.", line_number);
4a35548b
MS
487 continue;
488 }
df0db9fd 489 if (line[0] == '\0') {
41d214f6
BV
490 sr_spew("Blank line %zu skipped.", line_number);
491 continue;
492 }
df0db9fd
GS
493 strip_comment(line, inc->comment);
494 if (line[0] == '\0') {
41d214f6 495 sr_spew("Comment-only line %zu skipped.", line_number);
4a35548b
MS
496 continue;
497 }
498
41d214f6
BV
499 /* Reached first proper line. */
500 break;
501 }
502 if (!lines[l]) {
503 /* Not enough data for a proper line yet. */
60107497 504 ret = SR_ERR_NA;
41d214f6 505 goto out;
4a35548b
MS
506 }
507
508 /*
509 * In order to determine the number of columns parse the current line
510 * without limiting the number of columns.
511 */
df0db9fd
GS
512 columns = parse_line(line, inc, -1);
513 if (!columns) {
41d214f6
BV
514 sr_err("Error while parsing line %zu.", line_number);
515 ret = SR_ERR;
516 goto out;
4a35548b 517 }
4a35548b
MS
518 num_columns = g_strv_length(columns);
519
520 /* Ensure that the first column is not out of bounds. */
521 if (!num_columns) {
6433156c 522 sr_err("Column %u in line %zu is out of bounds.",
41d214f6
BV
523 inc->first_column, line_number);
524 ret = SR_ERR;
525 goto out;
4a35548b
MS
526 }
527
41d214f6 528 if (inc->multi_column_mode) {
4a35548b 529 /*
ba7dd8bb 530 * Detect the number of channels in multi column mode
4a35548b
MS
531 * automatically if not specified.
532 */
41d214f6
BV
533 if (!inc->num_channels) {
534 inc->num_channels = num_columns;
6433156c 535 sr_dbg("Number of auto-detected channels: %u.",
41d214f6 536 inc->num_channels);
4a35548b
MS
537 }
538
539 /*
ba7dd8bb 540 * Ensure that the number of channels does not exceed the number
4a35548b
MS
541 * of columns in multi column mode.
542 */
41d214f6 543 if (num_columns < inc->num_channels) {
ba7dd8bb 544 sr_err("Not enough columns for desired number of channels in line %zu.",
41d214f6
BV
545 line_number);
546 ret = SR_ERR;
547 goto out;
4a35548b
MS
548 }
549 }
550
41d214f6
BV
551 channel_name = g_string_sized_new(64);
552 for (i = 0; i < inc->num_channels; i++) {
df0db9fd
GS
553 column = columns[i];
554 if (inc->header && inc->multi_column_mode && column[0] != '\0')
555 g_string_assign(channel_name, column);
4a35548b 556 else
6433156c 557 g_string_printf(channel_name, "%u", i);
5e23fcab 558 sr_channel_new(in->sdi, i, SR_CHANNEL_LOGIC, TRUE, channel_name->str);
4a35548b 559 }
41d214f6 560 g_string_free(channel_name, TRUE);
4a35548b
MS
561
562 /*
563 * Calculate the minimum buffer size to store the sample data of the
ba7dd8bb 564 * channels.
4a35548b 565 */
41d214f6
BV
566 inc->sample_buffer_size = (inc->num_channels + 7) >> 3;
567 inc->sample_buffer = g_malloc(inc->sample_buffer_size);
4a35548b 568
41d214f6
BV
569out:
570 if (columns)
571 g_strfreev(columns);
572 g_strfreev(lines);
4a35548b 573
41d214f6 574 return ret;
4a35548b
MS
575}
576
41d214f6 577static int initial_receive(const struct sr_input *in)
4a35548b 578{
41d214f6
BV
579 struct context *inc;
580 GString *new_buf;
581 int len, ret;
329733d9
UH
582 char *p;
583 const char *termination;
4a35548b 584
41d214f6 585 inc = in->priv;
4a35548b 586
df0db9fd
GS
587 termination = get_line_termination(in->buf);
588 if (!termination)
41d214f6 589 /* Don't have a full line yet. */
d0181813 590 return SR_ERR_NA;
4a35548b 591
df0db9fd
GS
592 p = g_strrstr_len(in->buf->str, in->buf->len, termination);
593 if (!p)
41d214f6 594 /* Don't have a full line yet. */
d0181813 595 return SR_ERR_NA;
41d214f6
BV
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');
4a35548b 599
41d214f6
BV
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
7066fd46 612static int process_buffer(struct sr_input *in)
41d214f6
BV
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;
df0db9fd 621 char *p, **lines, *line, **columns;
41d214f6 622
41d214f6 623 inc = in->priv;
d0181813 624 if (!inc->started) {
bee2b016 625 std_session_send_df_header(in->sdi);
41d214f6
BV
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);
c01378c9 634 g_slist_free(meta.config);
41d214f6
BV
635 sr_config_free(src);
636 }
d0181813
BV
637
638 inc->started = TRUE;
4a35548b
MS
639 }
640
f9b74861
GS
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
4555d3bd
GS
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;
df0db9fd
GS
656 p = g_strrstr_len(in->buf->str, in->buf->len, inc->termination);
657 if (!p)
41d214f6 658 return SR_ERR;
41d214f6
BV
659 *p = '\0';
660 g_strstrip(in->buf->str);
4a35548b 661
18078d05 662 ret = SR_OK;
492dfa90 663 lines = g_strsplit_set(in->buf->str, delim_set, 0);
41d214f6
BV
664 for (l = 0; lines[l]; l++) {
665 inc->line_number++;
df0db9fd
GS
666 line = lines[l];
667 if (line[0] == '\0') {
41d214f6 668 sr_spew("Blank line %zu skipped.", inc->line_number);
4a35548b
MS
669 continue;
670 }
671
672 /* Remove trailing comment. */
df0db9fd
GS
673 strip_comment(line, inc->comment);
674 if (line[0] == '\0') {
41d214f6 675 sr_spew("Comment-only line %zu skipped.", inc->line_number);
4a35548b
MS
676 continue;
677 }
678
160691b9
JS
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
df0db9fd
GS
686 columns = parse_line(line, inc, max_columns);
687 if (!columns) {
41d214f6 688 sr_err("Error while parsing line %zu.", inc->line_number);
4a35548b
MS
689 return SR_ERR;
690 }
4a35548b 691 num_columns = g_strv_length(columns);
4a35548b 692 if (!num_columns) {
6433156c 693 sr_err("Column %u in line %zu is out of bounds.",
41d214f6 694 inc->first_column, inc->line_number);
4a35548b 695 g_strfreev(columns);
4a35548b
MS
696 return SR_ERR;
697 }
4a35548b 698 /*
ba7dd8bb 699 * Ensure that the number of channels does not exceed the number
4a35548b
MS
700 * of columns in multi column mode.
701 */
41d214f6 702 if (inc->multi_column_mode && num_columns < inc->num_channels) {
ba7dd8bb 703 sr_err("Not enough columns for desired number of channels in line %zu.",
41d214f6 704 inc->line_number);
4a35548b 705 g_strfreev(columns);
4a35548b
MS
706 return SR_ERR;
707 }
708
41d214f6
BV
709 if (inc->multi_column_mode)
710 ret = parse_multi_columns(columns, inc);
4a35548b 711 else
41d214f6
BV
712 ret = parse_single_column(columns[0], inc);
713 if (ret != SR_OK) {
4a35548b 714 g_strfreev(columns);
4a35548b
MS
715 return SR_ERR;
716 }
717
4a35548b 718 /* Send sample data to the session bus. */
41d214f6
BV
719 ret = send_samples(in->sdi, inc->sample_buffer,
720 inc->sample_buffer_size, 1);
721 if (ret != SR_OK) {
4a35548b 722 sr_err("Sending samples failed.");
4a35548b
MS
723 return SR_ERR;
724 }
41d214f6
BV
725 g_strfreev(columns);
726 }
727 g_strfreev(lines);
728 g_string_erase(in->buf, 0, p - in->buf->str + 1);
729
7066fd46 730 return ret;
41d214f6
BV
731}
732
7066fd46 733static int receive(struct sr_input *in, GString *buf)
41d214f6
BV
734{
735 struct context *inc;
7066fd46
BV
736 int ret;
737
738 g_string_append_len(in->buf, buf->str, buf->len);
41d214f6
BV
739
740 inc = in->priv;
7066fd46 741 if (!inc->termination) {
df0db9fd
GS
742 ret = initial_receive(in);
743 if (ret == SR_ERR_NA)
7066fd46
BV
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;
41d214f6 751 return SR_OK;
7066fd46
BV
752 }
753
754 ret = process_buffer(in);
755
756 return ret;
757}
758
759static int end(struct sr_input *in)
760{
761 struct context *inc;
7066fd46 762 int ret;
41d214f6 763
7066fd46
BV
764 if (in->sdi_ready)
765 ret = process_buffer(in);
766 else
767 ret = SR_OK;
768
769 inc = in->priv;
3be42bc2 770 if (inc->started)
bee2b016 771 std_session_send_df_end(in->sdi);
4a35548b 772
7066fd46
BV
773 return ret;
774}
775
d5cc282f 776static void cleanup(struct sr_input *in)
7066fd46
BV
777{
778 struct context *inc;
779
780 inc = in->priv;
781
41d214f6
BV
782 if (inc->delimiter)
783 g_string_free(inc->delimiter, TRUE);
784
785 if (inc->comment)
786 g_string_free(inc->comment, TRUE);
4a35548b 787
b1f83103
UH
788 g_free(inc->termination);
789 g_free(inc->sample_buffer);
4a35548b
MS
790}
791
ad93bfb0
SA
792static 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
41d214f6
BV
803static 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 },
06ad20be 813 ALL_ZERO
41d214f6
BV
814};
815
2c240774 816static const struct sr_option *get_options(void)
41d214f6
BV
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(";"));
6e8d95a5 824 options[5].def = g_variant_ref_sink(g_variant_new_uint64(0));
41d214f6
BV
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
d4c93774 833SR_PRIV struct sr_input_module input_csv = {
4a35548b 834 .id = "csv",
41d214f6
BV
835 .name = "CSV",
836 .desc = "Comma-separated values",
c7bc82ff 837 .exts = (const char*[]){"csv", NULL},
41d214f6 838 .options = get_options,
4a35548b 839 .init = init,
41d214f6 840 .receive = receive,
7066fd46 841 .end = end,
41d214f6 842 .cleanup = cleanup,
ad93bfb0 843 .reset = reset,
4a35548b 844};