core/string: Move sec2str() out of "string" namespace
And I replace it with a simple call to g_strdup_printf() that does the exact same thing. Note that callers are now required to free the returned string with g_free(). Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
parent
82d1da491b
commit
a314ee03ca
|
@ -2,25 +2,15 @@
|
|||
* Copyright 2015 (c) Anna Schumaker.
|
||||
*/
|
||||
#include <core/string.h>
|
||||
#include <glib.h>
|
||||
#include <sstream>
|
||||
|
||||
#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,
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
#ifndef OCARINA_CORE_STRING_H
|
||||
#define OCARINA_CORE_STRING_H
|
||||
|
||||
#include <glib.h>
|
||||
#include <string>
|
||||
|
||||
/**
|
||||
|
@ -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 */
|
||||
|
|
|
@ -5,21 +5,29 @@
|
|||
#include <core/string.h>
|
||||
#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()
|
||||
|
|
Loading…
Reference in New Issue