ObjFW  Check-in [d6de0a1b71]

Overview
Comment:Parse locale to get {language,territory,encoding}

Yes, there is really nothing to query this.
Yes, locales in C and POSIX are broken way beyond repair.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: d6de0a1b717257503508fa0626ba243744682538c3fed10cb5b1e1bac2ae3958
User & Date: js on 2017-01-08 03:45:05
Other Links: manifest | tags
Context
2017-01-08
03:49
configure.ac: Fix a stupid typo check-in: 504f39c629 user: js tags: trunk
03:45
Parse locale to get {language,territory,encoding} check-in: d6de0a1b71 user: js tags: trunk
02:38
Get native8BitEncoding from locale check-in: f12cecf014 user: js tags: trunk
Changes

Modified configure.ac from [4adbe6b80f] to [26942c5c25].

878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
		AC_MSG_RESULT(no)
	])
	OBJCFLAGS="$old_OBJCFLAGS"
])

AC_CHECK_FUNCS([sysconf gmtime_r localtime_r nanosleep fcntl])

AC_CHECK_HEADERS(langinfo.h xlocale.h)
AC_CHECK_FUNCS([strtod_l strtof_l vasprintf_l])

AC_CHECK_FUNC(pipe, [
	AC_DEFINE(OF_HAVE_PIPE, 1, [Whether we have pipe()])
])

AC_ARG_ENABLE(sockets,







|







878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
		AC_MSG_RESULT(no)
	])
	OBJCFLAGS="$old_OBJCFLAGS"
])

AC_CHECK_FUNCS([sysconf gmtime_r localtime_r nanosleep fcntl])

AC_CHECK_HEADERS(xlocale.h)
AC_CHECK_FUNCS([strtod_l strtof_l vasprintf_l])

AC_CHECK_FUNC(pipe, [
	AC_DEFINE(OF_HAVE_PIPE, 1, [Whether we have pipe()])
])

