| 1 | /* |
| 2 | Simple DirectMedia Layer |
| 3 | Copyright (C) 1997-2025 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 | |
| 25 | static SDL_Locale **build_locales_from_csv_string(char *csv, int *count) |
| 26 | { |
| 27 | int i, num_locales; |
| 28 | size_t slen; |
| 29 | size_t alloclen; |
| 30 | char *ptr; |
| 31 | SDL_Locale *loc; |
| 32 | SDL_Locale **result; |
| 33 | |
| 34 | if (count) { |
| 35 | *count = 0; |
| 36 | } |
| 37 | |
| 38 | while (csv && *csv && SDL_isspace(*csv)) { |
| 39 | ++csv; |
| 40 | } |
| 41 | if (!csv || !*csv) { |
| 42 | return NULL; // nothing to report |
| 43 | } |
| 44 | |
| 45 | num_locales = 1; // at least one |
| 46 | for (ptr = csv; *ptr; ptr++) { |
| 47 | if (*ptr == ',') { |
| 48 | num_locales++; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | slen = ((size_t)(ptr - csv)) + 1; // SDL_strlen(csv) + 1 |
| 53 | alloclen = ((num_locales + 1) * sizeof(SDL_Locale *)) + (num_locales * sizeof(SDL_Locale)) + slen; |
| 54 | |
| 55 | result = (SDL_Locale **)SDL_calloc(1, alloclen); |
| 56 | if (!result) { |
| 57 | return NULL; // oh well |
| 58 | } |
| 59 | loc = (SDL_Locale *)(result + (num_locales + 1)); |
| 60 | ptr = (char *)(loc + num_locales); |
| 61 | SDL_memcpy(ptr, csv, slen); |
| 62 | |
| 63 | i = 0; |
| 64 | result[i++] = loc; |
| 65 | while (true) { // parse out the string |
| 66 | while (SDL_isspace(*ptr)) { |
| 67 | ptr++; // skip whitespace. |
| 68 | } |
| 69 | |
| 70 | if (*ptr == '\0') { |
| 71 | break; |
| 72 | } |
| 73 | loc->language = ptr++; |
| 74 | while (true) { |
| 75 | const char ch = *ptr; |
| 76 | if (ch == '_') { |
| 77 | *(ptr++) = '\0'; |
| 78 | loc->country = ptr; |
| 79 | } else if (SDL_isspace(ch)) { |
| 80 | *(ptr++) = '\0'; // trim ending whitespace and keep going. |
| 81 | } else if (ch == ',') { |
| 82 | *(ptr++) = '\0'; |
| 83 | loc++; |
| 84 | result[i++] = loc; |
| 85 | break; |
| 86 | } else if (ch == '\0') { |
| 87 | break; |
| 88 | } else { |
| 89 | ptr++; // just keep going, still a valid string |
| 90 | } |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | if (count) { |
| 95 | *count = num_locales; |
| 96 | } |
| 97 | |
| 98 | return result; |
| 99 | } |
| 100 | |
| 101 | SDL_Locale **SDL_GetPreferredLocales(int *count) |
| 102 | { |
| 103 | char locbuf[128]; // enough for 21 "xx_YY," language strings. |
| 104 | const char *hint = SDL_GetHint(SDL_HINT_PREFERRED_LOCALES); |
| 105 | if (hint) { |
| 106 | SDL_strlcpy(locbuf, hint, sizeof(locbuf)); |
| 107 | } else { |
| 108 | SDL_zeroa(locbuf); |
| 109 | SDL_SYS_GetPreferredLocales(locbuf, sizeof(locbuf)); |
| 110 | } |
| 111 | return build_locales_from_csv_string(locbuf, count); |
| 112 | } |
| 113 | |