]> sigrok.org Git - libsigrok.git/commitdiff
scpi: readability nits in vector getters, style nit in malloc call
authorGerhard Sittig <redacted>
Sun, 16 May 2021 17:51:52 +0000 (19:51 +0200)
committerGerhard Sittig <redacted>
Sat, 22 May 2021 06:06:58 +0000 (08:06 +0200)
Improve readability of SCPI uint8 and float vector get routines. Move
assignment and use of variables closer together to simplify review.
Allocate the glib array based on the text split result's length. Move
data processing to the "straight" path and handle failed conversion as
an exceptional condition in an error path.

Eliminate a redundant data type reference in a malloc call.

src/scpi/scpi.c

index 0f767f2a2de1c6862b61620d6e2dc0b9178c2f7d..ec0ddc555c73b37b6cdec6799ecf9573fee3534c 100644 (file)
@@ -863,28 +863,30 @@ SR_PRIV int sr_scpi_get_floatv(struct sr_scpi_dev_inst *scpi,
        float tmp;
        char *response;
        gchar **ptr, **tokens;
+       size_t token_count;
        GArray *response_array;
 
        *scpi_response = NULL;
-       response = NULL;
-       tokens = NULL;
 
+       response = NULL;
        ret = sr_scpi_get_string(scpi, command, &response);
        if (ret != SR_OK && !response)
                return ret;
 
        tokens = g_strsplit(response, ",", 0);
-       ptr = tokens;
+       token_count = g_strv_length(tokens);
 
-       response_array = g_array_sized_new(TRUE, FALSE, sizeof(float), 256);
+       response_array = g_array_sized_new(TRUE, FALSE,
+               sizeof(float), token_count + 1);
 
+       ptr = tokens;
        while (*ptr) {
-               if (sr_atof_ascii(*ptr, &tmp) == SR_OK)
-                       response_array = g_array_append_val(response_array,
-                                                           tmp);
-               else
+               ret = sr_atof_ascii(*ptr, &tmp);
+               if (ret != SR_OK) {
                        ret = SR_ERR_DATA;
-
+                       break;
+               }
+               response_array = g_array_append_val(response_array, tmp);
                ptr++;
        }
        g_strfreev(tokens);
@@ -920,28 +922,30 @@ SR_PRIV int sr_scpi_get_uint8v(struct sr_scpi_dev_inst *scpi,
        int tmp, ret;
        char *response;
        gchar **ptr, **tokens;
+       size_t token_count;
        GArray *response_array;
 
        *scpi_response = NULL;
-       response = NULL;
-       tokens = NULL;
 
+       response = NULL;
        ret = sr_scpi_get_string(scpi, command, &response);
        if (ret != SR_OK && !response)
                return ret;
 
        tokens = g_strsplit(response, ",", 0);
-       ptr = tokens;
+       token_count = g_strv_length(tokens);
 
-       response_array = g_array_sized_new(TRUE, FALSE, sizeof(uint8_t), 256);
+       response_array = g_array_sized_new(TRUE, FALSE,
+               sizeof(uint8_t), token_count + 1);
 
+       ptr = tokens;
        while (*ptr) {
-               if (sr_atoi(*ptr, &tmp) == SR_OK)
-                       response_array = g_array_append_val(response_array,
-                                                           tmp);
-               else
+               ret = sr_atoi(*ptr, &tmp);
+               if (ret != SR_OK) {
                        ret = SR_ERR_DATA;
-
+                       break;
+               }
+               response_array = g_array_append_val(response_array, tmp);
                ptr++;
        }
        g_strfreev(tokens);
@@ -1151,7 +1155,7 @@ SR_PRIV int sr_scpi_get_hw_id(struct sr_scpi_dev_inst *scpi,
        }
        g_free(response);
 
-       hw_info = g_malloc0(sizeof(struct sr_scpi_hw_info));
+       hw_info = g_malloc0(sizeof(*hw_info));
 
        idn_substr = g_strstr_len(tokens[0], -1, "IDN ");
        if (idn_substr == NULL)