]> git.xonotic.org Git - xonotic/gmqcc.git/blobdiff - util.c
util_strtocmd, util_strtononcmd
[xonotic/gmqcc.git] / util.c
diff --git a/util.c b/util.c
index 9a23aed96400f62270240cc7d3d3d5421858e9da..4f4092830e539e5dece0e6d47bab273653a2cfbc 100644 (file)
--- a/util.c
+++ b/util.c
@@ -137,39 +137,6 @@ char *util_strchp(const char *s, const char *e) {
     return util_strdup(s);
 }
 
-/*
- * Remove newline from a string (if it exists).  This is
- * done pointer wise instead of strlen(), and an array
- * access.
- */
-char *util_strrnl(const char *src) {
-    char   *cpy = NULL;
-
-    if (src) {
-        cpy = (char*)src;
-        while (*cpy && *cpy != '\n')
-            cpy++;
-
-        *cpy = '\0';
-    }
-    return (char*)src;
-}
-
-/*
- * Removes any whitespace prefixed on a string by moving
- * skipping past it, and stroing the skip distance, so
- * the string can later be freed (if it was allocated)
- */
-char *util_strsws(const char *skip) {
-    size_t size = 0;
-    if (!skip)
-        return NULL;
-
-    while (*skip == ' ' || *skip == '\t')
-        skip++,size++;
-    return util_strdup(skip-size);
-}
-
 /*
  * Returns true if string is all uppercase, otherwise
  * it returns false.
@@ -196,6 +163,10 @@ bool util_strdigit(const char *str) {
     return true;
 }
 
+bool util_strncmpexact(const char *src, const char *ned, size_t len) {
+    return (!strncmp(src, ned, len) && !src[len]);
+}
+
 void util_debug(const char *area, const char *ms, ...) {
     va_list  va;
     if (!opts_debug)
@@ -399,3 +370,31 @@ int util_getline(char **lineptr, size_t *n, FILE *stream) {
     *pos = '\0';
     return (ret = pos - *lineptr);
 }
+
+size_t util_strtocmd(const char *in, char *out, size_t outsz) {
+    size_t sz = 1;
+    for (; *in && sz < outsz; ++in, ++out, ++sz) {
+        if (*in == '-')
+            *out = '_';
+        else if (isalpha(*in) && !isupper(*in))
+            *out = *in + 'A' - 'a';
+        else
+            *out = *in;
+    }
+    *out = 0;
+    return sz-1;
+}
+
+size_t util_strtononcmd(const char *, char *, size_t) {
+    size_t sz = 1;
+    for (; *in && sz < outsz; ++in, ++out, ++sz) {
+        if (*in == '_')
+            *out = '-';
+        else if (isalpha(*in) && isupper(*in))
+            *out = *in + 'a' - 'A';
+        else
+            *out = *in;
+    }
+    *out = 0;
+    return sz-1;
+}