]> sigrok.org Git - libsigrok.git/blobdiff - src/input/csv.c
input/csv: Fix a false negative after successful import
[libsigrok.git] / src / input / csv.c
index db6f46767e9688a5eb8c4483f1d693f00a9c6542..5399027bf823305ab0a33c79b02ec9783e1ca5c3 100644 (file)
@@ -447,6 +447,8 @@ static int init(struct sr_input *in, GHashTable *options)
        return SR_OK;
 }
 
+static const char *delim_set = "\r\n";
+
 static const char *get_line_termination(GString *buf)
 {
        const char *term;
@@ -476,7 +478,7 @@ static int initial_parse(const struct sr_input *in, GString *buf)
        columns = NULL;
 
        line_number = 0;
-       lines = g_strsplit_set(buf->str, "\r\n", 0);
+       lines = g_strsplit_set(buf->str, delim_set, 0);
        for (l = 0; lines[l]; l++) {
                line_number++;
                line = lines[l];
@@ -636,22 +638,29 @@ static int process_buffer(struct sr_input *in)
                inc->started = TRUE;
        }
 
-       p = g_strrstr_len(in->buf->str, in->buf->len, inc->termination);
-       if (!p)
-               /* Don't have a full line. */
-               return SR_ERR;
-
-       *p = '\0';
-       g_strstrip(in->buf->str);
-
        /* Limit the number of columns to parse. */
        if (inc->multi_column_mode)
                max_columns = inc->num_channels;
        else
                max_columns = 1;
 
+       /*
+        * Consider empty input non-fatal. Keep accumulating input until
+        * at least one full text line has become available. Grab the
+        * maximum amount of accumulated data that consists of full text
+        * lines, and process what has been received so far, leaving not
+        * yet complete lines for the next invocation.
+        */
+       if (!in->buf->len)
+               return SR_OK;
+       p = g_strrstr_len(in->buf->str, in->buf->len, inc->termination);
+       if (!p)
+               return SR_ERR;
+       *p = '\0';
+       g_strstrip(in->buf->str);
+
        ret = SR_OK;
-       lines = g_strsplit_set(in->buf->str, "\r\n", 0);
+       lines = g_strsplit_set(in->buf->str, delim_set, 0);
        for (l = 0; lines[l]; l++) {
                inc->line_number++;
                line = lines[l];