]> sigrok.org Git - libsigrok.git/blobdiff - src/input/csv.c
Change sr_dev_inst_new() to take no parameters.
[libsigrok.git] / src / input / csv.c
index c6aabd965e7347111b9a1c2328b9853203dd258c..b2756fa0aa124453dd266b2373896d2d06b37d21 100644 (file)
@@ -393,7 +393,7 @@ static int init(struct sr_input *in, GHashTable *options)
        struct context *inc;
        const char *s;
 
-       in->sdi = sr_dev_inst_new(SR_ST_ACTIVE, NULL, NULL, NULL);
+       in->sdi = sr_dev_inst_new();
        in->priv = inc = g_malloc0(sizeof(struct context));
 
        inc->single_column = g_variant_get_int32(g_hash_table_lookup(options, "single-column"));
@@ -506,7 +506,7 @@ static int initial_parse(const struct sr_input *in, GString *buf)
        }
        if (!lines[l]) {
                /* Not enough data for a proper line yet. */
-               ret = SR_OK_CONTINUE;
+               ret = SR_ERR_NA;
                goto out;
        }
 
@@ -557,7 +557,7 @@ static int initial_parse(const struct sr_input *in, GString *buf)
                if (inc->header && inc->multi_column_mode && strlen(columns[i]))
                        g_string_assign(channel_name, columns[i]);
                else
-                       g_string_printf(channel_name, "%lu", i);
+                       g_string_printf(channel_name, "%zu", i);
                ch = sr_channel_new(i, SR_CHANNEL_LOGIC, TRUE, channel_name->str);
                in->sdi->channels = g_slist_append(in->sdi->channels, ch);
        }
@@ -610,7 +610,7 @@ static int initial_receive(const struct sr_input *in)
        return ret;
 }
 
-static int receive(struct sr_input *in, GString *buf)
+static int process_buffer(struct sr_input *in)
 {
        struct sr_datafeed_packet packet;
        struct sr_datafeed_meta meta;
@@ -621,21 +621,7 @@ static int receive(struct sr_input *in, GString *buf)
        int max_columns, ret, l;
        char *p, **lines, **columns;
 
-       g_string_append_len(in->buf, buf->str, buf->len);
-
        inc = in->priv;
-       if (!inc->termination) {
-               if ((ret = initial_receive(in)) == SR_ERR_NA)
-                       /* Not enough data yet. */
-                       return SR_OK;
-               else if (ret != SR_OK)
-                       return SR_ERR;
-
-               /* sdi is ready, notify frontend. */
-               in->sdi_ready = TRUE;
-               return SR_OK;
-       }
-
        if (!inc->started) {
                std_session_send_df_header(in->sdi, LOG_PREFIX);
 
@@ -665,6 +651,7 @@ static int receive(struct sr_input *in, GString *buf)
        else
                max_columns = 1;
 
+       ret = SR_OK;
        lines = g_strsplit_set(in->buf->str, "\r\n", 0);
        for (l = 0; lines[l]; l++) {
                inc->line_number++;
@@ -680,6 +667,13 @@ static int receive(struct sr_input *in, GString *buf)
                        continue;
                }
 
+               /* Skip the header line, its content was used as the channel names. */
+               if (inc->header) {
+                       sr_spew("Header line %zu skipped.", inc->line_number);
+                       inc->header = FALSE;
+                       continue;
+               }
+
                if (!(columns = parse_line(lines[l], inc, max_columns))) {
                        sr_err("Error while parsing line %zu.", inc->line_number);
                        return SR_ERR;
@@ -723,24 +717,61 @@ static int receive(struct sr_input *in, GString *buf)
        g_strfreev(lines);
        g_string_erase(in->buf, 0, p - in->buf->str + 1);
 
-       return SR_OK;
+       return ret;
 }
 
-static int cleanup(struct sr_input *in)
+static int receive(struct sr_input *in, GString *buf)
 {
        struct context *inc;
-       struct sr_datafeed_packet packet;
+       int ret;
+
+       g_string_append_len(in->buf, buf->str, buf->len);
 
        inc = in->priv;
-       if (!inc)
+       if (!inc->termination) {
+               if ((ret = initial_receive(in)) == SR_ERR_NA)
+                       /* Not enough data yet. */
+                       return SR_OK;
+               else if (ret != SR_OK)
+                       return SR_ERR;
+
+               /* sdi is ready, notify frontend. */
+               in->sdi_ready = TRUE;
                return SR_OK;
+       }
+
+       ret = process_buffer(in);
+
+       return ret;
+}
+
+static int end(struct sr_input *in)
+{
+       struct context *inc;
+       struct sr_datafeed_packet packet;
+       int ret;
 
+       if (in->sdi_ready)
+               ret = process_buffer(in);
+       else
+               ret = SR_OK;
+
+       inc = in->priv;
        if (inc->started) {
                /* End of stream. */
                packet.type = SR_DF_END;
                sr_session_send(in->sdi, &packet);
        }
 
+       return ret;
+}
+
+static void cleanup(struct sr_input *in)
+{
+       struct context *inc;
+
+       inc = in->priv;
+
        if (inc->delimiter)
                g_string_free(inc->delimiter, TRUE);
 
@@ -752,11 +783,6 @@ static int cleanup(struct sr_input *in)
 
        if (inc->sample_buffer)
                g_free(inc->sample_buffer);
-
-       g_free(inc);
-       in->priv = NULL;
-
-       return SR_OK;
 }
 
 static struct sr_option options[] = {
@@ -798,5 +824,6 @@ SR_PRIV struct sr_input_module input_csv = {
        .format_match = format_match,
        .init = init,
        .receive = receive,
+       .end = end,
        .cleanup = cleanup,
 };