core/string: Move sec2str_detailed() out of "string" namespace

I also take the opportunity to rename the function to
string_sec2str_long().

Signed-off-by: Anna Schumaker <Anna@OcarinaProject.net>
This commit is contained in:
Anna Schumaker 2015-09-03 09:31:48 -04:00
parent 18b83f5c26
commit 83fe822dc7
4 changed files with 29 additions and 32 deletions

View File

@ -17,12 +17,11 @@ gchar *string_sec2str(unsigned int sec)
return g_strdup_printf("%u:%02u", sec / 60, sec % 60);
}
const std::string string :: sec2str_detailed(unsigned int sec)
gchar *string_sec2str_long(unsigned int sec)
{
gchar *tmp;
unsigned int val;
std::string res;
gchar *g_res = g_strdup("");
gchar *res = g_strdup("");
for (unsigned int i = 0; i < 4; i++) {
val = sec / factor[i];
@ -30,15 +29,13 @@ const std::string string :: sec2str_detailed(unsigned int sec)
if (val == 0)
continue;
tmp = g_strdup_printf("%s%u %s%s%s", g_res, val, field[i],
tmp = g_strdup_printf("%s%u %s%s%s", res, val, field[i],
(val > 1) ? "s" : "",
(sec > 0) ? ", " : "");
g_free(g_res);
g_res = tmp;
g_free(res);
res = tmp;
}
res = g_res;
g_free(g_res);
return res;
}

View File

@ -109,8 +109,10 @@ bool Tab :: tab_is_cur()
void Tab :: tab_runtime_changed()
{
gchar *len = string_sec2str_long(tab_pq->length());
if (tab_is_cur())
gui :: get_widget<Gtk::Label>("o_queue_time")->set_text(string :: sec2str_detailed(tab_pq->length()));
gui :: get_widget<Gtk::Label>("o_queue_time")->set_text(len);
g_free(len);
}
void Tab :: tab_display_sorting()

View File

@ -13,14 +13,6 @@
namespace string
{
/**
* Convert from seconds to a detailed time string.
* @param sec Number of seconds.
* @return A string listing the length in terms of days,
* months, hours, minutes, and seconds.
*/
const std::string sec2str_detailed(unsigned int);
/**
* Convert a string to lowercase, stripping out special characters
* along the way.
@ -37,4 +29,10 @@ namespace string
*/
gchar *string_sec2str(unsigned int);
/*
* Convert number of seconds into a long-form time string.
* This function allocates a new string that MUST be freed with g_free().
*/
gchar *string_sec2str_long(unsigned int);
#endif /* OCARINA_CORE_STRING_H */

View File

@ -5,7 +5,7 @@
#include <core/string.h>
#include "test.h"
static char buf[6];
static char buf[25];
static inline char *swap(gchar *res)
{
@ -30,28 +30,28 @@ void test_sec2str()
test_equal(swap(string_sec2str(675)), "11:15");
}
void test_sec2str_detailed()
void test_sec2str_long()
{
unsigned int minute = 60;
unsigned int hour = 3600;
unsigned int day = 86400;
test_equal(string :: sec2str_detailed(day), "1 day");
test_equal(string :: sec2str_detailed(2 * day), "2 days");
test_equal(swap(string_sec2str_long(day)), "1 day");
test_equal(swap(string_sec2str_long(2 * day)), "2 days");
test_equal(string :: sec2str_detailed(day + hour), "1 day, 1 hour");
test_equal(string :: sec2str_detailed(hour), "1 hour");
test_equal(string :: sec2str_detailed(2 * hour), "2 hours");
test_equal(swap(string_sec2str_long(day + hour)), "1 day, 1 hour");
test_equal(swap(string_sec2str_long(hour)), "1 hour");
test_equal(swap(string_sec2str_long(2 * hour)), "2 hours");
test_equal(string :: sec2str_detailed(hour + minute), "1 hour, 1 minute");
test_equal(string :: sec2str_detailed(minute), "1 minute");
test_equal(string :: sec2str_detailed(2 * minute), "2 minutes");
test_equal(swap(string_sec2str_long(hour + minute)), "1 hour, 1 minute");
test_equal(swap(string_sec2str_long(minute)), "1 minute");
test_equal(swap(string_sec2str_long(2 * minute)), "2 minutes");
test_equal(string :: sec2str_detailed(minute + 1), "1 minute, 1 second");
test_equal(string :: sec2str_detailed(0), "");
test_equal(string :: sec2str_detailed(1), "1 second");
test_equal(string :: sec2str_detailed(2), "2 seconds");
test_equal(swap(string_sec2str_long(minute + 1)), "1 minute, 1 second");
test_equal(swap(string_sec2str_long(0)), "");
test_equal(swap(string_sec2str_long(1)), "1 second");
test_equal(swap(string_sec2str_long(2)), "2 seconds");
}
void test_lowercase()
@ -74,6 +74,6 @@ void test_lowercase()
DECLARE_UNIT_TESTS(
UNIT_TEST("Seconds to String", test_sec2str),
UNIT_TEST("Seconds to String (Detailed)", test_sec2str_detailed),
UNIT_TEST("Seconds to String (Long)", test_sec2str_long),
UNIT_TEST("String Lowercase", test_lowercase),
);