message-params: add read length param reference to array read methods

Part-of: <https://gitlab.freedesktop.org/pulseaudio/pulseaudio/-/merge_requests/51>
This commit is contained in:
Igor V. Kovalenko 2020-11-30 22:39:23 +03:00 committed by Tanu Kaskinen
parent fe162e0bbd
commit 818a87deb2
2 changed files with 62 additions and 35 deletions

View file

@ -344,22 +344,24 @@ int pa_message_params_read_bool(char *c, bool *result, void **state) {
return PA_MESSAGE_PARAMS_OK; return PA_MESSAGE_PARAMS_OK;
} }
/* Converts a parameter list to a string array. Escaping is removed from /* Converts a parameter list to a string array. */
* the strings. Returns an array of pointers to sub-strings within c in int pa_message_params_read_string_array(char *c, const char ***results, int *length) {
* *results. The returned array must be freed, but not the strings
* within the array. The function returns the number of strings in the
* array. */
int pa_message_params_read_string_array(char *c, const char ***results) {
void *state = NULL; void *state = NULL;
uint32_t element_count, i; uint32_t element_count, i;
int err; int err;
const char **values; const char **values;
pa_assert(results); pa_assert(results);
pa_assert(length);
/* Count elements, return if no element was found or parse error. */ /* Count elements, return if no element was found or parse error. */
if ((element_count = count_elements(c)) <= 0) element_count = count_elements(c);
return element_count; if (element_count < 0) {
return PA_MESSAGE_PARAMS_PARSE_ERROR;
} else if (element_count == 0) {
*length = 0;
return PA_MESSAGE_PARAMS_OK;
}
/* Allocate array */ /* Allocate array */
values = pa_xmalloc0(element_count * sizeof(char *)); values = pa_xmalloc0(element_count * sizeof(char *));
@ -373,21 +375,29 @@ int pa_message_params_read_string_array(char *c, const char ***results) {
} }
*results = values; *results = values;
return element_count; *length = element_count;
return PA_MESSAGE_PARAMS_OK;
} }
/* Converts a parameter list to a double array. */ /* Converts a parameter list to a double array. */
int pa_message_params_read_double_array(char *c, double **results) { int pa_message_params_read_double_array(char *c, double **results, int *length) {
double *values; double *values;
void *state = NULL; void *state = NULL;
uint32_t element_count, i; uint32_t element_count, i;
int err; int err;
pa_assert(results); pa_assert(results);
pa_assert(length);
/* Count elements, return if no element was found or parse error. */ /* Count elements, return if no element was found or parse error. */
if ((element_count = count_elements(c)) <= 0) element_count = count_elements(c);
return element_count; if (element_count < 0) {
return PA_MESSAGE_PARAMS_PARSE_ERROR;
} else if (element_count == 0) {
*length = 0;
return PA_MESSAGE_PARAMS_OK;
}
/* Allocate array */ /* Allocate array */
values = pa_xmalloc0(element_count * sizeof(double)); values = pa_xmalloc0(element_count * sizeof(double));
@ -401,21 +411,29 @@ int pa_message_params_read_double_array(char *c, double **results) {
} }
*results = values; *results = values;
return element_count; *length = element_count;
return PA_MESSAGE_PARAMS_OK;
} }
/* Converts a parameter list to an int64 array. */ /* Converts a parameter list to an int64 array. */
int pa_message_params_read_int64_array(char *c, int64_t **results) { int pa_message_params_read_int64_array(char *c, int64_t **results, int *length) {
int64_t *values; int64_t *values;
void *state = NULL; void *state = NULL;
uint32_t element_count, i; uint32_t element_count, i;
int err; int err;
pa_assert(results); pa_assert(results);
pa_assert(length);
/* Count elements, return if no element was found or parse error. */ /* Count elements, return if no element was found or parse error. */
if ((element_count = count_elements(c)) <= 0) element_count = count_elements(c);
return element_count; if (element_count < 0) {
return PA_MESSAGE_PARAMS_PARSE_ERROR;
} else if (element_count == 0) {
*length = 0;
return PA_MESSAGE_PARAMS_OK;
}
/* Allocate array */ /* Allocate array */
values = pa_xmalloc0(element_count * sizeof(int64_t)); values = pa_xmalloc0(element_count * sizeof(int64_t));
@ -429,21 +447,29 @@ int pa_message_params_read_int64_array(char *c, int64_t **results) {
} }
*results = values; *results = values;
return element_count; *length = element_count;
return PA_MESSAGE_PARAMS_OK;
} }
/* Converts a parameter list to an uint64 array. */ /* Converts a parameter list to an uint64 array. */
int pa_message_params_read_uint64_array(char *c, uint64_t **results) { int pa_message_params_read_uint64_array(char *c, uint64_t **results, int *length) {
uint64_t *values; uint64_t *values;
void *state = NULL; void *state = NULL;
uint32_t element_count, i; uint32_t element_count, i;
int err; int err;
pa_assert(results); pa_assert(results);
pa_assert(length);
/* Count elements, return if no element was found or parse error. */ /* Count elements, return if no element was found or parse error. */
if ((element_count = count_elements(c)) <= 0) element_count = count_elements(c);
return element_count; if (element_count < 0) {
return PA_MESSAGE_PARAMS_PARSE_ERROR;
} else if (element_count == 0) {
*length = 0;
return PA_MESSAGE_PARAMS_OK;
}
/* Allocate array */ /* Allocate array */
values = pa_xmalloc0(element_count * sizeof(uint64_t)); values = pa_xmalloc0(element_count * sizeof(uint64_t));
@ -457,7 +483,9 @@ int pa_message_params_read_uint64_array(char *c, uint64_t **results) {
} }
*results = values; *results = values;
return element_count; *length = element_count;
return PA_MESSAGE_PARAMS_OK;
} }
/* Write functions. The functions are wrapper functions around pa_strbuf, /* Write functions. The functions are wrapper functions around pa_strbuf,

View file

@ -75,17 +75,17 @@ int pa_message_params_read_bool(char *c, bool *result, void **state);
int pa_message_params_read_double(char *c, double *result, void **state); int pa_message_params_read_double(char *c, double *result, void **state);
/** Converts a parameter list to a double array. Empty elements in the parameter /** Converts a parameter list to a double array. Empty elements in the parameter
* list are treated as error. Before the call, results must be initialized, either * list are treated as error. Returns allocated array in *results and array size in *length.
* to NULL or to an array with default values. \since 15.0 */ * The returned array must be freed with pa_xfree(). \since 15.0 */
int pa_message_params_read_double_array(char *c, double **results); int pa_message_params_read_double_array(char *c, double **results, int *length);
/** Read an integer from parameter list in c. \since 15.0 */ /** Read an integer from parameter list in c. \since 15.0 */
int pa_message_params_read_int64(char *c, int64_t *result, void **state); int pa_message_params_read_int64(char *c, int64_t *result, void **state);
/** Converts a parameter list to an int64 array. Empty elements in the parameter /** Converts a parameter list to an int64 array. Empty elements in the parameter
* list are treated as error. Before the call, results must be initialized, either * list are treated as error. Returns allocated array in *results and array size in *length.
* to NULL or to an array with default values. \since 15.0 */ * The returned array must be freed with pa_xfree(). \since 15.0 */
int pa_message_params_read_int64_array(char *c, int64_t **results); int pa_message_params_read_int64_array(char *c, int64_t **results, int *length);
/** Read raw data from parameter list in c. Used to split a message parameter /** Read raw data from parameter list in c. Used to split a message parameter
* string into list elements. The string returned in *result must not be freed. \since 15.0 */ * string into list elements. The string returned in *result must not be freed. \since 15.0 */
@ -96,19 +96,18 @@ int pa_message_params_read_raw(char *c, char **result, void **state);
int pa_message_params_read_string(char *c, const char **result, void **state); int pa_message_params_read_string(char *c, const char **result, void **state);
/** Convert a parameter list to a string array. Escaping is removed from /** Convert a parameter list to a string array. Escaping is removed from
* the strings. Returns an array of pointers to sub-strings within c in * the strings. Returns allocated array of pointers to sub-strings within c in
* *results. The returned array must be freed, but not the strings * *results and stores array size in *length. The returned array must be
* within the array. Before the call, results must be initialized, either * freed with pa_xfree(), but not the strings within the array. \since 15.0 */
* to NULL or to an array with default values. \since 15.0 */ int pa_message_params_read_string_array(char *c, const char ***results, int *length);
int pa_message_params_read_string_array(char *c, const char ***results);
/** Read an unsigned integer from parameter list in c. \since 15.0 */ /** Read an unsigned integer from parameter list in c. \since 15.0 */
int pa_message_params_read_uint64(char *c, uint64_t *result, void **state); int pa_message_params_read_uint64(char *c, uint64_t *result, void **state);
/** Converts a parameter list to an uint64 array. Empty elements in the parameter /** Converts a parameter list to an uint64 array. Empty elements in the parameter
* list are treated as error. Before the call, results must be initialized, either * list are treated as error. Returns allocated array in *results and array size in *length.
* to NULL or to an array with default values. \since 15.0 */ * The returned array must be freed with pa_xfree(). \since 15.0 */
int pa_message_params_read_uint64_array(char *c, uint64_t **results); int pa_message_params_read_uint64_array(char *c, uint64_t **results, int *length);
/** @} */ /** @} */