]> sigrok.org Git - libsigrok.git/blobdiff - src/input/csv.c
Build: Set local include directories in Makefile.am
[libsigrok.git] / src / input / csv.c
index e72357bb6d7f7983c02fd486af9c60467c8e3273..cc09045aa281919a88580e681149724c7519300f 100644 (file)
@@ -20,7 +20,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <glib.h>
-#include "libsigrok.h"
+#include <libsigrok/libsigrok.h>
 #include "libsigrok-internal.h"
 
 #define LOG_PREFIX "input/csv"
@@ -351,7 +351,7 @@ static int parse_single_column(const char *column, struct context *inc)
 
        res = SR_ERR;
 
-       switch(inc->format) {
+       switch (inc->format) {
        case FORMAT_BIN:
                res = parse_binstr(column, inc);
                break;
@@ -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 = g_malloc0(sizeof(struct sr_dev_inst));
        in->priv = inc = g_malloc0(sizeof(struct context));
 
        inc->single_column = g_variant_get_int32(g_hash_table_lookup(options, "single-column"));
@@ -454,9 +454,9 @@ static int init(struct sr_input *in, GHashTable *options)
        return SR_OK;
 }
 
-static char *get_line_termination(GString *buf)
+static const char *get_line_termination(GString *buf)
 {
-       char *term;
+       const char *term;
 
        term = NULL;
        if (g_strstr_len(buf->str, buf->len, "\r\n"))
@@ -472,7 +472,6 @@ static char *get_line_termination(GString *buf)
 static int initial_parse(const struct sr_input *in, GString *buf)
 {
        struct context *inc;
-       struct sr_channel *ch;
        GString *channel_name;
        gsize num_columns, l, i;
        unsigned int line_number;
@@ -506,7 +505,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,9 +556,8 @@ 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);
-               ch = sr_channel_new(i, SR_CHANNEL_LOGIC, TRUE, channel_name->str);
-               in->sdi->channels = g_slist_append(in->sdi->channels, ch);
+                       g_string_printf(channel_name, "%zu", i);
+               sr_channel_new(in->sdi, i, SR_CHANNEL_LOGIC, TRUE, channel_name->str);
        }
        g_string_free(channel_name, TRUE);
 
@@ -583,7 +581,8 @@ static int initial_receive(const struct sr_input *in)
        struct context *inc;
        GString *new_buf;
        int len, ret;
-       char *termination, *p;
+       char *p;
+       const char *termination;
 
        inc = in->priv;
 
@@ -651,6 +650,7 @@ static int process_buffer(struct sr_input *in)
        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++;
@@ -666,6 +666,13 @@ static int process_buffer(struct sr_input *in)
                        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;
@@ -770,11 +777,8 @@ static void cleanup(struct sr_input *in)
        if (inc->comment)
                g_string_free(inc->comment, TRUE);
 
-       if (inc->termination)
-               g_free(inc->termination);
-
-       if (inc->sample_buffer)
-               g_free(inc->sample_buffer);
+       g_free(inc->termination);
+       g_free(inc->sample_buffer);
 }
 
 static struct sr_option options[] = {
@@ -811,6 +815,7 @@ SR_PRIV struct sr_input_module input_csv = {
        .id = "csv",
        .name = "CSV",
        .desc = "Comma-separated values",
+       .exts = (const char*[]){"csv", NULL},
        .metadata = { SR_INPUT_META_MIMETYPE },
        .options = get_options,
        .format_match = format_match,