Some optimization and cleanup of color-related code in TTY layer.

Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
This commit is contained in:
Andrew Borodin 2010-09-12 18:32:26 +04:00 committed by Slava Zanko
parent 7f119ffbbc
commit 4a175cbbe6
4 changed files with 21 additions and 41 deletions

View File

@ -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;
}

View File

@ -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;

View File

@ -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;

View File

@ -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);