diff options
author | Harshavardhana <harsha@zresearch.com> | 2009-02-19 10:26:17 -0800 |
---|---|---|
committer | Anand V. Avati <avati@amp.gluster.com> | 2009-02-21 21:07:31 +0530 |
commit | 9679f8db65de29a40f622c12c2cc538d70b052b2 (patch) | |
tree | 5d77cf2519d347528cad765064d1a476e44426b8 /libglusterfs/src/dict.c | |
parent | e9ac5f587763b48acc19268ce57e6bfd886a0561 (diff) |
new functions dict_{get,set}_double for float/double value
fixed warning
Signed-off-by: Anand V. Avati <avati@amp.gluster.com>
Diffstat (limited to 'libglusterfs/src/dict.c')
-rw-r--r-- | libglusterfs/src/dict.c | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/libglusterfs/src/dict.c b/libglusterfs/src/dict.c index eb181f191..02c9e6062 100644 --- a/libglusterfs/src/dict.c +++ b/libglusterfs/src/dict.c @@ -810,6 +810,31 @@ data_from_uint64 (uint64_t value) return data; } +static data_t * +data_from_double (double value) +{ + data_t *data = NULL; + int ret = 0; + + data = get_new_data (); + + if (!data) { + gf_log ("dict", GF_LOG_CRITICAL, + "@data - NULL returned by CALLOC"); + return NULL; + } + + ret = asprintf (&data->data, "%f", value); + if (ret == -1) { + gf_log ("dict", GF_LOG_CRITICAL, + "@data - allocation failed by ASPRINTF"); + return NULL; + } + data->len = strlen (data->data) + 1; + + return data; +} + data_t * data_from_uint32 (uint32_t value) @@ -1410,6 +1435,34 @@ err: return ret; } +static int +_data_to_double (data_t *data, double *val) +{ + int ret = 0; + char * str = NULL; + + if (!data || !val) { + ret = -EINVAL; + goto err; + } + + str = alloca (data->len + 1); + if (!str) { + ret = -ENOMEM; + goto err; + } + memcpy (str, data->data, data->len); + str[data->len] = '\0'; + + errno = 0; + *val = strtod (str, NULL); + if (errno != 0) + ret = -errno; + +err: + return ret; +} + int dict_get_int8 (dict_t *this, char *key, int8_t *val) { @@ -1650,6 +1703,7 @@ err: } + int dict_set_uint32 (dict_t *this, char *key, uint32_t val) { @@ -1712,6 +1766,48 @@ err: } int +dict_get_double (dict_t *this, char *key, double *val) +{ + data_t *data = NULL; + int ret = 0; + + if (!this || !key || !val) { + ret = -EINVAL; + goto err; + } + + ret = dict_get_with_ref (this, key, &data); + if (ret != 0) { + goto err; + } + + ret = _data_to_double (data, val); + +err: + if (data) + data_unref (data); + return ret; +} + +int +dict_set_double (dict_t *this, char *key, double val) +{ + data_t * data = NULL; + int ret = 0; + + data = data_from_double (val); + if (!data) { + ret = -EINVAL; + goto err; + } + + ret = dict_set (this, key, data); + +err: + return ret; +} + +int dict_set_static_ptr (dict_t *this, char *key, void *ptr) { data_t * data = NULL; |