diff options
author | Arun Raghavan <arun.raghavan@collabora.co.uk> | 2012-02-06 16:18:49 +0530 |
---|---|---|
committer | Arun Raghavan <arun.raghavan@collabora.co.uk> | 2012-03-05 20:00:31 +0530 |
commit | 59af940dd5990588ccfd593efab71c3007ea632e (patch) | |
tree | e900cc98b9ff5b329dd8fcdbf387becdea082ace /src/pulse | |
parent | c60f698f1e1614309b0a568a20da840409adad2f (diff) |
format: Add more property getters
This adds integer range/array and string array property getters to the
pa_format_info API. Corresponding tests added as well to ensure the code
is valgrind-clean.
The corresponding functions are added to map-file manually for now.
Diffstat (limited to 'src/pulse')
-rw-r--r-- | src/pulse/format.c | 137 | ||||
-rw-r--r-- | src/pulse/format.h | 14 |
2 files changed, 151 insertions, 0 deletions
diff --git a/src/pulse/format.c b/src/pulse/format.c index 6e8e7079..c0a96db4 100644 --- a/src/pulse/format.c +++ b/src/pulse/format.c @@ -327,6 +327,90 @@ int pa_format_info_get_prop_int(pa_format_info *f, const char *key, int *v) { return 0; } +int pa_format_info_get_prop_int_range(pa_format_info *f, const char *key, int *min, int *max) { + const char *str; + json_object *o, *o1; + int ret = -PA_ERR_INVALID; + + pa_assert(f); + pa_assert(key); + pa_assert(min); + pa_assert(max); + + str = pa_proplist_gets(f->plist, key); + if (!str) + return -PA_ERR_NOENTITY; + + o = json_tokener_parse(str); + if (is_error(o)) + return -PA_ERR_INVALID; + + if (json_object_get_type(o) != json_type_object) + goto out; + + if (!(o1 = json_object_object_get(o, PA_JSON_MIN_KEY))) + goto out; + + *min = json_object_get_int(o1); + json_object_put(o1); + + if (!(o1 = json_object_object_get(o, PA_JSON_MAX_KEY))) + goto out; + + *max = json_object_get_int(o1); + json_object_put(o1); + + ret = 0; + +out: + json_object_put(o); + return ret; +} + +int pa_format_info_get_prop_int_array(pa_format_info *f, const char *key, int **values, int *n_values) +{ + const char *str; + json_object *o, *o1; + int i, ret = -PA_ERR_INVALID; + + pa_assert(f); + pa_assert(key); + pa_assert(values); + pa_assert(n_values); + + str = pa_proplist_gets(f->plist, key); + if (!str) + return -PA_ERR_NOENTITY; + + o = json_tokener_parse(str); + if (is_error(o)) + return -PA_ERR_INVALID; + + if (json_object_get_type(o) != json_type_array) + goto out; + + *n_values = json_object_array_length(o); + *values = pa_xnew(int, *n_values); + + for (i = 0; i < *n_values; i++) { + o1 = json_object_array_get_idx(o, i); + + if (json_object_get_type(o1) != json_type_int) { + json_object_put(o1); + goto out; + } + + (*values)[i] = json_object_get_int(o1); + json_object_put(o1); + } + + ret = 0; + +out: + json_object_put(o); + return ret; +} + int pa_format_info_get_prop_string(pa_format_info *f, const char *key, char **v) { const char *str = NULL; json_object *o; @@ -354,6 +438,59 @@ int pa_format_info_get_prop_string(pa_format_info *f, const char *key, char **v) return 0; } +int pa_format_info_get_prop_string_array(pa_format_info *f, const char *key, char ***values, int *n_values) +{ + const char *str; + json_object *o, *o1; + int i, ret = -PA_ERR_INVALID; + + pa_assert(f); + pa_assert(key); + pa_assert(values); + pa_assert(n_values); + + str = pa_proplist_gets(f->plist, key); + if (!str) + return -PA_ERR_NOENTITY; + + o = json_tokener_parse(str); + if (is_error(o)) + return -PA_ERR_INVALID; + + if (json_object_get_type(o) != json_type_array) + goto out; + + *n_values = json_object_array_length(o); + *values = pa_xnew(char *, *n_values); + + for (i = 0; i < *n_values; i++) { + o1 = json_object_array_get_idx(o, i); + + if (json_object_get_type(o1) != json_type_string) { + json_object_put(o1); + goto out; + } + + (*values)[i] = pa_xstrdup(json_object_get_string(o1)); + json_object_put(o1); + } + + ret = 0; + +out: + json_object_put(o); + return ret; +} + +void pa_format_info_free_string_array(char **values, int n_values) { + int i; + + for (i = 0; i < n_values; i++) + pa_xfree(values[i]); + + pa_xfree(values); +} + void pa_format_info_set_prop_int(pa_format_info *f, const char *key, int value) { json_object *o; diff --git a/src/pulse/format.h b/src/pulse/format.h index fd5727bc..5f710449 100644 --- a/src/pulse/format.h +++ b/src/pulse/format.h @@ -124,9 +124,23 @@ int pa_format_info_to_sample_spec(pa_format_info *f, pa_sample_spec *ss, pa_chan /** Gets an integer property from the given format info. Returns 0 on success and a negative integer on failure. \since 2.0 */ int pa_format_info_get_prop_int(pa_format_info *f, const char *key, int *v); +/** Gets an integer range property from the given format info. Returns 0 on success and a negative integer on failure. + * \since 2.0 */ +int pa_format_info_get_prop_int_range(pa_format_info *f, const char *key, int *min, int *max); +/** Gets an integer array property from the given format info. \a values contains the values and \a n_values contains the + * number of elements. The caller must free \a values using \ref pa_xfree. Returns 0 on success and a negative integer on + * failure. \since 2.0 */ +int pa_format_info_get_prop_int_array(pa_format_info *f, const char *key, int **values, int *n_values); /** Gets a string property from the given format info. The caller must free the returned string using \ref pa_xfree. Returns * 0 on success and a negative integer on failure. \since 2.0 */ int pa_format_info_get_prop_string(pa_format_info *f, const char *key, char **v); +/** Gets a string array property from the given format info. \a values contains the values and \a n_values contains + * the number of elements. The caller must free \a values using \ref pa_format_info_free_string_array. Returns 0 on success and + * a negative integer on failure. \since 2.0 */ +int pa_format_info_get_prop_string_array(pa_format_info *f, const char *key, char ***values, int *n_values); + +/** Frees a string array returned by \ref pa_format_info_get_prop_string_array. \since 2.0 */ +void pa_format_info_free_string_array(char **values, int n_values); /** Sets an integer property on the given format info. \since 1.0 */ void pa_format_info_set_prop_int(pa_format_info *f, const char *key, int value); |