Code

utils: pass preallocated buffer to format_duration_long()
[ncmpc.git] / src / utils.c
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;
 }
-
-