/* * Copyright 2015 (c) Anna Schumaker. */ #include #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; } static gunichar __string_get_char(const gchar *str, const gchar *cur, const gchar *res) { gunichar c = g_utf8_get_char(cur); gchar *prev = g_utf8_find_prev_char(str, res); if (g_unichar_ismark(c)) return '\0'; if (g_unichar_ispunct(c)) return '\0'; if (g_unichar_isspace(c)) { if (!prev || (*prev == ' ')) return '\0'; return ' '; } return g_unichar_tolower(c); } gchar *string_lowercase(const gchar *str) { gchar *res = g_utf8_normalize(str, -1, G_NORMALIZE_DEFAULT); gchar *i, *j = res; gunichar c; for (i = res; *i != '\0'; i = g_utf8_next_char(i)) { c = __string_get_char(res, i, j); if (c) { *j = c; j = g_utf8_next_char(j); } } *j = '\0'; g_strchomp(res); return g_realloc(res, strlen(res) + 1); } int string_compare(const gchar *lhs, const gchar *rhs) { if (strlen(lhs) == 0) { if (strlen(rhs) == 0) return 0; return 1; } if (strlen(rhs) == 0) return -1; return g_utf8_collate(lhs, rhs); }