Reputation: 141
Now I'm writing a C .header file for my library, which handles UTF-16
characters.
This .h should compile on Linux/Windows 32/64 bits
in MSVC/GCC
.
Since it's a lib header, I cannot stick to C99
and later. So I cannot use wchar_t
or uint16_t
. How can I specify a UTF-16
variable?
So far I came to this:
#if _WIN32
typedef wchar_t char_UTF16;
#else
#if __GNUC__
typedef unsigned short char_UTF16;
#else
#error "Compiler not supported"
#endif
#endif
But I really don't think this is the best solution.
Upvotes: 0
Views: 1929
Reputation: 126777
You can try some of the builtin types in a compiler-agnostic way:
#include <limits.h>
#include <wchar.h>
#if (WCHAR_MAX==65535) && WCHAR_MIN==0
typedef wchar_t char_UTF16;
#elif USHRT_MAX==65535
typedef unsigned short char_UTF16;
#elif UINT_MAX==65535
typedef unsigned char_UTF16;
#else
# error "Cannot find 16-bit type"
#endif
(there's no point in trying unsigned long
, since the standard requires it to be at least 32 bit wide)
... although, I'm not really sure if you should even try wchar_t
, probably I'd go straight for the numeric types, otherwise you risk having your clients assuming that e.g. wide char literals are of the "right" type for your library while in facts they are only on Windows.
Upvotes: 4