2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % L OOO CCCC AAA L EEEEE %
8 % L O O C AAAAA L EEE %
10 % LLLLL OOO CCCC A A LLLLL EEEEE %
13 % MagickCore Image Locale Methods %
20 % Copyright 1999-2014 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
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. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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"
67 #define LocaleFilename "locale.xml"
68 #define MaxRecursionDepth 200
73 #if defined(__CYGWIN__)
74 typedef struct _locale_t
83 "<?xml version=\"1.0\"?>"
85 " <locale name=\"C\">"
87 " <Message name=\"\">"
94 *locale_semaphore = (SemaphoreInfo *) NULL;
97 *locale_cache = (SplayTreeInfo *) NULL;
99 #if defined(MAGICKCORE_HAVE_STRTOD_L)
100 static volatile locale_t
101 c_locale = (locale_t) NULL;
105 Forward declarations.
107 static MagickBooleanType
108 IsLocaleTreeInstantiated(ExceptionInfo *),
109 LoadLocaleCache(SplayTreeInfo *,const char *,const char *,const char *,
110 const size_t,ExceptionInfo *);
112 #if defined(MAGICKCORE_HAVE_STRTOD_L)
114 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
118 + A c q u i r e C L o c a l e %
122 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
124 % AcquireCLocale() allocates the C locale object, or (locale_t) 0 with
125 % errno set if it cannot be acquired.
127 % The format of the AcquireCLocale method is:
129 % locale_t AcquireCLocale(void)
132 static locale_t AcquireCLocale(void)
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");
146 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
150 % A c q u i r e L o c a l e S p l a y T r e e %
154 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
156 % AcquireLocaleSplayTree() caches one or more locale configurations which
157 % provides a mapping between locale attributes and a locale tag.
159 % The format of the AcquireLocaleSplayTree method is:
161 % SplayTreeInfo *AcquireLocaleSplayTree(const char *filename,
162 % ExceptionInfo *exception)
164 % A description of each parameter follows:
166 % o filename: the font file tag.
168 % o locale: the actual locale.
170 % o exception: return any errors or warnings in this structure.
174 static void *DestroyLocaleNode(void *locale_info)
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));
189 static SplayTreeInfo *AcquireLocaleSplayTree(const char *filename,
190 const char *locale,ExceptionInfo *exception)
198 locale_cache=NewSplayTree(CompareSplayTreeString,(void *(*)(void *)) NULL,
200 if (locale_cache == (SplayTreeInfo *) NULL)
201 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
203 #if !defined(MAGICKCORE_ZERO_CONFIGURATION_SUPPORT)
211 options=GetLocaleOptions(filename,exception);
212 option=(const StringInfo *) GetNextValueInLinkedList(options);
213 while (option != (const StringInfo *) NULL)
215 status&=LoadLocaleCache(locale_cache,(const char *)
216 GetStringInfoDatum(option),GetStringInfoPath(option),locale,0,
218 option=(const StringInfo *) GetNextValueInLinkedList(options);
220 options=DestroyLocaleOptions(options);
221 if (GetNumberOfNodesInSplayTree(locale_cache) == 0)
223 options=GetLocaleOptions("english.xml",exception);
224 option=(const StringInfo *) GetNextValueInLinkedList(options);
225 while (option != (const StringInfo *) NULL)
227 status&=LoadLocaleCache(locale_cache,(const char *)
228 GetStringInfoDatum(option),GetStringInfoPath(option),locale,0,
230 option=(const StringInfo *) GetNextValueInLinkedList(options);
232 options=DestroyLocaleOptions(options);
236 if (GetNumberOfNodesInSplayTree(locale_cache) == 0)
237 status&=LoadLocaleCache(locale_cache,LocaleMap,"built-in",locale,0,
239 return(locale_cache);
242 #if defined(MAGICKCORE_HAVE_STRTOD_L)
244 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
248 + D e s t r o y C L o c a l e %
252 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
254 % DestroyCLocale() releases the resources allocated for a locale object
255 % returned by a call to the AcquireCLocale() method.
257 % The format of the DestroyCLocale method is:
259 % void DestroyCLocale(void)
262 static void DestroyCLocale(void)
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);
271 c_locale=(locale_t) NULL;
276 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
280 % D e s t r o y L o c a l e O p t i o n s %
284 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
286 % DestroyLocaleOptions() releases memory associated with an locale
289 % The format of the DestroyProfiles method is:
291 % LinkedListInfo *DestroyLocaleOptions(Image *image)
293 % A description of each parameter follows:
295 % o image: the image.
299 static void *DestroyOptions(void *message)
301 return(DestroyStringInfo((StringInfo *) message));
304 MagickExport LinkedListInfo *DestroyLocaleOptions(LinkedListInfo *messages)
306 assert(messages != (LinkedListInfo *) NULL);
307 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
308 return(DestroyLinkedList(messages,DestroyOptions));
312 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
316 + F o r m a t L o c a l e F i l e %
320 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
322 % FormatLocaleFile() prints formatted output of a variable argument list to a
323 % file in the "C" locale.
325 % The format of the FormatLocaleFile method is:
327 % ssize_t FormatLocaleFile(FILE *file,const char *format,...)
329 % A description of each parameter follows.
333 % o format: A file describing the format to use to write the remaining
338 MagickPrivate ssize_t FormatLocaleFileList(FILE *file,
339 const char *restrict format,va_list operands)
344 #if defined(MAGICKCORE_HAVE_VFPRINTF_L)
349 locale=AcquireCLocale();
350 if (locale == (locale_t) NULL)
351 n=(ssize_t) vfprintf(file,format,operands);
353 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
354 n=(ssize_t) vfprintf_l(file,format,locale,operands);
356 n=(ssize_t) vfprintf_l(file,locale,format,operands);
360 #if defined(MAGICKCORE_HAVE_USELOCALE) && defined(MAGICKCORE_HAVE_STRTOD_L)
366 locale=AcquireCLocale();
367 if (locale == (locale_t) NULL)
368 n=(ssize_t) vfprintf(file,format,operands);
371 previous_locale=uselocale(locale);
372 n=(ssize_t) vfprintf(file,format,operands);
373 uselocale(previous_locale);
377 n=(ssize_t) vfprintf(file,format,operands);
383 MagickExport ssize_t FormatLocaleFile(FILE *file,const char *restrict format,
392 va_start(operands,format);
393 n=FormatLocaleFileList(file,format,operands);
399 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
403 + F o r m a t L o c a l e S t r i n g %
407 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
409 % FormatLocaleString() prints formatted output of a variable argument list to
410 % a string buffer in the "C" locale.
412 % The format of the FormatLocaleString method is:
414 % ssize_t FormatLocaleString(char *string,const size_t length,
415 % const char *format,...)
417 % A description of each parameter follows.
419 % o string: FormatLocaleString() returns the formatted string in this
422 % o length: the maximum length of the string.
424 % o format: A string describing the format to use to write the remaining
429 MagickPrivate ssize_t FormatLocaleStringList(char *restrict string,
430 const size_t length,const char *restrict format,va_list operands)
435 #if defined(MAGICKCORE_HAVE_VSNPRINTF_L)
440 locale=AcquireCLocale();
441 if (locale == (locale_t) NULL)
442 n=(ssize_t) vsnprintf(string,length,format,operands);
444 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
445 n=(ssize_t) vsnprintf_l(string,length,format,locale,operands);
447 n=(ssize_t) vsnprintf_l(string,length,locale,format,operands);
450 #elif defined(MAGICKCORE_HAVE_VSNPRINTF)
451 #if defined(MAGICKCORE_HAVE_USELOCALE) && defined(MAGICKCORE_HAVE_STRTOD_L)
457 locale=AcquireCLocale();
458 if (locale == (locale_t) NULL)
459 n=(ssize_t) vsnprintf(string,length,format,operands);
462 previous_locale=uselocale(locale);
463 n=(ssize_t) vsnprintf(string,length,format,operands);
464 uselocale(previous_locale);
468 n=(ssize_t) vsnprintf(string,length,format,operands);
471 n=(ssize_t) vsprintf(string,format,operands);
474 string[length-1]='\0';
478 MagickExport ssize_t FormatLocaleString(char *restrict string,
479 const size_t length,const char *restrict format,...)
487 va_start(operands,format);
488 n=FormatLocaleStringList(string,length,format,operands);
494 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
498 + G e t L o c a l e I n f o _ %
502 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
504 % GetLocaleInfo_() searches the locale list for the specified tag and if
505 % found returns attributes for that element.
507 % The format of the GetLocaleInfo method is:
509 % const LocaleInfo *GetLocaleInfo_(const char *tag,
510 % ExceptionInfo *exception)
512 % A description of each parameter follows:
514 % o tag: the locale tag.
516 % o exception: return any errors or warnings in this structure.
519 MagickExport const LocaleInfo *GetLocaleInfo_(const char *tag,
520 ExceptionInfo *exception)
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))
531 ResetSplayTreeIterator(locale_cache);
532 locale_info=(const LocaleInfo *) GetNextValueInSplayTree(locale_cache);
533 UnlockSemaphoreInfo(locale_semaphore);
536 locale_info=(const LocaleInfo *) GetValueFromSplayTree(locale_cache,tag);
537 UnlockSemaphoreInfo(locale_semaphore);
542 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
546 % G e t L o c a l e I n f o L i s t %
550 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
552 % GetLocaleInfoList() returns any locale messages that match the
555 % The format of the GetLocaleInfoList function is:
557 % const LocaleInfo **GetLocaleInfoList(const char *pattern,
558 % size_t *number_messages,ExceptionInfo *exception)
560 % A description of each parameter follows:
562 % o pattern: Specifies a pointer to a text string containing a pattern.
564 % o number_messages: This integer returns the number of locale messages in
567 % o exception: return any errors or warnings in this structure.
571 #if defined(__cplusplus) || defined(c_plusplus)
575 static int LocaleInfoCompare(const void *x,const void *y)
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));
588 #if defined(__cplusplus) || defined(c_plusplus)
592 MagickExport const LocaleInfo **GetLocaleInfoList(const char *pattern,
593 size_t *number_messages,ExceptionInfo *exception)
598 register const LocaleInfo
605 Allocate locale list.
607 assert(pattern != (char *) NULL);
608 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
609 assert(number_messages != (size_t *) NULL);
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);
619 Generate locale list.
621 LockSemaphoreInfo(locale_semaphore);
622 ResetSplayTreeIterator(locale_cache);
623 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_cache);
624 for (i=0; p != (const LocaleInfo *) NULL; )
626 if ((p->stealth == MagickFalse) &&
627 (GlobExpression(p->tag,pattern,MagickTrue) != MagickFalse))
629 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_cache);
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;
639 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
643 % G e t L o c a l e L i s t %
647 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
649 % GetLocaleList() returns any locale messages that match the specified
652 % The format of the GetLocaleList function is:
654 % char **GetLocaleList(const char *pattern,size_t *number_messages,
655 % Exceptioninfo *exception)
657 % A description of each parameter follows:
659 % o pattern: Specifies a pointer to a text string containing a pattern.
661 % o number_messages: This integer returns the number of messages in the
664 % o exception: return any errors or warnings in this structure.
668 #if defined(__cplusplus) || defined(c_plusplus)
672 static int LocaleTagCompare(const void *x,const void *y)
680 return(LocaleCompare(*p,*q));
683 #if defined(__cplusplus) || defined(c_plusplus)
687 MagickExport char **GetLocaleList(const char *pattern,
688 size_t *number_messages,ExceptionInfo *exception)
693 register const LocaleInfo
700 Allocate locale list.
702 assert(pattern != (char *) NULL);
703 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
704 assert(number_messages != (size_t *) NULL);
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; )
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);
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;
730 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
734 % G e t L o c a l e M e s s a g e %
738 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
740 % GetLocaleMessage() returns a message in the current locale that matches the
743 % The format of the GetLocaleMessage method is:
745 % const char *GetLocaleMessage(const char *tag)
747 % A description of each parameter follows:
749 % o tag: Return a message that matches this tag in the current locale.
752 MagickExport const char *GetLocaleMessage(const char *tag)
763 if ((tag == (const char *) NULL) || (*tag == '\0'))
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);
775 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
779 % G e t L o c a l e O p t i o n s %
783 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
785 % GetLocaleOptions() returns any Magick configuration messages associated
786 % with the specified filename.
788 % The format of the GetLocaleOptions method is:
790 % LinkedListInfo *GetLocaleOptions(const char *filename,
791 % ExceptionInfo *exception)
793 % A description of each parameter follows:
795 % o filename: the locale file tag.
797 % o exception: return any errors or warnings in this structure.
800 MagickExport LinkedListInfo *GetLocaleOptions(const char *filename,
801 ExceptionInfo *exception)
816 assert(filename != (const char *) NULL);
817 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
818 assert(exception != (ExceptionInfo *) NULL);
819 (void) CopyMagickString(path,filename,MaxTextExtent);
821 Load XML from configuration files to linked-list.
823 messages=NewLinkedList(0);
824 paths=GetConfigurePaths(filename,exception);
825 if (paths != (LinkedListInfo *) NULL)
827 ResetLinkedListIterator(paths);
828 element=(const char *) GetNextValueInLinkedList(paths);
829 while (element != (const char *) NULL)
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);
839 paths=DestroyLinkedList(paths,RelinquishMagickMemory);
841 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
846 blob=(char *) NTResourceToBlob(filename);
847 if (blob != (char *) NULL)
849 xml=AcquireStringInfo(0);
850 SetStringInfoLength(xml,strlen(blob)+1);
851 SetStringInfoDatum(xml,(const unsigned char *) blob);
852 blob=(char *) RelinquishMagickMemory(blob);
853 SetStringInfoPath(xml,filename);
854 (void) AppendValueToLinkedList(messages,xml);
858 ResetLinkedListIterator(messages);
863 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
867 % G e t L o c a l e V a l u e %
871 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
873 % GetLocaleValue() returns the message associated with the locale info.
875 % The format of the GetLocaleValue method is:
877 % const char *GetLocaleValue(const LocaleInfo *locale_info)
879 % A description of each parameter follows:
881 % o locale_info: The locale info.
884 MagickExport const char *GetLocaleValue(const LocaleInfo *locale_info)
886 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
887 assert(locale_info != (LocaleInfo *) NULL);
888 assert(locale_info->signature == MagickSignature);
889 return(locale_info->message);
893 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
897 + I s L o c a l e T r e e I n s t a n t i a t e d %
901 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
903 % IsLocaleTreeInstantiated() determines if the locale tree is instantiated.
904 % If not, it instantiates the tree and returns it.
906 % The format of the IsLocaleInstantiated method is:
908 % MagickBooleanType IsLocaleTreeInstantiated(ExceptionInfo *exception)
910 % A description of each parameter follows.
912 % o exception: return any errors or warnings in this structure.
915 static MagickBooleanType IsLocaleTreeInstantiated(ExceptionInfo *exception)
917 if (locale_cache == (SplayTreeInfo *) NULL)
919 if (locale_semaphore == (SemaphoreInfo *) NULL)
920 ActivateSemaphoreInfo(&locale_semaphore);
921 LockSemaphoreInfo(locale_semaphore);
922 if (locale_cache == (SplayTreeInfo *) NULL)
930 locale=(char *) NULL;
931 p=setlocale(LC_CTYPE,(const char *) NULL);
932 if (p != (const char *) NULL)
933 locale=ConstantString(p);
934 if (locale == (char *) NULL)
935 locale=GetEnvironmentValue("LC_ALL");
936 if (locale == (char *) NULL)
937 locale=GetEnvironmentValue("LC_MESSAGES");
938 if (locale == (char *) NULL)
939 locale=GetEnvironmentValue("LC_CTYPE");
940 if (locale == (char *) NULL)
941 locale=GetEnvironmentValue("LANG");
942 if (locale == (char *) NULL)
943 locale=ConstantString("C");
944 locale_cache=AcquireLocaleSplayTree(LocaleFilename,locale,exception);
945 locale=DestroyString(locale);
947 UnlockSemaphoreInfo(locale_semaphore);
949 return(locale_cache != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
953 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
957 + I n t e r p r e t L o c a l e V a l u e %
961 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
963 % InterpretLocaleValue() interprets the string as a floating point number in
964 % the "C" locale and returns its value as a double. If sentinal is not a null
965 % pointer, the method also sets the value pointed by sentinal to point to the
966 % first character after the number.
968 % The format of the InterpretLocaleValue method is:
970 % double InterpretLocaleValue(const char *value,char **sentinal)
972 % A description of each parameter follows:
974 % o value: the string value.
976 % o sentinal: if sentinal is not NULL, a pointer to the character after the
977 % last character used in the conversion is stored in the location
978 % referenced by sentinal.
981 MagickExport double InterpretLocaleValue(const char *restrict string,
982 char **restrict sentinal)
990 if ((*string == '0') && ((string[1] | 0x20)=='x'))
991 value=(double) strtoul(string,&q,16);
994 #if defined(MAGICKCORE_HAVE_STRTOD_L)
998 locale=AcquireCLocale();
999 if (locale == (locale_t) NULL)
1000 value=strtod(string,&q);
1002 value=strtod_l(string,&q,locale);
1004 value=strtod(string,&q);
1007 if (sentinal != (char **) NULL)
1013 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1017 % L i s t L o c a l e I n f o %
1021 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1023 % ListLocaleInfo() lists the locale info to a file.
1025 % The format of the ListLocaleInfo method is:
1027 % MagickBooleanType ListLocaleInfo(FILE *file,ExceptionInfo *exception)
1029 % A description of each parameter follows.
1031 % o file: An pointer to a FILE.
1033 % o exception: return any errors or warnings in this structure.
1036 MagickExport MagickBooleanType ListLocaleInfo(FILE *file,
1037 ExceptionInfo *exception)
1051 if (file == (const FILE *) NULL)
1054 locale_info=GetLocaleInfoList("*",&number_messages,exception);
1055 if (locale_info == (const LocaleInfo **) NULL)
1056 return(MagickFalse);
1057 path=(const char *) NULL;
1058 for (i=0; i < (ssize_t) number_messages; i++)
1060 if (locale_info[i]->stealth != MagickFalse)
1062 if ((path == (const char *) NULL) ||
1063 (LocaleCompare(path,locale_info[i]->path) != 0))
1065 if (locale_info[i]->path != (char *) NULL)
1066 (void) FormatLocaleFile(file,"\nPath: %s\n\n",locale_info[i]->path);
1067 (void) FormatLocaleFile(file,"Tag/Message\n");
1068 (void) FormatLocaleFile(file,
1069 "-------------------------------------------------"
1070 "------------------------------\n");
1072 path=locale_info[i]->path;
1073 (void) FormatLocaleFile(file,"%s\n",locale_info[i]->tag);
1074 if (locale_info[i]->message != (char *) NULL)
1075 (void) FormatLocaleFile(file," %s",locale_info[i]->message);
1076 (void) FormatLocaleFile(file,"\n");
1078 (void) fflush(file);
1079 locale_info=(const LocaleInfo **)
1080 RelinquishMagickMemory((void *) locale_info);
1085 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1089 + L o a d L o c a l e L i s t %
1093 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1095 % LoadLocaleCache() loads the locale configurations which provides a mapping
1096 % between locale attributes and a locale name.
1098 % The format of the LoadLocaleCache method is:
1100 % MagickBooleanType LoadLocaleCache(SplayTreeInfo *locale_cache,
1101 % const char *xml,const char *filename,const size_t depth,
1102 % ExceptionInfo *exception)
1104 % A description of each parameter follows:
1106 % o xml: The locale list in XML format.
1108 % o filename: The locale list filename.
1110 % o depth: depth of <include /> statements.
1112 % o exception: return any errors or warnings in this structure.
1116 static void ChopLocaleComponents(char *path,const size_t components)
1126 p=path+strlen(path)-1;
1129 for (count=0; (count < (ssize_t) components) && (p > path); p--)
1135 if (count < (ssize_t) components)
1139 static void LocaleFatalErrorHandler(
1140 const ExceptionType magick_unused(severity),
1141 const char *reason,const char *description)
1143 if (reason == (char *) NULL)
1145 (void) FormatLocaleFile(stderr,"%s: %s",GetClientName(),reason);
1146 if (description != (char *) NULL)
1147 (void) FormatLocaleFile(stderr," (%s)",description);
1148 (void) FormatLocaleFile(stderr,".\n");
1149 (void) fflush(stderr);
1153 static inline size_t MagickMin(const size_t x,const size_t y)
1160 static MagickBooleanType LoadLocaleCache(SplayTreeInfo *locale_cache,
1161 const char *xml,const char *filename,const char *locale,const size_t depth,
1162 ExceptionInfo *exception)
1165 keyword[MaxTextExtent],
1166 message[MaxTextExtent],
1186 Read the locale configure file.
1188 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1189 "Loading locale configure file \"%s\" ...",filename);
1190 if (xml == (const char *) NULL)
1191 return(MagickFalse);
1193 locale_info=(LocaleInfo *) NULL;
1197 fatal_handler=SetFatalErrorHandler(LocaleFatalErrorHandler);
1198 token=AcquireString(xml);
1199 for (q=(char *) xml; *q != '\0'; )
1204 GetMagickToken(q,&q,token);
1207 (void) CopyMagickString(keyword,token,MaxTextExtent);
1208 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
1213 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
1215 GetMagickToken(q,&q,token);
1216 while (isspace((int) ((unsigned char) *q)) != 0)
1221 if (LocaleNCompare(keyword,"<!--",4) == 0)
1226 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
1228 GetMagickToken(q,&q,token);
1229 while (isspace((int) ((unsigned char) *q)) != 0)
1234 if (LocaleCompare(keyword,"<include") == 0)
1239 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
1241 (void) CopyMagickString(keyword,token,MaxTextExtent);
1242 GetMagickToken(q,&q,token);
1245 GetMagickToken(q,&q,token);
1246 if (LocaleCompare(keyword,"locale") == 0)
1248 if (LocaleCompare(locale,token) != 0)
1252 if (LocaleCompare(keyword,"file") == 0)
1255 (void) ThrowMagickException(exception,GetMagickModule(),
1256 ConfigureError,"IncludeElementNestedTooDeeply","`%s'",token);
1260 path[MaxTextExtent],
1264 GetPathComponent(filename,HeadPath,path);
1266 (void) ConcatenateMagickString(path,DirectorySeparator,
1268 if (*token == *DirectorySeparator)
1269 (void) CopyMagickString(path,token,MaxTextExtent);
1271 (void) ConcatenateMagickString(path,token,MaxTextExtent);
1272 xml=FileToXML(path,~0UL);
1273 if (xml != (char *) NULL)
1275 status&=LoadLocaleCache(locale_cache,xml,path,locale,
1277 xml=(char *) RelinquishMagickMemory(xml);
1284 if (LocaleCompare(keyword,"<locale") == 0)
1289 while ((*token != '>') && (*q != '\0'))
1291 (void) CopyMagickString(keyword,token,MaxTextExtent);
1292 GetMagickToken(q,&q,token);
1295 GetMagickToken(q,&q,token);
1299 if (LocaleCompare(keyword,"</locale>") == 0)
1301 ChopLocaleComponents(tag,1);
1302 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1305 if (LocaleCompare(keyword,"<localemap>") == 0)
1307 if (LocaleCompare(keyword,"</localemap>") == 0)
1309 if (LocaleCompare(keyword,"<message") == 0)
1314 while ((*token != '>') && (*q != '\0'))
1316 (void) CopyMagickString(keyword,token,MaxTextExtent);
1317 GetMagickToken(q,&q,token);
1320 GetMagickToken(q,&q,token);
1321 if (LocaleCompare(keyword,"name") == 0)
1323 (void) ConcatenateMagickString(tag,token,MaxTextExtent);
1324 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1327 for (p=(char *) q; (*q != '<') && (*q != '\0'); q++) ;
1328 while (isspace((int) ((unsigned char) *p)) != 0)
1331 while ((isspace((int) ((unsigned char) *q)) != 0) && (q > p))
1333 (void) CopyMagickString(message,p,MagickMin((size_t) (q-p+2),
1335 locale_info=(LocaleInfo *) AcquireMagickMemory(sizeof(*locale_info));
1336 if (locale_info == (LocaleInfo *) NULL)
1337 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1338 (void) ResetMagickMemory(locale_info,0,sizeof(*locale_info));
1339 locale_info->path=ConstantString(filename);
1340 locale_info->tag=ConstantString(tag);
1341 locale_info->message=ConstantString(message);
1342 locale_info->signature=MagickSignature;
1343 status=AddValueToSplayTree(locale_cache,locale_info->tag,locale_info);
1344 if (status == MagickFalse)
1345 (void) ThrowMagickException(exception,GetMagickModule(),
1346 ResourceLimitError,"MemoryAllocationFailed","`%s'",
1348 (void) ConcatenateMagickString(tag,message,MaxTextExtent);
1349 (void) ConcatenateMagickString(tag,"\n",MaxTextExtent);
1353 if (LocaleCompare(keyword,"</message>") == 0)
1355 ChopLocaleComponents(tag,2);
1356 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1359 if (*keyword == '<')
1364 if (*(keyword+1) == '?')
1366 if (*(keyword+1) == '/')
1368 ChopLocaleComponents(tag,1);
1370 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1373 token[strlen(token)-1]='\0';
1374 (void) CopyMagickString(token,token+1,MaxTextExtent);
1375 (void) ConcatenateMagickString(tag,token,MaxTextExtent);
1376 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1379 GetMagickToken(q,(const char **) NULL,token);
1383 token=(char *) RelinquishMagickMemory(token);
1384 (void) SetFatalErrorHandler(fatal_handler);
1385 return(status != 0 ? MagickTrue : MagickFalse);
1389 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1393 + L o c a l e C o m p o n e n t G e n e s i s %
1397 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1399 % LocaleComponentGenesis() instantiates the locale component.
1401 % The format of the LocaleComponentGenesis method is:
1403 % MagickBooleanType LocaleComponentGenesis(void)
1406 MagickPrivate MagickBooleanType LocaleComponentGenesis(void)
1408 if (locale_semaphore == (SemaphoreInfo *) NULL)
1409 locale_semaphore=AcquireSemaphoreInfo();
1414 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1418 + L o c a l e C o m p o n e n t T e r m i n u s %
1422 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1424 % LocaleComponentTerminus() destroys the locale component.
1426 % The format of the LocaleComponentTerminus method is:
1428 % LocaleComponentTerminus(void)
1431 MagickPrivate void LocaleComponentTerminus(void)
1433 if (locale_semaphore == (SemaphoreInfo *) NULL)
1434 ActivateSemaphoreInfo(&locale_semaphore);
1435 LockSemaphoreInfo(locale_semaphore);
1436 if (locale_cache != (SplayTreeInfo *) NULL)
1437 locale_cache=DestroySplayTree(locale_cache);
1438 #if defined(MAGICKCORE_HAVE_STRTOD_L)
1441 UnlockSemaphoreInfo(locale_semaphore);
1442 RelinquishSemaphoreInfo(&locale_semaphore);