Code

utils: pass preallocated buffer to format_duration_long()
authorMax Kellermann <max@duempel.org>
Fri, 2 Oct 2009 06:25:14 +0000 (08:25 +0200)
committerMax Kellermann <max@duempel.org>
Fri, 2 Oct 2009 06:25:14 +0000 (08:25 +0200)
src/screen_song.c
src/utils.c
src/utils.h

index 78fbf45e5a957d5537bfa157fc0d47e605b00763..7b4a4810f11a320a4ae944736cbe9339f5521b12 100644 (file)
@@ -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);
index 688f6c88f4463afc93938fe3008dca8d16794d39..8023bfb00879c67ae55abac46787fb9fc9ea58aa 100644 (file)
@@ -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;
 }
-
-
index 1c8fe8ba0c914a3a272124a44aaa9eebc3682ded..c9374fa3a3fc51c6b0f140ecf88d47688b22f444 100644 (file)
@@ -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