string	ws2utf8(const wchar_t *wstr)
	{
		if (!wstr)
			return "";

		int n = wcslen(wstr) + 1;
		if (n == 1)
			return "";


#if __S3E__
		char *s = (char *)fastAlloc(4 * n);
		s[0] = 0;

		int len = IwWideCharToUTF8((const ucs2char*)wstr, n, s, n * 4);
		s[len] = 0;

		string str = s;
		fastFree(s);
		return str;
#elif OXYGINE_SDL
		char  *s = 0;
		if (sizeof(wchar_t) == 2)		
			s = SDL_iconv_string("UTF-8", "UCS-2-INTERNAL", (const char*)wstr, n * sizeof(wchar_t));
		else
			s = SDL_iconv_string("UTF-8", "UCS-4-INTERNAL", (const char*)wstr, n * sizeof(wchar_t));

		string str = s;
		SDL_free(s);
		return str;
#else
		return "not_implemented";
#endif		
		return "";		
	}
Beispiel #2
0
int main(int argc, char *argv[])
{
	const char * formats[] = {
		"UTF8",
		"UTF-8",
		"UTF16BE",
		"UTF-16BE",
		"UTF16LE",
		"UTF-16LE",
		"UTF32BE",
		"UTF-32BE",
		"UTF32LE",
		"UTF-32LE",
		"UCS4",
		"UCS-4",
	};
	char buffer[BUFSIZ];
	char *ucs4;
	char *test[2];
	int i, index = 0;
	FILE *file;
	int errors = 0;

	if ( !argv[1] ) {
		argv[1] = "utf8.txt";
	}
	file = fopen(argv[1], "rb");
	if ( !file ) {
		fprintf(stderr, "Unable to open %s\n", argv[1]);
		return (1);
	}

	while ( fgets(buffer, sizeof(buffer), file) ) {
		
		size_t len;
		ucs4 = SDL_iconv_string("UCS-4", "UTF-8", buffer, SDL_strlen(buffer)+1);
		len = (widelen(ucs4)+1)*4;
		for ( i = 0; i < SDL_arraysize(formats); ++i ) {
			test[0] = SDL_iconv_string(formats[i], "UCS-4", ucs4, len);
			test[1] = SDL_iconv_string("UCS-4", formats[i], test[0], len);
			if ( !test[1] || SDL_memcmp(test[1], ucs4, len) != 0 ) {
				fprintf(stderr, "FAIL: %s\n", formats[i]);
				++errors;
			}
			if ( test[0] ) {
				SDL_free(test[0]);
			}
			if ( test[1] ) {
				SDL_free(test[1]);
			}
		}
		test[0] = SDL_iconv_string("UTF-8", "UCS-4", ucs4, len);
		SDL_free(ucs4);
		fputs(test[0], stdout);
		SDL_free(test[0]);
	}
	return (errors ? errors + 1 : 0);
}
Beispiel #3
0
static std::string Utf8FromWString(const wchar_t *ws) {
	auto *s = (char*)SDL_iconv_string("UTF-8", "UCS-2-INTERNAL", (char *)(ws), wcslen(ws)*2+2);
	if(!s) return "";
	std::string ss(s);
	SDL_free(s);
	return ss;
}
Beispiel #4
0
static __inline__ char *
utf16_to_utf8(const WCHAR *S)
{
    /* !!! FIXME: this should be UTF-16, not UCS-2! */
    return SDL_iconv_string("UTF-8", "UCS-2", (char *)(S),
                            (SDL_wcslen(S)+1)*sizeof(WCHAR));
}
	static std::wstring Utf8ToWString(const char *s) {
		auto *ws = (WCHAR*)SDL_iconv_string("UCS-2-INTERNAL", "UTF-8", (char *)(s), SDL_strlen(s)+1);
		if(!ws) return L"";
		std::wstring wss(ws);
		SDL_free(ws);
		return wss;
	}
	wstring utf8tows(const char *utf8str)
	{
		if (!utf8str)
			return L"";

		int n = strlen(utf8str) + 1;
		if (n == 1)
			return L"";

		

#if __S3E__
		wchar_t *s = (wchar_t *)fastAlloc(4 * n);
		s[0] = 0;

		int len = IwUTF8ToWideChar(utf8str, n, (ucs2char*)s, n * 4);
		s[len] = 0;

		wstring str = s;
		fastFree(s);
		
		return str;

#elif OXYGINE_SDL
		wchar_t *s = 0;
		if (sizeof(wchar_t) == 2)		
			s = (wchar_t*)SDL_iconv_string("UCS-2-INTERNAL", "UTF-8", utf8str, n);
		else
			s = (wchar_t*)SDL_iconv_string("UCS-4-INTERNAL", "UTF-8", utf8str, n);

		wstring str = s;
		SDL_free(s);
		return str;
#else
		return L"not_implemented";
#endif
	}