AC_ARG_ENABLE(sockets,

Modified src/OFApplication.m from [bbaed6eb9f] to [4b696ca492].

68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
- (void)OF_setArgumentCount: (int)argc
      andWideArgumentValues: (wchar_t*[])argv;
#endif
- (void)OF_run;
@end

static OFApplication *app = nil;
extern of_string_encoding_t of_system_info_native_8bit_encoding;
extern OFString *of_system_info_decimal_point;

static void
atexitHandler(void)
{
	id <OFApplicationDelegate> delegate = [app delegate];

	if ([delegate respondsToSelector: @selector(applicationWillTerminate)])







<
|







68
69
70
71
72
73
74

75
76
77
78
79
80
81
82
- (void)OF_setArgumentCount: (int)argc
      andWideArgumentValues: (wchar_t*[])argv;
#endif
- (void)OF_run;
@end

static OFApplication *app = nil;

extern void of_system_info_parse_locale(char*);

static void
atexitHandler(void)
{
	id <OFApplicationDelegate> delegate = [app delegate];

	if ([delegate respondsToSelector: @selector(applicationWillTerminate)])
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
of_application_main(int *argc, char **argv[], Class cls)
{
	id <OFApplicationDelegate> delegate;
#ifdef OF_WINDOWS
	wchar_t **wargv, **wenvp;
	int wargc, si = 0;
#endif
#ifdef HAVE_LANGINFO_H
	char *codeset, *lowerCodeset;
	size_t codesetLen;
#endif

	setlocale(LC_ALL, "");

#ifdef HAVE_LANGINFO_H
	codeset = nl_langinfo(CODESET);
	codesetLen = strlen(codeset);

	if (SIZE_MAX - codesetLen < 1)
		@throw [OFOutOfRangeException exception];

	if ((lowerCodeset = malloc(codesetLen + 1)) == NULL)
		@throw [OFOutOfMemoryException
		    exceptionWithRequestedSize: codesetLen + 1];

	for (size_t i = 0; i < codesetLen; i++)
		lowerCodeset[i] = of_ascii_tolower(codeset[i]);

	if (strcmp(lowerCodeset, "utf8") == 0 ||
	    strcmp(lowerCodeset, "utf-8") == 0)
		of_system_info_native_8bit_encoding = OF_STRING_ENCODING_UTF_8;
	else if (strcmp(lowerCodeset, "ascii") == 0 ||
	    strcmp(lowerCodeset, "us-ascii") == 0)
		of_system_info_native_8bit_encoding = OF_STRING_ENCODING_ASCII;
	else if (strcmp(lowerCodeset, "iso8859-1") == 0 ||
	    strcmp(lowerCodeset, "iso-8859-1") == 0)
		of_system_info_native_8bit_encoding =
		    OF_STRING_ENCODING_ISO_8859_1;
	else if (strcmp(lowerCodeset, "iso8859-15") == 0 ||
	    strcmp(lowerCodeset, "iso-8859-15") == 0)
		of_system_info_native_8bit_encoding =
		    OF_STRING_ENCODING_ISO_8859_15;

	free(lowerCodeset);
#endif

	of_system_info_decimal_point = [[OFString alloc]
	    initWithCString: localeconv()->decimal_point
		   encoding: of_system_info_native_8bit_encoding];

	if ([cls isSubclassOfClass: [OFApplication class]]) {
		fprintf(stderr, "FATAL ERROR:\n  Class %s is a subclass of "
		    "class OFApplication, but class\n  %s was specified as "
		    "application delegate!\n  Most likely, you wanted to "
		    "subclass OFObject instead or specified\n  the wrong class "
		    "with OF_APPLICATION_DELEGATE().\n",







<
<
<
<

|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







104
105
106
107
108
109
110




111
112




































113
114
115
116
117
118
119
of_application_main(int *argc, char **argv[], Class cls)
{
	id <OFApplicationDelegate> delegate;
#ifdef OF_WINDOWS
	wchar_t **wargv, **wenvp;
	int wargc, si = 0;
#endif





	of_system_info_parse_locale(setlocale(LC_ALL, ""));





































	if ([cls isSubclassOfClass: [OFApplication class]]) {
		fprintf(stderr, "FATAL ERROR:\n  Class %s is a subclass of "
		    "class OFApplication, but class\n  %s was specified as "
		    "application delegate!\n  Most likely, you wanted to "
		    "subclass OFObject instead or specified\n  the wrong class "
		    "with OF_APPLICATION_DELEGATE().\n",

Modified src/OFSystemInfo.h from [0c8db08aa1] to [c797094656].

42
43
44
45
46
47
48


49
50
51
52
53


















54
55
56
57
58
59
60
+ (size_t)numberOfCPUs;

/*!
 * @brief Returns the native 8-bit string encoding of the operating system.
 *
 * This is useful to encode strings correctly for passing them to operating
 * system calls.


 *
 * @return The native 8-bit string encoding of the operating system
 */
+ (of_string_encoding_t)native8BitEncoding;



















/*!
 * @brief Returns the decimal point in the system's locale.
 *
 * @return The decimal point in the system's locale
 */
+ (OFString*)decimalPoint;








>
>





>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
+ (size_t)numberOfCPUs;

/*!
 * @brief Returns the native 8-bit string encoding of the operating system.
 *
 * This is useful to encode strings correctly for passing them to operating
 * system calls.
 *
 * If the native 8-bit encoding is unknown, UTF-8 is assumed.
 *
 * @return The native 8-bit string encoding of the operating system
 */
+ (of_string_encoding_t)native8BitEncoding;

/*!
 * @brief Returns the language of the locale.
 *
 * If the language is unknown, nil is returned.
 *
 * @return The language of the locale.
 */
+ (OFString*)language;

/*!
 * @brief Returns the territory of the locale.
 *
 * If the territory is unknown, nil is returned.
 *
 * @return The territory of the locale.
 */
+ (OFString*)territory;

/*!
 * @brief Returns the decimal point in the system's locale.
 *
 * @return The decimal point in the system's locale
 */
+ (OFString*)decimalPoint;

Modified src/OFSystemInfo.m from [13b78669de] to [2f5eb2340c].

20
21
22
23
24
25
26

27
28
29
30
31
32
33

/* Work around __block being used by glibc */
#include <stdlib.h>	/* include any libc header to get the libc defines */
#ifdef __GLIBC__
# undef __USE_XOPEN
#endif


#include <unistd.h>

#include "platform.h"

#ifdef OF_MAC_OS_X
# include <sys/sysctl.h>
#endif







>







20
21
22
23
24
25
26
27
28
29
30
31
32
33
34

/* Work around __block being used by glibc */
#include <stdlib.h>	/* include any libc header to get the libc defines */
#ifdef __GLIBC__
# undef __USE_XOPEN
#endif

#include <locale.h>
#include <unistd.h>

#include "platform.h"

#ifdef OF_MAC_OS_X
# include <sys/sysctl.h>
#endif
61
62
63
64
65
66
67




68





69
































70

71



















72
73
74
75
76
77
78
struct x86_regs {
	uint32_t eax, ebx, ecx, edx;
};
#endif

static size_t pageSize;
static size_t numberOfCPUs;










of_string_encoding_t of_system_info_native_8bit_encoding =
































    OF_STRING_ENCODING_UTF_8;

OFString *of_system_info_decimal_point = @".";




















#if defined(OF_X86_64) || defined(OF_X86)
static OF_INLINE struct x86_regs OF_CONST_FUNC
x86_cpuid(uint32_t eax, uint32_t ecx)
{
	struct x86_regs regs;








>
>
>
>

>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
struct x86_regs {
	uint32_t eax, ebx, ecx, edx;
};
#endif

static size_t pageSize;
static size_t numberOfCPUs;
static of_string_encoding_t native8BitEncoding = OF_STRING_ENCODING_UTF_8;
static OFString *language = nil;
static OFString *territory = nil;
static OFString *decimalPoint = @".";

void
of_system_info_parse_locale(char *locale)
{
	if (locale == NULL)
		return;

	locale = of_strdup(locale);

	@try {
		char *tmp;

		/* We don't care for extras behind the @ */
		if ((tmp = strrchr(locale, '@')) != NULL)
			*tmp = '\0';

		/* Encoding */
		if ((tmp = strrchr(locale, '.')) != NULL) {
			size_t tmpLen;

			*tmp++ = '\0';

			tmpLen = strlen(tmp);
			for (size_t i = 0; i < tmpLen; i++)
				tmp[i] = of_ascii_tolower(tmp[i]);

			if (strcmp(tmp, "utf8") == 0 ||
			    strcmp(tmp, "utf-8") == 0)
				native8BitEncoding = OF_STRING_ENCODING_UTF_8;
			else if (strcmp(tmp, "ascii") == 0 ||
			    strcmp(tmp, "us-ascii") == 0)
				native8BitEncoding = OF_STRING_ENCODING_ASCII;
			else if (strcmp(tmp, "iso8859-1") == 0 ||
			    strcmp(tmp, "iso-8859-1") == 0)
				native8BitEncoding =
				    OF_STRING_ENCODING_ISO_8859_1;
			else if (strcmp(tmp, "iso8859-15") == 0 ||
			    strcmp(tmp, "iso-8859-15") == 0)
				native8BitEncoding =
				    OF_STRING_ENCODING_ISO_8859_15;
		}

		/* Territory */
		if ((tmp = strrchr(locale, '_')) != NULL) {
			*tmp++ = '\0';
			territory = [[OFString alloc]
			    initWithCString: tmp
				   encoding: OF_STRING_ENCODING_ASCII];
		}

		language = [[OFString alloc]
		    initWithCString: locale
			   encoding: OF_STRING_ENCODING_ASCII];
	} @finally {
		free(locale);
	}

	decimalPoint = [[OFString alloc]
	    initWithCString: localeconv()->decimal_point
		   encoding: native8BitEncoding];
}

#if defined(OF_X86_64) || defined(OF_X86)
static OF_INLINE struct x86_regs OF_CONST_FUNC
x86_cpuid(uint32_t eax, uint32_t ecx)
{
	struct x86_regs regs;

145
146
147
148
149
150
151


152








153
154
155
156
157
158
159
160
161
162
163
164
+ (size_t)numberOfCPUs
{
	return numberOfCPUs;
}

+ (of_string_encoding_t)native8BitEncoding
{


	return of_system_info_native_8bit_encoding;








}

+ (OFString*)decimalPoint
{
	return of_system_info_decimal_point;
}

+ (OFString*)userDataPath
{
#if defined(OF_MAC_OS_X) || defined(OF_IOS)
	void *pool = objc_autoreleasePoolPush();
	char pathC[PATH_MAX];







>
>
|
>
>
>
>
>
>
>
>




|







207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
+ (size_t)numberOfCPUs
{
	return numberOfCPUs;
}

+ (of_string_encoding_t)native8BitEncoding
{
	return native8BitEncoding;
}

+ (OFString*)language
{
	return language;
}

+ (OFString*)territory
{
	return territory;
}

+ (OFString*)decimalPoint
{
	return decimalPoint;
}

+ (OFString*)userDataPath
{
#if defined(OF_MAC_OS_X) || defined(OF_IOS)
	void *pool = objc_autoreleasePoolPush();
	char pathC[PATH_MAX];