]> sigrok.org Git - libsigrok.git/blame - src/input/csv.c
input/csv: Update developer comment (fix for last EOL marker)
[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
ccff468b
GS
70/*
71 * TODO
72 *
73 * - Determine how the text line handling can get improved, regarding
74 * all of robustness and flexibility and correctness.
75 * - The current implementation splits on "any run of CR and LF". Which
76 * translates to: Line numbers are wrong in the presence of empty
de788af4 77 * lines in the input stream. See below for an (expensive) fix.
ccff468b
GS
78 * - Dropping support for CR style end-of-line markers could improve
79 * the situation a lot. Code could search for and split on LF, and
80 * trim optional trailing CR. This would result in proper support
81 * for CRLF (Windows) as well as LF (Unix), and allow for correct
82 * line number counts.
83 * - When support for CR-only line termination cannot get dropped,
84 * then the current implementation is inappropriate. Currently the
85 * input stream is scanned for the first occurance of either of the
86 * supported termination styles (which is good). For the remaining
87 * session a consistent encoding of the text lines is assumed (which
de788af4 88 * is acceptable).
ccff468b
GS
89 * - When line numbers need to be correct and reliable, _and_ the full
90 * set of previously supported line termination sequences are required,
91 * and potentially more are to get added for improved compatibility
92 * with more platforms or generators, then the current approach of
93 * splitting on runs of termination characters needs to get replaced,
94 * by the more expensive approach to scan for and count the initially
95 * determined termination sequence.
96 *
97 * - Add support for analog input data? (optional)
98 * - Needs a syntax first for user specs which channels (columns) are
99 * logic and which are analog. May need heuristics(?) to guess from
100 * input data in the absence of user provided specs.
101 */
102
4a35548b
MS
103/* Single column formats. */
104enum {
105 FORMAT_BIN,
106 FORMAT_HEX,
107 FORMAT_OCT
108};
109
110struct context {
41d214f6
BV
111 gboolean started;
112
4a35548b
MS
113 /* Current selected samplerate. */
114 uint64_t samplerate;
115
ba7dd8bb 116 /* Number of channels. */
6433156c 117 unsigned int num_channels;
4a35548b
MS
118
119 /* Column delimiter character(s). */
120 GString *delimiter;
121
122 /* Comment prefix character(s). */
123 GString *comment;
124
d9251a2c 125 /* Termination character(s) used in current stream. */
41d214f6
BV
126 char *termination;
127
4a35548b
MS
128 /* Determines if sample data is stored in multiple columns. */
129 gboolean multi_column_mode;
130
131 /* Column number of the sample data in single column mode. */
6433156c 132 unsigned int single_column;
4a35548b
MS
133
134 /*
135 * Number of the first column to parse. Equivalent to the number of the
ba7dd8bb 136 * first channel in multi column mode and the single column number in
4a35548b
MS
137 * single column mode.
138 */
6433156c 139 unsigned int first_column;
4a35548b
MS
140
141 /*
ba7dd8bb
UH
142 * Column number of the first channel in multi column mode and position of
143 * the bit for the first channel in single column mode.
4a35548b 144 */
6433156c 145 unsigned int first_channel;
4a35548b
MS
146
147 /* Line number to start processing. */
6433156c 148 size_t start_line;
4a35548b
MS
149
150 /*
151 * Determines if the first line should be treated as header and used for
ba7dd8bb 152 * channel names in multi column mode.
4a35548b
MS
153 */
154 gboolean header;
155
156 /* Format sample data is stored in single column mode. */
157 int format;
158
159 /* Size of the sample buffer. */
6433156c 160 size_t sample_buffer_size;
4a35548b
MS
161
162 /* Buffer to store sample data. */
163 uint8_t *sample_buffer;
164
4a35548b 165 /* Current line number. */
6433156c 166 size_t line_number;
4a35548b
MS
167};
168
41d214f6 169static void strip_comment(char *buf, const GString *prefix)
4a35548b
MS
170{
171 char *ptr;
172
173 if (!prefix->len)
174 return;
175
41d214f6
BV
176 if ((ptr = strstr(buf, prefix->str)))
177 *ptr = '\0';
4a35548b
MS
178}
179
41d214f6 180static int parse_binstr(const char *str, struct context *inc)
4a35548b
MS
181{
182 gsize i, j, length;
183
184 length = strlen(str);
185
186 if (!length) {
6433156c 187 sr_err("Column %u in line %zu is empty.", inc->single_column,
41d214f6 188 inc->line_number);
4a35548b
MS
189 return SR_ERR;
190 }
191
192 /* Clear buffer in order to set bits only. */
41d214f6 193 memset(inc->sample_buffer, 0, (inc->num_channels + 7) >> 3);
4a35548b 194
41d214f6 195 i = inc->first_channel;
4a35548b 196
41d214f6 197 for (j = 0; i < length && j < inc->num_channels; i++, j++) {
4a35548b 198 if (str[length - i - 1] == '1') {
41d214f6 199 inc->sample_buffer[j / 8] |= (1 << (j % 8));
4a35548b 200 } else if (str[length - i - 1] != '0') {
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
207 return SR_OK;
208}
209
41d214f6 210static int parse_hexstr(const char *str, struct context *inc)
4a35548b
MS
211{
212 gsize i, j, k, length;
213 uint8_t value;
214 char c;
215
216 length = strlen(str);
217
218 if (!length) {
6433156c 219 sr_err("Column %u in line %zu is empty.", inc->single_column,
41d214f6 220 inc->line_number);
4a35548b
MS
221 return SR_ERR;
222 }
223
224 /* Clear buffer in order to set bits only. */
41d214f6 225 memset(inc->sample_buffer, 0, (inc->num_channels + 7) >> 3);
4a35548b
MS
226
227 /* Calculate the position of the first hexadecimal digit. */
41d214f6 228 i = inc->first_channel / 4;
4a35548b 229
41d214f6 230 for (j = 0; i < length && j < inc->num_channels; i++) {
4a35548b
MS
231 c = str[length - i - 1];
232
233 if (!g_ascii_isxdigit(c)) {
6433156c 234 sr_err("Invalid value '%s' in column %u in line %zu.",
41d214f6 235 str, inc->single_column, inc->line_number);
4a35548b
MS
236 return SR_ERR;
237 }
238
239 value = g_ascii_xdigit_value(c);
240
41d214f6 241 k = (inc->first_channel + j) % 4;
4a35548b 242
41d214f6 243 for (; j < inc->num_channels && k < 4; k++) {
4a35548b 244 if (value & (1 << k))
41d214f6 245 inc->sample_buffer[j / 8] |= (1 << (j % 8));
4a35548b
MS
246
247 j++;
248 }
249 }
250
251 return SR_OK;
252}
253
41d214f6 254static int parse_octstr(const char *str, struct context *inc)
4a35548b
MS
255{
256 gsize i, j, k, length;
257 uint8_t value;
258 char c;
259
260 length = strlen(str);
261
262 if (!length) {
6433156c 263 sr_err("Column %u in line %zu is empty.", inc->single_column,
41d214f6 264 inc->line_number);
4a35548b
MS
265 return SR_ERR;
266 }
267
268 /* Clear buffer in order to set bits only. */
41d214f6 269 memset(inc->sample_buffer, 0, (inc->num_channels + 7) >> 3);
4a35548b
MS
270
271 /* Calculate the position of the first octal digit. */
41d214f6 272 i = inc->first_channel / 3;
4a35548b 273
41d214f6 274 for (j = 0; i < length && j < inc->num_channels; i++) {
4a35548b
MS
275 c = str[length - i - 1];
276
277 if (c < '0' || c > '7') {
6433156c 278 sr_err("Invalid value '%s' in column %u in line %zu.",
41d214f6 279 str, inc->single_column, inc->line_number);
4a35548b
MS
280 return SR_ERR;
281 }
282
283 value = g_ascii_xdigit_value(c);
284
41d214f6 285 k = (inc->first_channel + j) % 3;
4a35548b 286
41d214f6 287 for (; j < inc->num_channels && k < 3; k++) {
4a35548b 288 if (value & (1 << k))
41d214f6 289 inc->sample_buffer[j / 8] |= (1 << (j % 8));
4a35548b
MS
290
291 j++;
292 }
293 }
294
295 return SR_OK;
296}
297
41d214f6 298static char **parse_line(char *buf, struct context *inc, int max_columns)
4a35548b
MS
299{
300 const char *str, *remainder;
301 GSList *list, *l;
302 char **columns;
303 char *column;
304 gsize n, k;
305
306 n = 0;
307 k = 0;
308 list = NULL;
309
41d214f6
BV
310 remainder = buf;
311 str = strstr(remainder, inc->delimiter->str);
4a35548b
MS
312
313 while (str && max_columns) {
41d214f6 314 if (n >= inc->first_column) {
4a35548b
MS
315 column = g_strndup(remainder, str - remainder);
316 list = g_slist_prepend(list, g_strstrip(column));
317
318 max_columns--;
319 k++;
320 }
321
41d214f6
BV
322 remainder = str + inc->delimiter->len;
323 str = strstr(remainder, inc->delimiter->str);
4a35548b
MS
324 n++;
325 }
326
41d214f6 327 if (buf[0] && max_columns && n >= inc->first_column) {
4a35548b
MS
328 column = g_strdup(remainder);
329 list = g_slist_prepend(list, g_strstrip(column));
330 k++;
331 }
332
333 if (!(columns = g_try_new(char *, k + 1)))
334 return NULL;
335
336 columns[k--] = NULL;
337
338 for (l = list; l; l = l->next)
339 columns[k--] = l->data;
340
341 g_slist_free(list);
342
343 return columns;
344}
345
41d214f6 346static int parse_multi_columns(char **columns, struct context *inc)
4a35548b
MS
347{
348 gsize i;
df0db9fd 349 char *column;
4a35548b
MS
350
351 /* Clear buffer in order to set bits only. */
41d214f6 352 memset(inc->sample_buffer, 0, (inc->num_channels + 7) >> 3);
4a35548b 353
41d214f6 354 for (i = 0; i < inc->num_channels; i++) {
df0db9fd
GS
355 column = columns[i];
356 if (column[0] == '1') {
41d214f6 357 inc->sample_buffer[i / 8] |= (1 << (i % 8));
df0db9fd 358 } else if (!strlen(column)) {
4a35548b 359 sr_err("Column %zu in line %zu is empty.",
41d214f6 360 inc->first_channel + i, inc->line_number);
4a35548b 361 return SR_ERR;
df0db9fd 362 } else if (column[0] != '0') {
4a35548b 363 sr_err("Invalid value '%s' in column %zu in line %zu.",
df0db9fd 364 column, inc->first_channel + i,
41d214f6 365 inc->line_number);
4a35548b
MS
366 return SR_ERR;
367 }
368 }
369
370 return SR_OK;
371}
372
41d214f6 373static int parse_single_column(const char *column, struct context *inc)
4a35548b
MS
374{
375 int res;
376
377 res = SR_ERR;
378
0c5f2abc 379 switch (inc->format) {
4a35548b 380 case FORMAT_BIN:
41d214f6 381 res = parse_binstr(column, inc);
4a35548b
MS
382 break;
383 case FORMAT_HEX:
41d214f6 384 res = parse_hexstr(column, inc);
4a35548b
MS
385 break;
386 case FORMAT_OCT:
41d214f6 387 res = parse_octstr(column, inc);
4a35548b
MS
388 break;
389 }
390
391 return res;
392}
393
394static int send_samples(const struct sr_dev_inst *sdi, uint8_t *buffer,
41d214f6 395 gsize buffer_size, gsize count)
4a35548b 396{
4a35548b
MS
397 struct sr_datafeed_packet packet;
398 struct sr_datafeed_logic logic;
41d214f6 399 int res;
4a35548b
MS
400 gsize i;
401
402 packet.type = SR_DF_LOGIC;
403 packet.payload = &logic;
404 logic.unitsize = buffer_size;
405 logic.length = buffer_size;
406 logic.data = buffer;
407
408 for (i = 0; i < count; i++) {
df0db9fd
GS
409 res = sr_session_send(sdi, &packet);
410 if (res != SR_OK)
4a35548b
MS
411 return res;
412 }
413
414 return SR_OK;
415}
416
41d214f6 417static int init(struct sr_input *in, GHashTable *options)
4a35548b 418{
41d214f6
BV
419 struct context *inc;
420 const char *s;
4a35548b 421
aac29cc1 422 in->sdi = g_malloc0(sizeof(struct sr_dev_inst));
41d214f6 423 in->priv = inc = g_malloc0(sizeof(struct context));
4a35548b 424
41d214f6
BV
425 inc->single_column = g_variant_get_int32(g_hash_table_lookup(options, "single-column"));
426 inc->multi_column_mode = inc->single_column == 0;
4a35548b 427
41d214f6 428 inc->num_channels = g_variant_get_int32(g_hash_table_lookup(options, "numchannels"));
4a35548b 429
41d214f6
BV
430 inc->delimiter = g_string_new(g_variant_get_string(
431 g_hash_table_lookup(options, "delimiter"), NULL));
432 if (inc->delimiter->len == 0) {
433 sr_err("Delimiter must be at least one character.");
434 return SR_ERR_ARG;
4a35548b
MS
435 }
436
41d214f6
BV
437 s = g_variant_get_string(g_hash_table_lookup(options, "format"), NULL);
438 if (!g_ascii_strncasecmp(s, "bin", 3)) {
439 inc->format = FORMAT_BIN;
440 } else if (!g_ascii_strncasecmp(s, "hex", 3)) {
441 inc->format = FORMAT_HEX;
442 } else if (!g_ascii_strncasecmp(s, "oct", 3)) {
443 inc->format = FORMAT_OCT;
444 } else {
445 sr_err("Invalid format: '%s'", s);
446 return SR_ERR_ARG;
4a35548b
MS
447 }
448
41d214f6
BV
449 inc->comment = g_string_new(g_variant_get_string(
450 g_hash_table_lookup(options, "comment"), NULL));
451 if (g_string_equal(inc->comment, inc->delimiter)) {
452 /* That's never going to work. Likely the result of the user
453 * setting the delimiter to ; -- the default comment. Clearing
454 * the comment setting will work in that case. */
455 g_string_truncate(inc->comment, 0);
4a35548b
MS
456 }
457
6e8d95a5 458 inc->samplerate = g_variant_get_uint64(g_hash_table_lookup(options, "samplerate"));
4a35548b 459
41d214f6 460 inc->first_channel = g_variant_get_int32(g_hash_table_lookup(options, "first-channel"));
4a35548b 461
41d214f6 462 inc->header = g_variant_get_boolean(g_hash_table_lookup(options, "header"));
4a35548b 463
41d214f6
BV
464 inc->start_line = g_variant_get_int32(g_hash_table_lookup(options, "startline"));
465 if (inc->start_line < 1) {
6433156c 466 sr_err("Invalid start line %zu.", inc->start_line);
41d214f6 467 return SR_ERR_ARG;
4a35548b
MS
468 }
469
41d214f6
BV
470 if (inc->multi_column_mode)
471 inc->first_column = inc->first_channel;
4a35548b 472 else
41d214f6 473 inc->first_column = inc->single_column;
4a35548b 474
41d214f6 475 if (!inc->multi_column_mode && !inc->num_channels) {
ba7dd8bb 476 sr_err("Number of channels needs to be specified in single column mode.");
41d214f6 477 return SR_ERR_ARG;
4a35548b
MS
478 }
479
41d214f6
BV
480 return SR_OK;
481}
4a35548b 482
492dfa90
GS
483static const char *delim_set = "\r\n";
484
329733d9 485static const char *get_line_termination(GString *buf)
41d214f6 486{
329733d9 487 const char *term;
4a35548b 488
41d214f6
BV
489 term = NULL;
490 if (g_strstr_len(buf->str, buf->len, "\r\n"))
491 term = "\r\n";
492 else if (memchr(buf->str, '\n', buf->len))
493 term = "\n";
494 else if (memchr(buf->str, '\r', buf->len))
495 term = "\r";
4a35548b 496
41d214f6
BV
497 return term;
498}
4a35548b 499
41d214f6
BV
500static int initial_parse(const struct sr_input *in, GString *buf)
501{
502 struct context *inc;
41d214f6 503 GString *channel_name;
6433156c
DE
504 unsigned int num_columns, i;
505 size_t line_number, l;
41d214f6 506 int ret;
df0db9fd 507 char **lines, *line, **columns, *column;
41d214f6
BV
508
509 ret = SR_OK;
510 inc = in->priv;
511 columns = NULL;
512
513 line_number = 0;
492dfa90 514 lines = g_strsplit_set(buf->str, delim_set, 0);
41d214f6
BV
515 for (l = 0; lines[l]; l++) {
516 line_number++;
df0db9fd 517 line = lines[l];
41d214f6
BV
518 if (inc->start_line > line_number) {
519 sr_spew("Line %zu skipped.", line_number);
4a35548b
MS
520 continue;
521 }
df0db9fd 522 if (line[0] == '\0') {
41d214f6
BV
523 sr_spew("Blank line %zu skipped.", line_number);
524 continue;
525 }
df0db9fd
GS
526 strip_comment(line, inc->comment);
527 if (line[0] == '\0') {
41d214f6 528 sr_spew("Comment-only line %zu skipped.", line_number);
4a35548b
MS
529 continue;
530 }
531
41d214f6
BV
532 /* Reached first proper line. */
533 break;
534 }
535 if (!lines[l]) {
536 /* Not enough data for a proper line yet. */
60107497 537 ret = SR_ERR_NA;
41d214f6 538 goto out;
4a35548b
MS
539 }
540
541 /*
542 * In order to determine the number of columns parse the current line
543 * without limiting the number of columns.
544 */
df0db9fd
GS
545 columns = parse_line(line, inc, -1);
546 if (!columns) {
41d214f6
BV
547 sr_err("Error while parsing line %zu.", line_number);
548 ret = SR_ERR;
549 goto out;
4a35548b 550 }
4a35548b
MS
551 num_columns = g_strv_length(columns);
552
553 /* Ensure that the first column is not out of bounds. */
554 if (!num_columns) {
6433156c 555 sr_err("Column %u in line %zu is out of bounds.",
41d214f6
BV
556 inc->first_column, line_number);
557 ret = SR_ERR;
558 goto out;
4a35548b
MS
559 }
560
41d214f6 561 if (inc->multi_column_mode) {
4a35548b 562 /*
ba7dd8bb 563 * Detect the number of channels in multi column mode
4a35548b
MS
564 * automatically if not specified.
565 */
41d214f6
BV
566 if (!inc->num_channels) {
567 inc->num_channels = num_columns;
6433156c 568 sr_dbg("Number of auto-detected channels: %u.",
41d214f6 569 inc->num_channels);
4a35548b
MS
570 }
571
572 /*
ba7dd8bb 573 * Ensure that the number of channels does not exceed the number
4a35548b
MS
574 * of columns in multi column mode.
575 */
41d214f6 576 if (num_columns < inc->num_channels) {
ba7dd8bb 577 sr_err("Not enough columns for desired number of channels in line %zu.",
41d214f6
BV
578 line_number);
579 ret = SR_ERR;
580 goto out;
4a35548b
MS
581 }
582 }
583
41d214f6
BV
584 channel_name = g_string_sized_new(64);
585 for (i = 0; i < inc->num_channels; i++) {
df0db9fd
GS
586 column = columns[i];
587 if (inc->header && inc->multi_column_mode && column[0] != '\0')
588 g_string_assign(channel_name, column);
4a35548b 589 else
6433156c 590 g_string_printf(channel_name, "%u", i);
5e23fcab 591 sr_channel_new(in->sdi, i, SR_CHANNEL_LOGIC, TRUE, channel_name->str);
4a35548b 592 }
41d214f6 593 g_string_free(channel_name, TRUE);
4a35548b
MS
594
595 /*
596 * Calculate the minimum buffer size to store the sample data of the
ba7dd8bb 597 * channels.
4a35548b 598 */
41d214f6
BV
599 inc->sample_buffer_size = (inc->num_channels + 7) >> 3;
600 inc->sample_buffer = g_malloc(inc->sample_buffer_size);
4a35548b 601
41d214f6
BV
602out:
603 if (columns)
604 g_strfreev(columns);
605 g_strfreev(lines);
4a35548b 606
41d214f6 607 return ret;
4a35548b
MS
608}
609
4439363a
GS
610/*
611 * Gets called from initial_receive(), which runs until the end-of-line
612 * encoding of the input stream could get determined. Assumes that this
613 * routine receives enough buffered initial input data to either see the
614 * BOM when there is one, or that no BOM will follow when a text line
615 * termination sequence was seen. Silently drops the UTF-8 BOM sequence
616 * from the input buffer if one was seen. Does not care to protect
617 * against multiple execution or dropping the BOM multiple times --
618 * there should be at most one in the input stream.
619 */
620static void initial_bom_check(const struct sr_input *in)
621{
622 static const char *utf8_bom = "\xef\xbb\xbf";
623
624 if (in->buf->len < strlen(utf8_bom))
625 return;
626 if (strncmp(in->buf->str, utf8_bom, strlen(utf8_bom)) != 0)
627 return;
628 g_string_erase(in->buf, 0, strlen(utf8_bom));
629}
630
41d214f6 631static int initial_receive(const struct sr_input *in)
4a35548b 632{
41d214f6
BV
633 struct context *inc;
634 GString *new_buf;
635 int len, ret;
329733d9
UH
636 char *p;
637 const char *termination;
4a35548b 638
4439363a
GS
639 initial_bom_check(in);
640
41d214f6 641 inc = in->priv;
4a35548b 642
df0db9fd
GS
643 termination = get_line_termination(in->buf);
644 if (!termination)
41d214f6 645 /* Don't have a full line yet. */
d0181813 646 return SR_ERR_NA;
4a35548b 647
df0db9fd
GS
648 p = g_strrstr_len(in->buf->str, in->buf->len, termination);
649 if (!p)
41d214f6 650 /* Don't have a full line yet. */
d0181813 651 return SR_ERR_NA;
41d214f6
BV
652 len = p - in->buf->str - 1;
653 new_buf = g_string_new_len(in->buf->str, len);
654 g_string_append_c(new_buf, '\0');
4a35548b 655
41d214f6
BV
656 inc->termination = g_strdup(termination);
657
658 if (in->buf->str[0] != '\0')
659 ret = initial_parse(in, new_buf);
660 else
661 ret = SR_OK;
662
663 g_string_free(new_buf, TRUE);
664
665 return ret;
666}
667
7f4c3a62 668static int process_buffer(struct sr_input *in, gboolean is_eof)
41d214f6
BV
669{
670 struct sr_datafeed_packet packet;
671 struct sr_datafeed_meta meta;
672 struct sr_config *src;
673 struct context *inc;
674 gsize num_columns;
675 uint64_t samplerate;
676 int max_columns, ret, l;
df0db9fd 677 char *p, **lines, *line, **columns;
41d214f6 678
41d214f6 679 inc = in->priv;
d0181813 680 if (!inc->started) {
bee2b016 681 std_session_send_df_header(in->sdi);
41d214f6
BV
682
683 if (inc->samplerate) {
684 packet.type = SR_DF_META;
685 packet.payload = &meta;
686 samplerate = inc->samplerate;
687 src = sr_config_new(SR_CONF_SAMPLERATE, g_variant_new_uint64(samplerate));
688 meta.config = g_slist_append(NULL, src);
689 sr_session_send(in->sdi, &packet);
c01378c9 690 g_slist_free(meta.config);
41d214f6
BV
691 sr_config_free(src);
692 }
d0181813
BV
693
694 inc->started = TRUE;
4a35548b
MS
695 }
696
f9b74861
GS
697 /* Limit the number of columns to parse. */
698 if (inc->multi_column_mode)
699 max_columns = inc->num_channels;
700 else
701 max_columns = 1;
702
4555d3bd
GS
703 /*
704 * Consider empty input non-fatal. Keep accumulating input until
705 * at least one full text line has become available. Grab the
706 * maximum amount of accumulated data that consists of full text
707 * lines, and process what has been received so far, leaving not
708 * yet complete lines for the next invocation.
7f4c3a62
GS
709 *
710 * Enforce that all previously buffered data gets processed in
711 * the "EOF" condition. Do not insist in the presence of the
712 * termination sequence for the last line (may often be missing
713 * on Windows). A present termination sequence will just result
714 * in the "execution of an empty line", and does not harm.
4555d3bd
GS
715 */
716 if (!in->buf->len)
717 return SR_OK;
7f4c3a62
GS
718 if (is_eof) {
719 p = in->buf->str + in->buf->len;
720 } else {
721 p = g_strrstr_len(in->buf->str, in->buf->len, inc->termination);
722 if (!p)
723 return SR_ERR;
724 *p = '\0';
725 p += strlen(inc->termination);
726 }
41d214f6 727 g_strstrip(in->buf->str);
4a35548b 728
18078d05 729 ret = SR_OK;
492dfa90 730 lines = g_strsplit_set(in->buf->str, delim_set, 0);
41d214f6
BV
731 for (l = 0; lines[l]; l++) {
732 inc->line_number++;
df0db9fd
GS
733 line = lines[l];
734 if (line[0] == '\0') {
41d214f6 735 sr_spew("Blank line %zu skipped.", inc->line_number);
4a35548b
MS
736 continue;
737 }
738
739 /* Remove trailing comment. */
df0db9fd
GS
740 strip_comment(line, inc->comment);
741 if (line[0] == '\0') {
41d214f6 742 sr_spew("Comment-only line %zu skipped.", inc->line_number);
4a35548b
MS
743 continue;
744 }
745
160691b9
JS
746 /* Skip the header line, its content was used as the channel names. */
747 if (inc->header) {
748 sr_spew("Header line %zu skipped.", inc->line_number);
749 inc->header = FALSE;
750 continue;
751 }
752
df0db9fd
GS
753 columns = parse_line(line, inc, max_columns);
754 if (!columns) {
41d214f6 755 sr_err("Error while parsing line %zu.", inc->line_number);
4a35548b
MS
756 return SR_ERR;
757 }
4a35548b 758 num_columns = g_strv_length(columns);
4a35548b 759 if (!num_columns) {
6433156c 760 sr_err("Column %u in line %zu is out of bounds.",
41d214f6 761 inc->first_column, inc->line_number);
4a35548b 762 g_strfreev(columns);
4a35548b
MS
763 return SR_ERR;
764 }
4a35548b 765 /*
ba7dd8bb 766 * Ensure that the number of channels does not exceed the number
4a35548b
MS
767 * of columns in multi column mode.
768 */
41d214f6 769 if (inc->multi_column_mode && num_columns < inc->num_channels) {
ba7dd8bb 770 sr_err("Not enough columns for desired number of channels in line %zu.",
41d214f6 771 inc->line_number);
4a35548b 772 g_strfreev(columns);
4a35548b
MS
773 return SR_ERR;
774 }
775
41d214f6
BV
776 if (inc->multi_column_mode)
777 ret = parse_multi_columns(columns, inc);
4a35548b 778 else
41d214f6
BV
779 ret = parse_single_column(columns[0], inc);
780 if (ret != SR_OK) {
4a35548b 781 g_strfreev(columns);
4a35548b
MS
782 return SR_ERR;
783 }
784
4a35548b 785 /* Send sample data to the session bus. */
41d214f6
BV
786 ret = send_samples(in->sdi, inc->sample_buffer,
787 inc->sample_buffer_size, 1);
788 if (ret != SR_OK) {
4a35548b 789 sr_err("Sending samples failed.");
4a35548b
MS
790 return SR_ERR;
791 }
41d214f6
BV
792 g_strfreev(columns);
793 }
794 g_strfreev(lines);
241c386a 795 g_string_erase(in->buf, 0, p - in->buf->str);
41d214f6 796
7066fd46 797 return ret;
41d214f6
BV
798}
799
7066fd46 800static int receive(struct sr_input *in, GString *buf)
41d214f6
BV
801{
802 struct context *inc;
7066fd46
BV
803 int ret;
804
805 g_string_append_len(in->buf, buf->str, buf->len);
41d214f6
BV
806
807 inc = in->priv;
7066fd46 808 if (!inc->termination) {
df0db9fd
GS
809 ret = initial_receive(in);
810 if (ret == SR_ERR_NA)
7066fd46
BV
811 /* Not enough data yet. */
812 return SR_OK;
813 else if (ret != SR_OK)
814 return SR_ERR;
815
816 /* sdi is ready, notify frontend. */
817 in->sdi_ready = TRUE;
41d214f6 818 return SR_OK;
7066fd46
BV
819 }
820
7f4c3a62 821 ret = process_buffer(in, FALSE);
7066fd46
BV
822
823 return ret;
824}
825
826static int end(struct sr_input *in)
827{
828 struct context *inc;
7066fd46 829 int ret;
41d214f6 830
7066fd46 831 if (in->sdi_ready)
7f4c3a62 832 ret = process_buffer(in, TRUE);
7066fd46
BV
833 else
834 ret = SR_OK;
835
836 inc = in->priv;
3be42bc2 837 if (inc->started)
bee2b016 838 std_session_send_df_end(in->sdi);
4a35548b 839
7066fd46
BV
840 return ret;
841}
842
d5cc282f 843static void cleanup(struct sr_input *in)
7066fd46
BV
844{
845 struct context *inc;
846
847 inc = in->priv;
848
41d214f6
BV
849 if (inc->delimiter)
850 g_string_free(inc->delimiter, TRUE);
851
852 if (inc->comment)
853 g_string_free(inc->comment, TRUE);
4a35548b 854
b1f83103
UH
855 g_free(inc->termination);
856 g_free(inc->sample_buffer);
4a35548b
MS
857}
858
ad93bfb0
SA
859static int reset(struct sr_input *in)
860{
861 struct context *inc = in->priv;
862
863 cleanup(in);
864 inc->started = FALSE;
865 g_string_truncate(in->buf, 0);
866
867 return SR_OK;
868}
869
41d214f6
BV
870static struct sr_option options[] = {
871 { "single-column", "Single column", "Enable/specify single column", NULL, NULL },
872 { "numchannels", "Max channels", "Number of channels", NULL, NULL },
873 { "delimiter", "Delimiter", "Column delimiter", NULL, NULL },
874 { "format", "Format", "Numeric format", NULL, NULL },
875 { "comment", "Comment", "Comment prefix character", NULL, NULL },
876 { "samplerate", "Samplerate", "Samplerate used during capture", NULL, NULL },
877 { "first-channel", "First channel", "Column number of first channel", NULL, NULL },
878 { "header", "Header", "Treat first line as header with channel names", NULL, NULL },
879 { "startline", "Start line", "Line number at which to start processing samples", NULL, NULL },
06ad20be 880 ALL_ZERO
41d214f6
BV
881};
882
2c240774 883static const struct sr_option *get_options(void)
41d214f6
BV
884{
885 if (!options[0].def) {
886 options[0].def = g_variant_ref_sink(g_variant_new_int32(0));
887 options[1].def = g_variant_ref_sink(g_variant_new_int32(0));
888 options[2].def = g_variant_ref_sink(g_variant_new_string(","));
889 options[3].def = g_variant_ref_sink(g_variant_new_string("bin"));
890 options[4].def = g_variant_ref_sink(g_variant_new_string(";"));
6e8d95a5 891 options[5].def = g_variant_ref_sink(g_variant_new_uint64(0));
41d214f6
BV
892 options[6].def = g_variant_ref_sink(g_variant_new_int32(0));
893 options[7].def = g_variant_ref_sink(g_variant_new_boolean(FALSE));
894 options[8].def = g_variant_ref_sink(g_variant_new_int32(1));
895 }
896
897 return options;
898}
899
d4c93774 900SR_PRIV struct sr_input_module input_csv = {
4a35548b 901 .id = "csv",
41d214f6
BV
902 .name = "CSV",
903 .desc = "Comma-separated values",
c7bc82ff 904 .exts = (const char*[]){"csv", NULL},
41d214f6 905 .options = get_options,
4a35548b 906 .init = init,
41d214f6 907 .receive = receive,
7066fd46 908 .end = end,
41d214f6 909 .cleanup = cleanup,
ad93bfb0 910 .reset = reset,
4a35548b 911};