# HG changeset patch # User Matti Hamalainen # Date 1580104046 -7200 # Node ID 284d5b789b7caeffea785f193609319ec6432bdf # Parent 81714d54689ceefd69c2eb01649b50d965a0ea36 Add th_char_t type. diff -r 81714d54689c -r 284d5b789b7c th_printf.c --- a/th_printf.c Mon Jan 27 07:44:44 2020 +0200 +++ b/th_printf.c Mon Jan 27 07:47:26 2020 +0200 @@ -151,11 +151,11 @@ int th_vprintf_put_int_format(th_vprintf_ctx *ctx, th_vprintf_putch vputch, - char *buf, int f_flags, int f_width, int f_prec, int f_len, int vret, + th_char_t *buf, int f_flags, int f_width, int f_prec, int f_len, int vret, BOOL f_neg, BOOL f_unsig, th_vprintf_altfmt_func f_alt) { int ret = 0, nspacepad, nzeropad, f_altstr_len = 0; - char f_sign, *f_altstr; + th_char_t f_sign, *f_altstr; // Special case for value of 0 if (vret == 0) @@ -254,7 +254,7 @@ va_list ap, const int f_radix, int f_flags, int f_width, int f_prec, const BOOL f_unsig, th_vprintf_altfmt_func f_alt) { - char buf[32]; + th_char_t buf[32]; int f_len = 0, vret; BOOL f_neg = FALSE; @@ -277,7 +277,7 @@ int th_vprintf_put_str(th_vprintf_ctx *ctx, th_vprintf_putch vputch, - const char *str, int f_flags, const int f_width, const int f_prec) + const th_char_t *str, int f_flags, const int f_width, const int f_prec) { int nspacepad, f_len, ret; @@ -338,7 +338,7 @@ int th_vprintf_do_format( th_vprintf_ctx *ctx, th_vprintf_putch vputch, int f_width, int f_prec, int f_flags, - const char fmt, va_list ap) + const th_char_t fmt, va_list ap) { int ret; @@ -394,7 +394,7 @@ #endif case 's': - return th_vprintf_put_str(ctx, vputch, va_arg(ap, char *), + return th_vprintf_put_str(ctx, vputch, va_arg(ap, th_char_t *), f_flags, f_width, f_prec); //case '%': @@ -404,7 +404,7 @@ } -int th_vprintf_do(th_vprintf_ctx *ctx, th_vprintf_putch vputch, const char *fmt, va_list ap) +int th_vprintf_do(th_vprintf_ctx *ctx, th_vprintf_putch vputch, const th_char_t *fmt, va_list ap) { int ret = 0; diff -r 81714d54689c -r 284d5b789b7c th_string.c --- 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; diff -r 81714d54689c -r 284d5b789b7c th_string.h --- a/th_string.h Mon Jan 27 07:44:44 2020 +0200 +++ b/th_string.h Mon Jan 27 07:47:26 2020 +0200 @@ -35,8 +35,6 @@ #define th_isxdigit(c) isxdigit((int)(unsigned char) (c)) #define th_iscrlf(c) ((c) == '\r' || (c) == '\n') -#define th_isspecial(q) ((q) >= 0x5b && (q) <= 0x60 || (q) >= 0x7b && (q) <= 0x7d) - #define th_tolower(c) tolower((int)(unsigned char) (c)) #define th_toupper(c) toupper((int)(unsigned char) (c)) @@ -79,7 +77,7 @@ */ typedef struct { - char *buf; ///< Resulting string buffer pointer (might not be used if printing to file or such) + th_char_t *buf; ///< Resulting string buffer pointer (might not be used if printing to file or such) size_t size; ///< Size of result string buffer size_t pos; ///< Current position in the buffer int ipos; ///< Signed position @@ -93,18 +91,18 @@ * @param[in] ch character to be outputted * @returns the character @p ch cast to int or @c EOF in case of error */ -typedef int (*th_vprintf_putch)(th_vprintf_ctx *ctx, const char ch); +typedef int (*th_vprintf_putch)(th_vprintf_ctx *ctx, const th_char_t ch); /** @brief * Internal *printf() implementation helper function typedef * for alternative formatting, such as octal and hexadecimal */ -typedef char * (*th_vprintf_altfmt_func)( - const char *buf, const size_t len, const int vret, int *prec, int *flags, int *outlen); +typedef th_char_t * (*th_vprintf_altfmt_func)( + const th_char_t *buf, const size_t len, const int vret, int *prec, int *flags, int *outlen); -#define TH_VPRINTF_ALTFMT_FUNC(fname) char * fname ( \ - const char *buf, const size_t len, const int vret, int *prec, int *flags, int *outlen) +#define TH_VPRINTF_ALTFMT_FUNC(fname) th_char_t * fname ( \ + const th_char_t *buf, const size_t len, const int vret, int *prec, int *flags, int *outlen) TH_VPRINTF_ALTFMT_FUNC(th_vprintf_altfmt_oct); @@ -113,52 +111,53 @@ /* Normal NUL-terminated string functions */ -char *th_strdup(const char *src); -char *th_strndup(const char *src, const size_t n); -char *th_strdup_trim(const char *src, const int flags); -char *th_strndup_trim(const char *src, const size_t n, const int flags); +th_char_t *th_strdup(const th_char_t *src); +th_char_t *th_strndup(const th_char_t *src, const size_t n); +th_char_t *th_strdup_trim(const th_char_t *src, const int flags); +th_char_t *th_strndup_trim(const th_char_t *src, const size_t n, const int flags); -char *th_strndup_no0(const char *src, const size_t len); -char *th_strndup_no0_trim(const char *src, const size_t len, const int flags); +th_char_t *th_strndup_no0(const th_char_t *src, const size_t len); +th_char_t *th_strndup_no0_trim(const th_char_t *src, const size_t len, const int flags); -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); +int th_strcasecmp(const th_char_t *haystack, const th_char_t *needle); +int th_strncasecmp(const th_char_t *haystack, const th_char_t *needle, size_t n); +th_char_t *th_strrcasecmp(th_char_t *haystack, const th_char_t *needle); -int th_vsnprintf(char *buf, size_t size, const char *fmt, va_list ap); -int th_snprintf(char *buf, size_t size, const char *fmt, ...) - TH_ATTR_PRINTF_FMT(3, 4); -int th_vfprintf(FILE *fh, const char *fmt, va_list ap); -int th_fprintf(FILE *fh, const char *fmt, ...) - TH_ATTR_PRINTF_FMT(2, 3); +int th_vsnprintf(th_char_t *buf, size_t size, const th_char_t *fmt, va_list ap); +int th_snprintf(th_char_t *buf, size_t size, const th_char_t *fmt, ...) + TH_ATTR_PRINTF_FMT(3, 4); +int th_vfprintf(FILE *fh, const th_char_t *fmt, va_list ap); +int th_fprintf(FILE *fh, const th_char_t *fmt, ...) + TH_ATTR_PRINTF_FMT(2, 3); -char *th_strdup_vprintf(const char *fmt, va_list ap); -char *th_strdup_printf(const char *fmt, ...) - TH_ATTR_PRINTF_FMT(1, 2); +th_char_t *th_strdup_vprintf(const th_char_t *fmt, va_list ap); +th_char_t *th_strdup_printf(const th_char_t *fmt, ...) + TH_ATTR_PRINTF_FMT(1, 2); -void th_pstr_vprintf(char **buf, const char *fmt, va_list ap); -void th_pstr_printf(char **buf, const char *fmt, ...) - TH_ATTR_PRINTF_FMT(2, 3); +void th_pstr_vprintf(th_char_t **buf, const th_char_t *fmt, va_list ap); +void th_pstr_printf(th_char_t **buf, const th_char_t *fmt, ...) + TH_ATTR_PRINTF_FMT(2, 3); -int th_pstr_cpy(char **pdst, const char *src); -int th_pstr_cat(char **pdst, const char *src); +int th_pstr_cpy(th_char_t **pdst, const th_char_t *src); +int th_pstr_cat(th_char_t **pdst, const th_char_t *src); /* Internal printf() implementation. NOTICE! This API may be unstable. */ -int th_vprintf_do(th_vprintf_ctx *ctx, th_vprintf_putch vputch, const char *fmt, va_list ap); +int th_vprintf_do(th_vprintf_ctx *ctx, th_vprintf_putch vputch, + const th_char_t *fmt, va_list ap); -int th_vprintf_put_str(th_vprintf_ctx *ctx, th_vprintf_putch vputch, - const char *str, int f_flags, const int f_width, const int f_prec); +int th_vprintf_put_str(th_vprintf_ctx *ctx, th_vprintf_putch vputch, + const th_char_t *str, int f_flags, const int f_width, const int f_prec); -int th_vprintf_put_int(th_vprintf_ctx *ctx, th_vprintf_putch vputch, - va_list ap, const int f_radix, int f_flags, int f_width, int f_prec, - const BOOL f_unsig, th_vprintf_altfmt_func f_alt); +int th_vprintf_put_int(th_vprintf_ctx *ctx, th_vprintf_putch vputch, + va_list ap, const int f_radix, int f_flags, int f_width, int f_prec, + const BOOL f_unsig, th_vprintf_altfmt_func f_alt); -int th_vprintf_put_int_format(th_vprintf_ctx *ctx, th_vprintf_putch vputch, - char *buf, int f_flags, int f_width, int f_prec, int f_len, int vret, - BOOL f_neg, BOOL f_unsig, th_vprintf_altfmt_func f_alt); +int th_vprintf_put_int_format(th_vprintf_ctx *ctx, th_vprintf_putch vputch, + th_char_t *buf, int f_flags, int f_width, int f_prec, int f_len, int vret, + BOOL f_neg, BOOL f_unsig, th_vprintf_altfmt_func f_alt); #define TH_VPRINTF_INTFMT_NAME th_vprintf_buf_int @@ -183,16 +182,16 @@ /* Parsing, matching */ -const char *th_findnext(const char *, size_t *); -const char *th_findsep(const char *, size_t *, char); -const char *th_findseporspace(const char *, size_t *, char); +const th_char_t *th_findnext(const th_char_t *str, size_t *pos); +const th_char_t *th_findsep(const th_char_t *str, size_t *pos, const th_char_t sep); +const th_char_t *th_findseporspace(const th_char_t *str, size_t *pos, const th_char_t sep); -BOOL th_strmatch(const char *haystack, const char *pattern); -BOOL th_strcasematch(const char *haystack, const char *pattern); +BOOL th_strmatch(const th_char_t *haystack, const th_char_t *pattern); +BOOL th_strcasematch(const th_char_t *haystack, const th_char_t *pattern); -BOOL th_get_hex_triplet(const char *str, unsigned int *value); -BOOL th_get_boolean(const char *str, BOOL *value); -BOOL th_get_int(const char *str, unsigned int *value, BOOL *neg); +BOOL th_get_hex_triplet(const th_char_t *str, unsigned int *value); +BOOL th_get_boolean(const th_char_t *str, BOOL *value); +BOOL th_get_int(const th_char_t *str, unsigned int *value, BOOL *neg); #ifdef __cplusplus diff -r 81714d54689c -r 284d5b789b7c th_types.h --- a/th_types.h Mon Jan 27 07:44:44 2020 +0200 +++ b/th_types.h Mon Jan 27 07:47:26 2020 +0200 @@ -166,4 +166,17 @@ # endif #endif + +/** @brief th_char_t + * Character type. Currently it is not recommended to re-define this, + * but in distant future it may be possible to change to uint32_t for + * Unicode 32bit handling. + */ +#ifdef TH_CHAR_TYPE +typedef TH_CHAR_TYPE th_char_t; +#else +typedef char th_char_t; +#endif + + #endif // TH_TYPES_H