SDL  2.0
SDL_locale.c
Go to the documentation of this file.
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_hints.h"
25 
26 static SDL_Locale *
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 }
87 
88 SDL_Locale *
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 }
101 
102 /* vi: set ts=4 sw=4 expandtab: */
103 
unsigned int size_t
#define SDL_strlcpy
#define SDL_calloc
#define SDL_GetHint
#define SDL_OutOfMemory()
Definition: SDL_error.h:88
#define SDL_HINT_PREFERRED_LOCALES
Override for SDL_GetPreferredLocales()
Definition: SDL_hints.h:1486
SDL_Locale * SDL_GetPreferredLocales(void)
Report the user's preferred locale.
Definition: SDL_locale.c:89
static SDL_Locale * build_locales_from_csv_string(char *csv)
Definition: SDL_locale.c:27
#define SDL_zeroa(x)
Definition: SDL_stdinc.h:428
@ SDL_TRUE
Definition: SDL_stdinc.h:170
void SDL_SYS_GetPreferredLocales(char *buf, size_t buflen)
Definition: SDL_syslocale.c:27
#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