1
0
mirror of https://github.com/Tha14/toxic.git synced 2025-06-30 03:56:45 +02:00

improve call duration string & remove a couple unused functions

This commit is contained in:
Jfreegman
2014-06-24 01:54:08 -04:00
parent 490c80dae9
commit 7ac7713268
5 changed files with 61 additions and 114 deletions

View File

@ -59,14 +59,14 @@ struct tm *get_time(void)
return timeinfo;
}
/*Puts the current time in buf in the format of [Hour:Min:Sec] */
/*Puts the current time in buf in the format of [HH:mm:ss] */
void get_time_str(uint8_t *buf, int bufsize)
{
const char *t = user_settings->time == TIME_12 ? "[%-I:%M:%S] " : "[%H:%M:%S] ";
strftime(buf, bufsize, t, get_time());
}
/* Converts seconds to hours:minutes:seconds string */
/* Converts seconds to string in format HH:mm:ss; truncates hours and minutes when necessary */
void get_elapsed_time_str(uint8_t *buf, int bufsize, uint64_t secs)
{
if (!secs)
@ -76,7 +76,12 @@ void get_elapsed_time_str(uint8_t *buf, int bufsize, uint64_t secs)
uint64_t minutes = (secs % 3600) / 60;
uint64_t hours = secs / 3600;
snprintf(buf, bufsize, "%.2ld:%.2ld:%.2ld", hours, minutes, seconds);
if (!minutes && !hours)
snprintf(buf, bufsize, "%.2ld", seconds);
else if (!hours)
snprintf(buf, bufsize, "%ld:%.2ld", minutes, seconds);
else
snprintf(buf, bufsize, "%ld:%.2ld:%.2ld", hours, minutes, seconds);
}
char *hex_string_to_bin(const char *hex_string)
@ -84,11 +89,8 @@ char *hex_string_to_bin(const char *hex_string)
size_t len = strlen(hex_string);
char *val = malloc(len);
if (val == NULL) {
endwin();
fprintf(stderr, "malloc() failed. Aborting...\n");
exit(EXIT_FAILURE);
}
if (val == NULL)
exit_toxic_err("failed in hex_string_to_bin", FATALERR_MEMORY);
size_t i;
@ -104,7 +106,7 @@ int string_is_empty(char *string)
return string[0] == '\0';
}
/* convert a multibyte string to a wide character string (must provide buffer) */
/* convert a multibyte string to a wide character string and puts in buf. */
int mbs_to_wcs_buf(wchar_t *buf, const uint8_t *string, size_t n)
{
size_t len = mbstowcs(NULL, string, 0) + 1;
@ -112,14 +114,13 @@ int mbs_to_wcs_buf(wchar_t *buf, const uint8_t *string, size_t n)
if (n < len)
return -1;
if ((len = mbstowcs(buf, string, n)) == (size_t) - 1)
if ((len = mbstowcs(buf, string, n)) == (size_t) -1)
return -1;
return len;
}
/* converts wide character string into a multibyte string.
Same thing as wcs_to_mbs() but caller must provide its own buffer */
/* converts wide character string into a multibyte string and puts in buf. */
int wcs_to_mbs_buf(uint8_t *buf, const wchar_t *string, size_t n)
{
size_t len = wcstombs(NULL, string, 0) + 1;
@ -127,59 +128,12 @@ int wcs_to_mbs_buf(uint8_t *buf, const wchar_t *string, size_t n)
if (n < len)
return -1;
if ((len = wcstombs(buf, string, n)) == (size_t) - 1)
if ((len = wcstombs(buf, string, n)) == (size_t) -1)
return -1;
return len;
}
/* convert wide characters to multibyte string: string returned must be freed */
uint8_t *wcs_to_mbs(wchar_t *string)
{
uint8_t *ret = NULL;
size_t len = wcstombs(NULL, string, 0);
if (len != (size_t) - 1) {
ret = malloc(++len);
if (ret != NULL) {
if (wcstombs(ret, string, len) == (size_t) - 1)
return NULL;
}
} else {
ret = malloc(2);
if (ret != NULL) {
ret[0] = ' ';
ret[1] = '\0';
}
}
if (ret == NULL) {
endwin();
fprintf(stderr, "malloc() failed. Aborting...\n");
exit(EXIT_FAILURE);
}
return ret;
}
/* convert a wide char to multibyte string */
char *wc_to_char(wchar_t ch)
{
static char ret[MB_LEN_MAX + 1];
int len = wctomb(ret, ch);
if (len == -1) {
ret[0] = ' ';
ret[1] = '\0';
} else {
ret[len] = '\0';
}
return ret;
}
/* Returns 1 if connection has timed out, 0 otherwise */
int timed_out(uint64_t timestamp, uint64_t curtime, uint64_t timeout)
{