2015-09-08 09:14:39 -04:00
|
|
|
/*
|
2015-01-26 09:40:48 -05:00
|
|
|
* Copyright 2015 (c) Anna Schumaker.
|
|
|
|
*/
|
|
|
|
#include <core/string.h>
|
|
|
|
|
2015-09-02 08:13:21 -04:00
|
|
|
#define O_SECONDS (1)
|
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-09-02 08:13:21 -04:00
|
|
|
static unsigned int factor[4] = { O_DAYS, O_HOURS, O_MINUTES, O_SECONDS };
|
|
|
|
static const char *field[4] = { "day", "hour", "minute", "second" };
|
|
|
|
|
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-09-03 09:31:48 -04:00
|
|
|
gchar *string_sec2str_long(unsigned int sec)
|
2015-01-29 08:27:25 -05:00
|
|
|
{
|
2015-09-02 08:13:21 -04:00
|
|
|
gchar *tmp;
|
2015-01-29 08:27:25 -05:00
|
|
|
unsigned int val;
|
2015-09-03 09:31:48 -04:00
|
|
|
gchar *res = g_strdup("");
|
2015-01-29 08:27:25 -05:00
|
|
|
|
2015-09-02 08:13:21 -04:00
|
|
|
for (unsigned int i = 0; i < 4; i++) {
|
2015-01-29 08:27:25 -05:00
|
|
|
val = sec / factor[i];
|
|
|
|
sec %= factor[i];
|
2015-09-02 08:13:21 -04:00
|
|
|
if (val == 0)
|
|
|
|
continue;
|
|
|
|
|
2015-09-03 09:31:48 -04:00
|
|
|
tmp = g_strdup_printf("%s%u %s%s%s", res, val, field[i],
|
2015-09-02 08:13:21 -04:00
|
|
|
(val > 1) ? "s" : "",
|
|
|
|
(sec > 0) ? ", " : "");
|
2015-09-03 09:31:48 -04:00
|
|
|
g_free(res);
|
|
|
|
res = tmp;
|
2015-01-29 08:27:25 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
2015-01-30 09:32:27 -05:00
|
|
|
|
2015-10-28 03:55:40 -04:00
|
|
|
gchar *string_tm2str(struct tm *tm)
|
|
|
|
{
|
|
|
|
gchar *buf = g_malloc(20 * sizeof(gchar));
|
|
|
|
strftime(buf, 20, "%Ex", tm);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
2016-05-19 11:00:40 -04:00
|
|
|
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;
|
|
|
|
}
|
2016-05-19 12:02:56 -04:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2017-04-11 09:12:17 -04:00
|
|
|
|
|
|
|
bool string_is_subdir(const gchar *a, const gchar *b)
|
|
|
|
{
|
|
|
|
gchar **parent = b ? g_strsplit(b, "/", -1) : NULL;
|
|
|
|
gchar **child = a ? g_strsplit(a, "/", -1) : NULL;
|
|
|
|
bool subdir = true;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (!parent || !child)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (i = 0; parent[i]; i++) {
|
|
|
|
if (!child[i] || g_utf8_collate(parent[i], child[i]) != 0) {
|
|
|
|
subdir = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
g_strfreev(parent);
|
|
|
|
g_strfreev(child);
|
|
|
|
return subdir;
|
|
|
|
}
|