C语言跨Windows/macOS实现const char*转UTF16及编译问题解决
ASCII转UTF16跨平台实现问题
我当前的实现方式不够规范且过于繁琐,有没有简单的方法可以在Windows和macOS平台实现ASCII到UTF16的转换?
注意:
prUTF16Char类型我无法修改🙃
现有实现
前置代码
#include <stdlib.h> #include <stdio.h> #include <string.h> #if defined(__APPLE__) && defined(__MACH__) #include <xcselect.h> #include <wchar.h> #include <CoreFoundation/CoreFoundation.h> typedef unsigned short int prUTF16Char; #else typedef wchar_t prUTF16Char; #endif #define WIDEN2(x) L ## x #define WIDEN(x) WIDEN2(x) #define PROJECT_NAME "foo"
功能函数
void copy2ConvertStringLiteralIntoUTF16(const wchar_t* inputString, prUTF16Char* destination) { size_t length = wcslen(inputString); #if (defined(_WIN32) || defined(__WIN32__) || defined(__WINDOWS__)) && defined(PLUGIN_MODE) wcscpy_s(destination, length + 1, inputString); #elif defined(__APPLE__) && defined(__MACH__) CFRange range = {0, 150}; range.length = length; CFStringRef inputStringCFSR = CFStringCreateWithBytes( kCFAllocatorDefault, reinterpret_cast<const UInt8 *>(inputString), length * sizeof(wchar_t), kCFStringEncodingUTF32LE, false); CFStringGetBytes( inputStringCFSR, range, kCFStringEncodingUTF16, 0, false, reiterpret_cast<UInt8 *>(destination), length * (sizeof (prUTF16Char)), NULL); destination[length] = 0; // Set NULL-terminator CFRelease(inputStringCFSR); #endif } const prUTF16Char * to_wchar(const char* message) { const size_t cSize = strlen(message); wchar_t *w_str = new wchar_t[cSize]; #if defined(_WIN32) || defined(__WIN32__) || defined(__WINDOWS__) size_t outSize; mbstowcs_s(&outSize, w_str, cSize, message, cSize-1); return w_str; #else mbstowcs(w_str, message, cSize); #endif #if defined(__APPLE__) && defined(__MACH__) prUTF16Char *ut16str = new prUTF16Char[cSize]; copy2ConvertStringLiteralIntoUTF16(w_str, ut16str); return ut16str; #else return w_str; #endif }
我之后定义了如下全局变量:
static const prUTF16Char* PROJECT_NAME_W = #if defined(__APPLE__) && defined(__MACH__) to_wchar #elif defined(_WIN32) || defined(__WIN32__) || defined(__WINDOWS__) WIDEN #endif (PROJECT_NAME);
以及接收message参数的通用打印函数体:
#if WCHAR_UTF16 wprintf(L"%s", #else printf("%ls\n", #endif message);
完整代码目前正在从C++重写为C。
报错信息
error: initializer element is not a compile-time constant
临时兼容方案
目前我参考解决方案实现了如下临时兼容代码:
#if defined(__APPLE__) && defined(__MACH__) extern #elif defined(_WIN32) || defined(__WIN32__) || defined(__WINDOWS__) static #endif const prUTF16Char* PROJECT_NAME_W #if defined(__APPLE__) && defined(__MACH__) ; #elif defined(_WIN32) || defined(__WIN32__) || defined(__WINDOWS__) WIDEN(PROJECT_NAME); #endif
…并且仅对extern声明的变量执行初始化和free操作。
内容的提问来源于stack exchange,提问作者Samuel Marks
相关产品推荐
相关产品推荐

