From 4a175cbbe6405dbdbf713bda3af97b9e0d057467 Mon Sep 17 00:00:00 2001 From: Andrew Borodin Date: Sun, 12 Sep 2010 18:32:26 +0400 Subject: [PATCH] Some optimization and cleanup of color-related code in TTY layer. Signed-off-by: Andrew Borodin --- lib/tty/color-internal.c | 8 +++++-- lib/tty/color-internal.h | 2 +- lib/tty/color-ncurses.c | 1 + lib/tty/color.c | 51 ++++++++++------------------------------ 4 files changed, 21 insertions(+), 41 deletions(-) diff --git a/lib/tty/color-internal.c b/lib/tty/color-internal.c index 99bb8c3d1..f5778c8a6 100644 --- a/lib/tty/color-internal.c +++ b/lib/tty/color-internal.c @@ -82,12 +82,14 @@ mc_tty_color_table_t const color_table[] = { const char * tty_color_get_valid_name (const char *color_name) { - int i; if (color_name != NULL) + { + size_t i; for (i = 0; color_table[i].name != NULL; i++) if (strcmp (color_name, color_table[i].name) == 0) return color_table[i].name; + } return NULL; } @@ -96,12 +98,14 @@ tty_color_get_valid_name (const char *color_name) int tty_color_get_index_by_name (const char *color_name) { - int i; if (color_name != NULL) + { + size_t i; for (i = 0; color_table[i].name != NULL; i++) if (strcmp (color_name, color_table[i].name) == 0) return color_table[i].value; + } return -1; } diff --git a/lib/tty/color-internal.h b/lib/tty/color-internal.h index fbfe90b00..609d850c5 100644 --- a/lib/tty/color-internal.h +++ b/lib/tty/color-internal.h @@ -25,7 +25,7 @@ typedef struct mc_color_pair_struct const char *cbg; int ifg; int ibg; - size_t pair_index; + int pair_index; gboolean is_temp; } tty_color_pair_t; diff --git a/lib/tty/color-ncurses.c b/lib/tty/color-ncurses.c index 1f3d3fff9..750e7a1f3 100644 --- a/lib/tty/color-ncurses.c +++ b/lib/tty/color-ncurses.c @@ -61,6 +61,7 @@ static int mc_tty_color_save_attr_lib (int color_pair, int color_attr) { int *attr, *key; + attr = g_try_new0 (int, 1); if (attr == NULL) return color_attr; diff --git a/lib/tty/color.c b/lib/tty/color.c index 21e958344..0d70702a8 100644 --- a/lib/tty/color.c +++ b/lib/tty/color.c @@ -61,23 +61,6 @@ static GHashTable *mc_tty_color__hashtable = NULL; /*** file scope functions ************************************************************************/ /* --------------------------------------------------------------------------------------------- */ -static inline void -color_hash_destroy_key (gpointer data) -{ - g_free (data); -} - -/* --------------------------------------------------------------------------------------------- */ - -static void -color_hash_destroy_value (gpointer data) -{ - tty_color_pair_t *mc_color_pair = (tty_color_pair_t *) data; - g_free (mc_color_pair); -} - -/* --------------------------------------------------------------------------------------------- */ - static gboolean tty_color_free_condition_cb (gpointer key, gpointer value, gpointer user_data) { @@ -96,7 +79,7 @@ static void tty_color_free_all (gboolean is_temp_color) { g_hash_table_foreach_remove (mc_tty_color__hashtable, tty_color_free_condition_cb, - (is_temp_color) ? (gpointer) 1 : NULL); + is_temp_color ? GINT_TO_POINTER (1) : NULL); } /* --------------------------------------------------------------------------------------------- */ @@ -104,33 +87,29 @@ tty_color_free_all (gboolean is_temp_color) static gboolean tty_color_get_next_cpn_cb (gpointer key, gpointer value, gpointer user_data) { - size_t cp; + int cp; tty_color_pair_t *mc_color_pair; (void) key; - cp = (size_t) user_data; + cp = GPOINTER_TO_INT (user_data); mc_color_pair = (tty_color_pair_t *) value; - if (cp == mc_color_pair->pair_index) - return TRUE; - - return FALSE; + return (cp == mc_color_pair->pair_index); } /* --------------------------------------------------------------------------------------------- */ static int -tty_color_get_next__color_pair_number () +tty_color_get_next__color_pair_number (void) { - size_t cp_count = g_hash_table_size (mc_tty_color__hashtable); - size_t cp = 0; + const size_t cp_count = g_hash_table_size (mc_tty_color__hashtable); + int cp; for (cp = 0; cp < cp_count; cp++) - { - if (g_hash_table_find (mc_tty_color__hashtable, tty_color_get_next_cpn_cb, (gpointer) cp) == - NULL) - return cp; - } + if (g_hash_table_find (mc_tty_color__hashtable, tty_color_get_next_cpn_cb, + GINT_TO_POINTER (cp)) == NULL) + break; + return cp; } @@ -142,9 +121,7 @@ void tty_init_colors (gboolean disable, gboolean force) { tty_color_init_lib (disable, force); - mc_tty_color__hashtable = g_hash_table_new_full (g_str_hash, g_str_equal, - color_hash_destroy_key, - color_hash_destroy_value); + mc_tty_color__hashtable = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free); } /* --------------------------------------------------------------------------------------------- */ @@ -178,11 +155,9 @@ tty_try_alloc_color_pair2 (const char *fg, const char *bg, gboolean is_temp_colo if (fg == NULL) fg = tty_color_defaults__fg; - if (bg == NULL) - { bg = tty_color_defaults__bg; - } + c_fg = tty_color_get_valid_name (fg); c_bg = tty_color_get_valid_name (bg);