blob: 93154832b407effc4913135189062b28ac949752 [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% %
cristy1454be72011-12-19 01:52:48 +000020% Copyright 1999-2012 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"
53#include "MagickCore/semaphore.h"
54#include "MagickCore/splay-tree.h"
55#include "MagickCore/string_.h"
cristy7832dc22011-09-05 01:21:53 +000056#include "MagickCore/string-private.h"
cristy4c08aed2011-07-01 19:47:50 +000057#include "MagickCore/token.h"
58#include "MagickCore/utility.h"
cristyd1dd6e42011-09-04 01:46:08 +000059#include "MagickCore/utility-private.h"
cristy4c08aed2011-07-01 19:47:50 +000060#include "MagickCore/xml-tree.h"
cristy3ed852e2009-09-05 21:47:34 +000061
62/*
63 Define declarations.
64*/
65#define LocaleFilename "locale.xml"
66#define MaxRecursionDepth 200
67
68/*
cristycaa2ce52011-05-20 23:59:13 +000069 Typedef declarations.
70*/
71#if defined(__CYGWIN__)
72typedef struct _locale_t *locale_t;
73#endif
74
75/*
cristy3ed852e2009-09-05 21:47:34 +000076 Static declarations.
77*/
78static const char
79 *LocaleMap =
80 "<?xml version=\"1.0\"?>"
81 "<localemap>"
82 " <locale name=\"C\">"
83 " <Exception>"
84 " <Message name=\"\">"
85 " </Message>"
86 " </Exception>"
87 " </locale>"
88 "</localemap>";
89
90static SemaphoreInfo
91 *locale_semaphore = (SemaphoreInfo *) NULL;
92
93static SplayTreeInfo
94 *locale_list = (SplayTreeInfo *) NULL;
95
cristya723b6a2011-05-25 12:08:51 +000096#if defined(MAGICKCORE_HAVE_STRTOD_L)
cristya20c9042011-05-19 13:22:18 +000097static volatile locale_t
cristyb51dff52011-05-19 16:55:47 +000098 c_locale = (locale_t) NULL;
cristya723b6a2011-05-25 12:08:51 +000099#endif
cristy022ce732011-05-19 23:34:51 +0000100
101static volatile MagickBooleanType
102 instantiate_locale = MagickFalse;
cristy3ed852e2009-09-05 21:47:34 +0000103
104/*
105 Forward declarations.
106*/
107static MagickBooleanType
108 InitializeLocaleList(ExceptionInfo *),
109 LoadLocaleLists(const char *,const char *,ExceptionInfo *);
110
cristya723b6a2011-05-25 12:08:51 +0000111#if defined(MAGICKCORE_HAVE_STRTOD_L)
cristy3ed852e2009-09-05 21:47:34 +0000112/*
113%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
114% %
115% %
116% %
cristyb51dff52011-05-19 16:55:47 +0000117+ A c q u i r e C L o c a l e %
cristya20c9042011-05-19 13:22:18 +0000118% %
119% %
120% %
121%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
122%
cristyb51dff52011-05-19 16:55:47 +0000123% AcquireCLocale() allocates the C locale object, or (locale_t) 0 with
cristya20c9042011-05-19 13:22:18 +0000124% errno set if it cannot be acquired.
125%
cristyb51dff52011-05-19 16:55:47 +0000126% The format of the AcquireCLocale method is:
cristya20c9042011-05-19 13:22:18 +0000127%
cristyb51dff52011-05-19 16:55:47 +0000128% locale_t AcquireCLocale(void)
cristya20c9042011-05-19 13:22:18 +0000129%
130*/
cristyb51dff52011-05-19 16:55:47 +0000131static locale_t AcquireCLocale(void)
cristya20c9042011-05-19 13:22:18 +0000132{
133#if defined(MAGICKCORE_HAVE_NEWLOCALE)
cristyb51dff52011-05-19 16:55:47 +0000134 if (c_locale == (locale_t) NULL)
135 c_locale=newlocale(LC_ALL_MASK,"C",(locale_t) 0);
cristya20c9042011-05-19 13:22:18 +0000136#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
cristyb51dff52011-05-19 16:55:47 +0000137 if (c_locale == (locale_t) NULL)
138 c_locale=_create_locale(LC_ALL,"C");
cristya20c9042011-05-19 13:22:18 +0000139#endif
cristyb51dff52011-05-19 16:55:47 +0000140 return(c_locale);
141}
cristya723b6a2011-05-25 12:08:51 +0000142#endif
cristyb51dff52011-05-19 16:55:47 +0000143
cristya723b6a2011-05-25 12:08:51 +0000144#if defined(MAGICKCORE_HAVE_STRTOD_L)
cristyb51dff52011-05-19 16:55:47 +0000145/*
146%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
147% %
148% %
149% %
150+ D e s t r o y C L o c a l e %
151% %
152% %
153% %
154%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
155%
156% DestroyCLocale() releases the resources allocated for a locale object
157% returned by a call to the AcquireCLocale() method.
158%
159% The format of the DestroyCLocale method is:
160%
161% void DestroyCLocale(void)
162%
163*/
164static void DestroyCLocale(void)
165{
166#if defined(MAGICKCORE_HAVE_NEWLOCALE)
167 if (c_locale != (locale_t) NULL)
168 freelocale(c_locale);
169#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
170 if (c_locale != (locale_t) NULL)
171 _free_locale(c_locale);
172#endif
173 c_locale=(locale_t) NULL;
cristya20c9042011-05-19 13:22:18 +0000174}
cristya723b6a2011-05-25 12:08:51 +0000175#endif
cristya20c9042011-05-19 13:22:18 +0000176
177/*
178%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
179% %
180% %
181% %
cristy3ed852e2009-09-05 21:47:34 +0000182% D e s t r o y L o c a l e O p t i o n s %
183% %
184% %
185% %
186%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
187%
188% DestroyLocaleOptions() releases memory associated with an locale
189% messages.
190%
191% The format of the DestroyProfiles method is:
192%
193% LinkedListInfo *DestroyLocaleOptions(Image *image)
194%
195% A description of each parameter follows:
196%
197% o image: the image.
198%
199*/
200
201static void *DestroyOptions(void *message)
202{
203 return(DestroyStringInfo((StringInfo *) message));
204}
205
206MagickExport LinkedListInfo *DestroyLocaleOptions(LinkedListInfo *messages)
207{
208 assert(messages != (LinkedListInfo *) NULL);
209 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
210 return(DestroyLinkedList(messages,DestroyOptions));
211}
212
213/*
214%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
215% %
216% %
217% %
cristyb51dff52011-05-19 16:55:47 +0000218+ F o r m a t L o c a l e F i l e %
cristya20c9042011-05-19 13:22:18 +0000219% %
220% %
221% %
222%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
223%
cristyb51dff52011-05-19 16:55:47 +0000224% FormatLocaleFile() prints formatted output of a variable argument list to a
225% file in the "C" locale.
cristya20c9042011-05-19 13:22:18 +0000226%
cristyb51dff52011-05-19 16:55:47 +0000227% The format of the FormatLocaleFile method is:
cristya20c9042011-05-19 13:22:18 +0000228%
cristyb51dff52011-05-19 16:55:47 +0000229% ssize_t FormatLocaleFile(FILE *file,const char *format,...)
230%
231% A description of each parameter follows.
232%
233% o file: the file.
234%
235% o format: A file describing the format to use to write the remaining
236% arguments.
cristya20c9042011-05-19 13:22:18 +0000237%
238*/
cristyb51dff52011-05-19 16:55:47 +0000239
cristy7832dc22011-09-05 01:21:53 +0000240MagickPrivate ssize_t FormatLocaleFileList(FILE *file,
cristyba56db52011-05-20 02:08:11 +0000241 const char *restrict format,va_list operands)
cristya20c9042011-05-19 13:22:18 +0000242{
cristy20ec7592011-05-29 01:28:05 +0000243 ssize_t
cristyb51dff52011-05-19 16:55:47 +0000244 n;
245
cristy5d0cd952011-05-19 21:21:41 +0000246#if defined(MAGICKCORE_HAVE_VFPRINTF_L)
cristyb51dff52011-05-19 16:55:47 +0000247 {
248 locale_t
249 locale;
250
251 locale=AcquireCLocale();
252 if (locale == (locale_t) NULL)
cristy20ec7592011-05-29 01:28:05 +0000253 n=(ssize_t) vfprintf(file,format,operands);
cristyb51dff52011-05-19 16:55:47 +0000254 else
cristy26d78202011-05-21 00:59:34 +0000255#if defined(MAGICKCORE_WINDOWS_SUPPORT)
cristy20ec7592011-05-29 01:28:05 +0000256 n=(ssize_t) vfprintf_l(file,format,locale,operands);
cristy26d78202011-05-21 00:59:34 +0000257#else
cristy20ec7592011-05-29 01:28:05 +0000258 n=(ssize_t) vfprintf_l(file,locale,format,operands);
cristy405905f2011-05-21 00:37:33 +0000259#endif
cristyb51dff52011-05-19 16:55:47 +0000260 }
261#else
262#if defined(MAGICKCORE_HAVE_USELOCALE)
263 {
264 locale_t
265 locale,
266 previous_locale;
267
268 locale=AcquireCLocale();
269 if (locale == (locale_t) NULL)
cristy20ec7592011-05-29 01:28:05 +0000270 n=(ssize_t) vfprintf(file,format,operands);
cristyb51dff52011-05-19 16:55:47 +0000271 else
272 {
273 previous_locale=uselocale(locale);
cristy20ec7592011-05-29 01:28:05 +0000274 n=(ssize_t) vfprintf(file,format,operands);
cristyb51dff52011-05-19 16:55:47 +0000275 uselocale(previous_locale);
276 }
277 }
278#else
cristy20ec7592011-05-29 01:28:05 +0000279 n=(ssize_t) vfprintf(file,format,operands);
cristya20c9042011-05-19 13:22:18 +0000280#endif
cristyb51dff52011-05-19 16:55:47 +0000281#endif
cristy3da0b402011-05-29 21:13:36 +0000282 return(n);
cristyb51dff52011-05-19 16:55:47 +0000283}
284
cristyba56db52011-05-20 02:08:11 +0000285MagickExport ssize_t FormatLocaleFile(FILE *file,const char *restrict format,
286 ...)
cristyb51dff52011-05-19 16:55:47 +0000287{
288 ssize_t
289 n;
290
291 va_list
292 operands;
293
294 va_start(operands,format);
cristy20ec7592011-05-29 01:28:05 +0000295 n=FormatLocaleFileList(file,format,operands);
cristyb51dff52011-05-19 16:55:47 +0000296 va_end(operands);
297 return(n);
298}
299
300/*
301%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
302% %
303% %
304% %
305+ F o r m a t L o c a l e S t r i n g %
306% %
307% %
308% %
309%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
310%
311% FormatLocaleString() prints formatted output of a variable argument list to
312% a string buffer in the "C" locale.
313%
314% The format of the FormatLocaleString method is:
315%
316% ssize_t FormatLocaleString(char *string,const size_t length,
317% const char *format,...)
318%
319% A description of each parameter follows.
320%
321% o string: FormatLocaleString() returns the formatted string in this
322% character buffer.
323%
324% o length: the maximum length of the string.
325%
326% o format: A string describing the format to use to write the remaining
327% arguments.
328%
329*/
330
cristy7832dc22011-09-05 01:21:53 +0000331MagickPrivate ssize_t FormatLocaleStringList(char *restrict string,
cristyba56db52011-05-20 02:08:11 +0000332 const size_t length,const char *restrict format,va_list operands)
cristyb51dff52011-05-19 16:55:47 +0000333{
cristy20ec7592011-05-29 01:28:05 +0000334 ssize_t
cristyb51dff52011-05-19 16:55:47 +0000335 n;
336
337#if defined(MAGICKCORE_HAVE_VSNPRINTF_L)
cristy405905f2011-05-21 00:37:33 +0000338 {
339 locale_t
340 locale;
341
342 locale=AcquireCLocale();
343 if (locale == (locale_t) NULL)
cristy20ec7592011-05-29 01:28:05 +0000344 n=(ssize_t) vsnprintf(string,length,format,operands);
cristy405905f2011-05-21 00:37:33 +0000345 else
cristy26d78202011-05-21 00:59:34 +0000346#if defined(MAGICKCORE_WINDOWS_SUPPORT)
cristy20ec7592011-05-29 01:28:05 +0000347 n=(ssize_t) vsnprintf_l(string,length,format,locale,operands);
cristy26d78202011-05-21 00:59:34 +0000348#else
cristy20ec7592011-05-29 01:28:05 +0000349 n=(ssize_t) vsnprintf_l(string,length,locale,format,operands);
cristy405905f2011-05-21 00:37:33 +0000350#endif
351 }
cristyb51dff52011-05-19 16:55:47 +0000352#elif defined(MAGICKCORE_HAVE_VSNPRINTF)
353#if defined(MAGICKCORE_HAVE_USELOCALE)
354 {
355 locale_t
356 locale,
357 previous_locale;
358
359 locale=AcquireCLocale();
360 if (locale == (locale_t) NULL)
cristy20ec7592011-05-29 01:28:05 +0000361 n=(ssize_t) vsnprintf(string,length,format,operands);
cristyb51dff52011-05-19 16:55:47 +0000362 else
363 {
364 previous_locale=uselocale(locale);
cristy20ec7592011-05-29 01:28:05 +0000365 n=(ssize_t) vsnprintf(string,length,format,operands);
cristyb51dff52011-05-19 16:55:47 +0000366 uselocale(previous_locale);
367 }
368 }
369#else
cristy20ec7592011-05-29 01:28:05 +0000370 n=(ssize_t) vsnprintf(string,length,format,operands);
cristyb51dff52011-05-19 16:55:47 +0000371#endif
372#else
cristy20ec7592011-05-29 01:28:05 +0000373 n=(ssize_t) vsprintf(string,format,operands);
cristyb51dff52011-05-19 16:55:47 +0000374#endif
375 if (n < 0)
376 string[length-1]='\0';
cristy20ec7592011-05-29 01:28:05 +0000377 return(n);
cristyb51dff52011-05-19 16:55:47 +0000378}
379
cristyba56db52011-05-20 02:08:11 +0000380MagickExport ssize_t FormatLocaleString(char *restrict string,
381 const size_t length,const char *restrict format,...)
cristyb51dff52011-05-19 16:55:47 +0000382{
383 ssize_t
384 n;
385
386 va_list
387 operands;
388
389 va_start(operands,format);
cristy20ec7592011-05-29 01:28:05 +0000390 n=FormatLocaleStringList(string,length,format,operands);
cristyb51dff52011-05-19 16:55:47 +0000391 va_end(operands);
392 return(n);
cristya20c9042011-05-19 13:22:18 +0000393}
394
395/*
396%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
397% %
398% %
399% %
cristy3ed852e2009-09-05 21:47:34 +0000400+ G e t L o c a l e I n f o _ %
401% %
402% %
403% %
404%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
405%
406% GetLocaleInfo_() searches the locale list for the specified tag and if
407% found returns attributes for that element.
408%
409% The format of the GetLocaleInfo method is:
410%
411% const LocaleInfo *GetLocaleInfo_(const char *tag,
412% ExceptionInfo *exception)
413%
414% A description of each parameter follows:
415%
416% o tag: the locale tag.
417%
418% o exception: return any errors or warnings in this structure.
419%
420*/
421MagickExport const LocaleInfo *GetLocaleInfo_(const char *tag,
422 ExceptionInfo *exception)
423{
424 assert(exception != (ExceptionInfo *) NULL);
425 if ((locale_list == (SplayTreeInfo *) NULL) ||
426 (instantiate_locale == MagickFalse))
427 if (InitializeLocaleList(exception) == MagickFalse)
428 return((const LocaleInfo *) NULL);
429 if ((locale_list == (SplayTreeInfo *) NULL) ||
430 (GetNumberOfNodesInSplayTree(locale_list) == 0))
431 return((const LocaleInfo *) NULL);
432 if ((tag == (const char *) NULL) || (LocaleCompare(tag,"*") == 0))
433 {
434 ResetSplayTreeIterator(locale_list);
435 return((const LocaleInfo *) GetNextValueInSplayTree(locale_list));
436 }
437 return((const LocaleInfo *) GetValueFromSplayTree(locale_list,tag));
438}
439
440/*
441%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
442% %
443% %
444% %
445% G e t L o c a l e I n f o L i s t %
446% %
447% %
448% %
449%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
450%
451% GetLocaleInfoList() returns any locale messages that match the
452% specified pattern.
453%
454% The format of the GetLocaleInfoList function is:
455%
456% const LocaleInfo **GetLocaleInfoList(const char *pattern,
cristybb503372010-05-27 20:51:26 +0000457% size_t *number_messages,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000458%
459% A description of each parameter follows:
460%
461% o pattern: Specifies a pointer to a text string containing a pattern.
462%
463% o number_messages: This integer returns the number of locale messages in
464% the list.
465%
466% o exception: return any errors or warnings in this structure.
467%
468*/
469
470#if defined(__cplusplus) || defined(c_plusplus)
471extern "C" {
472#endif
473
474static int LocaleInfoCompare(const void *x,const void *y)
475{
476 const LocaleInfo
477 **p,
478 **q;
479
480 p=(const LocaleInfo **) x,
481 q=(const LocaleInfo **) y;
482 if (LocaleCompare((*p)->path,(*q)->path) == 0)
483 return(LocaleCompare((*p)->tag,(*q)->tag));
484 return(LocaleCompare((*p)->path,(*q)->path));
485}
486
487#if defined(__cplusplus) || defined(c_plusplus)
488}
489#endif
490
491MagickExport const LocaleInfo **GetLocaleInfoList(const char *pattern,
cristybb503372010-05-27 20:51:26 +0000492 size_t *number_messages,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000493{
494 const LocaleInfo
495 **messages;
496
497 register const LocaleInfo
498 *p;
499
cristybb503372010-05-27 20:51:26 +0000500 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000501 i;
502
503 /*
504 Allocate locale list.
505 */
506 assert(pattern != (char *) NULL);
507 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
cristybb503372010-05-27 20:51:26 +0000508 assert(number_messages != (size_t *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000509 *number_messages=0;
510 p=GetLocaleInfo_("*",exception);
511 if (p == (const LocaleInfo *) NULL)
512 return((const LocaleInfo **) NULL);
513 messages=(const LocaleInfo **) AcquireQuantumMemory((size_t)
514 GetNumberOfNodesInSplayTree(locale_list)+1UL,sizeof(*messages));
515 if (messages == (const LocaleInfo **) NULL)
516 return((const LocaleInfo **) NULL);
517 /*
518 Generate locale list.
519 */
cristyf84a1932010-01-03 18:00:18 +0000520 LockSemaphoreInfo(locale_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000521 ResetSplayTreeIterator(locale_list);
522 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_list);
523 for (i=0; p != (const LocaleInfo *) NULL; )
524 {
525 if ((p->stealth == MagickFalse) &&
526 (GlobExpression(p->tag,pattern,MagickTrue) != MagickFalse))
527 messages[i++]=p;
528 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_list);
529 }
cristyf84a1932010-01-03 18:00:18 +0000530 UnlockSemaphoreInfo(locale_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000531 qsort((void *) messages,(size_t) i,sizeof(*messages),LocaleInfoCompare);
532 messages[i]=(LocaleInfo *) NULL;
cristybb503372010-05-27 20:51:26 +0000533 *number_messages=(size_t) i;
cristy3ed852e2009-09-05 21:47:34 +0000534 return(messages);
535}
536
537/*
538%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
539% %
540% %
541% %
542% G e t L o c a l e L i s t %
543% %
544% %
545% %
546%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
547%
548% GetLocaleList() returns any locale messages that match the specified
549% pattern.
550%
551% The format of the GetLocaleList function is:
552%
cristybb503372010-05-27 20:51:26 +0000553% char **GetLocaleList(const char *pattern,size_t *number_messages,
cristy3ed852e2009-09-05 21:47:34 +0000554% Exceptioninfo *exception)
555%
556% A description of each parameter follows:
557%
558% o pattern: Specifies a pointer to a text string containing a pattern.
559%
560% o number_messages: This integer returns the number of messages in the
561% list.
562%
563% o exception: return any errors or warnings in this structure.
564%
565*/
566
567#if defined(__cplusplus) || defined(c_plusplus)
568extern "C" {
569#endif
570
571static int LocaleTagCompare(const void *x,const void *y)
572{
573 register char
574 **p,
575 **q;
576
577 p=(char **) x;
578 q=(char **) y;
579 return(LocaleCompare(*p,*q));
580}
581
582#if defined(__cplusplus) || defined(c_plusplus)
583}
584#endif
585
586MagickExport char **GetLocaleList(const char *pattern,
cristybb503372010-05-27 20:51:26 +0000587 size_t *number_messages,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +0000588{
589 char
590 **messages;
591
592 register const LocaleInfo
593 *p;
594
cristybb503372010-05-27 20:51:26 +0000595 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000596 i;
597
598 /*
599 Allocate locale list.
600 */
601 assert(pattern != (char *) NULL);
602 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
cristybb503372010-05-27 20:51:26 +0000603 assert(number_messages != (size_t *) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000604 *number_messages=0;
605 p=GetLocaleInfo_("*",exception);
606 if (p == (const LocaleInfo *) NULL)
607 return((char **) NULL);
cristy3ed852e2009-09-05 21:47:34 +0000608 messages=(char **) AcquireQuantumMemory((size_t)
609 GetNumberOfNodesInSplayTree(locale_list)+1UL,sizeof(*messages));
610 if (messages == (char **) NULL)
611 return((char **) NULL);
cristyf84a1932010-01-03 18:00:18 +0000612 LockSemaphoreInfo(locale_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000613 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_list);
614 for (i=0; p != (const LocaleInfo *) NULL; )
615 {
616 if ((p->stealth == MagickFalse) &&
617 (GlobExpression(p->tag,pattern,MagickTrue) != MagickFalse))
618 messages[i++]=ConstantString(p->tag);
619 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_list);
620 }
cristyf84a1932010-01-03 18:00:18 +0000621 UnlockSemaphoreInfo(locale_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000622 qsort((void *) messages,(size_t) i,sizeof(*messages),LocaleTagCompare);
623 messages[i]=(char *) NULL;
cristybb503372010-05-27 20:51:26 +0000624 *number_messages=(size_t) i;
cristy3ed852e2009-09-05 21:47:34 +0000625 return(messages);
626}
627
628/*
629%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
630% %
631% %
632% %
633% G e t L o c a l e M e s s a g e %
634% %
635% %
636% %
637%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
638%
639% GetLocaleMessage() returns a message in the current locale that matches the
640% supplied tag.
641%
642% The format of the GetLocaleMessage method is:
643%
644% const char *GetLocaleMessage(const char *tag)
645%
646% A description of each parameter follows:
647%
648% o tag: Return a message that matches this tag in the current locale.
649%
650*/
651MagickExport const char *GetLocaleMessage(const char *tag)
652{
653 char
654 name[MaxTextExtent];
655
656 const LocaleInfo
657 *locale_info;
658
659 ExceptionInfo
660 *exception;
661
662 if ((tag == (const char *) NULL) || (*tag == '\0'))
663 return(tag);
664 exception=AcquireExceptionInfo();
cristyb51dff52011-05-19 16:55:47 +0000665 (void) FormatLocaleString(name,MaxTextExtent,"%s/",tag);
cristy3ed852e2009-09-05 21:47:34 +0000666 locale_info=GetLocaleInfo_(name,exception);
667 exception=DestroyExceptionInfo(exception);
668 if (locale_info != (const LocaleInfo *) NULL)
669 return(locale_info->message);
670 return(tag);
671}
672
673/*
674%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
675% %
676% %
677% %
678% G e t L o c a l e O p t i o n s %
679% %
680% %
681% %
682%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
683%
684% GetLocaleOptions() returns any Magick configuration messages associated
685% with the specified filename.
686%
687% The format of the GetLocaleOptions method is:
688%
689% LinkedListInfo *GetLocaleOptions(const char *filename,
690% ExceptionInfo *exception)
691%
692% A description of each parameter follows:
693%
694% o filename: the locale file tag.
695%
696% o exception: return any errors or warnings in this structure.
697%
698*/
699MagickExport LinkedListInfo *GetLocaleOptions(const char *filename,
700 ExceptionInfo *exception)
701{
702 char
703 path[MaxTextExtent];
704
705 const char
706 *element;
707
708 LinkedListInfo
709 *messages,
710 *paths;
711
712 StringInfo
713 *xml;
714
715 assert(filename != (const char *) NULL);
716 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
717 assert(exception != (ExceptionInfo *) NULL);
718 (void) CopyMagickString(path,filename,MaxTextExtent);
719 /*
720 Load XML from configuration files to linked-list.
721 */
722 messages=NewLinkedList(0);
723 paths=GetConfigurePaths(filename,exception);
724 if (paths != (LinkedListInfo *) NULL)
725 {
726 ResetLinkedListIterator(paths);
727 element=(const char *) GetNextValueInLinkedList(paths);
728 while (element != (const char *) NULL)
729 {
cristyb51dff52011-05-19 16:55:47 +0000730 (void) FormatLocaleString(path,MaxTextExtent,"%s%s",element,filename);
cristy3ed852e2009-09-05 21:47:34 +0000731 (void) LogMagickEvent(LocaleEvent,GetMagickModule(),
732 "Searching for locale file: \"%s\"",path);
733 xml=ConfigureFileToStringInfo(path);
734 if (xml != (StringInfo *) NULL)
735 (void) AppendValueToLinkedList(messages,xml);
736 element=(const char *) GetNextValueInLinkedList(paths);
737 }
738 paths=DestroyLinkedList(paths,RelinquishMagickMemory);
739 }
cristy0157aea2010-04-24 21:12:18 +0000740#if defined(MAGICKCORE_WINDOWS_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +0000741 {
742 char
743 *blob;
744
745 blob=(char *) NTResourceToBlob(filename);
746 if (blob != (char *) NULL)
747 {
cristy85d6de52011-11-18 15:49:27 +0000748 xml=AcquireStringInfo(0);
749 SetStringInfoLength(xml,strlen(blob)+1);
750 SetStringInfoDatum(xml,blob);
751 SetStringInfoPath(xml,filename);
cristy3ed852e2009-09-05 21:47:34 +0000752 (void) AppendValueToLinkedList(messages,xml);
cristy3ed852e2009-09-05 21:47:34 +0000753 }
754 }
755#endif
756 ResetLinkedListIterator(messages);
757 return(messages);
758}
759
760/*
761%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
762% %
763% %
764% %
765% G e t L o c a l e V a l u e %
766% %
767% %
768% %
769%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
770%
771% GetLocaleValue() returns the message associated with the locale info.
772%
773% The format of the GetLocaleValue method is:
774%
775% const char *GetLocaleValue(const LocaleInfo *locale_info)
776%
777% A description of each parameter follows:
778%
779% o locale_info: The locale info.
780%
781*/
782MagickExport const char *GetLocaleValue(const LocaleInfo *locale_info)
783{
784 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
785 assert(locale_info != (LocaleInfo *) NULL);
786 assert(locale_info->signature == MagickSignature);
787 return(locale_info->message);
788}
789
790/*
791%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
792% %
793% %
794% %
795+ I n i t i a l i z e L o c a l e L i s t %
796% %
797% %
798% %
799%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
800%
801% InitializeLocaleList() initializes the locale list.
802%
803% The format of the InitializeLocaleList method is:
804%
805% MagickBooleanType InitializeLocaleList(ExceptionInfo *exception)
806%
807% A description of each parameter follows.
808%
809% o exception: return any errors or warnings in this structure.
810%
811*/
812static MagickBooleanType InitializeLocaleList(ExceptionInfo *exception)
813{
814 if ((locale_list == (SplayTreeInfo *) NULL) &&
815 (instantiate_locale == MagickFalse))
816 {
cristy4e1dff62009-10-25 20:36:03 +0000817 if (locale_semaphore == (SemaphoreInfo *) NULL)
818 AcquireSemaphoreInfo(&locale_semaphore);
cristyf84a1932010-01-03 18:00:18 +0000819 LockSemaphoreInfo(locale_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000820 if ((locale_list == (SplayTreeInfo *) NULL) &&
821 (instantiate_locale == MagickFalse))
822 {
823 char
824 *locale;
825
826 register const char
827 *p;
828
829 locale=(char *) NULL;
830 p=setlocale(LC_CTYPE,(const char *) NULL);
831 if (p != (const char *) NULL)
832 locale=ConstantString(p);
833 if (locale == (char *) NULL)
834 locale=GetEnvironmentValue("LC_ALL");
835 if (locale == (char *) NULL)
836 locale=GetEnvironmentValue("LC_MESSAGES");
837 if (locale == (char *) NULL)
838 locale=GetEnvironmentValue("LC_CTYPE");
839 if (locale == (char *) NULL)
840 locale=GetEnvironmentValue("LANG");
841 if (locale == (char *) NULL)
842 locale=ConstantString("C");
843 (void) LoadLocaleLists(LocaleFilename,locale,exception);
844 locale=DestroyString(locale);
845 instantiate_locale=MagickTrue;
846 }
cristyf84a1932010-01-03 18:00:18 +0000847 UnlockSemaphoreInfo(locale_semaphore);
cristy3ed852e2009-09-05 21:47:34 +0000848 }
849 return(locale_list != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
850}
851
852/*
853%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
854% %
855% %
856% %
cristyc1acd842011-05-19 23:05:47 +0000857+ I n t e r p r e t L o c a l e V a l u e %
858% %
859% %
860% %
861%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
862%
863% InterpretLocaleValue() interprets the string as a floating point number in
864% the "C" locale and returns its value as a double. If sentinal is not a null
865% pointer, the method also sets the value pointed by sentinal to point to the
866% first character after the number.
867%
868% The format of the InterpretLocaleValue method is:
869%
870% double InterpretLocaleValue(const char *value,char **sentinal)
871%
872% A description of each parameter follows:
873%
874% o value: the string value.
875%
876% o sentinal: if sentinal is not NULL, a pointer to the character after the
877% last character used in the conversion is stored in the location
878% referenced by sentinal.
879%
880*/
cristyba56db52011-05-20 02:08:11 +0000881MagickExport double InterpretLocaleValue(const char *restrict string,
882 char **restrict sentinal)
cristyc1acd842011-05-19 23:05:47 +0000883{
cristy670aa3c2011-11-03 00:54:00 +0000884 char
885 *q;
886
cristyc1acd842011-05-19 23:05:47 +0000887 double
888 value;
889
cristy670aa3c2011-11-03 00:54:00 +0000890 if ((*string == '0') && ((string[1] | 0x20)=='x'))
891 value=(double) strtoul(string,&q,16);
892 else
893 {
894#if defined(MAGICKCORE_HAVE_STRTOD_L)
895 locale_t
896 locale;
897
898 locale=AcquireCLocale();
899 if (locale == (locale_t) NULL)
900 value=strtod(string,&q);
901 else
902 value=strtod_l(string,&q,locale);
cristyc1acd842011-05-19 23:05:47 +0000903#else
cristy670aa3c2011-11-03 00:54:00 +0000904 value=strtod(string,&q);
cristyc1acd842011-05-19 23:05:47 +0000905#endif
cristy670aa3c2011-11-03 00:54:00 +0000906 }
cristy670aa3c2011-11-03 00:54:00 +0000907 if (sentinal != (char **) NULL)
908 *sentinal=q;
cristyc1acd842011-05-19 23:05:47 +0000909 return(value);
910}
911
912/*
913%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
914% %
915% %
916% %
cristy3ed852e2009-09-05 21:47:34 +0000917% L i s t L o c a l e I n f o %
918% %
919% %
920% %
921%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
922%
923% ListLocaleInfo() lists the locale info to a file.
924%
925% The format of the ListLocaleInfo method is:
926%
927% MagickBooleanType ListLocaleInfo(FILE *file,ExceptionInfo *exception)
928%
929% A description of each parameter follows.
930%
931% o file: An pointer to a FILE.
932%
933% o exception: return any errors or warnings in this structure.
934%
935*/
936MagickExport MagickBooleanType ListLocaleInfo(FILE *file,
937 ExceptionInfo *exception)
938{
939 const char
940 *path;
941
942 const LocaleInfo
943 **locale_info;
944
cristybb503372010-05-27 20:51:26 +0000945 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000946 i;
947
cristybb503372010-05-27 20:51:26 +0000948 size_t
cristy3ed852e2009-09-05 21:47:34 +0000949 number_messages;
950
951 if (file == (const FILE *) NULL)
952 file=stdout;
953 number_messages=0;
954 locale_info=GetLocaleInfoList("*",&number_messages,exception);
955 if (locale_info == (const LocaleInfo **) NULL)
956 return(MagickFalse);
957 path=(const char *) NULL;
cristybb503372010-05-27 20:51:26 +0000958 for (i=0; i < (ssize_t) number_messages; i++)
cristy3ed852e2009-09-05 21:47:34 +0000959 {
960 if (locale_info[i]->stealth != MagickFalse)
961 continue;
962 if ((path == (const char *) NULL) ||
963 (LocaleCompare(path,locale_info[i]->path) != 0))
964 {
965 if (locale_info[i]->path != (char *) NULL)
cristyb51dff52011-05-19 16:55:47 +0000966 (void) FormatLocaleFile(file,"\nPath: %s\n\n",locale_info[i]->path);
967 (void) FormatLocaleFile(file,"Tag/Message\n");
cristy1e604812011-05-19 18:07:50 +0000968 (void) FormatLocaleFile(file,
969 "-------------------------------------------------"
cristy3ed852e2009-09-05 21:47:34 +0000970 "------------------------------\n");
971 }
972 path=locale_info[i]->path;
cristyb51dff52011-05-19 16:55:47 +0000973 (void) FormatLocaleFile(file,"%s\n",locale_info[i]->tag);
cristy3ed852e2009-09-05 21:47:34 +0000974 if (locale_info[i]->message != (char *) NULL)
cristyb51dff52011-05-19 16:55:47 +0000975 (void) FormatLocaleFile(file," %s",locale_info[i]->message);
976 (void) FormatLocaleFile(file,"\n");
cristy3ed852e2009-09-05 21:47:34 +0000977 }
978 (void) fflush(file);
979 locale_info=(const LocaleInfo **)
980 RelinquishMagickMemory((void *) locale_info);
981 return(MagickTrue);
982}
983
984/*
985%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
986% %
987% %
988% %
989+ L o a d L o c a l e L i s t %
990% %
991% %
992% %
993%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
994%
995% LoadLocaleList() loads the locale configuration file which provides a mapping
996% between locale attributes and a locale name.
997%
998% The format of the LoadLocaleList method is:
999%
1000% MagickBooleanType LoadLocaleList(const char *xml,const char *filename,
cristybb503372010-05-27 20:51:26 +00001001% const size_t depth,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001002%
1003% A description of each parameter follows:
1004%
1005% o xml: The locale list in XML format.
1006%
1007% o filename: The locale list filename.
1008%
1009% o depth: depth of <include /> statements.
1010%
1011% o exception: return any errors or warnings in this structure.
1012%
1013*/
1014
cristybb503372010-05-27 20:51:26 +00001015static void ChopLocaleComponents(char *path,const size_t components)
cristy3ed852e2009-09-05 21:47:34 +00001016{
cristy3ed852e2009-09-05 21:47:34 +00001017 register char
1018 *p;
1019
cristy9d314ff2011-03-09 01:30:28 +00001020 ssize_t
1021 count;
1022
cristy3ed852e2009-09-05 21:47:34 +00001023 if (*path == '\0')
1024 return;
1025 p=path+strlen(path)-1;
1026 if (*p == '/')
1027 *p='\0';
cristybb503372010-05-27 20:51:26 +00001028 for (count=0; (count < (ssize_t) components) && (p > path); p--)
cristy3ed852e2009-09-05 21:47:34 +00001029 if (*p == '/')
1030 {
1031 *p='\0';
1032 count++;
1033 }
cristybb503372010-05-27 20:51:26 +00001034 if (count < (ssize_t) components)
cristy3ed852e2009-09-05 21:47:34 +00001035 *path='\0';
1036}
1037
1038static void *DestroyLocaleNode(void *locale_info)
1039{
1040 register LocaleInfo
1041 *p;
1042
1043 p=(LocaleInfo *) locale_info;
1044 if (p->path != (char *) NULL)
1045 p->path=DestroyString(p->path);
1046 if (p->tag != (char *) NULL)
1047 p->tag=DestroyString(p->tag);
1048 if (p->message != (char *) NULL)
1049 p->message=DestroyString(p->message);
1050 return(RelinquishMagickMemory(p));
1051}
1052
cristy2359e742009-11-24 14:50:02 +00001053static void LocaleFatalErrorHandler(
1054 const ExceptionType magick_unused(severity),
1055 const char *reason,const char *description)
1056{
1057 if (reason == (char *) NULL)
1058 return;
cristyb51dff52011-05-19 16:55:47 +00001059 (void) FormatLocaleFile(stderr,"%s: %s",GetClientName(),reason);
cristy2359e742009-11-24 14:50:02 +00001060 if (description != (char *) NULL)
cristyb51dff52011-05-19 16:55:47 +00001061 (void) FormatLocaleFile(stderr," (%s)",description);
1062 (void) FormatLocaleFile(stderr,".\n");
cristy2359e742009-11-24 14:50:02 +00001063 (void) fflush(stderr);
1064 exit(1);
1065}
1066
1067
cristy3ed852e2009-09-05 21:47:34 +00001068static MagickBooleanType LoadLocaleList(const char *xml,const char *filename,
cristybb503372010-05-27 20:51:26 +00001069 const char *locale,const size_t depth,ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00001070{
1071 char
1072 keyword[MaxTextExtent],
1073 message[MaxTextExtent],
1074 tag[MaxTextExtent],
1075 *token;
1076
1077 const char
1078 *q;
1079
cristy909b6472009-11-24 14:45:00 +00001080 FatalErrorHandler
1081 fatal_handler;
1082
cristy3ed852e2009-09-05 21:47:34 +00001083 LocaleInfo
1084 *locale_info;
1085
1086 MagickBooleanType
1087 status;
1088
1089 register char
1090 *p;
1091
1092 /*
1093 Read the locale configure file.
1094 */
1095 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1096 "Loading locale configure file \"%s\" ...",filename);
1097 if (xml == (const char *) NULL)
1098 return(MagickFalse);
1099 if (locale_list == (SplayTreeInfo *) NULL)
1100 {
1101 locale_list=NewSplayTree(CompareSplayTreeString,(void *(*)(void *)) NULL,
1102 DestroyLocaleNode);
1103 if (locale_list == (SplayTreeInfo *) NULL)
cristy909b6472009-11-24 14:45:00 +00001104 return(MagickFalse);
cristy3ed852e2009-09-05 21:47:34 +00001105 }
1106 status=MagickTrue;
1107 locale_info=(LocaleInfo *) NULL;
1108 *tag='\0';
1109 *message='\0';
1110 *keyword='\0';
cristy2359e742009-11-24 14:50:02 +00001111 fatal_handler=SetFatalErrorHandler(LocaleFatalErrorHandler);
cristy3ed852e2009-09-05 21:47:34 +00001112 token=AcquireString(xml);
1113 for (q=(char *) xml; *q != '\0'; )
1114 {
1115 /*
1116 Interpret XML.
1117 */
1118 GetMagickToken(q,&q,token);
1119 if (*token == '\0')
1120 break;
1121 (void) CopyMagickString(keyword,token,MaxTextExtent);
1122 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
1123 {
1124 /*
1125 Doctype element.
1126 */
1127 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
1128 {
1129 GetMagickToken(q,&q,token);
1130 while (isspace((int) ((unsigned char) *q)) != 0)
1131 q++;
1132 }
1133 continue;
1134 }
1135 if (LocaleNCompare(keyword,"<!--",4) == 0)
1136 {
1137 /*
1138 Comment element.
1139 */
1140 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
1141 {
1142 GetMagickToken(q,&q,token);
1143 while (isspace((int) ((unsigned char) *q)) != 0)
1144 q++;
1145 }
1146 continue;
1147 }
1148 if (LocaleCompare(keyword,"<include") == 0)
1149 {
1150 /*
1151 Include element.
1152 */
1153 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
1154 {
1155 (void) CopyMagickString(keyword,token,MaxTextExtent);
1156 GetMagickToken(q,&q,token);
1157 if (*token != '=')
1158 continue;
1159 GetMagickToken(q,&q,token);
1160 if (LocaleCompare(keyword,"locale") == 0)
1161 {
1162 if (LocaleCompare(locale,token) != 0)
1163 break;
1164 continue;
1165 }
1166 if (LocaleCompare(keyword,"file") == 0)
1167 {
1168 if (depth > 200)
1169 (void) ThrowMagickException(exception,GetMagickModule(),
1170 ConfigureError,"IncludeElementNestedTooDeeply","`%s'",token);
1171 else
1172 {
1173 char
1174 path[MaxTextExtent],
1175 *xml;
1176
1177 *path='\0';
1178 GetPathComponent(filename,HeadPath,path);
1179 if (*path != '\0')
1180 (void) ConcatenateMagickString(path,DirectorySeparator,
1181 MaxTextExtent);
1182 if (*token == *DirectorySeparator)
1183 (void) CopyMagickString(path,token,MaxTextExtent);
1184 else
1185 (void) ConcatenateMagickString(path,token,MaxTextExtent);
1186 xml=FileToString(path,~0,exception);
1187 if (xml != (char *) NULL)
1188 {
1189 status=LoadLocaleList(xml,path,locale,depth+1,exception);
1190 xml=(char *) RelinquishMagickMemory(xml);
1191 }
1192 }
1193 }
1194 }
1195 continue;
1196 }
1197 if (LocaleCompare(keyword,"<locale") == 0)
1198 {
1199 /*
1200 Locale element.
1201 */
1202 while ((*token != '>') && (*q != '\0'))
1203 {
1204 (void) CopyMagickString(keyword,token,MaxTextExtent);
1205 GetMagickToken(q,&q,token);
1206 if (*token != '=')
1207 continue;
1208 GetMagickToken(q,&q,token);
1209 }
1210 continue;
1211 }
1212 if (LocaleCompare(keyword,"</locale>") == 0)
1213 {
1214 ChopLocaleComponents(tag,1);
1215 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1216 continue;
1217 }
1218 if (LocaleCompare(keyword,"<localemap>") == 0)
1219 continue;
1220 if (LocaleCompare(keyword,"</localemap>") == 0)
1221 continue;
1222 if (LocaleCompare(keyword,"<message") == 0)
1223 {
1224 /*
1225 Message element.
1226 */
1227 while ((*token != '>') && (*q != '\0'))
1228 {
1229 (void) CopyMagickString(keyword,token,MaxTextExtent);
1230 GetMagickToken(q,&q,token);
1231 if (*token != '=')
1232 continue;
1233 GetMagickToken(q,&q,token);
1234 if (LocaleCompare(keyword,"name") == 0)
1235 {
1236 (void) ConcatenateMagickString(tag,token,MaxTextExtent);
1237 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1238 }
1239 }
1240 for (p=(char *) q; (*q != '<') && (*q != '\0'); q++) ;
1241 while (isspace((int) ((unsigned char) *p)) != 0)
1242 p++;
1243 q--;
1244 while ((isspace((int) ((unsigned char) *q)) != 0) && (q > p))
1245 q--;
1246 (void) CopyMagickString(message,p,(size_t) (q-p+2));
cristy73bd4a52010-10-05 11:24:23 +00001247 locale_info=(LocaleInfo *) AcquireMagickMemory(sizeof(*locale_info));
cristy3ed852e2009-09-05 21:47:34 +00001248 if (locale_info == (LocaleInfo *) NULL)
1249 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1250 (void) ResetMagickMemory(locale_info,0,sizeof(*locale_info));
1251 locale_info->path=ConstantString(filename);
1252 locale_info->tag=ConstantString(tag);
1253 locale_info->message=ConstantString(message);
1254 locale_info->signature=MagickSignature;
1255 status=AddValueToSplayTree(locale_list,locale_info->tag,locale_info);
1256 if (status == MagickFalse)
1257 (void) ThrowMagickException(exception,GetMagickModule(),
1258 ResourceLimitError,"MemoryAllocationFailed","`%s'",
1259 locale_info->tag);
1260 (void) ConcatenateMagickString(tag,message,MaxTextExtent);
1261 (void) ConcatenateMagickString(tag,"\n",MaxTextExtent);
1262 q++;
1263 continue;
1264 }
1265 if (LocaleCompare(keyword,"</message>") == 0)
1266 {
1267 ChopLocaleComponents(tag,2);
1268 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1269 continue;
1270 }
1271 if (*keyword == '<')
1272 {
1273 /*
1274 Subpath element.
1275 */
1276 if (*(keyword+1) == '?')
1277 continue;
1278 if (*(keyword+1) == '/')
1279 {
1280 ChopLocaleComponents(tag,1);
1281 if (*tag != '\0')
1282 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1283 continue;
1284 }
1285 token[strlen(token)-1]='\0';
1286 (void) CopyMagickString(token,token+1,MaxTextExtent);
1287 (void) ConcatenateMagickString(tag,token,MaxTextExtent);
1288 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1289 continue;
1290 }
1291 GetMagickToken(q,(const char **) NULL,token);
1292 if (*token != '=')
1293 continue;
1294 }
1295 token=(char *) RelinquishMagickMemory(token);
cristy909b6472009-11-24 14:45:00 +00001296 (void) SetFatalErrorHandler(fatal_handler);
cristy3ed852e2009-09-05 21:47:34 +00001297 return(status);
1298}
1299
1300/*
1301%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1302% %
1303% %
1304% %
1305% L o a d L o c a l e L i s t s %
1306% %
1307% %
1308% %
1309%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1310%
1311% LoadLocaleList() loads one or more locale configuration file which
1312% provides a mapping between locale attributes and a locale tag.
1313%
1314% The format of the LoadLocaleLists method is:
1315%
1316% MagickBooleanType LoadLocaleLists(const char *filename,
1317% ExceptionInfo *exception)
1318%
1319% A description of each parameter follows:
1320%
1321% o filename: the font file tag.
1322%
1323% o locale: the actual locale.
1324%
1325% o exception: return any errors or warnings in this structure.
1326%
1327*/
1328static MagickBooleanType LoadLocaleLists(const char *filename,
1329 const char *locale,ExceptionInfo *exception)
1330{
cristy6e3607c2011-09-13 13:59:17 +00001331#if defined(MAGICKCORE_ZERO_CONFIGURATION_SUPPORT)
cristy3ed852e2009-09-05 21:47:34 +00001332 return(LoadLocaleList(LocaleMap,"built-in",locale,0,exception));
1333#else
1334 const StringInfo
1335 *option;
1336
1337 LinkedListInfo
1338 *options;
1339
1340 MagickStatusType
1341 status;
1342
1343 status=MagickFalse;
1344 options=GetLocaleOptions(filename,exception);
1345 option=(const StringInfo *) GetNextValueInLinkedList(options);
1346 while (option != (const StringInfo *) NULL)
1347 {
1348 status|=LoadLocaleList((const char *) GetStringInfoDatum(option),
1349 GetStringInfoPath(option),locale,0,exception);
1350 option=(const StringInfo *) GetNextValueInLinkedList(options);
1351 }
1352 options=DestroyLocaleOptions(options);
1353 if ((locale_list == (SplayTreeInfo *) NULL) ||
1354 (GetNumberOfNodesInSplayTree(locale_list) == 0))
1355 {
1356 options=GetLocaleOptions("english.xml",exception);
1357 option=(const StringInfo *) GetNextValueInLinkedList(options);
1358 while (option != (const StringInfo *) NULL)
1359 {
1360 status|=LoadLocaleList((const char *) GetStringInfoDatum(option),
1361 GetStringInfoPath(option),locale,0,exception);
1362 option=(const StringInfo *) GetNextValueInLinkedList(options);
1363 }
1364 options=DestroyLocaleOptions(options);
1365 }
1366 if ((locale_list == (SplayTreeInfo *) NULL) ||
1367 (GetNumberOfNodesInSplayTree(locale_list) == 0))
1368 status|=LoadLocaleList(LocaleMap,"built-in",locale,0,exception);
1369 return(status != 0 ? MagickTrue : MagickFalse);
1370#endif
1371}
cristyf34a1452009-10-24 22:29:27 +00001372
1373/*
1374%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1375% %
1376% %
1377% %
1378+ L o c a l e C o m p o n e n t G e n e s i s %
1379% %
1380% %
1381% %
1382%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1383%
1384% LocaleComponentGenesis() instantiates the locale component.
1385%
1386% The format of the LocaleComponentGenesis method is:
1387%
1388% MagickBooleanType LocaleComponentGenesis(void)
1389%
1390*/
cristy5ff4eaf2011-09-03 01:38:02 +00001391MagickPrivate MagickBooleanType LocaleComponentGenesis(void)
cristyf34a1452009-10-24 22:29:27 +00001392{
cristy165b6092009-10-26 13:52:10 +00001393 AcquireSemaphoreInfo(&locale_semaphore);
cristyf34a1452009-10-24 22:29:27 +00001394 return(MagickTrue);
1395}
1396
1397/*
1398%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1399% %
1400% %
1401% %
1402+ L o c a l e C o m p o n e n t T e r m i n u s %
1403% %
1404% %
1405% %
1406%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1407%
1408% LocaleComponentTerminus() destroys the locale component.
1409%
1410% The format of the LocaleComponentTerminus method is:
1411%
1412% LocaleComponentTerminus(void)
1413%
1414*/
cristy5ff4eaf2011-09-03 01:38:02 +00001415MagickPrivate void LocaleComponentTerminus(void)
cristyf34a1452009-10-24 22:29:27 +00001416{
cristy18b17442009-10-25 18:36:48 +00001417 if (locale_semaphore == (SemaphoreInfo *) NULL)
1418 AcquireSemaphoreInfo(&locale_semaphore);
cristyf84a1932010-01-03 18:00:18 +00001419 LockSemaphoreInfo(locale_semaphore);
cristya723b6a2011-05-25 12:08:51 +00001420#if defined(MAGICKCORE_HAVE_STRTOD_L)
cristyb51dff52011-05-19 16:55:47 +00001421 DestroyCLocale();
cristya723b6a2011-05-25 12:08:51 +00001422#endif
cristyf34a1452009-10-24 22:29:27 +00001423 instantiate_locale=MagickFalse;
cristyf84a1932010-01-03 18:00:18 +00001424 UnlockSemaphoreInfo(locale_semaphore);
cristyf34a1452009-10-24 22:29:27 +00001425 DestroySemaphoreInfo(&locale_semaphore);
1426}