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
76 "<?xml version=\"1.0\"?>"
78 " <locale name=\"C\">"
80 " <Message name=\"\">"
87 *locale_semaphore = (SemaphoreInfo *) NULL;
90 *locale_cache = (SplayTreeInfo *) NULL;
92 #if defined(MAGICKCORE_HAVE_LOCALE_H)
93 static volatile locale_t
94 c_locale = (locale_t) NULL;
100 static MagickBooleanType
101 IsLocaleTreeInstantiated(ExceptionInfo *),
102 LoadLocaleCache(SplayTreeInfo *,const char *,const char *,const char *,
103 const size_t,ExceptionInfo *);
105 #if defined(MAGICKCORE_HAVE_LOCALE_H)
107 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
111 + A c q u i r e C L o c a l e %
115 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
117 % AcquireCLocale() allocates the C locale object, or (locale_t) 0 with
118 % errno set if it cannot be acquired.
120 % The format of the AcquireCLocale method is:
122 % locale_t AcquireCLocale(void)
125 static locale_t AcquireCLocale(void)
127 #if defined(MAGICKCORE_HAVE_NEWLOCALE)
128 if (c_locale == (locale_t) NULL)
129 c_locale=newlocale(LC_ALL_MASK,"C",(locale_t) 0);
130 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
131 if (c_locale == (locale_t) NULL)
132 c_locale=_create_locale(LC_ALL,"C");
139 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
143 % A c q u i r e L o c a l e S p l a y T r e e %
147 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
149 % AcquireLocaleSplayTree() caches one or more locale configurations which
150 % provides a mapping between locale attributes and a locale tag.
152 % The format of the AcquireLocaleSplayTree method is:
154 % SplayTreeInfo *AcquireLocaleSplayTree(const char *filename,
155 % ExceptionInfo *exception)
157 % A description of each parameter follows:
159 % o filename: the font file tag.
161 % o locale: the actual locale.
163 % o exception: return any errors or warnings in this structure.
167 static void *DestroyLocaleNode(void *locale_info)
172 p=(LocaleInfo *) locale_info;
173 if (p->path != (char *) NULL)
174 p->path=DestroyString(p->path);
175 if (p->tag != (char *) NULL)
176 p->tag=DestroyString(p->tag);
177 if (p->message != (char *) NULL)
178 p->message=DestroyString(p->message);
179 return(RelinquishMagickMemory(p));
182 static SplayTreeInfo *AcquireLocaleSplayTree(const char *filename,
183 const char *locale,ExceptionInfo *exception)
191 locale_cache=NewSplayTree(CompareSplayTreeString,(void *(*)(void *)) NULL,
193 if (locale_cache == (SplayTreeInfo *) NULL)
194 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
196 #if !defined(MAGICKCORE_ZERO_CONFIGURATION_SUPPORT)
204 options=GetLocaleOptions(filename,exception);
205 option=(const StringInfo *) GetNextValueInLinkedList(options);
206 while (option != (const StringInfo *) NULL)
208 status&=LoadLocaleCache(locale_cache,(const char *)
209 GetStringInfoDatum(option),GetStringInfoPath(option),locale,0,
211 option=(const StringInfo *) GetNextValueInLinkedList(options);
213 options=DestroyLocaleOptions(options);
214 if (GetNumberOfNodesInSplayTree(locale_cache) == 0)
216 options=GetLocaleOptions("english.xml",exception);
217 option=(const StringInfo *) GetNextValueInLinkedList(options);
218 while (option != (const StringInfo *) NULL)
220 status&=LoadLocaleCache(locale_cache,(const char *)
221 GetStringInfoDatum(option),GetStringInfoPath(option),locale,0,
223 option=(const StringInfo *) GetNextValueInLinkedList(options);
225 options=DestroyLocaleOptions(options);
229 if (GetNumberOfNodesInSplayTree(locale_cache) == 0)
230 status&=LoadLocaleCache(locale_cache,LocaleMap,"built-in",locale,0,
232 return(locale_cache);
235 #if defined(MAGICKCORE_HAVE_LOCALE_H)
237 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
241 + D e s t r o y C L o c a l e %
245 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
247 % DestroyCLocale() releases the resources allocated for a locale object
248 % returned by a call to the AcquireCLocale() method.
250 % The format of the DestroyCLocale method is:
252 % void DestroyCLocale(void)
255 static void DestroyCLocale(void)
257 #if defined(MAGICKCORE_HAVE_NEWLOCALE)
258 if (c_locale != (locale_t) NULL)
259 freelocale(c_locale);
260 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
261 if (c_locale != (locale_t) NULL)
262 _free_locale(c_locale);
264 c_locale=(locale_t) NULL;
269 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
273 % D e s t r o y L o c a l e O p t i o n s %
277 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
279 % DestroyLocaleOptions() releases memory associated with an locale
282 % The format of the DestroyProfiles method is:
284 % LinkedListInfo *DestroyLocaleOptions(Image *image)
286 % A description of each parameter follows:
288 % o image: the image.
292 static void *DestroyOptions(void *message)
294 return(DestroyStringInfo((StringInfo *) message));
297 MagickExport LinkedListInfo *DestroyLocaleOptions(LinkedListInfo *messages)
299 assert(messages != (LinkedListInfo *) NULL);
300 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
301 return(DestroyLinkedList(messages,DestroyOptions));
305 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
309 + F o r m a t L o c a l e F i l e %
313 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
315 % FormatLocaleFile() prints formatted output of a variable argument list to a
316 % file in the "C" locale.
318 % The format of the FormatLocaleFile method is:
320 % ssize_t FormatLocaleFile(FILE *file,const char *format,...)
322 % A description of each parameter follows.
326 % o format: A file describing the format to use to write the remaining
331 MagickPrivate ssize_t FormatLocaleFileList(FILE *file,
332 const char *restrict format,va_list operands)
337 #if defined(MAGICKCORE_HAVE_LOCALE_H) && defined(MAGICKCORE_HAVE_VFPRINTF_L)
342 locale=AcquireCLocale();
343 if (locale == (locale_t) NULL)
344 n=(ssize_t) vfprintf(file,format,operands);
346 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
347 n=(ssize_t) vfprintf_l(file,format,locale,operands);
349 n=(ssize_t) vfprintf_l(file,locale,format,operands);
353 #if defined(MAGICKCORE_HAVE_LOCALE_H) && defined(MAGICKCORE_HAVE_USELOCALE)
359 locale=AcquireCLocale();
360 if (locale == (locale_t) NULL)
361 n=(ssize_t) vfprintf(file,format,operands);
364 previous_locale=uselocale(locale);
365 n=(ssize_t) vfprintf(file,format,operands);
366 uselocale(previous_locale);
370 n=(ssize_t) vfprintf(file,format,operands);
376 MagickExport ssize_t FormatLocaleFile(FILE *file,const char *restrict format,
385 va_start(operands,format);
386 n=FormatLocaleFileList(file,format,operands);
392 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
396 + F o r m a t L o c a l e S t r i n g %
400 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
402 % FormatLocaleString() prints formatted output of a variable argument list to
403 % a string buffer in the "C" locale.
405 % The format of the FormatLocaleString method is:
407 % ssize_t FormatLocaleString(char *string,const size_t length,
408 % const char *format,...)
410 % A description of each parameter follows.
412 % o string: FormatLocaleString() returns the formatted string in this
415 % o length: the maximum length of the string.
417 % o format: A string describing the format to use to write the remaining
422 MagickPrivate ssize_t FormatLocaleStringList(char *restrict string,
423 const size_t length,const char *restrict format,va_list operands)
428 #if defined(MAGICKCORE_HAVE_LOCALE_H) && defined(MAGICKCORE_HAVE_VSNPRINTF_L)
433 locale=AcquireCLocale();
434 if (locale == (locale_t) NULL)
435 n=(ssize_t) vsnprintf(string,length,format,operands);
437 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
438 n=(ssize_t) vsnprintf_l(string,length,format,locale,operands);
440 n=(ssize_t) vsnprintf_l(string,length,locale,format,operands);
443 #elif defined(MAGICKCORE_HAVE_VSNPRINTF)
444 #if defined(MAGICKCORE_HAVE_LOCALE_H) && defined(MAGICKCORE_HAVE_USELOCALE)
450 locale=AcquireCLocale();
451 if (locale == (locale_t) NULL)
452 n=(ssize_t) vsnprintf(string,length,format,operands);
455 previous_locale=uselocale(locale);
456 n=(ssize_t) vsnprintf(string,length,format,operands);
457 uselocale(previous_locale);
461 n=(ssize_t) vsnprintf(string,length,format,operands);
464 n=(ssize_t) vsprintf(string,format,operands);
467 string[length-1]='\0';
471 MagickExport ssize_t FormatLocaleString(char *restrict string,
472 const size_t length,const char *restrict format,...)
480 va_start(operands,format);
481 n=FormatLocaleStringList(string,length,format,operands);
487 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
491 + G e t L o c a l e I n f o _ %
495 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
497 % GetLocaleInfo_() searches the locale list for the specified tag and if
498 % found returns attributes for that element.
500 % The format of the GetLocaleInfo method is:
502 % const LocaleInfo *GetLocaleInfo_(const char *tag,
503 % ExceptionInfo *exception)
505 % A description of each parameter follows:
507 % o tag: the locale tag.
509 % o exception: return any errors or warnings in this structure.
512 MagickExport const LocaleInfo *GetLocaleInfo_(const char *tag,
513 ExceptionInfo *exception)
518 assert(exception != (ExceptionInfo *) NULL);
519 if (IsLocaleTreeInstantiated(exception) == MagickFalse)
520 return((const LocaleInfo *) NULL);
521 LockSemaphoreInfo(locale_semaphore);
522 if ((tag == (const char *) NULL) || (LocaleCompare(tag,"*") == 0))
524 ResetSplayTreeIterator(locale_cache);
525 locale_info=(const LocaleInfo *) GetNextValueInSplayTree(locale_cache);
526 UnlockSemaphoreInfo(locale_semaphore);
529 locale_info=(const LocaleInfo *) GetValueFromSplayTree(locale_cache,tag);
530 UnlockSemaphoreInfo(locale_semaphore);
535 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
539 % G e t L o c a l e I n f o L i s t %
543 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
545 % GetLocaleInfoList() returns any locale messages that match the
548 % The format of the GetLocaleInfoList function is:
550 % const LocaleInfo **GetLocaleInfoList(const char *pattern,
551 % size_t *number_messages,ExceptionInfo *exception)
553 % A description of each parameter follows:
555 % o pattern: Specifies a pointer to a text string containing a pattern.
557 % o number_messages: This integer returns the number of locale messages in
560 % o exception: return any errors or warnings in this structure.
564 #if defined(__cplusplus) || defined(c_plusplus)
568 static int LocaleInfoCompare(const void *x,const void *y)
574 p=(const LocaleInfo **) x,
575 q=(const LocaleInfo **) y;
576 if (LocaleCompare((*p)->path,(*q)->path) == 0)
577 return(LocaleCompare((*p)->tag,(*q)->tag));
578 return(LocaleCompare((*p)->path,(*q)->path));
581 #if defined(__cplusplus) || defined(c_plusplus)
585 MagickExport const LocaleInfo **GetLocaleInfoList(const char *pattern,
586 size_t *number_messages,ExceptionInfo *exception)
591 register const LocaleInfo
598 Allocate locale list.
600 assert(pattern != (char *) NULL);
601 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
602 assert(number_messages != (size_t *) NULL);
604 p=GetLocaleInfo_("*",exception);
605 if (p == (const LocaleInfo *) NULL)
606 return((const LocaleInfo **) NULL);
607 messages=(const LocaleInfo **) AcquireQuantumMemory((size_t)
608 GetNumberOfNodesInSplayTree(locale_cache)+1UL,sizeof(*messages));
609 if (messages == (const LocaleInfo **) NULL)
610 return((const LocaleInfo **) NULL);
612 Generate locale list.
614 LockSemaphoreInfo(locale_semaphore);
615 ResetSplayTreeIterator(locale_cache);
616 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_cache);
617 for (i=0; p != (const LocaleInfo *) NULL; )
619 if ((p->stealth == MagickFalse) &&
620 (GlobExpression(p->tag,pattern,MagickTrue) != MagickFalse))
622 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_cache);
624 UnlockSemaphoreInfo(locale_semaphore);
625 qsort((void *) messages,(size_t) i,sizeof(*messages),LocaleInfoCompare);
626 messages[i]=(LocaleInfo *) NULL;
627 *number_messages=(size_t) i;
632 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
636 % G e t L o c a l e L i s t %
640 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
642 % GetLocaleList() returns any locale messages that match the specified
645 % The format of the GetLocaleList function is:
647 % char **GetLocaleList(const char *pattern,size_t *number_messages,
648 % Exceptioninfo *exception)
650 % A description of each parameter follows:
652 % o pattern: Specifies a pointer to a text string containing a pattern.
654 % o number_messages: This integer returns the number of messages in the
657 % o exception: return any errors or warnings in this structure.
661 #if defined(__cplusplus) || defined(c_plusplus)
665 static int LocaleTagCompare(const void *x,const void *y)
673 return(LocaleCompare(*p,*q));
676 #if defined(__cplusplus) || defined(c_plusplus)
680 MagickExport char **GetLocaleList(const char *pattern,size_t *number_messages,
681 ExceptionInfo *exception)
686 register const LocaleInfo
693 Allocate locale list.
695 assert(pattern != (char *) NULL);
696 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
697 assert(number_messages != (size_t *) NULL);
699 p=GetLocaleInfo_("*",exception);
700 if (p == (const LocaleInfo *) NULL)
701 return((char **) NULL);
702 messages=(char **) AcquireQuantumMemory((size_t)
703 GetNumberOfNodesInSplayTree(locale_cache)+1UL,sizeof(*messages));
704 if (messages == (char **) NULL)
705 return((char **) NULL);
706 LockSemaphoreInfo(locale_semaphore);
707 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_cache);
708 for (i=0; p != (const LocaleInfo *) NULL; )
710 if ((p->stealth == MagickFalse) &&
711 (GlobExpression(p->tag,pattern,MagickTrue) != MagickFalse))
712 messages[i++]=ConstantString(p->tag);
713 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_cache);
715 UnlockSemaphoreInfo(locale_semaphore);
716 qsort((void *) messages,(size_t) i,sizeof(*messages),LocaleTagCompare);
717 messages[i]=(char *) NULL;
718 *number_messages=(size_t) i;
723 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
727 % G e t L o c a l e M e s s a g e %
731 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
733 % GetLocaleMessage() returns a message in the current locale that matches the
736 % The format of the GetLocaleMessage method is:
738 % const char *GetLocaleMessage(const char *tag)
740 % A description of each parameter follows:
742 % o tag: Return a message that matches this tag in the current locale.
745 MagickExport const char *GetLocaleMessage(const char *tag)
756 if ((tag == (const char *) NULL) || (*tag == '\0'))
758 exception=AcquireExceptionInfo();
759 (void) FormatLocaleString(name,MaxTextExtent,"%s/",tag);
760 locale_info=GetLocaleInfo_(name,exception);
761 exception=DestroyExceptionInfo(exception);
762 if (locale_info != (const LocaleInfo *) NULL)
763 return(locale_info->message);
768 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
772 % G e t L o c a l e O p t i o n s %
776 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
778 % GetLocaleOptions() returns any Magick configuration messages associated
779 % with the specified filename.
781 % The format of the GetLocaleOptions method is:
783 % LinkedListInfo *GetLocaleOptions(const char *filename,
784 % ExceptionInfo *exception)
786 % A description of each parameter follows:
788 % o filename: the locale file tag.
790 % o exception: return any errors or warnings in this structure.
793 MagickExport LinkedListInfo *GetLocaleOptions(const char *filename,
794 ExceptionInfo *exception)
809 assert(filename != (const char *) NULL);
810 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
811 assert(exception != (ExceptionInfo *) NULL);
812 (void) CopyMagickString(path,filename,MaxTextExtent);
814 Load XML from configuration files to linked-list.
816 messages=NewLinkedList(0);
817 paths=GetConfigurePaths(filename,exception);
818 if (paths != (LinkedListInfo *) NULL)
820 ResetLinkedListIterator(paths);
821 element=(const char *) GetNextValueInLinkedList(paths);
822 while (element != (const char *) NULL)
824 (void) FormatLocaleString(path,MaxTextExtent,"%s%s",element,filename);
825 (void) LogMagickEvent(LocaleEvent,GetMagickModule(),
826 "Searching for locale file: \"%s\"",path);
827 xml=ConfigureFileToStringInfo(path);
828 if (xml != (StringInfo *) NULL)
829 (void) AppendValueToLinkedList(messages,xml);
830 element=(const char *) GetNextValueInLinkedList(paths);
832 paths=DestroyLinkedList(paths,RelinquishMagickMemory);
834 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
839 blob=(char *) NTResourceToBlob(filename);
840 if (blob != (char *) NULL)
842 xml=AcquireStringInfo(0);
843 SetStringInfoLength(xml,strlen(blob)+1);
844 SetStringInfoDatum(xml,(const unsigned char *) blob);
845 blob=(char *) RelinquishMagickMemory(blob);
846 SetStringInfoPath(xml,filename);
847 (void) AppendValueToLinkedList(messages,xml);
851 ResetLinkedListIterator(messages);
856 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
860 % G e t L o c a l e V a l u e %
864 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
866 % GetLocaleValue() returns the message associated with the locale info.
868 % The format of the GetLocaleValue method is:
870 % const char *GetLocaleValue(const LocaleInfo *locale_info)
872 % A description of each parameter follows:
874 % o locale_info: The locale info.
877 MagickExport const char *GetLocaleValue(const LocaleInfo *locale_info)
879 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
880 assert(locale_info != (LocaleInfo *) NULL);
881 assert(locale_info->signature == MagickSignature);
882 return(locale_info->message);
886 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
890 + I s L o c a l e T r e e I n s t a n t i a t e d %
894 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
896 % IsLocaleTreeInstantiated() determines if the locale tree is instantiated.
897 % If not, it instantiates the tree and returns it.
899 % The format of the IsLocaleInstantiated method is:
901 % MagickBooleanType IsLocaleTreeInstantiated(ExceptionInfo *exception)
903 % A description of each parameter follows.
905 % o exception: return any errors or warnings in this structure.
908 static MagickBooleanType IsLocaleTreeInstantiated(ExceptionInfo *exception)
910 if (locale_cache == (SplayTreeInfo *) NULL)
912 if (locale_semaphore == (SemaphoreInfo *) NULL)
913 ActivateSemaphoreInfo(&locale_semaphore);
914 LockSemaphoreInfo(locale_semaphore);
915 if (locale_cache == (SplayTreeInfo *) NULL)
923 locale=(char *) NULL;
924 p=setlocale(LC_CTYPE,(const char *) NULL);
925 if (p != (const char *) NULL)
926 locale=ConstantString(p);
927 if (locale == (char *) NULL)
928 locale=GetEnvironmentValue("LC_ALL");
929 if (locale == (char *) NULL)
930 locale=GetEnvironmentValue("LC_MESSAGES");
931 if (locale == (char *) NULL)
932 locale=GetEnvironmentValue("LC_CTYPE");
933 if (locale == (char *) NULL)
934 locale=GetEnvironmentValue("LANG");
935 if (locale == (char *) NULL)
936 locale=ConstantString("C");
937 locale_cache=AcquireLocaleSplayTree(LocaleFilename,locale,exception);
938 locale=DestroyString(locale);
940 UnlockSemaphoreInfo(locale_semaphore);
942 return(locale_cache != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
946 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
950 + I n t e r p r e t L o c a l e V a l u e %
954 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
956 % InterpretLocaleValue() interprets the string as a floating point number in
957 % the "C" locale and returns its value as a double. If sentinal is not a null
958 % pointer, the method also sets the value pointed by sentinal to point to the
959 % first character after the number.
961 % The format of the InterpretLocaleValue method is:
963 % double InterpretLocaleValue(const char *value,char **sentinal)
965 % A description of each parameter follows:
967 % o value: the string value.
969 % o sentinal: if sentinal is not NULL, a pointer to the character after the
970 % last character used in the conversion is stored in the location
971 % referenced by sentinal.
974 MagickExport double InterpretLocaleValue(const char *restrict string,
975 char **restrict sentinal)
983 if ((*string == '0') && ((string[1] | 0x20)=='x'))
984 value=(double) strtoul(string,&q,16);
987 #if defined(MAGICKCORE_HAVE_LOCALE_H) && defined(MAGICKCORE_HAVE_STRTOD_L)
991 locale=AcquireCLocale();
992 if (locale == (locale_t) NULL)
993 value=strtod(string,&q);
995 value=strtod_l(string,&q,locale);
997 value=strtod(string,&q);
1000 if (sentinal != (char **) NULL)
1006 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1010 % L i s t L o c a l e I n f o %
1014 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1016 % ListLocaleInfo() lists the locale info to a file.
1018 % The format of the ListLocaleInfo method is:
1020 % MagickBooleanType ListLocaleInfo(FILE *file,ExceptionInfo *exception)
1022 % A description of each parameter follows.
1024 % o file: An pointer to a FILE.
1026 % o exception: return any errors or warnings in this structure.
1029 MagickExport MagickBooleanType ListLocaleInfo(FILE *file,
1030 ExceptionInfo *exception)
1044 if (file == (const FILE *) NULL)
1047 locale_info=GetLocaleInfoList("*",&number_messages,exception);
1048 if (locale_info == (const LocaleInfo **) NULL)
1049 return(MagickFalse);
1050 path=(const char *) NULL;
1051 for (i=0; i < (ssize_t) number_messages; i++)
1053 if (locale_info[i]->stealth != MagickFalse)
1055 if ((path == (const char *) NULL) ||
1056 (LocaleCompare(path,locale_info[i]->path) != 0))
1058 if (locale_info[i]->path != (char *) NULL)
1059 (void) FormatLocaleFile(file,"\nPath: %s\n\n",locale_info[i]->path);
1060 (void) FormatLocaleFile(file,"Tag/Message\n");
1061 (void) FormatLocaleFile(file,
1062 "-------------------------------------------------"
1063 "------------------------------\n");
1065 path=locale_info[i]->path;
1066 (void) FormatLocaleFile(file,"%s\n",locale_info[i]->tag);
1067 if (locale_info[i]->message != (char *) NULL)
1068 (void) FormatLocaleFile(file," %s",locale_info[i]->message);
1069 (void) FormatLocaleFile(file,"\n");
1071 (void) fflush(file);
1072 locale_info=(const LocaleInfo **)
1073 RelinquishMagickMemory((void *) locale_info);
1078 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1082 + L o a d L o c a l e L i s t %
1086 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1088 % LoadLocaleCache() loads the locale configurations which provides a mapping
1089 % between locale attributes and a locale name.
1091 % The format of the LoadLocaleCache method is:
1093 % MagickBooleanType LoadLocaleCache(SplayTreeInfo *locale_cache,
1094 % const char *xml,const char *filename,const size_t depth,
1095 % ExceptionInfo *exception)
1097 % A description of each parameter follows:
1099 % o xml: The locale list in XML format.
1101 % o filename: The locale list filename.
1103 % o depth: depth of <include /> statements.
1105 % o exception: return any errors or warnings in this structure.
1109 static void ChopLocaleComponents(char *path,const size_t components)
1119 p=path+strlen(path)-1;
1122 for (count=0; (count < (ssize_t) components) && (p > path); p--)
1128 if (count < (ssize_t) components)
1132 static void LocaleFatalErrorHandler(
1133 const ExceptionType magick_unused(severity),
1134 const char *reason,const char *description)
1136 if (reason == (char *) NULL)
1138 (void) FormatLocaleFile(stderr,"%s: %s",GetClientName(),reason);
1139 if (description != (char *) NULL)
1140 (void) FormatLocaleFile(stderr," (%s)",description);
1141 (void) FormatLocaleFile(stderr,".\n");
1142 (void) fflush(stderr);
1146 static MagickBooleanType LoadLocaleCache(SplayTreeInfo *locale_cache,
1147 const char *xml,const char *filename,const char *locale,const size_t depth,
1148 ExceptionInfo *exception)
1151 keyword[MaxTextExtent],
1152 message[MaxTextExtent],
1172 Read the locale configure file.
1174 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1175 "Loading locale configure file \"%s\" ...",filename);
1176 if (xml == (const char *) NULL)
1177 return(MagickFalse);
1179 locale_info=(LocaleInfo *) NULL;
1183 fatal_handler=SetFatalErrorHandler(LocaleFatalErrorHandler);
1184 token=AcquireString(xml);
1185 for (q=(char *) xml; *q != '\0'; )
1190 GetMagickToken(q,&q,token);
1193 (void) CopyMagickString(keyword,token,MaxTextExtent);
1194 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
1199 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
1201 GetMagickToken(q,&q,token);
1202 while (isspace((int) ((unsigned char) *q)) != 0)
1207 if (LocaleNCompare(keyword,"<!--",4) == 0)
1212 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
1214 GetMagickToken(q,&q,token);
1215 while (isspace((int) ((unsigned char) *q)) != 0)
1220 if (LocaleCompare(keyword,"<include") == 0)
1225 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
1227 (void) CopyMagickString(keyword,token,MaxTextExtent);
1228 GetMagickToken(q,&q,token);
1231 GetMagickToken(q,&q,token);
1232 if (LocaleCompare(keyword,"locale") == 0)
1234 if (LocaleCompare(locale,token) != 0)
1238 if (LocaleCompare(keyword,"file") == 0)
1241 (void) ThrowMagickException(exception,GetMagickModule(),
1242 ConfigureError,"IncludeElementNestedTooDeeply","`%s'",token);
1246 path[MaxTextExtent],
1250 GetPathComponent(filename,HeadPath,path);
1252 (void) ConcatenateMagickString(path,DirectorySeparator,
1254 if (*token == *DirectorySeparator)
1255 (void) CopyMagickString(path,token,MaxTextExtent);
1257 (void) ConcatenateMagickString(path,token,MaxTextExtent);
1258 xml=FileToXML(path,~0UL);
1259 if (xml != (char *) NULL)
1261 status&=LoadLocaleCache(locale_cache,xml,path,locale,
1263 xml=(char *) RelinquishMagickMemory(xml);
1270 if (LocaleCompare(keyword,"<locale") == 0)
1275 while ((*token != '>') && (*q != '\0'))
1277 (void) CopyMagickString(keyword,token,MaxTextExtent);
1278 GetMagickToken(q,&q,token);
1281 GetMagickToken(q,&q,token);
1285 if (LocaleCompare(keyword,"</locale>") == 0)
1287 ChopLocaleComponents(tag,1);
1288 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1291 if (LocaleCompare(keyword,"<localemap>") == 0)
1293 if (LocaleCompare(keyword,"</localemap>") == 0)
1295 if (LocaleCompare(keyword,"<message") == 0)
1300 while ((*token != '>') && (*q != '\0'))
1302 (void) CopyMagickString(keyword,token,MaxTextExtent);
1303 GetMagickToken(q,&q,token);
1306 GetMagickToken(q,&q,token);
1307 if (LocaleCompare(keyword,"name") == 0)
1309 (void) ConcatenateMagickString(tag,token,MaxTextExtent);
1310 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1313 for (p=(char *) q; (*q != '<') && (*q != '\0'); q++) ;
1314 while (isspace((int) ((unsigned char) *p)) != 0)
1317 while ((isspace((int) ((unsigned char) *q)) != 0) && (q > p))
1319 (void) CopyMagickString(message,p,MagickMin((size_t) (q-p+2),
1321 locale_info=(LocaleInfo *) AcquireMagickMemory(sizeof(*locale_info));
1322 if (locale_info == (LocaleInfo *) NULL)
1323 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1324 (void) ResetMagickMemory(locale_info,0,sizeof(*locale_info));
1325 locale_info->path=ConstantString(filename);
1326 locale_info->tag=ConstantString(tag);
1327 locale_info->message=ConstantString(message);
1328 locale_info->signature=MagickSignature;
1329 status=AddValueToSplayTree(locale_cache,locale_info->tag,locale_info);
1330 if (status == MagickFalse)
1331 (void) ThrowMagickException(exception,GetMagickModule(),
1332 ResourceLimitError,"MemoryAllocationFailed","`%s'",
1334 (void) ConcatenateMagickString(tag,message,MaxTextExtent);
1335 (void) ConcatenateMagickString(tag,"\n",MaxTextExtent);
1339 if (LocaleCompare(keyword,"</message>") == 0)
1341 ChopLocaleComponents(tag,2);
1342 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1345 if (*keyword == '<')
1350 if (*(keyword+1) == '?')
1352 if (*(keyword+1) == '/')
1354 ChopLocaleComponents(tag,1);
1356 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1359 token[strlen(token)-1]='\0';
1360 (void) CopyMagickString(token,token+1,MaxTextExtent);
1361 (void) ConcatenateMagickString(tag,token,MaxTextExtent);
1362 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1365 GetMagickToken(q,(const char **) NULL,token);
1369 token=(char *) RelinquishMagickMemory(token);
1370 (void) SetFatalErrorHandler(fatal_handler);
1371 return(status != 0 ? MagickTrue : MagickFalse);
1375 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1379 + L o c a l e C o m p o n e n t G e n e s i s %
1383 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1385 % LocaleComponentGenesis() instantiates the locale component.
1387 % The format of the LocaleComponentGenesis method is:
1389 % MagickBooleanType LocaleComponentGenesis(void)
1392 MagickPrivate MagickBooleanType LocaleComponentGenesis(void)
1394 if (locale_semaphore == (SemaphoreInfo *) NULL)
1395 locale_semaphore=AcquireSemaphoreInfo();
1400 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1404 + L o c a l e C o m p o n e n t T e r m i n u s %
1408 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1410 % LocaleComponentTerminus() destroys the locale component.
1412 % The format of the LocaleComponentTerminus method is:
1414 % LocaleComponentTerminus(void)
1417 MagickPrivate void LocaleComponentTerminus(void)
1419 if (locale_semaphore == (SemaphoreInfo *) NULL)
1420 ActivateSemaphoreInfo(&locale_semaphore);
1421 LockSemaphoreInfo(locale_semaphore);
1422 if (locale_cache != (SplayTreeInfo *) NULL)
1423 locale_cache=DestroySplayTree(locale_cache);
1424 #if defined(MAGICKCORE_HAVE_LOCALE_H)
1427 UnlockSemaphoreInfo(locale_semaphore);
1428 RelinquishSemaphoreInfo(&locale_semaphore);