Beispiel #7
0
static uint32 convUTF8ToUTF32(const char *src) {
	uint32 utf32 = 0;

	char *dst = SDL_iconv_string(
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
	                             "UTF-32BE",
#else
	                             "UTF-32LE",
#endif
                                 "UTF-8", src, SDL_strlen(src) + 1);

	if (dst) {
		utf32 = *((uint32 *)dst);
		SDL_free(dst);
	}

	return utf32;
}
Beispiel #8
0
static int
X11_GL_ErrorHandler(Display * d, XErrorEvent * e)
{
    char *x11_error = NULL;
    char x11_error_locale[256];

    errorCode = e->error_code;
    if (X11_XGetErrorText(d, errorCode, x11_error_locale, sizeof(x11_error_locale)) == Success)
    {
        x11_error = SDL_iconv_string("UTF-8", "", x11_error_locale, SDL_strlen(x11_error_locale)+1);
    }

    if (x11_error)
    {
        SDL_SetError("Could not %s: %s", errorHandlerOperation, x11_error);
        SDL_free(x11_error);
    }
    else
    {
        SDL_SetError("Could not %s: %i (Base %i)\n", errorHandlerOperation, errorCode, errorBase);
    }

    return (0);
}
Beispiel #9
0
/* This is where execution begins [windowed apps] */
int WINAPI
WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
{
    char **argv;
    int argc;
    char *cmdline;

    /* Grab the command line */
    TCHAR *text = GetCommandLine();
#if UNICODE
    cmdline = SDL_iconv_string("UTF-8", "UCS-2-INTERNAL", (char *)(text), (SDL_wcslen(text)+1)*sizeof(WCHAR));
#else
    cmdline = SDL_strdup(text);
#endif
    if (cmdline == NULL) {
        return OutOfMemory();
    }

    /* Parse it into argv and argc */
    argc = ParseCommandLine(cmdline, NULL);
    argv = SDL_stack_alloc(char *, argc + 1);
    if (argv == NULL) {
        return OutOfMemory();
    }
    ParseCommandLine(cmdline, argv);

    /* Run the main program */
    console_main(argc, argv);

    SDL_stack_free(argv);

    SDL_free(cmdline);

    /* Hush little compiler, don't you cry... */
    return 0;
}
Beispiel #10
0
int
main(int argc, char *argv[])
{
    const char *formats[] = {
        "UTF8",
        "UTF-8",
        "UTF16BE",
        "UTF-16BE",
        "UTF16LE",
        "UTF-16LE",
        "UTF32BE",
        "UTF-32BE",
        "UTF32LE",
        "UTF-32LE",
        "UCS4",
        "UCS-4",
    };
    char buffer[BUFSIZ];
    char *ucs4;
    char *test[2];
    int i;
    FILE *file;
    int errors = 0;

    /* Enable standard application logging */
    SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);

    if (!argv[1]) {
        argv[1] = "utf8.txt";
    }
    file = fopen(argv[1], "rb");
    if (!file) {
        SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Unable to open %s\n", argv[1]);
        return (1);
    }

    while (fgets(buffer, sizeof(buffer), file)) {
        /* Convert to UCS-4 */
        size_t len;
        ucs4 =
            SDL_iconv_string("UCS-4", "UTF-8", buffer,
                             SDL_strlen(buffer) + 1);
        len = (widelen(ucs4) + 1) * 4;
        for (i = 0; i < SDL_arraysize(formats); ++i) {
            test[0] = SDL_iconv_string(formats[i], "UCS-4", ucs4, len);
            test[1] = SDL_iconv_string("UCS-4", formats[i], test[0], len);
            if (!test[1] || SDL_memcmp(test[1], ucs4, len) != 0) {
                SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "FAIL: %s\n", formats[i]);
                ++errors;
            }
            SDL_free(test[0]);
            SDL_free(test[1]);
        }
        test[0] = SDL_iconv_string("UTF-8", "UCS-4", ucs4, len);
        SDL_free(ucs4);
        fputs(test[0], stdout);
        SDL_free(test[0]);
    }
    fclose(file);
    return (errors ? errors + 1 : 0);
}
Beispiel #11
0
static void IN_HandleEvents( void )
{
	Uint16 *wtext = NULL;
	SDL_PumpEvents();
	SDL_Event event;

	while( SDL_PollEvent( &event ) ) {
		switch( event.type ) {
			case SDL_KEYDOWN:
				key_event( &event.key, true );

				// Emulate copy/paste
				#if defined( __APPLE__ )
					#define KEYBOARD_COPY_PASTE_MODIFIER KMOD_GUI
				#else
					#define KEYBOARD_COPY_PASTE_MODIFIER KMOD_CTRL
				#endif
				
				if( event.key.keysym.sym == SDLK_c ) {
					if( event.key.keysym.mod & KEYBOARD_COPY_PASTE_MODIFIER ) {
						Key_CharEvent( KC_CTRLC, KC_CTRLC );
					}
				}
				else if( event.key.keysym.sym == SDLK_v ) {
					if( event.key.keysym.mod & KEYBOARD_COPY_PASTE_MODIFIER ) {
						Key_CharEvent( KC_CTRLV, KC_CTRLV );
					}
				}

				break;

			case SDL_KEYUP:
				key_event( &event.key, false );
				break;

			case SDL_TEXTINPUT:
				// SDL_iconv_utf8_ucs2 uses "UCS-2-INTERNAL" as tocode and fails to convert text on Linux
				// where SDL_iconv uses system iconv. So we force needed encoding directly

				#if SDL_BYTEORDER == SDL_LIL_ENDIAN
					#define UCS_2_INTERNAL "UCS-2LE"
				#else
					#define UCS_2_INTERNAL "UCS-2BE"
				#endif

				wtext = (Uint16 *)SDL_iconv_string( UCS_2_INTERNAL, "UTF-8", event.text.text, SDL_strlen( event.text.text ) + 1 );
				if( wtext ) {
					wchar_t charkey = wtext[0];
					int key = ( charkey <= 255 ) ? charkey : 0;
					Key_CharEvent( key, charkey );
					SDL_free( wtext );
				}
				break;

			case SDL_MOUSEMOTION:
				mouse_motion_event( &event.motion );
				break;

			case SDL_MOUSEBUTTONDOWN:
				mouse_button_event( &event.button, true );
				break;

			case SDL_MOUSEBUTTONUP:
				mouse_button_event( &event.button, false );
				break;

			case SDL_MOUSEWHEEL:
				mouse_wheel_event( &event.wheel );
				break;

			case SDL_QUIT:
				Cbuf_ExecuteText( EXEC_NOW, "quit" );
				break;

			case SDL_WINDOWEVENT:
				switch( event.window.event ) {
					case SDL_WINDOWEVENT_SHOWN:
						AppActivate( true );
						break;
					case SDL_WINDOWEVENT_HIDDEN:
						AppActivate( false );
						break;
					case SDL_WINDOWEVENT_CLOSE:
						break;
					case SDL_WINDOWEVENT_FOCUS_GAINED:
						input_focus = true;
						break;
					case SDL_WINDOWEVENT_FOCUS_LOST:
						input_focus = false;
						break;
					case SDL_WINDOWEVENT_MOVED:
						// FIXME: move this somewhere else
						Cvar_SetValue( "vid_xpos", event.window.data1 );
						Cvar_SetValue( "vid_ypos", event.window.data2 );
						vid_xpos->modified = false;
						vid_ypos->modified = false;
						break;
				}
				break;
		}
	}
}