Mercurial > hg > th-libs
view th_string.c @ 641:9a1ed82abefd
Fix parsing of +? and *?.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Fri, 24 Jan 2020 09:36:43 +0200 |
parents | d3aace9903fa |
children | da9618444c81 |
line wrap: on
line source
/* * Miscellaneous string-handling related utility-functions * Programmed and designed by Matti 'ccr' Hamalainen * (C) Copyright 2002-2020 Tecnic Software productions (TNSP) * * Please read file 'COPYING' for information on license and distribution. */ #include "th_util.h" #include "th_string.h" // Include printf implementation #include "th_printf.c" /** * Implementation of strdup() with a @c NULL check. * @param[in] src string to create a copy of * @returns copy of the given string, or @c NULL if @p src was @c NULL or memory could not be allocated. */ char *th_strdup(const char *src) { char *res; size_t len; if (src == NULL) return NULL; len = strlen(src); if ((res = th_malloc(len + 1)) == NULL) return NULL; memcpy(res, src, len + 1); return res; } /** * Implementation of strndup() with @c NULL check. Copies up to @p n * characters from the source. A NUL terminator is always added, * unless @p src was @c NULL or memory could not be allocated, in which * case @c NULL pointer is returned. * @param[in] src string pointer to be copied from * @param[in] n maximum number of characters to copy * @returns the resulting string or @c NULL pointer */ char *th_strndup(const char *src, const size_t n) { char *res; size_t len; if (src == NULL) return NULL; len = strlen(src); if (len > n) len = n; if ((res = th_malloc(len + 1)) == NULL) return NULL; memcpy(res, src, len); res[len] = 0; return res; } /** * Helper function for th_strdup_trim() and friends. Copies @p len characters from * given string, and trims whitespace from it according to specified @p flags. * See TH_TRIM_* in th_string.h. If @p len or the resulting trimmed string would * be empty (length 0), no copy is allocated and a @c NULL pointer is returned. * @param[in] src source string (does not need to be NUL terminated, as length must be specified) * @param[in] len length of the source string, or desired number of characters to copy at maximum * @param[in] flags trimming flags, see TH_TRIM_* in th_string.h * @returns the resulting string or @c NULL pointer */ static char * th_strdup_trim_do(const char *src, size_t len, const int flags) { char *res; size_t start, end; if (len == 0) return NULL; // Trim start: find first non-whitespace character if (flags & TH_TRIM_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 - 1; end > start && th_isspace(src[end]); end--); else end = len; // Allocate memory for result if (src[end] == 0 || th_isspace(src[end])) return NULL; len = end - start + 1; if ((res = th_malloc(len + 1)) == NULL) return NULL; memcpy(res, src + start, len); res[len] = 0; return res; } /** * Create copy of the given string, but trim the result according to specified @p flags. * See TH_TRIM_* in th_string.h. If length the resulting trimmed string would * be empty (0), no copy is allocated and a NULL pointer is returned. * @param[in] src source string * @param[in] flags trimming flags, see TH_TRIM_* in th_string.h * @returns the resulting string or @c NULL pointer */ char *th_strdup_trim(const char *src, const int flags) { if (src == NULL) return NULL; return th_strdup_trim_do(src, strlen(src), flags); } /** * Create copy of the given string @p src, up to @p n characters (or less, if the string * is shorter.) The result is trimmed according to specified @p flags. * See TH_TRIM_* in th_string.h. If @p n or the resulting trimmed string would * be empty (length 0), no copy is allocated and a NULL pointer is returned. * @param[in] src source string * @param[in] n maximum number of characters to copy from the source string * @param[in] flags trimming flags, see TH_TRIM_* in th_string.h * @returns the resulting string or @c NULL pointer */ char *th_strndup_trim(const char *src, const size_t n, const int flags) { size_t len; if (src == NULL || n == 0) return NULL; for (len = 0; len < n && src[len]; len++); return th_strdup_trim_do(src, len, flags); } char *th_strndup_no0(const char *src, const size_t len) { char *res; if ((res = th_malloc(len + 1)) == NULL) return NULL; memcpy(res, src, len); res[len] = 0; return res; } char *th_strndup_no0_trim(const char *src, const size_t len, const int flags) { return th_strdup_trim_do(src, len, flags); } #ifdef TH_USE_INTERNAL_SPRINTF static int th_pbuf_vputch(th_vprintf_ctx *ctx, const char ch) { if (ctx->pos < ctx->size) ctx->buf[ctx->pos] = ch; ctx->pos++; ctx->ipos++; return ch; } #endif /** * Wrapper for either libc vsnprintf() or th-libs internal * vsnprintf() implementation, as determined by a compile-time define. * @param[out] buf pointer to buffer to print to * @param[in] size available space in the buffer in bytes * @param[in] fmt format string * @param[in] ap a va_list variable arguments list structure */ int th_vsnprintf(char *buf, size_t size, const char *fmt, va_list ap) { #ifdef TH_USE_INTERNAL_SPRINTF int ret; th_vprintf_ctx ctx; ctx.buf = buf; ctx.size = size; ctx.pos = 0; ctx.ipos = 0; ret = th_vprintf_do(&ctx, th_pbuf_vputch, fmt, ap); if (ctx.pos < size) buf[ctx.pos] = 0; else if (size > 0) buf[size - 1] = 0; return ret; #else return vsnprintf(buf, size, fmt, ap); #endif } /** * Wrapper for either libc snprintf() or th-libs internal * snprintf() implementation, as determined by a compile-time define. * @param[out] buf pointer to buffer to print to * @param[in] size available space in the buffer in bytes * @param[in] fmt format string * @param[in] ... variable arguments */ int th_snprintf(char *buf, size_t size, const char *fmt, ...) { int n; va_list ap; va_start(ap, fmt); #ifdef TH_USE_INTERNAL_SPRINTF n = th_vsnprintf(buf, size, fmt, ap); #else n = vsnprintf(buf, size, fmt, ap); #endif va_end(ap); return n; } #ifdef TH_USE_INTERNAL_SPRINTF static int th_stdio_vputch(th_vprintf_ctx *ctx, const char ch) { ctx->pos++; ctx->ipos++; return fputc(ch, (FILE *) ctx->data); } #endif int th_vfprintf(FILE *fh, const char *fmt, va_list ap) { #ifdef TH_USE_INTERNAL_SPRINTF th_vprintf_ctx ctx; ctx.data = (void *) fh; ctx.pos = 0; ctx.ipos = 0; return th_vprintf_do(&ctx, th_stdio_vputch, fmt, ap); #else return vfprintf(fh, fmt, ap); #endif } int th_fprintf(FILE *fh, const char *fmt, ...) { int ret; va_list ap; #ifdef TH_USE_INTERNAL_SPRINTF th_vprintf_ctx ctx; #endif va_start(ap, fmt); #ifdef TH_USE_INTERNAL_SPRINTF ctx.data = (void *) fh; ctx.pos = 0; ctx.ipos = 0; ret = th_vprintf_do(&ctx, th_stdio_vputch, fmt, ap); #else ret = fprintf(fh, fmt, ap); #endif va_end(ap); return ret; } /* Simulate a sprintf() that allocates memory */ #ifdef TH_USE_INTERNAL_SPRINTF static int th_pbuf_alloc_vputch1(th_vprintf_ctx *ctx, const char ch) { ctx->pos++; return ch; } static int th_pbuf_alloc_vputch2(th_vprintf_ctx *ctx, const char ch) { ctx->buf[ctx->pos++] = ch; return ch; } #endif /** * Combination of vsprintf() and strdup. Automatically allocates memory * for the resulting string. * @param[in] fmt format string * @param[in] args variable arguments list structure * @returns the resulting string or @c NULL pointer in case of error */ char *th_strdup_vprintf(const char *fmt, va_list args) { #ifdef TH_USE_INTERNAL_SPRINTF // Using internal printf() implementation th_vprintf_ctx ctx; va_list ap; // Get size va_copy(ap, args); ctx.pos = 0; th_vprintf_do(&ctx, th_pbuf_alloc_vputch1, fmt, ap); va_end(ap); // Allocate memory ctx.size = ctx.pos + 1; if ((ctx.buf = th_malloc(ctx.size)) == NULL) return NULL; va_copy(ap, args); ctx.pos = 0; th_vprintf_do(&ctx, th_pbuf_alloc_vputch2, fmt, ap); va_end(ap); ctx.buf[ctx.pos] = 0; return ctx.buf; #else // Using libc vsnprintf() int size = 64; char *buf, *tmp; if (fmt == NULL) return NULL; if ((buf = th_malloc(size)) == NULL) return NULL; while (1) { int n; va_list ap; va_copy(ap, args); n = vsnprintf(buf, size, fmt, ap); va_end(ap); if (n > -1 && n < size) return buf; if (n > -1) size = n + 1; else size *= 2; if ((tmp = th_realloc(buf, size)) == NULL) { th_free(buf); return NULL; } else buf = tmp; } #endif } /** * Combination of sprintf() and strdup. Automatically allocates memory * for the resulting string. * @param[in] fmt format string * @param[in] ... optional printf arguments * @returns the resulting string or @c NULL pointer in case of error */ 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; } /** * A helper function that is given a pointer to a pointer of string, * which will be automatically freed (if necessary) and replaced with * a pointer to the newly created string. * @param[in,out] buf pointer to a char pointer/string * @param[in] fmt format string * @param[in] args variable arguments list structure */ void th_pstr_vprintf(char **buf, const char *fmt, va_list ap) { char *tmp = th_strdup_vprintf(fmt, ap); th_free(*buf); *buf = tmp; } /** * A helper function that is given a pointer to a pointer of string, * which will be automatically freed (if necessary) and replaced with * a pointer to the newly created string. * @param[in,out] buf pointer to a char pointer/string * @param[in] fmt format string * @param[in] ... optional printf arguments */ void th_pstr_printf(char **buf, const char *fmt, ...) { char *tmp; va_list ap; va_start(ap, fmt); tmp = th_strdup_vprintf(fmt, ap); va_end(ap); th_free(*buf); *buf = tmp; } /* Compare two strings ignoring case [strcasecmp, strncasecmp] */ int th_strcasecmp(const char *haystack, const char *needle) { const char *s1 = haystack, *s2 = needle; assert(haystack != NULL); assert(needle != NULL); if (haystack == needle) return 0; while (*s1 && *s2) { int k = th_tolower(*s1) - th_tolower(*s2); if (k != 0) return k; s1++; s2++; } return 0; } int th_strncasecmp(const char *haystack, const char *needle, size_t n) { const char *s1 = haystack, *s2 = needle; assert(haystack != NULL); assert(needle != NULL); if (haystack == needle) return 0; while (n > 0 && *s1 && *s2) { int k = th_tolower(*s1) - th_tolower(*s2); if (k != 0) return k; s1++; s2++; n--; } return 0; } /* Check if end of the given string str matches needle * case-insensitively, return pointer to start of the match, * if found, NULL otherwise. */ char *th_strrcasecmp(char *str, const char *needle) { if (str == NULL || needle == NULL) return NULL; const size_t slen = strlen(str), nlen = strlen(needle); if (slen < nlen) return NULL; if (th_strcasecmp(str + slen - nlen, needle) == 0) return str + slen - nlen; else return NULL; } /* Remove all occurences of control characters, in-place. * Resulting string is always shorter or same length than original. */ void th_strip_ctrlchars(char *str) { char *i, *j; assert(str != NULL); i = str; j = str; while (*i) { if (!th_iscntrl(*i)) *(j++) = *i; i++; } *j = 0; } /* Copy a given string over in *pdst. */ int th_pstr_cpy(char **pdst, const char *src) { assert(pdst != NULL); if (src == NULL) return THERR_NULLPTR; th_free(*pdst); size_t slen = strlen(src); if ((*pdst = th_malloc(slen + 1)) == NULL) return THERR_MALLOC; memcpy(*pdst, src, slen + 1); return THERR_OK; } /* Concatenates a given string into string pointed by *pdst. */ int th_pstr_cat(char **pdst, const char *src) { assert(pdst != NULL); if (src == NULL) return THERR_NULLPTR; if (*pdst != NULL) { size_t dlen = strlen(*pdst), slen = strlen(src); if ((*pdst = th_realloc(*pdst, dlen + slen + 1)) == NULL) return THERR_MALLOC; memcpy((*pdst) + dlen, src, slen + 1); } else { size_t slen = strlen(src); if ((*pdst = th_malloc(slen + 1)) == NULL) return THERR_MALLOC; memcpy(*pdst, src, slen + 1); } return THERR_OK; } /* Find next non-whitespace character in string. * Updates iPos into the position of such character and * returns pointer to the string. */ const char *th_findnext(const char *str, size_t *pos) { assert(str != NULL); // Terminating NULL-character is not whitespace! while (th_isspace(str[*pos])) (*pos)++; return &str[*pos]; } /* Find next sep-character from string */ const char *th_findsep(const char *str, size_t *pos, char sep) { assert(str != NULL); while (str[*pos] && str[*pos] != sep) (*pos)++; return &str[*pos]; } /* Find next sep- or whitespace from string */ const char *th_findseporspace(const char *str, size_t *pos, char sep) { assert(str != NULL); while (!th_isspace(str[*pos]) && str[*pos] != sep) (*pos)++; return &str[*pos]; } /* Compare a string to a pattern. Case-SENSITIVE version. * The matching pattern can consist of any normal characters plus * wildcards ? and *. "?" matches any character and "*" matches * any number of characters. */ #define TH_STRGLOB_FUNC th_strmatch #define TH_STRGLOB_COLLATE(px) (px) #include "th_strglob.c" /* Compare a string to a pattern. Case-INSENSITIVE version. */ #define TH_STRGLOB_FUNC th_strcasematch #define TH_STRGLOB_COLLATE(px) th_tolower(px) #include "th_strglob.c" BOOL th_get_hex_triplet(const char *str, unsigned int *value) { const char *p = str; int len; *value = 0; for (len = 0; *p && len < 4 * 2; p++, len++) { if (*p >= '0' && *p <= '9') { (*value) <<= 4; (*value) |= (*p - '0'); } else if (*p >= 'A' && *p <= 'F') { (*value) <<= 4; (*value) |= (*p - 'A') + 10; } else if (*p >= 'a' && *p <= 'f') { (*value) <<= 4; (*value) |= (*p - 'a') + 10; } else return FALSE; } return (len >= 3 * 2 && len <= 4 * 2); } BOOL th_get_boolean(const char *str, BOOL *value) { if (!th_strcasecmp(str, "yes") || !th_strcasecmp(str, "on") || !th_strcasecmp(str, "true") || !th_strcasecmp(str, "1")) { *value = TRUE; return TRUE; } else if (!th_strcasecmp(str, "no") || !th_strcasecmp(str, "off") || !th_strcasecmp(str, "false") || !th_strcasecmp(str, "0")) { *value = FALSE; return TRUE; } else return FALSE; } BOOL th_get_int(const char *str, unsigned int *value, BOOL *neg) { int ch; BOOL hex = FALSE; // Is the value negative? if (*str == '-') { if (neg == NULL) return FALSE; *neg = TRUE; str++; } else if (neg != NULL) *neg = FALSE; // Is it hexadecimal? if (*str == '$') { hex = TRUE; str++; } else if (str[0] == '0' && str[1] == 'x') { hex = TRUE; str += 2; } // Parse the value *value = 0; if (hex) { while ((ch = *str++)) { if (ch >= '0' && ch <= '9') { *value <<= 4; *value |= ch - '0'; } else if (ch >= 'A' && ch <= 'F') { *value <<= 4; *value |= ch - 'A' + 10; } else if (ch >= 'a' && ch <= 'f') { *value <<= 4; *value |= ch - 'a' + 10; } else return FALSE; } } else { while ((ch = *str++)) { if (ch >= '0' && ch <= '9') { *value *= 10; *value += ch - '0'; } else return FALSE; } } return TRUE; }