2015-01-26 09:40:48 -05:00
|
|
|
/**
|
|
|
|
* Copyright 2015 (c) Anna Schumaker.
|
|
|
|
*/
|
|
|
|
#include <core/string.h>
|
|
|
|
|
2015-01-29 08:27:25 -05:00
|
|
|
#define O_MINUTES (60)
|
|
|
|
#define O_HOURS (60 * O_MINUTES)
|
|
|
|
#define O_DAYS (24 * O_HOURS)
|
|
|
|
|
2015-01-27 08:34:32 -05:00
|
|
|
|
2015-09-02 08:02:41 -04:00
|
|
|
gchar *string_sec2str(unsigned int sec)
|
2015-01-27 08:34:32 -05:00
|
|
|
{
|
2015-09-02 08:02:41 -04:00
|
|
|
return g_strdup_printf("%u:%02u", sec / 60, sec % 60);
|
2015-01-27 08:34:32 -05:00
|
|
|
}
|
2015-01-29 08:27:25 -05:00
|
|
|
|
2015-01-30 10:54:03 -05:00
|
|
|
static const std::string _time_detail(unsigned int value,
|
|
|
|
unsigned int remaining,
|
|
|
|
const std::string &field)
|
2015-01-29 08:27:25 -05:00
|
|
|
{
|
|
|
|
std::string res;
|
2015-09-01 08:58:34 -04:00
|
|
|
gchar *g_res;
|
2015-01-29 08:27:25 -05:00
|
|
|
|
|
|
|
if (value > 0) {
|
2015-09-01 08:58:34 -04:00
|
|
|
g_res = g_strdup_printf("%u %s%s%s", value, field.c_str(),
|
|
|
|
(value > 1) ? "s" : "",
|
|
|
|
(remaining > 0) ? ", " : "");
|
|
|
|
res = g_res;
|
|
|
|
g_free(g_res);
|
2015-01-29 08:27:25 -05:00
|
|
|
}
|
2015-09-01 08:58:34 -04:00
|
|
|
|
2015-01-29 08:27:25 -05:00
|
|
|
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;
|
|
|
|
}
|
2015-01-30 09:32:27 -05:00
|
|
|
|
|
|
|
static char _to_lower(char c)
|
|
|
|
{
|
|
|
|
if ((c >= 'a') && (c <= 'z'))
|
|
|
|
return c;
|
|
|
|
if ((c >= '0') && (c <= '9'))
|
|
|
|
return c;
|
|
|
|
if ((c >= 'A') && (c <= 'Z'))
|
|
|
|
return c + ('a' - 'A');
|
|
|
|
|
|
|
|
switch (c) {
|
|
|
|
case '\\':
|
|
|
|
case '/':
|
|
|
|
case ',':
|
|
|
|
case ';':
|
|
|
|
case '(':
|
|
|
|
case ')':
|
|
|
|
case '_':
|
|
|
|
case '-':
|
|
|
|
case '~':
|
|
|
|
case '+':
|
|
|
|
case '"':
|
|
|
|
case ' ':
|
|
|
|
case ' ':
|
|
|
|
return ' ';
|
|
|
|
default:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::string string :: lowercase(const std::string &str)
|
|
|
|
{
|
|
|
|
std::string ret;
|
|
|
|
char c, last = ' ';
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < str.size(); i++) {
|
|
|
|
c = _to_lower(str[i]);
|
|
|
|
|
|
|
|
if (c > 0) {
|
|
|
|
if ((c != ' ') || (last != ' '))
|
|
|
|
ret += c;
|
|
|
|
last = c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret[ret.size() - 1] == ' ')
|
|
|
|
return ret.substr(0, ret.size() - 1);
|
|
|
|
return ret;
|
|
|
|
}
|