ReactOS 0.4.16-dev-2208-g6350669
xsltlocale.c File Reference
#include "libxslt.h"
#include <string.h>
#include <libxml/xmlmemory.h>
#include <libxml/threads.h>
#include "xsltlocale.h"
#include "xsltutils.h"
Include dependency graph for xsltlocale.c:

Go to the source code of this file.

Macros

#define IN_LIBXSLT
 
#define XSLT_LOCALE_NONE
 
#define TOUPPER(c)   (c & ~0x20)
 
#define TOLOWER(c)   (c | 0x20)
 
#define ISALPHA(c)   ((unsigned)(TOUPPER(c) - 'A') < 26)
 
#define XSLTMAX_ISO639LANGLEN   8
 
#define XSLTMAX_ISO3166CNTRYLEN   8
 
#define XSLTMAX_LANGTAGLEN   (XSLTMAX_ISO639LANGLEN+1+XSLTMAX_ISO3166CNTRYLEN)
 

Functions

static const xmlCharxsltDefaultRegion (const xmlChar *localeName)
 
void xsltFreeLocales (void)
 
voidxsltNewLocale (const xmlChar *languageTag, int lowerFirst ATTRIBUTE_UNUSED)
 
void xsltFreeLocale (void *locale)
 
xmlCharxsltStrxfrm (void *vlocale, const xmlChar *string)
 
int xsltLocaleStrcmp (void *locale, const xmlChar *str1, const xmlChar *str2)
 

Macro Definition Documentation

◆ IN_LIBXSLT

#define IN_LIBXSLT

Definition at line 13 of file xsltlocale.c.

◆ ISALPHA

#define ISALPHA (   c)    ((unsigned)(TOUPPER(c) - 'A') < 26)

Definition at line 49 of file xsltlocale.c.

◆ TOLOWER

#define TOLOWER (   c)    (c | 0x20)

Definition at line 48 of file xsltlocale.c.

◆ TOUPPER

#define TOUPPER (   c)    (c & ~0x20)

Definition at line 47 of file xsltlocale.c.

◆ XSLT_LOCALE_NONE

#define XSLT_LOCALE_NONE

Definition at line 43 of file xsltlocale.c.

◆ XSLTMAX_ISO3166CNTRYLEN

#define XSLTMAX_ISO3166CNTRYLEN   8

Definition at line 53 of file xsltlocale.c.

◆ XSLTMAX_ISO639LANGLEN

#define XSLTMAX_ISO639LANGLEN   8

Definition at line 52 of file xsltlocale.c.

◆ XSLTMAX_LANGTAGLEN

Definition at line 55 of file xsltlocale.c.

Function Documentation

◆ xsltDefaultRegion()

static const xmlChar * xsltDefaultRegion ( const xmlChar localeName)
static

Definition at line 228 of file xsltlocale.c.

