]> granicus.if.org Git - imagemagick/blob - MagickCore/locale.c
Removed MagickMax/MagickMin inline methods.
[imagemagick] / MagickCore / locale.c
1 /*
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 %                                   Cristy                                    %
17 %                                 July 2003                                   %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2015 ImageMagick Studio LLC, a non-profit organization      %
21 %  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 \f
39 /*
40   Include declarations.
41 */
42 #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/image-private.h"
50 #include "MagickCore/locale_.h"
51 #include "MagickCore/locale-private.h"
52 #include "MagickCore/log.h"
53 #include "MagickCore/memory_.h"
54 #include "MagickCore/nt-base-private.h"
55 #include "MagickCore/semaphore.h"
56 #include "MagickCore/splay-tree.h"
57 #include "MagickCore/string_.h"
58 #include "MagickCore/string-private.h"
59 #include "MagickCore/token.h"
60 #include "MagickCore/utility.h"
61 #include "MagickCore/utility-private.h"
62 #include "MagickCore/xml-tree.h"
63 #include "MagickCore/xml-tree-private.h"
64 \f
65 /*
66   Define declarations.
67 */
68 #define LocaleFilename  "locale.xml"
69 #define MaxRecursionDepth  200
70 \f
71 /*
72   Typedef declarations.
73 */
74 #if defined(__CYGWIN__)
75 typedef struct _locale_t
76   *locale_t;
77 #endif
78 \f
79 /*
80   Static declarations.
81 */
82 static const char
83   *LocaleMap =
84     "<?xml version=\"1.0\"?>"
85     "<localemap>"
86     "  <locale name=\"C\">"
87     "    <Exception>"
88     "     <Message name=\"\">"
89     "     </Message>"
90     "    </Exception>"
91     "  </locale>"
92     "</localemap>";
93
94 static SemaphoreInfo
95   *locale_semaphore = (SemaphoreInfo *) NULL;
96
97 static SplayTreeInfo
98   *locale_cache = (SplayTreeInfo *) NULL;
99
100 #if defined(MAGICKCORE_HAVE_STRTOD_L)
101 static volatile locale_t
102   c_locale = (locale_t) NULL;
103 #endif
104 \f
105 /*
106   Forward declarations.
107 */
108 static MagickBooleanType
109   IsLocaleTreeInstantiated(ExceptionInfo *),
110   LoadLocaleCache(SplayTreeInfo *,const char *,const char *,const char *,
111     const size_t,ExceptionInfo *);
112 \f
113 #if defined(MAGICKCORE_HAVE_STRTOD_L)
114 /*
115 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
116 %                                                                             %
117 %                                                                             %
118 %                                                                             %
119 +   A c q u i r e C L o c a l e                                               %
120 %                                                                             %
121 %                                                                             %
122 %                                                                             %
123 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
124 %
125 %  AcquireCLocale() allocates the C locale object, or (locale_t) 0 with
126 %  errno set if it cannot be acquired.
127 %
128 %  The format of the AcquireCLocale method is:
129 %
130 %      locale_t AcquireCLocale(void)
131 %
132 */
133 static locale_t AcquireCLocale(void)
134 {
135 #if defined(MAGICKCORE_HAVE_NEWLOCALE)
136   if (c_locale == (locale_t) NULL)
137     c_locale=newlocale(LC_ALL_MASK,"C",(locale_t) 0);
138 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
139   if (c_locale == (locale_t) NULL)
140     c_locale=_create_locale(LC_ALL,"C");
141 #endif
142   return(c_locale);
143 }
144 #endif
145 \f
146 /*
147 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
148 %                                                                             %
149 %                                                                             %
150 %                                                                             %
151 %  A c q u i r e L o c a l e S p l a y T r e e                                %
152 %                                                                             %
153 %                                                                             %
154 %                                                                             %
155 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
156 %
157 %  AcquireLocaleSplayTree() caches one or more locale configurations which
158 %  provides a mapping between locale attributes and a locale tag.
159 %
160 %  The format of the AcquireLocaleSplayTree method is:
161 %
162 %      SplayTreeInfo *AcquireLocaleSplayTree(const char *filename,
163 %        ExceptionInfo *exception)
164 %
165 %  A description of each parameter follows:
166 %
167 %    o filename: the font file tag.
168 %
169 %    o locale: the actual locale.
170 %
171 %    o exception: return any errors or warnings in this structure.
172 %
173 */
174
175 static void *DestroyLocaleNode(void *locale_info)
176 {
177   register LocaleInfo
178     *p;
179
180   p=(LocaleInfo *) locale_info;
181   if (p->path != (char *) NULL)
182     p->path=DestroyString(p->path);
183   if (p->tag != (char *) NULL)
184     p->tag=DestroyString(p->tag);
185   if (p->message != (char *) NULL)
186     p->message=DestroyString(p->message);
187   return(RelinquishMagickMemory(p));
188 }
189
190 static SplayTreeInfo *AcquireLocaleSplayTree(const char *filename,
191   const char *locale,ExceptionInfo *exception)
192 {
193   MagickStatusType
194     status;
195
196   SplayTreeInfo
197     *locale_cache;
198
199   locale_cache=NewSplayTree(CompareSplayTreeString,(void *(*)(void *)) NULL,
200     DestroyLocaleNode);
201   if (locale_cache == (SplayTreeInfo *) NULL)
202     ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
203   status=MagickTrue;
204 #if !defined(MAGICKCORE_ZERO_CONFIGURATION_SUPPORT)
205   {
206     const StringInfo
207       *option;
208
209     LinkedListInfo
210       *options;
211
212     options=GetLocaleOptions(filename,exception);
213     option=(const StringInfo *) GetNextValueInLinkedList(options);
214     while (option != (const StringInfo *) NULL)
215     {
216       status&=LoadLocaleCache(locale_cache,(const char *)
217         GetStringInfoDatum(option),GetStringInfoPath(option),locale,0,
218         exception);
219       option=(const StringInfo *) GetNextValueInLinkedList(options);
220     }
221     options=DestroyLocaleOptions(options);
222     if (GetNumberOfNodesInSplayTree(locale_cache) == 0)
223       {
224         options=GetLocaleOptions("english.xml",exception);
225         option=(const StringInfo *) GetNextValueInLinkedList(options);
226         while (option != (const StringInfo *) NULL)
227         {
228           status&=LoadLocaleCache(locale_cache,(const char *)
229             GetStringInfoDatum(option),GetStringInfoPath(option),locale,0,
230             exception);
231           option=(const StringInfo *) GetNextValueInLinkedList(options);
232         }
233         options=DestroyLocaleOptions(options);
234       }
235   }
236 #endif
237   if (GetNumberOfNodesInSplayTree(locale_cache) == 0)
238     status&=LoadLocaleCache(locale_cache,LocaleMap,"built-in",locale,0,
239       exception);
240   return(locale_cache);
241 }
242 \f
243 #if defined(MAGICKCORE_HAVE_STRTOD_L)
244 /*
245 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
246 %                                                                             %
247 %                                                                             %
248 %                                                                             %
249 +   D e s t r o y C L o c a l e                                               %
250 %                                                                             %
251 %                                                                             %
252 %                                                                             %
253 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
254 %
255 %  DestroyCLocale() releases the resources allocated for a locale object
256 %  returned by a call to the AcquireCLocale() method.
257 %
258 %  The format of the DestroyCLocale method is:
259 %
260 %      void DestroyCLocale(void)
261 %
262 */
263 static void DestroyCLocale(void)
264 {
265 #if defined(MAGICKCORE_HAVE_NEWLOCALE)
266   if (c_locale != (locale_t) NULL)
267     freelocale(c_locale);
268 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
269   if (c_locale != (locale_t) NULL)
270     _free_locale(c_locale);
271 #endif
272   c_locale=(locale_t) NULL;
273 }
274 #endif
275 \f
276 /*
277 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
278 %                                                                             %
279 %                                                                             %
280 %                                                                             %
281 %   D e s t r o y L o c a l e O p t i o n s                                   %
282 %                                                                             %
283 %                                                                             %
284 %                                                                             %
285 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
286 %
287 %  DestroyLocaleOptions() releases memory associated with an locale
288 %  messages.
289 %
290 %  The format of the DestroyProfiles method is:
291 %
292 %      LinkedListInfo *DestroyLocaleOptions(Image *image)
293 %
294 %  A description of each parameter follows:
295 %
296 %    o image: the image.
297 %
298 */
299
300 static void *DestroyOptions(void *message)
301 {
302   return(DestroyStringInfo((StringInfo *) message));
303 }
304
305 MagickExport LinkedListInfo *DestroyLocaleOptions(LinkedListInfo *messages)
306 {
307   assert(messages != (LinkedListInfo *) NULL);
308   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
309   return(DestroyLinkedList(messages,DestroyOptions));
310 }
311 \f
312 /*
313 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
314 %                                                                             %
315 %                                                                             %
316 %                                                                             %
317 +  F o r m a t L o c a l e F i l e                                            %
318 %                                                                             %
319 %                                                                             %
320 %                                                                             %
321 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
322 %
323 %  FormatLocaleFile() prints formatted output of a variable argument list to a
324 %  file in the "C" locale.
325 %
326 %  The format of the FormatLocaleFile method is:
327 %
328 %      ssize_t FormatLocaleFile(FILE *file,const char *format,...)
329 %
330 %  A description of each parameter follows.
331 %
332 %   o file:  the file.
333 %
334 %   o format:  A file describing the format to use to write the remaining
335 %     arguments.
336 %
337 */
338
339 MagickPrivate ssize_t FormatLocaleFileList(FILE *file,
340   const char *restrict format,va_list operands)
341 {
342   ssize_t
343     n;
344
345 #if defined(MAGICKCORE_HAVE_VFPRINTF_L)
346   {
347     locale_t
348       locale;
349
350     locale=AcquireCLocale();
351     if (locale == (locale_t) NULL)
352       n=(ssize_t) vfprintf(file,format,operands);
353     else
354 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
355       n=(ssize_t) vfprintf_l(file,format,locale,operands);
356 #else
357       n=(ssize_t) vfprintf_l(file,locale,format,operands);
358 #endif
359   }
360 #else
361 #if defined(MAGICKCORE_HAVE_USELOCALE) && defined(MAGICKCORE_HAVE_STRTOD_L)
362   {
363     locale_t
364       locale,
365       previous_locale;
366
367     locale=AcquireCLocale();
368     if (locale == (locale_t) NULL)
369       n=(ssize_t) vfprintf(file,format,operands);
370     else
371       {
372         previous_locale=uselocale(locale);
373         n=(ssize_t) vfprintf(file,format,operands);
374         uselocale(previous_locale);
375       }
376   }
377 #else
378   n=(ssize_t) vfprintf(file,format,operands);
379 #endif
380 #endif
381   return(n);
382 }
383
384 MagickExport ssize_t FormatLocaleFile(FILE *file,const char *restrict format,
385   ...)
386 {
387   ssize_t
388     n;
389
390   va_list
391     operands;
392
393   va_start(operands,format);
394   n=FormatLocaleFileList(file,format,operands);
395   va_end(operands);
396   return(n);
397 }
398 \f
399 /*
400 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
401 %                                                                             %
402 %                                                                             %
403 %                                                                             %
404 +  F o r m a t L o c a l e S t r i n g                                        %
405 %                                                                             %
406 %                                                                             %
407 %                                                                             %
408 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
409 %
410 %  FormatLocaleString() prints formatted output of a variable argument list to
411 %  a string buffer in the "C" locale.
412 %
413 %  The format of the FormatLocaleString method is:
414 %
415 %      ssize_t FormatLocaleString(char *string,const size_t length,
416 %        const char *format,...)
417 %
418 %  A description of each parameter follows.
419 %
420 %   o string:  FormatLocaleString() returns the formatted string in this
421 %     character buffer.
422 %
423 %   o length: the maximum length of the string.
424 %
425 %   o format:  A string describing the format to use to write the remaining
426 %     arguments.
427 %
428 */
429
430 MagickPrivate ssize_t FormatLocaleStringList(char *restrict string,
431   const size_t length,const char *restrict format,va_list operands)
432 {
433   ssize_t
434     n;
435
436 #if defined(MAGICKCORE_HAVE_VSNPRINTF_L)
437   {
438     locale_t
439       locale;
440
441     locale=AcquireCLocale();
442     if (locale == (locale_t) NULL)
443       n=(ssize_t) vsnprintf(string,length,format,operands);
444     else
445 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
446       n=(ssize_t) vsnprintf_l(string,length,format,locale,operands);
447 #else
448       n=(ssize_t) vsnprintf_l(string,length,locale,format,operands);
449 #endif
450   }
451 #elif defined(MAGICKCORE_HAVE_VSNPRINTF)
452 #if defined(MAGICKCORE_HAVE_USELOCALE) && defined(MAGICKCORE_HAVE_STRTOD_L)
453   {
454     locale_t
455       locale,
456       previous_locale;
457
458     locale=AcquireCLocale();
459     if (locale == (locale_t) NULL)
460       n=(ssize_t) vsnprintf(string,length,format,operands);
461     else
462       {
463         previous_locale=uselocale(locale);
464         n=(ssize_t) vsnprintf(string,length,format,operands);
465         uselocale(previous_locale);
466       }
467   }
468 #else
469   n=(ssize_t) vsnprintf(string,length,format,operands);
470 #endif
471 #else
472   n=(ssize_t) vsprintf(string,format,operands);
473 #endif
474   if (n < 0)
475     string[length-1]='\0';
476   return(n);
477 }
478
479 MagickExport ssize_t FormatLocaleString(char *restrict string,
480   const size_t length,const char *restrict format,...)
481 {
482   ssize_t
483     n;
484
485   va_list
486     operands;
487
488   va_start(operands,format);
489   n=FormatLocaleStringList(string,length,format,operands);
490   va_end(operands);
491   return(n);
492 }
493 \f
494 /*
495 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
496 %                                                                             %
497 %                                                                             %
498 %                                                                             %
499 +   G e t L o c a l e I n f o _                                               %
500 %                                                                             %
501 %                                                                             %
502 %                                                                             %
503 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
504 %
505 %  GetLocaleInfo_() searches the locale list for the specified tag and if
506 %  found returns attributes for that element.
507 %
508 %  The format of the GetLocaleInfo method is:
509 %
510 %      const LocaleInfo *GetLocaleInfo_(const char *tag,
511 %        ExceptionInfo *exception)
512 %
513 %  A description of each parameter follows:
514 %
515 %    o tag: the locale tag.
516 %
517 %    o exception: return any errors or warnings in this structure.
518 %
519 */
520 MagickExport const LocaleInfo *GetLocaleInfo_(const char *tag,
521   ExceptionInfo *exception)
522 {
523   const LocaleInfo
524     *locale_info;
525
526   assert(exception != (ExceptionInfo *) NULL);
527   if (IsLocaleTreeInstantiated(exception) == MagickFalse)
528     return((const LocaleInfo *) NULL);
529   LockSemaphoreInfo(locale_semaphore);
530   if ((tag == (const char *) NULL) || (LocaleCompare(tag,"*") == 0))
531     {
532       ResetSplayTreeIterator(locale_cache);
533       locale_info=(const LocaleInfo *) GetNextValueInSplayTree(locale_cache);
534       UnlockSemaphoreInfo(locale_semaphore);
535       return(locale_info);
536     }
537   locale_info=(const LocaleInfo *) GetValueFromSplayTree(locale_cache,tag);
538   UnlockSemaphoreInfo(locale_semaphore);
539   return(locale_info);
540 }
541 \f
542 /*
543 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
544 %                                                                             %
545 %                                                                             %
546 %                                                                             %
547 %   G e t L o c a l e I n f o L i s t                                         %
548 %                                                                             %
549 %                                                                             %
550 %                                                                             %
551 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
552 %
553 %  GetLocaleInfoList() returns any locale messages that match the
554 %  specified pattern.
555 %
556 %  The format of the GetLocaleInfoList function is:
557 %
558 %      const LocaleInfo **GetLocaleInfoList(const char *pattern,
559 %        size_t *number_messages,ExceptionInfo *exception)
560 %
561 %  A description of each parameter follows:
562 %
563 %    o pattern: Specifies a pointer to a text string containing a pattern.
564 %
565 %    o number_messages:  This integer returns the number of locale messages in
566 %    the list.
567 %
568 %    o exception: return any errors or warnings in this structure.
569 %
570 */
571
572 #if defined(__cplusplus) || defined(c_plusplus)
573 extern "C" {
574 #endif
575
576 static int LocaleInfoCompare(const void *x,const void *y)
577 {
578   const LocaleInfo
579     **p,
580     **q;
581
582   p=(const LocaleInfo **) x,
583   q=(const LocaleInfo **) y;
584   if (LocaleCompare((*p)->path,(*q)->path) == 0)
585     return(LocaleCompare((*p)->tag,(*q)->tag));
586   return(LocaleCompare((*p)->path,(*q)->path));
587 }
588
589 #if defined(__cplusplus) || defined(c_plusplus)
590 }
591 #endif
592
593 MagickExport const LocaleInfo **GetLocaleInfoList(const char *pattern,
594   size_t *number_messages,ExceptionInfo *exception)
595 {
596   const LocaleInfo
597     **messages;
598
599   register const LocaleInfo
600     *p;
601
602   register ssize_t
603     i;
604
605   /*
606     Allocate locale list.
607   */
608   assert(pattern != (char *) NULL);
609   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
610   assert(number_messages != (size_t *) NULL);
611   *number_messages=0;
612   p=GetLocaleInfo_("*",exception);
613   if (p == (const LocaleInfo *) NULL)
614     return((const LocaleInfo **) NULL);
615   messages=(const LocaleInfo **) AcquireQuantumMemory((size_t)
616     GetNumberOfNodesInSplayTree(locale_cache)+1UL,sizeof(*messages));
617   if (messages == (const LocaleInfo **) NULL)
618     return((const LocaleInfo **) NULL);
619   /*
620     Generate locale list.
621   */
622   LockSemaphoreInfo(locale_semaphore);
623   ResetSplayTreeIterator(locale_cache);
624   p=(const LocaleInfo *) GetNextValueInSplayTree(locale_cache);
625   for (i=0; p != (const LocaleInfo *) NULL; )
626   {
627     if ((p->stealth == MagickFalse) &&
628         (GlobExpression(p->tag,pattern,MagickTrue) != MagickFalse))
629       messages[i++]=p;
630     p=(const LocaleInfo *) GetNextValueInSplayTree(locale_cache);
631   }
632   UnlockSemaphoreInfo(locale_semaphore);
633   qsort((void *) messages,(size_t) i,sizeof(*messages),LocaleInfoCompare);
634   messages[i]=(LocaleInfo *) NULL;
635   *number_messages=(size_t) i;
636   return(messages);
637 }
638 \f
639 /*
640 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
641 %                                                                             %
642 %                                                                             %
643 %                                                                             %
644 %   G e t L o c a l e L i s t                                                 %
645 %                                                                             %
646 %                                                                             %
647 %                                                                             %
648 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
649 %
650 %  GetLocaleList() returns any locale messages that match the specified
651 %  pattern.
652 %
653 %  The format of the GetLocaleList function is:
654 %
655 %      char **GetLocaleList(const char *pattern,size_t *number_messages,
656 %        Exceptioninfo *exception)
657 %
658 %  A description of each parameter follows:
659 %
660 %    o pattern: Specifies a pointer to a text string containing a pattern.
661 %
662 %    o number_messages:  This integer returns the number of messages in the
663 %      list.
664 %
665 %    o exception: return any errors or warnings in this structure.
666 %
667 */
668
669 #if defined(__cplusplus) || defined(c_plusplus)
670 extern "C" {
671 #endif
672
673 static int LocaleTagCompare(const void *x,const void *y)
674 {
675   register char
676     **p,
677     **q;
678
679   p=(char **) x;
680   q=(char **) y;
681   return(LocaleCompare(*p,*q));
682 }
683
684 #if defined(__cplusplus) || defined(c_plusplus)
685 }
686 #endif
687
688 MagickExport char **GetLocaleList(const char *pattern,
689   size_t *number_messages,ExceptionInfo *exception)
690 {
691   char
692     **messages;
693
694   register const LocaleInfo
695     *p;
696
697   register ssize_t
698     i;
699
700   /*
701     Allocate locale list.
702   */
703   assert(pattern != (char *) NULL);
704   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
705   assert(number_messages != (size_t *) NULL);
706   *number_messages=0;
707   p=GetLocaleInfo_("*",exception);
708   if (p == (const LocaleInfo *) NULL)
709     return((char **) NULL);
710   messages=(char **) AcquireQuantumMemory((size_t)
711     GetNumberOfNodesInSplayTree(locale_cache)+1UL,sizeof(*messages));
712   if (messages == (char **) NULL)
713     return((char **) NULL);
714   LockSemaphoreInfo(locale_semaphore);
715   p=(const LocaleInfo *) GetNextValueInSplayTree(locale_cache);
716   for (i=0; p != (const LocaleInfo *) NULL; )
717   {
718     if ((p->stealth == MagickFalse) &&
719         (GlobExpression(p->tag,pattern,MagickTrue) != MagickFalse))
720       messages[i++]=ConstantString(p->tag);
721     p=(const LocaleInfo *) GetNextValueInSplayTree(locale_cache);
722   }
723   UnlockSemaphoreInfo(locale_semaphore);
724   qsort((void *) messages,(size_t) i,sizeof(*messages),LocaleTagCompare);
725   messages[i]=(char *) NULL;
726   *number_messages=(size_t) i;
727   return(messages);
728 }
729 \f
730 /*
731 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
732 %                                                                             %
733 %                                                                             %
734 %                                                                             %
735 %   G e t L o c a l e M e s s a g e                                           %
736 %                                                                             %
737 %                                                                             %
738 %                                                                             %
739 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
740 %
741 %  GetLocaleMessage() returns a message in the current locale that matches the
742 %  supplied tag.
743 %
744 %  The format of the GetLocaleMessage method is:
745 %
746 %      const char *GetLocaleMessage(const char *tag)
747 %
748 %  A description of each parameter follows:
749 %
750 %    o tag: Return a message that matches this tag in the current locale.
751 %
752 */
753 MagickExport const char *GetLocaleMessage(const char *tag)
754 {
755   char
756     name[MaxTextExtent];
757
758   const LocaleInfo
759     *locale_info;
760
761   ExceptionInfo
762     *exception;
763
764   if ((tag == (const char *) NULL) || (*tag == '\0'))
765     return(tag);
766   exception=AcquireExceptionInfo();
767   (void) FormatLocaleString(name,MaxTextExtent,"%s/",tag);
768   locale_info=GetLocaleInfo_(name,exception);
769   exception=DestroyExceptionInfo(exception);
770   if (locale_info != (const LocaleInfo *) NULL)
771     return(locale_info->message);
772   return(tag);
773 }
774 \f
775 /*
776 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
777 %                                                                             %
778 %                                                                             %
779 %                                                                             %
780 %  G e t L o c a l e O p t i o n s                                            %
781 %                                                                             %
782 %                                                                             %
783 %                                                                             %
784 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
785 %
786 %  GetLocaleOptions() returns any Magick configuration messages associated
787 %  with the specified filename.
788 %
789 %  The format of the GetLocaleOptions method is:
790 %
791 %      LinkedListInfo *GetLocaleOptions(const char *filename,
792 %        ExceptionInfo *exception)
793 %
794 %  A description of each parameter follows:
795 %
796 %    o filename: the locale file tag.
797 %
798 %    o exception: return any errors or warnings in this structure.
799 %
800 */
801 MagickExport LinkedListInfo *GetLocaleOptions(const char *filename,
802   ExceptionInfo *exception)
803 {
804   char
805     path[MaxTextExtent];
806
807   const char
808     *element;
809
810   LinkedListInfo
811     *messages,
812     *paths;
813
814   StringInfo
815     *xml;
816
817   assert(filename != (const char *) NULL);
818   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
819   assert(exception != (ExceptionInfo *) NULL);
820   (void) CopyMagickString(path,filename,MaxTextExtent);
821   /*
822     Load XML from configuration files to linked-list.
823   */
824   messages=NewLinkedList(0);
825   paths=GetConfigurePaths(filename,exception);
826   if (paths != (LinkedListInfo *) NULL)
827     {
828       ResetLinkedListIterator(paths);
829       element=(const char *) GetNextValueInLinkedList(paths);
830       while (element != (const char *) NULL)
831       {
832         (void) FormatLocaleString(path,MaxTextExtent,"%s%s",element,filename);
833         (void) LogMagickEvent(LocaleEvent,GetMagickModule(),
834           "Searching for locale file: \"%s\"",path);
835         xml=ConfigureFileToStringInfo(path);
836         if (xml != (StringInfo *) NULL)
837           (void) AppendValueToLinkedList(messages,xml);
838         element=(const char *) GetNextValueInLinkedList(paths);
839       }
840       paths=DestroyLinkedList(paths,RelinquishMagickMemory);
841     }
842 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
843   {
844     char
845       *blob;
846
847     blob=(char *) NTResourceToBlob(filename);
848     if (blob != (char *) NULL)
849       {
850         xml=AcquireStringInfo(0);
851         SetStringInfoLength(xml,strlen(blob)+1);
852         SetStringInfoDatum(xml,(const unsigned char *) blob);
853         blob=(char *) RelinquishMagickMemory(blob);
854         SetStringInfoPath(xml,filename);
855         (void) AppendValueToLinkedList(messages,xml);
856       }
857   }
858 #endif
859   ResetLinkedListIterator(messages);
860   return(messages);
861 }
862 \f
863 /*
864 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
865 %                                                                             %
866 %                                                                             %
867 %                                                                             %
868 %   G e t L o c a l e V a l u e                                               %
869 %                                                                             %
870 %                                                                             %
871 %                                                                             %
872 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
873 %
874 %  GetLocaleValue() returns the message associated with the locale info.
875 %
876 %  The format of the GetLocaleValue method is:
877 %
878 %      const char *GetLocaleValue(const LocaleInfo *locale_info)
879 %
880 %  A description of each parameter follows:
881 %
882 %    o locale_info:  The locale info.
883 %
884 */
885 MagickExport const char *GetLocaleValue(const LocaleInfo *locale_info)
886 {
887   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
888   assert(locale_info != (LocaleInfo *) NULL);
889   assert(locale_info->signature == MagickSignature);
890   return(locale_info->message);
891 }
892 \f
893 /*
894 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
895 %                                                                             %
896 %                                                                             %
897 %                                                                             %
898 +   I s L o c a l e T r e e I n s t a n t i a t e d                           %
899 %                                                                             %
900 %                                                                             %
901 %                                                                             %
902 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
903 %
904 %  IsLocaleTreeInstantiated() determines if the locale tree is instantiated.
905 %  If not, it instantiates the tree and returns it.
906 %
907 %  The format of the IsLocaleInstantiated method is:
908 %
909 %      MagickBooleanType IsLocaleTreeInstantiated(ExceptionInfo *exception)
910 %
911 %  A description of each parameter follows.
912 %
913 %    o exception: return any errors or warnings in this structure.
914 %
915 */
916 static MagickBooleanType IsLocaleTreeInstantiated(ExceptionInfo *exception)
917 {
918   if (locale_cache == (SplayTreeInfo *) NULL)
919     {
920       if (locale_semaphore == (SemaphoreInfo *) NULL)
921         ActivateSemaphoreInfo(&locale_semaphore);
922       LockSemaphoreInfo(locale_semaphore);
923       if (locale_cache == (SplayTreeInfo *) NULL)
924         {
925           char
926             *locale;
927
928           register const char
929             *p;
930
931           locale=(char *) NULL;
932           p=setlocale(LC_CTYPE,(const char *) NULL);
933           if (p != (const char *) NULL)
934             locale=ConstantString(p);
935           if (locale == (char *) NULL)
936             locale=GetEnvironmentValue("LC_ALL");
937           if (locale == (char *) NULL)
938             locale=GetEnvironmentValue("LC_MESSAGES");
939           if (locale == (char *) NULL)
940             locale=GetEnvironmentValue("LC_CTYPE");
941           if (locale == (char *) NULL)
942             locale=GetEnvironmentValue("LANG");
943           if (locale == (char *) NULL)
944             locale=ConstantString("C");
945           locale_cache=AcquireLocaleSplayTree(LocaleFilename,locale,exception);
946           locale=DestroyString(locale);
947         }
948       UnlockSemaphoreInfo(locale_semaphore);
949     }
950   return(locale_cache != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
951 }
952 \f
953 /*
954 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
955 %                                                                             %
956 %                                                                             %
957 %                                                                             %
958 +   I n t e r p r e t L o c a l e V a l u e                                   %
959 %                                                                             %
960 %                                                                             %
961 %                                                                             %
962 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
963 %
964 %  InterpretLocaleValue() interprets the string as a floating point number in
965 %  the "C" locale and returns its value as a double. If sentinal is not a null
966 %  pointer, the method also sets the value pointed by sentinal to point to the
967 %  first character after the number.
968 %
969 %  The format of the InterpretLocaleValue method is:
970 %
971 %      double InterpretLocaleValue(const char *value,char **sentinal)
972 %
973 %  A description of each parameter follows:
974 %
975 %    o value: the string value.
976 %
977 %    o sentinal:  if sentinal is not NULL, a pointer to the character after the
978 %      last character used in the conversion is stored in the location
979 %      referenced by sentinal.
980 %
981 */
982 MagickExport double InterpretLocaleValue(const char *restrict string,
983   char **restrict sentinal)
984 {
985   char
986     *q;
987
988   double
989     value;
990
991   if ((*string == '0') && ((string[1] | 0x20)=='x'))
992     value=(double) strtoul(string,&q,16);
993   else
994     {
995 #if defined(MAGICKCORE_HAVE_STRTOD_L)
996       locale_t
997         locale;
998
999       locale=AcquireCLocale();
1000       if (locale == (locale_t) NULL)
1001         value=strtod(string,&q);
1002       else
1003         value=strtod_l(string,&q,locale);
1004 #else
1005       value=strtod(string,&q);
1006 #endif
1007     }
1008   if (sentinal != (char **) NULL)
1009     *sentinal=q;
1010   return(value);
1011 }
1012 \f
1013 /*
1014 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1015 %                                                                             %
1016 %                                                                             %
1017 %                                                                             %
1018 %  L i s t L o c a l e I n f o                                                %
1019 %                                                                             %
1020 %                                                                             %
1021 %                                                                             %
1022 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1023 %
1024 %  ListLocaleInfo() lists the locale info to a file.
1025 %
1026 %  The format of the ListLocaleInfo method is:
1027 %
1028 %      MagickBooleanType ListLocaleInfo(FILE *file,ExceptionInfo *exception)
1029 %
1030 %  A description of each parameter follows.
1031 %
1032 %    o file:  An pointer to a FILE.
1033 %
1034 %    o exception: return any errors or warnings in this structure.
1035 %
1036 */
1037 MagickExport MagickBooleanType ListLocaleInfo(FILE *file,
1038   ExceptionInfo *exception)
1039 {
1040   const char
1041     *path;
1042
1043   const LocaleInfo
1044     **locale_info;
1045
1046   register ssize_t
1047     i;
1048
1049   size_t
1050     number_messages;
1051
1052   if (file == (const FILE *) NULL)
1053     file=stdout;
1054   number_messages=0;
1055   locale_info=GetLocaleInfoList("*",&number_messages,exception);
1056   if (locale_info == (const LocaleInfo **) NULL)
1057     return(MagickFalse);
1058   path=(const char *) NULL;
1059   for (i=0; i < (ssize_t) number_messages; i++)
1060   {
1061     if (locale_info[i]->stealth != MagickFalse)
1062       continue;
1063     if ((path == (const char *) NULL) ||
1064         (LocaleCompare(path,locale_info[i]->path) != 0))
1065       {
1066         if (locale_info[i]->path != (char *) NULL)
1067           (void) FormatLocaleFile(file,"\nPath: %s\n\n",locale_info[i]->path);
1068         (void) FormatLocaleFile(file,"Tag/Message\n");
1069         (void) FormatLocaleFile(file,
1070           "-------------------------------------------------"
1071           "------------------------------\n");
1072       }
1073     path=locale_info[i]->path;
1074     (void) FormatLocaleFile(file,"%s\n",locale_info[i]->tag);
1075     if (locale_info[i]->message != (char *) NULL)
1076       (void) FormatLocaleFile(file,"  %s",locale_info[i]->message);
1077     (void) FormatLocaleFile(file,"\n");
1078   }
1079   (void) fflush(file);
1080   locale_info=(const LocaleInfo **)
1081     RelinquishMagickMemory((void *) locale_info);
1082   return(MagickTrue);
1083 }
1084 \f
1085 /*
1086 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1087 %                                                                             %
1088 %                                                                             %
1089 %                                                                             %
1090 +   L o a d L o c a l e L i s t                                               %
1091 %                                                                             %
1092 %                                                                             %
1093 %                                                                             %
1094 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1095 %
1096 %  LoadLocaleCache() loads the locale configurations which provides a mapping
1097 %  between locale attributes and a locale name.
1098 %
1099 %  The format of the LoadLocaleCache method is:
1100 %
1101 %      MagickBooleanType LoadLocaleCache(SplayTreeInfo *locale_cache,
1102 %        const char *xml,const char *filename,const size_t depth,
1103 %        ExceptionInfo *exception)
1104 %
1105 %  A description of each parameter follows:
1106 %
1107 %    o xml:  The locale list in XML format.
1108 %
1109 %    o filename:  The locale list filename.
1110 %
1111 %    o depth: depth of <include /> statements.
1112 %
1113 %    o exception: return any errors or warnings in this structure.
1114 %
1115 */
1116
1117 static void ChopLocaleComponents(char *path,const size_t components)
1118 {
1119   register char
1120     *p;
1121
1122   ssize_t
1123     count;
1124
1125   if (*path == '\0')
1126     return;
1127   p=path+strlen(path)-1;
1128   if (*p == '/')
1129     *p='\0';
1130   for (count=0; (count < (ssize_t) components) && (p > path); p--)
1131     if (*p == '/')
1132       {
1133         *p='\0';
1134         count++;
1135       }
1136   if (count < (ssize_t) components)
1137     *path='\0';
1138 }
1139
1140 static void LocaleFatalErrorHandler(
1141   const ExceptionType magick_unused(severity),
1142   const char *reason,const char *description)
1143 {
1144   if (reason == (char *) NULL)
1145     return;
1146   (void) FormatLocaleFile(stderr,"%s: %s",GetClientName(),reason);
1147   if (description != (char *) NULL)
1148     (void) FormatLocaleFile(stderr," (%s)",description);
1149   (void) FormatLocaleFile(stderr,".\n");
1150   (void) fflush(stderr);
1151   exit(1);
1152 }
1153
1154 static MagickBooleanType LoadLocaleCache(SplayTreeInfo *locale_cache,
1155   const char *xml,const char *filename,const char *locale,const size_t depth,
1156   ExceptionInfo *exception)
1157 {
1158   char
1159     keyword[MaxTextExtent],
1160     message[MaxTextExtent],
1161     tag[MaxTextExtent],
1162     *token;
1163
1164   const char
1165     *q;
1166
1167   FatalErrorHandler
1168     fatal_handler;
1169
1170   LocaleInfo
1171     *locale_info;
1172
1173   MagickStatusType
1174     status;
1175
1176   register char
1177     *p;
1178
1179   /*
1180     Read the locale configure file.
1181   */
1182   (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1183     "Loading locale configure file \"%s\" ...",filename);
1184   if (xml == (const char *) NULL)
1185     return(MagickFalse);
1186   status=MagickTrue;
1187   locale_info=(LocaleInfo *) NULL;
1188   *tag='\0';
1189   *message='\0';
1190   *keyword='\0';
1191   fatal_handler=SetFatalErrorHandler(LocaleFatalErrorHandler);
1192   token=AcquireString(xml);
1193   for (q=(char *) xml; *q != '\0'; )
1194   {
1195     /*
1196       Interpret XML.
1197     */
1198     GetMagickToken(q,&q,token);
1199     if (*token == '\0')
1200       break;
1201     (void) CopyMagickString(keyword,token,MaxTextExtent);
1202     if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
1203       {
1204         /*
1205           Doctype element.
1206         */
1207         while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
1208         {
1209           GetMagickToken(q,&q,token);
1210           while (isspace((int) ((unsigned char) *q)) != 0)
1211             q++;
1212         }
1213         continue;
1214       }
1215     if (LocaleNCompare(keyword,"<!--",4) == 0)
1216       {
1217         /*
1218           Comment element.
1219         */
1220         while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
1221         {
1222           GetMagickToken(q,&q,token);
1223           while (isspace((int) ((unsigned char) *q)) != 0)
1224             q++;
1225         }
1226         continue;
1227       }
1228     if (LocaleCompare(keyword,"<include") == 0)
1229       {
1230         /*
1231           Include element.
1232         */
1233         while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
1234         {
1235           (void) CopyMagickString(keyword,token,MaxTextExtent);
1236           GetMagickToken(q,&q,token);
1237           if (*token != '=')
1238             continue;
1239           GetMagickToken(q,&q,token);
1240           if (LocaleCompare(keyword,"locale") == 0)
1241             {
1242               if (LocaleCompare(locale,token) != 0)
1243                 break;
1244               continue;
1245             }
1246           if (LocaleCompare(keyword,"file") == 0)
1247             {
1248               if (depth > 200)
1249                 (void) ThrowMagickException(exception,GetMagickModule(),
1250                   ConfigureError,"IncludeElementNestedTooDeeply","`%s'",token);
1251               else
1252                 {
1253                   char
1254                     path[MaxTextExtent],
1255                     *xml;
1256
1257                   *path='\0';
1258                   GetPathComponent(filename,HeadPath,path);
1259                   if (*path != '\0')
1260                     (void) ConcatenateMagickString(path,DirectorySeparator,
1261                       MaxTextExtent);
1262                   if (*token == *DirectorySeparator)
1263                     (void) CopyMagickString(path,token,MaxTextExtent);
1264                   else
1265                     (void) ConcatenateMagickString(path,token,MaxTextExtent);
1266                   xml=FileToXML(path,~0UL);
1267                   if (xml != (char *) NULL)
1268                     {
1269                       status&=LoadLocaleCache(locale_cache,xml,path,locale,
1270                         depth+1,exception);
1271                       xml=(char *) RelinquishMagickMemory(xml);
1272                     }
1273                 }
1274             }
1275         }
1276         continue;
1277       }
1278     if (LocaleCompare(keyword,"<locale") == 0)
1279       {
1280         /*
1281           Locale element.
1282         */
1283         while ((*token != '>') && (*q != '\0'))
1284         {
1285           (void) CopyMagickString(keyword,token,MaxTextExtent);
1286           GetMagickToken(q,&q,token);
1287           if (*token != '=')
1288             continue;
1289           GetMagickToken(q,&q,token);
1290         }
1291         continue;
1292       }
1293     if (LocaleCompare(keyword,"</locale>") == 0)
1294       {
1295         ChopLocaleComponents(tag,1);
1296         (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1297         continue;
1298       }
1299     if (LocaleCompare(keyword,"<localemap>") == 0)
1300       continue;
1301     if (LocaleCompare(keyword,"</localemap>") == 0)
1302       continue;
1303     if (LocaleCompare(keyword,"<message") == 0)
1304       {
1305         /*
1306           Message element.
1307         */
1308         while ((*token != '>') && (*q != '\0'))
1309         {
1310           (void) CopyMagickString(keyword,token,MaxTextExtent);
1311           GetMagickToken(q,&q,token);
1312           if (*token != '=')
1313             continue;
1314           GetMagickToken(q,&q,token);
1315           if (LocaleCompare(keyword,"name") == 0)
1316             {
1317               (void) ConcatenateMagickString(tag,token,MaxTextExtent);
1318               (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1319             }
1320         }
1321         for (p=(char *) q; (*q != '<') && (*q != '\0'); q++) ;
1322         while (isspace((int) ((unsigned char) *p)) != 0)
1323           p++;
1324         q--;
1325         while ((isspace((int) ((unsigned char) *q)) != 0) && (q > p))
1326           q--;
1327         (void) CopyMagickString(message,p,MagickMin((size_t) (q-p+2),
1328           MaxTextExtent));
1329         locale_info=(LocaleInfo *) AcquireMagickMemory(sizeof(*locale_info));
1330         if (locale_info == (LocaleInfo *) NULL)
1331           ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1332         (void) ResetMagickMemory(locale_info,0,sizeof(*locale_info));
1333         locale_info->path=ConstantString(filename);
1334         locale_info->tag=ConstantString(tag);
1335         locale_info->message=ConstantString(message);
1336         locale_info->signature=MagickSignature;
1337         status=AddValueToSplayTree(locale_cache,locale_info->tag,locale_info);
1338         if (status == MagickFalse)
1339           (void) ThrowMagickException(exception,GetMagickModule(),
1340             ResourceLimitError,"MemoryAllocationFailed","`%s'",
1341             locale_info->tag);
1342         (void) ConcatenateMagickString(tag,message,MaxTextExtent);
1343         (void) ConcatenateMagickString(tag,"\n",MaxTextExtent);
1344         q++;
1345         continue;
1346       }
1347     if (LocaleCompare(keyword,"</message>") == 0)
1348       {
1349         ChopLocaleComponents(tag,2);
1350         (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1351         continue;
1352       }
1353     if (*keyword == '<')
1354       {
1355         /*
1356           Subpath element.
1357         */
1358         if (*(keyword+1) == '?')
1359           continue;
1360         if (*(keyword+1) == '/')
1361           {
1362             ChopLocaleComponents(tag,1);
1363             if (*tag != '\0')
1364               (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1365             continue;
1366           }
1367         token[strlen(token)-1]='\0';
1368         (void) CopyMagickString(token,token+1,MaxTextExtent);
1369         (void) ConcatenateMagickString(tag,token,MaxTextExtent);
1370         (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1371         continue;
1372       }
1373     GetMagickToken(q,(const char **) NULL,token);
1374     if (*token != '=')
1375       continue;
1376   }
1377   token=(char *) RelinquishMagickMemory(token);
1378   (void) SetFatalErrorHandler(fatal_handler);
1379   return(status != 0 ? MagickTrue : MagickFalse);
1380 }
1381 \f
1382 /*
1383 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1384 %                                                                             %
1385 %                                                                             %
1386 %                                                                             %
1387 +   L o c a l e C o m p o n e n t G e n e s i s                               %
1388 %                                                                             %
1389 %                                                                             %
1390 %                                                                             %
1391 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1392 %
1393 %  LocaleComponentGenesis() instantiates the locale component.
1394 %
1395 %  The format of the LocaleComponentGenesis method is:
1396 %
1397 %      MagickBooleanType LocaleComponentGenesis(void)
1398 %
1399 */
1400 MagickPrivate MagickBooleanType LocaleComponentGenesis(void)
1401 {
1402   if (locale_semaphore == (SemaphoreInfo *) NULL)
1403     locale_semaphore=AcquireSemaphoreInfo();
1404   return(MagickTrue);
1405 }
1406 \f
1407 /*
1408 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1409 %                                                                             %
1410 %                                                                             %
1411 %                                                                             %
1412 +   L o c a l e C o m p o n e n t T e r m i n u s                             %
1413 %                                                                             %
1414 %                                                                             %
1415 %                                                                             %
1416 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1417 %
1418 %  LocaleComponentTerminus() destroys the locale component.
1419 %
1420 %  The format of the LocaleComponentTerminus method is:
1421 %
1422 %      LocaleComponentTerminus(void)
1423 %
1424 */
1425 MagickPrivate void LocaleComponentTerminus(void)
1426 {
1427   if (locale_semaphore == (SemaphoreInfo *) NULL)
1428     ActivateSemaphoreInfo(&locale_semaphore);
1429   LockSemaphoreInfo(locale_semaphore);
1430   if (locale_cache != (SplayTreeInfo *) NULL)
1431     locale_cache=DestroySplayTree(locale_cache);
1432 #if defined(MAGICKCORE_HAVE_STRTOD_L)
1433   DestroyCLocale();
1434 #endif
1435   UnlockSemaphoreInfo(locale_semaphore);
1436   RelinquishSemaphoreInfo(&locale_semaphore);
1437 }