scpi: readability nits in vector getters, style nit in malloc call

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.
This commit is contained in:
Gerhard Sittig 2021-05-16 19:51:52 +02:00
parent 70158398f3
commit 191af3d9ca
1 changed files with 25 additions and 21 deletions

View 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);
token_count = g_strv_length(tokens);
response_array = g_array_sized_new(TRUE, FALSE,
sizeof(float), token_count + 1);
ptr = tokens;
response_array = g_array_sized_new(TRUE, FALSE, sizeof(float), 256);
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);
token_count = g_strv_length(tokens);
response_array = g_array_sized_new(TRUE, FALSE,
sizeof(uint8_t), token_count + 1);
ptr = tokens;
response_array = g_array_sized_new(TRUE, FALSE, sizeof(uint8_t), 256);
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)