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