summaryrefslogtreecommitdiff
path: root/src/core
diff options
context:
space:
mode:
authorSimmo Saan <simmo.saan@gmail.com>2015-12-13 16:55:59 +0200
committerSimmo Saan <simmo.saan@gmail.com>2017-06-17 20:03:40 +0300
commit0a4be02dc368218f729eb118521885f30fa23a6d (patch)
treebc441fcba3731fcf25468f08ff2fdc262fc4ec9c /src/core
parent4563d43166ad13b1de9028db94d832a9e12a2b74 (diff)
downloadweechat-0a4be02dc368218f729eb118521885f30fa23a6d.zip
core: add hashtable_add_from_infolist to API
Diffstat (limited to 'src/core')
-rw-r--r--src/core/wee-hashtable.c92
-rw-r--r--src/core/wee-hashtable.h4
2 files changed, 96 insertions, 0 deletions
diff --git a/src/core/wee-hashtable.c b/src/core/wee-hashtable.c
index 15ebd14c4..d9f480c42 100644
--- a/src/core/wee-hashtable.c
+++ b/src/core/wee-hashtable.c
@@ -1062,6 +1062,8 @@ hashtable_add_to_infolist (struct t_hashtable *hashtable,
hashtable_to_string (hashtable->type_keys,
ptr_item->key)))
return 0;
+ /* TODO: implement other key types */
+
snprintf (option_name, sizeof (option_name),
"%s_value_%05d", prefix, item_number);
switch (hashtable->type_values)
@@ -1102,6 +1104,96 @@ hashtable_add_to_infolist (struct t_hashtable *hashtable,
}
/*
+ * Adds hashtable keys and values from an infolist.
+ *
+ * Returns:
+ * 1: OK
+ * 0: error
+ */
+
+int
+hashtable_add_from_infolist (struct t_hashtable *hashtable,
+ struct t_infolist *infolist,
+ const char *prefix)
+{
+ struct t_infolist_item *infolist_item;
+ struct t_infolist_var *ptr_name, *ptr_value;
+ void *value;
+ char prefix_name[128], option_value[128];
+ int prefix_length;
+
+ if (!hashtable || !infolist || !prefix)
+ return 0;
+
+ infolist_item = infolist->ptr_item;
+ if (!infolist_item)
+ return 0;
+
+ if (hashtable->type_keys != HASHTABLE_STRING)
+ return 0;
+ /* TODO: implement other key types */
+
+ snprintf (prefix_name, sizeof (prefix_name),
+ "%s_name_", prefix);
+ prefix_length = strlen (prefix_name);
+
+ for (ptr_name = infolist_item->vars; ptr_name; ptr_name = ptr_name->next_var)
+ {
+ if (string_strncasecmp (ptr_name->name, prefix_name, prefix_length) == 0)
+ {
+ snprintf (option_value, sizeof (option_value),
+ "%s_value_%s", prefix, ptr_name->name + prefix_length);
+
+ for (ptr_value = infolist_item->vars; ptr_value; ptr_value = ptr_value->next_var)
+ {
+ if (string_strcasecmp (ptr_value->name, option_value) == 0)
+ {
+ switch (hashtable->type_values)
+ {
+ case HASHTABLE_INTEGER:
+ if (ptr_value->type != INFOLIST_INTEGER)
+ return 0;
+
+ value = ptr_value->value;
+ break;
+ case HASHTABLE_STRING:
+ if (ptr_value->type != INFOLIST_STRING)
+ return 0;
+
+ value = ptr_value->value;
+ break;
+ case HASHTABLE_POINTER:
+ if (ptr_value->type != INFOLIST_POINTER)
+ return 0;
+
+ value = ptr_value->value;
+ break;
+ case HASHTABLE_BUFFER:
+ if (ptr_value->type != INFOLIST_BUFFER)
+ return 0;
+
+ value = ptr_value->value; /* TODO: implement size */
+ break;
+ case HASHTABLE_TIME:
+ if (ptr_value->type != INFOLIST_TIME)
+ return 0;
+
+ value = ptr_value->value;
+ break;
+ case HASHTABLE_NUM_TYPES:
+ break;
+ }
+ hashtable_set (hashtable, ptr_name->value, value);
+ break;
+ }
+ }
+ }
+ }
+
+ return 1;
+}
+
+/*
* Removes an item from hashtable.
*/
diff --git a/src/core/wee-hashtable.h b/src/core/wee-hashtable.h
index 38706d7cb..2ea414574 100644
--- a/src/core/wee-hashtable.h
+++ b/src/core/wee-hashtable.h
@@ -22,6 +22,7 @@
struct t_hashtable;
struct t_infolist_item;
+struct t_infolist;
typedef unsigned long long (t_hashtable_hash_key)(struct t_hashtable *hashtable,
const void *key);
@@ -149,6 +150,9 @@ extern void hashtable_set_pointer (struct t_hashtable *hashtable,
extern int hashtable_add_to_infolist (struct t_hashtable *hashtable,
struct t_infolist_item *infolist_item,
const char *prefix);
+extern int hashtable_add_from_infolist (struct t_hashtable *hashtable,
+ struct t_infolist *infolist,
+ const char *prefix);
extern void hashtable_remove (struct t_hashtable *hashtable, const void *key);
extern void hashtable_remove_all (struct t_hashtable *hashtable);
extern void hashtable_free (struct t_hashtable *hashtable);