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