84 lines
1.5 KiB
C
84 lines
1.5 KiB
C
/*
|
|
* Copyright 2015 (c) Anna Schumaker.
|
|
*/
|
|
#include <core/string.h>
|
|
|
|
#define O_SECONDS (1)
|
|
#define O_MINUTES (60)
|
|
#define O_HOURS (60 * O_MINUTES)
|
|
#define O_DAYS (24 * O_HOURS)
|
|
|
|
static unsigned int factor[4] = { O_DAYS, O_HOURS, O_MINUTES, O_SECONDS };
|
|
static const char *field[4] = { "day", "hour", "minute", "second" };
|
|
|
|
|
|
gchar *string_sec2str(unsigned int sec)
|
|
{
|
|
return g_strdup_printf("%u:%02u", sec / 60, sec % 60);
|
|
}
|
|
|
|
gchar *string_sec2str_long(unsigned int sec)
|
|
{
|
|
gchar *tmp;
|
|
unsigned int val;
|
|
gchar *res = g_strdup("");
|
|
|
|
for (unsigned int i = 0; i < 4; i++) {
|
|
val = sec / factor[i];
|
|
sec %= factor[i];
|
|
if (val == 0)
|
|
continue;
|
|
|
|
tmp = g_strdup_printf("%s%u %s%s%s", res, val, field[i],
|
|
(val > 1) ? "s" : "",
|
|
(sec > 0) ? ", " : "");
|
|
g_free(res);
|
|
res = tmp;
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
gchar *string_tm2str(struct tm *tm)
|
|
{
|
|
gchar *buf = g_malloc(20 * sizeof(gchar));
|
|
strftime(buf, 20, "%Ex", tm);
|
|
return buf;
|
|
}
|
|
|
|
int string_compare_tokens(gchar **lhs, gchar **rhs)
|
|
{
|
|
unsigned int i, cmp;
|
|
|
|
if (!lhs[0] && rhs[0])
|
|
return 1;
|
|
if (lhs[0] && !rhs[0])
|
|
return -1;
|
|
|
|
for (i = 0; lhs[i]; i++) {
|
|
if (!rhs[i])
|
|
break;
|
|
cmp = g_utf8_collate(lhs[i], rhs[i]);
|
|
if (cmp != 0)
|
|
return cmp;
|
|
}
|
|
|
|
if (lhs[i])
|
|
return 1;
|
|
if (rhs[i])
|
|
return -1;
|
|
return 0;
|
|
}
|
|
|
|
bool string_match_token(const gchar *prefix, gchar **tokens)
|
|
{
|
|
unsigned int i;
|
|
|
|
for (i = 0; tokens[i]; i++) {
|
|
if (g_str_has_prefix(tokens[i], prefix))
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|