summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNicolas Pitre <nico@fluxnic.net>2021-03-20 16:42:03 -0400
committerNicolas Pitre <nico@fluxnic.net>2021-03-20 20:01:22 -0400
commit5ab0f201ee0b5631939c30e323c2241007902dc8 (patch)
treede55bdc6b513864e25cd4b04c92257a055ac0878
parentff591ab004afd4451049f5c10b7c183ecf00c715 (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.c20
-rw-r--r--src/conf.h1
-rwxr-xr-xsrc/utils/dictionary.c25
-rwxr-xr-xsrc/utils/dictionary.h2
4 files changed, 46 insertions, 2 deletions
diff --git a/src/conf.c b/src/conf.c
index c152bdd..3142bd8 100644
--- a/src/conf.c
+++ b/src/conf.c
@@ -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);
+}
diff --git a/src/conf.h b/src/conf.h
index aa10d5a..57329ba 100644
--- a/src/conf.h
+++ b/src/conf.h
@@ -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);