#include "config.h"
+#include <ctype.h>
#include <glib.h>
#include <stdlib.h>
#include <string.h>
+#include <strings.h>
#include <libsigrok/libsigrok.h>
#include "libsigrok-internal.h"
* single-bit logic), and an optional bit count (translating to: logic
* channels communicated in that column). The 'a' format marks analog
* data, an optionally following number is the digits count (resolution).
- * This "column_formats" option is most versatile, other forms of
- * specifying the column layout only exist for backwards compatibility,
- * and are rather limited. They exclusively support logic input data in
- * strictly adjacent columns, with further constraints on column layout
- * for multi-bit data.
+ * The 't' format marks timestamp values, which could help in automatic
+ * determination of the input stream's samplerate. This "column_formats"
+ * option is most versatile, other forms of specifying the column layout
+ * only exist for backwards compatibility, and are rather limited. They
+ * exclusively support logic input data in strictly adjacent columns,
+ * with further constraints on column layout for multi-bit data.
*
* single_column: Specifies the column number which contains the logic data
* for single-column mode. All logic data is taken from several bits
* - ... -I csv:column_formats=*a6 ...
* Each column contains an analog value with six significant digits
* after the decimal period.
+ * - ... -I csv:column_formats=t,2a ...
+ * The first column contains timestamps, the next two columns contain
+ * analog values. The capture's samplerate could get determined from
+ * the timestamp values if not provided by the user by means of the
+ * 'samplerate' option. This assumes a mere number in units of seconds,
+ * and equidistant rows, there is no fancy support for textual unit
+ * suffixes nor gaps in the stream of samples nor other non-linearity,
+ * just '-' ignore the column if the format is not supported).
+ *
+ * IMPORTANT! Make sure the .format_match() logic matches the default
+ * values for the input module's options. Ideally the format match test
+ * shall pass for all input data that is supported by default.
*/
/*
* TODO
*
- * - Extend support for analog input data.
- * - Determine why analog samples of 'double' data type get scrambled
- * in sigrok-cli screen output. Is analog.encoding->unitsize not
- * handled properly? A sigrok-cli or libsigrok (src/output) issue?
- * - Reconsider the channel creation after format processing. Current
- * logic may "bleed" channel names into the analog group when logic
- * channels' columns follow analog columns (seen with "-,2a,x8").
- * Trying to sort it out, a naive change used to map logic channels'
- * data to incorrect bitmap positions. The whole channel numbering
- * needs reconsideration. Probably it's easiest to first create _all_
- * logic channels so that they have adjacent numbers starting at 0
- * (addressing logic bits), then all analog channels (again adjacent)
- * to simplify the calculation of their index in the sample set as
- * well as their sdi channel index from the "analog column index".
- * - Optionally get sample rate from timestamp column. Just best-effort
- * approach, not necessarily reliable. Users can always specify rates.
* - Add a test suite for input modules in general, and CSV in specific?
* Becomes more important with the multitude of options and their
* interaction. Could cover edge cases (BOM presence, line termination
* samplerates, etc).
*/
-typedef float csv_analog_t; /* 'double' currently is flawed. */
+typedef double csv_analog_t;
/* Single column formats. */
enum single_col_format {
FORMAT_HEX, /* Hex digits for a set of bits. */
FORMAT_OCT, /* Oct digits for a set of bits. */
FORMAT_ANALOG, /* Floating point number for an analog channel. */
+ FORMAT_TIME, /* Timestamps. */
};
static const char *col_format_text[] = {
[FORMAT_HEX] = "hexadecimal",
[FORMAT_OCT] = "octal",
[FORMAT_ANALOG] = "analog",
+ [FORMAT_TIME] = "timestamp",
};
static const char col_format_char[] = {
[FORMAT_HEX] = 'x',
[FORMAT_OCT] = 'o',
[FORMAT_ANALOG] = 'a',
+ [FORMAT_TIME] = 't',
};
static gboolean format_is_ignore(enum single_col_format fmt)
return fmt == FORMAT_ANALOG;
}
+static gboolean format_is_timestamp(enum single_col_format fmt)
+{
+ return fmt == FORMAT_TIME;
+}
+
struct column_details {
size_t col_nr;
enum single_col_format text_format;
size_t channel_offset;
size_t channel_count;
- size_t channel_index;
int analog_digits;
+ GString **channel_names;
};
struct context {
gboolean started;
- /* Current selected samplerate. */
+ /* Current samplerate, optionally determined from input data. */
uint64_t samplerate;
+ uint64_t calc_samplerate;
+ double prev_timestamp;
gboolean samplerate_sent;
/* Number of channels. */
csv_analog_t *analog_datafeed_buffer; /**!< Queue for analog datafeed. */
size_t analog_datafeed_buf_size;
size_t analog_datafeed_buf_fill;
- GSList **analog_datafeed_channels;
int *analog_datafeed_digits;
+ GSList **analog_datafeed_channels;
/* Current line number. */
size_t line_number;
/* List of previously created sigrok channels. */
GSList *prev_sr_channels;
+ GSList **prev_df_channels;
};
/*
static int flush_samplerate(const struct sr_input *in)
{
struct context *inc;
- struct sr_datafeed_packet packet;
- struct sr_datafeed_meta meta;
- struct sr_config *src;
inc = in->priv;
- if (inc->samplerate && !inc->samplerate_sent) {
- packet.type = SR_DF_META;
- packet.payload = &meta;
- src = sr_config_new(SR_CONF_SAMPLERATE, g_variant_new_uint64(inc->samplerate));
- meta.config = g_slist_append(NULL, src);
- sr_session_send(in->sdi, &packet);
- g_slist_free(meta.config);
- sr_config_free(src);
+ if (!inc->calc_samplerate && inc->samplerate)
+ inc->calc_samplerate = inc->samplerate;
+ if (inc->calc_samplerate && !inc->samplerate_sent) {
+ (void)sr_session_send_meta(in->sdi, SR_CONF_SAMPLERATE,
+ g_variant_new_uint64(inc->calc_samplerate));
inc->samplerate_sent = TRUE;
}
return rc;
inc->datafeed_buf_fill = 0;
+
return SR_OK;
}
if (rc != SR_OK)
return rc;
}
+
return SR_OK;
}
}
inc->analog_datafeed_buf_fill = 0;
+
return SR_OK;
}
if (rc != SR_OK)
return rc;
}
+
return SR_OK;
}
case 'a':
format_code = FORMAT_ANALOG;
break;
+ case 't':
+ format_code = FORMAT_TIME;
+ break;
default: /* includes NUL */
return SR_ERR_ARG;
}
char *column;
const char *caption;
int channel_type, channel_sdi_nr;
+ void *channel;
int ret;
inc = in->priv;
inc->column_seen_count = g_strv_length(column_texts);
- /* Split the input spec, count involved columns and bits. */
+ /* Split the input spec, count involved columns and channels. */
formats = g_strsplit(column_format, ",", 0);
if (!formats) {
sr_err("Cannot parse columns format %s (comma split).", column_format);
sr_dbg("Column format %s -> %zu columns, %zu logic, %zu analog channels.",
column_format, column_count, logic_count, analog_count);
- /* Allocate and fill in "column processing" details. Create channels. */
+ /* Allocate and fill in "column processing" details. */
inc->column_want_count = column_count;
if (inc->column_seen_count < inc->column_want_count) {
sr_err("Insufficient input text width for desired data amount, got %zu but want %zu columns.",
g_strfreev(formats);
return SR_ERR_ARG;
}
+ inc->logic_channels = logic_count;
+ inc->analog_channels = analog_count;
+ inc->analog_datafeed_digits = g_malloc0(inc->analog_channels * sizeof(inc->analog_datafeed_digits[0]));
+ inc->analog_datafeed_channels = g_malloc0(inc->analog_channels * sizeof(inc->analog_datafeed_channels[0]));
inc->column_details = g_malloc0_n(column_count, sizeof(inc->column_details[0]));
column_idx = channel_idx = analog_idx = 0;
channel_name = g_string_sized_new(64);
if (!caption || !*caption)
caption = NULL;
/*
- * TODO Need we first create _all_ logic channels,
- * before creating analog channels? Just store the
- * parameters here (index, type, name) and have the
- * creation sequence done outside of the format
- * spec parse loop.
+ * Collect channel creation details here, but defer
+ * actual creation of the channels such that all
+ * logic channels can get created first and analog
+ * channels only get created afterwards.
*/
+ detail->channel_names = g_malloc0(detail->channel_count * sizeof(detail->channel_names[0]));
for (create_idx = 0; create_idx < detail->channel_count; create_idx++) {
if (caption && detail->channel_count == 1) {
g_string_assign(channel_name, caption);
g_string_printf(channel_name, "%zu",
detail->channel_offset + create_idx);
}
- if (format_is_analog(detail->text_format)) {
- channel_sdi_nr = logic_count + detail->channel_offset + create_idx;
- channel_type = SR_CHANNEL_ANALOG;
- detail->channel_index = g_slist_length(in->sdi->channels);
- } else if (format_is_logic(detail->text_format)) {
- channel_sdi_nr = detail->channel_offset + create_idx;
- channel_type = SR_CHANNEL_LOGIC;
- } else {
- continue;
- }
- sr_channel_new(in->sdi, channel_sdi_nr,
- channel_type, TRUE, channel_name->str);
+ detail->channel_names[create_idx] = g_string_new_len(channel_name->str, channel_name->len);
}
}
}
- inc->logic_channels = channel_idx;
- inc->analog_channels = analog_idx;
g_string_free(channel_name, TRUE);
g_strfreev(formats);
+ /* Create channels in strict logic to analog order. */
+ channel_type = SR_CHANNEL_LOGIC;
+ for (column_idx = 0; column_idx < inc->column_want_count; column_idx++) {
+ detail = &inc->column_details[column_idx];
+ if (!format_is_logic(detail->text_format))
+ continue;
+ for (create_idx = 0; create_idx < detail->channel_count; create_idx++) {
+ caption = detail->channel_names[create_idx]->str;
+ channel_sdi_nr = g_slist_length(in->sdi->channels);
+ sr_channel_new(in->sdi, channel_sdi_nr, channel_type, TRUE, caption);
+ }
+ }
+ channel_type = SR_CHANNEL_ANALOG;
+ for (column_idx = 0; column_idx < inc->column_want_count; column_idx++) {
+ detail = &inc->column_details[column_idx];
+ if (!format_is_analog(detail->text_format))
+ continue;
+ caption = detail->channel_names[0]->str;
+ channel_sdi_nr = g_slist_length(in->sdi->channels);
+ channel = sr_channel_new(in->sdi, channel_sdi_nr, channel_type, TRUE, caption);
+ channel_idx = channel_sdi_nr - inc->logic_channels;
+ inc->analog_datafeed_digits[channel_idx] = detail->analog_digits;
+ inc->analog_datafeed_channels[channel_idx] = g_slist_append(NULL, channel);
+ }
+
return SR_OK;
}
return NULL;
if (!nr || nr > inc->column_want_count)
return NULL;
+
return &inc->column_details[nr - 1];
}
}
/**
- * @brief Splits a text line into a set of columns.
+ * Splits a text line into a set of columns.
*
* @param[in] buf The input text line to split.
* @param[in] inc The input module's context.
*/
static char **split_line(char *buf, struct context *inc)
{
- return g_strsplit(buf, inc->delimiter->str, 0);
+ char **fields, *f;
+ size_t l;
+
+ fields = g_strsplit(buf, inc->delimiter->str, 0);
+ if (!fields)
+ return NULL;
+
+ l = g_strv_length(fields);
+ while (l--) {
+ f = fields[l];
+ g_strchomp(f);
+ }
+
+ return fields;
}
/**
- * @brief Parse a multi-bit field into several logic channels.
+ * Parse a multi-bit field into several logic channels.
*
* @param[in] column The input text, a run of bin/hex/oct digits.
* @param[in] inc The input module's context.
}
/**
- * @brief Parse a floating point text into an analog value.
+ * Parse a floating point text into an analog value.
*
* @param[in] column The input text, a floating point number.
* @param[in] inc The input module's context.
}
/**
- * @brief Parse routine which ignores the input text.
+ * Parse a timestamp text, auto-determine samplerate.
+ *
+ * @param[in] column The input text, a floating point number.
+ * @param[in] inc The input module's context.
+ * @param[in] details The column processing details.
+ *
+ * @retval SR_OK Success.
+ * @retval SR_ERR Invalid input data (empty, or format error).
+ *
+ * This routine attempts to automatically determine the input data's
+ * samplerate from text rows' timestamp values. Only simple formats are
+ * supported, user provided values always take precedence.
+ */
+static int parse_timestamp(const char *column, struct context *inc,
+ const struct column_details *details)
+{
+ double ts, rate;
+ int ret;
+
+ if (!format_is_timestamp(details->text_format))
+ return SR_ERR_BUG;
+
+ /*
+ * Implementor's notes on timestamp interpretation. Use a simple
+ * approach for improved maintainability which covers most cases
+ * of input data. There is not much gain in adding complexity,
+ * users can easily provide the rate when auto-detection fails.
+ * - Bail out if samplerate is known already.
+ * - Try to interpret the timestamp (simple float conversion).
+ * If conversion fails then clear all previous knowledge and
+ * bail out (non-fatal, perhaps warn). Silently ignore values
+ * of zero since those could be silent fails -- assume that
+ * genuine data contains at least two adjacent rows with useful
+ * timestamps for the feature to work reliably. Annoying users
+ * with "failed to detect" messages is acceptable here, since
+ * users expecting the feature to work should provide useful
+ * data, and there are easy ways to disable the detection or
+ * ignore the column.
+ * - If there is no previous timestamp, keep the current value
+ * for later reference and bail out.
+ * - If a previous timestamp was seen, determine the difference
+ * between them, and derive the samplerate. Update internal
+ * state (the value automatically gets sent to the datafeed),
+ * and clear previous knowledge. Subsequent calls will ignore
+ * following input data (see above, rate is known).
+ *
+ * TODO Potential future improvements:
+ * - Prefer rationals over floats for improved precision and
+ * reduced rounding errors which result in odd rates.
+ * - Support other formats ("2 ms" or similar)?
+ */
+ if (inc->calc_samplerate)
+ return SR_OK;
+ ret = sr_atod_ascii(column, &ts);
+ if (ret != SR_OK)
+ ts = 0.0;
+ if (!ts) {
+ sr_info("Cannot convert timestamp text %s in line %zu (or zero value).",
+ column, inc->line_number);
+ inc->prev_timestamp = 0.0;
+ return SR_OK;
+ }
+ if (!inc->prev_timestamp) {
+ sr_dbg("First timestamp value %g in line %zu.",
+ ts, inc->line_number);
+ inc->prev_timestamp = ts;
+ return SR_OK;
+ }
+ sr_dbg("Second timestamp value %g in line %zu.", ts, inc->line_number);
+ ts -= inc->prev_timestamp;
+ sr_dbg("Timestamp difference %g in line %zu.",
+ ts, inc->line_number);
+ if (!ts) {
+ sr_warn("Zero timestamp difference in line %zu.",
+ inc->line_number);
+ inc->prev_timestamp = ts;
+ return SR_OK;
+ }
+ rate = 1.0 / ts;
+ rate += 0.5;
+ rate = (uint64_t)rate;
+ sr_dbg("Rate from timestamp %g in line %zu.", rate, inc->line_number);
+ inc->calc_samplerate = rate;
+ inc->prev_timestamp = 0.0;
+
+ return SR_OK;
+}
+
+/**
+ * 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.
(void)column;
(void)inc;
(void)details;
+
return SR_OK;
}
[FORMAT_OCT] = parse_logic,
[FORMAT_HEX] = parse_logic,
[FORMAT_ANALOG] = parse_analog,
+ [FORMAT_TIME] = parse_timestamp,
};
+/*
+ * BEWARE! Implementor's notes. Sync with feature set and default option
+ * values required during maintenance of the input module implementation.
+ *
+ * When applications invoke .format_match() routines, trying automatic
+ * determination of an input file's format handler, then no options are
+ * in effect. Because specifying options requires selection of an input
+ * module to pass the options to, which obsoletes the format-match check.
+ *
+ * Which means that we only need to deal with the default format here,
+ * which happens to be the simple multi-column format without header
+ * lines or leading garbage. Which means that the check can be rather
+ * strict, resulting in high levels of confidence upon match, never
+ * "accidently" winning for unreadable or unsupported-by-default formats.
+ *
+ * This .format_match() logic only needs to become more involved when
+ * default option values change, or when automatic detection of column
+ * data types improves. Then the supported-by-default types of input
+ * data must be considered acceptable here in the format-match check
+ * as well.
+ *
+ * Notice that the format check cannot re-use regular processing logic
+ * when their implementation assumes proper input data and wll generate
+ * diagnostics for unexpected input data. Failure to match the format is
+ * non-fatal here, mismatch must remain silent. It's up to applications
+ * how large a chunk of data gets passed here (start of the file's
+ * content). But inspection of the first few hundred bytes will usually
+ * be GoodEnough(TM) for the format-match purpose. Notice that filenames
+ * need not necessarily be available to the format-match routine.
+ *
+ * This implementation errs on the safe side. Users can always select
+ * the CSV input module when automatic format detection fails.
+ */
+static int format_match(GHashTable *metadata, unsigned int *confidence)
+{
+ const int match_confidence = 100;
+ const char *default_extension = ".csv";
+ const char *line_termination = "\n";
+ const char *comment_leader = ";";
+ const char *column_separator = ",";
+ const char *binary_charset = "01";
+
+ const char *fn;
+ GString *buf;
+ size_t fn_len;
+ GString *tmpbuf;
+ gboolean status;
+ size_t line_idx, col_idx;
+ char *rdptr, **lines, *line;
+ char **cols, *col;
+
+ /* Get the application provided input data properties. */
+ fn = g_hash_table_lookup(metadata, GINT_TO_POINTER(SR_INPUT_META_FILENAME));
+ buf = g_hash_table_lookup(metadata, GINT_TO_POINTER(SR_INPUT_META_HEADER));
+
+ /* Filenames are a strong hint. Use then when available. */
+ if (fn && *fn && (fn_len = strlen(fn)) >= strlen(default_extension)) {
+ if (strcasecmp(&fn[fn_len - strlen(default_extension)], default_extension) == 0) {
+ *confidence = 10;
+ return SR_OK;
+ }
+ }
+
+ /*
+ * Check file content for compatibility with the input module's
+ * default format. Which translates to:
+ * - Must be at least one text line worth of input data. Ignore
+ * incomplete lines at the end of the available buffer.
+ * - Must be LF terminated text lines, optional CR-LF sequence.
+ * (Drop CR-only for simplicity since that's rare and users
+ * can override the automatic detection.)
+ * - Strip comments and skip empty lines.
+ * - Data lines must be binary input (potentially multiple bits
+ * per column which then get ignored). Presence of comma is
+ * optional but then must be followed by another data column.
+ * - No other content is acceptable, there neither are ignored
+ * columns nor analog data nor timestamps in the default layout.
+ * (See the above "sync format match with default options"
+ * comment though during maintenance!)
+ * Run the check on a copy to not affect the caller's buffer.
+ */
+ if (!buf || !buf->len || !buf->str || !*buf->str)
+ return SR_ERR;
+ rdptr = g_strstr_len(buf->str, buf->len, line_termination);
+ if (!rdptr)
+ return SR_ERR;
+ tmpbuf = g_string_new_len(buf->str, rdptr + 1 - buf->str);
+ tmpbuf->str[tmpbuf->len - 1] = '\0';
+ status = TRUE;
+ *confidence = match_confidence;
+ lines = g_strsplit(tmpbuf->str, line_termination, 0);
+ for (line_idx = 0; status && (line = lines[line_idx]); line_idx++) {
+ rdptr = strstr(line, comment_leader);
+ if (rdptr)
+ *rdptr = '\0';
+ line = g_strstrip(line);
+ if (!line || !*line)
+ continue;
+ cols = g_strsplit(line, column_separator, 0);
+ if (!cols) {
+ status = FALSE;
+ break;
+ }
+ for (col_idx = 0; status && (col = cols[col_idx]); col_idx++) {
+ if (strspn(col, binary_charset) != strlen(col)) {
+ status = FALSE;
+ break;
+ }
+ }
+ g_strfreev(cols);
+ }
+ g_strfreev(lines);
+ g_string_free(tmpbuf, TRUE);
+
+ if (!status)
+ return SR_ERR;
+
+ return SR_OK;
+}
+
static int init(struct sr_input *in, GHashTable *options)
{
struct context *inc;
* Check the channel list for consistency across file re-import. See
* the VCD input module for more details and motivation.
*/
+static void release_df_channels(struct context *inc, GSList **l)
+{
+ size_t idx;
+
+ if (!inc->analog_channels || !l)
+ return;
+ for (idx = 0; idx < inc->analog_channels; idx++)
+ g_slist_free(l[idx]);
+ g_free(l);
+}
static void keep_header_for_reread(const struct sr_input *in)
{
struct context *inc;
inc = in->priv;
+
g_slist_free_full(inc->prev_sr_channels, sr_channel_free_cb);
inc->prev_sr_channels = in->sdi->channels;
in->sdi->channels = NULL;
+
+ release_df_channels(inc, inc->prev_df_channels);
+ inc->prev_df_channels = inc->analog_datafeed_channels;
+ inc->analog_datafeed_channels = NULL;
}
static int check_header_in_reread(const struct sr_input *in)
sr_err("Channel list change not supported for file re-read.");
return FALSE;
}
+
g_slist_free_full(in->sdi->channels, sr_channel_free_cb);
in->sdi->channels = inc->prev_sr_channels;
inc->prev_sr_channels = NULL;
+ release_df_channels(inc, inc->analog_datafeed_channels);
+ inc->analog_datafeed_channels = inc->prev_df_channels;
+ inc->prev_df_channels = NULL;
+
return TRUE;
}
ret = SR_ERR;
goto out;
}
- sr_dbg("DIAG Got %zu columns in text line: %s.", num_columns, line);
+ sr_dbg("Got %zu columns in text line: %s.", num_columns, line);
/*
* Interpret the user provided column format specs. This might
if (inc->analog_channels) {
size_t sample_size, sample_count;
- size_t detail_idx;
- struct column_details *detail;
- int *digits_item;
- void *channel;
sample_size = sizeof(inc->analog_datafeed_buffer[0]);
inc->analog_datafeed_buf_size = CHUNK_SIZE;
inc->analog_datafeed_buf_size /= sample_size;
goto out;
}
inc->analog_datafeed_buf_fill = 0;
- inc->analog_datafeed_channels = g_malloc0(inc->analog_channels * sizeof(inc->analog_datafeed_channels[0]));
- inc->analog_datafeed_digits = g_malloc0(inc->analog_channels * sizeof(inc->analog_datafeed_digits[0]));
- digits_item = inc->analog_datafeed_digits;
- for (detail_idx = 0; detail_idx < inc->column_want_count; detail_idx++) {
- detail = &inc->column_details[detail_idx];
- if (!format_is_analog(detail->text_format))
- continue;
- channel = g_slist_nth_data(in->sdi->channels, detail->channel_index);
- inc->analog_datafeed_channels[detail->channel_offset] = g_slist_append(NULL, channel);
- *digits_item++ = detail->analog_digits;
- }
}
out:
const struct column_details *details;
col_parse_cb parse_func;
int ret;
- char *p, **lines, *line, **columns, *column;
+ char *processed_up_to;
+ char **lines, *line, **columns, *column;
inc = in->priv;
if (!inc->started) {
if (!in->buf->len)
return SR_OK;
if (is_eof) {
- p = in->buf->str + in->buf->len;
+ processed_up_to = in->buf->str + in->buf->len;
} else {
- p = g_strrstr_len(in->buf->str, in->buf->len, inc->termination);
- if (!p)
- return SR_ERR;
- *p = '\0';
- p += strlen(inc->termination);
+ processed_up_to = g_strrstr_len(in->buf->str, in->buf->len,
+ inc->termination);
+ if (!processed_up_to)
+ return SR_OK;
+ *processed_up_to = '\0';
+ processed_up_to += strlen(inc->termination);
}
- g_strstrip(in->buf->str);
+ /* Split input text lines and process their columns. */
ret = SR_OK;
lines = g_strsplit(in->buf->str, inc->termination, 0);
for (line_idx = 0; (line = lines[line_idx]); line_idx++) {
g_strfreev(columns);
}
g_strfreev(lines);
- g_string_erase(in->buf, 0, p - in->buf->str);
+ g_string_erase(in->buf, 0, processed_up_to - in->buf->str);
return ret;
}
static void cleanup(struct sr_input *in)
{
- struct context *inc;
+ struct context *inc, save_ctx;
+ /* Keep channel references between file re-imports. */
keep_header_for_reread(in);
+ /* Release dynamically allocated resources. */
inc = in->priv;
g_free(inc->termination);
inc->datafeed_buffer = NULL;
g_free(inc->analog_datafeed_buffer);
inc->analog_datafeed_buffer = NULL;
+ g_free(inc->analog_datafeed_digits);
+ inc->analog_datafeed_digits = NULL;
+ /* analog_datafeed_channels was released in keep_header_for_reread() */
+ /* TODO Release channel names (before releasing details). */
+ g_free(inc->column_details);
+ inc->column_details = NULL;
+
+ /* Clear internal state, but keep what .init() has provided. */
+ save_ctx = *inc;
+ memset(inc, 0, sizeof(*inc));
+ inc->samplerate = save_ctx.samplerate;
+ inc->delimiter = save_ctx.delimiter;
+ inc->comment = save_ctx.comment;
+ inc->column_formats = save_ctx.column_formats;
+ inc->start_line = save_ctx.start_line;
+ inc->use_header = save_ctx.use_header;
+ inc->prev_sr_channels = save_ctx.prev_sr_channels;
+ inc->prev_df_channels = save_ctx.prev_df_channels;
}
static int reset(struct sr_input *in)
{
- struct context *inc = in->priv;
+ struct context *inc;
+ inc = in->priv;
cleanup(in);
inc->started = FALSE;
g_string_truncate(in->buf, 0);
OPT_SINGLE_COL,
OPT_FIRST_COL,
OPT_NUM_LOGIC,
- OPT_FORMAT,
- OPT_START,
+ OPT_SINGLE_FMT,
+ OPT_START_LINE,
OPT_HEADER,
- OPT_RATE,
- OPT_DELIM,
+ OPT_SAMPLERATE,
+ OPT_COL_SEP,
OPT_COMMENT,
OPT_MAX,
};
static struct sr_option options[] = {
[OPT_COL_FMTS] = {
"column_formats", "Column format specs",
- "Specifies text columns data types: A comma separated list of [<cols>]<fmt>[<bits>] items, with - to ignore columns, x/o/b/l for logic data, a (and resolution) for analog data.",
+ "Text columns data types. A comma separated list of [<cols>]<fmt>[<bits>] items. * for all remaining columns. - ignores columns, x/o/b/l logic data, a (and digits) analog data, t timestamps.",
NULL, NULL,
},
[OPT_SINGLE_COL] = {
"single_column", "Single column",
- "Enable single-column mode, exclusively use text from the specified column (number starting at 1). Obsoleted by 'column_formats'.",
+ "Simple single-column mode, exclusively use text from the specified column (number starting at 1). Obsoleted by 'column_formats=4-,x16'.",
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). Obsoleted by 'column_formats'.",
+ "First column with logic data in simple multi-column mode (number starting at 1, default 1). Obsoleted by 'column_formats=4-,*l'.",
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'.",
+ "Logic channel count, required in simple single-column mode, defaults to \"all remaining columns\" in simple multi-column mode. Obsoleted by 'column_formats=8l'.",
NULL, NULL,
},
- [OPT_FORMAT] = {
+ [OPT_SINGLE_FMT] = {
"single_format", "Data format for simple single-column mode.",
- "The number format of single-column mode input data: bin, hex, oct. Obsoleted by 'column_formats'.",
+ "The input text number format of simple single-column mode: bin, hex, oct. Obsoleted by 'column_formats=x8'.",
NULL, NULL,
},
- [OPT_START] = {
+ [OPT_START_LINE] = {
"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. Off by default",
+ "Use the first processed line's column captions (when available) as channel names. Enabled by default.",
NULL, NULL,
},
- [OPT_RATE] = {
+ [OPT_SAMPLERATE] = {
"samplerate", "Samplerate (Hz)",
"The input data's sample rate in Hz. No default value.",
NULL, NULL,
},
- [OPT_DELIM] = {
+ [OPT_COL_SEP] = {
"column_separator", "Column separator",
"The sequence which separates text columns. Non-empty text, comma by default.",
NULL, NULL,
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_FORMAT].def = g_variant_ref_sink(g_variant_new_string("bin"));
+ options[OPT_SINGLE_FMT].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_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_SINGLE_FMT].values = l;
+ options[OPT_START_LINE].def = g_variant_ref_sink(g_variant_new_uint32(1));
+ options[OPT_HEADER].def = g_variant_ref_sink(g_variant_new_boolean(TRUE));
+ options[OPT_SAMPLERATE].def = g_variant_ref_sink(g_variant_new_uint64(0));
+ options[OPT_COL_SEP].def = g_variant_ref_sink(g_variant_new_string(","));
options[OPT_COMMENT].def = g_variant_ref_sink(g_variant_new_string(";"));
}
.name = "CSV",
.desc = "Comma-separated values",
.exts = (const char*[]){"csv", NULL},
+ .metadata = { SR_INPUT_META_FILENAME, SR_INPUT_META_HEADER | SR_INPUT_META_REQUIRED },
.options = get_options,
+ .format_match = format_match,
.init = init,
.receive = receive,
.end = end,