Ryan's fix is still good for the built-in version of iconv_open() SDL-1.2
authorSam Lantinga <slouken@libsdl.org>
Thu, 12 Jul 2007 08:04:10 +0000
branchSDL-1.2
changeset 40556ed1fded55ff
parent 4054 64bf737873a2
child 4056 6f7af895ab02
Ryan's fix is still good for the built-in version of iconv_open()
src/stdlib/SDL_iconv.c
     1.1 --- a/src/stdlib/SDL_iconv.c	Thu Jul 12 07:47:29 2007 +0000
     1.2 +++ b/src/stdlib/SDL_iconv.c	Thu Jul 12 08:04:10 2007 +0000
     1.3 @@ -131,9 +131,10 @@
     1.4  	{ "UCS-4",	ENCODING_UCS4 },
     1.5  };
     1.6  
     1.7 -static const char *getlocale()
     1.8 +static const char *getlocale(char *buffer, size_t bufsize)
     1.9  {
    1.10  	const char *lang;
    1.11 +	char *ptr;
    1.12  
    1.13  	lang = SDL_getenv("LC_ALL");
    1.14  	if ( !lang ) {
    1.15 @@ -148,7 +149,20 @@
    1.16  	if ( !lang || !*lang || SDL_strcmp(lang, "C") == 0 ) {
    1.17  		lang = "ASCII";
    1.18  	}
    1.19 -	return lang;
    1.20 +
    1.21 +	/* We need to trim down strings like "en_US.UTF-8@blah" to "UTF-8" */
    1.22 +	ptr = SDL_strchr(lang, '.');
    1.23 +	if (ptr != NULL) {
    1.24 +		lang = ptr + 1;
    1.25 +	}
    1.26 +
    1.27 +	SDL_strlcpy(buffer, lang, bufsize);
    1.28 +	ptr = SDL_strchr(buffer, '@');
    1.29 +	if (ptr != NULL) {
    1.30 +		*ptr = '\0';  /* chop end of string. */
    1.31 +	}
    1.32 +
    1.33 +	return buffer;
    1.34  }
    1.35  
    1.36  SDL_iconv_t SDL_iconv_open(const char *tocode, const char *fromcode)
    1.37 @@ -156,12 +170,14 @@
    1.38  	int src_fmt = ENCODING_UNKNOWN;
    1.39  	int dst_fmt = ENCODING_UNKNOWN;
    1.40  	int i;
    1.41 +	char fromcode_buffer[64];
    1.42 +	char tocode_buffer[64];
    1.43  
    1.44  	if ( !fromcode || !*fromcode ) {
    1.45 -		fromcode = getlocale();
    1.46 +		fromcode = getlocale(fromcode_buffer, sizeof(fromcode_buffer));
    1.47  	}
    1.48  	if ( !tocode || !*tocode ) {
    1.49 -		fromcode = getlocale();
    1.50 +		tocode = getlocale(tocode_buffer, sizeof(tocode_buffer));
    1.51  	}
    1.52  	for ( i = 0; i < SDL_arraysize(encodings); ++i ) {
    1.53  		if ( SDL_strcasecmp(fromcode, encodings[i].name) == 0 ) {