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-2015 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/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"
68 #define LocaleFilename "locale.xml"
69 #define MaxRecursionDepth 200
74 #if defined(__CYGWIN__)
75 typedef struct _locale_t
84 "<?xml version=\"1.0\"?>"
86 " <locale name=\"C\">"
88 " <Message name=\"\">"
95 *locale_semaphore = (SemaphoreInfo *) NULL;
98 *locale_cache = (SplayTreeInfo *) NULL;
100 #if defined(MAGICKCORE_HAVE_STRTOD_L)
101 static volatile locale_t
102 c_locale = (locale_t) NULL;
106 Forward declarations.
108 static MagickBooleanType
109 IsLocaleTreeInstantiated(ExceptionInfo *),
110 LoadLocaleCache(SplayTreeInfo *,const char *,const char *,const char *,
111 const size_t,ExceptionInfo *);
113 #if defined(MAGICKCORE_HAVE_STRTOD_L)
115 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
119 + A c q u i r e C L o c a l e %
123 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
125 % AcquireCLocale() allocates the C locale object, or (locale_t) 0 with
126 % errno set if it cannot be acquired.
128 % The format of the AcquireCLocale method is:
130 % locale_t AcquireCLocale(void)
133 static locale_t AcquireCLocale(void)
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");
147 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
151 % A c q u i r e L o c a l e S p l a y T r e e %
155 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
157 % AcquireLocaleSplayTree() caches one or more locale configurations which
158 % provides a mapping between locale attributes and a locale tag.
160 % The format of the AcquireLocaleSplayTree method is:
162 % SplayTreeInfo *AcquireLocaleSplayTree(const char *filename,
163 % ExceptionInfo *exception)
165 % A description of each parameter follows:
167 % o filename: the font file tag.
169 % o locale: the actual locale.
171 % o exception: return any errors or warnings in this structure.
175 static void *DestroyLocaleNode(void *locale_info)
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));
190 static SplayTreeInfo *AcquireLocaleSplayTree(const char *filename,
191 const char *locale,ExceptionInfo *exception)
199 locale_cache=NewSplayTree(CompareSplayTreeString,(void *(*)(void *)) NULL,
201 if (locale_cache == (SplayTreeInfo *) NULL)
202 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
204 #if !defined(MAGICKCORE_ZERO_CONFIGURATION_SUPPORT)
212 options=GetLocaleOptions(filename,exception);
213 option=(const StringInfo *) GetNextValueInLinkedList(options);
214 while (option != (const StringInfo *) NULL)
216 status&=LoadLocaleCache(locale_cache,(const char *)
217 GetStringInfoDatum(option),GetStringInfoPath(option),locale,0,
219 option=(const StringInfo *) GetNextValueInLinkedList(options);
221 options=DestroyLocaleOptions(options);
222 if (GetNumberOfNodesInSplayTree(locale_cache) == 0)
224 options=GetLocaleOptions("english.xml",exception);
225 option=(const StringInfo *) GetNextValueInLinkedList(options);
226 while (option != (const StringInfo *) NULL)
228 status&=LoadLocaleCache(locale_cache,(const char *)
229 GetStringInfoDatum(option),GetStringInfoPath(option),locale,0,
231 option=(const StringInfo *) GetNextValueInLinkedList(options);
233 options=DestroyLocaleOptions(options);
237 if (GetNumberOfNodesInSplayTree(locale_cache) == 0)
238 status&=LoadLocaleCache(locale_cache,LocaleMap,"built-in",locale,0,
240 return(locale_cache);
243 #if defined(MAGICKCORE_HAVE_STRTOD_L)
245 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
249 + D e s t r o y C L o c a l e %
253 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
255 % DestroyCLocale() releases the resources allocated for a locale object
256 % returned by a call to the AcquireCLocale() method.
258 % The format of the DestroyCLocale method is:
260 % void DestroyCLocale(void)
263 static void DestroyCLocale(void)
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);
272 c_locale=(locale_t) NULL;
277 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
281 % D e s t r o y L o c a l e O p t i o n s %
285 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
287 % DestroyLocaleOptions() releases memory associated with an locale
290 % The format of the DestroyProfiles method is:
292 % LinkedListInfo *DestroyLocaleOptions(Image *image)
294 % A description of each parameter follows:
296 % o image: the image.
300 static void *DestroyOptions(void *message)
302 return(DestroyStringInfo((StringInfo *) message));
305 MagickExport LinkedListInfo *DestroyLocaleOptions(LinkedListInfo *messages)
307 assert(messages != (LinkedListInfo *) NULL);
308 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
309 return(DestroyLinkedList(messages,DestroyOptions));
313 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
317 + F o r m a t L o c a l e F i l e %
321 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
323 % FormatLocaleFile() prints formatted output of a variable argument list to a
324 % file in the "C" locale.
326 % The format of the FormatLocaleFile method is:
328 % ssize_t FormatLocaleFile(FILE *file,const char *format,...)
330 % A description of each parameter follows.
334 % o format: A file describing the format to use to write the remaining
339 MagickPrivate ssize_t FormatLocaleFileList(FILE *file,
340 const char *restrict format,va_list operands)
345 #if defined(MAGICKCORE_HAVE_VFPRINTF_L)
350 locale=AcquireCLocale();
351 if (locale == (locale_t) NULL)
352 n=(ssize_t) vfprintf(file,format,operands);
354 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
355 n=(ssize_t) vfprintf_l(file,format,locale,operands);
357 n=(ssize_t) vfprintf_l(file,locale,format,operands);
361 #if defined(MAGICKCORE_HAVE_USELOCALE) && defined(MAGICKCORE_HAVE_STRTOD_L)
367 locale=AcquireCLocale();
368 if (locale == (locale_t) NULL)
369 n=(ssize_t) vfprintf(file,format,operands);
372 previous_locale=uselocale(locale);
373 n=(ssize_t) vfprintf(file,format,operands);
374 uselocale(previous_locale);
378 n=(ssize_t) vfprintf(file,format,operands);
384 MagickExport ssize_t FormatLocaleFile(FILE *file,const char *restrict format,
393 va_start(operands,format);
394 n=FormatLocaleFileList(file,format,operands);
400 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
404 + F o r m a t L o c a l e S t r i n g %
408 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
410 % FormatLocaleString() prints formatted output of a variable argument list to
411 % a string buffer in the "C" locale.
413 % The format of the FormatLocaleString method is:
415 % ssize_t FormatLocaleString(char *string,const size_t length,
416 % const char *format,...)
418 % A description of each parameter follows.
420 % o string: FormatLocaleString() returns the formatted string in this
423 % o length: the maximum length of the string.
425 % o format: A string describing the format to use to write the remaining
430 MagickPrivate ssize_t FormatLocaleStringList(char *restrict string,
431 const size_t length,const char *restrict format,va_list operands)
436 #if defined(MAGICKCORE_HAVE_VSNPRINTF_L)
441 locale=AcquireCLocale();
442 if (locale == (locale_t) NULL)
443 n=(ssize_t) vsnprintf(string,length,format,operands);
445 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
446 n=(ssize_t) vsnprintf_l(string,length,format,locale,operands);
448 n=(ssize_t) vsnprintf_l(string,length,locale,format,operands);
451 #elif defined(MAGICKCORE_HAVE_VSNPRINTF)
452 #if defined(MAGICKCORE_HAVE_USELOCALE) && defined(MAGICKCORE_HAVE_STRTOD_L)
458 locale=AcquireCLocale();
459 if (locale == (locale_t) NULL)
460 n=(ssize_t) vsnprintf(string,length,format,operands);
463 previous_locale=uselocale(locale);
464 n=(ssize_t) vsnprintf(string,length,format,operands);
465 uselocale(previous_locale);
469 n=(ssize_t) vsnprintf(string,length,format,operands);
472 n=(ssize_t) vsprintf(string,format,operands);
475 string[length-1]='\0';
479 MagickExport ssize_t FormatLocaleString(char *restrict string,
480 const size_t length,const char *restrict format,...)
488 va_start(operands,format);
489 n=FormatLocaleStringList(string,length,format,operands);
495 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
499 + G e t L o c a l e I n f o _ %
503 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
505 % GetLocaleInfo_() searches the locale list for the specified tag and if
506 % found returns attributes for that element.
508 % The format of the GetLocaleInfo method is:
510 % const LocaleInfo *GetLocaleInfo_(const char *tag,
511 % ExceptionInfo *exception)
513 % A description of each parameter follows:
515 % o tag: the locale tag.
517 % o exception: return any errors or warnings in this structure.
520 MagickExport const LocaleInfo *GetLocaleInfo_(const char *tag,
521 ExceptionInfo *exception)
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))
532 ResetSplayTreeIterator(locale_cache);
533 locale_info=(const LocaleInfo *) GetNextValueInSplayTree(locale_cache);
534 UnlockSemaphoreInfo(locale_semaphore);
537 locale_info=(const LocaleInfo *) GetValueFromSplayTree(locale_cache,tag);
538 UnlockSemaphoreInfo(locale_semaphore);
543 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
547 % G e t L o c a l e I n f o L i s t %
551 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
553 % GetLocaleInfoList() returns any locale messages that match the
556 % The format of the GetLocaleInfoList function is:
558 % const LocaleInfo **GetLocaleInfoList(const char *pattern,
559 % size_t *number_messages,ExceptionInfo *exception)
561 % A description of each parameter follows:
563 % o pattern: Specifies a pointer to a text string containing a pattern.
565 % o number_messages: This integer returns the number of locale messages in
568 % o exception: return any errors or warnings in this structure.
572 #if defined(__cplusplus) || defined(c_plusplus)
576 static int LocaleInfoCompare(const void *x,const void *y)
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));
589 #if defined(__cplusplus) || defined(c_plusplus)
593 MagickExport const LocaleInfo **GetLocaleInfoList(const char *pattern,
594 size_t *number_messages,ExceptionInfo *exception)
599 register const LocaleInfo
606 Allocate locale list.
608 assert(pattern != (char *) NULL);
609 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
610 assert(number_messages != (size_t *) NULL);
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);
620 Generate locale list.
622 LockSemaphoreInfo(locale_semaphore);
623 ResetSplayTreeIterator(locale_cache);
624 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_cache);
625 for (i=0; p != (const LocaleInfo *) NULL; )
627 if ((p->stealth == MagickFalse) &&
628 (GlobExpression(p->tag,pattern,MagickTrue) != MagickFalse))
630 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_cache);
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;
640 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
644 % G e t L o c a l e L i s t %
648 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
650 % GetLocaleList() returns any locale messages that match the specified
653 % The format of the GetLocaleList function is:
655 % char **GetLocaleList(const char *pattern,size_t *number_messages,
656 % Exceptioninfo *exception)
658 % A description of each parameter follows:
660 % o pattern: Specifies a pointer to a text string containing a pattern.
662 % o number_messages: This integer returns the number of messages in the
665 % o exception: return any errors or warnings in this structure.
669 #if defined(__cplusplus) || defined(c_plusplus)
673 static int LocaleTagCompare(const void *x,const void *y)
681 return(LocaleCompare(*p,*q));
684 #if defined(__cplusplus) || defined(c_plusplus)
688 MagickExport char **GetLocaleList(const char *pattern,
689 size_t *number_messages,ExceptionInfo *exception)
694 register const LocaleInfo
701 Allocate locale list.
703 assert(pattern != (char *) NULL);
704 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
705 assert(number_messages != (size_t *) NULL);
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; )
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);
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;
731 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
735 % G e t L o c a l e M e s s a g e %
739 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
741 % GetLocaleMessage() returns a message in the current locale that matches the
744 % The format of the GetLocaleMessage method is:
746 % const char *GetLocaleMessage(const char *tag)
748 % A description of each parameter follows:
750 % o tag: Return a message that matches this tag in the current locale.
753 MagickExport const char *GetLocaleMessage(const char *tag)
764 if ((tag == (const char *) NULL) || (*tag == '\0'))
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);
776 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
780 % G e t L o c a l e O p t i o n s %
784 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
786 % GetLocaleOptions() returns any Magick configuration messages associated
787 % with the specified filename.
789 % The format of the GetLocaleOptions method is:
791 % LinkedListInfo *GetLocaleOptions(const char *filename,
792 % ExceptionInfo *exception)
794 % A description of each parameter follows:
796 % o filename: the locale file tag.
798 % o exception: return any errors or warnings in this structure.
801 MagickExport LinkedListInfo *GetLocaleOptions(const char *filename,
802 ExceptionInfo *exception)
817 assert(filename != (const char *) NULL);
818 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
819 assert(exception != (ExceptionInfo *) NULL);
820 (void) CopyMagickString(path,filename,MaxTextExtent);
822 Load XML from configuration files to linked-list.
824 messages=NewLinkedList(0);
825 paths=GetConfigurePaths(filename,exception);
826 if (paths != (LinkedListInfo *) NULL)
828 ResetLinkedListIterator(paths);
829 element=(const char *) GetNextValueInLinkedList(paths);
830 while (element != (const char *) NULL)
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);
840 paths=DestroyLinkedList(paths,RelinquishMagickMemory);
842 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
847 blob=(char *) NTResourceToBlob(filename);
848 if (blob != (char *) NULL)
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);
859 ResetLinkedListIterator(messages);
864 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
868 % G e t L o c a l e V a l u e %
872 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
874 % GetLocaleValue() returns the message associated with the locale info.
876 % The format of the GetLocaleValue method is:
878 % const char *GetLocaleValue(const LocaleInfo *locale_info)
880 % A description of each parameter follows:
882 % o locale_info: The locale info.
885 MagickExport const char *GetLocaleValue(const LocaleInfo *locale_info)
887 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
888 assert(locale_info != (LocaleInfo *) NULL);
889 assert(locale_info->signature == MagickSignature);
890 return(locale_info->message);
894 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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 %
902 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
904 % IsLocaleTreeInstantiated() determines if the locale tree is instantiated.
905 % If not, it instantiates the tree and returns it.
907 % The format of the IsLocaleInstantiated method is:
909 % MagickBooleanType IsLocaleTreeInstantiated(ExceptionInfo *exception)
911 % A description of each parameter follows.
913 % o exception: return any errors or warnings in this structure.
916 static MagickBooleanType IsLocaleTreeInstantiated(ExceptionInfo *exception)
918 if (locale_cache == (SplayTreeInfo *) NULL)
920 if (locale_semaphore == (SemaphoreInfo *) NULL)
921 ActivateSemaphoreInfo(&locale_semaphore);
922 LockSemaphoreInfo(locale_semaphore);
923 if (locale_cache == (SplayTreeInfo *) NULL)
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);
948 UnlockSemaphoreInfo(locale_semaphore);
950 return(locale_cache != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
954 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
958 + I n t e r p r e t L o c a l e V a l u e %
962 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
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.
969 % The format of the InterpretLocaleValue method is:
971 % double InterpretLocaleValue(const char *value,char **sentinal)
973 % A description of each parameter follows:
975 % o value: the string value.
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.
982 MagickExport double InterpretLocaleValue(const char *restrict string,
983 char **restrict sentinal)
991 if ((*string == '0') && ((string[1] | 0x20)=='x'))
992 value=(double) strtoul(string,&q,16);
995 #if defined(MAGICKCORE_HAVE_STRTOD_L)
999 locale=AcquireCLocale();
1000 if (locale == (locale_t) NULL)
1001 value=strtod(string,&q);
1003 value=strtod_l(string,&q,locale);
1005 value=strtod(string,&q);
1008 if (sentinal != (char **) NULL)
1014 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1018 % L i s t L o c a l e I n f o %
1022 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1024 % ListLocaleInfo() lists the locale info to a file.
1026 % The format of the ListLocaleInfo method is:
1028 % MagickBooleanType ListLocaleInfo(FILE *file,ExceptionInfo *exception)
1030 % A description of each parameter follows.
1032 % o file: An pointer to a FILE.
1034 % o exception: return any errors or warnings in this structure.
1037 MagickExport MagickBooleanType ListLocaleInfo(FILE *file,
1038 ExceptionInfo *exception)
1052 if (file == (const FILE *) NULL)
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++)
1061 if (locale_info[i]->stealth != MagickFalse)
1063 if ((path == (const char *) NULL) ||
1064 (LocaleCompare(path,locale_info[i]->path) != 0))
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");
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");
1079 (void) fflush(file);
1080 locale_info=(const LocaleInfo **)
1081 RelinquishMagickMemory((void *) locale_info);
1086 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1090 + L o a d L o c a l e L i s t %
1094 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1096 % LoadLocaleCache() loads the locale configurations which provides a mapping
1097 % between locale attributes and a locale name.
1099 % The format of the LoadLocaleCache method is:
1101 % MagickBooleanType LoadLocaleCache(SplayTreeInfo *locale_cache,
1102 % const char *xml,const char *filename,const size_t depth,
1103 % ExceptionInfo *exception)
1105 % A description of each parameter follows:
1107 % o xml: The locale list in XML format.
1109 % o filename: The locale list filename.
1111 % o depth: depth of <include /> statements.
1113 % o exception: return any errors or warnings in this structure.
1117 static void ChopLocaleComponents(char *path,const size_t components)
1127 p=path+strlen(path)-1;
1130 for (count=0; (count < (ssize_t) components) && (p > path); p--)
1136 if (count < (ssize_t) components)
1140 static void LocaleFatalErrorHandler(
1141 const ExceptionType magick_unused(severity),
1142 const char *reason,const char *description)
1144 if (reason == (char *) NULL)
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);
1154 static MagickBooleanType LoadLocaleCache(SplayTreeInfo *locale_cache,
1155 const char *xml,const char *filename,const char *locale,const size_t depth,
1156 ExceptionInfo *exception)
1159 keyword[MaxTextExtent],
1160 message[MaxTextExtent],
1180 Read the locale configure file.
1182 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1183 "Loading locale configure file \"%s\" ...",filename);
1184 if (xml == (const char *) NULL)
1185 return(MagickFalse);
1187 locale_info=(LocaleInfo *) NULL;
1191 fatal_handler=SetFatalErrorHandler(LocaleFatalErrorHandler);
1192 token=AcquireString(xml);
1193 for (q=(char *) xml; *q != '\0'; )
1198 GetMagickToken(q,&q,token);
1201 (void) CopyMagickString(keyword,token,MaxTextExtent);
1202 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
1207 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
1209 GetMagickToken(q,&q,token);
1210 while (isspace((int) ((unsigned char) *q)) != 0)
1215 if (LocaleNCompare(keyword,"<!--",4) == 0)
1220 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
1222 GetMagickToken(q,&q,token);
1223 while (isspace((int) ((unsigned char) *q)) != 0)
1228 if (LocaleCompare(keyword,"<include") == 0)
1233 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
1235 (void) CopyMagickString(keyword,token,MaxTextExtent);
1236 GetMagickToken(q,&q,token);
1239 GetMagickToken(q,&q,token);
1240 if (LocaleCompare(keyword,"locale") == 0)
1242 if (LocaleCompare(locale,token) != 0)
1246 if (LocaleCompare(keyword,"file") == 0)
1249 (void) ThrowMagickException(exception,GetMagickModule(),
1250 ConfigureError,"IncludeElementNestedTooDeeply","`%s'",token);
1254 path[MaxTextExtent],
1258 GetPathComponent(filename,HeadPath,path);
1260 (void) ConcatenateMagickString(path,DirectorySeparator,
1262 if (*token == *DirectorySeparator)
1263 (void) CopyMagickString(path,token,MaxTextExtent);
1265 (void) ConcatenateMagickString(path,token,MaxTextExtent);
1266 xml=FileToXML(path,~0UL);
1267 if (xml != (char *) NULL)
1269 status&=LoadLocaleCache(locale_cache,xml,path,locale,
1271 xml=(char *) RelinquishMagickMemory(xml);
1278 if (LocaleCompare(keyword,"<locale") == 0)
1283 while ((*token != '>') && (*q != '\0'))
1285 (void) CopyMagickString(keyword,token,MaxTextExtent);
1286 GetMagickToken(q,&q,token);
1289 GetMagickToken(q,&q,token);
1293 if (LocaleCompare(keyword,"</locale>") == 0)
1295 ChopLocaleComponents(tag,1);
1296 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1299 if (LocaleCompare(keyword,"<localemap>") == 0)
1301 if (LocaleCompare(keyword,"</localemap>") == 0)
1303 if (LocaleCompare(keyword,"<message") == 0)
1308 while ((*token != '>') && (*q != '\0'))
1310 (void) CopyMagickString(keyword,token,MaxTextExtent);
1311 GetMagickToken(q,&q,token);
1314 GetMagickToken(q,&q,token);
1315 if (LocaleCompare(keyword,"name") == 0)
1317 (void) ConcatenateMagickString(tag,token,MaxTextExtent);
1318 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1321 for (p=(char *) q; (*q != '<') && (*q != '\0'); q++) ;
1322 while (isspace((int) ((unsigned char) *p)) != 0)
1325 while ((isspace((int) ((unsigned char) *q)) != 0) && (q > p))
1327 (void) CopyMagickString(message,p,MagickMin((size_t) (q-p+2),
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'",
1342 (void) ConcatenateMagickString(tag,message,MaxTextExtent);
1343 (void) ConcatenateMagickString(tag,"\n",MaxTextExtent);
1347 if (LocaleCompare(keyword,"</message>") == 0)
1349 ChopLocaleComponents(tag,2);
1350 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1353 if (*keyword == '<')
1358 if (*(keyword+1) == '?')
1360 if (*(keyword+1) == '/')
1362 ChopLocaleComponents(tag,1);
1364 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1367 token[strlen(token)-1]='\0';
1368 (void) CopyMagickString(token,token+1,MaxTextExtent);
1369 (void) ConcatenateMagickString(tag,token,MaxTextExtent);
1370 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1373 GetMagickToken(q,(const char **) NULL,token);
1377 token=(char *) RelinquishMagickMemory(token);
1378 (void) SetFatalErrorHandler(fatal_handler);
1379 return(status != 0 ? MagickTrue : MagickFalse);
1383 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1387 + L o c a l e C o m p o n e n t G e n e s i s %
1391 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1393 % LocaleComponentGenesis() instantiates the locale component.
1395 % The format of the LocaleComponentGenesis method is:
1397 % MagickBooleanType LocaleComponentGenesis(void)
1400 MagickPrivate MagickBooleanType LocaleComponentGenesis(void)
1402 if (locale_semaphore == (SemaphoreInfo *) NULL)
1403 locale_semaphore=AcquireSemaphoreInfo();
1408 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1412 + L o c a l e C o m p o n e n t T e r m i n u s %
1416 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1418 % LocaleComponentTerminus() destroys the locale component.
1420 % The format of the LocaleComponentTerminus method is:
1422 % LocaleComponentTerminus(void)
1425 MagickPrivate void LocaleComponentTerminus(void)
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)
1435 UnlockSemaphoreInfo(locale_semaphore);
1436 RelinquishSemaphoreInfo(&locale_semaphore);