diff options
author | Nicolas Pitre <nico@fluxnic.net> | 2021-03-20 16:42:03 -0400 |
---|---|---|
committer | Nicolas Pitre <nico@fluxnic.net> | 2021-03-20 20:01:22 -0400 |
commit | 5ab0f201ee0b5631939c30e323c2241007902dc8 (patch) | |
tree | de55bdc6b513864e25cd4b04c92257a055ac0878 | |
parent | ff591ab004afd4451049f5c10b7c183ecf00c715 (diff) |
store dictionary values as both a string and an integer value
This will allow for not doing atoi() over and over everywhere.
-rw-r--r-- | src/conf.c | 20 | ||||
-rw-r--r-- | src/conf.h | 1 | ||||
-rwxr-xr-x | src/utils/dictionary.c | 25 | ||||
-rwxr-xr-x | src/utils/dictionary.h | 2 |
4 files changed, 46 insertions, 2 deletions
@@ -153,7 +153,7 @@ char * get_conf_values(char * salida) { } /** - * \brief Retreive the value of a given key in user_conf_d + * \brief Retreive the string value of a given key in user_conf_d * * \details This function will look for a given key in the user_conf_d * dictionary. If the key is found it will return the value of that @@ -169,3 +169,21 @@ char * get_conf_values(char * salida) { char * get_conf_value(char * key) { return get(user_conf_d, key); } + +/** + * \brief Retreive the integer value of a given key in user_conf_d + * + * \details This function will look for a given key in the user_conf_d + * dictionary. If the key is found it will return the value of that + * dictionary entry, or 0 otherwise. + * + * \param[in] key The key to search for in user_conf_d + * + * \return key value + */ +// TODO Make this function take a pointer to a dictionary as an +// argument rather than using user_conf_d directly. + +int get_conf_int(char * key) { + return get_int(user_conf_d, key); +} @@ -46,4 +46,5 @@ extern struct dictionary * user_conf_d; void store_default_config_values(); char * get_conf_value(char * key); +int get_conf_int(char * key); char * get_conf_values(char * salida); diff --git a/src/utils/dictionary.c b/src/utils/dictionary.c index 4c777ef..c75e4f3 100755 --- a/src/utils/dictionary.c +++ b/src/utils/dictionary.c @@ -86,6 +86,7 @@ void put(struct dictionary * d, char * k, char * v) { // If an existing key is inserted, the value is overwritten. free(nl->val); nl->val = strdup(v); + nl->intval = atoi(v); return; } @@ -93,6 +94,7 @@ void put(struct dictionary * d, char * k, char * v) { nl = malloc(sizeof(struct nlist)); nl->key = strdup(k); nl->val = strdup(v); + nl->intval = atoi(v); nl->next = *p_nl; *p_nl = nl; d->len++; @@ -143,7 +145,7 @@ int get_dict_buffer_size(struct dictionary * d) { } /** - * \brief Get the value for KEY + * \brief Get the string value for KEY * * \param[in] d * \param[in] key @@ -163,6 +165,27 @@ char * get(struct dictionary * d, char * key) { return NULL; } +/** + * \brief Get the integer value for KEY + * + * \param[in] d + * \param[in] key + * + * \return value for the key + */ + +int get_int(struct dictionary * d, char * key) { + struct nlist * nl; + + for (nl = d->list; nl != NULL; nl = nl->next) { + int cmp = strcmp(key, nl->key); + if (cmp > 0) continue; + if (cmp < 0) break; + return nl->intval; + } + return 0; +} + /* Get the key name from a value char * get_key_name(struct dictionary * d, char * value) { struct nlist * nl; diff --git a/src/utils/dictionary.h b/src/utils/dictionary.h index af866a3..899dd9e 100755 --- a/src/utils/dictionary.h +++ b/src/utils/dictionary.h @@ -50,6 +50,7 @@ struct dictionary { struct nlist { char * key; char * val; + int intval; struct nlist * next; }; @@ -57,6 +58,7 @@ struct dictionary * create_dictionary(); void put(struct dictionary * d, char * k, char * v); void destroy_dictionary(struct dictionary * d); char * get(struct dictionary * d, char * key); +int get_int(struct dictionary * d, char * key); //char * get_key_name(struct dictionary * d, char * value); void parse_str(struct dictionary * d, char * str, int blank_space); int get_dict_buffer_size(struct dictionary * d); |