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