From cc6f4c92931eeb5ce914df6b7965ae2b029139cd Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Thu, 29 Jan 2015 08:27:25 -0500 Subject: [PATCH] string: Add a function for creating a detailed time string I use this to display the total running time of queues in the gui. Signed-off-by: Anna Schumaker --- core/queue.cpp | 27 +++------------------------ core/string.cpp | 36 ++++++++++++++++++++++++++++++++++++ gui/tabs.cpp | 2 +- include/core/queue.h | 4 ++-- include/core/string.h | 10 +++++++++- tests/core/queue.cpp | 29 +++++++++++------------------ tests/core/string.cpp | 24 ++++++++++++++++++++++++ 7 files changed, 86 insertions(+), 46 deletions(-) diff --git a/core/queue.cpp b/core/queue.cpp index 7f08870e..3aacb9af 100644 --- a/core/queue.cpp +++ b/core/queue.cpp @@ -5,14 +5,11 @@ #include #include #include +#include #include #include -#define O_MINUTES (60) -#define O_HOURS (60 * O_MINUTES) -#define O_DAYS (24 * O_HOURS) - Queue :: Queue(unsigned int flags) : _cur(-1), _flags(flags), _length(0) @@ -201,27 +198,9 @@ unsigned int Queue :: size() return _tracks.size(); } -const std::string Queue :: length_str() +unsigned int Queue :: length() { - std::stringstream ss; - unsigned int factor[4] = { O_DAYS, O_HOURS, O_MINUTES, 1 }; - std::string fields[4] = { "day", "hour", "minute", "second" }; - unsigned int len = _length; - - for (unsigned int i = 0; i < 4; i++) { - unsigned int dur = len / factor[i]; - len -= dur * factor[i]; - - if (dur > 0) { - ss << dur << " " << fields[i]; - if (dur > 1) - ss << "s"; - if (len > 0) - ss << ", "; - } - } - - return ss.str(); + return _length; } class SortTracks { diff --git a/core/string.cpp b/core/string.cpp index 5e056c61..838cfa0b 100644 --- a/core/string.cpp +++ b/core/string.cpp @@ -5,6 +5,10 @@ #include #include +#define O_MINUTES (60) +#define O_HOURS (60 * O_MINUTES) +#define O_DAYS (24 * O_HOURS) + const std::string string :: utos(unsigned int u) { std::stringstream ss; @@ -24,3 +28,35 @@ const std::string string :: sec2str(unsigned int sec) ss << seconds; return ss.str(); } + +const std::string _time_detail(unsigned int value, unsigned int remaining, + const std::string &field) +{ + std::string res; + + if (value > 0) { + res += string :: utos(value) + " " + field; + if (value > 1) + res += "s"; + if (remaining > 0) + res += ", "; + } + return res; +} + +const std::string string :: sec2str_detailed(unsigned int sec) +{ + std::string res; + unsigned int factor[3] = { O_DAYS, O_HOURS, O_MINUTES }; + std::string field[3] = { "day", "hour", "minute" }; + unsigned int val; + + for (unsigned int i = 0; i < 3; i++) { + val = sec / factor[i]; + sec %= factor[i]; + res += _time_detail(val, sec, field[i]); + } + + res += _time_detail(sec, 0, "second"); + return res; +} diff --git a/gui/tabs.cpp b/gui/tabs.cpp index 367a2efe..2f585e72 100644 --- a/gui/tabs.cpp +++ b/gui/tabs.cpp @@ -110,7 +110,7 @@ bool Tab :: tab_is_cur() void Tab :: tab_runtime_changed() { if (tab_is_cur()) - gui :: get_widget("o_queue_time")->set_text(tab_pq->length_str()); + gui :: get_widget("o_queue_time")->set_text(string :: sec2str_detailed(tab_pq->length())); } void Tab :: tab_display_sorting() diff --git a/include/core/queue.h b/include/core/queue.h index 04f203aa..8131dbb2 100644 --- a/include/core/queue.h +++ b/include/core/queue.h @@ -168,9 +168,9 @@ public: /** * Find the runtime of the queue. * - * @return The runtime of the queue, as a string. + * @return The runtime of the queue. */ - const std::string length_str(); + unsigned int length(); /** diff --git a/include/core/string.h b/include/core/string.h index 10d44b3f..a1c5a369 100644 --- a/include/core/string.h +++ b/include/core/string.h @@ -22,10 +22,18 @@ namespace string /** * Convert from seconds to a time string. - * @param sec Number of seconds. + * @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. + * @return A string listing the length in terms of days, + * months, hours, minutes, and seconds. + */ + const std::string sec2str_detailed(unsigned int); } #endif /* OCARINA_CORE_STRING_H */ diff --git a/tests/core/queue.cpp b/tests/core/queue.cpp index 209cf604..b064ec43 100644 --- a/tests/core/queue.cpp +++ b/tests/core/queue.cpp @@ -22,7 +22,6 @@ public: TestQueue(unsigned int f) : Queue(f) {} unsigned int get_cur() { return _cur; } unsigned int get_flags() { return _flags; } - unsigned int get_length() { return _length; } std::vector get_sorder() { return _sort_order; }; }; @@ -37,7 +36,7 @@ void test_default() test_equal(q.get_cur(), (unsigned)-1); test_equal(q.get_flags(), (unsigned)0); - test_equal(q.get_length(), (unsigned)0); + test_equal(q.length(), (unsigned)0); test_equal(q.get_sorder().size(), (size_t)0); test_equal(q.next(), (Track *)NULL); } @@ -49,7 +48,7 @@ void test_constructor() test_equal(q.get_cur(), (unsigned)-1); test_equal(q.get_flags(), flags); - test_equal(q.get_length(), (unsigned)0); + test_equal(q.length(), (unsigned)0); test_equal(q.get_sorder().size(), (size_t)0); test_equal(q.next(), (Track *)NULL); } @@ -132,46 +131,40 @@ void test_add_remove() get_callbacks()->on_queue_track_add = test_add_cb; get_callbacks()->on_queue_track_del = test_del_cb; - test_equal(q.get_length(), expected); - test_equal(q.length_str(), (std::string)""); + test_equal(q.length(), expected); test_equal(q.size(), (unsigned)0); /* Add tracks */ test_for_each(0, 24, 1, _test_add_loop); - test_equal(q.get_length(), expected); - test_equal(q.length_str(), (std::string)"1 hour, 26 minutes"); + test_equal(q.length(), expected); test_equal(q.size(), (unsigned)24); /* Add everything again */ test_for_each(24, 48, 1, _test_add_loop); - test_equal(q.get_length(), expected); - test_equal(q.length_str(), (std::string)"2 hours, 52 minutes"); + test_equal(q.length(), expected); test_equal(q.size(), (unsigned)48); /* Test removing multiple tracks at once */ count_del = 0; test_for_each(0, 12, 1, _test_del_loop); - test_equal(q.get_length(), expected); - test_equal(q.length_str(), (std::string)"1 hour, 50 minutes"); + test_equal(q.length(), expected); test_equal(q.size(), (unsigned)24); /* Test removing tracks one at a time */ count_del = 0; test_for_each(0, 12, 1, _test_del_middle_loop); - test_equal(q.get_length(), expected); - test_equal(q.length_str(), (std::string)"55 minutes"); + test_equal(q.length(), expected); test_equal(q.size(), (unsigned)12); /* Remove remaining tracks */ count_del = 0; test_for_each(0, 12, 1, _test_del_front_loop); - test_equal(q.get_length(), (unsigned)0); - test_equal(q.length_str(), (std::string)""); + test_equal(q.length(), (unsigned)0); test_equal(q.size(), (unsigned)0); } @@ -254,7 +247,7 @@ void test_next() test_for_each(0, 24, 1, _test_next_loop); test_equal(q.size(), (unsigned)0); - test_equal(q.length_str(), (std::string)""); + test_equal(q.length(), 0); test_equal(q.next(), TRACK_NULL); @@ -264,7 +257,7 @@ void test_next() test_for_each(0, 24, 1, _test_next_rand_loop); test_equal(q.size(), (unsigned)0); - test_equal(q.length_str(), (std::string)""); + test_equal(q.length(), 0); test_equal(q.next(), TRACK_NULL); @@ -380,7 +373,7 @@ void test_saving() test_equal(r.has_flag(Q_RANDOM), q.has_flag(Q_RANDOM)); test_equal(r.size(), q.size()); - test_equal(r.length_str(), q.length_str()); + test_equal(r.length(), q.length()); test_for_each(0, 24, 1, _test_saving_loop); } diff --git a/tests/core/string.cpp b/tests/core/string.cpp index fe979255..8976b981 100644 --- a/tests/core/string.cpp +++ b/tests/core/string.cpp @@ -29,8 +29,32 @@ void test_sec2str() test_equal(string :: sec2str(675), "11:15"); } +void test_sec2str_detailed() +{ + + 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(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(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(string :: sec2str_detailed(minute + 1), "1 minute, 1 second"); + test_equal(string :: sec2str_detailed(1), "1 second"); + test_equal(string :: sec2str_detailed(2), "2 seconds"); +} + int main(int argc, char **argv) { test :: run("Unsigned to String Test", test_utos); test :: run("Seconds to String Test", test_sec2str); + test :: run("Seconds to String (Detailed) Test", test_sec2str_detailed); }