summaryrefslogtreecommitdiffstats
path: root/util.c
diff options
context:
space:
mode:
authorLouis Burda <quent.burda@gmail.com>2021-12-04 13:48:01 +0100
committerLouis Burda <quent.burda@gmail.com>2021-12-20 15:31:16 +0100
commit9fe644f0d99375ffd3011d8828f7dbd0fb103af0 (patch)
tree71ed89f1c7316dba818bc921d772aaa1fc957dab /util.c
parentd68d0f39ab7675745e7d177f6774736f7ec58783 (diff)
downloadtmus-9fe644f0d99375ffd3011d8828f7dbd0fb103af0.tar.gz
tmus-9fe644f0d99375ffd3011d8828f7dbd0fb103af0.zip
Added more gui interaction
Diffstat (limited to 'util.c')
-rw-r--r--util.c46
1 files changed, 46 insertions, 0 deletions
diff --git a/util.c b/util.c
index b9f37de..675dc3c 100644
--- a/util.c
+++ b/util.c
@@ -2,11 +2,15 @@
#include "util.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)
{
@@ -41,3 +45,45 @@ assert(int cond, const char *file, int line, const char *condstr)
fprintf(stderr, "Assertion failed %s:%i (%s)", file, line, condstr);
exit(1);
}
+
+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;
+}
+
+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;
+}