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-2017 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/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 #if defined(MAGICKCORE_HAVE_NEWLOCALE) || defined(MAGICKCORE_WINDOWS_SUPPORT)
69 # define MAGICKCORE_LOCALE_SUPPORT
71 #define LocaleFilename "locale.xml"
72 #define MaxRecursionDepth 200
79 "<?xml version=\"1.0\"?>"
81 " <locale name=\"C\">"
83 " <Message name=\"\">"
90 *locale_semaphore = (SemaphoreInfo *) NULL;
93 *locale_cache = (SplayTreeInfo *) NULL;
95 #if defined(MAGICKCORE_LOCALE_SUPPORT)
96 static volatile locale_t
97 c_locale = (locale_t) NULL;
101 Forward declarations.
103 static MagickBooleanType
104 IsLocaleTreeInstantiated(ExceptionInfo *),
105 LoadLocaleCache(SplayTreeInfo *,const char *,const char *,const char *,
106 const size_t,ExceptionInfo *);
108 #if defined(MAGICKCORE_LOCALE_SUPPORT)
110 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
114 + A c q u i r e C L o c a l e %
118 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
120 % AcquireCLocale() allocates the C locale object, or (locale_t) 0 with
121 % errno set if it cannot be acquired.
123 % The format of the AcquireCLocale method is:
125 % locale_t AcquireCLocale(void)
128 static locale_t AcquireCLocale(void)
130 #if defined(MAGICKCORE_HAVE_NEWLOCALE)
131 if (c_locale == (locale_t) NULL)
132 c_locale=newlocale(LC_ALL_MASK,"C",(locale_t) 0);
133 #elif defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__MINGW32__)
134 if (c_locale == (locale_t) NULL)
135 c_locale=_create_locale(LC_ALL,"C");
142 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
146 % A c q u i r e L o c a l e S p l a y T r e e %
150 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
152 % AcquireLocaleSplayTree() caches one or more locale configurations which
153 % provides a mapping between locale attributes and a locale tag.
155 % The format of the AcquireLocaleSplayTree method is:
157 % SplayTreeInfo *AcquireLocaleSplayTree(const char *filename,
158 % ExceptionInfo *exception)
160 % A description of each parameter follows:
162 % o filename: the font file tag.
164 % o locale: the actual locale.
166 % o exception: return any errors or warnings in this structure.
170 static void *DestroyLocaleNode(void *locale_info)
175 p=(LocaleInfo *) locale_info;
176 if (p->path != (char *) NULL)
177 p->path=DestroyString(p->path);
178 if (p->tag != (char *) NULL)
179 p->tag=DestroyString(p->tag);
180 if (p->message != (char *) NULL)
181 p->message=DestroyString(p->message);
182 return(RelinquishMagickMemory(p));
185 static SplayTreeInfo *AcquireLocaleSplayTree(const char *filename,
186 const char *locale,ExceptionInfo *exception)
194 cache=NewSplayTree(CompareSplayTreeString,(void *(*)(void *)) NULL,
196 if (cache == (SplayTreeInfo *) NULL)
197 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
199 #if !defined(MAGICKCORE_ZERO_CONFIGURATION_SUPPORT)
207 options=GetLocaleOptions(filename,exception);
208 option=(const StringInfo *) GetNextValueInLinkedList(options);
209 while (option != (const StringInfo *) NULL)
211 status&=LoadLocaleCache(cache,(const char *)
212 GetStringInfoDatum(option),GetStringInfoPath(option),locale,0,
214 option=(const StringInfo *) GetNextValueInLinkedList(options);
216 options=DestroyLocaleOptions(options);
217 if (GetNumberOfNodesInSplayTree(cache) == 0)
219 options=GetLocaleOptions("english.xml",exception);
220 option=(const StringInfo *) GetNextValueInLinkedList(options);
221 while (option != (const StringInfo *) NULL)
223 status&=LoadLocaleCache(cache,(const char *)
224 GetStringInfoDatum(option),GetStringInfoPath(option),locale,0,
226 option=(const StringInfo *) GetNextValueInLinkedList(options);
228 options=DestroyLocaleOptions(options);
232 if (GetNumberOfNodesInSplayTree(cache) == 0)
233 status&=LoadLocaleCache(cache,LocaleMap,"built-in",locale,0,
238 #if defined(MAGICKCORE_LOCALE_SUPPORT)
240 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
244 + D e s t r o y C L o c a l e %
248 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
250 % DestroyCLocale() releases the resources allocated for a locale object
251 % returned by a call to the AcquireCLocale() method.
253 % The format of the DestroyCLocale method is:
255 % void DestroyCLocale(void)
258 static void DestroyCLocale(void)
260 #if defined(MAGICKCORE_HAVE_NEWLOCALE)
261 if (c_locale != (locale_t) NULL)
262 freelocale(c_locale);
263 #elif defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__MINGW32__)
264 if (c_locale != (locale_t) NULL)
265 _free_locale(c_locale);
267 c_locale=(locale_t) NULL;
272 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
276 % D e s t r o y L o c a l e O p t i o n s %
280 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
282 % DestroyLocaleOptions() releases memory associated with an locale
285 % The format of the DestroyProfiles method is:
287 % LinkedListInfo *DestroyLocaleOptions(Image *image)
289 % A description of each parameter follows:
291 % o image: the image.
295 static void *DestroyOptions(void *message)
297 return(DestroyStringInfo((StringInfo *) message));
300 MagickExport LinkedListInfo *DestroyLocaleOptions(LinkedListInfo *messages)
302 assert(messages != (LinkedListInfo *) NULL);
303 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
304 return(DestroyLinkedList(messages,DestroyOptions));
308 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
312 + F o r m a t L o c a l e F i l e %
316 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
318 % FormatLocaleFile() prints formatted output of a variable argument list to a
319 % file in the "C" locale.
321 % The format of the FormatLocaleFile method is:
323 % ssize_t FormatLocaleFile(FILE *file,const char *format,...)
325 % A description of each parameter follows.
329 % o format: A file describing the format to use to write the remaining
334 MagickPrivate ssize_t FormatLocaleFileList(FILE *file,
335 const char *magick_restrict format,va_list operands)
340 #if defined(MAGICKCORE_LOCALE_SUPPORT) && defined(MAGICKCORE_HAVE_VFPRINTF_L)
345 locale=AcquireCLocale();
346 if (locale == (locale_t) NULL)
347 n=(ssize_t) vfprintf(file,format,operands);
349 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
350 n=(ssize_t) vfprintf_l(file,format,locale,operands);
352 n=(ssize_t) vfprintf_l(file,locale,format,operands);
356 #if defined(MAGICKCORE_LOCALE_SUPPORT) && defined(MAGICKCORE_HAVE_USELOCALE)
362 locale=AcquireCLocale();
363 if (locale == (locale_t) NULL)
364 n=(ssize_t) vfprintf(file,format,operands);
367 previous_locale=uselocale(locale);
368 n=(ssize_t) vfprintf(file,format,operands);
369 uselocale(previous_locale);
373 n=(ssize_t) vfprintf(file,format,operands);
379 MagickExport ssize_t FormatLocaleFile(FILE *file,
380 const char *magick_restrict format,...)
388 va_start(operands,format);
389 n=FormatLocaleFileList(file,format,operands);
395 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
399 + F o r m a t L o c a l e S t r i n g %
403 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
405 % FormatLocaleString() prints formatted output of a variable argument list to
406 % a string buffer in the "C" locale.
408 % The format of the FormatLocaleString method is:
410 % ssize_t FormatLocaleString(char *string,const size_t length,
411 % const char *format,...)
413 % A description of each parameter follows.
415 % o string: FormatLocaleString() returns the formatted string in this
418 % o length: the maximum length of the string.
420 % o format: A string describing the format to use to write the remaining
425 MagickPrivate ssize_t FormatLocaleStringList(char *magick_restrict string,
426 const size_t length,const char *magick_restrict format,va_list operands)
431 #if defined(MAGICKCORE_LOCALE_SUPPORT) && defined(MAGICKCORE_HAVE_VSNPRINTF_L)
436 locale=AcquireCLocale();
437 if (locale == (locale_t) NULL)
438 n=(ssize_t) vsnprintf(string,length,format,operands);
440 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
441 n=(ssize_t) vsnprintf_l(string,length,format,locale,operands);
443 n=(ssize_t) vsnprintf_l(string,length,locale,format,operands);
446 #elif defined(MAGICKCORE_HAVE_VSNPRINTF)
447 #if defined(MAGICKCORE_LOCALE_SUPPORT) && defined(MAGICKCORE_HAVE_USELOCALE)
453 locale=AcquireCLocale();
454 if (locale == (locale_t) NULL)
455 n=(ssize_t) vsnprintf(string,length,format,operands);
458 previous_locale=uselocale(locale);
459 n=(ssize_t) vsnprintf(string,length,format,operands);
460 uselocale(previous_locale);
464 n=(ssize_t) vsnprintf(string,length,format,operands);
467 n=(ssize_t) vsprintf(string,format,operands);
470 string[length-1]='\0';
474 MagickExport ssize_t FormatLocaleString(char *magick_restrict string,
475 const size_t length,const char *magick_restrict format,...)
483 va_start(operands,format);
484 n=FormatLocaleStringList(string,length,format,operands);
490 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
494 + G e t L o c a l e I n f o _ %
498 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
500 % GetLocaleInfo_() searches the locale list for the specified tag and if
501 % found returns attributes for that element.
503 % The format of the GetLocaleInfo method is:
505 % const LocaleInfo *GetLocaleInfo_(const char *tag,
506 % ExceptionInfo *exception)
508 % A description of each parameter follows:
510 % o tag: the locale tag.
512 % o exception: return any errors or warnings in this structure.
515 MagickExport const LocaleInfo *GetLocaleInfo_(const char *tag,
516 ExceptionInfo *exception)
521 assert(exception != (ExceptionInfo *) NULL);
522 if (IsLocaleTreeInstantiated(exception) == MagickFalse)
523 return((const LocaleInfo *) NULL);
524 LockSemaphoreInfo(locale_semaphore);
525 if ((tag == (const char *) NULL) || (LocaleCompare(tag,"*") == 0))
527 ResetSplayTreeIterator(locale_cache);
528 locale_info=(const LocaleInfo *) GetNextValueInSplayTree(locale_cache);
529 UnlockSemaphoreInfo(locale_semaphore);
532 locale_info=(const LocaleInfo *) GetValueFromSplayTree(locale_cache,tag);
533 UnlockSemaphoreInfo(locale_semaphore);
538 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
542 % G e t L o c a l e I n f o L i s t %
546 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
548 % GetLocaleInfoList() returns any locale messages that match the
551 % The format of the GetLocaleInfoList function is:
553 % const LocaleInfo **GetLocaleInfoList(const char *pattern,
554 % size_t *number_messages,ExceptionInfo *exception)
556 % A description of each parameter follows:
558 % o pattern: Specifies a pointer to a text string containing a pattern.
560 % o number_messages: This integer returns the number of locale messages in
563 % o exception: return any errors or warnings in this structure.
567 #if defined(__cplusplus) || defined(c_plusplus)
571 static int LocaleInfoCompare(const void *x,const void *y)
577 p=(const LocaleInfo **) x,
578 q=(const LocaleInfo **) y;
579 if (LocaleCompare((*p)->path,(*q)->path) == 0)
580 return(LocaleCompare((*p)->tag,(*q)->tag));
581 return(LocaleCompare((*p)->path,(*q)->path));
584 #if defined(__cplusplus) || defined(c_plusplus)
588 MagickExport const LocaleInfo **GetLocaleInfoList(const char *pattern,
589 size_t *number_messages,ExceptionInfo *exception)
594 register const LocaleInfo
601 Allocate locale list.
603 assert(pattern != (char *) NULL);
604 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
605 assert(number_messages != (size_t *) NULL);
607 p=GetLocaleInfo_("*",exception);
608 if (p == (const LocaleInfo *) NULL)
609 return((const LocaleInfo **) NULL);
610 messages=(const LocaleInfo **) AcquireQuantumMemory((size_t)
611 GetNumberOfNodesInSplayTree(locale_cache)+1UL,sizeof(*messages));
612 if (messages == (const LocaleInfo **) NULL)
613 return((const LocaleInfo **) NULL);
615 Generate locale list.
617 LockSemaphoreInfo(locale_semaphore);
618 ResetSplayTreeIterator(locale_cache);
619 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_cache);
620 for (i=0; p != (const LocaleInfo *) NULL; )
622 if ((p->stealth == MagickFalse) &&
623 (GlobExpression(p->tag,pattern,MagickTrue) != MagickFalse))
625 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_cache);
627 UnlockSemaphoreInfo(locale_semaphore);
628 qsort((void *) messages,(size_t) i,sizeof(*messages),LocaleInfoCompare);
629 messages[i]=(LocaleInfo *) NULL;
630 *number_messages=(size_t) i;
635 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
639 % G e t L o c a l e L i s t %
643 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
645 % GetLocaleList() returns any locale messages that match the specified
648 % The format of the GetLocaleList function is:
650 % char **GetLocaleList(const char *pattern,size_t *number_messages,
651 % Exceptioninfo *exception)
653 % A description of each parameter follows:
655 % o pattern: Specifies a pointer to a text string containing a pattern.
657 % o number_messages: This integer returns the number of messages in the
660 % o exception: return any errors or warnings in this structure.
664 #if defined(__cplusplus) || defined(c_plusplus)
668 static int LocaleTagCompare(const void *x,const void *y)
676 return(LocaleCompare(*p,*q));
679 #if defined(__cplusplus) || defined(c_plusplus)
683 MagickExport char **GetLocaleList(const char *pattern,size_t *number_messages,
684 ExceptionInfo *exception)
689 register const LocaleInfo
696 Allocate locale list.
698 assert(pattern != (char *) NULL);
699 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
700 assert(number_messages != (size_t *) NULL);
702 p=GetLocaleInfo_("*",exception);
703 if (p == (const LocaleInfo *) NULL)
704 return((char **) NULL);
705 messages=(char **) AcquireQuantumMemory((size_t)
706 GetNumberOfNodesInSplayTree(locale_cache)+1UL,sizeof(*messages));
707 if (messages == (char **) NULL)
708 return((char **) NULL);
709 LockSemaphoreInfo(locale_semaphore);
710 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_cache);
711 for (i=0; p != (const LocaleInfo *) NULL; )
713 if ((p->stealth == MagickFalse) &&
714 (GlobExpression(p->tag,pattern,MagickTrue) != MagickFalse))
715 messages[i++]=ConstantString(p->tag);
716 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_cache);
718 UnlockSemaphoreInfo(locale_semaphore);
719 qsort((void *) messages,(size_t) i,sizeof(*messages),LocaleTagCompare);
720 messages[i]=(char *) NULL;
721 *number_messages=(size_t) i;
726 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
730 % G e t L o c a l e M e s s a g e %
734 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
736 % GetLocaleMessage() returns a message in the current locale that matches the
739 % The format of the GetLocaleMessage method is:
741 % const char *GetLocaleMessage(const char *tag)
743 % A description of each parameter follows:
745 % o tag: Return a message that matches this tag in the current locale.
748 MagickExport const char *GetLocaleMessage(const char *tag)
751 name[MagickLocaleExtent];
759 if ((tag == (const char *) NULL) || (*tag == '\0'))
761 exception=AcquireExceptionInfo();
762 (void) FormatLocaleString(name,MagickLocaleExtent,"%s/",tag);
763 locale_info=GetLocaleInfo_(name,exception);
764 exception=DestroyExceptionInfo(exception);
765 if (locale_info != (const LocaleInfo *) NULL)
766 return(locale_info->message);
771 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
775 % G e t L o c a l e O p t i o n s %
779 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
781 % GetLocaleOptions() returns any Magick configuration messages associated
782 % with the specified filename.
784 % The format of the GetLocaleOptions method is:
786 % LinkedListInfo *GetLocaleOptions(const char *filename,
787 % ExceptionInfo *exception)
789 % A description of each parameter follows:
791 % o filename: the locale file tag.
793 % o exception: return any errors or warnings in this structure.
796 MagickExport LinkedListInfo *GetLocaleOptions(const char *filename,
797 ExceptionInfo *exception)
800 path[MagickPathExtent];
812 assert(filename != (const char *) NULL);
813 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
814 assert(exception != (ExceptionInfo *) NULL);
815 (void) CopyMagickString(path,filename,MagickPathExtent);
817 Load XML from configuration files to linked-list.
819 messages=NewLinkedList(0);
820 paths=GetConfigurePaths(filename,exception);
821 if (paths != (LinkedListInfo *) NULL)
823 ResetLinkedListIterator(paths);
824 element=(const char *) GetNextValueInLinkedList(paths);
825 while (element != (const char *) NULL)
827 (void) FormatLocaleString(path,MagickPathExtent,"%s%s",element,
829 (void) LogMagickEvent(LocaleEvent,GetMagickModule(),
830 "Searching for locale file: \"%s\"",path);
831 xml=ConfigureFileToStringInfo(path);
832 if (xml != (StringInfo *) NULL)
833 (void) AppendValueToLinkedList(messages,xml);
834 element=(const char *) GetNextValueInLinkedList(paths);
836 paths=DestroyLinkedList(paths,RelinquishMagickMemory);
838 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
843 blob=(char *) NTResourceToBlob(filename);
844 if (blob != (char *) NULL)
846 xml=AcquireStringInfo(0);
847 SetStringInfoLength(xml,strlen(blob)+1);
848 SetStringInfoDatum(xml,(const unsigned char *) blob);
849 blob=(char *) RelinquishMagickMemory(blob);
850 SetStringInfoPath(xml,filename);
851 (void) AppendValueToLinkedList(messages,xml);
855 ResetLinkedListIterator(messages);
860 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
864 % G e t L o c a l e V a l u e %
868 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
870 % GetLocaleValue() returns the message associated with the locale info.
872 % The format of the GetLocaleValue method is:
874 % const char *GetLocaleValue(const LocaleInfo *locale_info)
876 % A description of each parameter follows:
878 % o locale_info: The locale info.
881 MagickExport const char *GetLocaleValue(const LocaleInfo *locale_info)
883 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
884 assert(locale_info != (LocaleInfo *) NULL);
885 assert(locale_info->signature == MagickCoreSignature);
886 return(locale_info->message);
890 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
894 + I s L o c a l e T r e e I n s t a n t i a t e d %
898 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
900 % IsLocaleTreeInstantiated() determines if the locale tree is instantiated.
901 % If not, it instantiates the tree and returns it.
903 % The format of the IsLocaleInstantiated method is:
905 % MagickBooleanType IsLocaleTreeInstantiated(ExceptionInfo *exception)
907 % A description of each parameter follows.
909 % o exception: return any errors or warnings in this structure.
912 static MagickBooleanType IsLocaleTreeInstantiated(ExceptionInfo *exception)
914 if (locale_cache == (SplayTreeInfo *) NULL)
916 if (locale_semaphore == (SemaphoreInfo *) NULL)
917 ActivateSemaphoreInfo(&locale_semaphore);
918 LockSemaphoreInfo(locale_semaphore);
919 if (locale_cache == (SplayTreeInfo *) NULL)
927 locale=(char *) NULL;
928 p=setlocale(LC_CTYPE,(const char *) NULL);
929 if (p != (const char *) NULL)
930 locale=ConstantString(p);
931 if (locale == (char *) NULL)
932 locale=GetEnvironmentValue("LC_ALL");
933 if (locale == (char *) NULL)
934 locale=GetEnvironmentValue("LC_MESSAGES");
935 if (locale == (char *) NULL)
936 locale=GetEnvironmentValue("LC_CTYPE");
937 if (locale == (char *) NULL)
938 locale=GetEnvironmentValue("LANG");
939 if (locale == (char *) NULL)
940 locale=ConstantString("C");
941 locale_cache=AcquireLocaleSplayTree(LocaleFilename,locale,exception);
942 locale=DestroyString(locale);
944 UnlockSemaphoreInfo(locale_semaphore);
946 return(locale_cache != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
950 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
954 + I n t e r p r e t L o c a l e V a l u e %
958 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
960 % InterpretLocaleValue() interprets the string as a floating point number in
961 % the "C" locale and returns its value as a double. If sentinal is not a null
962 % pointer, the method also sets the value pointed by sentinal to point to the
963 % first character after the number.
965 % The format of the InterpretLocaleValue method is:
967 % double InterpretLocaleValue(const char *value,char **sentinal)
969 % A description of each parameter follows:
971 % o value: the string value.
973 % o sentinal: if sentinal is not NULL, a pointer to the character after the
974 % last character used in the conversion is stored in the location
975 % referenced by sentinal.
978 MagickExport double InterpretLocaleValue(const char *magick_restrict string,
979 char **magick_restrict sentinal)
987 if ((*string == '0') && ((string[1] | 0x20)=='x'))
988 value=(double) strtoul(string,&q,16);
991 #if defined(MAGICKCORE_LOCALE_SUPPORT) && defined(MAGICKCORE_HAVE_STRTOD_L)
995 locale=AcquireCLocale();
996 if (locale == (locale_t) NULL)
997 value=strtod(string,&q);
999 value=strtod_l(string,&q,locale);
1001 value=strtod(string,&q);
1004 if (sentinal != (char **) NULL)
1010 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1014 % L i s t L o c a l e I n f o %
1018 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1020 % ListLocaleInfo() lists the locale info to a file.
1022 % The format of the ListLocaleInfo method is:
1024 % MagickBooleanType ListLocaleInfo(FILE *file,ExceptionInfo *exception)
1026 % A description of each parameter follows.
1028 % o file: An pointer to a FILE.
1030 % o exception: return any errors or warnings in this structure.
1033 MagickExport MagickBooleanType ListLocaleInfo(FILE *file,
1034 ExceptionInfo *exception)
1048 if (file == (const FILE *) NULL)
1051 locale_info=GetLocaleInfoList("*",&number_messages,exception);
1052 if (locale_info == (const LocaleInfo **) NULL)
1053 return(MagickFalse);
1054 path=(const char *) NULL;
1055 for (i=0; i < (ssize_t) number_messages; i++)
1057 if (locale_info[i]->stealth != MagickFalse)
1059 if ((path == (const char *) NULL) ||
1060 (LocaleCompare(path,locale_info[i]->path) != 0))
1062 if (locale_info[i]->path != (char *) NULL)
1063 (void) FormatLocaleFile(file,"\nPath: %s\n\n",locale_info[i]->path);
1064 (void) FormatLocaleFile(file,"Tag/Message\n");
1065 (void) FormatLocaleFile(file,
1066 "-------------------------------------------------"
1067 "------------------------------\n");
1069 path=locale_info[i]->path;
1070 (void) FormatLocaleFile(file,"%s\n",locale_info[i]->tag);
1071 if (locale_info[i]->message != (char *) NULL)
1072 (void) FormatLocaleFile(file," %s",locale_info[i]->message);
1073 (void) FormatLocaleFile(file,"\n");
1075 (void) fflush(file);
1076 locale_info=(const LocaleInfo **)
1077 RelinquishMagickMemory((void *) locale_info);
1082 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1086 + L o a d L o c a l e C a c h e %
1090 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1092 % LoadLocaleCache() loads the locale configurations which provides a mapping
1093 % between locale attributes and a locale name.
1095 % The format of the LoadLocaleCache method is:
1097 % MagickBooleanType LoadLocaleCache(SplayTreeInfo *cache,const char *xml,
1098 % const char *filename,const size_t depth,ExceptionInfo *exception)
1100 % A description of each parameter follows:
1102 % o xml: The locale list in XML format.
1104 % o filename: The locale list filename.
1106 % o depth: depth of <include /> statements.
1108 % o exception: return any errors or warnings in this structure.
1112 static void ChopLocaleComponents(char *path,const size_t components)
1122 p=path+strlen(path)-1;
1125 for (count=0; (count < (ssize_t) components) && (p > path); p--)
1131 if (count < (ssize_t) components)
1135 static void LocaleFatalErrorHandler(
1136 const ExceptionType magick_unused(severity),
1137 const char *reason,const char *description)
1139 magick_unreferenced(severity);
1141 if (reason == (char *) NULL)
1143 (void) FormatLocaleFile(stderr,"%s: %s",GetClientName(),reason);
1144 if (description != (char *) NULL)
1145 (void) FormatLocaleFile(stderr," (%s)",description);
1146 (void) FormatLocaleFile(stderr,".\n");
1147 (void) fflush(stderr);
1151 static MagickBooleanType LoadLocaleCache(SplayTreeInfo *cache,const char *xml,
1152 const char *filename,const char *locale,const size_t depth,ExceptionInfo *exception)
1155 keyword[MagickLocaleExtent],
1156 message[MagickLocaleExtent],
1157 tag[MagickLocaleExtent],
1179 Read the locale configure file.
1181 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1182 "Loading locale configure file \"%s\" ...",filename);
1183 if (xml == (const char *) NULL)
1184 return(MagickFalse);
1186 locale_info=(LocaleInfo *) NULL;
1190 fatal_handler=SetFatalErrorHandler(LocaleFatalErrorHandler);
1191 token=AcquireString(xml);
1192 extent=strlen(token)+MagickPathExtent;
1193 for (q=(char *) xml; *q != '\0'; )
1198 GetNextToken(q,&q,extent,token);
1201 (void) CopyMagickString(keyword,token,MagickLocaleExtent);
1202 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
1207 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
1209 GetNextToken(q,&q,extent,token);
1210 while (isspace((int) ((unsigned char) *q)) != 0)
1215 if (LocaleNCompare(keyword,"<!--",4) == 0)
1220 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
1222 GetNextToken(q,&q,extent,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,MagickLocaleExtent);
1236 GetNextToken(q,&q,extent,token);
1239 GetNextToken(q,&q,extent,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[MagickPathExtent],
1258 GetPathComponent(filename,HeadPath,path);
1260 (void) ConcatenateMagickString(path,DirectorySeparator,
1262 if (*token == *DirectorySeparator)
1263 (void) CopyMagickString(path,token,MagickPathExtent);
1265 (void) ConcatenateMagickString(path,token,MagickPathExtent);
1266 file_xml=FileToXML(path,~0UL);
1267 if (file_xml != (char *) NULL)
1269 status&=LoadLocaleCache(cache,file_xml,path,locale,
1271 file_xml=DestroyString(file_xml);
1278 if (LocaleCompare(keyword,"<locale") == 0)
1283 while ((*token != '>') && (*q != '\0'))
1285 (void) CopyMagickString(keyword,token,MagickLocaleExtent);
1286 GetNextToken(q,&q,extent,token);
1289 GetNextToken(q,&q,extent,token);
1293 if (LocaleCompare(keyword,"</locale>") == 0)
1295 ChopLocaleComponents(tag,1);
1296 (void) ConcatenateMagickString(tag,"/",MagickLocaleExtent);
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,MagickLocaleExtent);
1311 GetNextToken(q,&q,extent,token);
1314 GetNextToken(q,&q,extent,token);
1315 if (LocaleCompare(keyword,"name") == 0)
1317 (void) ConcatenateMagickString(tag,token,MagickLocaleExtent);
1318 (void) ConcatenateMagickString(tag,"/",MagickLocaleExtent);
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),
1328 MagickLocaleExtent));
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=MagickCoreSignature;
1337 status=AddValueToSplayTree(cache,locale_info->tag,locale_info);
1338 if (status == MagickFalse)
1339 (void) ThrowMagickException(exception,GetMagickModule(),
1340 ResourceLimitError,"MemoryAllocationFailed","`%s'",
1342 (void) ConcatenateMagickString(tag,message,MagickLocaleExtent);
1343 (void) ConcatenateMagickString(tag,"\n",MagickLocaleExtent);
1347 if (LocaleCompare(keyword,"</message>") == 0)
1349 ChopLocaleComponents(tag,2);
1350 (void) ConcatenateMagickString(tag,"/",MagickLocaleExtent);
1353 if (*keyword == '<')
1358 if (*(keyword+1) == '?')
1360 if (*(keyword+1) == '/')
1362 ChopLocaleComponents(tag,1);
1364 (void) ConcatenateMagickString(tag,"/",MagickLocaleExtent);
1367 token[strlen(token)-1]='\0';
1368 (void) CopyMagickString(token,token+1,MagickLocaleExtent);
1369 (void) ConcatenateMagickString(tag,token,MagickLocaleExtent);
1370 (void) ConcatenateMagickString(tag,"/",MagickLocaleExtent);
1373 GetNextToken(q,(const char **) NULL,extent,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 a r e %
1391 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1393 % LocaleCompare() performs a case-insensitive comparison of two strings
1394 % byte-by-byte, according to the ordering of the current locale encoding.
1395 % LocaleCompare returns an integer greater than, equal to, or less than 0,
1396 % if the string pointed to by p is greater than, equal to, or less than the
1397 % string pointed to by q respectively. The sign of a non-zero return value
1398 % is determined by the sign of the difference between the values of the first
1399 % pair of bytes that differ in the strings being compared.
1401 % The format of the LocaleCompare method is:
1403 % int LocaleCompare(const char *p,const char *q)
1405 % A description of each parameter follows:
1407 % o p: A pointer to a character string.
1409 % o q: A pointer to a character string to compare to p.
1412 MagickExport int LocaleCompare(const char *p,const char *q)
1414 if ((p == (char *) NULL) && (q == (char *) NULL))
1416 if (p == (char *) NULL)
1418 if (q == (char *) NULL)
1420 #if defined(MAGICKCORE_HAVE_STRCASECMP)
1421 return(strcasecmp(p,q));
1430 c=(int) *((unsigned char *) p);
1431 d=(int) *((unsigned char *) q);
1432 if ((c == 0) || (AsciiMap[c] != AsciiMap[d]))
1437 return(AsciiMap[c]-(int) AsciiMap[d]);
1443 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1447 % L o c a l e L o w e r %
1451 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1453 % LocaleLower() transforms all of the characters in the supplied
1454 % null-terminated string, changing all uppercase letters to lowercase.
1456 % The format of the LocaleLower method is:
1458 % void LocaleLower(char *string)
1460 % A description of each parameter follows:
1462 % o string: A pointer to the string to convert to lower-case Locale.
1465 MagickExport void LocaleLower(char *string)
1470 assert(string != (char *) NULL);
1471 for (q=string; *q != '\0'; q++)
1472 *q=(char) tolower((int) *q);
1476 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1480 % L o c a l e N C o m p a r e %
1484 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1486 % LocaleNCompare() performs a case-insensitive comparison of two strings
1487 % byte-by-byte, according to the ordering of the current locale encoding.
1489 % LocaleNCompare returns an integer greater than, equal to, or less than 0,
1490 % if the string pointed to by p is greater than, equal to, or less than the
1491 % string pointed to by q respectively. The sign of a non-zero return value
1492 % is determined by the sign of the difference between the values of the first
1493 % pair of bytes that differ in the strings being compared.
1495 % The LocaleNCompare method makes the same comparison as LocaleCompare but
1496 % looks at a maximum of n bytes. Bytes following a null byte are not
1499 % The format of the LocaleNCompare method is:
1501 % int LocaleNCompare(const char *p,const char *q,const size_t n)
1503 % A description of each parameter follows:
1505 % o p: A pointer to a character string.
1507 % o q: A pointer to a character string to compare to p.
1509 % o length: the number of characters to compare in strings p and q.
1512 MagickExport int LocaleNCompare(const char *p,const char *q,const size_t length)
1514 if ((p == (char *) NULL) && (q == (char *) NULL))
1516 if (p == (char *) NULL)
1518 if (q == (char *) NULL)
1520 #if defined(MAGICKCORE_HAVE_STRNCASECMP)
1521 return(strncasecmp(p,q,length));
1531 for (i=length; i != 0; i--)
1533 c=(int) *((unsigned char *) p);
1534 d=(int) *((unsigned char *) q);
1535 if (AsciiMap[c] != AsciiMap[d])
1536 return(AsciiMap[c]-(int) AsciiMap[d]);
1548 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1552 % L o c a l e U p p e r %
1556 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1558 % LocaleUpper() transforms all of the characters in the supplied
1559 % null-terminated string, changing all lowercase letters to uppercase.
1561 % The format of the LocaleUpper method is:
1563 % void LocaleUpper(char *string)
1565 % A description of each parameter follows:
1567 % o string: A pointer to the string to convert to upper-case Locale.
1570 MagickExport void LocaleUpper(char *string)
1575 assert(string != (char *) NULL);
1576 for (q=string; *q != '\0'; q++)
1577 *q=(char) toupper((int) *q);
1581 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1585 + L o c a l e C o m p o n e n t G e n e s i s %
1589 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1591 % LocaleComponentGenesis() instantiates the locale component.
1593 % The format of the LocaleComponentGenesis method is:
1595 % MagickBooleanType LocaleComponentGenesis(void)
1598 MagickPrivate MagickBooleanType LocaleComponentGenesis(void)
1600 if (locale_semaphore == (SemaphoreInfo *) NULL)
1601 locale_semaphore=AcquireSemaphoreInfo();
1606 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1610 + L o c a l e C o m p o n e n t T e r m i n u s %
1614 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1616 % LocaleComponentTerminus() destroys the locale component.
1618 % The format of the LocaleComponentTerminus method is:
1620 % LocaleComponentTerminus(void)
1623 MagickPrivate void LocaleComponentTerminus(void)
1625 if (locale_semaphore == (SemaphoreInfo *) NULL)
1626 ActivateSemaphoreInfo(&locale_semaphore);
1627 LockSemaphoreInfo(locale_semaphore);
1628 if (locale_cache != (SplayTreeInfo *) NULL)
1629 locale_cache=DestroySplayTree(locale_cache);
1630 #if defined(MAGICKCORE_LOCALE_SUPPORT)
1633 UnlockSemaphoreInfo(locale_semaphore);
1634 RelinquishSemaphoreInfo(&locale_semaphore);