#include "SDL_internal.h"
#include "../SDL_syslocale.h"
static void normalize_locale_str(char *dst, char *str, size_t buflen)
{
char *ptr;
ptr = SDL_strchr(str, '.'); if (ptr) {
*ptr = '\0';
}
ptr = SDL_strchr(str, '@'); if (ptr) {
*ptr = '\0';
}
if ((str[0] == 'C') && (str[1] == '\0')) {
return;
}
if (*str) {
if (*dst) {
SDL_strlcat(dst, ",", buflen); }
SDL_strlcat(dst, str, buflen);
}
}
static void normalize_locales(char *dst, char *src, size_t buflen)
{
char *ptr;
while ((ptr = SDL_strchr(src, ':')) != NULL) {
*ptr = '\0';
normalize_locale_str(dst, src, buflen);
src = ptr + 1;
}
normalize_locale_str(dst, src, buflen);
}
bool SDL_SYS_GetPreferredLocales(char *buf, size_t buflen)
{
bool isstack;
const char *envr;
char *tmp;
SDL_assert(buflen > 0);
tmp = SDL_small_alloc(char, buflen, &isstack);
if (!tmp) {
return false;
}
*tmp = '\0';
envr = SDL_getenv("LANG");
if (envr) {
SDL_strlcpy(tmp, envr, buflen);
}
envr = SDL_getenv("LANGUAGE");
if (envr) {
if (*tmp) {
SDL_strlcat(tmp, ":", buflen);
}
SDL_strlcat(tmp, envr, buflen);
}
if (*tmp == '\0') {
SDL_SetError("LANG environment variable isn't set");
} else {
normalize_locales(buf, tmp, buflen);
}
SDL_small_free(tmp, isstack);
return true;
}