228 {
229 xmlChar c;
230 /* region should be xmlChar, but gcc warns on all string assignments */
231 const char *region = NULL;
232
233 c = localeName[1];
234 /* This is based on the locales from glibc 2.3.3 */
235
236 switch (localeName[0]) {
237 case 'a':
238 if (c == 'a' || c == 'm') region = "ET";
239 else if (c == 'f') region = "ZA";
240 else if (c == 'n') region = "ES";
241 else if (c == 'r') region = "AE";
242 else if (c == 'z') region = "AZ";
243 break;
244 case 'b':
245 if (c == 'e') region = "BY";
246 else if (c == 'g') region = "BG";
247 else if (c == 'n') region = "BD";
248 else if (c == 'r') region = "FR";
249 else if (c == 's') region = "BA";
250 break;
251 case 'c':
252 if (c == 'a') region = "ES";
253 else if (c == 's') region = "CZ";
254 else if (c == 'y') region = "GB";
255 break;
256 case 'd':
257 if (c == 'a') region = "DK";
258 else if (c == 'e') region = "DE";
259 break;
260 case 'e':
261 if (c == 'l') region = "GR";
262 else if (c == 'n' || c == 'o') region = "US";
263 else if (c == 's' || c == 'u') region = "ES";
264 else if (c == 't') region = "EE";
265 break;
266 case 'f':
267 if (c == 'a') region = "IR";
268 else if (c == 'i') region = "FI";
269 else if (c == 'o') region = "FO";
270 else if (c == 'r') region = "FR";
271 break;
272 case 'g':
273 if (c == 'a') region = "IE";
274 else if (c == 'l') region = "ES";
275 else if (c == 'v') region = "GB";
276 break;
277 case 'h':
278 if (c == 'e') region = "IL";
279 else if (c == 'i') region = "IN";
280 else if (c == 'r') region = "HT";
281 else if (c == 'u') region = "HU";
282 break;
283 case 'i':
284 if (c == 'd') region = "ID";
285 else if (c == 's') region = "IS";
286 else if (c == 't') region = "IT";
287 else if (c == 'w') region = "IL";
288 break;
289 case 'j':
290 if (c == 'a') region = "JP";
291 break;
292 case 'k':
293 if (c == 'l') region = "GL";
294 else if (c == 'o') region = "KR";
295 else if (c == 'w') region = "GB";
296 break;
297 case 'l':
298 if (c == 't') region = "LT";
299 else if (c == 'v') region = "LV";
300 break;
301 case 'm':
302 if (c == 'k') region = "MK";
303 else if (c == 'l' || c == 'r') region = "IN";
304 else if (c == 'n') region = "MN";
305 else if (c == 's') region = "MY";
306 else if (c == 't') region = "MT";
307 break;
308 case 'n':
309 if (c == 'b' || c == 'n' || c == 'o') region = "NO";
310 else if (c == 'e') region = "NP";
311 else if (c == 'l') region = "NL";
312 break;
313 case 'o':
314 if (c == 'm') region = "ET";
315 break;
316 case 'p':
317 if (c == 'a') region = "IN";
318 else if (c == 'l') region = "PL";
319 else if (c == 't') region = "PT";
320 break;
321 case 'r':
322 if (c == 'o') region = "RO";
323 else if (c == 'u') region = "RU";
324 break;
325 case 's':
326 switch (c) {
327 case 'e': region = "NO"; break;
328 case 'h': region = "YU"; break;
329 case 'k': region = "SK"; break;
330 case 'l': region = "SI"; break;
331 case 'o': region = "ET"; break;
332 case 'q': region = "AL"; break;
333 case 't': region = "ZA"; break;
334 case 'v': region = "SE"; break;
335 }
336 break;
337 case 't':
338 if (c == 'a' || c == 'e') region = "IN";
339 else if (c == 'h') region = "TH";
340 else if (c == 'i') region = "ER";
341 else if (c == 'r') region = "TR";
342 else if (c == 't') region = "RU";
343 break;
344 case 'u':
345 if (c == 'k') region = "UA";
346 else if (c == 'r') region = "PK";
347 break;
348 case 'v':
349 if (c == 'i') region = "VN";
350 break;
351 case 'w':
352 if (c == 'a') region = "BE";
353 break;
354 case 'x':
355 if (c == 'h') region = "ZA";
356 break;
357 case 'z':
358 if (c == 'h') region = "CN";
359 else if (c == 'u') region = "ZA";
360 break;
361 }
362 return((xmlChar *)region);
363}
#define NULL
Definition: types.h:112
const GLubyte * c
Definition: glext.h:8905
#define c
Definition: ke_i.h:80
unsigned char xmlChar
Definition: xmlstring.h:28

Referenced by xsltNewLocale().

◆ xsltFreeLocale()

void xsltFreeLocale ( void locale)

xsltFreeLocale: @locale: the locale to free

Frees a locale created with xsltNewLocale

Definition at line 372 of file xsltlocale.c.

372 {
373#ifdef XSLT_LOCALE_POSIX
374 if (locale != NULL)
375 freelocale(locale);
376#else
377 (void) locale;
378#endif
379}
Definition: _locale.h:75

Referenced by xsltNewTransformContext().

◆ xsltFreeLocales()

void xsltFreeLocales ( void  )

xsltFreeLocales:

Cleanup function for the locale support on shutdown

Definition at line 93 of file xsltlocale.c.

93 {
94#ifdef XSLT_LOCALE_WINAPI
95 xmlRMutexLock(xsltLocaleMutex);
96 xmlFree(xsltLocaleList);
97 xsltLocaleList = NULL;
98 xmlRMutexUnlock(xsltLocaleMutex);
99#endif
100}
XMLPUBFUN void xmlRMutexLock(xmlRMutexPtr tok)
Definition: threads.c:297
XMLPUBFUN void xmlRMutexUnlock(xmlRMutexPtr tok)
Definition: threads.c:333
xmlFreeFunc xmlFree
Definition: globals.c:184

Referenced by xsltCleanupGlobals().

◆ xsltLocaleStrcmp()

int xsltLocaleStrcmp ( void locale,
const xmlChar str1,
const xmlChar str2 
)

xsltLocaleStrcmp: @locale: unused @str1: a string transformed with xsltStrxfrm @str2: a string transformed with xsltStrxfrm

DEPRECATED: Same as xmlStrcmp.

Compares two strings transformed with xsltStrxfrm.

Returns a value < 0 if str1 sorts before str2, a value > 0 if str1 sorts after str2, 0 if str1 and str2 are equal wrt sorting

Definition at line 482 of file xsltlocale.c.

