void gui_nicklist_send_hsignal (const char *signal, struct t_gui_buffer *buffer, struct t_gui_nick_group *group, struct t_gui_nick *nick) { if (!gui_nicklist_hsignal) { gui_nicklist_hsignal = hashtable_new (32, WEECHAT_HASHTABLE_STRING, WEECHAT_HASHTABLE_POINTER, NULL, NULL); } if (!gui_nicklist_hsignal) return; hashtable_remove_all (gui_nicklist_hsignal); hashtable_set (gui_nicklist_hsignal, "buffer", buffer); hashtable_set (gui_nicklist_hsignal, "parent_group", (group) ? group->parent : nick->group); if (group) hashtable_set (gui_nicklist_hsignal, "group", group); if (nick) hashtable_set (gui_nicklist_hsignal, "nick", nick); (void) hook_hsignal_send (signal, gui_nicklist_hsignal); }
void gui_color_palette_build_aliases () { int i; struct t_gui_color_palette *color_palette; char str_number[64]; if (!gui_color_hash_palette_alias || !gui_color_list_with_alias || !gui_color_hash_palette_color) { gui_color_palette_alloc_structs (); } hashtable_remove_all (gui_color_hash_palette_alias); weelist_remove_all (gui_color_list_with_alias); for (i = 0; i < GUI_CURSES_NUM_WEECHAT_COLORS; i++) { weelist_add (gui_color_list_with_alias, gui_weechat_colors[i].string, WEECHAT_LIST_POS_END, NULL); } for (i = 0; i <= gui_color_term_colors; i++) { color_palette = gui_color_palette_get (i); if (color_palette) { weelist_add (gui_color_list_with_alias, color_palette->alias, WEECHAT_LIST_POS_END, NULL); } else { snprintf (str_number, sizeof (str_number), "%d", i); weelist_add (gui_color_list_with_alias, str_number, WEECHAT_LIST_POS_END, NULL); } } hashtable_map (gui_color_hash_palette_color, &gui_color_palette_add_alias_cb, NULL); }
int secure_reload_cb (void *data, struct t_config_file *config_file) { /* make C compiler happy */ (void) data; if (secure_hashtable_data_encrypted->items_count > 0) { gui_chat_printf (NULL, _("%sError: not possible to reload file sec.conf " "because there is still encrypted data (use /secure " "decrypt, see /help secure)"), gui_chat_prefix[GUI_CHAT_PREFIX_ERROR]); return WEECHAT_CONFIG_READ_FILE_NOT_FOUND; } secure_data_encrypted = 0; /* remove all secured data */ hashtable_remove_all (secure_hashtable_data); return config_file_reload (config_file); }
TEST(Hashtable, SetGetRemove) { struct t_hashtable *hashtable, *hashtable2; struct t_hashtable_item *item, *ptr_item, *ptr_item2; const char *str_key = HASHTABLE_TEST_KEY; const char *str_value = HASHTABLE_TEST_VALUE; const char *ptr_value; unsigned long long hash; int i; hashtable = hashtable_new (32, WEECHAT_HASHTABLE_STRING, WEECHAT_HASHTABLE_STRING, &test_hashtable_hash_key_cb, &test_hashtable_keycmp_cb); LONGS_EQUAL(32, hashtable->size); LONGS_EQUAL(0, hashtable->items_count); /* invalid set of items */ POINTERS_EQUAL(NULL, hashtable_set_with_size (NULL, NULL, -1, NULL, -1)); POINTERS_EQUAL(NULL, hashtable_set_with_size (NULL, NULL, -1, NULL, -1)); /* add an item in hashtable with NULL value */ item = hashtable_set (hashtable, str_key, NULL); CHECK(item); LONGS_EQUAL(1, hashtable->items_count); STRCMP_EQUAL(str_key, (const char *)item->key); LONGS_EQUAL(strlen (str_key) + 1, item->key_size); POINTERS_EQUAL(NULL, item->value); LONGS_EQUAL(0, item->value_size); POINTERS_EQUAL(NULL, item->prev_item); POINTERS_EQUAL(NULL, item->next_item); /* set a string value for the same key */ item = hashtable_set (hashtable, str_key, str_value); CHECK(item); LONGS_EQUAL(1, hashtable->items_count); STRCMP_EQUAL(str_key, (const char *)item->key); LONGS_EQUAL(strlen (str_key) + 1, item->key_size); STRCMP_EQUAL(str_value, (const char *)item->value); LONGS_EQUAL(strlen (str_value) + 1, item->value_size); POINTERS_EQUAL(NULL, item->prev_item); POINTERS_EQUAL(NULL, item->next_item); /* get item */ item = hashtable_get_item (hashtable, str_key, &hash); CHECK(item); STRCMP_EQUAL(str_key, (const char *)item->key); STRCMP_EQUAL(str_value, (const char *)item->value); LONGS_EQUAL(2, hash); /* get value */ ptr_value = (const char *)hashtable_get (hashtable, str_key); CHECK(ptr_value); STRCMP_EQUAL(ptr_value, str_value); /* check if key is in hashtable */ LONGS_EQUAL(0, hashtable_has_key (hashtable, NULL)); LONGS_EQUAL(0, hashtable_has_key (hashtable, "")); LONGS_EQUAL(0, hashtable_has_key (hashtable, "xxx")); LONGS_EQUAL(1, hashtable_has_key (hashtable, str_key)); /* delete an item */ hashtable_remove (hashtable, str_key); LONGS_EQUAL(0, hashtable->items_count); /* add an item with size in hashtable */ item = hashtable_set_with_size (hashtable, str_key, strlen (str_key) + 1, str_value, strlen (str_value) + 1); CHECK(item); LONGS_EQUAL(1, hashtable->items_count); STRCMP_EQUAL(str_key, (const char *)item->key); LONGS_EQUAL(strlen (str_key) + 1, item->key_size); STRCMP_EQUAL(str_value, (const char *)item->value); LONGS_EQUAL(strlen (str_value) + 1, item->value_size); /* add another item */ hashtable_set (hashtable, "xxx", "zzz"); LONGS_EQUAL(2, hashtable->items_count); /* * test duplication of hashtable and check that duplicated content is * exactly the same as initial hashtable */ hashtable2 = hashtable_dup (hashtable); CHECK(hashtable2); LONGS_EQUAL(hashtable->size, hashtable2->size); LONGS_EQUAL(hashtable->items_count, hashtable2->items_count); for (i = 0; i < hashtable->size; i++) { if (hashtable->htable[i]) { ptr_item = hashtable->htable[i]; ptr_item2 = hashtable2->htable[i]; while (ptr_item && ptr_item2) { LONGS_EQUAL(ptr_item->key_size, ptr_item2->key_size); LONGS_EQUAL(ptr_item->value_size, ptr_item2->value_size); if (ptr_item->key) { STRCMP_EQUAL((const char *)ptr_item->key, (const char *)ptr_item2->key); } else { POINTERS_EQUAL(ptr_item->key, ptr_item2->key); } if (ptr_item->value) { STRCMP_EQUAL((const char *)ptr_item->value, (const char *)ptr_item2->value); } else { POINTERS_EQUAL(ptr_item->value, ptr_item2->value); } ptr_item = ptr_item->next_item; ptr_item2 = ptr_item2->next_item; CHECK((ptr_item && ptr_item2) || (!ptr_item && !ptr_item2)); } } else { POINTERS_EQUAL(hashtable->htable[i], hashtable2->htable[i]); } } /* remove all items */ hashtable_remove_all (hashtable); LONGS_EQUAL(0, hashtable->items_count); /* free hashtables */ hashtable_free (hashtable); hashtable_free (hashtable2); /* * create a hashtable with size 8, and add 6 items, * to check if many items with same hashed key work fine, * the expected htable inside hashtable is: * +-----+ * | 0 | * +-----+ * | 1 | * +-----+ * | 2 | --> "extensible" * +-----+ * | 3 | --> "fast" --> "light" * +-----+ * | 4 | * +-----+ * | 5 | --> "chat" * +-----+ * | 6 | --> "client" * +-----+ * | 7 | --> "weechat" * +-----+ */ hashtable = hashtable_new (8, WEECHAT_HASHTABLE_STRING, WEECHAT_HASHTABLE_STRING, NULL, NULL); LONGS_EQUAL(8, hashtable->size); LONGS_EQUAL(0, hashtable->items_count); item = hashtable_set (hashtable, "weechat", NULL); CHECK(item); POINTERS_EQUAL(item, hashtable->htable[7]); item = hashtable_set (hashtable, "fast", NULL); CHECK(item); POINTERS_EQUAL(item, hashtable->htable[3]); item = hashtable_set (hashtable, "light", NULL); CHECK(item); POINTERS_EQUAL(item, hashtable->htable[3]->next_item); item = hashtable_set (hashtable, "extensible", NULL); CHECK(item); POINTERS_EQUAL(item, hashtable->htable[2]); item = hashtable_set (hashtable, "chat", NULL); CHECK(item); POINTERS_EQUAL(item, hashtable->htable[5]); item = hashtable_set (hashtable, "client", NULL); CHECK(item); POINTERS_EQUAL(item, hashtable->htable[6]); /* free hashtable */ hashtable_free (hashtable); }
void * hdata_search (struct t_hdata *hdata, void *pointer, const char *search, int move) { char *result; int rc; if (!hdata || !pointer || !search || !search[0] || (move == 0)) return NULL; /* clear or create hashtable with pointer for search */ if (hdata_search_pointers) { hashtable_remove_all (hdata_search_pointers); } else { hdata_search_pointers = hashtable_new (32, DOGECHAT_HASHTABLE_STRING, DOGECHAT_HASHTABLE_POINTER, NULL, NULL); } /* * create hashtable with extra vars (empty hashtable) * (hashtable would be created in eval_expression(), but it's created here * so it will not be created for each call to eval_expression()) */ if (!hdata_search_extra_vars) { hdata_search_extra_vars = hashtable_new (32, DOGECHAT_HASHTABLE_STRING, DOGECHAT_HASHTABLE_STRING, NULL, NULL); } if (!hdata_search_options) { hdata_search_options = hashtable_new (32, DOGECHAT_HASHTABLE_STRING, DOGECHAT_HASHTABLE_STRING, NULL, NULL); if (hdata_search_options) hashtable_set (hdata_search_options, "type", "condition"); } while (pointer) { /* set pointer in hashtable (used for evaluating expression) */ hashtable_set (hdata_search_pointers, hdata->name, pointer); /* evaluate expression */ result = eval_expression (search, hdata_search_pointers, hdata_search_extra_vars, hdata_search_options); rc = eval_is_true (result); if (result) free (result); if (rc) return pointer; pointer = hdata_move (hdata, pointer, move); } return NULL; }
char * eval_expression (const char *expr, struct t_hashtable *pointers, struct t_hashtable *extra_vars, struct t_hashtable *options) { int condition, rc; char *value; const char *prefix, *suffix, *default_prefix = "${", *default_suffix = "}"; const char *ptr_value; struct t_gui_window *window; if (!expr) return NULL; condition = 0; prefix = default_prefix; suffix = default_suffix; /* create hashtable pointers if it's NULL */ if (!pointers) { if (eval_hashtable_pointers) hashtable_remove_all (eval_hashtable_pointers); else { eval_hashtable_pointers = hashtable_new (32, WEECHAT_HASHTABLE_STRING, WEECHAT_HASHTABLE_POINTER, NULL, NULL); if (!eval_hashtable_pointers) return NULL; } pointers = eval_hashtable_pointers; } /* * set window/buffer with pointer to current window/buffer * (if not already defined in the hashtable) */ if (gui_current_window) { if (!hashtable_has_key (pointers, "window")) hashtable_set (pointers, "window", gui_current_window); if (!hashtable_has_key (pointers, "buffer")) { window = (struct t_gui_window *)hashtable_get (pointers, "window"); if (window) hashtable_set (pointers, "buffer", window->buffer); } } /* read options */ if (options) { /* check the type of evaluation */ ptr_value = hashtable_get (options, "type"); if (ptr_value && (strcmp (ptr_value, "condition") == 0)) condition = 1; /* check for custom prefix */ ptr_value = hashtable_get (options, "prefix"); if (ptr_value && ptr_value[0]) prefix = ptr_value; /* check for custom suffix */ ptr_value = hashtable_get (options, "suffix"); if (ptr_value && ptr_value[0]) suffix = ptr_value; } /* evaluate expression */ if (condition) { /* evaluate as condition (return a boolean: "0" or "1") */ value = eval_expression_condition (expr, pointers, extra_vars, 0, prefix, suffix); rc = eval_is_true (value); if (value) free (value); value = strdup ((rc) ? EVAL_STR_TRUE : EVAL_STR_FALSE); } else { /* only replace variables in expression */ value = eval_replace_vars (expr, pointers, extra_vars, prefix, suffix); } return value; }