From 05131d999eff2af8e892bc22219461c7e4a5f10d Mon Sep 17 00:00:00 2001 From: mjentsch Date: Mon, 23 Nov 2015 01:33:20 +0100 Subject: [PATCH] Remove code duplication --- telegram-purple.c | 40 +++++++--------------------------------- tgp-utils.c | 9 +++++++++ tgp-utils.h | 1 + 3 files changed, 17 insertions(+), 33 deletions(-) diff --git a/telegram-purple.c b/telegram-purple.c index 74f3f09..497e277 100644 --- a/telegram-purple.c +++ b/telegram-purple.c @@ -81,7 +81,8 @@ static void update_user_typing (struct tgl_state *TLS, struct tgl_user *U, enum static void update_marked_read (struct tgl_state *TLS, int num, struct tgl_message *list[]); static void update_on_logged_in (struct tgl_state *TLS); static void update_on_ready (struct tgl_state *TLS); -static char *format_print_name (struct tgl_state *TLS, tgl_peer_id_t id, const char *a1, const char *a2, const char *a3, const char *a4); +static char *format_print_name (struct tgl_state *TLS, tgl_peer_id_t id, const char *a1, const char *a2, const char *a3, + const char *a4); static void on_user_get_info (struct tgl_state *TLS, void *info_data, int success, struct tgl_user *U); const char *config_dir = "telegram-purple"; @@ -266,38 +267,11 @@ static void update_marked_read (struct tgl_state *TLS, int num, struct tgl_messa } } -static char *format_print_name (struct tgl_state *TLS, tgl_peer_id_t id, const char *a1, const char *a2, const char *a3, const char *a4) { - const char *d[4]; - d[0] = a1; d[1] = a2; d[2] = a3; d[3] = a4; - static char buf[10000]; - buf[0] = 0; - int i; - int p = 0; - for (i = 0; i < 4; i++) { - if (d[i] && strlen (d[i])) { - p += tgl_snprintf (buf + p, 9999 - p, "%s%s", p ? " " : "", d[i]); - assert (p < 9990); - } - } - char *s = buf; - while (*s) { - if (*s == '\n') { *s = ' '; } - if (*s == '#') { *s = '@'; } - s++; - } - s = buf; - int fl = (int)strlen (s); - int cc = 0; - while (1) { - tgl_peer_t *P = tgl_peer_get_by_name (TLS, s); - if (!P || !tgl_cmp_peer_id (P->id, id)) { - break; - } - cc ++; - assert (cc <= 9999); - tgl_snprintf (s + fl, 9999 - fl, " #%d", cc); - } - return tgl_strdup (s); +static char *format_print_name (struct tgl_state *TLS, tgl_peer_id_t id, const char *a1, const char *a2, + const char *a3, const char *a4) { + char *name = tgls_default_create_print_name (TLS, id, a1, a2, a3, a4); + tgp_replace (name, '_', ' '); + return name; } /* diff --git a/tgp-utils.c b/tgp-utils.c index 2262b76..0ad2706 100644 --- a/tgp-utils.c +++ b/tgp-utils.c @@ -126,3 +126,12 @@ int tgp_startswith (const char *str, const char *with) { } return TRUE; } + +void tgp_replace (char *string, char what, char with) { + char *p = string; + while (*(p ++)) { + if (*p == what) { + *p = with; + } + } +} diff --git a/tgp-utils.h b/tgp-utils.h index 2158b50..4d7e5be 100644 --- a/tgp-utils.h +++ b/tgp-utils.h @@ -48,5 +48,6 @@ void tgp_g_queue_free_full (GQueue *queue, GDestroyNotify free_func); void tgp_g_list_free_full (GList *list, GDestroyNotify free_func); const char *tgp_mime_to_filetype (const char *mime); int tgp_startswith (const char *str, const char *with); +void tgp_replace (char *string, char what, char with); #endif