Mercurial > hg > th-libs
diff th_string.c @ 14:e5e6370217ef
Various cleanups, additions and removals.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Sat, 30 Oct 2010 17:47:41 +0300 |
parents | a25f5d22483e |
children | 1f7d8693fda8 |
line wrap: on
line diff
--- a/th_string.c Fri Oct 29 13:48:31 2010 +0300 +++ b/th_string.c Sat Oct 30 17:47:41 2010 +0300 @@ -10,10 +10,6 @@ #endif #include "th_string.h" -#define LPREV (pNode->pPrev) -#define LNEXT (pNode->pNext) - - /* strdup with a NULL check */ char *th_strdup(const char *s) @@ -30,23 +26,7 @@ } -/* Allocate memory for a string with given length - */ -char *th_stralloc(const size_t l) -{ - assert(l > 0); - return th_malloc(sizeof(char) * l); -} - - -char *th_strrealloc(char * s, const size_t l) -{ - assert(l > 0); - return th_realloc(s, sizeof(char) * l); -} - - -char *th_strncpy(char * dst, const char * src, size_t n) +char *th_strncpy(char * dst, const char * src, const size_t n) { const char *s = src; char *d = dst; @@ -56,7 +36,7 @@ /* Copy to the destination */ i = n; - while (*s && (i > 0)) { + while (*s && i > 0) { *(d++) = *(s++); i--; } @@ -74,46 +54,60 @@ } -int th_strncmp(char * str1, char * str2, size_t n) +/* Simulate a sprintf() that allocates memory + */ +char * th_strdup_vprintf(const char *fmt, va_list args) { - char *s1, *s2; - assert(str1 != NULL); - assert(str2 != NULL); - - /* Check the string pointers */ - if (str1 == str2) - return 0; + int size = 100; + char *buf, *nbuf = NULL; - /* Go through the string */ - s1 = str1; - s2 = str2; - while ((n > 0) && *s1 && *s2 && (*s1 == *s2)) { - s1++; - s2++; - n--; + if ((buf = th_malloc(size)) == NULL) + return NULL; + + while (1) { + int n = vsnprintf(buf, size, fmt, args); + if (n > -1 && n < size) + return buf; + if (n > -1) + size = n + 1; + else + size *= 2; + + if ((nbuf = th_realloc(nbuf, size)) == NULL) { + th_free(buf); + return NULL; + } + + buf = nbuf; } +} - if (n > 0) - return ((*s1) - (*s2)); - else - return 0; + +char * th_strdup_printf(const char *fmt, ...) +{ + char *res; + va_list ap; + + va_start(ap, fmt); + res = th_strdup_vprintf(fmt, ap); + va_end(ap); + + return res; } /* Compare two strings ignoring case [strcasecmp, strncasecmp] */ -int th_strcasecmp(char * str1, char * str2) +int th_strcasecmp(const char * str1, const char * str2) { - char *s1 = str1, *s2 = str2; + const char *s1 = str1, *s2 = str2; assert(str1 != NULL); assert(str2 != NULL); - /* Check the string pointers */ if (str1 == str2) return 0; - /* Go through the string */ - while (*s1 && *s2 && (th_tolower(*s1) == th_tolower(*s2))) { + while (*s1 && *s2 && th_tolower(*s1) == th_tolower(*s2)) { s1++; s2++; } @@ -122,27 +116,22 @@ } -int th_strncasecmp(char * str1, char * str2, size_t n) +int th_strncasecmp(const char * str1, const char * str2, size_t n) { - char *s1 = str1, *s2 = str2; + const char *s1 = str1, *s2 = str2; assert(str1 != NULL); assert(str2 != NULL); - /* Check the string pointers */ if (str1 == str2) return 0; - /* Go through the string */ - while ((n > 0) && *s1 && *s2 && (th_tolower(*s1) == th_tolower(*s2))) { + while (n > 0 && *s1 && *s2 && th_tolower(*s1) == th_tolower(*s2)) { s1++; s2++; n--; } - if (n > 0) - return (th_tolower(*s1) - th_tolower(*s2)); - else - return 0; + return n > 0 ? (th_tolower(*s1) - th_tolower(*s2)) : 0; } @@ -168,45 +157,39 @@ /* Copy a given string over in *result. */ -int th_pstrcpy(char ** result, char * str) +int th_pstrcpy(char ** result, const char * str) { assert(result != NULL); - /* Check the string pointers */ if (str == NULL) return -1; - /* Allocate memory for destination */ th_free(*result); - *result = th_stralloc(strlen(str) + 1); - if (!*result) + if ((*result = th_malloc(strlen(str) + 1)) == NULL) return -2; - /* Copy to the destination */ strcpy(*result, str); - return 0; } /* Concatenates a given string into string pointed by *result. */ -int th_pstrcat(char ** result, char * str) +int th_pstrcat(char ** result, const char * str) { assert(result != NULL); - /* Check the string pointers */ if (str == NULL) return -1; if (*result != NULL) { - *result = th_strrealloc(*result, strlen(*result) + strlen(str) + 1); + *result = th_realloc(*result, strlen(*result) + strlen(str) + 1); if (*result == NULL) return -1; strcat(*result, str); } else { - *result = th_stralloc(strlen(str) + 1); + *result = th_malloc(strlen(str) + 1); if (*result == NULL) return -1; @@ -221,40 +204,40 @@ * Updates iPos into the position of such character and * returns pointer to the string. */ -char *th_findnext(char * str, size_t * iPos) +const char *th_findnext(const char * str, size_t * pos) { assert(str != NULL); /* Terminating NULL-character is not whitespace! */ - while (th_isspace(str[*iPos])) - (*iPos)++; - return &str[*iPos]; + while (th_isspace(str[*pos])) + (*pos)++; + return &str[*pos]; } -/* Find next chSep-character from string +/* Find next sep-character from string */ -char *th_findsep(char * str, size_t * iPos, char chSep) +const char *th_findsep(const char * str, size_t * pos, char sep) { assert(str != NULL); - /* Terminating NULL-character is not digit! */ - while (str[*iPos] && (str[*iPos] != chSep)) - (*iPos)++; - return &str[*iPos]; + while (str[*pos] && str[*pos] != sep) + (*pos)++; + + return &str[*pos]; } -/* Find next chSep- or whitespace from string +/* Find next sep- or whitespace from string */ -char *th_findseporspace(char * str, size_t * iPos, char chSep) +const char *th_findseporspace(const char * str, size_t * pos, char sep) { assert(str != NULL); - /* Terminating NULL-character is not digit! */ - while (!th_isspace(str[*iPos]) && (str[*iPos] != chSep)) - (*iPos)++; - return &str[*iPos]; + while (!th_isspace(str[*pos]) && str[*pos] != sep) + (*pos)++; + + return &str[*pos]; } @@ -263,10 +246,10 @@ * wildcards ? and *. "?" matches any character and "*" matches * any number of characters. */ -BOOL th_strmatch(char * str, char * pattern) +BOOL th_strmatch(const char * str, const char * pattern) { BOOL didMatch = TRUE, isAnyMode = FALSE, isEnd = FALSE; - char *tmpPattern = NULL; + const char *tmpPattern = NULL; /* Check given pattern and string */ if (str == NULL || pattern == NULL) @@ -352,10 +335,10 @@ /* Compare a string to a pattern. Case-INSENSITIVE version. */ -BOOL th_strcasematch(char * str, char * pattern) +BOOL th_strcasematch(const char * str, const char * pattern) { BOOL didMatch = TRUE, isAnyMode = FALSE, isEnd = FALSE; - char *tmpPattern = NULL; + const char *tmpPattern = NULL; /* Check given pattern and string */ if (str == NULL || pattern == NULL) @@ -435,3 +418,24 @@ return didMatch; } + +int th_get_hex_triplet(const char *str) +{ + const char *p = str; + int len, val = 0; + + for (len = 0; *p && len < 6; p++, len++) { + if (*p >= '0' && *p <= '9') { + val *= 16; val += (*p - '0'); + } else if (*p >= 'A' && *p <= 'F') { + val *= 16; val += (*p - 'A') + 10; + } else if (*p >= 'a' && *p <= 'f') { + val *= 16; val += (*p - 'a') + 10; + } else + return -1; + } + + return (len == 6) ? val : -1; +} + +