blob: 6d50cfbd9a37054dca01c2fd969b292120c10768 [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
6% L OOO CCCC AAA L EEEEE %
7% L O O C A A L E %
8% L O O C AAAAA L EEE %
9% L O O C A A L E %
10% LLLLL OOO CCCC A A LLLLL EEEEE %
11% %
12% %
13% MagickCore Image Locale Methods %
14% %
15% Software Design %
16% John Cristy %
17% July 2003 %
18% %
19% %
cristy45ef08f2012-12-07 13:13:34 +000020% Copyright 1999-2013 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% dedicated to making software imaging solutions freely available. %
22% %
23% You may not use this file except in compliance with the License. You may %
24% obtain a copy of the License at %
25% %
26% http://www.imagemagick.org/script/license.php %
27% %
28% Unless required by applicable law or agreed to in writing, software %
29% distributed under the License is distributed on an "AS IS" BASIS, %
30% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31% See the License for the specific language governing permissions and %
32% limitations under the License. %
33% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37*/
38
39/*
40 Include declarations.
41*/
cristy4c08aed2011-07-01 19:47:50 +000042#include "MagickCore/studio.h"
43#include "MagickCore/blob.h"
44#include "MagickCore/client.h"
45#include "MagickCore/configure.h"
46#include "MagickCore/exception.h"
47#include "MagickCore/exception-private.h"
48#include "MagickCore/hashmap.h"
49#include "MagickCore/locale_.h"
cristy5ff4eaf2011-09-03 01:38:02 +000050#include "MagickCore/locale-private.h"
cristy4c08aed2011-07-01 19:47:50 +000051#include "MagickCore/log.h"
52#include "MagickCore/memory_.h"
cristyd2d11ec2012-03-28 13:53:49 +000053#include "MagickCore/nt-base-private.h"
cristy4c08aed2011-07-01 19:47:50 +000054#include "MagickCore/semaphore.h"
55#include "MagickCore/splay-tree.h"
56#include "MagickCore/string_.h"
cristy7832dc22011-09-05 01:21:53 +000057#include "MagickCore/string-private.h"
cristy4c08aed2011-07-01 19:47:50 +000058#include "MagickCore/token.h"
59#include "MagickCore/utility.h"
cristyd1dd6e42011-09-04 01:46:08 +000060#include "MagickCore/utility-private.h"
cristy4c08aed2011-07-01 19:47:50 +000061#include "MagickCore/xml-tree.h"
cristy3ed852e2009-09-05 21:47:34 +000062
63/*
64 Define declarations.
65*/
66#define LocaleFilename "locale.xml"
67#define MaxRecursionDepth 200
68
69/*
cristycaa2ce52011-05-20 23:59:13 +000070 Typedef declarations.
71*/
72#if defined(__CYGWIN__)
73typedef struct _locale_t *locale_t;
74#endif
75
76/*
cristy3ed852e2009-09-05 21:47:34 +000077 Static declarations.
78*/
79static const char
80 *LocaleMap =
81 "<?xml version=\"1.0\"?>"
82 "<localemap>"
83 " <locale name=\"C\">"
84 " <Exception>"
85 " <Message name=\"\">"
86 " </Message>"
87 " </Exception>"
88 " </locale>"
89 "</localemap>";
90
91static SemaphoreInfo
92 *locale_semaphore = (SemaphoreInfo *) NULL;
93
94static SplayTreeInfo
95 *locale_list = (SplayTreeInfo *) NULL;
96
cristya723b6a2011-05-25 12:08:51 +000097#if defined(MAGICKCORE_HAVE_STRTOD_L)
cristya20c9042011-05-19 13:22:18 +000098static volatile locale_t
cristyb51dff52011-05-19 16:55:47 +000099 c_locale = (locale_t) NULL;
cristya723b6a2011-05-25 12:08:51 +0000100#endif
cristy022ce732011-05-19 23:34:51 +0000101
102static volatile MagickBooleanType
103 instantiate_locale = MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +0000104
105/*
106 Forward declarations.
107*/
108static MagickBooleanType
109 InitializeLocaleList(ExceptionInfo *),
110 LoadLocaleLists(const char *,const char *,ExceptionInfo *);
111
cristya723b6a2011-05-25 12:08:51 +0000112#if defined(MAGICKCORE_HAVE_STRTOD_L)
cristy3ed852e2009-09-05 21:47:34 +0000113/*
114%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
115% %
116% %
117% %
cristyb51dff52011-05-19 16:55:47 +0000118+ A c q u i r e C L o c a l e %
cristya20c9042011-05-19 13:22:18 +0000119% %
120% %
121% %
122%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
123%
cristyb51dff52011-05-19 16:55:47 +0000124% AcquireCLocale() allocates the C locale object, or (locale_t) 0 with
cristya20c9042011-05-19 13:22:18 +0000125% errno set if it cannot be acquired.
126%
cristyb51dff52011-05-19 16:55:47 +0000127% The format of the AcquireCLocale method is:
cristya20c9042011-05-19 13:22:18 +0000128%
cristyb51dff52011-05-19 16:55:47 +0000129% locale_t AcquireCLocale(void)
cristya20c9042011-05-19 13:22:18 +0000130%
131*/
cristyb51dff52011-05-19 16:55:47 +0000132static locale_t AcquireCLocale(void)
cristya20c9042011-05-19 13:22:18 +0000133{
134#if defined(MAGICKCORE_HAVE_NEWLOCALE)
cristyb51dff52011-05-19 16:55:47 +0000135 if (c_locale == (locale_t) NULL)
136 c_locale=newlocale(LC_ALL_MASK,"C",(locale_t) 0);
cristya20c9042011-05-19 13:22:18 +0000137#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
cristyb51dff52011-05-19 16:55:47 +0000138 if (c_locale == (locale_t) NULL)
139 c_locale=_create_locale(LC_ALL,"C");
cristya20c9042011-05-19 13:22:18 +0000140#endif
cristyb51dff52011-05-19 16:55:47 +0000141 return(c_locale);
142}
cristya723b6a2011-05-25 12:08:51 +0000143#endif
cristyb51dff52011-05-19 16:55:47 +0000144
cristya723b6a2011-05-25 12:08:51 +0000145#if defined(MAGICKCORE_HAVE_STRTOD_L)
cristyb51dff52011-05-19 16:55:47 +0000146/*
147%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
148% %
149% %
150% %
151+ D e s t r o y C L o c a l e %
152% %
153% %
154% %
155%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
156%
157% DestroyCLocale() releases the resources allocated for a locale object
158% returned by a call to the AcquireCLocale() method.
159%
160% The format of the DestroyCLocale method is:
161%
162% void DestroyCLocale(void)
163%
164*/
165static void DestroyCLocale(void)
166{
167#if defined(MAGICKCORE_HAVE_NEWLOCALE)
168 if (c_locale != (locale_t) NULL)
169 freelocale(c_locale);
170#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
171 if (c_locale != (locale_t) NULL)
172 _free_locale(c_locale);
173#endif
174 c_locale=(locale_t) NULL;
cristya20c9042011-05-19 13:22:18 +0000175}
cristya723b6a2011-05-25 12:08:51 +0000176#endif
cristya20c9042011-05-19 13:22:18 +0000177
178/*
179%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
180% %
181% %
182% %
cristy3ed852e2009-09-05 21:47:34 +0000183% D e s t r o y L o c a l e O p t i o n s %
184% %
185% %
186% %
187%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
188%
189% DestroyLocaleOptions() releases memory associated with an locale
190% messages.
191%
192% The format of the DestroyProfiles method is:
193%
194% LinkedListInfo *DestroyLocaleOptions(Image *image)
195%
196% A description of each parameter follows:
197%
198% o image: the image.
199%
200*/
201
202static void *DestroyOptions(void *message)
203{
204 return(DestroyStringInfo((StringInfo *) message));
205}
206
207MagickExport LinkedListInfo *DestroyLocaleOptions(LinkedListInfo *messages)
208{
209 assert(messages != (LinkedListInfo *) NULL);
210 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
211 return(DestroyLinkedList(messages,DestroyOptions));
212}
213
214/*
215%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
216% %
217% %
218% %
cristyb51dff52011-05-19 16:55:47 +0000219+ F o r m a t L o c a l e F i l e %
cristya20c9042011-05-19 13:22:18 +0000220% %
221% %
222% %
223%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
224%
cristyb51dff52011-05-19 16:55:47 +0000225% FormatLocaleFile() prints formatted output of a variable argument list to a
226% file in the "C" locale.
cristya20c9042011-05-19 13:22:18 +0000227%
cristyb51dff52011-05-19 16:55:47 +0000228% The format of the FormatLocaleFile method is:
cristya20c9042011-05-19 13:22:18 +0000229%
cristyb51dff52011-05-19 16:55:47 +0000230% ssize_t FormatLocaleFile(FILE *file,const char *format,...)
231%
232% A description of each parameter follows.
233%
234% o file: the file.
235%
236% o format: A file describing the format to use to write the remaining
237% arguments.
cristya20c9042011-05-19 13:22:18 +0000238%
239*/
cristyb51dff52011-05-19 16:55:47 +0000240
cristy7832dc22011-09-05 01:21:53 +0000241MagickPrivate ssize_t FormatLocaleFileList(FILE *file,
cristyba56db52011-05-20 02:08:11 +0000242 const char *restrict format,va_list operands)
cristya20c9042011-05-19 13:22:18 +0000243{
cristy20ec7592011-05-29 01:28:05 +0000244 ssize_t
cristyb51dff52011-05-19 16:55:47 +0000245 n;
246
cristy5d0cd952011-05-19 21:21:41 +0000247#if defined(MAGICKCORE_HAVE_VFPRINTF_L)
cristyb51dff52011-05-19 16:55:47 +0000248 {
249 locale_t
250 locale;
251
252 locale=AcquireCLocale();
253 if (locale == (locale_t) NULL)
cristy20ec7592011-05-29 01:28:05 +0000254 n=(ssize_t) vfprintf(file,format,operands);
cristyb51dff52011-05-19 16:55:47 +0000255 else
cristy26d78202011-05-21 00:59:34 +0000256#if defined(MAGICKCORE_WINDOWS_SUPPORT)
cristy20ec7592011-05-29 01:28:05 +0000257 n=(ssize_t) vfprintf_l(file,format,locale,operands);
cristy26d78202011-05-21 00:59:34 +0000258#else
cristy20ec7592011-05-29 01:28:05 +0000259 n=(ssize_t) vfprintf_l(file,locale,format,operands);
cristy405905f2011-05-21 00:37:33 +0000260#endif
cristyb51dff52011-05-19 16:55:47 +0000261 }
262#else
263#if defined(MAGICKCORE_HAVE_USELOCALE)
264 {
265 locale_t
266 locale,
267 previous_locale;
268
269 locale=AcquireCLocale();
270 if (locale == (locale_t) NULL)
cristy20ec7592011-05-29 01:28:05 +0000271 n=(ssize_t) vfprintf(file,format,operands);
cristyb51dff52011-05-19 16:55:47 +0000272 else
273 {
274 previous_locale=uselocale(locale);
cristy20ec7592011-05-29 01:28:05 +0000275 n=(ssize_t) vfprintf(file,format,operands);
cristyb51dff52011-05-19 16:55:47 +0000276 uselocale(previous_locale);
277 }
278 }
279#else
cristy20ec7592011-05-29 01:28:05 +0000280 n=(ssize_t) vfprintf(file,format,operands);
cristya20c9042011-05-19 13:22:18 +0000281#endif
cristyb51dff52011-05-19 16:55:47 +0000282#endif
cristy3da0b402011-05-29 21:13:36 +0000283 return(n);
cristyb51dff52011-05-19 16:55:47 +0000284}
285
cristyba56db52011-05-20 02:08:11 +0000286MagickExport ssize_t FormatLocaleFile(FILE *file,const char *restrict format,
287 ...)
cristyb51dff52011-05-19 16:55:47 +0000288{
289 ssize_t
290 n;
291
292 va_list
293 operands;
294
295 va_start(operands,format);
cristy20ec7592011-05-29 01:28:05 +0000296 n=FormatLocaleFileList(file,format,operands);
cristyb51dff52011-05-19 16:55:47 +0000297 va_end(operands);
298 return(n);
299}
300
301/*
302%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
303% %
304% %
305% %
306+ F o r m a t L o c a l e S t r i n g %
307% %
308% %
309% %
310%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
311%
312% FormatLocaleString() prints formatted output of a variable argument list to
313% a string buffer in the "C" locale.
314%
315% The format of the FormatLocaleString method is:
316%
317% ssize_t FormatLocaleString(char *string,const size_t length,
318% const char *format,...)
319%
320% A description of each parameter follows.
321%
322% o string: FormatLocaleString() returns the formatted string in this
323% character buffer.
324%
325% o length: the maximum length of the string.
326%
327% o format: A string describing the format to use to write the remaining
328% arguments.
329%
330*/
331
cristy7832dc22011-09-05 01:21:53 +0000332MagickPrivate ssize_t FormatLocaleStringList(char *restrict string,
cristyba56db52011-05-20 02:08:11 +0000333 const size_t length,const char *restrict format,va_list operands)
cristyb51dff52011-05-19 16:55:47 +0000334{
cristy20ec7592011-05-29 01:28:05 +0000335 ssize_t
cristyb51dff52011-05-19 16:55:47 +0000336 n;
337
338#if defined(MAGICKCORE_HAVE_VSNPRINTF_L)
cristy405905f2011-05-21 00:37:33 +0000339 {
340 locale_t
341 locale;
342
343 locale=AcquireCLocale();
344 if (locale == (locale_t) NULL)
cristy20ec7592011-05-29 01:28:05 +0000345 n=(ssize_t) vsnprintf(string,length,format,operands);
cristy405905f2011-05-21 00:37:33 +0000346 else
cristy26d78202011-05-21 00:59:34 +0000347#if defined(MAGICKCORE_WINDOWS_SUPPORT)
cristy20ec7592011-05-29 01:28:05 +0000348 n=(ssize_t) vsnprintf_l(string,length,format,locale,operands);
cristy26d78202011-05-21 00:59:34 +0000349#else
cristy20ec7592011-05-29 01:28:05 +0000350 n=(ssize_t) vsnprintf_l(string,length,locale,format,operands);
cristy405905f2011-05-21 00:37:33 +0000351#endif
352 }
cristyb51dff52011-05-19 16:55:47 +0000353#elif defined(MAGICKCORE_HAVE_VSNPRINTF)
354#if defined(MAGICKCORE_HAVE_USELOCALE)
355 {
356 locale_t
357 locale,
358 previous_locale;
359
360 locale=AcquireCLocale();
361 if (locale == (locale_t) NULL)
cristy20ec7592011-05-29 01:28:05 +0000362 n=(ssize_t) vsnprintf(string,length,format,operands);
cristyb51dff52011-05-19 16:55:47 +0000363 else
364 {
365 previous_locale=uselocale(locale);
cristy20ec7592011-05-29 01:28:05 +0000366 n=(ssize_t) vsnprintf(string,length,format,operands);
cristyb51dff52011-05-19 16:55:47 +0000367 uselocale(previous_locale);
368 }
369 }
370#else
cristy20ec7592011-05-29 01:28:05 +0000371 n=(ssize_t) vsnprintf(string,length,format,operands);
cristyb51dff52011-05-19 16:55:47 +0000372#endif
373#else
cristy20ec7592011-05-29 01:28:05 +0000374 n=(ssize_t) vsprintf(string,format,operands);
cristyb51dff52011-05-19 16:55:47 +0000375#endif
376 if (n < 0)
377 string[length-1]='\0';
cristy20ec7592011-05-29 01:28:05 +0000378 return(n);
cristyb51dff52011-05-19 16:55:47 +0000379}
380
cristyba56db52011-05-20 02:08:11 +0000381MagickExport ssize_t FormatLocaleString(char *restrict string,
382 const size_t length,const char *restrict format,...)
cristyb51dff52011-05-19 16:55:47 +0000383{
384 ssize_t
385 n;
386
387 va_list
388 operands;
389
390 va_start(operands,format);
cristy20ec7592011-05-29 01:28:05 +0000391 n=FormatLocaleStringList(string,length,format,operands);
cristyb51dff52011-05-19 16:55:47 +0000392 va_end(operands);
393 return(n);
cristya20c9042011-05-19 13:22:18 +0000394}
395
396/*
397%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
398% %
399% %
400% %
cristy3ed852e2009-09-05 21:47:34 +0000401+ G e t L o c a l e I n f o _ %
402% %
403% %
404% %
405%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
406%
407% GetLocaleInfo_() searches the locale list for the specified tag and if
408% found returns attributes for that element.
409%
410% The format of the GetLocaleInfo method is:
411%
412% const LocaleInfo *GetLocaleInfo_(const char *tag,
413% ExceptionInfo *exception)
414%
415% A description of each parameter follows:
416%
417% o tag: the locale tag.
418%
419% o exception: return any errors or warnings in this structure.
420%
421*/
422MagickExport const LocaleInfo *GetLocaleInfo_(const char *tag,
423 ExceptionInfo *exception)
424{
425 assert(exception != (ExceptionInfo *) NULL);
426 if ((locale_list == (SplayTreeInfo *) NULL) ||
427 (instantiate_locale == MagickFalse))
428 if (InitializeLocaleList(exception) == MagickFalse)
429 return((const LocaleInfo *) NULL);
430 if ((locale_list == (SplayTreeInfo *) NULL) ||
431 (GetNumberOfNodesInSplayTree(locale_list) == 0))
432 return((const LocaleInfo *) NULL);
433 if ((tag == (const char *) NULL) || (LocaleCompare(tag,"*") == 0))
434 {
435 ResetSplayTreeIterator(locale_list);
436 return((const LocaleInfo *) GetNextValueInSplayTree(locale_list));
437 }
438 return((const LocaleInfo *) GetValueFromSplayTree(locale_list,tag));
439}
440
441/*
442%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
443% %
444% %
445% %
446% G e t L o c a l e I n f o L i s t %
447% %
448% %
449% %
450%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
451%
452% GetLocaleInfoList() returns any locale messages that match the
453% specified pattern.
454%
455% The format of the GetLocaleInfoList function is:
456%
457% const LocaleInfo **GetLocaleInfoList(const char *pattern,
cristybb503372010-05-27 20:51:26 +0000458% size_t *number_messages,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000459%
460% A description of each parameter follows:
461%
462% o pattern: Specifies a pointer to a text string containing a pattern.
463%
464% o number_messages: This integer returns the number of locale messages in
465% the list.
466%
467% o exception: return any errors or warnings in this structure.
468%
469*/
470
471#if defined(__cplusplus) || defined(c_plusplus)
472extern "C" {
473#endif
474
475static int LocaleInfoCompare(const void *x,const void *y)
476{
477 const LocaleInfo
478 **p,
479 **q;
480
481 p=(const LocaleInfo **) x,
482 q=(const LocaleInfo **) y;
483 if (LocaleCompare((*p)->path,(*q)->path) == 0)
484 return(LocaleCompare((*p)->tag,(*q)->tag));
485 return(LocaleCompare((*p)->path,(*q)->path));
486}
487
488#if defined(__cplusplus) || defined(c_plusplus)
489}
490#endif
491
492MagickExport const LocaleInfo **GetLocaleInfoList(const char *pattern,
cristybb503372010-05-27 20:51:26 +0000493 size_t *number_messages,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000494{
495 const LocaleInfo
496 **messages;
497
498 register const LocaleInfo
499 *p;
500
cristybb503372010-05-27 20:51:26 +0000501 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000502 i;
503
504 /*
505 Allocate locale list.
506 */
507 assert(pattern != (char *) NULL);
508 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
cristybb503372010-05-27 20:51:26 +0000509 assert(number_messages != (size_t *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000510 *number_messages=0;
511 p=GetLocaleInfo_("*",exception);
512 if (p == (const LocaleInfo *) NULL)
513 return((const LocaleInfo **) NULL);
514 messages=(const LocaleInfo **) AcquireQuantumMemory((size_t)
515 GetNumberOfNodesInSplayTree(locale_list)+1UL,sizeof(*messages));
516 if (messages == (const LocaleInfo **) NULL)
517 return((const LocaleInfo **) NULL);
518 /*
519 Generate locale list.
520 */
cristyf84a1932010-01-03 18:00:18 +0000521 LockSemaphoreInfo(locale_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000522 ResetSplayTreeIterator(locale_list);
523 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_list);
524 for (i=0; p != (const LocaleInfo *) NULL; )
525 {
526 if ((p->stealth == MagickFalse) &&
527 (GlobExpression(p->tag,pattern,MagickTrue) != MagickFalse))
528 messages[i++]=p;
529 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_list);
530 }
cristyf84a1932010-01-03 18:00:18 +0000531 UnlockSemaphoreInfo(locale_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000532 qsort((void *) messages,(size_t) i,sizeof(*messages),LocaleInfoCompare);
533 messages[i]=(LocaleInfo *) NULL;
cristybb503372010-05-27 20:51:26 +0000534 *number_messages=(size_t) i;
cristy3ed852e2009-09-05 21:47:34 +0000535 return(messages);
536}
537
538/*
539%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
540% %
541% %
542% %
543% G e t L o c a l e L i s t %
544% %
545% %
546% %
547%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
548%
549% GetLocaleList() returns any locale messages that match the specified
550% pattern.
551%
552% The format of the GetLocaleList function is:
553%
cristybb503372010-05-27 20:51:26 +0000554% char **GetLocaleList(const char *pattern,size_t *number_messages,
cristy3ed852e2009-09-05 21:47:34 +0000555% Exceptioninfo *exception)
556%
557% A description of each parameter follows:
558%
559% o pattern: Specifies a pointer to a text string containing a pattern.
560%
561% o number_messages: This integer returns the number of messages in the
562% list.
563%
564% o exception: return any errors or warnings in this structure.
565%
566*/
567
568#if defined(__cplusplus) || defined(c_plusplus)
569extern "C" {
570#endif
571
572static int LocaleTagCompare(const void *x,const void *y)
573{
574 register char
575 **p,
576 **q;
577
578 p=(char **) x;
579 q=(char **) y;
580 return(LocaleCompare(*p,*q));
581}
582
583#if defined(__cplusplus) || defined(c_plusplus)
584}
585#endif
586
587MagickExport char **GetLocaleList(const char *pattern,
cristybb503372010-05-27 20:51:26 +0000588 size_t *number_messages,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000589{
590 char
591 **messages;
592
593 register const LocaleInfo
594 *p;
595
cristybb503372010-05-27 20:51:26 +0000596 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000597 i;
598
599 /*
600 Allocate locale list.
601 */
602 assert(pattern != (char *) NULL);
603 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
cristybb503372010-05-27 20:51:26 +0000604 assert(number_messages != (size_t *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000605 *number_messages=0;
606 p=GetLocaleInfo_("*",exception);
607 if (p == (const LocaleInfo *) NULL)
608 return((char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000609 messages=(char **) AcquireQuantumMemory((size_t)
610 GetNumberOfNodesInSplayTree(locale_list)+1UL,sizeof(*messages));
611 if (messages == (char **) NULL)
612 return((char **) NULL);
cristyf84a1932010-01-03 18:00:18 +0000613 LockSemaphoreInfo(locale_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000614 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_list);
615 for (i=0; p != (const LocaleInfo *) NULL; )
616 {
617 if ((p->stealth == MagickFalse) &&
618 (GlobExpression(p->tag,pattern,MagickTrue) != MagickFalse))
619 messages[i++]=ConstantString(p->tag);
620 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_list);
621 }
cristyf84a1932010-01-03 18:00:18 +0000622 UnlockSemaphoreInfo(locale_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000623 qsort((void *) messages,(size_t) i,sizeof(*messages),LocaleTagCompare);
624 messages[i]=(char *) NULL;
cristybb503372010-05-27 20:51:26 +0000625 *number_messages=(size_t) i;
cristy3ed852e2009-09-05 21:47:34 +0000626 return(messages);
627}
628
629/*
630%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
631% %
632% %
633% %
634% G e t L o c a l e M e s s a g e %
635% %
636% %
637% %
638%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
639%
640% GetLocaleMessage() returns a message in the current locale that matches the
641% supplied tag.
642%
643% The format of the GetLocaleMessage method is:
644%
645% const char *GetLocaleMessage(const char *tag)
646%
647% A description of each parameter follows:
648%
649% o tag: Return a message that matches this tag in the current locale.
650%
651*/
652MagickExport const char *GetLocaleMessage(const char *tag)
653{
654 char
655 name[MaxTextExtent];
656
657 const LocaleInfo
658 *locale_info;
659
660 ExceptionInfo
661 *exception;
662
663 if ((tag == (const char *) NULL) || (*tag == '\0'))
664 return(tag);
665 exception=AcquireExceptionInfo();
cristyb51dff52011-05-19 16:55:47 +0000666 (void) FormatLocaleString(name,MaxTextExtent,"%s/",tag);
cristy3ed852e2009-09-05 21:47:34 +0000667 locale_info=GetLocaleInfo_(name,exception);
668 exception=DestroyExceptionInfo(exception);
669 if (locale_info != (const LocaleInfo *) NULL)
670 return(locale_info->message);
671 return(tag);
672}
673
674/*
675%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
676% %
677% %
678% %
679% G e t L o c a l e O p t i o n s %
680% %
681% %
682% %
683%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
684%
685% GetLocaleOptions() returns any Magick configuration messages associated
686% with the specified filename.
687%
688% The format of the GetLocaleOptions method is:
689%
690% LinkedListInfo *GetLocaleOptions(const char *filename,
691% ExceptionInfo *exception)
692%
693% A description of each parameter follows:
694%
695% o filename: the locale file tag.
696%
697% o exception: return any errors or warnings in this structure.
698%
699*/
700MagickExport LinkedListInfo *GetLocaleOptions(const char *filename,
701 ExceptionInfo *exception)
702{
703 char
704 path[MaxTextExtent];
705
706 const char
707 *element;
708
709 LinkedListInfo
710 *messages,
711 *paths;
712
713 StringInfo
714 *xml;
715
716 assert(filename != (const char *) NULL);
717 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
718 assert(exception != (ExceptionInfo *) NULL);
719 (void) CopyMagickString(path,filename,MaxTextExtent);
720 /*
721 Load XML from configuration files to linked-list.
722 */
723 messages=NewLinkedList(0);
724 paths=GetConfigurePaths(filename,exception);
725 if (paths != (LinkedListInfo *) NULL)
726 {
727 ResetLinkedListIterator(paths);
728 element=(const char *) GetNextValueInLinkedList(paths);
729 while (element != (const char *) NULL)
730 {
cristyb51dff52011-05-19 16:55:47 +0000731 (void) FormatLocaleString(path,MaxTextExtent,"%s%s",element,filename);
cristy3ed852e2009-09-05 21:47:34 +0000732 (void) LogMagickEvent(LocaleEvent,GetMagickModule(),
733 "Searching for locale file: \"%s\"",path);
734 xml=ConfigureFileToStringInfo(path);
735 if (xml != (StringInfo *) NULL)
736 (void) AppendValueToLinkedList(messages,xml);
737 element=(const char *) GetNextValueInLinkedList(paths);
738 }
739 paths=DestroyLinkedList(paths,RelinquishMagickMemory);
740 }
cristy0157aea2010-04-24 21:12:18 +0000741#if defined(MAGICKCORE_WINDOWS_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000742 {
743 char
744 *blob;
745
746 blob=(char *) NTResourceToBlob(filename);
747 if (blob != (char *) NULL)
748 {
cristy85d6de52011-11-18 15:49:27 +0000749 xml=AcquireStringInfo(0);
750 SetStringInfoLength(xml,strlen(blob)+1);
cristy52070b42012-02-19 22:03:21 +0000751 SetStringInfoDatum(xml,(unsigned char *) blob);
cristy85d6de52011-11-18 15:49:27 +0000752 SetStringInfoPath(xml,filename);
cristy3ed852e2009-09-05 21:47:34 +0000753 (void) AppendValueToLinkedList(messages,xml);
cristy3ed852e2009-09-05 21:47:34 +0000754 }
755 }
756#endif
757 ResetLinkedListIterator(messages);
758 return(messages);
759}
760
761/*
762%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
763% %
764% %
765% %
766% G e t L o c a l e V a l u e %
767% %
768% %
769% %
770%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
771%
772% GetLocaleValue() returns the message associated with the locale info.
773%
774% The format of the GetLocaleValue method is:
775%
776% const char *GetLocaleValue(const LocaleInfo *locale_info)
777%
778% A description of each parameter follows:
779%
780% o locale_info: The locale info.
781%
782*/
783MagickExport const char *GetLocaleValue(const LocaleInfo *locale_info)
784{
785 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
786 assert(locale_info != (LocaleInfo *) NULL);
787 assert(locale_info->signature == MagickSignature);
788 return(locale_info->message);
789}
790
791/*
792%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
793% %
794% %
795% %
796+ I n i t i a l i z e L o c a l e L i s t %
797% %
798% %
799% %
800%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
801%
802% InitializeLocaleList() initializes the locale list.
803%
804% The format of the InitializeLocaleList method is:
805%
806% MagickBooleanType InitializeLocaleList(ExceptionInfo *exception)
807%
808% A description of each parameter follows.
809%
810% o exception: return any errors or warnings in this structure.
811%
812*/
813static MagickBooleanType InitializeLocaleList(ExceptionInfo *exception)
814{
815 if ((locale_list == (SplayTreeInfo *) NULL) &&
816 (instantiate_locale == MagickFalse))
817 {
cristy4e1dff62009-10-25 20:36:03 +0000818 if (locale_semaphore == (SemaphoreInfo *) NULL)
819 AcquireSemaphoreInfo(&locale_semaphore);
cristyf84a1932010-01-03 18:00:18 +0000820 LockSemaphoreInfo(locale_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000821 if ((locale_list == (SplayTreeInfo *) NULL) &&
822 (instantiate_locale == MagickFalse))
823 {
824 char
825 *locale;
826
827 register const char
828 *p;
829
830 locale=(char *) NULL;
831 p=setlocale(LC_CTYPE,(const char *) NULL);
832 if (p != (const char *) NULL)
833 locale=ConstantString(p);
834 if (locale == (char *) NULL)
835 locale=GetEnvironmentValue("LC_ALL");
836 if (locale == (char *) NULL)
837 locale=GetEnvironmentValue("LC_MESSAGES");
838 if (locale == (char *) NULL)
839 locale=GetEnvironmentValue("LC_CTYPE");
840 if (locale == (char *) NULL)
841 locale=GetEnvironmentValue("LANG");
842 if (locale == (char *) NULL)
843 locale=ConstantString("C");
844 (void) LoadLocaleLists(LocaleFilename,locale,exception);
845 locale=DestroyString(locale);
846 instantiate_locale=MagickTrue;
847 }
cristyf84a1932010-01-03 18:00:18 +0000848 UnlockSemaphoreInfo(locale_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000849 }
850 return(locale_list != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
851}
852
853/*
854%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
855% %
856% %
857% %
cristyc1acd842011-05-19 23:05:47 +0000858+ I n t e r p r e t L o c a l e V a l u e %
859% %
860% %
861% %
862%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
863%
864% InterpretLocaleValue() interprets the string as a floating point number in
865% the "C" locale and returns its value as a double. If sentinal is not a null
866% pointer, the method also sets the value pointed by sentinal to point to the
867% first character after the number.
868%
869% The format of the InterpretLocaleValue method is:
870%
871% double InterpretLocaleValue(const char *value,char **sentinal)
872%
873% A description of each parameter follows:
874%
875% o value: the string value.
876%
877% o sentinal: if sentinal is not NULL, a pointer to the character after the
878% last character used in the conversion is stored in the location
879% referenced by sentinal.
880%
881*/
cristyba56db52011-05-20 02:08:11 +0000882MagickExport double InterpretLocaleValue(const char *restrict string,
883 char **restrict sentinal)
cristyc1acd842011-05-19 23:05:47 +0000884{
cristy670aa3c2011-11-03 00:54:00 +0000885 char
886 *q;
887
cristyc1acd842011-05-19 23:05:47 +0000888 double
889 value;
890
cristy670aa3c2011-11-03 00:54:00 +0000891 if ((*string == '0') && ((string[1] | 0x20)=='x'))
892 value=(double) strtoul(string,&q,16);
893 else
894 {
895#if defined(MAGICKCORE_HAVE_STRTOD_L)
896 locale_t
897 locale;
898
899 locale=AcquireCLocale();
900 if (locale == (locale_t) NULL)
901 value=strtod(string,&q);
902 else
903 value=strtod_l(string,&q,locale);
cristyc1acd842011-05-19 23:05:47 +0000904#else
cristy670aa3c2011-11-03 00:54:00 +0000905 value=strtod(string,&q);
cristyc1acd842011-05-19 23:05:47 +0000906#endif
cristy670aa3c2011-11-03 00:54:00 +0000907 }
cristy670aa3c2011-11-03 00:54:00 +0000908 if (sentinal != (char **) NULL)
909 *sentinal=q;
cristyc1acd842011-05-19 23:05:47 +0000910 return(value);
911}
912
913/*
914%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
915% %
916% %
917% %
cristy3ed852e2009-09-05 21:47:34 +0000918% L i s t L o c a l e I n f o %
919% %
920% %
921% %
922%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
923%
924% ListLocaleInfo() lists the locale info to a file.
925%
926% The format of the ListLocaleInfo method is:
927%
928% MagickBooleanType ListLocaleInfo(FILE *file,ExceptionInfo *exception)
929%
930% A description of each parameter follows.
931%
932% o file: An pointer to a FILE.
933%
934% o exception: return any errors or warnings in this structure.
935%
936*/
937MagickExport MagickBooleanType ListLocaleInfo(FILE *file,
938 ExceptionInfo *exception)
939{
940 const char
941 *path;
942
943 const LocaleInfo
944 **locale_info;
945
cristybb503372010-05-27 20:51:26 +0000946 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000947 i;
948
cristybb503372010-05-27 20:51:26 +0000949 size_t
cristy3ed852e2009-09-05 21:47:34 +0000950 number_messages;
951
952 if (file == (const FILE *) NULL)
953 file=stdout;
954 number_messages=0;
955 locale_info=GetLocaleInfoList("*",&number_messages,exception);
956 if (locale_info == (const LocaleInfo **) NULL)
957 return(MagickFalse);
958 path=(const char *) NULL;
cristybb503372010-05-27 20:51:26 +0000959 for (i=0; i < (ssize_t) number_messages; i++)
cristy3ed852e2009-09-05 21:47:34 +0000960 {
961 if (locale_info[i]->stealth != MagickFalse)
962 continue;
963 if ((path == (const char *) NULL) ||
964 (LocaleCompare(path,locale_info[i]->path) != 0))
965 {
966 if (locale_info[i]->path != (char *) NULL)
cristyb51dff52011-05-19 16:55:47 +0000967 (void) FormatLocaleFile(file,"\nPath: %s\n\n",locale_info[i]->path);
968 (void) FormatLocaleFile(file,"Tag/Message\n");
cristy1e604812011-05-19 18:07:50 +0000969 (void) FormatLocaleFile(file,
970 "-------------------------------------------------"
cristy3ed852e2009-09-05 21:47:34 +0000971 "------------------------------\n");
972 }
973 path=locale_info[i]->path;
cristyb51dff52011-05-19 16:55:47 +0000974 (void) FormatLocaleFile(file,"%s\n",locale_info[i]->tag);
cristy3ed852e2009-09-05 21:47:34 +0000975 if (locale_info[i]->message != (char *) NULL)
cristyb51dff52011-05-19 16:55:47 +0000976 (void) FormatLocaleFile(file," %s",locale_info[i]->message);
977 (void) FormatLocaleFile(file,"\n");
cristy3ed852e2009-09-05 21:47:34 +0000978 }
979 (void) fflush(file);
980 locale_info=(const LocaleInfo **)
981 RelinquishMagickMemory((void *) locale_info);
982 return(MagickTrue);
983}
984
985/*
986%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
987% %
988% %
989% %
990+ L o a d L o c a l e L i s t %
991% %
992% %
993% %
994%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
995%
996% LoadLocaleList() loads the locale configuration file which provides a mapping
997% between locale attributes and a locale name.
998%
999% The format of the LoadLocaleList method is:
1000%
1001% MagickBooleanType LoadLocaleList(const char *xml,const char *filename,
cristybb503372010-05-27 20:51:26 +00001002% const size_t depth,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001003%
1004% A description of each parameter follows:
1005%
1006% o xml: The locale list in XML format.
1007%
1008% o filename: The locale list filename.
1009%
1010% o depth: depth of <include /> statements.
1011%
1012% o exception: return any errors or warnings in this structure.
1013%
1014*/
1015
cristybb503372010-05-27 20:51:26 +00001016static void ChopLocaleComponents(char *path,const size_t components)
cristy3ed852e2009-09-05 21:47:34 +00001017{
cristy3ed852e2009-09-05 21:47:34 +00001018 register char
1019 *p;
1020
cristy9d314ff2011-03-09 01:30:28 +00001021 ssize_t
1022 count;
1023
cristy3ed852e2009-09-05 21:47:34 +00001024 if (*path == '\0')
1025 return;
1026 p=path+strlen(path)-1;
1027 if (*p == '/')
1028 *p='\0';
cristybb503372010-05-27 20:51:26 +00001029 for (count=0; (count < (ssize_t) components) && (p > path); p--)
cristy3ed852e2009-09-05 21:47:34 +00001030 if (*p == '/')
1031 {
1032 *p='\0';
1033 count++;
1034 }
cristybb503372010-05-27 20:51:26 +00001035 if (count < (ssize_t) components)
cristy3ed852e2009-09-05 21:47:34 +00001036 *path='\0';
1037}
1038
1039static void *DestroyLocaleNode(void *locale_info)
1040{
1041 register LocaleInfo
1042 *p;
1043
1044 p=(LocaleInfo *) locale_info;
1045 if (p->path != (char *) NULL)
1046 p->path=DestroyString(p->path);
1047 if (p->tag != (char *) NULL)
1048 p->tag=DestroyString(p->tag);
1049 if (p->message != (char *) NULL)
1050 p->message=DestroyString(p->message);
1051 return(RelinquishMagickMemory(p));
1052}
1053
cristy2359e742009-11-24 14:50:02 +00001054static void LocaleFatalErrorHandler(
1055 const ExceptionType magick_unused(severity),
1056 const char *reason,const char *description)
1057{
1058 if (reason == (char *) NULL)
1059 return;
cristyb51dff52011-05-19 16:55:47 +00001060 (void) FormatLocaleFile(stderr,"%s: %s",GetClientName(),reason);
cristy2359e742009-11-24 14:50:02 +00001061 if (description != (char *) NULL)
cristyb51dff52011-05-19 16:55:47 +00001062 (void) FormatLocaleFile(stderr," (%s)",description);
1063 (void) FormatLocaleFile(stderr,".\n");
cristy2359e742009-11-24 14:50:02 +00001064 (void) fflush(stderr);
1065 exit(1);
1066}
1067
1068
cristy3ed852e2009-09-05 21:47:34 +00001069static MagickBooleanType LoadLocaleList(const char *xml,const char *filename,
cristybb503372010-05-27 20:51:26 +00001070 const char *locale,const size_t depth,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001071{
1072 char
1073 keyword[MaxTextExtent],
1074 message[MaxTextExtent],
1075 tag[MaxTextExtent],
1076 *token;
1077
1078 const char
1079 *q;
1080
cristy909b6472009-11-24 14:45:00 +00001081 FatalErrorHandler
1082 fatal_handler;
1083
cristy3ed852e2009-09-05 21:47:34 +00001084 LocaleInfo
1085 *locale_info;
1086
1087 MagickBooleanType
1088 status;
1089
1090 register char
1091 *p;
1092
1093 /*
1094 Read the locale configure file.
1095 */
1096 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1097 "Loading locale configure file \"%s\" ...",filename);
1098 if (xml == (const char *) NULL)
1099 return(MagickFalse);
1100 if (locale_list == (SplayTreeInfo *) NULL)
1101 {
1102 locale_list=NewSplayTree(CompareSplayTreeString,(void *(*)(void *)) NULL,
1103 DestroyLocaleNode);
1104 if (locale_list == (SplayTreeInfo *) NULL)
cristy909b6472009-11-24 14:45:00 +00001105 return(MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +00001106 }
1107 status=MagickTrue;
1108 locale_info=(LocaleInfo *) NULL;
1109 *tag='\0';
1110 *message='\0';
1111 *keyword='\0';
cristy2359e742009-11-24 14:50:02 +00001112 fatal_handler=SetFatalErrorHandler(LocaleFatalErrorHandler);
cristy3ed852e2009-09-05 21:47:34 +00001113 token=AcquireString(xml);
1114 for (q=(char *) xml; *q != '\0'; )
1115 {
1116 /*
1117 Interpret XML.
1118 */
1119 GetMagickToken(q,&q,token);
1120 if (*token == '\0')
1121 break;
1122 (void) CopyMagickString(keyword,token,MaxTextExtent);
1123 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
1124 {
1125 /*
1126 Doctype element.
1127 */
1128 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
1129 {
1130 GetMagickToken(q,&q,token);
1131 while (isspace((int) ((unsigned char) *q)) != 0)
1132 q++;
1133 }
1134 continue;
1135 }
1136 if (LocaleNCompare(keyword,"<!--",4) == 0)
1137 {
1138 /*
1139 Comment element.
1140 */
1141 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
1142 {
1143 GetMagickToken(q,&q,token);
1144 while (isspace((int) ((unsigned char) *q)) != 0)
1145 q++;
1146 }
1147 continue;
1148 }
1149 if (LocaleCompare(keyword,"<include") == 0)
1150 {
1151 /*
1152 Include element.
1153 */
1154 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
1155 {
1156 (void) CopyMagickString(keyword,token,MaxTextExtent);
1157 GetMagickToken(q,&q,token);
1158 if (*token != '=')
1159 continue;
1160 GetMagickToken(q,&q,token);
1161 if (LocaleCompare(keyword,"locale") == 0)
1162 {
1163 if (LocaleCompare(locale,token) != 0)
1164 break;
1165 continue;
1166 }
1167 if (LocaleCompare(keyword,"file") == 0)
1168 {
1169 if (depth > 200)
1170 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00001171 ConfigureError,"IncludeElementNestedTooDeeply","`%s'",token);
cristy3ed852e2009-09-05 21:47:34 +00001172 else
1173 {
1174 char
1175 path[MaxTextExtent],
1176 *xml;
1177
1178 *path='\0';
1179 GetPathComponent(filename,HeadPath,path);
1180 if (*path != '\0')
1181 (void) ConcatenateMagickString(path,DirectorySeparator,
1182 MaxTextExtent);
1183 if (*token == *DirectorySeparator)
1184 (void) CopyMagickString(path,token,MaxTextExtent);
1185 else
1186 (void) ConcatenateMagickString(path,token,MaxTextExtent);
1187 xml=FileToString(path,~0,exception);
1188 if (xml != (char *) NULL)
1189 {
1190 status=LoadLocaleList(xml,path,locale,depth+1,exception);
1191 xml=(char *) RelinquishMagickMemory(xml);
1192 }
1193 }
1194 }
1195 }
1196 continue;
1197 }
1198 if (LocaleCompare(keyword,"<locale") == 0)
1199 {
1200 /*
1201 Locale element.
1202 */
1203 while ((*token != '>') && (*q != '\0'))
1204 {
1205 (void) CopyMagickString(keyword,token,MaxTextExtent);
1206 GetMagickToken(q,&q,token);
1207 if (*token != '=')
1208 continue;
1209 GetMagickToken(q,&q,token);
1210 }
1211 continue;
1212 }
1213 if (LocaleCompare(keyword,"</locale>") == 0)
1214 {
1215 ChopLocaleComponents(tag,1);
1216 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1217 continue;
1218 }
1219 if (LocaleCompare(keyword,"<localemap>") == 0)
1220 continue;
1221 if (LocaleCompare(keyword,"</localemap>") == 0)
1222 continue;
1223 if (LocaleCompare(keyword,"<message") == 0)
1224 {
1225 /*
1226 Message element.
1227 */
1228 while ((*token != '>') && (*q != '\0'))
1229 {
1230 (void) CopyMagickString(keyword,token,MaxTextExtent);
1231 GetMagickToken(q,&q,token);
1232 if (*token != '=')
1233 continue;
1234 GetMagickToken(q,&q,token);
1235 if (LocaleCompare(keyword,"name") == 0)
1236 {
1237 (void) ConcatenateMagickString(tag,token,MaxTextExtent);
1238 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1239 }
1240 }
1241 for (p=(char *) q; (*q != '<') && (*q != '\0'); q++) ;
1242 while (isspace((int) ((unsigned char) *p)) != 0)
1243 p++;
1244 q--;
1245 while ((isspace((int) ((unsigned char) *q)) != 0) && (q > p))
1246 q--;
1247 (void) CopyMagickString(message,p,(size_t) (q-p+2));
cristy73bd4a52010-10-05 11:24:23 +00001248 locale_info=(LocaleInfo *) AcquireMagickMemory(sizeof(*locale_info));
cristy3ed852e2009-09-05 21:47:34 +00001249 if (locale_info == (LocaleInfo *) NULL)
1250 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1251 (void) ResetMagickMemory(locale_info,0,sizeof(*locale_info));
1252 locale_info->path=ConstantString(filename);
1253 locale_info->tag=ConstantString(tag);
1254 locale_info->message=ConstantString(message);
1255 locale_info->signature=MagickSignature;
1256 status=AddValueToSplayTree(locale_list,locale_info->tag,locale_info);
1257 if (status == MagickFalse)
1258 (void) ThrowMagickException(exception,GetMagickModule(),
cristyefe601c2013-01-05 17:51:12 +00001259 ResourceLimitError,"MemoryAllocationFailed","`%s'",
cristy3ed852e2009-09-05 21:47:34 +00001260 locale_info->tag);
1261 (void) ConcatenateMagickString(tag,message,MaxTextExtent);
1262 (void) ConcatenateMagickString(tag,"\n",MaxTextExtent);
1263 q++;
1264 continue;
1265 }
1266 if (LocaleCompare(keyword,"</message>") == 0)
1267 {
1268 ChopLocaleComponents(tag,2);
1269 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1270 continue;
1271 }
1272 if (*keyword == '<')
1273 {
1274 /*
1275 Subpath element.
1276 */
1277 if (*(keyword+1) == '?')
1278 continue;
1279 if (*(keyword+1) == '/')
1280 {
1281 ChopLocaleComponents(tag,1);
1282 if (*tag != '\0')
1283 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1284 continue;
1285 }
1286 token[strlen(token)-1]='\0';
1287 (void) CopyMagickString(token,token+1,MaxTextExtent);
1288 (void) ConcatenateMagickString(tag,token,MaxTextExtent);
1289 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1290 continue;
1291 }
1292 GetMagickToken(q,(const char **) NULL,token);
1293 if (*token != '=')
1294 continue;
1295 }
1296 token=(char *) RelinquishMagickMemory(token);
cristy909b6472009-11-24 14:45:00 +00001297 (void) SetFatalErrorHandler(fatal_handler);
cristy3ed852e2009-09-05 21:47:34 +00001298 return(status);
1299}
1300
1301/*
1302%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1303% %
1304% %
1305% %
1306% L o a d L o c a l e L i s t s %
1307% %
1308% %
1309% %
1310%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1311%
1312% LoadLocaleList() loads one or more locale configuration file which
1313% provides a mapping between locale attributes and a locale tag.
1314%
1315% The format of the LoadLocaleLists method is:
1316%
1317% MagickBooleanType LoadLocaleLists(const char *filename,
1318% ExceptionInfo *exception)
1319%
1320% A description of each parameter follows:
1321%
1322% o filename: the font file tag.
1323%
1324% o locale: the actual locale.
1325%
1326% o exception: return any errors or warnings in this structure.
1327%
1328*/
1329static MagickBooleanType LoadLocaleLists(const char *filename,
1330 const char *locale,ExceptionInfo *exception)
1331{
cristy6e3607c2011-09-13 13:59:17 +00001332#if defined(MAGICKCORE_ZERO_CONFIGURATION_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00001333 return(LoadLocaleList(LocaleMap,"built-in",locale,0,exception));
1334#else
1335 const StringInfo
1336 *option;
1337
1338 LinkedListInfo
1339 *options;
1340
1341 MagickStatusType
1342 status;
1343
1344 status=MagickFalse;
1345 options=GetLocaleOptions(filename,exception);
1346 option=(const StringInfo *) GetNextValueInLinkedList(options);
1347 while (option != (const StringInfo *) NULL)
1348 {
1349 status|=LoadLocaleList((const char *) GetStringInfoDatum(option),
1350 GetStringInfoPath(option),locale,0,exception);
1351 option=(const StringInfo *) GetNextValueInLinkedList(options);
1352 }
1353 options=DestroyLocaleOptions(options);
1354 if ((locale_list == (SplayTreeInfo *) NULL) ||
1355 (GetNumberOfNodesInSplayTree(locale_list) == 0))
1356 {
1357 options=GetLocaleOptions("english.xml",exception);
1358 option=(const StringInfo *) GetNextValueInLinkedList(options);
1359 while (option != (const StringInfo *) NULL)
1360 {
1361 status|=LoadLocaleList((const char *) GetStringInfoDatum(option),
1362 GetStringInfoPath(option),locale,0,exception);
1363 option=(const StringInfo *) GetNextValueInLinkedList(options);
1364 }
1365 options=DestroyLocaleOptions(options);
1366 }
1367 if ((locale_list == (SplayTreeInfo *) NULL) ||
1368 (GetNumberOfNodesInSplayTree(locale_list) == 0))
1369 status|=LoadLocaleList(LocaleMap,"built-in",locale,0,exception);
1370 return(status != 0 ? MagickTrue : MagickFalse);
1371#endif
1372}
cristyf34a1452009-10-24 22:29:27 +00001373
1374/*
1375%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1376% %
1377% %
1378% %
1379+ L o c a l e C o m p o n e n t G e n e s i s %
1380% %
1381% %
1382% %
1383%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1384%
1385% LocaleComponentGenesis() instantiates the locale component.
1386%
1387% The format of the LocaleComponentGenesis method is:
1388%
1389% MagickBooleanType LocaleComponentGenesis(void)
1390%
1391*/
cristy5ff4eaf2011-09-03 01:38:02 +00001392MagickPrivate MagickBooleanType LocaleComponentGenesis(void)
cristyf34a1452009-10-24 22:29:27 +00001393{
cristy165b6092009-10-26 13:52:10 +00001394 AcquireSemaphoreInfo(&locale_semaphore);
cristyf34a1452009-10-24 22:29:27 +00001395 return(MagickTrue);
1396}
1397
1398/*
1399%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1400% %
1401% %
1402% %
1403+ L o c a l e C o m p o n e n t T e r m i n u s %
1404% %
1405% %
1406% %
1407%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1408%
1409% LocaleComponentTerminus() destroys the locale component.
1410%
1411% The format of the LocaleComponentTerminus method is:
1412%
1413% LocaleComponentTerminus(void)
1414%
1415*/
cristy5ff4eaf2011-09-03 01:38:02 +00001416MagickPrivate void LocaleComponentTerminus(void)
cristyf34a1452009-10-24 22:29:27 +00001417{
cristy18b17442009-10-25 18:36:48 +00001418 if (locale_semaphore == (SemaphoreInfo *) NULL)
1419 AcquireSemaphoreInfo(&locale_semaphore);
cristyf84a1932010-01-03 18:00:18 +00001420 LockSemaphoreInfo(locale_semaphore);
cristy04715a62012-06-20 17:36:42 +00001421 if (locale_list != (SplayTreeInfo *) NULL)
1422 locale_list=DestroySplayTree(locale_list);
cristya723b6a2011-05-25 12:08:51 +00001423#if defined(MAGICKCORE_HAVE_STRTOD_L)
cristyb51dff52011-05-19 16:55:47 +00001424 DestroyCLocale();
cristya723b6a2011-05-25 12:08:51 +00001425#endif
cristyf34a1452009-10-24 22:29:27 +00001426 instantiate_locale=MagickFalse;
cristyf84a1932010-01-03 18:00:18 +00001427 UnlockSemaphoreInfo(locale_semaphore);
cristyf34a1452009-10-24 22:29:27 +00001428 DestroySemaphoreInfo(&locale_semaphore);
1429}