]> sigrok.org Git - libsigrok.git/blobdiff - src/input/csv.c
input/csv: trim cell content before conversion
[libsigrok.git] / src / input / csv.c
index 4c4143e24cb9e4496507685dfc10a974ac50fc98..66cf4bb3c2445af3ad9c8c3c1be36e2d658b03d4 100644 (file)
  *   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 {
@@ -210,8 +198,8 @@ struct column_details {
        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 {
@@ -219,6 +207,7 @@ struct context {
 
        /* Current samplerate, optionally determined from input data. */
        uint64_t samplerate;
+       uint64_t calc_samplerate;
        double prev_timestamp;
        gboolean samplerate_sent;
 
@@ -258,14 +247,15 @@ 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;
 
        /* List of previously created sigrok channels. */
        GSList *prev_sr_channels;
+       GSList **prev_df_channels;
 };
 
 /*
@@ -284,19 +274,13 @@ struct context {
 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;
        }
 
@@ -355,6 +339,7 @@ static int flush_logic_samples(const struct sr_input *in)
                return rc;
 
        inc->datafeed_buf_fill = 0;
+
        return SR_OK;
 }
 
@@ -373,6 +358,7 @@ static int queue_logic_samples(const struct sr_input *in)
                if (rc != SR_OK)
                        return rc;
        }
+
        return SR_OK;
 }
 
@@ -448,6 +434,7 @@ static int flush_analog_samples(const struct sr_input *in)
        }
 
        inc->analog_datafeed_buf_fill = 0;
+
        return SR_OK;
 }
 
@@ -466,6 +453,7 @@ static int queue_analog_samples(const struct sr_input *in)
                if (rc != SR_OK)
                        return rc;
        }
+
        return SR_OK;
 }
 
@@ -565,12 +553,13 @@ static int make_column_details_from_format(const struct sr_input *in,
        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);
@@ -612,7 +601,7 @@ static int make_column_details_from_format(const struct sr_input *in,
        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.",
@@ -620,6 +609,10 @@ static int make_column_details_from_format(const struct sr_input *in,
                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);
@@ -671,12 +664,12 @@ static int make_column_details_from_format(const struct sr_input *in,
                        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);
@@ -687,26 +680,38 @@ static int make_column_details_from_format(const struct sr_input *in,
                                        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;
 }
 
@@ -716,6 +721,7 @@ static const struct column_details *lookup_column_details(struct context *inc, s
                return NULL;
        if (!nr || nr > inc->column_want_count)
                return NULL;
+
        return &inc->column_details[nr - 1];
 }
 
@@ -739,7 +745,7 @@ static void strip_comment(char *buf, const GString *prefix)
 }
 
 /**
- * @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.
@@ -750,11 +756,24 @@ static void strip_comment(char *buf, const GString *prefix)
  */
 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.
@@ -864,7 +883,7 @@ static int parse_logic(const char *column, struct context *inc,
 }
 
 /**
- * @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.
@@ -913,7 +932,7 @@ static int parse_analog(const char *column, struct context *inc,
 }
 
 /**
- * @brief Parse a timestamp text, auto-determine samplerate.
+ * 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.
@@ -964,13 +983,13 @@ static int parse_timestamp(const char *column, struct context *inc,
         *   reduced rounding errors which result in odd rates.
         * - Support other formats ("2 ms" or similar)?
         */
-       if (inc->samplerate)
+       if (inc->calc_samplerate)
                return SR_OK;
        ret = sr_atod_ascii(column, &ts);
        if (ret != SR_OK)
                ts = 0.0;
        if (!ts) {
-               sr_warn("Cannot convert timestamp text %s in line %zu (or zero value).",
+               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;
@@ -995,14 +1014,14 @@ static int parse_timestamp(const char *column, struct context *inc,
        rate += 0.5;
        rate = (uint64_t)rate;
        sr_dbg("Rate from timestamp %g in line %zu.", rate, inc->line_number);
-       inc->samplerate = rate;
+       inc->calc_samplerate = rate;
        inc->prev_timestamp = 0.0;
 
        return SR_OK;
 }
 
 /**
- * @brief Parse routine which ignores the input text.
+ * 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.
@@ -1013,6 +1032,7 @@ static int parse_ignore(const char *column, struct context *inc,
        (void)column;
        (void)inc;
        (void)details;
+
        return SR_OK;
 }
 
@@ -1144,6 +1164,7 @@ static int format_match(GHashTable *metadata, unsigned int *confidence)
 
        if (!status)
                return SR_ERR;
+
        return SR_OK;
 }
 
@@ -1249,15 +1270,30 @@ static int init(struct sr_input *in, GHashTable *options)
  * 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)
@@ -1276,10 +1312,15 @@ 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;
 }
 
@@ -1356,7 +1397,7 @@ static int initial_parse(const struct sr_input *in, GString *buf)
                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
@@ -1406,10 +1447,6 @@ static int initial_parse(const struct sr_input *in, GString *buf)
 
        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;
@@ -1422,17 +1459,6 @@ static int initial_parse(const struct sr_input *in, GString *buf)
                        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:
@@ -1509,7 +1535,8 @@ static int process_buffer(struct sr_input *in, gboolean is_eof)
        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) {
@@ -1533,16 +1560,17 @@ static int process_buffer(struct sr_input *in, gboolean is_eof)
        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++) {
@@ -1619,7 +1647,7 @@ static int process_buffer(struct sr_input *in, gboolean is_eof)
                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;
 }
@@ -1676,10 +1704,12 @@ static int end(struct sr_input *in)
 
 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);
@@ -1688,12 +1718,31 @@ static void cleanup(struct sr_input *in)
        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);
@@ -1748,7 +1797,7 @@ static struct sr_option options[] = {
        },
        [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_SAMPLERATE] = {
@@ -1785,7 +1834,7 @@ static const struct sr_option *get_options(void)
                l = g_slist_append(l, g_variant_ref_sink(g_variant_new_string("oct")));
                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(FALSE));
+               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(";"));