sdl

FORK: Simple Directmedia Layer
git clone https://git.neptards.moe/neptards/sdl.git
Log | Files | Refs

SDL_syslocale.c (2902B)


      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 
     25 static void
     26 normalize_locale_str(char *dst, char *str, size_t buflen)
     27 {
     28     char *ptr;
     29 
     30     ptr = SDL_strchr(str, '.');  /* chop off encoding if specified. */
     31     if (ptr != NULL) {
     32         *ptr = '\0';
     33     }
     34 
     35     ptr = SDL_strchr(str, '@');  /* chop off extra bits if specified. */
     36     if (ptr != NULL) {
     37         *ptr = '\0';
     38     }
     39 
     40     /* The "C" locale isn't useful for our needs, ignore it if you see it. */
     41     if ((str[0] == 'C') && (str[1] == '\0')) {
     42         return;
     43     }
     44 
     45     if (*str) {
     46         if (*dst) {
     47             SDL_strlcat(dst, ",", buflen);  /* SDL has these split by commas */
     48         }
     49         SDL_strlcat(dst, str, buflen);
     50     }
     51 }
     52 
     53 static void
     54 normalize_locales(char *dst, char *src, size_t buflen)
     55 {
     56     char *ptr;
     57 
     58     /* entries are separated by colons */
     59     while ((ptr = SDL_strchr(src, ':')) != NULL) {
     60         *ptr = '\0';
     61         normalize_locale_str(dst, src, buflen);
     62         src = ptr + 1;
     63     }
     64     normalize_locale_str(dst, src, buflen);
     65 }
     66 
     67 void
     68 SDL_SYS_GetPreferredLocales(char *buf, size_t buflen)
     69 {
     70     /* !!! FIXME: should we be using setlocale()? Or some D-Bus thing? */
     71     SDL_bool isstack;
     72     const char *envr;
     73     char *tmp;
     74 
     75     SDL_assert(buflen > 0);
     76     tmp = SDL_small_alloc(char, buflen, &isstack);
     77     if (!tmp) {
     78         SDL_OutOfMemory();
     79         return;
     80     }
     81 
     82     *tmp = '\0';
     83 
     84     /* LANG is the primary locale (maybe) */
     85     envr = SDL_getenv("LANG");
     86     if (envr) {
     87         SDL_strlcpy(tmp, envr, buflen);
     88     }
     89 
     90     /* fallback languages */
     91     envr = SDL_getenv("LANGUAGE");
     92     if (envr) {
     93         if (*tmp) {
     94             SDL_strlcat(tmp, ":", buflen);
     95         }
     96         SDL_strlcat(tmp, envr, buflen);
     97     }
     98 
     99     if (*tmp == '\0') {
    100         SDL_SetError("LANG environment variable isn't set");
    101     } else {
    102         normalize_locales(buf, tmp, buflen);
    103     }
    104 
    105     SDL_small_free(tmp, isstack);
    106 }
    107 
    108 /* vi: set ts=4 sw=4 expandtab: */
    109