|
@@ -22,39 +22,48 @@
|
|
|
#include "SDL_internal.h"
|
|
|
#include "SDL_syslocale.h"
|
|
|
|
|
|
-static SDL_Locale *build_locales_from_csv_string(char *csv)
|
|
|
+static const SDL_Locale * const *build_locales_from_csv_string(char *csv, int *count)
|
|
|
{
|
|
|
- size_t num_locales = 1; /* at least one */
|
|
|
+ int i, num_locales;
|
|
|
size_t slen;
|
|
|
size_t alloclen;
|
|
|
char *ptr;
|
|
|
SDL_Locale *loc;
|
|
|
- SDL_Locale *retval;
|
|
|
+ const SDL_Locale **retval;
|
|
|
|
|
|
- if (!csv || !csv[0]) {
|
|
|
+ if (count) {
|
|
|
+ *count = 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ while (csv && *csv && SDL_isspace(*csv)) {
|
|
|
+ ++csv;
|
|
|
+ }
|
|
|
+ if (!csv || !*csv) {
|
|
|
return NULL; /* nothing to report */
|
|
|
}
|
|
|
|
|
|
+ num_locales = 1; /* at least one */
|
|
|
for (ptr = csv; *ptr; ptr++) {
|
|
|
if (*ptr == ',') {
|
|
|
num_locales++;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- num_locales++; /* one more for terminator */
|
|
|
-
|
|
|
slen = ((size_t)(ptr - csv)) + 1; /* SDL_strlen(csv) + 1 */
|
|
|
- alloclen = slen + (num_locales * sizeof(SDL_Locale));
|
|
|
+ alloclen = (num_locales * sizeof(SDL_Locale *)) + (num_locales * sizeof(SDL_Locale)) + slen;
|
|
|
|
|
|
- loc = retval = (SDL_Locale *)SDL_calloc(1, alloclen);
|
|
|
+ retval = (const SDL_Locale **)SDL_calloc(1, alloclen);
|
|
|
if (!retval) {
|
|
|
return NULL; /* oh well */
|
|
|
}
|
|
|
- ptr = (char *)(retval + num_locales);
|
|
|
- SDL_strlcpy(ptr, csv, slen);
|
|
|
+ loc = (SDL_Locale *)((Uint8 *)retval + ((num_locales + 1) * sizeof(SDL_Locale *)));
|
|
|
+ ptr = (char *)(loc + num_locales);
|
|
|
+ SDL_memcpy(ptr, csv, slen);
|
|
|
|
|
|
+ i = 0;
|
|
|
+ retval[i++] = loc;
|
|
|
while (SDL_TRUE) { /* parse out the string */
|
|
|
- while (*ptr == ' ') {
|
|
|
+ while (SDL_isspace(*ptr)) {
|
|
|
ptr++; /* skip whitespace. */
|
|
|
}
|
|
|
|
|
@@ -64,17 +73,17 @@ static SDL_Locale *build_locales_from_csv_string(char *csv)
|
|
|
loc->language = ptr++;
|
|
|
while (SDL_TRUE) {
|
|
|
const char ch = *ptr;
|
|
|
- if (ch == '_') {
|
|
|
+ if (ch == '_' || ch == '-') {
|
|
|
*(ptr++) = '\0';
|
|
|
loc->country = ptr;
|
|
|
- } else if (ch == ' ') {
|
|
|
+ } else if (SDL_isspace(ch)) {
|
|
|
*(ptr++) = '\0'; /* trim ending whitespace and keep going. */
|
|
|
} else if (ch == ',') {
|
|
|
*(ptr++) = '\0';
|
|
|
loc++;
|
|
|
+ retval[i++] = loc;
|
|
|
break;
|
|
|
} else if (ch == '\0') {
|
|
|
- loc++;
|
|
|
break;
|
|
|
} else {
|
|
|
ptr++; /* just keep going, still a valid string */
|
|
@@ -82,10 +91,14 @@ static SDL_Locale *build_locales_from_csv_string(char *csv)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- return retval;
|
|
|
+ if (count) {
|
|
|
+ *count = num_locales;
|
|
|
+ }
|
|
|
+
|
|
|
+ return SDL_FreeLater(retval);
|
|
|
}
|
|
|
|
|
|
-SDL_Locale *SDL_GetPreferredLocales(void)
|
|
|
+const SDL_Locale * const *SDL_GetPreferredLocales(int *count)
|
|
|
{
|
|
|
char locbuf[128]; /* enough for 21 "xx_YY," language strings. */
|
|
|
const char *hint = SDL_GetHint(SDL_HINT_PREFERRED_LOCALES);
|
|
@@ -95,5 +108,5 @@ SDL_Locale *SDL_GetPreferredLocales(void)
|
|
|
SDL_zeroa(locbuf);
|
|
|
SDL_SYS_GetPreferredLocales(locbuf, sizeof(locbuf));
|
|
|
}
|
|
|
- return build_locales_from_csv_string(locbuf);
|
|
|
+ return build_locales_from_csv_string(locbuf, count);
|
|
|
}
|