SDL  2.0
SDL_locale.c File Reference
#include "../SDL_internal.h"
#include "SDL_syslocale.h"
#include "SDL_hints.h"
+ Include dependency graph for SDL_locale.c:

Go to the source code of this file.

Functions

static SDL_Localebuild_locales_from_csv_string (char *csv)
 
SDL_LocaleSDL_GetPreferredLocales (void)
 Report the user's preferred locale. More...
 

Function Documentation

◆ build_locales_from_csv_string()

static SDL_Locale* build_locales_from_csv_string ( char *  csv)
static

Definition at line 27 of file SDL_locale.c.

28 {
29  size_t num_locales = 1; /* at least one */
30  size_t slen;
31  size_t alloclen;
32  char *ptr;
33  SDL_Locale *loc;
35 
36  if (!csv || !csv[0]) {
37  return NULL; /* nothing to report */
38  }
39 
40  for (ptr = csv; *ptr; ptr++) {
41  if (*ptr == ',') {
42  num_locales++;
43  }
44  }
45 
46  num_locales++; /* one more for terminator */
47 
48  slen = ((size_t) (ptr - csv)) + 1; /* strlen(csv) + 1 */
49  alloclen = slen + (num_locales * sizeof (SDL_Locale));
50 
51  loc = retval = (SDL_Locale *) SDL_calloc(1, alloclen);
52  if (!retval) {
54  return NULL; /* oh well */
55  }
56  ptr = (char *) (retval + num_locales);
57  SDL_strlcpy(ptr, csv, slen);
58 
59  while (SDL_TRUE) { /* parse out the string */
60  while (*ptr == ' ') ptr++; /* skip whitespace. */
61  if (*ptr == '\0') {
62  break;
63  }
64  loc->language = ptr++;
65  while (SDL_TRUE) {
66  const char ch = *ptr;
67  if (ch == '_') {
68  *(ptr++) = '\0';
69  loc->country = ptr;
70  } else if (ch == ' ') {
71  *(ptr++) = '\0'; /* trim ending whitespace and keep going. */
72  } else if (ch == ',') {
73  *(ptr++) = '\0';
74  loc++;
75  break;
76  } else if (ch == '\0') {
77  loc++;
78  break;
79  } else {
80  ptr++; /* just keep going, still a valid string */
81  }
82  }
83  }
84 
85  return retval;
86 }
unsigned int size_t
#define SDL_strlcpy
#define SDL_calloc
#define SDL_OutOfMemory()
Definition: SDL_error.h:88
@ SDL_TRUE
Definition: SDL_stdinc.h:170
#define NULL
Definition: begin_code.h:163
set set set set set set set set set set set set set set set set set set set set *set set set macro pixldst op &r &cond WK op &r &cond WK op &r &cond WK else op &m &cond &ia op &r &cond WK else op &m &cond &ia elseif elseif else error unsupported base if elseif elseif else error unsupported unaligned pixldst unaligned endm macro pixst base base else pixldst base endif endm macro PF ptr
const char * country
Definition: SDL_locale.h:46
const char * language
Definition: SDL_locale.h:45
SDL_bool retval

References SDL_Locale::country, SDL_Locale::language, NULL, ptr, retval, SDL_calloc, SDL_OutOfMemory, SDL_strlcpy, and SDL_TRUE.

Referenced by SDL_GetPreferredLocales().

◆ SDL_GetPreferredLocales()

SDL_Locale* SDL_GetPreferredLocales ( void  )

Report the user's preferred locale.

This returns an array of SDL_Locale structs, the final item zeroed out. When the caller is done with this array, it should call SDL_free() on the returned value; all the memory involved is allocated in a single block, so a single SDL_free() will suffice.

Returned language strings are in the format xx, where 'xx' is an ISO-639 language specifier (such as "en" for English, "de" for German, etc). Country strings are in the format YY, where "YY" is an ISO-3166 country code (such as "US" for the United States, "CA" for Canada, etc). Country might be NULL if there's no specific guidance on them (so you might get { "en", "US" } for American English, but { "en", NULL } means "English language, generically"). Language strings are never NULL, except to terminate the array.

Please note that not all of these strings are 2 characters; some are three or more.

The returned list of locales are in the order of the user's preference. For example, a German citizen that is fluent in US English and knows enough Japanese to navigate around Tokyo might have a list like: { "de", "en_US", "jp", NULL }. Someone from England might prefer British English (where "color" is spelled "colour", etc), but will settle for anything like it: { "en_GB", "en", NULL }.

This function returns NULL on error, including when the platform does not supply this information at all.

This might be a "slow" call that has to query the operating system. It's best to ask for this once and save the results. However, this list can change, usually because the user has changed a system preference outside of your program; SDL will send an SDL_LOCALECHANGED event in this case, if possible, and you can call this function again to get an updated copy of preferred locales.

Returns
array of locales, terminated with a locale with a NULL language field. Will return NULL on error.

Definition at line 89 of file SDL_locale.c.

90 {
91  char locbuf[128]; /* enough for 21 "xx_YY," language strings. */
92  const char *hint = SDL_GetHint(SDL_HINT_PREFERRED_LOCALES);
93  if (hint) {
94  SDL_strlcpy(locbuf, hint, sizeof (locbuf));
95  } else {
96  SDL_zeroa(locbuf);
97  SDL_SYS_GetPreferredLocales(locbuf, sizeof (locbuf));
98  }
99  return build_locales_from_csv_string(locbuf);
100 }
#define SDL_GetHint
#define SDL_HINT_PREFERRED_LOCALES
Override for SDL_GetPreferredLocales()
Definition: SDL_hints.h:1486
static SDL_Locale * build_locales_from_csv_string(char *csv)
Definition: SDL_locale.c:27
#define SDL_zeroa(x)
Definition: SDL_stdinc.h:428
void SDL_SYS_GetPreferredLocales(char *buf, size_t buflen)
Definition: SDL_syslocale.c:27

References build_locales_from_csv_string(), SDL_GetHint, SDL_HINT_PREFERRED_LOCALES, SDL_strlcpy, SDL_SYS_GetPreferredLocales(), and SDL_zeroa.