summaryrefslogtreecommitdiffstats
path: root/util.c
diff options
context:
space:
mode:
authorLouis Burda <quent.burda@gmail.com>2021-12-20 16:25:43 +0100
committerLouis Burda <quent.burda@gmail.com>2021-12-20 16:25:43 +0100
commitf07580d31d1148c4a1811c36b09ca0ad50d9576b (patch)
tree05a8fbb44af81f3b5937df80c5af3305ec3ed3c9 /util.c
parentfaee8e9c6db45c6adacfc5113d55927f92e8bf29 (diff)
downloadtmus-f07580d31d1148c4a1811c36b09ca0ad50d9576b.tar.gz
tmus-f07580d31d1148c4a1811c36b09ca0ad50d9576b.zip
Restructured repository and added automatic make dependency generation
Diffstat (limited to 'util.c')
-rw-r--r--util.c135
1 files changed, 0 insertions, 135 deletions
diff --git a/util.c b/util.c
deleted file mode 100644
index e8c0935..0000000
--- a/util.c
+++ /dev/null
@@ -1,135 +0,0 @@
-#define _XOPEN_SOURCE 600
-
-#include "util.h"
-
-#include "ncurses.h"
-
-#include <stdarg.h>
-#include <stdlib.h>
-#include <string.h>
-#include <wchar.h>
-#include <wctype.h>
-
-static const char *allowed = "abcdefghijklmnopqrstuvwxyz"
- "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.:,;-_(){}[]";
-
-int
-strnwidth(const char *s, int n)
-{
- mbstate_t shift_state;
- wchar_t wc;
- size_t wc_len;
- size_t width = 0;
-
- memset(&shift_state, '\0', sizeof shift_state);
-
- for (size_t i = 0; i < n; i += wc_len) {
- wc_len = mbrtowc(&wc, s + i, MB_CUR_MAX, &shift_state);
- if (!wc_len) {
- break;
- } else if (wc_len >= (size_t)-2) {
- width += MIN(n - 1, strlen(s + i));
- break;
- } else {
- width += iswcntrl(wc) ? 2 : MAX(0, wcwidth(wc));
- }
- }
-
-done:
- return width;
-}
-
-void
-assert(int cond, const char *file, int line, const char *condstr)
-{
- if (cond) return;
-
- endwin();
- fprintf(stderr, "Assertion failed %s:%i (%s)\n", file, line, condstr);
- exit(1);
-}
-
-char *
-aprintf(const char *fmtstr, ...)
-{
- va_list ap, cpy;
- size_t size;
- char *str;
-
- va_copy(cpy, ap);
-
- va_start(ap, fmtstr);
- size = vsnprintf(NULL, 0, fmtstr, ap);
- va_end(ap);
-
- str = malloc(size + 1);
- ASSERT(str != NULL);
-
- va_start(cpy, fmtstr);
- vsnprintf(str, size + 1, fmtstr, cpy);
- va_end(cpy);
-
- return str;
-}
-
-char *
-appendstrf(char *alloc, const char *fmtstr, ...)
-{
- va_list ap, cpy;
- size_t size, prevlen;
-
- va_copy(cpy, ap);
-
- va_start(ap, fmtstr);
- size = vsnprintf(NULL, 0, fmtstr, ap);
- va_end(ap);
-
- prevlen = alloc ? strlen(alloc) : 0;
- alloc = realloc(alloc, prevlen + size + 1);
- ASSERT(alloc != NULL);
-
- va_start(cpy, fmtstr);
- vsnprintf(alloc + prevlen, size + 1, fmtstr, cpy);
- va_end(cpy);
-
- return alloc;
-}
-
-char *
-sanitized(const char *instr)
-{
- const char *p;
- char *clean;
- int i;
-
- clean = strdup(instr);
- ASSERT(clean != NULL);
- for (i = 0, p = instr; *p; p++) {
- if (strchr(allowed, *p))
- clean[i++] = *p;
- }
- ASSERT(i != 0);
- clean[i] = '\0';
-
- return clean;
-}
-
-const char *
-timestr(unsigned int secs)
-{
- static char buf[16];
- unsigned int mins, hours;
-
- hours = secs / 3600;
- mins = secs / 60 % 60;
- secs = secs % 60;
-
- if (hours) {
- snprintf(buf, sizeof(buf), "%02u:%02u:%02u", hours, mins, secs);
- } else {
- snprintf(buf, sizeof(buf), "%02u:%02u", mins, secs);
- }
-
- return buf;
-}
-