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-2018 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 % https://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/image-private.h"
49 #include "MagickCore/linked-list.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/memory-private.h"
55 #include "MagickCore/nt-base-private.h"
56 #include "MagickCore/semaphore.h"
57 #include "MagickCore/splay-tree.h"
58 #include "MagickCore/string_.h"
59 #include "MagickCore/string-private.h"
60 #include "MagickCore/token.h"
61 #include "MagickCore/utility.h"
62 #include "MagickCore/utility-private.h"
63 #include "MagickCore/xml-tree.h"
64 #include "MagickCore/xml-tree-private.h"
69 #if defined(MAGICKCORE_HAVE_NEWLOCALE) || defined(MAGICKCORE_WINDOWS_SUPPORT)
70 # define MAGICKCORE_LOCALE_SUPPORT
72 #define LocaleFilename "locale.xml"
73 #define MaxRecursionDepth 200
80 "<?xml version=\"1.0\"?>"
82 " <locale name=\"C\">"
84 " <Message name=\"\">"
91 *locale_semaphore = (SemaphoreInfo *) NULL;
94 *locale_cache = (SplayTreeInfo *) NULL;
96 #if defined(MAGICKCORE_LOCALE_SUPPORT)
97 static volatile locale_t
98 c_locale = (locale_t) NULL;
102 Forward declarations.
104 static MagickBooleanType
105 IsLocaleTreeInstantiated(ExceptionInfo *),
106 LoadLocaleCache(SplayTreeInfo *,const char *,const char *,const char *,
107 const size_t,ExceptionInfo *);
109 #if defined(MAGICKCORE_LOCALE_SUPPORT)
111 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
115 + A c q u i r e C L o c a l e %
119 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
121 % AcquireCLocale() allocates the C locale object, or (locale_t) 0 with
122 % errno set if it cannot be acquired.
124 % The format of the AcquireCLocale method is:
126 % locale_t AcquireCLocale(void)
129 static locale_t AcquireCLocale(void)
131 #if defined(MAGICKCORE_HAVE_NEWLOCALE)
132 if (c_locale == (locale_t) NULL)
133 c_locale=newlocale(LC_ALL_MASK,"C",(locale_t) 0);
134 #elif defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__MINGW32__)
135 if (c_locale == (locale_t) NULL)
136 c_locale=_create_locale(LC_ALL,"C");
143 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
147 % A c q u i r e L o c a l e S p l a y T r e e %
151 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
153 % AcquireLocaleSplayTree() caches one or more locale configurations which
154 % provides a mapping between locale attributes and a locale tag.
156 % The format of the AcquireLocaleSplayTree method is:
158 % SplayTreeInfo *AcquireLocaleSplayTree(const char *filename,
159 % ExceptionInfo *exception)
161 % A description of each parameter follows:
163 % o filename: the font file tag.
165 % o locale: the actual locale.
167 % o exception: return any errors or warnings in this structure.
171 static void *DestroyLocaleNode(void *locale_info)
176 p=(LocaleInfo *) locale_info;
177 if (p->path != (char *) NULL)
178 p->path=DestroyString(p->path);
179 if (p->tag != (char *) NULL)
180 p->tag=DestroyString(p->tag);
181 if (p->message != (char *) NULL)
182 p->message=DestroyString(p->message);
183 return(RelinquishMagickMemory(p));
186 static SplayTreeInfo *AcquireLocaleSplayTree(const char *filename,
187 const char *locale,ExceptionInfo *exception)
195 cache=NewSplayTree(CompareSplayTreeString,(void *(*)(void *)) NULL,
198 #if !defined(MAGICKCORE_ZERO_CONFIGURATION_SUPPORT)
206 options=GetLocaleOptions(filename,exception);
207 option=(const StringInfo *) GetNextValueInLinkedList(options);
208 while (option != (const StringInfo *) NULL)
210 status&=LoadLocaleCache(cache,(const char *)
211 GetStringInfoDatum(option),GetStringInfoPath(option),locale,0,
213 option=(const StringInfo *) GetNextValueInLinkedList(options);
215 options=DestroyLocaleOptions(options);
216 if (GetNumberOfNodesInSplayTree(cache) == 0)
218 options=GetLocaleOptions("english.xml",exception);
219 option=(const StringInfo *) GetNextValueInLinkedList(options);
220 while (option != (const StringInfo *) NULL)
222 status&=LoadLocaleCache(cache,(const char *)
223 GetStringInfoDatum(option),GetStringInfoPath(option),locale,0,
225 option=(const StringInfo *) GetNextValueInLinkedList(options);
227 options=DestroyLocaleOptions(options);
231 if (GetNumberOfNodesInSplayTree(cache) == 0)
232 status&=LoadLocaleCache(cache,LocaleMap,"built-in",locale,0,
237 #if defined(MAGICKCORE_LOCALE_SUPPORT)
239 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
243 + D e s t r o y C L o c a l e %
247 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
249 % DestroyCLocale() releases the resources allocated for a locale object
250 % returned by a call to the AcquireCLocale() method.
252 % The format of the DestroyCLocale method is:
254 % void DestroyCLocale(void)
257 static void DestroyCLocale(void)
259 #if defined(MAGICKCORE_HAVE_NEWLOCALE)
260 if (c_locale != (locale_t) NULL)
261 freelocale(c_locale);
262 #elif defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__MINGW32__)
263 if (c_locale != (locale_t) NULL)
264 _free_locale(c_locale);
266 c_locale=(locale_t) NULL;
271 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
275 % D e s t r o y L o c a l e O p t i o n s %
279 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
281 % DestroyLocaleOptions() releases memory associated with an locale
284 % The format of the DestroyProfiles method is:
286 % LinkedListInfo *DestroyLocaleOptions(Image *image)
288 % A description of each parameter follows:
290 % o image: the image.
294 static void *DestroyOptions(void *message)
296 return(DestroyStringInfo((StringInfo *) message));
299 MagickExport LinkedListInfo *DestroyLocaleOptions(LinkedListInfo *messages)
301 assert(messages != (LinkedListInfo *) NULL);
302 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
303 return(DestroyLinkedList(messages,DestroyOptions));
307 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
311 + F o r m a t L o c a l e F i l e %
315 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
317 % FormatLocaleFile() prints formatted output of a variable argument list to a
318 % file in the "C" locale.
320 % The format of the FormatLocaleFile method is:
322 % ssize_t FormatLocaleFile(FILE *file,const char *format,...)
324 % A description of each parameter follows.
328 % o format: A file describing the format to use to write the remaining
333 MagickPrivate ssize_t FormatLocaleFileList(FILE *file,
334 const char *magick_restrict format,va_list operands)
339 #if defined(MAGICKCORE_LOCALE_SUPPORT) && defined(MAGICKCORE_HAVE_VFPRINTF_L)
344 locale=AcquireCLocale();
345 if (locale == (locale_t) NULL)
346 n=(ssize_t) vfprintf(file,format,operands);
348 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
349 n=(ssize_t) vfprintf_l(file,format,locale,operands);
351 n=(ssize_t) vfprintf_l(file,locale,format,operands);
355 #if defined(MAGICKCORE_LOCALE_SUPPORT) && defined(MAGICKCORE_HAVE_USELOCALE)
361 locale=AcquireCLocale();
362 if (locale == (locale_t) NULL)
363 n=(ssize_t) vfprintf(file,format,operands);
366 previous_locale=uselocale(locale);
367 n=(ssize_t) vfprintf(file,format,operands);
368 uselocale(previous_locale);
372 n=(ssize_t) vfprintf(file,format,operands);
378 MagickExport ssize_t FormatLocaleFile(FILE *file,
379 const char *magick_restrict format,...)
387 va_start(operands,format);
388 n=FormatLocaleFileList(file,format,operands);
394 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
398 + F o r m a t L o c a l e S t r i n g %
402 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
404 % FormatLocaleString() prints formatted output of a variable argument list to
405 % a string buffer in the "C" locale.
407 % The format of the FormatLocaleString method is:
409 % ssize_t FormatLocaleString(char *string,const size_t length,
410 % const char *format,...)
412 % A description of each parameter follows.
414 % o string: FormatLocaleString() returns the formatted string in this
417 % o length: the maximum length of the string.
419 % o format: A string describing the format to use to write the remaining
424 MagickPrivate ssize_t FormatLocaleStringList(char *magick_restrict string,
425 const size_t length,const char *magick_restrict format,va_list operands)
430 #if defined(MAGICKCORE_LOCALE_SUPPORT) && defined(MAGICKCORE_HAVE_VSNPRINTF_L)
435 locale=AcquireCLocale();
436 if (locale == (locale_t) NULL)
437 n=(ssize_t) vsnprintf(string,length,format,operands);
439 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
440 n=(ssize_t) vsnprintf_l(string,length,format,locale,operands);
442 n=(ssize_t) vsnprintf_l(string,length,locale,format,operands);
445 #elif defined(MAGICKCORE_HAVE_VSNPRINTF)
446 #if defined(MAGICKCORE_LOCALE_SUPPORT) && defined(MAGICKCORE_HAVE_USELOCALE)
452 locale=AcquireCLocale();
453 if (locale == (locale_t) NULL)
454 n=(ssize_t) vsnprintf(string,length,format,operands);
457 previous_locale=uselocale(locale);
458 n=(ssize_t) vsnprintf(string,length,format,operands);
459 uselocale(previous_locale);
463 n=(ssize_t) vsnprintf(string,length,format,operands);
466 n=(ssize_t) vsprintf(string,format,operands);
469 string[length-1]='\0';
473 MagickExport ssize_t FormatLocaleString(char *magick_restrict string,
474 const size_t length,const char *magick_restrict format,...)
482 va_start(operands,format);
483 n=FormatLocaleStringList(string,length,format,operands);
489 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
493 + G e t L o c a l e I n f o _ %
497 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
499 % GetLocaleInfo_() searches the locale list for the specified tag and if
500 % found returns attributes for that element.
502 % The format of the GetLocaleInfo method is:
504 % const LocaleInfo *GetLocaleInfo_(const char *tag,
505 % ExceptionInfo *exception)
507 % A description of each parameter follows:
509 % o tag: the locale tag.
511 % o exception: return any errors or warnings in this structure.
514 MagickExport const LocaleInfo *GetLocaleInfo_(const char *tag,
515 ExceptionInfo *exception)
520 assert(exception != (ExceptionInfo *) NULL);
521 if (IsLocaleTreeInstantiated(exception) == MagickFalse)
522 return((const LocaleInfo *) NULL);
523 LockSemaphoreInfo(locale_semaphore);
524 if ((tag == (const char *) NULL) || (LocaleCompare(tag,"*") == 0))
526 ResetSplayTreeIterator(locale_cache);
527 locale_info=(const LocaleInfo *) GetNextValueInSplayTree(locale_cache);
528 UnlockSemaphoreInfo(locale_semaphore);
531 locale_info=(const LocaleInfo *) GetValueFromSplayTree(locale_cache,tag);
532 UnlockSemaphoreInfo(locale_semaphore);
537 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
541 % G e t L o c a l e I n f o L i s t %
545 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
547 % GetLocaleInfoList() returns any locale messages that match the
550 % The format of the GetLocaleInfoList function is:
552 % const LocaleInfo **GetLocaleInfoList(const char *pattern,
553 % size_t *number_messages,ExceptionInfo *exception)
555 % A description of each parameter follows:
557 % o pattern: Specifies a pointer to a text string containing a pattern.
559 % o number_messages: This integer returns the number of locale messages in
562 % o exception: return any errors or warnings in this structure.
566 #if defined(__cplusplus) || defined(c_plusplus)
570 static int LocaleInfoCompare(const void *x,const void *y)
576 p=(const LocaleInfo **) x,
577 q=(const LocaleInfo **) y;
578 if (LocaleCompare((*p)->path,(*q)->path) == 0)
579 return(LocaleCompare((*p)->tag,(*q)->tag));
580 return(LocaleCompare((*p)->path,(*q)->path));
583 #if defined(__cplusplus) || defined(c_plusplus)
587 MagickExport const LocaleInfo **GetLocaleInfoList(const char *pattern,
588 size_t *number_messages,ExceptionInfo *exception)
593 register const LocaleInfo
600 Allocate locale list.
602 assert(pattern != (char *) NULL);
603 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
604 assert(number_messages != (size_t *) NULL);
606 p=GetLocaleInfo_("*",exception);
607 if (p == (const LocaleInfo *) NULL)
608 return((const LocaleInfo **) NULL);
609 messages=(const LocaleInfo **) AcquireQuantumMemory((size_t)
610 GetNumberOfNodesInSplayTree(locale_cache)+1UL,sizeof(*messages));
611 if (messages == (const LocaleInfo **) NULL)
612 return((const LocaleInfo **) NULL);
614 Generate locale list.
616 LockSemaphoreInfo(locale_semaphore);
617 ResetSplayTreeIterator(locale_cache);
618 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_cache);
619 for (i=0; p != (const LocaleInfo *) NULL; )
621 if ((p->stealth == MagickFalse) &&
622 (GlobExpression(p->tag,pattern,MagickTrue) != MagickFalse))
624 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_cache);
626 UnlockSemaphoreInfo(locale_semaphore);
627 qsort((void *) messages,(size_t) i,sizeof(*messages),LocaleInfoCompare);
628 messages[i]=(LocaleInfo *) NULL;
629 *number_messages=(size_t) i;
634 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
638 % G e t L o c a l e L i s t %
642 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
644 % GetLocaleList() returns any locale messages that match the specified
647 % The format of the GetLocaleList function is:
649 % char **GetLocaleList(const char *pattern,size_t *number_messages,
650 % Exceptioninfo *exception)
652 % A description of each parameter follows:
654 % o pattern: Specifies a pointer to a text string containing a pattern.
656 % o number_messages: This integer returns the number of messages in the
659 % o exception: return any errors or warnings in this structure.
663 #if defined(__cplusplus) || defined(c_plusplus)
667 static int LocaleTagCompare(const void *x,const void *y)
675 return(LocaleCompare(*p,*q));
678 #if defined(__cplusplus) || defined(c_plusplus)
682 MagickExport char **GetLocaleList(const char *pattern,size_t *number_messages,
683 ExceptionInfo *exception)
688 register const LocaleInfo
695 Allocate locale list.
697 assert(pattern != (char *) NULL);
698 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
699 assert(number_messages != (size_t *) NULL);
701 p=GetLocaleInfo_("*",exception);
702 if (p == (const LocaleInfo *) NULL)
703 return((char **) NULL);
704 messages=(char **) AcquireQuantumMemory((size_t)
705 GetNumberOfNodesInSplayTree(locale_cache)+1UL,sizeof(*messages));
706 if (messages == (char **) NULL)
707 return((char **) NULL);
708 LockSemaphoreInfo(locale_semaphore);
709 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_cache);
710 for (i=0; p != (const LocaleInfo *) NULL; )
712 if ((p->stealth == MagickFalse) &&
713 (GlobExpression(p->tag,pattern,MagickTrue) != MagickFalse))
714 messages[i++]=ConstantString(p->tag);
715 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_cache);
717 UnlockSemaphoreInfo(locale_semaphore);
718 qsort((void *) messages,(size_t) i,sizeof(*messages),LocaleTagCompare);
719 messages[i]=(char *) NULL;
720 *number_messages=(size_t) i;
725 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
729 % G e t L o c a l e M e s s a g e %
733 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
735 % GetLocaleMessage() returns a message in the current locale that matches the
738 % The format of the GetLocaleMessage method is:
740 % const char *GetLocaleMessage(const char *tag)
742 % A description of each parameter follows:
744 % o tag: Return a message that matches this tag in the current locale.
747 MagickExport const char *GetLocaleMessage(const char *tag)
750 name[MagickLocaleExtent];
758 if ((tag == (const char *) NULL) || (*tag == '\0'))
760 exception=AcquireExceptionInfo();
761 (void) FormatLocaleString(name,MagickLocaleExtent,"%s/",tag);
762 locale_info=GetLocaleInfo_(name,exception);
763 exception=DestroyExceptionInfo(exception);
764 if (locale_info != (const LocaleInfo *) NULL)
765 return(locale_info->message);
770 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
774 % G e t L o c a l e O p t i o n s %
778 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
780 % GetLocaleOptions() returns any Magick configuration messages associated
781 % with the specified filename.
783 % The format of the GetLocaleOptions method is:
785 % LinkedListInfo *GetLocaleOptions(const char *filename,
786 % ExceptionInfo *exception)
788 % A description of each parameter follows:
790 % o filename: the locale file tag.
792 % o exception: return any errors or warnings in this structure.
795 MagickExport LinkedListInfo *GetLocaleOptions(const char *filename,
796 ExceptionInfo *exception)
799 path[MagickPathExtent];
811 assert(filename != (const char *) NULL);
812 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
813 assert(exception != (ExceptionInfo *) NULL);
814 (void) CopyMagickString(path,filename,MagickPathExtent);
816 Load XML from configuration files to linked-list.
818 messages=NewLinkedList(0);
819 paths=GetConfigurePaths(filename,exception);
820 if (paths != (LinkedListInfo *) NULL)
822 ResetLinkedListIterator(paths);
823 element=(const char *) GetNextValueInLinkedList(paths);
824 while (element != (const char *) NULL)
826 (void) FormatLocaleString(path,MagickPathExtent,"%s%s",element,
828 (void) LogMagickEvent(LocaleEvent,GetMagickModule(),
829 "Searching for locale file: \"%s\"",path);
830 xml=ConfigureFileToStringInfo(path);
831 if (xml != (StringInfo *) NULL)
832 (void) AppendValueToLinkedList(messages,xml);
833 element=(const char *) GetNextValueInLinkedList(paths);
835 paths=DestroyLinkedList(paths,RelinquishMagickMemory);
837 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
842 blob=(char *) NTResourceToBlob(filename);
843 if (blob != (char *) NULL)
845 xml=AcquireStringInfo(0);
846 SetStringInfoLength(xml,strlen(blob)+1);
847 SetStringInfoDatum(xml,(const unsigned char *) blob);
848 blob=(char *) RelinquishMagickMemory(blob);
849 SetStringInfoPath(xml,filename);
850 (void) AppendValueToLinkedList(messages,xml);
854 ResetLinkedListIterator(messages);
859 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
863 % G e t L o c a l e V a l u e %
867 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
869 % GetLocaleValue() returns the message associated with the locale info.
871 % The format of the GetLocaleValue method is:
873 % const char *GetLocaleValue(const LocaleInfo *locale_info)
875 % A description of each parameter follows:
877 % o locale_info: The locale info.
880 MagickExport const char *GetLocaleValue(const LocaleInfo *locale_info)
882 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
883 assert(locale_info != (LocaleInfo *) NULL);
884 assert(locale_info->signature == MagickCoreSignature);
885 return(locale_info->message);
889 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
893 + I s L o c a l e T r e e I n s t a n t i a t e d %
897 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
899 % IsLocaleTreeInstantiated() determines if the locale tree is instantiated.
900 % If not, it instantiates the tree and returns it.
902 % The format of the IsLocaleInstantiated method is:
904 % MagickBooleanType IsLocaleTreeInstantiated(ExceptionInfo *exception)
906 % A description of each parameter follows.
908 % o exception: return any errors or warnings in this structure.
911 static MagickBooleanType IsLocaleTreeInstantiated(ExceptionInfo *exception)
913 if (locale_cache == (SplayTreeInfo *) NULL)
915 if (locale_semaphore == (SemaphoreInfo *) NULL)
916 ActivateSemaphoreInfo(&locale_semaphore);
917 LockSemaphoreInfo(locale_semaphore);
918 if (locale_cache == (SplayTreeInfo *) NULL)
926 locale=(char *) NULL;
927 p=setlocale(LC_CTYPE,(const char *) NULL);
928 if (p != (const char *) NULL)
929 locale=ConstantString(p);
930 if (locale == (char *) NULL)
931 locale=GetEnvironmentValue("LC_ALL");
932 if (locale == (char *) NULL)
933 locale=GetEnvironmentValue("LC_MESSAGES");
934 if (locale == (char *) NULL)
935 locale=GetEnvironmentValue("LC_CTYPE");
936 if (locale == (char *) NULL)
937 locale=GetEnvironmentValue("LANG");
938 if (locale == (char *) NULL)
939 locale=ConstantString("C");
940 locale_cache=AcquireLocaleSplayTree(LocaleFilename,locale,exception);
941 locale=DestroyString(locale);
943 UnlockSemaphoreInfo(locale_semaphore);
945 return(locale_cache != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
949 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
953 + I n t e r p r e t L o c a l e V a l u e %
957 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
959 % InterpretLocaleValue() interprets the string as a floating point number in
960 % the "C" locale and returns its value as a double. If sentinal is not a null
961 % pointer, the method also sets the value pointed by sentinal to point to the
962 % first character after the number.
964 % The format of the InterpretLocaleValue method is:
966 % double InterpretLocaleValue(const char *value,char **sentinal)
968 % A description of each parameter follows:
970 % o value: the string value.
972 % o sentinal: if sentinal is not NULL, a pointer to the character after the
973 % last character used in the conversion is stored in the location
974 % referenced by sentinal.
977 MagickExport double InterpretLocaleValue(const char *magick_restrict string,
978 char **magick_restrict sentinal)
986 if ((*string == '0') && ((string[1] | 0x20)=='x'))
987 value=(double) strtoul(string,&q,16);
990 #if defined(MAGICKCORE_LOCALE_SUPPORT) && defined(MAGICKCORE_HAVE_STRTOD_L)
994 locale=AcquireCLocale();
995 if (locale == (locale_t) NULL)
996 value=strtod(string,&q);
998 value=strtod_l(string,&q,locale);
1000 value=strtod(string,&q);
1003 if (sentinal != (char **) NULL)
1009 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1013 % L i s t L o c a l e I n f o %
1017 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1019 % ListLocaleInfo() lists the locale info to a file.
1021 % The format of the ListLocaleInfo method is:
1023 % MagickBooleanType ListLocaleInfo(FILE *file,ExceptionInfo *exception)
1025 % A description of each parameter follows.
1027 % o file: An pointer to a FILE.
1029 % o exception: return any errors or warnings in this structure.
1032 MagickExport MagickBooleanType ListLocaleInfo(FILE *file,
1033 ExceptionInfo *exception)
1047 if (file == (const FILE *) NULL)
1050 locale_info=GetLocaleInfoList("*",&number_messages,exception);
1051 if (locale_info == (const LocaleInfo **) NULL)
1052 return(MagickFalse);
1053 path=(const char *) NULL;
1054 for (i=0; i < (ssize_t) number_messages; i++)
1056 if (locale_info[i]->stealth != MagickFalse)
1058 if ((path == (const char *) NULL) ||
1059 (LocaleCompare(path,locale_info[i]->path) != 0))
1061 if (locale_info[i]->path != (char *) NULL)
1062 (void) FormatLocaleFile(file,"\nPath: %s\n\n",locale_info[i]->path);
1063 (void) FormatLocaleFile(file,"Tag/Message\n");
1064 (void) FormatLocaleFile(file,
1065 "-------------------------------------------------"
1066 "------------------------------\n");
1068 path=locale_info[i]->path;
1069 (void) FormatLocaleFile(file,"%s\n",locale_info[i]->tag);
1070 if (locale_info[i]->message != (char *) NULL)
1071 (void) FormatLocaleFile(file," %s",locale_info[i]->message);
1072 (void) FormatLocaleFile(file,"\n");
1074 (void) fflush(file);
1075 locale_info=(const LocaleInfo **)
1076 RelinquishMagickMemory((void *) locale_info);
1081 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1085 + L o a d L o c a l e C a c h e %
1089 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1091 % LoadLocaleCache() loads the locale configurations which provides a mapping
1092 % between locale attributes and a locale name.
1094 % The format of the LoadLocaleCache method is:
1096 % MagickBooleanType LoadLocaleCache(SplayTreeInfo *cache,const char *xml,
1097 % const char *filename,const size_t depth,ExceptionInfo *exception)
1099 % A description of each parameter follows:
1101 % o xml: The locale list in XML format.
1103 % o filename: The locale list filename.
1105 % o depth: depth of <include /> statements.
1107 % o exception: return any errors or warnings in this structure.
1111 static void ChopLocaleComponents(char *path,const size_t components)
1121 p=path+strlen(path)-1;
1124 for (count=0; (count < (ssize_t) components) && (p > path); p--)
1130 if (count < (ssize_t) components)
1134 static void LocaleFatalErrorHandler(
1135 const ExceptionType magick_unused(severity),
1136 const char *reason,const char *description)
1138 magick_unreferenced(severity);
1140 if (reason == (char *) NULL)
1142 (void) FormatLocaleFile(stderr,"%s: %s",GetClientName(),reason);
1143 if (description != (char *) NULL)
1144 (void) FormatLocaleFile(stderr," (%s)",description);
1145 (void) FormatLocaleFile(stderr,".\n");
1146 (void) fflush(stderr);
1150 static MagickBooleanType LoadLocaleCache(SplayTreeInfo *cache,const char *xml,
1151 const char *filename,const char *locale,const size_t depth,ExceptionInfo *exception)
1154 keyword[MagickLocaleExtent],
1155 message[MagickLocaleExtent],
1156 tag[MagickLocaleExtent],
1178 Read the locale configure file.
1180 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1181 "Loading locale configure file \"%s\" ...",filename);
1182 if (xml == (const char *) NULL)
1183 return(MagickFalse);
1185 locale_info=(LocaleInfo *) NULL;
1189 fatal_handler=SetFatalErrorHandler(LocaleFatalErrorHandler);
1190 token=AcquireString(xml);
1191 extent=strlen(token)+MagickPathExtent;
1192 for (q=(char *) xml; *q != '\0'; )
1197 GetNextToken(q,&q,extent,token);
1200 (void) CopyMagickString(keyword,token,MagickLocaleExtent);
1201 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
1206 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
1208 GetNextToken(q,&q,extent,token);
1209 while (isspace((int) ((unsigned char) *q)) != 0)
1214 if (LocaleNCompare(keyword,"<!--",4) == 0)
1219 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
1221 GetNextToken(q,&q,extent,token);
1222 while (isspace((int) ((unsigned char) *q)) != 0)
1227 if (LocaleCompare(keyword,"<include") == 0)
1232 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
1234 (void) CopyMagickString(keyword,token,MagickLocaleExtent);
1235 GetNextToken(q,&q,extent,token);
1238 GetNextToken(q,&q,extent,token);
1239 if (LocaleCompare(keyword,"locale") == 0)
1241 if (LocaleCompare(locale,token) != 0)
1245 if (LocaleCompare(keyword,"file") == 0)
1248 (void) ThrowMagickException(exception,GetMagickModule(),
1249 ConfigureError,"IncludeElementNestedTooDeeply","`%s'",token);
1253 path[MagickPathExtent],
1257 GetPathComponent(filename,HeadPath,path);
1259 (void) ConcatenateMagickString(path,DirectorySeparator,
1261 if (*token == *DirectorySeparator)
1262 (void) CopyMagickString(path,token,MagickPathExtent);
1264 (void) ConcatenateMagickString(path,token,MagickPathExtent);
1265 file_xml=FileToXML(path,~0UL);
1266 if (file_xml != (char *) NULL)
1268 status&=LoadLocaleCache(cache,file_xml,path,locale,
1270 file_xml=DestroyString(file_xml);
1277 if (LocaleCompare(keyword,"<locale") == 0)
1282 while ((*token != '>') && (*q != '\0'))
1284 (void) CopyMagickString(keyword,token,MagickLocaleExtent);
1285 GetNextToken(q,&q,extent,token);
1288 GetNextToken(q,&q,extent,token);
1292 if (LocaleCompare(keyword,"</locale>") == 0)
1294 ChopLocaleComponents(tag,1);
1295 (void) ConcatenateMagickString(tag,"/",MagickLocaleExtent);
1298 if (LocaleCompare(keyword,"<localemap>") == 0)
1300 if (LocaleCompare(keyword,"</localemap>") == 0)
1302 if (LocaleCompare(keyword,"<message") == 0)
1307 while ((*token != '>') && (*q != '\0'))
1309 (void) CopyMagickString(keyword,token,MagickLocaleExtent);
1310 GetNextToken(q,&q,extent,token);
1313 GetNextToken(q,&q,extent,token);
1314 if (LocaleCompare(keyword,"name") == 0)
1316 (void) ConcatenateMagickString(tag,token,MagickLocaleExtent);
1317 (void) ConcatenateMagickString(tag,"/",MagickLocaleExtent);
1320 for (p=(char *) q; (*q != '<') && (*q != '\0'); q++) ;
1321 while (isspace((int) ((unsigned char) *p)) != 0)
1324 while ((isspace((int) ((unsigned char) *q)) != 0) && (q > p))
1326 (void) CopyMagickString(message,p,MagickMin((size_t) (q-p+2),
1327 MagickLocaleExtent));
1328 locale_info=(LocaleInfo *) AcquireCriticalMemory(sizeof(*locale_info));
1329 (void) ResetMagickMemory(locale_info,0,sizeof(*locale_info));
1330 locale_info->path=ConstantString(filename);
1331 locale_info->tag=ConstantString(tag);
1332 locale_info->message=ConstantString(message);
1333 locale_info->signature=MagickCoreSignature;
1334 status=AddValueToSplayTree(cache,locale_info->tag,locale_info);
1335 if (status == MagickFalse)
1336 (void) ThrowMagickException(exception,GetMagickModule(),
1337 ResourceLimitError,"MemoryAllocationFailed","`%s'",
1339 (void) ConcatenateMagickString(tag,message,MagickLocaleExtent);
1340 (void) ConcatenateMagickString(tag,"\n",MagickLocaleExtent);
1344 if (LocaleCompare(keyword,"</message>") == 0)
1346 ChopLocaleComponents(tag,2);
1347 (void) ConcatenateMagickString(tag,"/",MagickLocaleExtent);
1350 if (*keyword == '<')
1355 if (*(keyword+1) == '?')
1357 if (*(keyword+1) == '/')
1359 ChopLocaleComponents(tag,1);
1361 (void) ConcatenateMagickString(tag,"/",MagickLocaleExtent);
1364 token[strlen(token)-1]='\0';
1365 (void) CopyMagickString(token,token+1,MagickLocaleExtent);
1366 (void) ConcatenateMagickString(tag,token,MagickLocaleExtent);
1367 (void) ConcatenateMagickString(tag,"/",MagickLocaleExtent);
1370 GetNextToken(q,(const char **) NULL,extent,token);
1374 token=(char *) RelinquishMagickMemory(token);
1375 (void) SetFatalErrorHandler(fatal_handler);
1376 return(status != 0 ? MagickTrue : MagickFalse);
1380 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1384 % L o c a l e C o m p a r e %
1388 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1390 % LocaleCompare() performs a case-insensitive comparison of two strings
1391 % byte-by-byte, according to the ordering of the current locale encoding.
1392 % LocaleCompare returns an integer greater than, equal to, or less than 0,
1393 % if the string pointed to by p is greater than, equal to, or less than the
1394 % string pointed to by q respectively. The sign of a non-zero return value
1395 % is determined by the sign of the difference between the values of the first
1396 % pair of bytes that differ in the strings being compared.
1398 % The format of the LocaleCompare method is:
1400 % int LocaleCompare(const char *p,const char *q)
1402 % A description of each parameter follows:
1404 % o p: A pointer to a character string.
1406 % o q: A pointer to a character string to compare to p.
1409 MagickExport int LocaleCompare(const char *p,const char *q)
1411 if ((p == (char *) NULL) && (q == (char *) NULL))
1413 if (p == (char *) NULL)
1415 if (q == (char *) NULL)
1417 #if defined(MAGICKCORE_HAVE_STRCASECMP)
1418 return(strcasecmp(p,q));
1427 c=(int) *((unsigned char *) p);
1428 d=(int) *((unsigned char *) q);
1429 if ((c == 0) || (AsciiMap[c] != AsciiMap[d]))
1434 return(AsciiMap[c]-(int) AsciiMap[d]);
1440 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1444 % L o c a l e L o w e r %
1448 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1450 % LocaleLower() transforms all of the characters in the supplied
1451 % null-terminated string, changing all uppercase letters to lowercase.
1453 % The format of the LocaleLower method is:
1455 % void LocaleLower(char *string)
1457 % A description of each parameter follows:
1459 % o string: A pointer to the string to convert to lower-case Locale.
1462 MagickExport void LocaleLower(char *string)
1467 assert(string != (char *) NULL);
1468 for (q=string; *q != '\0'; q++)
1469 *q=(char) tolower((int) *q);
1473 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1477 % L o c a l e N C o m p a r e %
1481 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1483 % LocaleNCompare() performs a case-insensitive comparison of two strings
1484 % byte-by-byte, according to the ordering of the current locale encoding.
1486 % LocaleNCompare returns an integer greater than, equal to, or less than 0,
1487 % if the string pointed to by p is greater than, equal to, or less than the
1488 % string pointed to by q respectively. The sign of a non-zero return value
1489 % is determined by the sign of the difference between the values of the first
1490 % pair of bytes that differ in the strings being compared.
1492 % The LocaleNCompare method makes the same comparison as LocaleCompare but
1493 % looks at a maximum of n bytes. Bytes following a null byte are not
1496 % The format of the LocaleNCompare method is:
1498 % int LocaleNCompare(const char *p,const char *q,const size_t n)
1500 % A description of each parameter follows:
1502 % o p: A pointer to a character string.
1504 % o q: A pointer to a character string to compare to p.
1506 % o length: the number of characters to compare in strings p and q.
1509 MagickExport int LocaleNCompare(const char *p,const char *q,const size_t length)
1511 if ((p == (char *) NULL) && (q == (char *) NULL))
1513 if (p == (char *) NULL)
1515 if (q == (char *) NULL)
1517 #if defined(MAGICKCORE_HAVE_STRNCASECMP)
1518 return(strncasecmp(p,q,length));
1528 for (i=length; i != 0; i--)
1530 c=(int) *((unsigned char *) p);
1531 d=(int) *((unsigned char *) q);
1532 if (AsciiMap[c] != AsciiMap[d])
1533 return(AsciiMap[c]-(int) AsciiMap[d]);
1545 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1549 % L o c a l e U p p e r %
1553 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1555 % LocaleUpper() transforms all of the characters in the supplied
1556 % null-terminated string, changing all lowercase letters to uppercase.
1558 % The format of the LocaleUpper method is:
1560 % void LocaleUpper(char *string)
1562 % A description of each parameter follows:
1564 % o string: A pointer to the string to convert to upper-case Locale.
1567 MagickExport void LocaleUpper(char *string)
1572 assert(string != (char *) NULL);
1573 for (q=string; *q != '\0'; q++)
1574 *q=(char) toupper((int) *q);
1578 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1582 + L o c a l e C o m p o n e n t G e n e s i s %
1586 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1588 % LocaleComponentGenesis() instantiates the locale component.
1590 % The format of the LocaleComponentGenesis method is:
1592 % MagickBooleanType LocaleComponentGenesis(void)
1595 MagickPrivate MagickBooleanType LocaleComponentGenesis(void)
1597 if (locale_semaphore == (SemaphoreInfo *) NULL)
1598 locale_semaphore=AcquireSemaphoreInfo();
1603 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1607 + L o c a l e C o m p o n e n t T e r m i n u s %
1611 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1613 % LocaleComponentTerminus() destroys the locale component.
1615 % The format of the LocaleComponentTerminus method is:
1617 % LocaleComponentTerminus(void)
1620 MagickPrivate void LocaleComponentTerminus(void)
1622 if (locale_semaphore == (SemaphoreInfo *) NULL)
1623 ActivateSemaphoreInfo(&locale_semaphore);
1624 LockSemaphoreInfo(locale_semaphore);
1625 if (locale_cache != (SplayTreeInfo *) NULL)
1626 locale_cache=DestroySplayTree(locale_cache);
1627 #if defined(MAGICKCORE_LOCALE_SUPPORT)
1630 UnlockSemaphoreInfo(locale_semaphore);
1631 RelinquishSemaphoreInfo(&locale_semaphore);