summaryrefslogtreecommitdiff
path: root/src/core
diff options
context:
space:
mode:
Diffstat (limited to 'src/core')
-rw-r--r--src/core/wee-hashtable.c266
-rw-r--r--src/core/wee-hashtable.h6
-rw-r--r--src/core/wee-hdata.c24
-rw-r--r--src/core/wee-hdata.h2
4 files changed, 143 insertions, 155 deletions
diff --git a/src/core/wee-hashtable.c b/src/core/wee-hashtable.c
index 90eaa8a43..f712e5459 100644
--- a/src/core/wee-hashtable.c
+++ b/src/core/wee-hashtable.c
@@ -423,6 +423,46 @@ hashtable_has_key (struct t_hashtable *hashtable, const void *key)
}
/*
+ * hashtable_to_string: convert a value (from any type) to a string
+ * Value returned is a pointer to a static buffer (except
+ * if type is string, then pointer to string itself is
+ * returned) and must be used immediately, it is
+ * overwritten by subsequent calls to this function.
+ */
+
+const char *
+hashtable_to_string (enum t_hashtable_type type, const void *value)
+{
+ static char str_value[128];
+
+ switch (type)
+ {
+ case HASHTABLE_INTEGER:
+ snprintf (str_value, sizeof (str_value), "%d", *((int *)value));
+ return str_value;
+ break;
+ case HASHTABLE_STRING:
+ return (const char *)value;
+ break;
+ case HASHTABLE_POINTER:
+ case HASHTABLE_BUFFER:
+ snprintf (str_value, sizeof (str_value),
+ "0x%lx", (long unsigned int)value);
+ return str_value;
+ break;
+ case HASHTABLE_TIME:
+ snprintf (str_value, sizeof (str_value),
+ "%ld", (long)(*((time_t *)value)));
+ return str_value;
+ break;
+ case HASHTABLE_NUM_TYPES:
+ break;
+ }
+
+ return NULL;
+}
+
+/*
* hashtable_map: call a function on all hashtable entries
*/
@@ -455,6 +495,54 @@ hashtable_map (struct t_hashtable *hashtable,
}
/*
+ * hashtable_map_string: call a function on all hashtable entries,
+ * sending keys and values as strings
+ */
+
+void
+hashtable_map_string (struct t_hashtable *hashtable,
+ t_hashtable_map_string *callback_map,
+ void *callback_map_data)
+{
+ int i;
+ struct t_hashtable_item *ptr_item, *ptr_next_item;
+ const char *str_key, *str_value;
+ char *key, *value;
+
+ if (!hashtable)
+ return;
+
+ for (i = 0; i < hashtable->size; i++)
+ {
+ ptr_item = hashtable->htable[i];
+ while (ptr_item)
+ {
+ ptr_next_item = ptr_item->next_item;
+
+ str_key = hashtable_to_string (hashtable->type_keys,
+ ptr_item->key);
+ key = (str_key) ? strdup (str_key) : NULL;
+
+ str_value = hashtable_to_string (hashtable->type_values,
+ ptr_item->value);
+ value = (str_value) ? strdup (str_value) : NULL;
+
+ (void) (callback_map) (callback_map_data,
+ hashtable,
+ key,
+ value);
+
+ if (key)
+ free (key);
+ if (value)
+ free (value);
+
+ ptr_item = ptr_next_item;
+ }
+ }
+}
+
+/*
* hashtable_duplicate_map_cb: function called for each variable in hashtable
* to duplicate all keys
*/
@@ -510,7 +598,6 @@ hashtable_get_list_keys_map_cb (void *data,
const void *key, const void *value)
{
struct t_weelist *list;
- char str_key[128];
/* make C compiler happy */
(void) hashtable;
@@ -518,27 +605,8 @@ hashtable_get_list_keys_map_cb (void *data,
list = (struct t_weelist *)data;
- switch (hashtable->type_keys)
- {
- case HASHTABLE_INTEGER:
- snprintf (str_key, sizeof (str_key), "%d", *((int *)key));
- weelist_add (list, str_key, WEECHAT_LIST_POS_SORT, NULL);
- break;
- case HASHTABLE_STRING:
- weelist_add (list, (const char *)key, WEECHAT_LIST_POS_SORT, NULL);
- break;
- case HASHTABLE_POINTER:
- case HASHTABLE_BUFFER:
- snprintf (str_key, sizeof (str_key), "0x%lx", (long unsigned int)key);
- weelist_add (list, str_key, WEECHAT_LIST_POS_SORT, NULL);
- break;
- case HASHTABLE_TIME:
- snprintf (str_key, sizeof (str_key), "%ld", (long)(*((time_t *)key)));
- weelist_add (list, str_key, WEECHAT_LIST_POS_SORT, NULL);
- break;
- case HASHTABLE_NUM_TYPES:
- break;
- }
+ weelist_add (list, hashtable_to_string (hashtable->type_keys, key),
+ WEECHAT_LIST_POS_SORT, NULL);
}
/*
@@ -584,7 +652,7 @@ hashtable_compute_length_keys_cb (void *data,
struct t_hashtable *hashtable,
const void *key, const void *value)
{
- char str_value[128];
+ const char *str_key;
int *length;
/* make C compiler happy */
@@ -592,27 +660,9 @@ hashtable_compute_length_keys_cb (void *data,
length = (int *)data;
- switch (hashtable->type_keys)
- {
- case HASHTABLE_INTEGER:
- snprintf (str_value, sizeof (str_value), "%d", *((int *)key));
- *length += strlen (str_value) + 1;
- break;
- case HASHTABLE_STRING:
- *length += strlen ((char *)key) + 1;
- break;
- case HASHTABLE_POINTER:
- case HASHTABLE_BUFFER:
- snprintf (str_value, sizeof (str_value), "0x%lx", (long unsigned int)key);
- *length += strlen (str_value) + 1;
- break;
- case HASHTABLE_TIME:
- snprintf (str_value, sizeof (str_value), "%ld", (long)(*((time_t *)key)));
- *length += strlen (str_value) + 1;
- break;
- case HASHTABLE_NUM_TYPES:
- break;
- }
+ str_key = hashtable_to_string (hashtable->type_keys, key);
+ if (str_key)
+ *length += strlen (str_key) + 1;
}
/*
@@ -624,7 +674,7 @@ hashtable_compute_length_values_cb (void *data,
struct t_hashtable *hashtable,
const void *key, const void *value)
{
- char str_value[128];
+ const char *str_value;
int *length;
/* make C compiler happy */
@@ -634,27 +684,9 @@ hashtable_compute_length_values_cb (void *data,
if (value)
{
- switch (hashtable->type_values)
- {
- case HASHTABLE_INTEGER:
- snprintf (str_value, sizeof (str_value), "%d", *((int *)value));
- *length += strlen (str_value) + 1;
- break;
- case HASHTABLE_STRING:
- *length += strlen ((char *)value) + 1;
- break;
- case HASHTABLE_POINTER:
- case HASHTABLE_BUFFER:
- snprintf (str_value, sizeof (str_value), "0x%lx", (long unsigned int)value);
- *length += strlen (str_value) + 1;
- break;
- case HASHTABLE_TIME:
- snprintf (str_value, sizeof (str_value), "%ld", (long)(*((time_t *)value)));
- *length += strlen (str_value) + 1;
- break;
- case HASHTABLE_NUM_TYPES:
- break;
- }
+ str_value = hashtable_to_string (hashtable->type_values, value);
+ if (str_value)
+ *length += strlen (str_value) + 1;
}
else
{
@@ -684,7 +716,7 @@ hashtable_build_string_keys_cb (void *data,
struct t_hashtable *hashtable,
const void *key, const void *value)
{
- char str_value[128];
+ const char *str_key;
char *str;
/* make C compiler happy */
@@ -695,27 +727,9 @@ hashtable_build_string_keys_cb (void *data,
if (str[0])
strcat (str, ",");
- switch (hashtable->type_keys)
- {
- case HASHTABLE_INTEGER:
- snprintf (str_value, sizeof (str_value), "%d", *((int *)key));
- strcat (str, str_value);
- break;
- case HASHTABLE_STRING:
- strcat (str, (char *)key);
- break;
- case HASHTABLE_POINTER:
- case HASHTABLE_BUFFER:
- snprintf (str_value, sizeof (str_value), "0x%lx", (long unsigned int)key);
- strcat (str, str_value);
- break;
- case HASHTABLE_TIME:
- snprintf (str_value, sizeof (str_value), "%ld", (long)(*((time_t *)key)));
- strcat (str, str_value);
- break;
- case HASHTABLE_NUM_TYPES:
- break;
- }
+ str_key = hashtable_to_string (hashtable->type_keys, key);
+ if (str_key)
+ strcat (str, str_key);
}
/*
@@ -727,7 +741,7 @@ hashtable_build_string_values_cb (void *data,
struct t_hashtable *hashtable,
const void *key, const void *value)
{
- char str_value[128];
+ const char *str_value;
char *str;
/* make C compiler happy */
@@ -740,27 +754,9 @@ hashtable_build_string_values_cb (void *data,
if (value)
{
- switch (hashtable->type_values)
- {
- case HASHTABLE_INTEGER:
- snprintf (str_value, sizeof (str_value), "%d", *((int *)value));
- strcat (str, str_value);
- break;
- case HASHTABLE_STRING:
- strcat (str, (char *)value);
- break;
- case HASHTABLE_POINTER:
- case HASHTABLE_BUFFER:
- snprintf (str_value, sizeof (str_value), "0x%lx", (long unsigned int)value);
- strcat (str, str_value);
- break;
- case HASHTABLE_TIME:
- snprintf (str_value, sizeof (str_value), "%ld", (long)(*((time_t *)value)));
- strcat (str, str_value);
- break;
- case HASHTABLE_NUM_TYPES:
- break;
- }
+ str_value = hashtable_to_string (hashtable->type_values, value);
+ if (str_value)
+ strcat (str, str_value);
}
else
{
@@ -777,7 +773,7 @@ hashtable_build_string_keys_values_cb (void *data,
struct t_hashtable *hashtable,
const void *key, const void *value)
{
- char str_value[128];
+ const char *str_key, *str_value;
char *str;
str = (char *)data;
@@ -785,53 +781,17 @@ hashtable_build_string_keys_values_cb (void *data,
if (str[0])
strcat (str, ",");
- switch (hashtable->type_keys)
- {
- case HASHTABLE_INTEGER:
- snprintf (str_value, sizeof (str_value), "%d", *((int *)key));
- strcat (str, str_value);
- break;
- case HASHTABLE_STRING:
- strcat (str, (char *)key);
- break;
- case HASHTABLE_POINTER:
- case HASHTABLE_BUFFER:
- snprintf (str_value, sizeof (str_value), "0x%lx", (long unsigned int)key);
- strcat (str, str_value);
- break;
- case HASHTABLE_TIME:
- snprintf (str_value, sizeof (str_value), "%ld", (long)(*((time_t *)key)));
- strcat (str, str_value);
- break;
- case HASHTABLE_NUM_TYPES:
- break;
- }
+ str_key = hashtable_to_string (hashtable->type_keys, key);
+ if (str_key)
+ strcat (str, str_key);
strcat (str, ":");
if (value)
{
- switch (hashtable->type_values)
- {
- case HASHTABLE_INTEGER:
- snprintf (str_value, sizeof (str_value), "%d", *((int *)value));
- strcat (str, str_value);
- break;
- case HASHTABLE_STRING:
- strcat (str, (char *)value);
- break;
- case HASHTABLE_POINTER:
- case HASHTABLE_BUFFER:
- snprintf (str_value, sizeof (str_value), "0x%lx", (long unsigned int)value);
- strcat (str, str_value);
- break;
- case HASHTABLE_TIME:
- snprintf (str_value, sizeof (str_value), "%ld", (long)(*((time_t *)value)));
- strcat (str, str_value);
- break;
- case HASHTABLE_NUM_TYPES:
- break;
- }
+ str_value = hashtable_to_string (hashtable->type_values, value);
+ if (str_value)
+ strcat (str, str_value);
}
else
{
diff --git a/src/core/wee-hashtable.h b/src/core/wee-hashtable.h
index 4c8eecf2f..726e00077 100644
--- a/src/core/wee-hashtable.h
+++ b/src/core/wee-hashtable.h
@@ -32,6 +32,9 @@ typedef void (t_hashtable_free_value)(struct t_hashtable *hashtable,
typedef void (t_hashtable_map)(void *data,
struct t_hashtable *hashtable,
const void *key, const void *value);
+typedef void (t_hashtable_map_string)(void *data,
+ struct t_hashtable *hashtable,
+ const char *key, const char *value);
/*
* Hashtable is a structure with an array "htable", each entry is a pointer
@@ -121,6 +124,9 @@ extern int hashtable_has_key (struct t_hashtable *hashtable, const void *key);
extern void hashtable_map (struct t_hashtable *hashtable,
t_hashtable_map *callback_map,
void *callback_map_data);
+extern void hashtable_map_string (struct t_hashtable *hashtable,
+ t_hashtable_map_string *callback_map,
+ void *callback_map_data);
extern struct t_hashtable *hashtable_dup (struct t_hashtable *hashtable);
struct t_weelist *hashtable_get_list_keys (struct t_hashtable *hashtable);
extern int hashtable_get_integer (struct t_hashtable *hashtable,
diff --git a/src/core/wee-hdata.c b/src/core/wee-hdata.c
index 2204af014..9e92dd8cb 100644
--- a/src/core/wee-hdata.c
+++ b/src/core/wee-hdata.c
@@ -38,8 +38,9 @@
struct t_hashtable *weechat_hdata = NULL;
-char *hdata_type_string[7] =
-{ "other", "char", "integer", "long", "string", "pointer", "time" };
+char *hdata_type_string[8] =
+{ "other", "char", "integer", "long", "string", "pointer", "time",
+ "hashtable" };
/*
@@ -403,6 +404,25 @@ hdata_time (struct t_hdata *hdata, void *pointer, const char *name)
}
/*
+ * hdata_hashtable: get hashtable value of a variable in structure using hdata
+ */
+
+struct t_hashtable *
+hdata_hashtable (struct t_hdata *hdata, void *pointer, const char *name)
+{
+ int offset;
+
+ if (hdata && pointer)
+ {
+ offset = hdata_get_var_offset (hdata, name);
+ if (offset >= 0)
+ return *((struct t_hashtable **)(pointer + offset));
+ }
+
+ return NULL;
+}
+
+/*
* hdata_get_string: get a hdata property as string
*/
diff --git a/src/core/wee-hdata.h b/src/core/wee-hdata.h
index 66d82202b..49694ce30 100644
--- a/src/core/wee-hdata.h
+++ b/src/core/wee-hdata.h
@@ -76,6 +76,8 @@ extern void *hdata_pointer (struct t_hdata *hdata, void *pointer,
const char *name);
extern time_t hdata_time (struct t_hdata *hdata, void *pointer,
const char *name);
+extern struct t_hashtable *hdata_hashtable (struct t_hdata *hdata,
+ void *pointer, const char *name);
extern const char *hdata_get_string (struct t_hdata *hdata,
const char *property);
extern void hdata_free_all_plugin (struct t_weechat_plugin *plugin);