From e67bb415ba1b36c52bfad7a2ea787199897625b5 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 2 Oct 2009 08:25:14 +0200 Subject: [PATCH] utils: pass preallocated buffer to format_duration_long() --- src/screen_song.c | 23 +++++++++++++---------- src/utils.c | 13 +++---------- src/utils.h | 4 ++-- 3 files changed, 18 insertions(+), 22 deletions(-) diff --git a/src/screen_song.c b/src/screen_song.c index 78fbf45..7b4a481 100644 --- a/src/screen_song.c +++ b/src/screen_song.c @@ -281,7 +281,6 @@ screen_song_add_stats(const struct mpdclient *c) { unsigned i, max_label_width; char buf[64]; - char *duration; GDate *date; enum label { ARTISTS, ALBUMS, SONGS, UPTIME, @@ -319,15 +318,19 @@ screen_song_add_stats(const struct mpdclient *c) g_snprintf(buf, sizeof(buf), "%d", mpd_stats_get_number_of_songs(mpd_stats)); screen_song_append(labels[SONGS], buf, max_label_width); - duration = format_duration_long(mpd_stats_get_db_play_time(mpd_stats)); - screen_song_append(labels[DBPLAYTIME], duration, max_label_width); - g_free(duration); - duration = format_duration_long(mpd_stats_get_play_time(mpd_stats)); - screen_song_append(labels[PLAYTIME], duration, max_label_width); - g_free(duration); - duration = format_duration_long(mpd_stats_get_uptime(mpd_stats)); - screen_song_append(labels[UPTIME], duration, max_label_width); - g_free(duration); + + format_duration_long(buf, sizeof(buf), + mpd_stats_get_db_play_time(mpd_stats)); + screen_song_append(labels[DBPLAYTIME], buf, max_label_width); + + format_duration_long(buf, sizeof(buf), + mpd_stats_get_play_time(mpd_stats)); + screen_song_append(labels[PLAYTIME], buf, max_label_width); + + format_duration_long(buf, sizeof(buf), + mpd_stats_get_uptime(mpd_stats)); + screen_song_append(labels[UPTIME], buf, max_label_width); + date = g_date_new(); g_date_set_time_t(date, mpd_stats_get_db_update_time(mpd_stats)); g_date_strftime(buf, sizeof(buf), "%x", date); diff --git a/src/utils.c b/src/utils.c index 688f6c8..8023bfb 100644 --- a/src/utils.c +++ b/src/utils.c @@ -122,8 +122,8 @@ gcmp_list_from_path(struct mpdclient *c, const gchar *path, return list; } -char * -format_duration_long(unsigned long duration) +void +format_duration_long(char *p, size_t length, unsigned long duration) { const char *year = _("year"); const char *years = _("years"); @@ -131,13 +131,8 @@ format_duration_long(unsigned long duration) const char *weeks = _("weeks"); const char *day = _("day"); const char *days = _("days"); - char *buffer, *p; unsigned bytes_written = 0; - unsigned length = utf8_width(years) + - utf8_width(weeks) + utf8_width(days) + 32; - buffer = g_malloc(length); - p = buffer; if (duration / 31536000 > 0) { if (duration / 31536000 == 1) bytes_written = g_snprintf(p, length, "%d %s, ", 1, year); @@ -165,9 +160,7 @@ format_duration_long(unsigned long duration) length -= bytes_written; p += bytes_written; } + g_snprintf(p, length, "%02lu:%02lu:%02lu", duration / 3600, duration % 3600 / 60, duration % 3600 % 60); - return buffer; } - - diff --git a/src/utils.h b/src/utils.h index 1c8fe8b..c9374fa 100644 --- a/src/utils.h +++ b/src/utils.h @@ -40,7 +40,7 @@ GList * gcmp_list_from_path(struct mpdclient *c, const gchar *path, GList *list, gint types); -char * -format_duration_long(unsigned long duration); +void +format_duration_long(char *buffer, size_t length, unsigned long duration); #endif -- 2.30.2