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.
float tmp;
char *response;
gchar **ptr, **tokens;
float tmp;
char *response;
gchar **ptr, **tokens;
GArray *response_array;
*scpi_response = NULL;
GArray *response_array;
*scpi_response = NULL;
- response = NULL;
- tokens = NULL;
ret = sr_scpi_get_string(scpi, command, &response);
if (ret != SR_OK && !response)
return ret;
tokens = g_strsplit(response, ",", 0);
ret = sr_scpi_get_string(scpi, command, &response);
if (ret != SR_OK && !response)
return ret;
tokens = g_strsplit(response, ",", 0);
+ 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);
- 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) {
+ break;
+ }
+ response_array = g_array_append_val(response_array, tmp);
ptr++;
}
g_strfreev(tokens);
ptr++;
}
g_strfreev(tokens);
int tmp, ret;
char *response;
gchar **ptr, **tokens;
int tmp, ret;
char *response;
gchar **ptr, **tokens;
GArray *response_array;
*scpi_response = NULL;
GArray *response_array;
*scpi_response = NULL;
- response = NULL;
- tokens = NULL;
ret = sr_scpi_get_string(scpi, command, &response);
if (ret != SR_OK && !response)
return ret;
tokens = g_strsplit(response, ",", 0);
ret = sr_scpi_get_string(scpi, command, &response);
if (ret != SR_OK && !response)
return ret;
tokens = g_strsplit(response, ",", 0);
+ 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);
- 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) {
+ break;
+ }
+ response_array = g_array_append_val(response_array, tmp);
ptr++;
}
g_strfreev(tokens);
ptr++;
}
g_strfreev(tokens);
- 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)
idn_substr = g_strstr_len(tokens[0], -1, "IDN ");
if (idn_substr == NULL)