SDL  2.0
SDL_syslocale.cc
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 <AppKit.h>
23 #include <LocaleRoster.h>
24 #include <TypeConstants.h>
25 
26 #include "../../SDL_internal.h"
27 #include "../SDL_syslocale.h"
28 
29 void
30 SDL_SYS_GetPreferredLocales(char *buf, size_t buflen)
31 {
32  BLocaleRoster *roster = BLocaleRoster::Default();
33  roster->Refresh();
34 
35  BMessage msg;
36  if (roster->GetPreferredLanguages(&msg) != B_OK) {
37  SDL_SetError("BLocaleRoster couldn't get preferred languages");
38  return;
39  }
40 
41  const char *key = "language";
42  type_code typ = B_ANY_TYPE;
43  int32 numlangs = 0;
44  if ((msg.GetInfo(key, &typ, &numlangs) != B_OK) || (typ != B_STRING_TYPE)) {
45  SDL_SetError("BLocaleRoster message was wrong");
46  return;
47  }
48 
49  for (int32 i = 0; i < numlangs; i++) {
50  const char *str = NULL;
51  if (msg.FindString(key, i, &str) != B_OK) {
52  continue;
53  }
54 
55  const size_t len = SDL_strlen(str);
56  if (buflen <= len) {
57  break; // can't fit it, we're done.
58  }
59 
60  SDL_strlcpy(buf, str, buflen);
61  buf += len;
62  buflen -= len;
63 
64  if (i < (numlangs - 1)) {
65  if (buflen <= 1) {
66  break; // out of room, stop looking.
67  }
68  buf[0] = ','; // add a comma between entries.
69  buf[1] = '\0';
70  buf++;
71  buflen--;
72  }
73  }
74 }
75 
76 /* vi: set ts=4 sw=4 expandtab: */
77 
#define SDL_SetError
#define SDL_strlen
#define SDL_strlcpy
GLenum GLsizei len
GLenum GLuint GLenum GLsizei const GLchar * buf
void SDL_SYS_GetPreferredLocales(char *buf, size_t buflen)
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
#define NULL
Definition: begin_code.h:163
GLuint64 key
Definition: gl2ext.h:2192