Atlas - SDL_syslocale.c

Home / ext / SDL / src / locale / unix Lines: 1 | Size: 2808 bytes [Download] [Show on GitHub] [Search similar files] [Raw] [Raw (proxy)]
[FILE BEGIN]
1/* 2 Simple DirectMedia Layer 3 Copyright (C) 1997-2025 Sam Lantinga <[email protected]> 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 25static void normalize_locale_str(char *dst, char *str, size_t buflen) 26{ 27 char *ptr; 28 29 ptr = SDL_strchr(str, '.'); // chop off encoding if specified. 30 if (ptr) { 31 *ptr = '\0'; 32 } 33 34 ptr = SDL_strchr(str, '@'); // chop off extra bits if specified. 35 if (ptr) { 36 *ptr = '\0'; 37 } 38 39 // The "C" locale isn't useful for our needs, ignore it if you see it. 40 if ((str[0] == 'C') && (str[1] == '\0')) { 41 return; 42 } 43 44 if (*str) { 45 if (*dst) { 46 SDL_strlcat(dst, ",", buflen); // SDL has these split by commas 47 } 48 SDL_strlcat(dst, str, buflen); 49 } 50} 51 52static void normalize_locales(char *dst, char *src, size_t buflen) 53{ 54 char *ptr; 55 56 // entries are separated by colons 57 while ((ptr = SDL_strchr(src, ':')) != NULL) { 58 *ptr = '\0'; 59 normalize_locale_str(dst, src, buflen); 60 src = ptr + 1; 61 } 62 normalize_locale_str(dst, src, buflen); 63} 64 65bool SDL_SYS_GetPreferredLocales(char *buf, size_t buflen) 66{ 67 // !!! FIXME: should we be using setlocale()? Or some D-Bus thing? 68 bool isstack; 69 const char *envr; 70 char *tmp; 71 72 SDL_assert(buflen > 0); 73 tmp = SDL_small_alloc(char, buflen, &isstack); 74 if (!tmp) { 75 return false; 76 } 77 78 *tmp = '\0'; 79 80 // LANG is the primary locale (maybe) 81 envr = SDL_getenv("LANG"); 82 if (envr) { 83 SDL_strlcpy(tmp, envr, buflen); 84 } 85 86 // fallback languages 87 envr = SDL_getenv("LANGUAGE"); 88 if (envr) { 89 if (*tmp) { 90 SDL_strlcat(tmp, ":", buflen); 91 } 92 SDL_strlcat(tmp, envr, buflen); 93 } 94 95 if (*tmp == '\0') { 96 SDL_SetError("LANG environment variable isn't set"); 97 } else { 98 normalize_locales(buf, tmp, buflen); 99 } 100 101 SDL_small_free(tmp, isstack); 102 return true; 103} 104
[FILE END]
(C) 2025 0x4248 (C) 2025 4248 Media and 4248 Systems, All part of 0x4248 See LICENCE files for more information. Not all files are by 0x4248 always check Licencing.