1
1

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

Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
Этот коммит содержится в:
Andrew Borodin 2010-09-12 18:32:26 +04:00 коммит произвёл Slava Zanko
родитель 7f119ffbbc
Коммит 4a175cbbe6
4 изменённых файлов: 21 добавлений и 41 удалений

Просмотреть файл

@ -82,12 +82,14 @@ mc_tty_color_table_t const color_table[] = {
const char * const char *
tty_color_get_valid_name (const char *color_name) tty_color_get_valid_name (const char *color_name)
{ {
int i;
if (color_name != NULL) if (color_name != NULL)
{
size_t i;
for (i = 0; color_table[i].name != NULL; i++) for (i = 0; color_table[i].name != NULL; i++)
if (strcmp (color_name, color_table[i].name) == 0) if (strcmp (color_name, color_table[i].name) == 0)
return color_table[i].name; return color_table[i].name;
}
return NULL; return NULL;
} }
@ -96,12 +98,14 @@ tty_color_get_valid_name (const char *color_name)
int int
tty_color_get_index_by_name (const char *color_name) tty_color_get_index_by_name (const char *color_name)
{ {
int i;
if (color_name != NULL) if (color_name != NULL)
{
size_t i;
for (i = 0; color_table[i].name != NULL; i++) for (i = 0; color_table[i].name != NULL; i++)
if (strcmp (color_name, color_table[i].name) == 0) if (strcmp (color_name, color_table[i].name) == 0)
return color_table[i].value; return color_table[i].value;
}
return -1; return -1;
} }

Просмотреть файл

@ -25,7 +25,7 @@ typedef struct mc_color_pair_struct
const char *cbg; const char *cbg;
int ifg; int ifg;
int ibg; int ibg;
size_t pair_index; int pair_index;
gboolean is_temp; gboolean is_temp;
} tty_color_pair_t; } tty_color_pair_t;

Просмотреть файл

@ -61,6 +61,7 @@ static int
mc_tty_color_save_attr_lib (int color_pair, int color_attr) mc_tty_color_save_attr_lib (int color_pair, int color_attr)
{ {
int *attr, *key; int *attr, *key;
attr = g_try_new0 (int, 1); attr = g_try_new0 (int, 1);
if (attr == NULL) if (attr == NULL)
return color_attr; return color_attr;

Просмотреть файл

@ -61,23 +61,6 @@ static GHashTable *mc_tty_color__hashtable = NULL;
/*** file scope functions ************************************************************************/ /*** 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 static gboolean
tty_color_free_condition_cb (gpointer key, gpointer value, gpointer user_data) 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) tty_color_free_all (gboolean is_temp_color)
{ {
g_hash_table_foreach_remove (mc_tty_color__hashtable, tty_color_free_condition_cb, 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 static gboolean
tty_color_get_next_cpn_cb (gpointer key, gpointer value, gpointer user_data) 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; tty_color_pair_t *mc_color_pair;
(void) key; (void) key;
cp = (size_t) user_data; cp = GPOINTER_TO_INT (user_data);
mc_color_pair = (tty_color_pair_t *) value; mc_color_pair = (tty_color_pair_t *) value;
if (cp == mc_color_pair->pair_index) return (cp == mc_color_pair->pair_index);
return TRUE;
return FALSE;
} }
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */
static int 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); const size_t cp_count = g_hash_table_size (mc_tty_color__hashtable);
size_t cp = 0; int cp;
for (cp = 0; cp < cp_count; cp++) for (cp = 0; cp < cp_count; cp++)
{ if (g_hash_table_find (mc_tty_color__hashtable, tty_color_get_next_cpn_cb,
if (g_hash_table_find (mc_tty_color__hashtable, tty_color_get_next_cpn_cb, (gpointer) cp) == GINT_TO_POINTER (cp)) == NULL)
NULL) break;
return cp;
}
return cp; return cp;
} }
@ -142,9 +121,7 @@ void
tty_init_colors (gboolean disable, gboolean force) tty_init_colors (gboolean disable, gboolean force)
{ {
tty_color_init_lib (disable, force); tty_color_init_lib (disable, force);
mc_tty_color__hashtable = g_hash_table_new_full (g_str_hash, g_str_equal, mc_tty_color__hashtable = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
color_hash_destroy_key,
color_hash_destroy_value);
} }
/* --------------------------------------------------------------------------------------------- */ /* --------------------------------------------------------------------------------------------- */
@ -178,11 +155,9 @@ tty_try_alloc_color_pair2 (const char *fg, const char *bg, gboolean is_temp_colo
if (fg == NULL) if (fg == NULL)
fg = tty_color_defaults__fg; fg = tty_color_defaults__fg;
if (bg == NULL) if (bg == NULL)
{
bg = tty_color_defaults__bg; bg = tty_color_defaults__bg;
}
c_fg = tty_color_get_valid_name (fg); c_fg = tty_color_get_valid_name (fg);
c_bg = tty_color_get_valid_name (bg); c_bg = tty_color_get_valid_name (bg);