#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"
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 {
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;
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;
}
[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;
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:
.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,