X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=src%2Finput%2Fcsv.c;h=4a415181fc50f1c8c7cfaa879b19ea5a4ce89c46;hb=72903e9d5546c5b2a012007040920a34acac921e;hp=0833f3182dad07658380c0a9923ecae667e198c5;hpb=e53f32d2b8163838c404c4fade01fb02648e7505;p=libsigrok.git diff --git a/src/input/csv.c b/src/input/csv.c index 0833f318..4a415181 100644 --- a/src/input/csv.c +++ b/src/input/csv.c @@ -26,6 +26,7 @@ #include #include "libsigrok-internal.h" +#include "scpi.h" /* String un-quote for channel name from header line. */ #define LOG_PREFIX "input/csv" @@ -34,42 +35,80 @@ /* * The CSV input module has the following options: * - * single-column: Specifies the column number which stores the sample data for - * single column mode and enables single column mode. Multi - * column mode is used if this parameter is omitted. + * column_formats: Specifies the data formats and channel counts for the + * input file's text columns. Accepts a comma separated list of tuples + * with: an optional column repeat count ('*' as a wildcard meaning + * "all remaining columns", only applicable to the last field), a format + * specifying character ('x' hexadecimal, 'o' octal, 'b' binary, 'l' + * single-bit logic), and an optional bit count (translating to: logic + * channels communicated in that column). This "column_formats" option + * is most versatile, other forms of specifying the column layout only + * exist for backwards compatibility. * - * numchannels: Specifies the number of channels to use. In multi column mode - * the number of channels are the number of columns and in single - * column mode the number of bits (LSB first) beginning at - * 'first-channel'. + * single_column: Specifies the column number which contains the logic data + * for single-column mode. All logic data is taken from several bits + * which all are kept within that one column. Only exists for backwards + * compatibility, see "column_formats" for more flexibility. * - * delimiter: Specifies the delimiter for columns. Must be at least one - * character. Comma is used as default delimiter. + * first_column: Specifies the number of the first column with logic data + * in simple multi-column mode. Only exists for backwards compatibility, + * see "column_formats" for more flexibility. * - * format: Specifies the format of the sample data in single column mode. - * Available formats are: 'bin', 'hex' and 'oct'. The binary - * format is used by default. This option has no effect in multi - * column mode. + * logic_channels: Specifies the number of logic channels. Is required in + * simple single-column mode. Is optional in simple multi-column mode + * (and defaults to all remaining columns). Only exists for backwards + * compatibility, see "column_formats" for more flexibility. * - * comment: Specifies the prefix character(s) for comments. No prefix - * characters are used by default which disables removing of - * comments. + * single_format: Specifies the format of the input text in simple single- + * column mode. Available formats are: 'bin' (default), 'hex' and 'oct'. + * Simple multi-column mode always uses single-bit data per column. + * Only exists for backwards compatibility, see "column_formats" for + * more flexibility. * - * samplerate: Samplerate which the sample data was captured with. Default - * value is 0. + * start_line: Specifies at which line to start processing the input file. + * Allows to skip leading lines which neither are header nor data lines. + * By default all of the input file gets processed. * - * first-channel: Column number of the first channel in multi column mode and - * position of the bit for the first channel in single column mode. - * Default value is 0. + * header: Boolean option, controls whether the first processed line is used + * to determine channel names. Off by default. Generic channel names are + * used in the absence of header line content. * - * header: Determines if the first line should be treated as header - * and used for channel names in multi column mode. Empty header - * names will be replaced by the channel number. If enabled in - * single column mode the first line will be skipped. Usage of - * header is disabled by default. + * samplerate: Specifies the samplerate of the input data. Defaults to 0. + * User specs take precedence over data which optionally gets derived + * from input data. * - * startline: Line number to start processing sample data. Must be greater - * than 0. The default line number to start processing is 1. + * column_separator: Specifies the sequence which separates the text file + * columns. Cannot be empty. Defaults to comma. + * + * comment_leader: Specifies the sequence which starts comments that run + * up to the end of the current text line. Can be empty to disable + * comment support. Defaults to semicolon. + * + * Typical examples of using these options: + * - ... -I csv:column_formats=*l ... + * All columns are single-bit logic data. Identical to the previous + * multi-column mode (the default when no options were given at all). + * - ... -I csv:column_formats=3-,*l ... + * Ignore the first three columns, get single-bit logic data from all + * remaining lines (multi-column mode with first-column above 1). + * - ... -I csv:column_formats=3-,4l,x8 ... + * Ignore the first three columns, get single-bit logic data from the + * next four columns, then eight-bit data in hex format from the next + * column. More columns may follow in the input text but won't get + * processed. (Mix of previous multi-column as well as single-column + * modes.) + * - ... -I csv:column_formats=4x8,b16,5l ... + * Get eight-bit data in hex format from the first four columns, then + * sixteen-bit data in binary format, then five times single-bit data. + * - ... -I csv:single_column=2:single_format=bin:logic_channels=8 ... + * Get eight logic bits in binary format from column 2. (Simple + * single-column mode, corresponds to the "-,b8" format.) + * - ... -I csv:first_column=6:logic_channels=4 ... + * Get four single-bit logic channels from columns 6 to 9 respectively. + * (Simple multi-column mode, corresponds to the "5-,4b" format.) + * - ... -I csv:start_line=20:header=yes:... + * Skip the first 19 text lines. Use line 20 to derive channel names. + * Data starts at line 21. */ /* @@ -120,6 +159,13 @@ static const char *col_format_text[] = { [FORMAT_OCT] = "octal", }; +static const char col_format_char[] = { + [FORMAT_NONE] = '?', + [FORMAT_BIN] = 'b', + [FORMAT_HEX] = 'x', + [FORMAT_OCT] = 'o', +}; + struct column_details { size_t col_nr; enum single_col_format text_format; @@ -134,31 +180,20 @@ struct context { uint64_t samplerate; gboolean samplerate_sent; - /* Number of channels. */ - size_t num_channels; + /* Number of logic channels. */ + size_t logic_channels; - /* Column delimiter character(s). */ + /* Column delimiter (actually separator), comment leader, EOL sequence. */ GString *delimiter; - - /* Comment prefix character(s). */ GString *comment; - - /* Termination character(s) used in current stream. */ char *termination; - /* Determines if sample data is stored in multiple columns. */ - gboolean multi_column_mode; - - /* Parameters how to process the columns. */ + /* Format specs for input columns, and processing state. */ + size_t column_seen_count; + const char *column_formats; size_t column_want_count; struct column_details *column_details; - /* Column number of the sample data in single column mode. */ - size_t single_column; - - /* Column number of the first channel. */ - size_t first_column; - /* Line number to start processing. */ size_t start_line; @@ -169,9 +204,6 @@ struct context { gboolean use_header; gboolean header_seen; - /* Format sample data is stored in single column mode. */ - enum single_col_format format; - size_t sample_unit_size; /**!< Byte count for a single sample. */ uint8_t *sample_buffer; /**!< Buffer for a single sample. */ @@ -210,7 +242,7 @@ static void set_logic_level(struct context *inc, size_t ch_idx, int on) size_t byte_idx, bit_idx; uint8_t bit_mask; - if (ch_idx >= inc->num_channels) + if (ch_idx >= inc->logic_channels) return; if (!on) return; @@ -269,6 +301,8 @@ static int queue_logic_samples(const struct sr_input *in) int rc; inc = in->priv; + if (!inc->logic_channels) + return SR_OK; inc->datafeed_buf_fill += inc->sample_unit_size; if (inc->datafeed_buf_fill == inc->datafeed_buf_size) { @@ -279,64 +313,155 @@ static int queue_logic_samples(const struct sr_input *in) return SR_OK; } -static int make_column_details_single(struct context *inc, - size_t col_nr, size_t bit_count, enum single_col_format format) +/* Helpers for "column processing". */ + +static int split_column_format(const char *spec, + size_t *column_count, enum single_col_format *format, size_t *bit_count) { - struct column_details *details; + size_t count; + char *endp, format_char; + enum single_col_format format_code; - /* - * Need at least as many columns to also include the one with - * the single-column input data. - */ - inc->column_want_count = col_nr; + if (!spec || !*spec) + return SR_ERR_ARG; - /* - * Allocate the columns' processing details. Columns are counted - * from 1 (user's perspective), array items from 0 (programmer's - * perspective). - */ - inc->column_details = g_malloc0_n(col_nr, sizeof(inc->column_details[0])); - details = &inc->column_details[col_nr - 1]; - details->col_nr = col_nr; + /* Get the (optional, decimal, default 1) column count. Accept '*'. */ + endp = NULL; + if (*spec == '*') { + count = 0; + endp = (char *)&spec[1]; + } else { + count = strtoul(spec, &endp, 10); + } + if (!endp) + return SR_ERR_ARG; + if (endp == spec) + count = 1; + if (column_count) + *column_count = count; + spec = endp; + + /* Get the (mandatory, single letter) type spec (-/xob/l). */ + format_char = *spec++; + switch (format_char) { + case '-': /* Might conflict with number-parsing. */ + case '/': + format_char = '-'; + format_code = FORMAT_NONE; + break; + case 'x': + format_code = FORMAT_HEX; + break; + case 'o': + format_code = FORMAT_OCT; + break; + case 'b': + case 'l': + format_code = FORMAT_BIN; + break; + default: /* includes NUL */ + return SR_ERR_ARG; + } + if (format) + *format = format_code; - /* - * In single-column mode this single column will hold all bits - * of all logic channels, in the user specified number format. - */ - details->text_format = format; - details->channel_offset = 0; - details->channel_count = bit_count; + /* Get the (optional, decimal, default 1) bit count. */ + endp = NULL; + count = strtoul(spec, &endp, 10); + if (!endp) + return SR_ERR_ARG; + if (endp == spec) + count = 1; + if (format_char == '-') + count = 0; + if (format_char == 'l') + count = 1; + if (bit_count) + *bit_count = count; + spec = endp; + + /* Input spec must have been exhausted. */ + if (*spec) + return SR_ERR_ARG; return SR_OK; } -static int make_column_details_multi(struct context *inc, - size_t first_col, size_t last_col) +static int make_column_details_from_format(struct context *inc, + const char *column_format) { - struct column_details *details; - size_t col_nr; - - /* - * Need at least as many columns to also include the one with - * the last channel's data. - */ - inc->column_want_count = last_col; + char **formats, *format; + size_t format_count, column_count, bit_count; + size_t auto_column_count; + size_t format_idx, c, b, column_idx, channel_idx; + enum single_col_format f; + struct column_details *detail; + int ret; - /* - * Allocate the columns' processing details. Columns are counted - * from 1, array items from 0. - * In multi-column mode each column will hold a single bit for - * the respective channel. - */ - inc->column_details = g_malloc0_n(last_col, sizeof(inc->column_details[0])); - for (col_nr = first_col; col_nr <= last_col; col_nr++) { - details = &inc->column_details[col_nr - 1]; - details->col_nr = col_nr; - details->text_format = FORMAT_BIN; - details->channel_offset = col_nr - first_col; - details->channel_count = 1; + /* Split the input spec, count involved columns and bits. */ + formats = g_strsplit(column_format, ",", 0); + if (!formats) { + sr_err("Cannot parse columns format %s (comma split).", column_format); + return SR_ERR_ARG; } - + format_count = g_strv_length(formats); + if (!format_count) { + sr_err("Cannot parse columns format %s (field count).", column_format); + g_strfreev(formats); + return SR_ERR_ARG; + } + column_count = bit_count = 0; + auto_column_count = 0; + for (format_idx = 0; format_idx < format_count; format_idx++) { + format = formats[format_idx]; + ret = split_column_format(format, &c, &f, &b); + sr_dbg("fmt %s -> %zu cols, %s fmt, %zu bits, rc %d", format, c, col_format_text[f], b, ret); + if (ret != SR_OK) { + sr_err("Cannot parse columns format %s (field split, %s).", column_format, format); + g_strfreev(formats); + return SR_ERR_ARG; + } + if (f && !c) { + /* User requested "auto-count", must be last format. */ + if (formats[format_idx + 1]) { + sr_err("Auto column count must be last format field."); + g_strfreev(formats); + return SR_ERR_ARG; + } + auto_column_count = inc->column_seen_count - column_count; + c = auto_column_count; + } + column_count += c; + bit_count += c * b; + } + sr_dbg("Column format %s -> %zu columns, %zu logic channels.", + column_format, column_count, bit_count); + + /* Allocate and fill in "column processing" details. */ + inc->column_want_count = column_count; + inc->column_details = g_malloc0_n(column_count, sizeof(inc->column_details[0])); + column_idx = channel_idx = 0; + for (format_idx = 0; format_idx < format_count; format_idx++) { + format = formats[format_idx]; + (void)split_column_format(format, &c, &f, &b); + if (f && !c) + c = auto_column_count; + while (c-- > 0) { + detail = &inc->column_details[column_idx++]; + detail->col_nr = column_idx; + detail->text_format = f; + if (detail->text_format) { + detail->channel_offset = channel_idx; + detail->channel_count = b; + channel_idx += b; + } + sr_dbg("detail -> col %zu, fmt %s, ch off/cnt %zu/%zu", + detail->col_nr, col_format_text[detail->text_format], + detail->channel_offset, detail->channel_count); + } + } + inc->logic_channels = channel_idx; + g_strfreev(formats); return SR_OK; } @@ -389,7 +514,7 @@ static char **split_line(char *buf, struct context *inc) * * @param[in] column The input text, a run of bin/hex/oct digits. * @param[in] inc The input module's context. - * @param[in] col_nr The involved column number (1-based). + * @param[in] details The column processing details. * * @retval SR_OK Success. * @retval SR_ERR Invalid input data (empty, or format error). @@ -397,9 +522,9 @@ static char **split_line(char *buf, struct context *inc) * This routine modifies the logic levels in the current sample set, * based on the text input and a user provided format spec. */ -static int parse_column(const char *column, struct context *inc, size_t col_nr) +static int parse_logic(const char *column, struct context *inc, + const struct column_details *details) { - const struct column_details *col_det; size_t length, ch_rem, ch_idx, ch_inc; const char *rdptr; char c; @@ -407,16 +532,6 @@ static int parse_column(const char *column, struct context *inc, size_t col_nr) const char *type_text; uint8_t bits; - /* See whether and how the columns needs to get processed. */ - col_det = lookup_column_details(inc, col_nr); - if (!col_det) { - sr_dbg("Column %zu in line %zu without processing details?", - col_nr, inc->line_number); - return SR_ERR; - } - if (col_det->text_format == FORMAT_NONE) - return SR_OK; - /* * Prepare to read the digits from the text end towards the start. * A digit corresponds to a variable number of channels (depending @@ -425,13 +540,13 @@ static int parse_column(const char *column, struct context *inc, size_t col_nr) */ length = strlen(column); if (!length) { - sr_err("Column %zu in line %zu is empty.", col_nr, + sr_err("Column %zu in line %zu is empty.", details->col_nr, inc->line_number); return SR_ERR; } rdptr = &column[length]; - ch_idx = col_det->channel_offset; - ch_rem = col_det->channel_count; + ch_idx = details->channel_offset; + ch_rem = details->channel_count; /* * Get another digit and derive up to four logic channels' state from @@ -441,7 +556,7 @@ static int parse_column(const char *column, struct context *inc, size_t col_nr) while (rdptr > column && ch_rem) { /* Check for valid digits according to the input radix. */ c = *(--rdptr); - switch (inc->format) { + switch (details->text_format) { case FORMAT_BIN: valid = g_ascii_isxdigit(c) && c < '2'; ch_inc = 1; @@ -459,17 +574,14 @@ static int parse_column(const char *column, struct context *inc, size_t col_nr) break; } if (!valid) { - type_text = col_format_text[inc->format]; + type_text = col_format_text[details->text_format]; sr_err("Invalid text '%s' in %s type column %zu in line %zu.", - column, type_text, col_nr, inc->line_number); + column, type_text, details->col_nr, inc->line_number); return SR_ERR; } /* Use the digit's bits for logic channels' data. */ bits = g_ascii_xdigit_value(c); - switch (inc->format) { - case FORMAT_NONE: - /* ShouldNotHappen(TM), but silences compiler warning. */ - return SR_ERR; + switch (details->text_format) { case FORMAT_HEX: if (ch_rem >= 4) { ch_rem--; @@ -490,6 +602,9 @@ static int parse_column(const char *column, struct context *inc, size_t col_nr) ch_rem--; set_logic_level(inc, ch_idx + 0, bits & (1 << 0)); break; + case FORMAT_NONE: + /* ShouldNotHappen(TM), but silences compiler warning. */ + return SR_ERR; } ch_idx += ch_inc; } @@ -504,100 +619,132 @@ static int parse_column(const char *column, struct context *inc, size_t col_nr) return SR_OK; } +/** + * @brief Parse routine which ignores the input text. + * + * This routine exists to unify dispatch code paths, mapping input file + * columns' data types to their respective parse routines. + */ +static int parse_ignore(const char *column, struct context *inc, + const struct column_details *details) +{ + (void)column; + (void)inc; + (void)details; + return SR_OK; +} + +typedef int (*col_parse_cb)(const char *column, struct context *inc, + const struct column_details *details); + +static const col_parse_cb col_parse_funcs[] = { + [FORMAT_NONE] = parse_ignore, + [FORMAT_BIN] = parse_logic, + [FORMAT_OCT] = parse_logic, + [FORMAT_HEX] = parse_logic, +}; + static int init(struct sr_input *in, GHashTable *options) { struct context *inc; + size_t single_column, first_column, logic_channels; const char *s; - int ret; + enum single_col_format format; + char format_char; - in->sdi = g_malloc0(sizeof(struct sr_dev_inst)); - in->priv = inc = g_malloc0(sizeof(struct context)); + in->sdi = g_malloc0(sizeof(*in->sdi)); + in->priv = inc = g_malloc0(sizeof(*inc)); - inc->single_column = g_variant_get_uint32(g_hash_table_lookup(options, "single-column")); - inc->multi_column_mode = inc->single_column == 0; + single_column = g_variant_get_uint32(g_hash_table_lookup(options, "single_column")); - inc->num_channels = g_variant_get_uint32(g_hash_table_lookup(options, "numchannels")); + logic_channels = g_variant_get_uint32(g_hash_table_lookup(options, "logic_channels")); inc->delimiter = g_string_new(g_variant_get_string( - g_hash_table_lookup(options, "delimiter"), NULL)); - if (inc->delimiter->len == 0) { - sr_err("Column delimiter cannot be empty."); + g_hash_table_lookup(options, "column_separator"), NULL)); + if (!inc->delimiter->len) { + sr_err("Column separator cannot be empty."); return SR_ERR_ARG; } - s = g_variant_get_string(g_hash_table_lookup(options, "format"), NULL); - if (!g_ascii_strncasecmp(s, "bin", 3)) { - inc->format = FORMAT_BIN; - } else if (!g_ascii_strncasecmp(s, "hex", 3)) { - inc->format = FORMAT_HEX; - } else if (!g_ascii_strncasecmp(s, "oct", 3)) { - inc->format = FORMAT_OCT; + s = g_variant_get_string(g_hash_table_lookup(options, "single_format"), NULL); + if (g_ascii_strncasecmp(s, "bin", 3) == 0) { + format = FORMAT_BIN; + } else if (g_ascii_strncasecmp(s, "hex", 3) == 0) { + format = FORMAT_HEX; + } else if (g_ascii_strncasecmp(s, "oct", 3) == 0) { + format = FORMAT_OCT; } else { - sr_err("Invalid format: '%s'", s); + sr_err("Invalid single-column format: '%s'", s); return SR_ERR_ARG; } inc->comment = g_string_new(g_variant_get_string( - g_hash_table_lookup(options, "comment"), NULL)); + g_hash_table_lookup(options, "comment_leader"), NULL)); if (g_string_equal(inc->comment, inc->delimiter)) { /* * Using the same sequence as comment leader and column - * delimiter won't work. The user probably specified ';' - * as the column delimiter but did not adjust the comment + * separator won't work. The user probably specified ';' + * as the column separator but did not adjust the comment * leader. Try DWIM, drop comment strippin support here. */ - sr_warn("Comment leader and column delimiter conflict, disabling comment support."); + sr_warn("Comment leader and column separator conflict, disabling comment support."); g_string_truncate(inc->comment, 0); } inc->samplerate = g_variant_get_uint64(g_hash_table_lookup(options, "samplerate")); - inc->first_column = g_variant_get_uint32(g_hash_table_lookup(options, "first-column")); + first_column = g_variant_get_uint32(g_hash_table_lookup(options, "first_column")); inc->use_header = g_variant_get_boolean(g_hash_table_lookup(options, "header")); - inc->start_line = g_variant_get_uint32(g_hash_table_lookup(options, "startline")); + inc->start_line = g_variant_get_uint32(g_hash_table_lookup(options, "start_line")); if (inc->start_line < 1) { sr_err("Invalid start line %zu.", inc->start_line); return SR_ERR_ARG; } /* - * Derive the set of columns to inspect and their respective - * formats from simple input specs. Remain close to the previous - * set of option keywords and their meaning. Exclusively support - * a single column with multiple bits in it, or an adjacent set - * of colums with one bit each. The latter may not know the total - * column count here (when the user omitted the spec), and will - * derive it from the first text line of the input file. + * Scan flexible, to get prefered format specs which describe + * the input file's data formats. As well as some simple specs + * for backwards compatibility and user convenience. + * + * This logic ends up with a copy of the format string, either + * user provided or internally derived. Actual creation of the + * column processing details gets deferred until the first line + * of input data was seen. To support automatic determination of + * e.g. channel counts from column counts. */ - if (inc->single_column && inc->num_channels) { - sr_dbg("DIAG Got single column (%zu) and channels (%zu).", - inc->single_column, inc->num_channels); - sr_dbg("DIAG -> column %zu, %zu bits in %s format.", - inc->single_column, inc->num_channels, - col_format_text[inc->format]); - ret = make_column_details_single(inc, - inc->single_column, inc->num_channels, inc->format); - if (ret != SR_OK) - return ret; - } else if (inc->multi_column_mode) { - sr_dbg("DIAG Got multi-column, first column %zu, count %zu.", - inc->first_column, inc->num_channels); - if (inc->num_channels) { - sr_dbg("DIAG -> columns %zu-%zu, 1 bit each.", - inc->first_column, - inc->first_column + inc->num_channels - 1); - ret = make_column_details_multi(inc, inc->first_column, - inc->first_column + inc->num_channels - 1); - if (ret != SR_OK) - return ret; + s = g_variant_get_string(g_hash_table_lookup(options, "column_formats"), NULL); + if (s && *s) { + inc->column_formats = g_strdup(s); + sr_dbg("User specified column_formats: %s.", s); + } else if (single_column && logic_channels) { + format_char = col_format_char[format]; + if (single_column == 1) { + inc->column_formats = g_strdup_printf("%c%zu", + format_char, logic_channels); } else { - sr_dbg("DIAG -> incomplete spec, have to update later."); + inc->column_formats = g_strdup_printf("%zu-,%c%zu", + single_column - 1, + format_char, logic_channels); } + sr_dbg("Backwards compat single_column, col %zu, fmt %s, bits %zu -> %s.", + single_column, col_format_text[format], logic_channels, + inc->column_formats); + } else if (!single_column) { + if (first_column > 1) { + inc->column_formats = g_strdup_printf("%zu-,%zul", + first_column - 1, logic_channels); + } else { + inc->column_formats = g_strdup_printf("%zul", + logic_channels); + } + sr_dbg("Backwards compat multi-column, col %zu, chans %zu -> %s.", + first_column, logic_channels, + inc->column_formats); } else { - sr_err("Unknown or unsupported combination of option values."); - return SR_ERR_ARG; + sr_warn("Unknown or unsupported columns layout spec, assuming simple multi-column mode."); + inc->column_formats = g_strdup("*l"); } return SR_OK; @@ -666,6 +813,8 @@ static int initial_parse(const struct sr_input *in, GString *buf) size_t line_number, line_idx; int ret; char **lines, *line, **columns, *column; + const char *col_caption; + gboolean got_caption; const struct column_details *detail; ret = SR_OK; @@ -673,7 +822,10 @@ static int initial_parse(const struct sr_input *in, GString *buf) columns = NULL; line_number = 0; - lines = g_strsplit_set(buf->str, delim_set, 0); + if (inc->termination) + lines = g_strsplit(buf->str, inc->termination, 0); + else + lines = g_strsplit_set(buf->str, delim_set, 0); for (line_idx = 0; (line = lines[line_idx]); line_idx++) { line_number++; if (inc->start_line > line_number) { @@ -714,16 +866,16 @@ static int initial_parse(const struct sr_input *in, GString *buf) } sr_dbg("DIAG Got %zu columns in text line: %s.", num_columns, line); - /* Optionally update incomplete multi-column specs. */ - if (inc->multi_column_mode && !inc->num_channels) { - inc->num_channels = num_columns - inc->first_column + 1; - sr_dbg("DIAG -> multi-column update: columns %zu-%zu, 1 bit each.", - inc->first_column, - inc->first_column + inc->num_channels - 1); - ret = make_column_details_multi(inc, inc->first_column, - inc->first_column + inc->num_channels - 1); - if (ret != SR_OK) - goto out; + /* + * Track the observed number of columns in the input file. Do + * process the previously gathered columns format spec now that + * automatic channel count can be dealt with. + */ + inc->column_seen_count = num_columns; + ret = make_column_details_from_format(inc, inc->column_formats); + if (ret != SR_OK) { + sr_err("Cannot parse columns format using line %zu.", line_number); + goto out; } /* @@ -744,20 +896,29 @@ static int initial_parse(const struct sr_input *in, GString *buf) * line (when requested by the user, and only works in multi * column mode). In the absence of header text, or in single * column mode, channels are assigned rather generic names. + * + * Manipulation of the column's caption is acceptable here, the + * header line will never get processed another time. */ channel_name = g_string_sized_new(64); for (col_idx = 0; col_idx < inc->column_want_count; col_idx++) { + col_nr = col_idx + 1; detail = lookup_column_details(inc, col_nr); if (detail->text_format == FORMAT_NONE) continue; column = columns[col_idx]; + col_caption = sr_scpi_unquote_string(column); + got_caption = inc->use_header && *col_caption; sr_dbg("DIAG col %zu, ch count %zu, text %s.", - col_nr, detail->channel_count, column); + col_nr, detail->channel_count, col_caption); for (ch_idx = 0; ch_idx < detail->channel_count; ch_idx++) { ch_name_idx = detail->channel_offset + ch_idx; - if (inc->use_header && *column && inc->multi_column_mode) - g_string_assign(channel_name, column); + if (got_caption && detail->channel_count == 1) + g_string_assign(channel_name, col_caption); + else if (got_caption) + g_string_printf(channel_name, "%s[%zu]", + col_caption, ch_idx); else g_string_printf(channel_name, "%zu", ch_name_idx); sr_dbg("DIAG ch idx %zu, name %s.", ch_name_idx, channel_name->str); @@ -778,7 +939,7 @@ static int initial_parse(const struct sr_input *in, GString *buf) * Allocate the larger buffer, the "sample buffer" will point * to a location within that large buffer later. */ - inc->sample_unit_size = (inc->num_channels + 7) / 8; + inc->sample_unit_size = (inc->logic_channels + 7) / 8; inc->datafeed_buf_size = CHUNK_SIZE; inc->datafeed_buf_size *= inc->sample_unit_size; inc->datafeed_buffer = g_malloc(inc->datafeed_buf_size); @@ -855,6 +1016,8 @@ static int process_buffer(struct sr_input *in, gboolean is_eof) struct context *inc; gsize num_columns; size_t line_idx, col_idx, col_nr; + const struct column_details *details; + col_parse_cb parse_func; int ret; char *p, **lines, *line, **columns, *column; @@ -891,9 +1054,13 @@ static int process_buffer(struct sr_input *in, gboolean is_eof) g_strstrip(in->buf->str); ret = SR_OK; - lines = g_strsplit_set(in->buf->str, delim_set, 0); + lines = g_strsplit(in->buf->str, inc->termination, 0); for (line_idx = 0; (line = lines[line_idx]); line_idx++) { inc->line_number++; + if (inc->line_number < inc->start_line) { + sr_spew("Line %zu skipped (before start).", inc->line_number); + continue; + } if (line[0] == '\0') { sr_spew("Blank line %zu skipped.", inc->line_number); continue; @@ -929,12 +1096,18 @@ static int process_buffer(struct sr_input *in, gboolean is_eof) return SR_ERR; } + /* Have the columns of the current text line processed. */ clear_logic_samples(inc); - for (col_idx = 0; col_idx < inc->column_want_count; col_idx++) { column = columns[col_idx]; col_nr = col_idx + 1; - ret = parse_column(column, inc, col_nr); + details = lookup_column_details(inc, col_nr); + if (!details || !details->text_format) + continue; + parse_func = col_parse_funcs[details->text_format]; + if (!parse_func) + continue; + ret = parse_func(column, inc, details); if (ret != SR_OK) { g_strfreev(columns); g_strfreev(lines); @@ -967,7 +1140,7 @@ static int receive(struct sr_input *in, GString *buf) g_string_append_len(in->buf, buf->str, buf->len); inc = in->priv; - if (!inc->termination) { + if (!inc->column_seen_count) { ret = initial_receive(in); if (ret == SR_ERR_NA) /* Not enough data yet. */ @@ -1034,28 +1207,70 @@ static int reset(struct sr_input *in) } enum option_index { + OPT_COL_FMTS, OPT_SINGLE_COL, + OPT_FIRST_COL, OPT_NUM_LOGIC, - OPT_DELIM, OPT_FORMAT, - OPT_COMMENT, - OPT_RATE, - OPT_FIRST_COL, - OPT_HEADER, OPT_START, + OPT_HEADER, + OPT_RATE, + OPT_DELIM, + OPT_COMMENT, OPT_MAX, }; static struct sr_option options[] = { - [OPT_SINGLE_COL] = { "single-column", "Single column", "Enable single-column mode, using the specified column (>= 1); 0: multi-col. mode", NULL, NULL }, - [OPT_NUM_LOGIC] = { "numchannels", "Number of logic channels", "The number of (logic) channels (single-col. mode: number of bits beginning at 'first channel', LSB-first)", NULL, NULL }, - [OPT_DELIM] = { "delimiter", "Column delimiter", "The column delimiter (>= 1 characters)", NULL, NULL }, - [OPT_FORMAT] = { "format", "Data format (single-col. mode)", "The numeric format of the data (single-col. mode): bin, hex, oct", NULL, NULL }, - [OPT_COMMENT] = { "comment", "Comment character(s)", "The comment prefix character(s)", NULL, NULL }, - [OPT_RATE] = { "samplerate", "Samplerate (Hz)", "The sample rate (used during capture) in Hz", NULL, NULL }, - [OPT_FIRST_COL] = { "first-column", "First column", "The column number of the first channel (multi-col. mode)", NULL, NULL }, - [OPT_HEADER] = { "header", "Interpret first line as header (multi-col. mode)", "Treat the first line as header with channel names (multi-col. mode)", NULL, NULL }, - [OPT_START] = { "startline", "Start line", "The line number at which to start processing samples (>= 1)", NULL, NULL }, + [OPT_COL_FMTS] = { + "column_formats", "Column format specs", + "Specifies text columns data types: comma separated list of [][], with -/x/o/b/l format specifiers.", + NULL, NULL, + }, + [OPT_SINGLE_COL] = { + "single_column", "Single column", + "Enable single-column mode, exclusively use text from the specified column (number starting at 1).", + NULL, NULL, + }, + [OPT_FIRST_COL] = { + "first_column", "First column", + "Number of the first column with logic data in simple multi-column mode (number starting at 1, default 1).", + NULL, NULL, + }, + [OPT_NUM_LOGIC] = { + "logic_channels", "Number of logic channels", + "Logic channel count, required in simple single-column mode, defaults to \"all remaining columns\" in simple multi-column mode. Obsoleted by 'column_formats'.", + NULL, NULL, + }, + [OPT_FORMAT] = { + "single_format", "Data format for simple single-column mode.", + "The number format of single-column mode input data: bin, hex, oct.", + NULL, NULL, + }, + [OPT_START] = { + "start_line", "Start line", + "The line number at which to start processing input text (default: 1).", + NULL, NULL, + }, + [OPT_HEADER] = { + "header", "Get channel names from first line.", + "Use the first processed line's column captions (when available) as channel names.", + NULL, NULL, + }, + [OPT_RATE] = { + "samplerate", "Samplerate (Hz)", + "The input data's sample rate in Hz.", + NULL, NULL, + }, + [OPT_DELIM] = { + "column_separator", "Column separator", + "The sequence which separates text columns. Non-empty text, comma by default.", + NULL, NULL, + }, + [OPT_COMMENT] = { + "comment_leader", "Comment leader character", + "The text which starts comments at the end of text lines.", + NULL, NULL, + }, [OPT_MAX] = ALL_ZERO, }; @@ -1064,20 +1279,21 @@ static const struct sr_option *get_options(void) GSList *l; if (!options[0].def) { + options[OPT_COL_FMTS].def = g_variant_ref_sink(g_variant_new_string("")); options[OPT_SINGLE_COL].def = g_variant_ref_sink(g_variant_new_uint32(0)); + options[OPT_FIRST_COL].def = g_variant_ref_sink(g_variant_new_uint32(1)); options[OPT_NUM_LOGIC].def = g_variant_ref_sink(g_variant_new_uint32(0)); - options[OPT_DELIM].def = g_variant_ref_sink(g_variant_new_string(",")); options[OPT_FORMAT].def = g_variant_ref_sink(g_variant_new_string("bin")); l = NULL; l = g_slist_append(l, g_variant_ref_sink(g_variant_new_string("bin"))); l = g_slist_append(l, g_variant_ref_sink(g_variant_new_string("hex"))); l = g_slist_append(l, g_variant_ref_sink(g_variant_new_string("oct"))); options[OPT_FORMAT].values = l; - options[OPT_COMMENT].def = g_variant_ref_sink(g_variant_new_string(";")); - options[OPT_RATE].def = g_variant_ref_sink(g_variant_new_uint64(0)); - options[OPT_FIRST_COL].def = g_variant_ref_sink(g_variant_new_uint32(1)); - options[OPT_HEADER].def = g_variant_ref_sink(g_variant_new_boolean(FALSE)); options[OPT_START].def = g_variant_ref_sink(g_variant_new_uint32(1)); + options[OPT_HEADER].def = g_variant_ref_sink(g_variant_new_boolean(FALSE)); + options[OPT_RATE].def = g_variant_ref_sink(g_variant_new_uint64(0)); + options[OPT_DELIM].def = g_variant_ref_sink(g_variant_new_string(",")); + options[OPT_COMMENT].def = g_variant_ref_sink(g_variant_new_string(";")); } return options;