ObjFW  Diff

Differences From Artifact [70cccb5f86]:

To Artifact [f489c81282]:

  • File src/OFSystemInfo.m — part of check-in [06bcb21fc7] at 2017-01-09 06:26:04 on branch trunk — Add OFLocalization

    This singleton gives access to all things locale, including the ability
    to get localized strings.

    This also adds the OF_LOCALIZED() macro. Its first argument is an ID for
    the string to be localized and its second argument is the fallback
    string to be used if it cannot retrieve the localized string. Following
    that are variable name / value pairs to be replaced in the localized
    string.

    Getting translated strings is not implemented yet: Instead, it always
    uses the fallback string.

    This also switches ofhttp to localized strings. (user: js, size: 9770) [annotate] [blame] [check-ins using]


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







<







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
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
141
142
143
144
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;
			/* Windows uses a codepage */
			else if (strcmp(tmp, "1252") == 0)
				native8BitEncoding =
				    OF_STRING_ENCODING_WINDOWS_1252;
		}

		/* 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;








<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







61
62
63
64
65
66
67





































































68
69
70
71
72
73
74
struct x86_regs {
	uint32_t eax, ebx, ecx, edx;
};
#endif

static size_t pageSize;
static size_t numberOfCPUs;






































































#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;

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
237
238
239
240
241
242
}

+ (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];
	OFMutableString *path;
	OFString *home;







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







139
140
141
142
143
144
145




















146
147
148
149
150
151
152
}

+ (size_t)numberOfCPUs
{
	return numberOfCPUs;
}





















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