482 {
483 (void)locale;
484 return(xmlStrcmp(str1, str2));
485}
XML_HIDDEN void xmlParserErrors const char const xmlChar const xmlChar * str2
Definition: parser.h:35
XML_HIDDEN void xmlParserErrors const char const xmlChar * str1
Definition: parser.h:35
XMLPUBFUN int xmlStrcmp(const xmlChar *str1, const xmlChar *str2)
Definition: xmlstring.c:135

◆ xsltNewLocale()

void * xsltNewLocale ( const xmlChar languageTag,
int lowerFirst  ATTRIBUTE_UNUSED 
)

xsltNewLocale: @languageTag: RFC 3066 language tag

Creates a new locale of an opaque system dependent type based on the language tag.

Returns the locale or NULL on error or if no matching locale was found

Definition at line 112 of file xsltlocale.c.

112 {
113#ifdef XSLT_LOCALE_POSIX
114 locale_t locale;
115 char localeName[XSLTMAX_LANGTAGLEN+7]; /* 7 chars for ".UTF-8\0" */
116 const xmlChar *p = languageTag;
117 const char *region = NULL;
118 char *q = localeName;
119 int i, llen;
120
121 /* Convert something like "pt-br" to "pt_BR.UTF-8" */
122
123 if (languageTag == NULL)
124 return(NULL);
125
126 for (i=0; i<XSLTMAX_ISO639LANGLEN && ISALPHA(*p); ++i)
127 *q++ = TOLOWER(*p++);
128
129 if (i == 0)
130 return(NULL);
131
132 llen = i;
133
134 if (*p) {
135 if (*p++ != '-')
136 return(NULL);
137 *q++ = '_';
138
139 for (i=0; i<XSLTMAX_ISO3166CNTRYLEN && ISALPHA(*p); ++i)
140 *q++ = TOUPPER(*p++);
141
142 if (i == 0 || *p)
143 return(NULL);
144
145 memcpy(q, ".UTF-8", 7);
146 locale = newlocale(LC_ALL_MASK, localeName, NULL);
147 if (locale != NULL)
148 return(locale);
149
150 /* Continue without using country code */
151
152 q = localeName + llen;
153 }
154
155 /* Try locale without territory, e.g. for Esperanto (eo) */
156
157 memcpy(q, ".UTF-8", 7);
158 locale = newlocale(LC_ALL_MASK, localeName, NULL);
159 if (locale != NULL)
160 return(locale);
161
162 /* Try to find most common country for language */
163
164 if (llen != 2)
165 return(NULL);
166
167 region = (char *)xsltDefaultRegion((xmlChar *)localeName);
168 if (region == NULL)
169 return(NULL);
170
171 q = localeName + llen;
172 *q++ = '_';
173 *q++ = region[0];
174 *q++ = region[1];
175 memcpy(q, ".UTF-8", 7);
176 locale = newlocale(LC_ALL_MASK, localeName, NULL);
177
178 return(locale);
179#endif
180
181#ifdef XSLT_LOCALE_WINAPI
182{
183 void *locale = NULL;
184 xmlChar localeName[XSLTMAX_LANGTAGLEN+1];
185 xmlChar *q = localeName;
186 const xmlChar *p = languageTag;
187 int i, llen;
188 const xmlChar *region = NULL;
189
190 if (languageTag == NULL) goto end;
191
192 xsltEnumSupportedLocales();
193
194 for (i=0; i<XSLTMAX_ISO639LANGLEN && ISALPHA(*p); ++i)
195 *q++ = TOLOWER(*p++);
196 if (i == 0) goto end;
197
198 llen = i;
199 *q++ = '-';
200 if (*p) { /*if country tag is given*/
201 if (*p++ != '-') goto end;
202
203 for (i=0; i<XSLTMAX_ISO3166CNTRYLEN && ISALPHA(*p); ++i)
204 *q++ = TOUPPER(*p++);
205 if (i == 0 || *p) goto end;
206
207 *q = '\0';
208 locale = xslt_locale_WINAPI(localeName);
209 if (locale != (xsltLocale)0) goto end;
210 }
211 /* Try to find most common country for language */
212 region = xsltDefaultRegion(localeName);
213 if (region == NULL) goto end;
214
215 strcpy((char *) localeName + llen + 1, (char *) region);
216 locale = xslt_locale_WINAPI(localeName);
217end:
218 return(locale);
219}
220#endif
221
222#ifdef XSLT_LOCALE_NONE
223 return(NULL);
224#endif
225}
GLuint GLuint end
Definition: gl.h:1545
GLdouble GLdouble GLdouble GLdouble q
Definition: gl.h:2063
GLfloat GLfloat p
Definition: glext.h:8902
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
strcpy
Definition: string.h:131
static const xmlChar * xsltDefaultRegion(const xmlChar *localeName)
Definition: xsltlocale.c:228
#define XSLTMAX_LANGTAGLEN
Definition: xsltlocale.c:55
#define XSLTMAX_ISO3166CNTRYLEN
Definition: xsltlocale.c:53
#define XSLTMAX_ISO639LANGLEN
Definition: xsltlocale.c:52
#define TOUPPER(c)
Definition: xsltlocale.c:47
#define TOLOWER(c)
Definition: xsltlocale.c:48
#define ISALPHA(c)
Definition: xsltlocale.c:49

