changeset 186:1ee46a95aa8c

Clean up the code, add argument identifiers to function prototypes and sanitize some of them.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 10 Feb 2016 16:34:44 +0200
parents d560371c85c0
children acee96f864f8
files th_string.c th_string.h
diffstat 2 files changed, 87 insertions(+), 88 deletions(-) [+]
line wrap: on
line diff
--- a/th_string.c	Tue Mar 31 11:26:26 2015 +0300
+++ b/th_string.c	Wed Feb 10 16:34:44 2016 +0200
@@ -11,38 +11,38 @@
 
 /* Implementation of strdup() with a NULL check
  */
-char *th_strdup(const char *s)
+char *th_strdup(const char *src)
 {
     char *res;
-    if (s == NULL)
+    if (src == NULL)
         return NULL;
 
-    if ((res = th_malloc(strlen(s) + 1)) == NULL)
+    if ((res = th_malloc(strlen(src) + 1)) == NULL)
         return NULL;
 
-    strcpy(res, s);
+    strcpy(res, src);
     return res;
 }
 
 
 /* Implementation of strndup() with NULL check
  */
-char *th_strndup(const char *s, const size_t n)
+char *th_strndup(const char *src, const size_t n)
 {
     char *res;
     size_t len;
 
-    if (s == NULL)
+    if (src == NULL)
         return NULL;
 
-    len = strlen(s);
+    len = strlen(src);
     if (len > n)
         len = n;
 
     if ((res = th_malloc(len + 1)) == NULL)
         return NULL;
 
-    memcpy(res, s, len);
+    memcpy(res, src, len);
     res[len] = 0;
 
     return res;
@@ -53,24 +53,24 @@
  * according to specified flags. See TH_TRIM_* in
  * th_string.h
  */
-char *th_strdup_trim(const char *s, const int flags)
+char *th_strdup_trim(const char *src, const int flags)
 {
     char *res;
     size_t start, end, len;
-    if (s == NULL)
+    if (src == NULL)
         return NULL;
 
-    len = strlen(s);
+    len = strlen(src);
 
     // Trim start: find first non-whitespace character
     if (flags & TH_TRIM_START)
-        for (start = 0; start < len && th_isspace(s[start]); start++);
+        for (start = 0; start < len && th_isspace(src[start]); start++);
     else
         start = 0;
 
     // Trim end: find last non-whitespace character
     if (flags & TH_TRIM_END)
-        for (end = len; end > start && th_isspace(s[end]); end--);
+        for (end = len; end > start && th_isspace(src[end]); end--);
     else
         end = len;
 
@@ -79,7 +79,7 @@
     if ((res = th_malloc(len + 1)) == NULL)
         return NULL;
 
-    memcpy(res, s, len);
+    memcpy(res, src, len);
     res[len] = 0;
     return res;
 }
@@ -158,13 +158,13 @@
 
 /* Compare two strings ignoring case [strcasecmp, strncasecmp]
  */
-int th_strcasecmp(const char *str1, const char *str2)
+int th_strcasecmp(const char *haystack, const char *needle)
 {
-    const char *s1 = str1, *s2 = str2;
-    assert(str1 != NULL);
-    assert(str2 != NULL);
+    const char *s1 = haystack, *s2 = needle;
+    assert(haystack != NULL);
+    assert(needle != NULL);
 
-    if (str1 == str2)
+    if (haystack == needle)
         return 0;
 
     while (*s1 && *s2 && th_tolower(*s1) == th_tolower(*s2))
@@ -177,13 +177,13 @@
 }
 
 
-int th_strncasecmp(const char *str1, const char *str2, size_t n)
+int th_strncasecmp(const char *haystack, const char *needle, size_t n)
 {
-    const char *s1 = str1, *s2 = str2;
-    assert(str1 != NULL);
-    assert(str2 != NULL);
+    const char *s1 = haystack, *s2 = needle;
+    assert(haystack != NULL);
+    assert(needle != NULL);
 
-    if (str1 == str2)
+    if (haystack == needle)
         return 0;
 
     while (n > 0 && *s1 && *s2 && th_tolower(*s1) == th_tolower(*s2))
@@ -241,48 +241,48 @@
 }
 
 
-/* Copy a given string over in *result.
+/* Copy a given string over in *pdst.
  */
-int th_pstrcpy(char **result, const char *str)
+int th_pstrcpy(char **pdst, const char *src)
 {
-    assert(result != NULL);
+    assert(pdst != NULL);
 
-    if (str == NULL)
+    if (src == NULL)
         return -1;
 
-    th_free(*result);
-    if ((*result = th_malloc(strlen(str) + 1)) == NULL)
+    th_free(*pdst);
+    if ((*pdst = th_malloc(strlen(src) + 1)) == NULL)
         return -2;
 
-    strcpy(*result, str);
+    strcpy(*pdst, src);
     return 0;
 }
 
 
-/* Concatenates a given string into string pointed by *result.
+/* Concatenates a given string into string pointed by *pdst.
  */
-int th_pstrcat(char **result, const char *str)
+int th_pstrcat(char **pdst, const char *src)
 {
-    assert(result != NULL);
+    assert(pdst != NULL);
 
-    if (str == NULL)
+    if (src == NULL)
         return -1;
 
-    if (*result != NULL)
+    if (*pdst != NULL)
     {
-        *result = th_realloc(*result, strlen(*result) + strlen(str) + 1);
-        if (*result == NULL)
+        *pdst = th_realloc(*pdst, strlen(*pdst) + strlen(src) + 1);
+        if (*pdst == NULL)
             return -1;
 
-        strcat(*result, str);
+        strcat(*pdst, src);
     }
     else
     {
-        *result = th_malloc(strlen(str) + 1);
-        if (*result == NULL)
+        *pdst = th_malloc(strlen(src) + 1);
+        if (*pdst == NULL)
             return -1;
 
-        strcpy(*result, str);
+        strcpy(*pdst, src);
     }
 
     return 0;
@@ -336,13 +336,13 @@
  * wildcards ? and *. "?" matches any character and "*" matches
  * any number of characters.
  */
-BOOL th_strmatch(const char *str, const char *pattern)
+BOOL th_strmatch(const char *haystack, const char *pattern)
 {
     BOOL matched = TRUE, any = FALSE, end = FALSE;
     const char *tmp = NULL;
 
     // Check given pattern and string
-    if (str == NULL || pattern == NULL)
+    if (haystack == NULL || pattern == NULL)
         return FALSE;
 
     // Start comparision
@@ -352,11 +352,11 @@
         {
         case '?':
             // Any single character matches
-            if (*str)
+            if (*haystack)
             {
                 matched = TRUE;
                 pattern++;
-                str++;
+                haystack++;
             }
             break;
 
@@ -372,14 +372,14 @@
         case 0:
             if (any)
             {
-                if (*str)
-                    str++;
+                if (*haystack)
+                    haystack++;
                 else
                     end = TRUE;
             }
             else
             {
-                if (*str)
+                if (*haystack)
                 {
                     if (tmp)
                     {
@@ -397,29 +397,29 @@
         default:
             if (any)
             {
-                if (*pattern == *str)
+                if (*pattern == *haystack)
                 {
                     any = FALSE;
                     matched = TRUE;
                 }
                 else
                 {
-                    if (*str)
+                    if (*haystack)
                     {
                         matched = TRUE;
-                        str++;
+                        haystack++;
                     }
                 }
             }
             else
             {
-                if (*pattern == *str)
+                if (*pattern == *haystack)
                 {
                     matched = TRUE;
                     if (*pattern)
                         pattern++;
-                    if (*str)
-                        str++;
+                    if (*haystack)
+                        haystack++;
                 }
                 else
                 {
@@ -432,7 +432,7 @@
                 }
             }
 
-            if (!*str && !*pattern)
+            if (!*haystack && !*pattern)
                 end = TRUE;
             break;
 
@@ -445,13 +445,13 @@
 
 /* Compare a string to a pattern. Case-INSENSITIVE version.
  */
-BOOL th_strcasematch(const char *str, const char *pattern)
+BOOL th_strcasematch(const char *haystack, const char *pattern)
 {
     BOOL matched = TRUE, any = FALSE, end = FALSE;
     const char *tmp = NULL;
 
     // Check given pattern and string
-    if (str == NULL || pattern == NULL)
+    if (haystack == NULL || pattern == NULL)
         return FALSE;
 
     // Start comparision
@@ -459,10 +459,10 @@
         switch (*pattern) {
         case '?':
             // Any single character matches
-            if (*str)
+            if (*haystack)
             {
                 pattern++;
-                str++;
+                haystack++;
             }
             else
                 matched = FALSE;
@@ -479,14 +479,14 @@
         case 0:
             if (any)
             {
-                if (*str)
-                    str++;
+                if (*haystack)
+                    haystack++;
                 else
                     end = TRUE;
             }
             else
             {
-                if (*str)
+                if (*haystack)
                 {
                     if (tmp)
                     {
@@ -504,26 +504,26 @@
         default:
             if (any)
             {
-                if (th_tolower(*pattern) == th_tolower(*str))
+                if (th_tolower(*pattern) == th_tolower(*haystack))
                 {
                     any = FALSE;
                 }
                 else
                 {
-                    if (*str)
-                        str++;
+                    if (*haystack)
+                        haystack++;
                     else
                         matched = FALSE;
                 }
             }
             else
             {
-                if (th_tolower(*pattern) == th_tolower(*str))
+                if (th_tolower(*pattern) == th_tolower(*haystack))
                 {
                     if (*pattern)
                         pattern++;
-                    if (*str)
-                        str++;
+                    if (*haystack)
+                        haystack++;
                 }
                 else
                 {
@@ -537,7 +537,7 @@
                 }
             }
 
-            if (!*str && !*pattern)
+            if (!*haystack && !*pattern)
                 end = TRUE;
 
             break;
@@ -657,5 +657,3 @@
         fprintf(fh, "\n");
     }
 }
-
-
--- a/th_string.h	Tue Mar 31 11:26:26 2015 +0300
+++ b/th_string.h	Wed Feb 10 16:34:44 2016 +0200
@@ -50,23 +50,24 @@
 
 /* Normal NUL-terminated string functions
  */
-char    *th_strdup(const char *);
-char    *th_strndup(const char *, const size_t);
-char    *th_strdup_trim(const char *, const int);
+char    *th_strdup(const char *src);
+char    *th_strndup(const char *src, const size_t n);
+char    *th_strdup_trim(const char *, const int flags);
 
-int     th_strcasecmp(const char *, const char *);
-int     th_strncasecmp(const char *, const char *, size_t);
-char    *th_strrcasecmp(char *str, const char *needle);
-void    th_strip_ctrlchars(char *);
+int     th_strcasecmp(const char *haystack, const char *needle);
+int     th_strncasecmp(const char *haystack, const char *needle, size_t n);
+char    *th_strrcasecmp(char *haystack, const char *needle);
+void    th_strip_ctrlchars(char *str);
 
-char    *th_strdup_vprintf(const char *, va_list);
-char    *th_strdup_printf(const char *, ...);
+char    *th_strdup_vprintf(const char *fmt, va_list ap);
+char    *th_strdup_printf(const char *fmt, ...);
 
-void    th_pstr_vprintf(char **, const char *, va_list);
-void    th_pstr_printf(char **, const char *, ...);
+void    th_pstr_vprintf(char **buf, const char *fmt, va_list ap);
+void    th_pstr_printf(char **buf, const char *fmt, ...);
 
-int     th_pstrcpy(char **, const char *);
-int     th_pstrcat(char **, const char *);
+int     th_pstrcpy(char **pdst, const char *src);
+int     th_pstrcat(char **pdst, const char *src);
+
 
 /* Parsing, matching
  */
@@ -74,10 +75,10 @@
 const char    *th_findsep(const char *, size_t *, char);
 const char    *th_findseporspace(const char *, size_t *, char);
 
-BOOL    th_strmatch(const char *, const char *);
-BOOL    th_strcasematch(const char *, const char *);
+BOOL    th_strmatch(const char *haystack, const char *pattern);
+BOOL    th_strcasematch(const char *haystack, const char *pattern);
 
-int     th_get_hex_triplet(const char *);
+int     th_get_hex_triplet(const char *str);
 BOOL    th_get_boolean(const char *str, BOOL *value);
 
 void    th_print_wrap(FILE *fh, const char *str, int spad, int rpad, int width);