1 /*
2   Simple DirectMedia Layer
3   Copyright (C) 1997-2020 Sam Lantinga <slouken@libsdl.org>
4 
5   This software is provided 'as-is', without any express or implied
6   warranty.  In no event will the authors be held liable for any damages
7   arising from the use of this software.
8 
9   Permission is granted to anyone to use this software for any purpose,
10   including commercial applications, and to alter it and redistribute it
11   freely, subject to the following restrictions:
12 
13   1. The origin of this software must not be misrepresented; you must not
14      claim that you wrote the original software. If you use this software
15      in a product, an acknowledgment in the product documentation would be
16      appreciated but is not required.
17   2. Altered source versions must be plainly marked as such, and must not be
18      misrepresented as being the original software.
19   3. This notice may not be removed or altered from any source distribution.
20 */
21 
22 #include "../../SDL_internal.h"
23 #include "../SDL_syslocale.h"
24 #include "SDL_assert.h"
25 
26 static void
normalize_locale_str(char * dst,char * str,size_t buflen)27 normalize_locale_str(char *dst, char *str, size_t buflen)
28 {
29     char *ptr;
30 
31     ptr = SDL_strchr(str, '.');  /* chop off encoding if specified. */
32     if (ptr != NULL) {
33         *ptr = '\0';
34     }
35 
36     ptr = SDL_strchr(str, '@');  /* chop off extra bits if specified. */
37     if (ptr != NULL) {
38         *ptr = '\0';
39     }
40 
41     /* The "C" locale isn't useful for our needs, ignore it if you see it. */
42     if ((str[0] == 'C') && (str[1] == '\0')) {
43         return;
44     }
45 
46     if (*str) {
47         if (*dst) {
48             SDL_strlcat(dst, ",", buflen);  /* SDL has these split by commas */
49         }
50         SDL_strlcat(dst, str, buflen);
51     }
52 }
53 
54 static void
normalize_locales(char * dst,char * src,size_t buflen)55 normalize_locales(char *dst, char *src, size_t buflen)
56 {
57     char *ptr;
58 
59     /* entries are separated by colons */
60     while ((ptr = SDL_strchr(src, ':')) != NULL) {
61         *ptr = '\0';
62         normalize_locale_str(dst, src, buflen);
63         src = ptr + 1;
64     }
65     normalize_locale_str(dst, src, buflen);
66 }
67 
68 void
SDL_SYS_GetPreferredLocales(char * buf,size_t buflen)69 SDL_SYS_GetPreferredLocales(char *buf, size_t buflen)
70 {
71     /* !!! FIXME: should we be using setlocale()? Or some D-Bus thing? */
72     SDL_bool isstack;
73     const char *envr;
74     char *tmp;
75 
76     SDL_assert(buflen > 0);
77     tmp = SDL_small_alloc(char, buflen, &isstack);
78     if (!tmp) {
79         SDL_OutOfMemory();
80         return;
81     }
82 
83     *tmp = '\0';
84 
85     /* LANG is the primary locale (maybe) */
86     envr = SDL_getenv("LANG");
87     if (envr) {
88         SDL_strlcpy(tmp, envr, buflen);
89     }
90 
91     /* fallback languages */
92     envr = SDL_getenv("LANGUAGE");
93     if (envr) {
94         if (*tmp) {
95             SDL_strlcat(tmp, ":", buflen);
96         }
97         SDL_strlcat(tmp, envr, buflen);
98     }
99 
100     if (*tmp == '\0') {
101         SDL_SetError("LANG environment variable isn't set");
102     } else {
103         normalize_locales(buf, tmp, buflen);
104     }
105 
106     SDL_small_free(tmp, isstack);
107 }
108 
109 /* vi: set ts=4 sw=4 expandtab: */
110 
111