From e12c08e8d170b7ca40f204a5b0423c23a9fbc2c1 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Wed, 17 Apr 2019 06:17:24 +0200 Subject: ClangFormat: apply to source, most of intern Apply clang format as proposed in T53211. For details on usage and instructions for migrating branches without conflicts, see: https://wiki.blender.org/wiki/Tools/ClangFormat --- intern/utfconv/CMakeLists.txt | 14 +- intern/utfconv/utf_winfunc.c | 233 +++++++++++----------- intern/utfconv/utf_winfunc.h | 6 +- intern/utfconv/utfconv.c | 443 +++++++++++++++++++++++------------------- intern/utfconv/utfconv.h | 23 ++- 5 files changed, 384 insertions(+), 335 deletions(-) (limited to 'intern/utfconv') diff --git a/intern/utfconv/CMakeLists.txt b/intern/utfconv/CMakeLists.txt index 9450461f50b..1ed3849d53e 100644 --- a/intern/utfconv/CMakeLists.txt +++ b/intern/utfconv/CMakeLists.txt @@ -17,7 +17,7 @@ # ***** END GPL LICENSE BLOCK ***** set(INC - . + . ) set(INC_SYS @@ -25,9 +25,9 @@ set(INC_SYS ) set(SRC - utfconv.c + utfconv.c - utfconv.h + utfconv.h ) set(LIB @@ -37,10 +37,10 @@ set(LIB # Why have win32 check here? - this is only used for windows. # ... because one day we might want to use it on other platforms. if(WIN32) - list(APPEND SRC - utf_winfunc.c - utf_winfunc.h - ) + list(APPEND SRC + utf_winfunc.c + utf_winfunc.h + ) endif() blender_add_lib(bf_intern_utfconv "${SRC}" "${INC}" "${INC_SYS}" "${LIB}") diff --git a/intern/utfconv/utf_winfunc.c b/intern/utfconv/utf_winfunc.c index 6a46807e148..4dc290a0146 100644 --- a/intern/utfconv/utf_winfunc.c +++ b/intern/utfconv/utf_winfunc.c @@ -18,7 +18,7 @@ */ #ifndef _WIN32_IE -#define _WIN32_IE 0x0501 +# define _WIN32_IE 0x0501 #endif #include "utf_winfunc.h" @@ -27,157 +27,156 @@ #include #include - FILE *ufopen(const char *filename, const char *mode) { - FILE *f = NULL; - UTF16_ENCODE(filename); - UTF16_ENCODE(mode); - - if (filename_16 && mode_16) { - f = _wfopen(filename_16, mode_16); - } - - UTF16_UN_ENCODE(mode); - UTF16_UN_ENCODE(filename); - - if (!f) { - if ((f = fopen(filename, mode))) { - printf("WARNING: %s is not utf path. Please update it.\n",filename); - } - } - - return f; + FILE *f = NULL; + UTF16_ENCODE(filename); + UTF16_ENCODE(mode); + + if (filename_16 && mode_16) { + f = _wfopen(filename_16, mode_16); + } + + UTF16_UN_ENCODE(mode); + UTF16_UN_ENCODE(filename); + + if (!f) { + if ((f = fopen(filename, mode))) { + printf("WARNING: %s is not utf path. Please update it.\n", filename); + } + } + + return f; } int uopen(const char *filename, int oflag, int pmode) { - int f = -1; - UTF16_ENCODE(filename); - - if (filename_16) { - f = _wopen(filename_16, oflag, pmode); - } - - UTF16_UN_ENCODE(filename); - - if (f == -1) { - if ((f = open(filename,oflag, pmode)) != -1) { - printf("WARNING: %s is not utf path. Please update it.\n",filename); - } - } - - return f; + int f = -1; + UTF16_ENCODE(filename); + + if (filename_16) { + f = _wopen(filename_16, oflag, pmode); + } + + UTF16_UN_ENCODE(filename); + + if (f == -1) { + if ((f = open(filename, oflag, pmode)) != -1) { + printf("WARNING: %s is not utf path. Please update it.\n", filename); + } + } + + return f; } int uaccess(const char *filename, int mode) { - int r = -1; - UTF16_ENCODE(filename); + int r = -1; + UTF16_ENCODE(filename); - if (filename_16) { - r = _waccess(filename_16, mode); - } + if (filename_16) { + r = _waccess(filename_16, mode); + } - UTF16_UN_ENCODE(filename); + UTF16_UN_ENCODE(filename); - return r; + return r; } -int urename(const char *oldname, const char *newname ) +int urename(const char *oldname, const char *newname) { - int r = -1; - UTF16_ENCODE(oldname); - UTF16_ENCODE(newname); - - if (oldname_16 && newname_16) { - r = _wrename(oldname_16, newname_16); - } - - UTF16_UN_ENCODE(newname); - UTF16_UN_ENCODE(oldname); - return r; + int r = -1; + UTF16_ENCODE(oldname); + UTF16_ENCODE(newname); + + if (oldname_16 && newname_16) { + r = _wrename(oldname_16, newname_16); + } + + UTF16_UN_ENCODE(newname); + UTF16_UN_ENCODE(oldname); + return r; } int umkdir(const char *pathname) { - BOOL r = 0; - UTF16_ENCODE(pathname); - - if (pathname_16) { - r = CreateDirectoryW(pathname_16, NULL); - } + BOOL r = 0; + UTF16_ENCODE(pathname); + + if (pathname_16) { + r = CreateDirectoryW(pathname_16, NULL); + } - UTF16_UN_ENCODE(pathname); + UTF16_UN_ENCODE(pathname); - return r ? 0 : -1; + return r ? 0 : -1; } char *u_alloc_getenv(const char *varname) { - char * r = 0; - wchar_t * str; - UTF16_ENCODE(varname); - if (varname_16) { - str = _wgetenv(varname_16); - r = alloc_utf_8_from_16(str, 0); - } - UTF16_UN_ENCODE(varname); - - return r; + char *r = 0; + wchar_t *str; + UTF16_ENCODE(varname); + if (varname_16) { + str = _wgetenv(varname_16); + r = alloc_utf_8_from_16(str, 0); + } + UTF16_UN_ENCODE(varname); + + return r; } -void u_free_getenv(char *val) +void u_free_getenv(char *val) { - free(val); + free(val); } int uput_getenv(const char *varname, char *value, size_t buffsize) { - int r = 0; - wchar_t * str; - - if (!buffsize) { - return r; - } - - UTF16_ENCODE(varname); - if (varname_16) { - str = _wgetenv(varname_16); - conv_utf_16_to_8(str, value, buffsize); - r = 1; - } - UTF16_UN_ENCODE(varname); - - if (!r) { - value[0] = 0; - } - - return r; + int r = 0; + wchar_t *str; + + if (!buffsize) { + return r; + } + + UTF16_ENCODE(varname); + if (varname_16) { + str = _wgetenv(varname_16); + conv_utf_16_to_8(str, value, buffsize); + r = 1; + } + UTF16_UN_ENCODE(varname); + + if (!r) { + value[0] = 0; + } + + return r; } int uputenv(const char *name, const char *value) { - int r = -1; - UTF16_ENCODE(name); - - if (value) { - /* set */ - UTF16_ENCODE(value); - - if (name_16 && value_16) { - r = (SetEnvironmentVariableW(name_16,value_16)!= 0) ? 0 : -1; - } - UTF16_UN_ENCODE(value); - } - else { - /* clear */ - if (name_16) { - r = (SetEnvironmentVariableW(name_16,NULL)!= 0) ? 0 : -1; - } - } - - UTF16_UN_ENCODE(name); - - return r; + int r = -1; + UTF16_ENCODE(name); + + if (value) { + /* set */ + UTF16_ENCODE(value); + + if (name_16 && value_16) { + r = (SetEnvironmentVariableW(name_16, value_16) != 0) ? 0 : -1; + } + UTF16_UN_ENCODE(value); + } + else { + /* clear */ + if (name_16) { + r = (SetEnvironmentVariableW(name_16, NULL) != 0) ? 0 : -1; + } + } + + UTF16_UN_ENCODE(name); + + return r; } diff --git a/intern/utfconv/utf_winfunc.h b/intern/utfconv/utf_winfunc.h index 06d3b106fd9..71fc9835d74 100644 --- a/intern/utfconv/utf_winfunc.h +++ b/intern/utfconv/utf_winfunc.h @@ -27,17 +27,17 @@ #include -FILE *ufopen(const char * filename, const char * mode); +FILE *ufopen(const char *filename, const char *mode); int uopen(const char *filename, int oflag, int pmode); int uaccess(const char *filename, int mode); int urename(const char *oldname, const char *newname); char *u_alloc_getenv(const char *varname); -void u_free_getenv(char *val); +void u_free_getenv(char *val); int uput_getenv(const char *varname, char *value, size_t buffsize); int uputenv(const char *name, const char *value); int umkdir(const char *pathname); -#endif /* __UTF_WINFUNC_H__ */ +#endif /* __UTF_WINFUNC_H__ */ diff --git a/intern/utfconv/utfconv.c b/intern/utfconv/utfconv.c index 767337b84b3..1dd868b8170 100644 --- a/intern/utfconv/utfconv.c +++ b/intern/utfconv/utfconv.c @@ -22,235 +22,282 @@ size_t count_utf_8_from_16(const wchar_t *string16) { - int i; - size_t count = 0; - wchar_t u = 0; - if (!string16) { - return 0; - } - - for (i = 0; (u = string16[i]); i++) { - if (u < 0x0080) { - count += 1; - } - else { - if (u < 0x0800) { - count += 2; - } - else { - if (u < 0xD800) { - count += 3; - } - else { - if (u < 0xDC00) { - i++; - if ((u = string16[i]) == 0) { - break; - } - if (u >= 0xDC00 && u < 0xE000) { - count += 4; - } - } - else { - if (u < 0xE000) { - /*illigal*/; - } - else { - count += 3; - } - } - } - } - } - } - - return ++count; + int i; + size_t count = 0; + wchar_t u = 0; + if (!string16) { + return 0; + } + + for (i = 0; (u = string16[i]); i++) { + if (u < 0x0080) { + count += 1; + } + else { + if (u < 0x0800) { + count += 2; + } + else { + if (u < 0xD800) { + count += 3; + } + else { + if (u < 0xDC00) { + i++; + if ((u = string16[i]) == 0) { + break; + } + if (u >= 0xDC00 && u < 0xE000) { + count += 4; + } + } + else { + if (u < 0xE000) { + /*illigal*/; + } + else { + count += 3; + } + } + } + } + } + } + + return ++count; } - size_t count_utf_16_from_8(const char *string8) { - size_t count = 0; - char u; - char type = 0; - unsigned int u32 = 0; - - if (!string8) return 0; - - for (; (u = *string8); string8++) { - if (type == 0) { - if ((u & 0x01 << 7) == 0) { count++; u32 = 0; continue; } //1 utf-8 char - if ((u & 0x07 << 5) == 0xC0) { type = 1; u32 = u & 0x1F; continue; } //2 utf-8 char - if ((u & 0x0F << 4) == 0xE0) { type = 2; u32 = u & 0x0F; continue; } //3 utf-8 char - if ((u & 0x1F << 3) == 0xF0) { type = 3; u32 = u & 0x07; continue; } //4 utf-8 char - continue; - } - else { - if ((u & 0xC0) == 0x80) { - u32 = (u32 << 6) | (u & 0x3F); - type--; - } - else { - u32 = 0; - type = 0; - } - } - - if (type == 0) { - if ((0 < u32 && u32 < 0xD800) || (0xE000 <= u32 && u32 < 0x10000)) count++; - else if (0x10000 <= u32 && u32 < 0x110000) count += 2; - u32 = 0; - } - } - - return ++count; + size_t count = 0; + char u; + char type = 0; + unsigned int u32 = 0; + + if (!string8) + return 0; + + for (; (u = *string8); string8++) { + if (type == 0) { + if ((u & 0x01 << 7) == 0) { + count++; + u32 = 0; + continue; + } //1 utf-8 char + if ((u & 0x07 << 5) == 0xC0) { + type = 1; + u32 = u & 0x1F; + continue; + } //2 utf-8 char + if ((u & 0x0F << 4) == 0xE0) { + type = 2; + u32 = u & 0x0F; + continue; + } //3 utf-8 char + if ((u & 0x1F << 3) == 0xF0) { + type = 3; + u32 = u & 0x07; + continue; + } //4 utf-8 char + continue; + } + else { + if ((u & 0xC0) == 0x80) { + u32 = (u32 << 6) | (u & 0x3F); + type--; + } + else { + u32 = 0; + type = 0; + } + } + + if (type == 0) { + if ((0 < u32 && u32 < 0xD800) || (0xE000 <= u32 && u32 < 0x10000)) + count++; + else if (0x10000 <= u32 && u32 < 0x110000) + count += 2; + u32 = 0; + } + } + + return ++count; } - int conv_utf_16_to_8(const wchar_t *in16, char *out8, size_t size8) { - char *out8end = out8 + size8; - wchar_t u = 0; - int err = 0; - if (!size8 || !in16 || !out8) return UTF_ERROR_NULL_IN; - out8end--; - - for (; out8 < out8end && (u = *in16); in16++, out8++) { - if (u < 0x0080) { - *out8 = u; - } - else if (u < 0x0800) { - if (out8 + 1 >= out8end) break; - *out8++ = (0x3 << 6) | (0x1F & (u >> 6)); - *out8 = (0x1 << 7) | (0x3F & (u)); - } - else if (u < 0xD800 || u >= 0xE000) { - if (out8 + 2 >= out8end) break; - *out8++ = (0x7 << 5) | (0xF & (u >> 12)); - *out8++ = (0x1 << 7) | (0x3F & (u >> 6)); - *out8 = (0x1 << 7) | (0x3F & (u)); - } - else if (u < 0xDC00) { - wchar_t u2 = *++in16; - - if (!u2) break; - if (u2 >= 0xDC00 && u2 < 0xE000) { - if (out8 + 3 >= out8end) break; else { - unsigned int uc = 0x10000 + (u2 - 0xDC00) + ((u - 0xD800) << 10); - - *out8++ = (0xF << 4) | (0x7 & (uc >> 18)); - *out8++ = (0x1 << 7) | (0x3F & (uc >> 12)); - *out8++ = (0x1 << 7) | (0x3F & (uc >> 6)); - *out8 = (0x1 << 7) | (0x3F & (uc)); - } - } - else { - out8--; err |= UTF_ERROR_ILLCHAR; - } - } - else if (u < 0xE000) { - out8--; err |= UTF_ERROR_ILLCHAR; - } - } - - *out8 = *out8end = 0; - - if (*in16) err |= UTF_ERROR_SMALL; - - return err; + char *out8end = out8 + size8; + wchar_t u = 0; + int err = 0; + if (!size8 || !in16 || !out8) + return UTF_ERROR_NULL_IN; + out8end--; + + for (; out8 < out8end && (u = *in16); in16++, out8++) { + if (u < 0x0080) { + *out8 = u; + } + else if (u < 0x0800) { + if (out8 + 1 >= out8end) + break; + *out8++ = (0x3 << 6) | (0x1F & (u >> 6)); + *out8 = (0x1 << 7) | (0x3F & (u)); + } + else if (u < 0xD800 || u >= 0xE000) { + if (out8 + 2 >= out8end) + break; + *out8++ = (0x7 << 5) | (0xF & (u >> 12)); + *out8++ = (0x1 << 7) | (0x3F & (u >> 6)); + *out8 = (0x1 << 7) | (0x3F & (u)); + } + else if (u < 0xDC00) { + wchar_t u2 = *++in16; + + if (!u2) + break; + if (u2 >= 0xDC00 && u2 < 0xE000) { + if (out8 + 3 >= out8end) + break; + else { + unsigned int uc = 0x10000 + (u2 - 0xDC00) + ((u - 0xD800) << 10); + + *out8++ = (0xF << 4) | (0x7 & (uc >> 18)); + *out8++ = (0x1 << 7) | (0x3F & (uc >> 12)); + *out8++ = (0x1 << 7) | (0x3F & (uc >> 6)); + *out8 = (0x1 << 7) | (0x3F & (uc)); + } + } + else { + out8--; + err |= UTF_ERROR_ILLCHAR; + } + } + else if (u < 0xE000) { + out8--; + err |= UTF_ERROR_ILLCHAR; + } + } + + *out8 = *out8end = 0; + + if (*in16) + err |= UTF_ERROR_SMALL; + + return err; } - int conv_utf_8_to_16(const char *in8, wchar_t *out16, size_t size16) { - char u; - char type = 0; - unsigned int u32 = 0; - wchar_t *out16end = out16 + size16; - int err = 0; - if (!size16 || !in8 || !out16) return UTF_ERROR_NULL_IN; - out16end--; - - for (; out16 < out16end && (u = *in8); in8++) { - if (type == 0) { - if ((u & 0x01 << 7) == 0) { *out16 = u; out16++; u32 = 0; continue; } //1 utf-8 char - if ((u & 0x07 << 5) == 0xC0) { type = 1; u32 = u & 0x1F; continue; } //2 utf-8 char - if ((u & 0x0F << 4) == 0xE0) { type = 2; u32 = u & 0x0F; continue; } //3 utf-8 char - if ((u & 0x1F << 3) == 0xF0) { type = 3; u32 = u & 0x07; continue; } //4 utf-8 char - err |= UTF_ERROR_ILLCHAR; - continue; - } - else { - if ((u & 0xC0) == 0x80) { - u32 = (u32 << 6) | (u & 0x3F); - type--; - } - else { - u32 = 0; type = 0; err |= UTF_ERROR_ILLSEQ; - } - } - if (type == 0) { - if ((0 < u32 && u32 < 0xD800) || (0xE000 <= u32 && u32 < 0x10000)) { - *out16 = u32; - out16++; - } - else if (0x10000 <= u32 && u32 < 0x110000) { - if (out16 + 1 >= out16end) break; - u32 -= 0x10000; - *out16 = 0xD800 + (u32 >> 10); - out16++; - *out16 = 0xDC00 + (u32 & 0x3FF); - out16++; - } - u32 = 0; - } - - } - - *out16 = *out16end = 0; - - if (*in8) err |= UTF_ERROR_SMALL; - - return err; + char u; + char type = 0; + unsigned int u32 = 0; + wchar_t *out16end = out16 + size16; + int err = 0; + if (!size16 || !in8 || !out16) + return UTF_ERROR_NULL_IN; + out16end--; + + for (; out16 < out16end && (u = *in8); in8++) { + if (type == 0) { + if ((u & 0x01 << 7) == 0) { + *out16 = u; + out16++; + u32 = 0; + continue; + } //1 utf-8 char + if ((u & 0x07 << 5) == 0xC0) { + type = 1; + u32 = u & 0x1F; + continue; + } //2 utf-8 char + if ((u & 0x0F << 4) == 0xE0) { + type = 2; + u32 = u & 0x0F; + continue; + } //3 utf-8 char + if ((u & 0x1F << 3) == 0xF0) { + type = 3; + u32 = u & 0x07; + continue; + } //4 utf-8 char + err |= UTF_ERROR_ILLCHAR; + continue; + } + else { + if ((u & 0xC0) == 0x80) { + u32 = (u32 << 6) | (u & 0x3F); + type--; + } + else { + u32 = 0; + type = 0; + err |= UTF_ERROR_ILLSEQ; + } + } + if (type == 0) { + if ((0 < u32 && u32 < 0xD800) || (0xE000 <= u32 && u32 < 0x10000)) { + *out16 = u32; + out16++; + } + else if (0x10000 <= u32 && u32 < 0x110000) { + if (out16 + 1 >= out16end) + break; + u32 -= 0x10000; + *out16 = 0xD800 + (u32 >> 10); + out16++; + *out16 = 0xDC00 + (u32 & 0x3FF); + out16++; + } + u32 = 0; + } + } + + *out16 = *out16end = 0; + + if (*in8) + err |= UTF_ERROR_SMALL; + + return err; } /* UNUSED FUNCTIONS */ #if 0 static int is_ascii(const char *in8) { - for (; *in8; in8++) - if (0x80 & *in8) return 0; + for (; *in8; in8++) + if (0x80 & *in8) return 0; - return 1; + return 1; } static void utf_8_cut_end(char *inout8, size_t maxcutpoint) { - char *cur = inout8 + maxcutpoint; - char cc; - if (!inout8) return; + char *cur = inout8 + maxcutpoint; + char cc; + if (!inout8) return; - cc = *cur; + cc = *cur; } #endif - char *alloc_utf_8_from_16(const wchar_t *in16, size_t add) { - size_t bsize = count_utf_8_from_16(in16); - char *out8 = NULL; - if (!bsize) return NULL; - out8 = (char *)malloc(sizeof(char) * (bsize + add)); - conv_utf_16_to_8(in16, out8, bsize); - return out8; + size_t bsize = count_utf_8_from_16(in16); + char *out8 = NULL; + if (!bsize) + return NULL; + out8 = (char *)malloc(sizeof(char) * (bsize + add)); + conv_utf_16_to_8(in16, out8, bsize); + return out8; } wchar_t *alloc_utf16_from_8(const char *in8, size_t add) { - size_t bsize = count_utf_16_from_8(in8); - wchar_t *out16 = NULL; - if (!bsize) return NULL; - out16 = (wchar_t *) malloc(sizeof(wchar_t) * (bsize + add)); - conv_utf_8_to_16(in8, out16, bsize); - return out16; + size_t bsize = count_utf_16_from_8(in8); + wchar_t *out16 = NULL; + if (!bsize) + return NULL; + out16 = (wchar_t *)malloc(sizeof(wchar_t) * (bsize + add)); + conv_utf_8_to_16(in8, out16, bsize); + return out16; } diff --git a/intern/utfconv/utfconv.h b/intern/utfconv/utfconv.h index 35bbe3ac108..f13afbc0934 100644 --- a/intern/utfconv/utfconv.h +++ b/intern/utfconv/utfconv.h @@ -25,8 +25,8 @@ #include #include -#ifdef __cplusplus -extern "C" { +#ifdef __cplusplus +extern "C" { #endif /** @@ -48,8 +48,9 @@ size_t count_utf_16_from_8(const char *string8); */ #define UTF_ERROR_NULL_IN 1 << 0 /* Error occures when requered parameter is missing*/ #define UTF_ERROR_ILLCHAR 1 << 1 /* Error if character is in illigal UTF rage*/ -#define UTF_ERROR_SMALL 1 << 2 /* Passed size is to small. It gives legal string with character missing at the end*/ -#define UTF_ERROR_ILLSEQ 1 << 3 /* Error if sequence is broken and doesn't finish*/ +#define UTF_ERROR_SMALL \ + 1 << 2 /* Passed size is to small. It gives legal string with character missing at the end*/ +#define UTF_ERROR_ILLSEQ 1 << 3 /* Error if sequence is broken and doesn't finish*/ /** * Converts utf-16 string to allocated utf-8 string @@ -69,7 +70,6 @@ int conv_utf_16_to_8(const wchar_t *in16, char *out8, size_t size8); */ int conv_utf_8_to_16(const char *in8, wchar_t *out16, size_t size16); - /** * Allocates and converts the utf-8 string from utf-16 * @param in16 utf-16 string to convert @@ -87,14 +87,17 @@ char *alloc_utf_8_from_16(const wchar_t *in16, size_t add); wchar_t *alloc_utf16_from_8(const char *in8, size_t add); /* Easy allocation and conversion of new utf-16 string. New string has _16 suffix. Must be deallocated with UTF16_UN_ENCODE in right order*/ -#define UTF16_ENCODE(in8str) if (1) { \ - wchar_t *in8str ## _16 = alloc_utf16_from_8((const char *)in8str, 0) +#define UTF16_ENCODE(in8str) \ + if (1) { \ + wchar_t *in8str##_16 = alloc_utf16_from_8((const char *)in8str, 0) #define UTF16_UN_ENCODE(in8str) \ - free(in8str ## _16); } (void)0 + free(in8str##_16); \ + } \ + (void)0 -#ifdef __cplusplus +#ifdef __cplusplus } #endif -#endif /* __UTFCONV_H__ */ +#endif /* __UTFCONV_H__ */ -- cgit v1.2.3