X-Git-Url: https://wimlib.net/git/?a=blobdiff_plain;f=src%2Futil.h;h=77c4b352a1d549320245b4a0be112ec859b7f737;hb=35905b989d7955ac704cf7a033a51c920df2caab;hp=71590ffe2dde2d1674e7709c4b24f1765adeff54;hpb=14baa6ae892debbaa18dba8119931580efd0e517;p=wimlib diff --git a/src/util.h b/src/util.h index 71590ffe..77c4b352 100644 --- a/src/util.h +++ b/src/util.h @@ -52,9 +52,10 @@ typedef uint64_t u64; /* A pointer to 'mbchar' indicates a string of "multibyte characters" provided * in the default encoding of the user's locale, which may be "UTF-8", * "ISO-8859-1", "C", or any other ASCII-compatible encoding. - * "ASCII-compatible" here means any encoding where all ASCII characters have - * the same representation, and any non-ASCII character is represented as a - * sequence of one or more bytes not already used by any ASCII character. */ + * "ASCII-compatible" here means any encoding where all ASCII-representable + * characters have the same representation as in ASCII itself, and any non-ASCII + * character is represented as a sequence of one or more bytes not already used + * by any ASCII character. */ typedef char mbchar; /* A pointer to 'utf8char' indicates a UTF-8 encoded string */ @@ -67,6 +68,43 @@ typedef char utf8char; /* A pointer to 'utf16lechar' indicates a UTF-16LE encoded string */ typedef u16 utf16lechar; +extern size_t +utf16le_strlen(const utf16lechar *s); + +/* encoding.c */ +extern void +iconv_global_cleanup(); + +extern bool wimlib_mbs_is_utf8; + +#define DECLARE_CHAR_CONVERSION_FUNCTIONS(varname1, varname2, \ + chartype1, chartype2) \ + \ +extern int \ +varname1##_to_##varname2##_nbytes(const chartype1 *in, size_t in_nbytes,\ + size_t *out_nbytes_ret); \ + \ +extern int \ +varname1##_to_##varname2##_buf(const chartype1 *in, size_t in_nbytes, \ + chartype2 *out); \ + \ +extern int \ +varname1##_to_##varname2(const chartype1 *in, size_t in_nbytes, \ + chartype2 **out_ret, \ + size_t *out_nbytes_ret); \ + +/* multi-byte string to UTF16-LE string */ +DECLARE_CHAR_CONVERSION_FUNCTIONS(mbs, utf16le, mbchar, utf16lechar); + +/* UTF16-LE string to multi-byte string */ +DECLARE_CHAR_CONVERSION_FUNCTIONS(utf16le, mbs, utf16lechar, mbchar); + +/* UTF-8 string to multi-byte string */ +DECLARE_CHAR_CONVERSION_FUNCTIONS(utf8, mbs, utf8char, mbchar); + +extern bool +utf8_str_contains_nonascii_chars(const utf8char *utf8_str); + #ifndef min #define min(a, b) ({ typeof(a) __a = (a); typeof(b) __b = (b); \ (__a < __b) ? __a : __b; }) @@ -124,51 +162,54 @@ wimlib_warning(const char *format, ...) FORMAT(printf, 1, 2) COLD; extern void wimlib_warning_with_errno(const char *format, ...) FORMAT(printf, 1, 2) COLD; -# define ERROR wimlib_error -# define ERROR_WITH_ERRNO wimlib_error_with_errno -# define WARNING wimlib_warning -# define WARNING_WITH_ERRNO wimlib_warning -#else -# define ERROR(format, ...) dummy_printf(format, ## __VA_ARGS__) -# define ERROR_WITH_ERRNO(format, ...) dummy_printf(format, ## __VA_ARGS__) -# define WARNING(format, ...) dummy_printf(format, ## __VA_ARGS__) -# define WARNING_WITH_ERRNO(format, ...) dummy_printf(format, ## __VA_ARGS__) -#endif /* ENABLE_ERROR_MESSAGES */ - -#if defined(ENABLE_DEBUG) || defined(ENABLE_MORE_DEBUG) -# include -# define DEBUG(format, ...) \ - ({ \ - int __errno_save = errno; \ - wimlib_fprintf(stdout, "[%s %d] %s(): " format, \ - __FILE__, __LINE__, __func__, ## __VA_ARGS__); \ - putchar('\n'); \ - fflush(stdout); \ - errno = __errno_save; \ - }) +# define ERROR wimlib_error +# define ERROR_WITH_ERRNO wimlib_error_with_errno +# define WARNING wimlib_warning +# define WARNING_WITH_ERRNO wimlib_warning +#else /* ENABLE_ERROR_MESSAGES */ +# define ERROR(format, ...) dummy_printf(format, ## __VA_ARGS__) +# define ERROR_WITH_ERRNO(format, ...) dummy_printf(format, ## __VA_ARGS__) +# define WARNING(format, ...) dummy_printf(format, ## __VA_ARGS__) +# define WARNING_WITH_ERRNO(format, ...) dummy_printf(format, ## __VA_ARGS__) +#endif /* !ENABLE_ERROR_MESSAGES */ + +#if defined(ENABLE_MORE_DEBUG) && !defined(ENABLE_DEBUG) +# define ENABLE_DEBUG 1 +#endif + +#if defined(ENABLE_MORE_ASSERTIONS) && !defined(ENABLE_ASSERTIONS) +# define ENABLE_ASSERTIONS 1 +#endif + +#ifdef ENABLE_DEBUG +extern void +wimlib_debug(const char *file, int line, const char *func, + const char *format, ...); +# define DEBUG(format, ...) \ + wimlib_debug(__FILE__, __LINE__, __func__, format, ## __VA_ARGS__); #else -# define DEBUG(format, ...) dummy_printf(format, ## __VA_ARGS__) -#endif /* ENABLE_DEBUG || ENABLE_MORE_DEBUG */ +# define DEBUG(format, ...) dummy_printf(format, ## __VA_ARGS__) +#endif /* !ENABLE_DEBUG */ #ifdef ENABLE_MORE_DEBUG -# define DEBUG2(format, ...) DEBUG(format, ## __VA_ARGS__) +# define DEBUG2(format, ...) DEBUG(format, ## __VA_ARGS__) #else -# define DEBUG2(format, ...) dummy_printf(format, ## __VA_ARGS__) -#endif /* ENABLE_DEBUG */ +# define DEBUG2(format, ...) dummy_printf(format, ## __VA_ARGS__) +#endif /* !ENABLE_MORE_DEBUG */ #ifdef ENABLE_ASSERTIONS #include -# define wimlib_assert(expr) assert(expr) +# define wimlib_assert(expr) assert(expr) #else -# define wimlib_assert(expr) -#endif +# define wimlib_assert(expr) +#endif /* !ENABLE_ASSERTIONS */ #ifdef ENABLE_MORE_ASSERTIONS -#define wimlib_assert2(expr) wimlib_assert(expr) +# define wimlib_assert2(expr) wimlib_assert(expr) #else -#define wimlib_assert2(expr) -#endif +# define wimlib_assert2(expr) +#endif /* !ENABLE_MORE_ASSERTIONS */ #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)])) @@ -178,20 +219,20 @@ extern void (*wimlib_free_func)(void *); extern void *(*wimlib_realloc_func)(void *, size_t); extern void *wimlib_calloc(size_t nmemb, size_t size); extern char *wimlib_strdup(const char *str); -# define MALLOC wimlib_malloc_func -# define FREE wimlib_free_func -# define REALLOC wimlib_realloc_func -# define CALLOC wimlib_calloc -# define STRDUP wimlib_strdup -#else -# include -# include -# define MALLOC malloc -# define FREE free -# define REALLOC realloc -# define CALLOC calloc -# define STRDUP strdup -#endif /* ENABLE_CUSTOM_MEMORY_ALLOCATOR */ +# define MALLOC wimlib_malloc_func +# define FREE wimlib_free_func +# define REALLOC wimlib_realloc_func +# define CALLOC wimlib_calloc +# define STRDUP wimlib_strdup +#else /* ENABLE_CUSTOM_MEMORY_ALLOCATOR */ +# include +# include +# define MALLOC malloc +# define FREE free +# define REALLOC realloc +# define CALLOC calloc +# define STRDUP strdup +#endif /* !ENABLE_CUSTOM_MEMORY_ALLOCATOR */ /* util.c */ @@ -243,9 +284,13 @@ bsr32(u32 n) } extern int -wimlib_fprintf(FILE *fp, const char *format, ...) FORMAT(printf, 2, 3); +wimlib_fprintf(FILE *fp, const char *format, ...) + //FORMAT(printf, 2, 3) + ; extern int -wimlib_printf(const char *format, ...) FORMAT(printf, 1, 2); +wimlib_printf(const char *format, ...) + //FORMAT(printf, 1, 2) + ; #endif /* _WIMLIB_UTIL_H */