diff options
-rw-r--r-- | src/pulse/message-params.c | 70 | ||||
-rw-r--r-- | src/pulse/message-params.h | 27 |
2 files changed, 62 insertions, 35 deletions
diff --git a/src/pulse/message-params.c b/src/pulse/message-params.c index 7d2dd8020..d5cafab3b 100644 --- a/src/pulse/message-params.c +++ b/src/pulse/message-params.c @@ -344,22 +344,24 @@ int pa_message_params_read_bool(char *c, bool *result, void **state) { return PA_MESSAGE_PARAMS_OK; } -/* Converts a parameter list to a string array. Escaping is removed from - * the strings. Returns an array of pointers to sub-strings within c in - * *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) { +/* Converts a parameter list to a string array. */ +int pa_message_params_read_string_array(char *c, const char ***results, int *length) { void *state = NULL; uint32_t element_count, i; int err; const char **values; pa_assert(results); + pa_assert(length); /* Count elements, return if no element was found or parse error. */ - if ((element_count = count_elements(c)) <= 0) - return element_count; + element_count = count_elements(c); + if (element_count < 0) { + return PA_MESSAGE_PARAMS_PARSE_ERROR; + } else if (element_count == 0) { + *length = 0; + return PA_MESSAGE_PARAMS_OK; + } /* Allocate array */ 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; - return element_count; + *length = element_count; + + return PA_MESSAGE_PARAMS_OK; } /* 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; void *state = NULL; uint32_t element_count, i; int err; pa_assert(results); + pa_assert(length); /* Count elements, return if no element was found or parse error. */ - if ((element_count = count_elements(c)) <= 0) - return element_count; + element_count = count_elements(c); + if (element_count < 0) { + return PA_MESSAGE_PARAMS_PARSE_ERROR; + } else if (element_count == 0) { + *length = 0; + return PA_MESSAGE_PARAMS_OK; + } /* Allocate array */ values = pa_xmalloc0(element_count * sizeof(double)); @@ -401,21 +411,29 @@ int pa_message_params_read_double_array(char *c, double **results) { } *results = values; - return element_count; + *length = element_count; + + return PA_MESSAGE_PARAMS_OK; } /* 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; void *state = NULL; uint32_t element_count, i; int err; pa_assert(results); + pa_assert(length); /* Count elements, return if no element was found or parse error. */ - if ((element_count = count_elements(c)) <= 0) - return element_count; + element_count = count_elements(c); + if (element_count < 0) { + return PA_MESSAGE_PARAMS_PARSE_ERROR; + } else if (element_count == 0) { + *length = 0; + return PA_MESSAGE_PARAMS_OK; + } /* Allocate array */ 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; - return element_count; + *length = element_count; + + return PA_MESSAGE_PARAMS_OK; } /* 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; void *state = NULL; uint32_t element_count, i; int err; pa_assert(results); + pa_assert(length); /* Count elements, return if no element was found or parse error. */ - if ((element_count = count_elements(c)) <= 0) - return element_count; + element_count = count_elements(c); + if (element_count < 0) { + return PA_MESSAGE_PARAMS_PARSE_ERROR; + } else if (element_count == 0) { + *length = 0; + return PA_MESSAGE_PARAMS_OK; + } /* Allocate array */ 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; - return element_count; + *length = element_count; + + return PA_MESSAGE_PARAMS_OK; } /* Write functions. The functions are wrapper functions around pa_strbuf, diff --git a/src/pulse/message-params.h b/src/pulse/message-params.h index 3d90eac3c..18addee96 100644 --- a/src/pulse/message-params.h +++ b/src/pulse/message-params.h @@ -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); /** 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 - * to NULL or to an array with default values. \since 15.0 */ -int pa_message_params_read_double_array(char *c, double **results); + * list are treated as error. Returns allocated array in *results and array size in *length. + * The returned array must be freed with pa_xfree(). \since 15.0 */ +int pa_message_params_read_double_array(char *c, double **results, int *length); /** Read an integer from parameter list in c. \since 15.0 */ 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 - * list are treated as error. Before the call, results must be initialized, either - * to NULL or to an array with default values. \since 15.0 */ -int pa_message_params_read_int64_array(char *c, int64_t **results); + * list are treated as error. Returns allocated array in *results and array size in *length. + * 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 *length); /** 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 */ @@ -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); /** 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 - * *results. The returned array must be freed, but not the strings - * within the array. Before the call, results must be initialized, either - * to NULL or to an array with default values. \since 15.0 */ -int pa_message_params_read_string_array(char *c, const char ***results); + * the strings. Returns allocated array of pointers to sub-strings within c in + * *results and stores array size in *length. The returned array must be + * freed with pa_xfree(), but not the strings within the array. \since 15.0 */ +int pa_message_params_read_string_array(char *c, const char ***results, int *length); /** 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); /** 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 - * to NULL or to an array with default values. \since 15.0 */ -int pa_message_params_read_uint64_array(char *c, uint64_t **results); + * list are treated as error. Returns allocated array in *results and array size in *length. + * 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 *length); /** @} */ |