From 545a23e92f0b381f98276dd2bc1265cd1a88c81e Mon Sep 17 00:00:00 2001 From: Dmitry Podgorny Date: Mon, 26 Aug 2013 03:29:50 +0300 Subject: removed strdup in autocomplete calls --- src/command/command.c | 144 +++++++++++++++++++++++----------------------- src/config/accounts.c | 20 +++---- src/config/preferences.c | 4 +- src/muc.c | 4 +- src/xmpp/bookmark.c | 4 +- src/xmpp/presence.c | 2 +- src/xmpp/roster.c | 12 ++-- tests/test_autocomplete.c | 49 ++++++---------- 8 files changed, 112 insertions(+), 127 deletions(-) diff --git a/src/command/command.c b/src/command/command.c index 41c8a2ae..626b108e 100644 --- a/src/command/command.c +++ b/src/command/command.c @@ -878,16 +878,16 @@ cmd_init(void) commands_ac = autocomplete_new(); help_ac = autocomplete_new(); - autocomplete_add(help_ac, strdup("commands")); - autocomplete_add(help_ac, strdup("basic")); - autocomplete_add(help_ac, strdup("chatting")); - autocomplete_add(help_ac, strdup("groupchat")); - autocomplete_add(help_ac, strdup("presence")); - autocomplete_add(help_ac, strdup("contacts")); - autocomplete_add(help_ac, strdup("service")); - autocomplete_add(help_ac, strdup("settings")); - autocomplete_add(help_ac, strdup("other")); - autocomplete_add(help_ac, strdup("navigation")); + autocomplete_add(help_ac, "commands"); + autocomplete_add(help_ac, "basic"); + autocomplete_add(help_ac, "chatting"); + autocomplete_add(help_ac, "groupchat"); + autocomplete_add(help_ac, "presence"); + autocomplete_add(help_ac, "contacts"); + autocomplete_add(help_ac, "service"); + autocomplete_add(help_ac, "settings"); + autocomplete_add(help_ac, "other"); + autocomplete_add(help_ac, "navigation"); // load command defs into hash table commands = g_hash_table_new(g_str_hash, g_str_equal); @@ -899,102 +899,102 @@ cmd_init(void) g_hash_table_insert(commands, pcmd->cmd, pcmd); // add to commands and help autocompleters - autocomplete_add(commands_ac, (gchar *)strdup(pcmd->cmd)); - autocomplete_add(help_ac, (gchar *)strdup(pcmd->cmd+1)); + autocomplete_add(commands_ac, pcmd->cmd); + autocomplete_add(help_ac, pcmd->cmd+1); } prefs_ac = autocomplete_new(); - autocomplete_add(prefs_ac, strdup("ui")); - autocomplete_add(prefs_ac, strdup("desktop")); - autocomplete_add(prefs_ac, strdup("chat")); - autocomplete_add(prefs_ac, strdup("log")); - autocomplete_add(prefs_ac, strdup("conn")); - autocomplete_add(prefs_ac, strdup("presence")); + autocomplete_add(prefs_ac, "ui"); + autocomplete_add(prefs_ac, "desktop"); + autocomplete_add(prefs_ac, "chat"); + autocomplete_add(prefs_ac, "log"); + autocomplete_add(prefs_ac, "conn"); + autocomplete_add(prefs_ac, "presence"); notify_ac = autocomplete_new(); - autocomplete_add(notify_ac, strdup("message")); - autocomplete_add(notify_ac, strdup("typing")); - autocomplete_add(notify_ac, strdup("remind")); - autocomplete_add(notify_ac, strdup("invite")); - autocomplete_add(notify_ac, strdup("sub")); + autocomplete_add(notify_ac, "message"); + autocomplete_add(notify_ac, "typing"); + autocomplete_add(notify_ac, "remind"); + autocomplete_add(notify_ac, "invite"); + autocomplete_add(notify_ac, "sub"); sub_ac = autocomplete_new(); - autocomplete_add(sub_ac, strdup("request")); - autocomplete_add(sub_ac, strdup("allow")); - autocomplete_add(sub_ac, strdup("deny")); - autocomplete_add(sub_ac, strdup("show")); - autocomplete_add(sub_ac, strdup("sent")); - autocomplete_add(sub_ac, strdup("received")); + autocomplete_add(sub_ac, "request"); + autocomplete_add(sub_ac, "allow"); + autocomplete_add(sub_ac, "deny"); + autocomplete_add(sub_ac, "show"); + autocomplete_add(sub_ac, "sent"); + autocomplete_add(sub_ac, "received"); titlebar_ac = autocomplete_new(); - autocomplete_add(titlebar_ac, strdup("version")); + autocomplete_add(titlebar_ac, "version"); log_ac = autocomplete_new(); - autocomplete_add(log_ac, strdup("maxsize")); + autocomplete_add(log_ac, "maxsize"); autoaway_ac = autocomplete_new(); - autocomplete_add(autoaway_ac, strdup("mode")); - autocomplete_add(autoaway_ac, strdup("time")); - autocomplete_add(autoaway_ac, strdup("message")); - autocomplete_add(autoaway_ac, strdup("check")); + autocomplete_add(autoaway_ac, "mode"); + autocomplete_add(autoaway_ac, "time"); + autocomplete_add(autoaway_ac, "message"); + autocomplete_add(autoaway_ac, "check"); autoaway_mode_ac = autocomplete_new(); - autocomplete_add(autoaway_mode_ac, strdup("away")); - autocomplete_add(autoaway_mode_ac, strdup("idle")); - autocomplete_add(autoaway_mode_ac, strdup("off")); + autocomplete_add(autoaway_mode_ac, "away"); + autocomplete_add(autoaway_mode_ac, "idle"); + autocomplete_add(autoaway_mode_ac, "off"); theme_ac = autocomplete_new(); - autocomplete_add(theme_ac, strdup("list")); - autocomplete_add(theme_ac, strdup("set")); + autocomplete_add(theme_ac, "list"); + autocomplete_add(theme_ac, "set"); disco_ac = autocomplete_new(); - autocomplete_add(disco_ac, strdup("info")); - autocomplete_add(disco_ac, strdup("items")); + autocomplete_add(disco_ac, "info"); + autocomplete_add(disco_ac, "items"); account_ac = autocomplete_new(); - autocomplete_add(account_ac, strdup("list")); - autocomplete_add(account_ac, strdup("show")); - autocomplete_add(account_ac, strdup("add")); - autocomplete_add(account_ac, strdup("enable")); - autocomplete_add(account_ac, strdup("disable")); - autocomplete_add(account_ac, strdup("rename")); - autocomplete_add(account_ac, strdup("set")); + autocomplete_add(account_ac, "list"); + autocomplete_add(account_ac, "show"); + autocomplete_add(account_ac, "add"); + autocomplete_add(account_ac, "enable"); + autocomplete_add(account_ac, "disable"); + autocomplete_add(account_ac, "rename"); + autocomplete_add(account_ac, "set"); close_ac = autocomplete_new(); - autocomplete_add(close_ac, strdup("read")); - autocomplete_add(close_ac, strdup("all")); + autocomplete_add(close_ac, "read"); + autocomplete_add(close_ac, "all"); wins_ac = autocomplete_new(); - autocomplete_add(wins_ac, strdup("prune")); - autocomplete_add(wins_ac, strdup("tidy")); + autocomplete_add(wins_ac, "prune"); + autocomplete_add(wins_ac, "tidy"); roster_ac = autocomplete_new(); - autocomplete_add(roster_ac, strdup("add")); - autocomplete_add(roster_ac, strdup("nick")); - autocomplete_add(roster_ac, strdup("remove")); + autocomplete_add(roster_ac, "add"); + autocomplete_add(roster_ac, "nick"); + autocomplete_add(roster_ac, "remove"); group_ac = autocomplete_new(); - autocomplete_add(group_ac, strdup("show")); - autocomplete_add(group_ac, strdup("add")); - autocomplete_add(group_ac, strdup("remove")); + autocomplete_add(group_ac, "show"); + autocomplete_add(group_ac, "add"); + autocomplete_add(group_ac, "remove"); theme_load_ac = NULL; who_ac = autocomplete_new(); - autocomplete_add(who_ac, strdup("chat")); - autocomplete_add(who_ac, strdup("online")); - autocomplete_add(who_ac, strdup("away")); - autocomplete_add(who_ac, strdup("xa")); - autocomplete_add(who_ac, strdup("dnd")); - autocomplete_add(who_ac, strdup("offline")); - autocomplete_add(who_ac, strdup("available")); - autocomplete_add(who_ac, strdup("unavailable")); - autocomplete_add(who_ac, strdup("any")); + autocomplete_add(who_ac, "chat"); + autocomplete_add(who_ac, "online"); + autocomplete_add(who_ac, "away"); + autocomplete_add(who_ac, "xa"); + autocomplete_add(who_ac, "dnd"); + autocomplete_add(who_ac, "offline"); + autocomplete_add(who_ac, "available"); + autocomplete_add(who_ac, "unavailable"); + autocomplete_add(who_ac, "any"); bookmark_ac = autocomplete_new(); - autocomplete_add(bookmark_ac, strdup("add")); - autocomplete_add(bookmark_ac, strdup("list")); - autocomplete_add(bookmark_ac, strdup("remove")); + autocomplete_add(bookmark_ac, "add"); + autocomplete_add(bookmark_ac, "list"); + autocomplete_add(bookmark_ac, "remove"); cmd_history_init(); } @@ -3813,7 +3813,7 @@ _theme_autocomplete(char *input, int *size) theme_load_ac = autocomplete_new(); GSList *themes = theme_list(); while (themes != NULL) { - autocomplete_add(theme_load_ac, strdup(themes->data)); + autocomplete_add(theme_load_ac, themes->data); themes = g_slist_next(themes); } g_slist_free(themes); diff --git a/src/config/accounts.c b/src/config/accounts.c index 3d198874..3e3daec0 100644 --- a/src/config/accounts.c +++ b/src/config/accounts.c @@ -64,9 +64,9 @@ accounts_load(void) gsize i; for (i = 0; i < naccounts; i++) { - autocomplete_add(all_ac, strdup(account_names[i])); + autocomplete_add(all_ac, account_names[i]); if (g_key_file_get_boolean(accounts, account_names[i], "enabled", NULL)) { - autocomplete_add(enabled_ac, strdup(account_names[i])); + autocomplete_add(enabled_ac, account_names[i]); } _fix_legacy_accounts(account_names[i]); @@ -138,8 +138,8 @@ accounts_add(const char *account_name, const char *altdomain) g_key_file_set_integer(accounts, account_name, "priority.dnd", 0); _save_accounts(); - autocomplete_add(all_ac, strdup(account_name)); - autocomplete_add(enabled_ac, strdup(account_name)); + autocomplete_add(all_ac, account_name); + autocomplete_add(enabled_ac, account_name); } jid_destroy(jid); @@ -260,7 +260,7 @@ accounts_enable(const char * const name) if (g_key_file_has_group(accounts, name)) { g_key_file_set_boolean(accounts, name, "enabled", TRUE); _save_accounts(); - autocomplete_add(enabled_ac, strdup(name)); + autocomplete_add(enabled_ac, name); return TRUE; } else { return FALSE; @@ -273,7 +273,7 @@ accounts_disable(const char * const name) if (g_key_file_has_group(accounts, name)) { g_key_file_set_boolean(accounts, name, "enabled", FALSE); _save_accounts(); - autocomplete_remove(enabled_ac, strdup(name)); + autocomplete_remove(enabled_ac, name); return TRUE; } else { return FALSE; @@ -317,11 +317,11 @@ accounts_rename(const char * const account_name, const char * const new_name) g_key_file_remove_group(accounts, account_name, NULL); _save_accounts(); - autocomplete_remove(all_ac, strdup(account_name)); - autocomplete_add(all_ac, strdup(new_name)); + autocomplete_remove(all_ac, account_name); + autocomplete_add(all_ac, new_name); if (g_key_file_get_boolean(accounts, new_name, "enabled", NULL)) { - autocomplete_remove(enabled_ac, strdup(account_name)); - autocomplete_add(enabled_ac, strdup(new_name)); + autocomplete_remove(enabled_ac, account_name); + autocomplete_add(enabled_ac, new_name); } return TRUE; diff --git a/src/config/preferences.c b/src/config/preferences.c index fe34e00e..1d417e41 100644 --- a/src/config/preferences.c +++ b/src/config/preferences.c @@ -77,8 +77,8 @@ prefs_load(void) } boolean_choice_ac = autocomplete_new(); - autocomplete_add(boolean_choice_ac, strdup("on")); - autocomplete_add(boolean_choice_ac, strdup("off")); + autocomplete_add(boolean_choice_ac, "on"); + autocomplete_add(boolean_choice_ac, "off"); } void diff --git a/src/muc.c b/src/muc.c index 1d5b910a..93202a54 100644 --- a/src/muc.c +++ b/src/muc.c @@ -57,7 +57,7 @@ muc_init(void) void muc_add_invite(char *room) { - autocomplete_add(invite_ac, strdup(room)); + autocomplete_add(invite_ac, room); } void @@ -298,7 +298,7 @@ muc_add_to_roster(const char * const room, const char * const nick, if (old == NULL) { updated = TRUE; - autocomplete_add(chat_room->nick_ac, strdup(nick)); + autocomplete_add(chat_room->nick_ac, nick); } else if ((g_strcmp0(p_contact_presence(old), show) != 0) || (g_strcmp0(p_contact_status(old), status) != 0)) { updated = TRUE; diff --git a/src/xmpp/bookmark.c b/src/xmpp/bookmark.c index 92e6d7e8..0818f9ee 100644 --- a/src/xmpp/bookmark.c +++ b/src/xmpp/bookmark.c @@ -69,7 +69,7 @@ bookmark_add(const char *jid, const char *nick, gboolean autojoin) /* this may be command for modifying */ autocomplete_remove(bookmark_ac, jid); - autocomplete_add(bookmark_ac, strdup(jid)); + autocomplete_add(bookmark_ac, jid); } void @@ -174,7 +174,7 @@ _bookmark_handle_result(xmpp_conn_t * const conn, autojoin_val = FALSE; } - autocomplete_add(bookmark_ac, strdup(jid)); + autocomplete_add(bookmark_ac, jid); item = malloc(sizeof(*item)); item->jid = strdup(jid); item->nick = name; diff --git a/src/xmpp/presence.c b/src/xmpp/presence.c index ec439871..607bb02b 100644 --- a/src/xmpp/presence.c +++ b/src/xmpp/presence.c @@ -372,7 +372,7 @@ _subscribe_handler(xmpp_conn_t * const conn, } prof_handle_subscription(from_jid->barejid, PRESENCE_SUBSCRIBE); - autocomplete_add(sub_requests_ac, strdup(from_jid->barejid)); + autocomplete_add(sub_requests_ac, from_jid->barejid); jid_destroy(from_jid); diff --git a/src/xmpp/roster.c b/src/xmpp/roster.c index b31a2725..c073464d 100644 --- a/src/xmpp/roster.c +++ b/src/xmpp/roster.c @@ -184,12 +184,12 @@ roster_add(const char * const barejid, const char * const name, GSList *groups, // add groups while (groups != NULL) { - autocomplete_add(groups_ac, strdup(groups->data)); + autocomplete_add(groups_ac, groups->data); groups = g_slist_next(groups); } g_hash_table_insert(contacts, strdup(barejid), contact); - autocomplete_add(barejid_ac, strdup(barejid)); + autocomplete_add(barejid_ac, barejid); _add_name_and_barejid(name, barejid); if (!from_initial) { @@ -226,7 +226,7 @@ roster_update(const char * const barejid, const char * const name, // add groups while (groups != NULL) { - autocomplete_add(groups_ac, strdup(groups->data)); + autocomplete_add(groups_ac, groups->data); groups = g_slist_next(groups); } } @@ -248,7 +248,7 @@ roster_update_presence(const char * const barejid, Resource *resource, } p_contact_set_presence(contact, resource); Jid *jid = jid_create_from_bare_and_resource(barejid, resource->name); - autocomplete_add(fulljid_ac, strdup(jid->fulljid)); + autocomplete_add(fulljid_ac, jid->fulljid); jid_destroy(jid); return TRUE; @@ -646,10 +646,10 @@ static void _add_name_and_barejid(const char * const name, const char * const barejid) { if (name != NULL) { - autocomplete_add(name_ac, strdup(name)); + autocomplete_add(name_ac, name); g_hash_table_insert(name_to_barejid, strdup(name), strdup(barejid)); } else { - autocomplete_add(name_ac, strdup(barejid)); + autocomplete_add(name_ac, barejid); g_hash_table_insert(name_to_barejid, strdup(barejid), strdup(barejid)); } } diff --git a/tests/test_autocomplete.c b/tests/test_autocomplete.c index b7d70e9b..beb18335 100644 --- a/tests/test_autocomplete.c +++ b/tests/test_autocomplete.c @@ -39,9 +39,9 @@ static void get_after_create_returns_null(void) static void add_one_and_complete(void) { - char *item = strdup("Hello"); + char *item = "Hello"; Autocomplete ac = autocomplete_new(); - autocomplete_add(ac, item); + autocomplete_add(ac, "Hello"); char *result = autocomplete_complete(ac, "Hel"); assert_string_equals("Hello", result); @@ -51,11 +51,9 @@ static void add_one_and_complete(void) static void add_two_and_complete_returns_first(void) { - char *item1 = strdup("Hello"); - char *item2 = strdup("Help"); Autocomplete ac = autocomplete_new(); - autocomplete_add(ac, item1); - autocomplete_add(ac, item2); + autocomplete_add(ac, "Hello"); + autocomplete_add(ac, "Help"); char *result = autocomplete_complete(ac, "Hel"); assert_string_equals("Hello", result); @@ -65,11 +63,9 @@ static void add_two_and_complete_returns_first(void) static void add_two_and_complete_returns_second(void) { - char *item1 = strdup("Hello"); - char *item2 = strdup("Help"); Autocomplete ac = autocomplete_new(); - autocomplete_add(ac, item1); - autocomplete_add(ac, item2); + autocomplete_add(ac, "Hello"); + autocomplete_add(ac, "Help"); char *result1 = autocomplete_complete(ac, "Hel"); char *result2 = autocomplete_complete(ac, result1); @@ -80,11 +76,9 @@ static void add_two_and_complete_returns_second(void) static void add_two_adds_two(void) { - char *item1 = strdup("Hello"); - char *item2 = strdup("Help"); Autocomplete ac = autocomplete_new(); - autocomplete_add(ac, item1); - autocomplete_add(ac, item2); + autocomplete_add(ac, "Hello"); + autocomplete_add(ac, "Help"); GSList *result = autocomplete_get_list(ac); assert_int_equals(2, g_slist_length(result)); @@ -94,11 +88,9 @@ static void add_two_adds_two(void) static void add_two_same_adds_one(void) { - char *item1 = strdup("Hello"); - char *item2 = strdup("Hello"); Autocomplete ac = autocomplete_new(); - autocomplete_add(ac, item1); - autocomplete_add(ac, item2); + autocomplete_add(ac, "Hello"); + autocomplete_add(ac, "Hello"); GSList *result = autocomplete_get_list(ac); assert_int_equals(1, g_slist_length(result)); @@ -108,11 +100,9 @@ static void add_two_same_adds_one(void) static void add_two_same_updates(void) { - char *item1 = strdup("Hello"); - char *item2 = strdup("Hello"); Autocomplete ac = autocomplete_new(); - autocomplete_add(ac, item1); - autocomplete_add(ac, item2); + autocomplete_add(ac, "Hello"); + autocomplete_add(ac, "Hello"); GSList *result = autocomplete_get_list(ac); GSList *first = g_slist_nth(result, 0); @@ -126,9 +116,8 @@ static void add_two_same_updates(void) static void add_one_returns_true(void) { - char *item = strdup("Hello"); Autocomplete ac = autocomplete_new(); - int result = autocomplete_add(ac, item); + int result = autocomplete_add(ac, "Hello"); assert_true(result); @@ -137,11 +126,9 @@ static void add_one_returns_true(void) static void add_two_different_returns_true(void) { - char *item1 = strdup("Hello"); - char *item2 = strdup("Hello there"); Autocomplete ac = autocomplete_new(); - int result1 = autocomplete_add(ac, item1); - int result2 = autocomplete_add(ac, item2); + int result1 = autocomplete_add(ac, "Hello"); + int result2 = autocomplete_add(ac, "Hello there"); assert_true(result1); assert_true(result2); @@ -151,11 +138,9 @@ static void add_two_different_returns_true(void) static void add_two_same_returns_false(void) { - char *item1 = strdup("Hello"); - char *item2 = strdup("Hello"); Autocomplete ac = autocomplete_new(); - int result1 = autocomplete_add(ac, item1); - int result2 = autocomplete_add(ac, item2); + int result1 = autocomplete_add(ac, "Hello"); + int result2 = autocomplete_add(ac, "Hello"); assert_true(result1); assert_false(result2); -- cgit 1.4.1-2-gfad0