Referenced by xsltNewTransformContext().

◆ xsltStrxfrm()

xmlChar * xsltStrxfrm ( void vlocale,
const xmlChar string 
)

xsltStrxfrm: @locale: locale created with xsltNewLocale @string: UTF-8 string to transform

Transforms a string according to locale. The transformed string must be freed with xmlFree.

Returns the transformed string or NULL on error

Definition at line 392 of file xsltlocale.c.

393{
394#ifdef XSLT_LOCALE_NONE
395 return(NULL);
396#else
397 xmlChar *xstr;
398
399#ifdef XSLT_LOCALE_POSIX
400 size_t xstrlen, r;
401
402 xstrlen = strxfrm_l(NULL, (const char *)string, 0, vlocale) + 1;
403 xstr = (xmlChar *) xmlMalloc(xstrlen);
404 if (xstr == NULL) {
406 "xsltStrxfrm : out of memory error\n");
407 return(NULL);
408 }
409
410 r = strxfrm_l((char *)xstr, (const char *)string, xstrlen, vlocale);
411
412 if (r >= xstrlen) {
413 xsltTransformError(NULL, NULL, NULL, "xsltStrxfrm : strxfrm failed\n");
414 xmlFree(xstr);
415 return(NULL);
416 }
417#endif
418
419#ifdef XSLT_LOCALE_WINAPI
420 int wstrlen, xstrlen, r;
421 wchar_t *wstr;
422 LCID *lcid = vlocale;
423
424 wstrlen = MultiByteToWideChar(CP_UTF8, 0, (char *) string, -1, NULL, 0);
425 if (wstrlen == 0) {
426 xsltTransformError(NULL, NULL, NULL, "xsltStrxfrm : MultiByteToWideChar check failed\n");
427 return(NULL);
428 }
429 wstr = (wchar_t *) xmlMalloc(wstrlen * sizeof(wchar_t));
430 if (wstr == NULL) {
431 xsltTransformError(NULL, NULL, NULL, "xsltStrxfrm : out of memory\n");
432 return(NULL);
433 }
434 r = MultiByteToWideChar(CP_UTF8, 0, (char *) string, -1, wstr, wstrlen);
435 if (r == 0) {
436 xsltTransformError(NULL, NULL, NULL, "xsltStrxfrm : MultiByteToWideChar failed\n");
437 xmlFree(wstr);
438 return(NULL);
439 }
440 /* This returns the size in bytes. */
441 xstrlen = LCMapStringW(*lcid, LCMAP_SORTKEY, wstr, wstrlen, NULL, 0);
442 if (xstrlen == 0) {
443 xsltTransformError(NULL, NULL, NULL, "xsltStrxfrm : LCMapStringW failed\n");
444 xmlFree(wstr);
445 return(NULL);
446 }
447 xstr = (xmlChar*) xmlMalloc(xstrlen);
448 if (xstr == NULL) {
449 xsltTransformError(NULL, NULL, NULL, "xsltStrxfrm : out of memory\n");
450 xmlFree(wstr);
451 return(NULL);
452 }
453 r = LCMapStringW(*lcid, LCMAP_SORTKEY, wstr, wstrlen, (wchar_t *) xstr,
454 xstrlen);
455 xmlFree(wstr);
456 if (r == 0) {
457 xsltTransformError(NULL, NULL, NULL, "xsltStrxfrm : LCMapStringW failed\n");
458 xmlFree(xstr);
459 return(NULL);
460 }
461#endif /* XSLT_LOCALE_WINAPI */
462
463 return(xstr);
464#endif /* XSLT_LOCALE_NONE */
465}
#define MultiByteToWideChar
Definition: compat.h:110
INT WINAPI LCMapStringW(LCID lcid, DWORD flags, LPCWSTR src, INT srclen, LPWSTR dst, INT dstlen)
Definition: locale.c:3808
LCID lcid
Definition: locale.c:5656
GLdouble GLdouble GLdouble r
Definition: gl.h:2055
DWORD LCID
Definition: nls.h:13
#define CP_UTF8
Definition: nls.h:20
xmlMallocFunc xmlMalloc
Definition: globals.c:193
#define LCMAP_SORTKEY
Definition: winnls.h:199
void xsltTransformError(xsltTransformContextPtr ctxt, xsltStylesheetPtr style, xmlNodePtr node, const char *msg,...)
Definition: xsltutils.c:762

Referenced by xsltNewTransformContext().