Mercurial > hg > th-libs
diff th_string.c @ 663:284d5b789b7c
Add th_char_t type.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Mon, 27 Jan 2020 07:47:26 +0200 |
parents | 6510d76966f8 |
children | 4932188c9101 |
line wrap: on
line diff
--- a/th_string.c Mon Jan 27 07:44:44 2020 +0200 +++ b/th_string.c Mon Jan 27 07:47:26 2020 +0200 @@ -17,9 +17,9 @@ * @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) +th_char_t *th_strdup(const th_char_t *src) { - char *res; + th_char_t *res; size_t len; if (src == NULL) @@ -44,9 +44,9 @@ * @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) +th_char_t *th_strndup(const th_char_t *src, const size_t n) { - char *res; + th_char_t *res; size_t len; if (src == NULL) @@ -56,10 +56,10 @@ if (len > n) len = n; - if ((res = th_malloc((len + 1) * sizeof(char))) == NULL) + if ((res = th_malloc((len + 1) * sizeof(th_char_t))) == NULL) return NULL; - memcpy(res, src, len * sizeof(char)); + memcpy(res, src, len * sizeof(th_char_t)); res[len] = 0; return res; @@ -76,9 +76,9 @@ * @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) +static th_char_t * th_strdup_trim_do(const th_char_t *src, size_t len, const int flags) { - char *res; + th_char_t *res; size_t start, end; if (len == 0) @@ -101,10 +101,10 @@ return NULL; len = end - start + 1; - if ((res = th_malloc((len + 1) * sizeof(char))) == NULL) + if ((res = th_malloc((len + 1) * sizeof(th_char_t))) == NULL) return NULL; - memcpy(res, src + start, len * sizeof(char)); + memcpy(res, src + start, len * sizeof(th_char_t)); res[len] = 0; return res; } @@ -118,7 +118,7 @@ * @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) +th_char_t *th_strdup_trim(const th_char_t *src, const int flags) { if (src == NULL) return NULL; @@ -137,7 +137,7 @@ * @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) +th_char_t *th_strndup_trim(const th_char_t *src, const size_t n, const int flags) { size_t len; @@ -150,28 +150,28 @@ } -char *th_strndup_no0(const char *src, const size_t len) +th_char_t *th_strndup_no0(const th_char_t *src, const size_t len) { - char *res; + th_char_t *res; - if ((res = th_malloc((len + 1) * sizeof(char))) == NULL) + if ((res = th_malloc((len + 1) * sizeof(th_char_t))) == NULL) return NULL; - memcpy(res, src, len * sizeof(char)); + memcpy(res, src, len * sizeof(th_char_t)); res[len] = 0; return res; } -char *th_strndup_no0_trim(const char *src, const size_t len, const int flags) +th_char_t *th_strndup_no0_trim(const th_char_t *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) +static int th_pbuf_vputch(th_vprintf_ctx *ctx, const th_char_t ch) { if (ctx->pos < ctx->size) ctx->buf[ctx->pos] = ch; @@ -191,7 +191,7 @@ * @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) +int th_vsnprintf(th_char_t *buf, size_t size, const th_char_t *fmt, va_list ap) { #ifdef TH_USE_INTERNAL_SPRINTF int ret; @@ -224,7 +224,7 @@ * @param[in] fmt format string * @param[in] ... variable arguments */ -int th_snprintf(char *buf, size_t size, const char *fmt, ...) +int th_snprintf(th_char_t *buf, size_t size, const th_char_t *fmt, ...) { int n; va_list ap; @@ -240,7 +240,7 @@ #ifdef TH_USE_INTERNAL_SPRINTF -static int th_stdio_vputch(th_vprintf_ctx *ctx, const char ch) +static int th_stdio_vputch(th_vprintf_ctx *ctx, const th_char_t ch) { ctx->pos++; ctx->ipos++; @@ -249,7 +249,7 @@ #endif -int th_vfprintf(FILE *fh, const char *fmt, va_list ap) +int th_vfprintf(FILE *fh, const th_char_t *fmt, va_list ap) { #ifdef TH_USE_INTERNAL_SPRINTF th_vprintf_ctx ctx; @@ -264,7 +264,7 @@ } -int th_fprintf(FILE *fh, const char *fmt, ...) +int th_fprintf(FILE *fh, const th_char_t *fmt, ...) { int ret; va_list ap; @@ -291,13 +291,13 @@ /* Simulate a sprintf() that allocates memory */ #ifdef TH_USE_INTERNAL_SPRINTF -static int th_pbuf_alloc_vputch1(th_vprintf_ctx *ctx, const char ch) +static int th_pbuf_alloc_vputch1(th_vprintf_ctx *ctx, const th_char_t ch) { ctx->pos++; return ch; } -static int th_pbuf_alloc_vputch2(th_vprintf_ctx *ctx, const char ch) +static int th_pbuf_alloc_vputch2(th_vprintf_ctx *ctx, const th_char_t ch) { ctx->buf[ctx->pos++] = ch; return ch; @@ -312,7 +312,7 @@ * @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) +th_char_t *th_strdup_vprintf(const th_char_t *fmt, va_list args) { #ifdef TH_USE_INTERNAL_SPRINTF // Using internal printf() implementation @@ -327,7 +327,7 @@ // Allocate memory for it ctx.size = ctx.pos + 1; - if ((ctx.buf = th_malloc(ctx.size * sizeof(char))) == NULL) + if ((ctx.buf = th_malloc(ctx.size * sizeof(th_char_t))) == NULL) return NULL; // Print the final result into the buffer @@ -342,12 +342,12 @@ #else // Using libc vsnprintf() int size = 64; - char *buf, *tmp; + th_char_t *buf, *tmp; if (fmt == NULL) return NULL; - if ((buf = th_malloc(size * sizeof(char))) == NULL) + if ((buf = th_malloc(size * sizeof(th_char_t))) == NULL) return NULL; while (1) @@ -365,7 +365,7 @@ else size *= 2; - if ((tmp = th_realloc(buf, size * sizeof(char))) == NULL) + if ((tmp = th_realloc(buf, size * sizeof(th_char_t))) == NULL) { th_free(buf); return NULL; @@ -384,9 +384,9 @@ * @param[in] ... optional printf arguments * @returns the resulting string or @c NULL pointer in case of error */ -char *th_strdup_printf(const char *fmt, ...) +th_char_t *th_strdup_printf(const th_char_t *fmt, ...) { - char *res; + th_char_t *res; va_list ap; va_start(ap, fmt); @@ -405,9 +405,9 @@ * @param[in] fmt format string * @param[in] args variable arguments list structure */ -void th_pstr_vprintf(char **buf, const char *fmt, va_list ap) +void th_pstr_vprintf(th_char_t **buf, const th_char_t *fmt, va_list ap) { - char *tmp = th_strdup_vprintf(fmt, ap); + th_char_t *tmp = th_strdup_vprintf(fmt, ap); th_free(*buf); *buf = tmp; } @@ -421,9 +421,9 @@ * @param[in] fmt format string * @param[in] ... optional printf arguments */ -void th_pstr_printf(char **buf, const char *fmt, ...) +void th_pstr_printf(th_char_t **buf, const th_char_t *fmt, ...) { - char *tmp; + th_char_t *tmp; va_list ap; va_start(ap, fmt); @@ -437,9 +437,9 @@ /* Compare two strings ignoring case [strcasecmp, strncasecmp] */ -int th_strcasecmp(const char *haystack, const char *needle) +int th_strcasecmp(const th_char_t *haystack, const th_char_t *needle) { - const char *s1 = haystack, *s2 = needle; + const th_char_t *s1 = haystack, *s2 = needle; assert(haystack != NULL); assert(needle != NULL); @@ -459,9 +459,9 @@ } -int th_strncasecmp(const char *haystack, const char *needle, size_t n) +int th_strncasecmp(const th_char_t *haystack, const th_char_t *needle, size_t n) { - const char *s1 = haystack, *s2 = needle; + const th_char_t *s1 = haystack, *s2 = needle; assert(haystack != NULL); assert(needle != NULL); @@ -486,7 +486,7 @@ * case-insensitively, return pointer to start of the match, * if found, NULL otherwise. */ -char *th_strrcasecmp(char *str, const char *needle) +th_char_t *th_strrcasecmp(th_char_t *str, const th_char_t *needle) { if (str == NULL || needle == NULL) return NULL; @@ -507,7 +507,7 @@ /* Copy a given string over in *pdst. */ -int th_pstr_cpy(char **pdst, const char *src) +int th_pstr_cpy(th_char_t **pdst, const th_char_t *src) { assert(pdst != NULL); @@ -517,10 +517,10 @@ th_free(*pdst); size_t slen = strlen(src); - if ((*pdst = th_malloc((slen + 1) * sizeof(char))) == NULL) + if ((*pdst = th_malloc((slen + 1) * sizeof(th_char_t))) == NULL) return THERR_MALLOC; - memcpy(*pdst, src, (slen + 1) * sizeof(char)); + memcpy(*pdst, src, (slen + 1) * sizeof(th_char_t)); return THERR_OK; } @@ -528,7 +528,7 @@ /* Concatenates a given string into string pointed by *pdst. */ -int th_pstr_cat(char **pdst, const char *src) +int th_pstr_cat(th_char_t **pdst, const th_char_t *src) { assert(pdst != NULL); @@ -538,18 +538,18 @@ if (*pdst != NULL) { size_t dlen = strlen(*pdst), slen = strlen(src); - if ((*pdst = th_realloc(*pdst, (dlen + slen + 1) * sizeof(char))) == NULL) + if ((*pdst = th_realloc(*pdst, (dlen + slen + 1) * sizeof(th_char_t))) == NULL) return THERR_MALLOC; - memcpy((*pdst) + dlen, src, (slen + 1) * sizeof(char)); + memcpy((*pdst) + dlen, src, (slen + 1) * sizeof(th_char_t)); } else { size_t slen = strlen(src); - if ((*pdst = th_malloc((slen + 1) * sizeof(char))) == NULL) + if ((*pdst = th_malloc((slen + 1) * sizeof(th_char_t))) == NULL) return THERR_MALLOC; - memcpy(*pdst, src, (slen + 1) * sizeof(char)); + memcpy(*pdst, src, (slen + 1) * sizeof(th_char_t)); } return THERR_OK; @@ -560,7 +560,7 @@ * Updates iPos into the position of such character and * returns pointer to the string. */ -const char *th_findnext(const char *str, size_t *pos) +const th_char_t *th_findnext(const th_char_t *str, size_t *pos) { assert(str != NULL); @@ -568,33 +568,33 @@ while (th_isspace(str[*pos])) (*pos)++; - return &str[*pos]; + return str + *pos; } /* Find next sep-character from string */ -const char *th_findsep(const char *str, size_t *pos, char sep) +const th_char_t *th_findsep(const th_char_t *str, size_t *pos, const th_char_t sep) { assert(str != NULL); while (str[*pos] && str[*pos] != sep) (*pos)++; - return &str[*pos]; + return str + *pos; } /* Find next sep- or whitespace from string */ -const char *th_findseporspace(const char *str, size_t *pos, char sep) +const th_char_t *th_findseporspace(const th_char_t *str, size_t *pos, const th_char_t sep) { assert(str != NULL); while (!th_isspace(str[*pos]) && str[*pos] != sep) (*pos)++; - return &str[*pos]; + return str + *pos; } @@ -615,9 +615,9 @@ #include "th_strglob.c" -BOOL th_get_hex_triplet(const char *str, unsigned int *value) +BOOL th_get_hex_triplet(const th_char_t *str, unsigned int *value) { - const char *p = str; + const th_char_t *p = str; int len; *value = 0; @@ -648,7 +648,7 @@ } -BOOL th_get_boolean(const char *str, BOOL *value) +BOOL th_get_boolean(const th_char_t *str, BOOL *value) { if (!th_strcasecmp(str, "yes") || !th_strcasecmp(str, "on") || @@ -672,7 +672,7 @@ } -BOOL th_get_int(const char *str, unsigned int *value, BOOL *neg) +BOOL th_get_int(const th_char_t *str, unsigned int *value, BOOL *neg) { int ch; BOOL hex = FALSE;