]> sigrok.org Git - libsigrok.git/blame - src/input/csv.c
fx2lafw: Code cleanup.
[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 {
77 /* Current selected samplerate. */
78 uint64_t samplerate;
79
ba7dd8bb
UH
80 /* Number of channels. */
81 gsize num_channels;
4a35548b
MS
82
83 /* Column delimiter character(s). */
84 GString *delimiter;
85
86 /* Comment prefix character(s). */
87 GString *comment;
88
89 /* Determines if sample data is stored in multiple columns. */
90 gboolean multi_column_mode;
91
92 /* Column number of the sample data in single column mode. */
93 gsize single_column;
94
95 /*
96 * Number of the first column to parse. Equivalent to the number of the
ba7dd8bb 97 * first channel in multi column mode and the single column number in
4a35548b
MS
98 * single column mode.
99 */
100 gsize first_column;
101
102 /*
ba7dd8bb
UH
103 * Column number of the first channel in multi column mode and position of
104 * the bit for the first channel in single column mode.
4a35548b 105 */
ba7dd8bb 106 gsize first_channel;
4a35548b
MS
107
108 /* Line number to start processing. */
109 gsize start_line;
110
111 /*
112 * Determines if the first line should be treated as header and used for
ba7dd8bb 113 * channel names in multi column mode.
4a35548b
MS
114 */
115 gboolean header;
116
117 /* Format sample data is stored in single column mode. */
118 int format;
119
120 /* Size of the sample buffer. */
121 gsize sample_buffer_size;
122
123 /* Buffer to store sample data. */
124 uint8_t *sample_buffer;
125
126 GIOChannel *channel;
127
128 /* Buffer for the current line. */
129 GString *buffer;
130
131 /* Current line number. */
132 gsize line_number;
133};
134
135static int format_match(const char *filename)
136{
b866fc09
BV
137 /* Require .csv extension. */
138 if (strcmp(filename + strlen(filename) - 4, ".csv"))
139 return FALSE;
140
4a35548b
MS
141 if (!filename) {
142 sr_err("%s: filename was NULL.", __func__);
143 return FALSE;
144 }
145
146 if (!g_file_test(filename, G_FILE_TEST_EXISTS)) {
147 sr_err("Input file '%s' does not exist.", filename);
148 return FALSE;
149 }
150
151 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
152 sr_err("Input file '%s' not a regular file.", filename);
153 return FALSE;
154 }
155
156 return TRUE;
157}
158
159static void free_context(struct context *ctx)
160{
161 if (!ctx)
162 return;
163
164 if (ctx->delimiter)
165 g_string_free(ctx->delimiter, TRUE);
166
167 if (ctx->comment)
168 g_string_free(ctx->comment, TRUE);
169
170 if (ctx->channel) {
171 g_io_channel_shutdown(ctx->channel, FALSE, NULL);
172 g_io_channel_unref(ctx->channel);
173 }
174
175 if (ctx->sample_buffer)
176 g_free(ctx->sample_buffer);
177
178 if (ctx->buffer)
179 g_string_free(ctx->buffer, TRUE);
180
181 g_free(ctx);
182}
183
184static void strip_comment(GString *string, const GString *prefix)
185{
186 char *ptr;
187
188 if (!prefix->len)
189 return;
190
191 if (!(ptr = strstr(string->str, prefix->str)))
192 return;
193
194 g_string_truncate(string, ptr - string->str);
195}
196
197static int parse_binstr(const char *str, struct context *ctx)
198{
199 gsize i, j, length;
200
201 length = strlen(str);
202
203 if (!length) {
204 sr_err("Column %zu in line %zu is empty.", ctx->single_column,
205 ctx->line_number);
206 return SR_ERR;
207 }
208
209 /* Clear buffer in order to set bits only. */
ba7dd8bb 210 memset(ctx->sample_buffer, 0, (ctx->num_channels + 7) >> 3);
4a35548b 211
ba7dd8bb 212 i = ctx->first_channel;
4a35548b 213
ba7dd8bb 214 for (j = 0; i < length && j < ctx->num_channels; i++, j++) {
4a35548b
MS
215 if (str[length - i - 1] == '1') {
216 ctx->sample_buffer[j / 8] |= (1 << (j % 8));
217 } else if (str[length - i - 1] != '0') {
218 sr_err("Invalid value '%s' in column %zu in line %zu.",
219 str, ctx->single_column, ctx->line_number);
220 return SR_ERR;
221 }
222 }
223
224 return SR_OK;
225}
226
227static int parse_hexstr(const char *str, struct context *ctx)
228{
229 gsize i, j, k, length;
230 uint8_t value;
231 char c;
232
233 length = strlen(str);
234
235 if (!length) {
236 sr_err("Column %zu in line %zu is empty.", ctx->single_column,
237 ctx->line_number);
238 return SR_ERR;
239 }
240
241 /* Clear buffer in order to set bits only. */
ba7dd8bb 242 memset(ctx->sample_buffer, 0, (ctx->num_channels + 7) >> 3);
4a35548b
MS
243
244 /* Calculate the position of the first hexadecimal digit. */
ba7dd8bb 245 i = ctx->first_channel / 4;
4a35548b 246
ba7dd8bb 247 for (j = 0; i < length && j < ctx->num_channels; i++) {
4a35548b
MS
248 c = str[length - i - 1];
249
250 if (!g_ascii_isxdigit(c)) {
251 sr_err("Invalid value '%s' in column %zu in line %zu.",
252 str, ctx->single_column, ctx->line_number);
253 return SR_ERR;
254 }
255
256 value = g_ascii_xdigit_value(c);
257
ba7dd8bb 258 k = (ctx->first_channel + j) % 4;
4a35548b 259
ba7dd8bb 260 for (; j < ctx->num_channels && k < 4; k++) {
4a35548b
MS
261 if (value & (1 << k))
262 ctx->sample_buffer[j / 8] |= (1 << (j % 8));
263
264 j++;
265 }
266 }
267
268 return SR_OK;
269}
270
271static int parse_octstr(const char *str, struct context *ctx)
272{
273 gsize i, j, k, length;
274 uint8_t value;
275 char c;
276
277 length = strlen(str);
278
279 if (!length) {
280 sr_err("Column %zu in line %zu is empty.", ctx->single_column,
281 ctx->line_number);
282 return SR_ERR;
283 }
284
285 /* Clear buffer in order to set bits only. */
ba7dd8bb 286 memset(ctx->sample_buffer, 0, (ctx->num_channels + 7) >> 3);
4a35548b
MS
287
288 /* Calculate the position of the first octal digit. */
ba7dd8bb 289 i = ctx->first_channel / 3;
4a35548b 290
ba7dd8bb 291 for (j = 0; i < length && j < ctx->num_channels; i++) {
4a35548b
MS
292 c = str[length - i - 1];
293
294 if (c < '0' || c > '7') {
295 sr_err("Invalid value '%s' in column %zu in line %zu.",
296 str, ctx->single_column, ctx->line_number);
297 return SR_ERR;
298 }
299
300 value = g_ascii_xdigit_value(c);
301
ba7dd8bb 302 k = (ctx->first_channel + j) % 3;
4a35548b 303
ba7dd8bb 304 for (; j < ctx->num_channels && k < 3; k++) {
4a35548b
MS
305 if (value & (1 << k))
306 ctx->sample_buffer[j / 8] |= (1 << (j % 8));
307
308 j++;
309 }
310 }
311
312 return SR_OK;
313}
314
315static char **parse_line(const struct context *ctx, int max_columns)
316{
317 const char *str, *remainder;
318 GSList *list, *l;
319 char **columns;
320 char *column;
321 gsize n, k;
322
323 n = 0;
324 k = 0;
325 list = NULL;
326
327 remainder = ctx->buffer->str;
328 str = strstr(remainder, ctx->delimiter->str);
329
330 while (str && max_columns) {
331 if (n >= ctx->first_column) {
332 column = g_strndup(remainder, str - remainder);
333 list = g_slist_prepend(list, g_strstrip(column));
334
335 max_columns--;
336 k++;
337 }
338
339 remainder = str + ctx->delimiter->len;
340 str = strstr(remainder, ctx->delimiter->str);
341 n++;
342 }
343
344 if (ctx->buffer->len && max_columns && n >= ctx->first_column) {
345 column = g_strdup(remainder);
346 list = g_slist_prepend(list, g_strstrip(column));
347 k++;
348 }
349
350 if (!(columns = g_try_new(char *, k + 1)))
351 return NULL;
352
353 columns[k--] = NULL;
354
355 for (l = list; l; l = l->next)
356 columns[k--] = l->data;
357
358 g_slist_free(list);
359
360 return columns;
361}
362
363static int parse_multi_columns(char **columns, struct context *ctx)
364{
365 gsize i;
366
367 /* Clear buffer in order to set bits only. */
ba7dd8bb 368 memset(ctx->sample_buffer, 0, (ctx->num_channels + 7) >> 3);
4a35548b 369
ba7dd8bb 370 for (i = 0; i < ctx->num_channels; i++) {
4a35548b
MS
371 if (columns[i][0] == '1') {
372 ctx->sample_buffer[i / 8] |= (1 << (i % 8));
373 } else if (!strlen(columns[i])) {
374 sr_err("Column %zu in line %zu is empty.",
ba7dd8bb 375 ctx->first_channel + i, ctx->line_number);
4a35548b
MS
376 return SR_ERR;
377 } else if (columns[i][0] != '0') {
378 sr_err("Invalid value '%s' in column %zu in line %zu.",
ba7dd8bb 379 columns[i], ctx->first_channel + i,
4a35548b
MS
380 ctx->line_number);
381 return SR_ERR;
382 }
383 }
384
385 return SR_OK;
386}
387
388static int parse_single_column(const char *column, struct context *ctx)
389{
390 int res;
391
392 res = SR_ERR;
393
394 switch(ctx->format) {
395 case FORMAT_BIN:
396 res = parse_binstr(column, ctx);
397 break;
398 case FORMAT_HEX:
399 res = parse_hexstr(column, ctx);
400 break;
401 case FORMAT_OCT:
402 res = parse_octstr(column, ctx);
403 break;
404 }
405
406 return res;
407}
408
409static int send_samples(const struct sr_dev_inst *sdi, uint8_t *buffer,
410 gsize buffer_size, gsize count)
411{
412 int res;
413 struct sr_datafeed_packet packet;
414 struct sr_datafeed_logic logic;
415 gsize i;
416
417 packet.type = SR_DF_LOGIC;
418 packet.payload = &logic;
419 logic.unitsize = buffer_size;
420 logic.length = buffer_size;
421 logic.data = buffer;
422
423 for (i = 0; i < count; i++) {
424 if ((res = sr_session_send(sdi, &packet)) != SR_OK)
425 return res;
426 }
427
428 return SR_OK;
429}
430
431static int init(struct sr_input *in, const char *filename)
432{
433 int res;
434 struct context *ctx;
435 const char *param;
436 GIOStatus status;
437 gsize i, term_pos;
3f239f08 438 char channel_name[SR_MAX_CHANNELNAME_LEN + 1];
ba7dd8bb 439 struct sr_channel *ch;
4a35548b
MS
440 char **columns;
441 gsize num_columns;
442 char *ptr;
443
444 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
445 sr_err("Context malloc failed.");
446 return SR_ERR_MALLOC;
447 }
448
449 /* Create a virtual device. */
450 in->sdi = sr_dev_inst_new(0, SR_ST_ACTIVE, NULL, NULL, NULL);
451 in->internal = ctx;
452
453 /* Set default samplerate. */
454 ctx->samplerate = 0;
455
456 /*
ba7dd8bb
UH
457 * Enable auto-detection of the number of channels in multi column mode
458 * and enforce the specification of the number of channels in single
4a35548b
MS
459 * column mode.
460 */
ba7dd8bb 461 ctx->num_channels = 0;
4a35548b
MS
462
463 /* Set default delimiter. */
464 if (!(ctx->delimiter = g_string_new(","))) {
465 sr_err("Delimiter malloc failed.");
466 free_context(ctx);
467 return SR_ERR_MALLOC;
468 }
469
470 /*
471 * Set default comment prefix. Note that an empty comment prefix
472 * disables removing of comments.
473 */
474 if (!(ctx->comment = g_string_new(""))) {
475 sr_err("Comment malloc failed.");
476 free_context(ctx);
477 return SR_ERR_MALLOC;
478 }
479
480 /* Enable multi column mode by default. */
481 ctx->multi_column_mode = TRUE;
482
483 /* Use first column as default single column number. */
484 ctx->single_column = 0;
485
486 /*
487 * In multi column mode start parsing sample data at the first column
488 * and in single column mode at the first bit.
489 */
ba7dd8bb 490 ctx->first_channel = 0;
4a35548b
MS
491
492 /* Start at the beginning of the file. */
493 ctx->start_line = 1;
494
495 /* Disable the usage of the first line as header by default. */
496 ctx->header = FALSE;
497
498 /* Set default format for single column mode. */
499 ctx->format = FORMAT_BIN;
500
501 if (!(ctx->buffer = g_string_new(""))) {
502 sr_err("Line buffer malloc failed.");
503 free_context(ctx);
504 return SR_ERR_MALLOC;
505 }
506
507 if (in->param) {
508 if ((param = g_hash_table_lookup(in->param, "samplerate"))) {
509 res = sr_parse_sizestring(param, &ctx->samplerate);
510
511 if (res != SR_OK) {
512 sr_err("Invalid samplerate: %s.", param);
513 free_context(ctx);
514 return SR_ERR_ARG;
515 }
516 }
517
ba7dd8bb
UH
518 if ((param = g_hash_table_lookup(in->param, "numchannels")))
519 ctx->num_channels = g_ascii_strtoull(param, NULL, 10);
4a35548b
MS
520
521 if ((param = g_hash_table_lookup(in->param, "delimiter"))) {
522 if (!strlen(param)) {
523 sr_err("Delimiter must be at least one character.");
524 free_context(ctx);
525 return SR_ERR_ARG;
526 }
527
528 if (!g_ascii_strcasecmp(param, "\\t"))
529 g_string_assign(ctx->delimiter, "\t");
530 else
531 g_string_assign(ctx->delimiter, param);
532 }
533
534 if ((param = g_hash_table_lookup(in->param, "comment")))
535 g_string_assign(ctx->comment, param);
536
537 if ((param = g_hash_table_lookup(in->param, "single-column"))) {
538 ctx->single_column = g_ascii_strtoull(param, &ptr, 10);
539 ctx->multi_column_mode = FALSE;
540
541 if (param == ptr) {
542 sr_err("Invalid single-colum number: %s.",
543 param);
544 free_context(ctx);
545 return SR_ERR_ARG;
546 }
547 }
548
ba7dd8bb
UH
549 if ((param = g_hash_table_lookup(in->param, "first-channel")))
550 ctx->first_channel = g_ascii_strtoull(param, NULL, 10);
4a35548b
MS
551
552 if ((param = g_hash_table_lookup(in->param, "startline"))) {
553 ctx->start_line = g_ascii_strtoull(param, NULL, 10);
554
555 if (ctx->start_line < 1) {
556 sr_err("Invalid start line: %s.", param);
557 free_context(ctx);
558 return SR_ERR_ARG;
559 }
560 }
561
562 if ((param = g_hash_table_lookup(in->param, "header")))
563 ctx->header = sr_parse_boolstring(param);
564
565 if ((param = g_hash_table_lookup(in->param, "format"))) {
566 if (!g_ascii_strncasecmp(param, "bin", 3)) {
567 ctx->format = FORMAT_BIN;
568 } else if (!g_ascii_strncasecmp(param, "hex", 3)) {
569 ctx->format = FORMAT_HEX;
570 } else if (!g_ascii_strncasecmp(param, "oct", 3)) {
571 ctx->format = FORMAT_OCT;
572 } else {
573 sr_err("Invalid format: %s.", param);
574 free_context(ctx);
575 return SR_ERR;
576 }
577 }
578 }
579
580 if (ctx->multi_column_mode)
ba7dd8bb 581 ctx->first_column = ctx->first_channel;
4a35548b
MS
582 else
583 ctx->first_column = ctx->single_column;
584
ba7dd8bb
UH
585 if (!ctx->multi_column_mode && !ctx->num_channels) {
586 sr_err("Number of channels needs to be specified in single column mode.");
4a35548b
MS
587 free_context(ctx);
588 return SR_ERR;
589 }
590
591 if (!(ctx->channel = g_io_channel_new_file(filename, "r", NULL))) {
592 sr_err("Input file '%s' could not be opened.", filename);
593 free_context(ctx);
594 return SR_ERR;
595 }
596
597 while (TRUE) {
598 ctx->line_number++;
599 status = g_io_channel_read_line_string(ctx->channel,
600 ctx->buffer, &term_pos, NULL);
601
602 if (status == G_IO_STATUS_EOF) {
603 sr_err("Input file is empty.");
604 free_context(ctx);
605 return SR_ERR;
606 }
607
608 if (status != G_IO_STATUS_NORMAL) {
609 sr_err("Error while reading line %zu.",
610 ctx->line_number);
611 free_context(ctx);
612 return SR_ERR;
613 }
614
615 if (ctx->start_line > ctx->line_number) {
616 sr_spew("Line %zu skipped.", ctx->line_number);
617 continue;
618 }
619
620 /* Remove line termination character(s). */
621 g_string_truncate(ctx->buffer, term_pos);
622
623 if (!ctx->buffer->len) {
624 sr_spew("Blank line %zu skipped.", ctx->line_number);
625 continue;
626 }
627
628 /* Remove trailing comment. */
629 strip_comment(ctx->buffer, ctx->comment);
630
631 if (ctx->buffer->len)
632 break;
633
634 sr_spew("Comment-only line %zu skipped.", ctx->line_number);
635 }
636
637 /*
638 * In order to determine the number of columns parse the current line
639 * without limiting the number of columns.
640 */
641 if (!(columns = parse_line(ctx, -1))) {
642 sr_err("Error while parsing line %zu.", ctx->line_number);
643 free_context(ctx);
644 return SR_ERR;
645 }
646
647 num_columns = g_strv_length(columns);
648
649 /* Ensure that the first column is not out of bounds. */
650 if (!num_columns) {
651 sr_err("Column %zu in line %zu is out of bounds.",
652 ctx->first_column, ctx->line_number);
653 g_strfreev(columns);
654 free_context(ctx);
655 return SR_ERR;
656 }
657
658 if (ctx->multi_column_mode) {
659 /*
ba7dd8bb 660 * Detect the number of channels in multi column mode
4a35548b
MS
661 * automatically if not specified.
662 */
ba7dd8bb
UH
663 if (!ctx->num_channels) {
664 ctx->num_channels = num_columns;
665 sr_info("Number of auto-detected channels: %zu.",
666 ctx->num_channels);
4a35548b
MS
667 }
668
669 /*
ba7dd8bb 670 * Ensure that the number of channels does not exceed the number
4a35548b
MS
671 * of columns in multi column mode.
672 */
ba7dd8bb
UH
673 if (num_columns < ctx->num_channels) {
674 sr_err("Not enough columns for desired number of channels in line %zu.",
4a35548b
MS
675 ctx->line_number);
676 g_strfreev(columns);
677 free_context(ctx);
678 return SR_ERR;
679 }
680 }
681
ba7dd8bb 682 for (i = 0; i < ctx->num_channels; i++) {
4a35548b 683 if (ctx->header && ctx->multi_column_mode && strlen(columns[i]))
ba7dd8bb 684 snprintf(channel_name, sizeof(channel_name), "%s",
4a35548b
MS
685 columns[i]);
686 else
ba7dd8bb 687 snprintf(channel_name, sizeof(channel_name), "%zu", i);
4a35548b 688
3f239f08 689 ch = sr_channel_new(i, SR_CHANNEL_LOGIC, TRUE, channel_name);
4a35548b 690
ba7dd8bb
UH
691 if (!ch) {
692 sr_err("Channel creation failed.");
4a35548b
MS
693 free_context(ctx);
694 g_strfreev(columns);
695 return SR_ERR;
696 }
697
ba7dd8bb 698 in->sdi->channels = g_slist_append(in->sdi->channels, ch);
4a35548b
MS
699 }
700
701 g_strfreev(columns);
702
703 /*
704 * Calculate the minimum buffer size to store the sample data of the
ba7dd8bb 705 * channels.
4a35548b 706 */
ba7dd8bb 707 ctx->sample_buffer_size = (ctx->num_channels + 7) >> 3;
4a35548b
MS
708
709 if (!(ctx->sample_buffer = g_try_malloc(ctx->sample_buffer_size))) {
710 sr_err("Sample buffer malloc failed.");
711 free_context(ctx);
712 return SR_ERR_MALLOC;
713 }
714
715 return SR_OK;
716}
717
718static int loadfile(struct sr_input *in, const char *filename)
719{
720 int res;
721 struct context *ctx;
722 struct sr_datafeed_packet packet;
723 struct sr_datafeed_meta meta;
724 struct sr_config *cfg;
725 GIOStatus status;
726 gboolean read_new_line;
727 gsize term_pos;
728 char **columns;
729 gsize num_columns;
730 int max_columns;
731
732 (void)filename;
733
734 ctx = in->internal;
735
736 /* Send header packet to the session bus. */
737 std_session_send_df_header(in->sdi, LOG_PREFIX);
738
739 if (ctx->samplerate) {
740 packet.type = SR_DF_META;
741 packet.payload = &meta;
742 cfg = sr_config_new(SR_CONF_SAMPLERATE,
743 g_variant_new_uint64(ctx->samplerate));
744 meta.config = g_slist_append(NULL, cfg);
745 sr_session_send(in->sdi, &packet);
746 sr_config_free(cfg);
747 }
748
749 read_new_line = FALSE;
750
751 /* Limit the number of columns to parse. */
752 if (ctx->multi_column_mode)
ba7dd8bb 753 max_columns = ctx->num_channels;
4a35548b
MS
754 else
755 max_columns = 1;
756
757 while (TRUE) {
758 /*
759 * Skip reading a new line for the first time if the last read
760 * line was not a header because the sample data is not parsed
761 * yet.
762 */
763 if (read_new_line || ctx->header) {
764 ctx->line_number++;
765 status = g_io_channel_read_line_string(ctx->channel,
766 ctx->buffer, &term_pos, NULL);
767
768 if (status == G_IO_STATUS_EOF)
769 break;
770
771 if (status != G_IO_STATUS_NORMAL) {
772 sr_err("Error while reading line %zu.",
773 ctx->line_number);
774 free_context(ctx);
775 return SR_ERR;
776 }
777
778 /* Remove line termination character(s). */
779 g_string_truncate(ctx->buffer, term_pos);
780 }
781
782 read_new_line = TRUE;
783
784 if (!ctx->buffer->len) {
785 sr_spew("Blank line %zu skipped.", ctx->line_number);
786 continue;
787 }
788
789 /* Remove trailing comment. */
790 strip_comment(ctx->buffer, ctx->comment);
791
792 if (!ctx->buffer->len) {
793 sr_spew("Comment-only line %zu skipped.",
794 ctx->line_number);
795 continue;
796 }
797
798 if (!(columns = parse_line(ctx, max_columns))) {
799 sr_err("Error while parsing line %zu.",
800 ctx->line_number);
801 free_context(ctx);
802 return SR_ERR;
803 }
804
805 num_columns = g_strv_length(columns);
806
807 /* Ensure that the first column is not out of bounds. */
808 if (!num_columns) {
809 sr_err("Column %zu in line %zu is out of bounds.",
810 ctx->first_column, ctx->line_number);
811 g_strfreev(columns);
812 free_context(ctx);
813 return SR_ERR;
814 }
815
816 /*
ba7dd8bb 817 * Ensure that the number of channels does not exceed the number
4a35548b
MS
818 * of columns in multi column mode.
819 */
ba7dd8bb
UH
820 if (ctx->multi_column_mode && num_columns < ctx->num_channels) {
821 sr_err("Not enough columns for desired number of channels in line %zu.",
4a35548b
MS
822 ctx->line_number);
823 g_strfreev(columns);
824 free_context(ctx);
825 return SR_ERR;
826 }
827
828 if (ctx->multi_column_mode)
829 res = parse_multi_columns(columns, ctx);
830 else
831 res = parse_single_column(columns[0], ctx);
832
833 if (res != SR_OK) {
834 g_strfreev(columns);
835 free_context(ctx);
836 return SR_ERR;
837 }
838
839 g_strfreev(columns);
840
841 /*
842 * TODO: Parse sample numbers / timestamps and use it for
843 * decompression.
844 */
845
846 /* Send sample data to the session bus. */
847 res = send_samples(in->sdi, ctx->sample_buffer,
848 ctx->sample_buffer_size, 1);
849
850 if (res != SR_OK) {
851 sr_err("Sending samples failed.");
852 free_context(ctx);
853 return SR_ERR;
854 }
855 }
856
857 /* Send end packet to the session bus. */
858 packet.type = SR_DF_END;
859 sr_session_send(in->sdi, &packet);
860
861 free_context(ctx);
862
863 return SR_OK;
864}
865
866SR_PRIV struct sr_input_format input_csv = {
867 .id = "csv",
868 .description = "Comma-separated values (CSV)",
869 .format_match = format_match,
870 .init = init,
871 .loadfile = loadfile,
872};