diff --git a/core/string.cpp b/core/string.cpp index 4584f931..ddd1fc85 100644 --- a/core/string.cpp +++ b/core/string.cpp @@ -2,25 +2,15 @@ * Copyright 2015 (c) Anna Schumaker. */ #include -#include -#include #define O_MINUTES (60) #define O_HOURS (60 * O_MINUTES) #define O_DAYS (24 * O_HOURS) -const std::string string :: sec2str(unsigned int sec) +gchar *string_sec2str(unsigned int sec) { - std::stringstream ss; - unsigned int minutes = sec / 60; - unsigned int seconds = sec % 60; - - ss << minutes << ":"; - if (seconds < 10) - ss << "0"; - ss << seconds; - return ss.str(); + return g_strdup_printf("%u:%02u", sec / 60, sec % 60); } static const std::string _time_detail(unsigned int value, diff --git a/gui/gst.cpp b/gui/gst.cpp index b605522c..2af30bef 100644 --- a/gui/gst.cpp +++ b/gui/gst.cpp @@ -58,6 +58,7 @@ public: void load(Track *track) { gchar *uri = gst_filename_to_uri(track->path().c_str(), NULL); + gchar *len = string_sec2str(track->length()); gst_change_state(GST_STATE_NULL); g_object_set(G_OBJECT(gst_player), "uri", uri, NULL); @@ -66,7 +67,8 @@ public: set_markup(o_album, "x-large", "From: " + track->album()->name()); set_markup(o_artist, "x-large", "By: " + track->artist()->name()); set_markup(o_title, "xx-large", track->name()); - o_duration->set_text(string :: sec2str(track->length())); + o_duration->set_text(len); + g_free(len); plist :: track_loaded(track); } @@ -181,9 +183,12 @@ static void on_pause_enabled() static bool on_timeout() { - o_position->set_text(string :: sec2str(audio :: position() / GST_SECOND)); + gchar *pos = string_sec2str(audio :: position() / GST_SECOND); + o_progress->set_upper(audio :: duration()); o_progress->set_value(audio :: position()); + o_position->set_text(pos); + g_free(pos); return true; } diff --git a/gui/queue/model.cpp b/gui/queue/model.cpp index b652ef9f..94bd8bc5 100644 --- a/gui/queue/model.cpp +++ b/gui/queue/model.cpp @@ -96,6 +96,7 @@ void QueueModel::get_value_vfunc(const Gtk::TreeIter &iter, int column, Glib::ValueBase &value) const { Track *track; + gchar *str; if (!check_iter_validity(iter) || column > get_n_columns_vfunc()) @@ -109,7 +110,10 @@ void QueueModel::get_value_vfunc(const Gtk::TreeIter &iter, int column, case 1: return set_val(track->name(), value); case 2: - return set_val(string :: sec2str(track->length()), value); + str = string_sec2str(track->length()); + set_val(Glib::ustring(str), value); + g_free(str); + return; case 3: return set_val(track->artist()->name(), value); case 4: diff --git a/include/core/string.h b/include/core/string.h index 84eaa492..1e80ef59 100644 --- a/include/core/string.h +++ b/include/core/string.h @@ -4,6 +4,7 @@ #ifndef OCARINA_CORE_STRING_H #define OCARINA_CORE_STRING_H +#include #include /** @@ -12,13 +13,6 @@ namespace string { - /** - * Convert from seconds to a time string. - * @param sec Number of seconds. - * @return A string in mm:ss format. - */ - const std::string sec2str(unsigned int); - /** * Convert from seconds to a detailed time string. * @param sec Number of seconds. @@ -37,4 +31,10 @@ namespace string } +/* + * Convert number of seconds into a string with format mm:ss. + * This function allocates a new string that MUST be freed with g_free(). + */ +gchar *string_sec2str(unsigned int); + #endif /* OCARINA_CORE_STRING_H */ diff --git a/tests/core/string.cpp b/tests/core/string.cpp index 38b4d49b..30f19491 100644 --- a/tests/core/string.cpp +++ b/tests/core/string.cpp @@ -5,21 +5,29 @@ #include #include "test.h" +static char buf[6]; + +static inline char *swap(gchar *res) +{ + strcpy(buf, res); + g_free(res); + return buf; +} void test_sec2str() { - test_equal(string :: sec2str(0), "0:00"); - test_equal(string :: sec2str(5), "0:05"); - test_equal(string :: sec2str(10), "0:10"); - test_equal(string :: sec2str(60), "1:00"); - test_equal(string :: sec2str(65), "1:05"); - test_equal(string :: sec2str(75), "1:15"); - test_equal(string :: sec2str(600), "10:00"); - test_equal(string :: sec2str(605), "10:05"); - test_equal(string :: sec2str(615), "10:15"); - test_equal(string :: sec2str(660), "11:00"); - test_equal(string :: sec2str(665), "11:05"); - test_equal(string :: sec2str(675), "11:15"); + test_equal(swap(string_sec2str(0)), "0:00"); + test_equal(swap(string_sec2str(5)), "0:05"); + test_equal(swap(string_sec2str(10)), "0:10"); + test_equal(swap(string_sec2str(60)), "1:00"); + test_equal(swap(string_sec2str(65)), "1:05"); + test_equal(swap(string_sec2str(75)), "1:15"); + test_equal(swap(string_sec2str(600)), "10:00"); + test_equal(swap(string_sec2str(605)), "10:05"); + test_equal(swap(string_sec2str(615)), "10:15"); + test_equal(swap(string_sec2str(660)), "11:00"); + test_equal(swap(string_sec2str(665)), "11:05"); + test_equal(swap(string_sec2str(675)), "11:15"); } void test_sec2str_detailed()