2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % L OOO CCCC AAA L EEEEE %
8 % L O O C AAAAA L EEE %
10 % LLLLL OOO CCCC A A LLLLL EEEEE %
13 % MagickCore Image Locale Methods %
20 % Copyright 1999-2014 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #include "MagickCore/studio.h"
43 #include "MagickCore/blob.h"
44 #include "MagickCore/client.h"
45 #include "MagickCore/configure.h"
46 #include "MagickCore/exception.h"
47 #include "MagickCore/exception-private.h"
48 #include "MagickCore/hashmap.h"
49 #include "MagickCore/locale_.h"
50 #include "MagickCore/locale-private.h"
51 #include "MagickCore/log.h"
52 #include "MagickCore/memory_.h"
53 #include "MagickCore/nt-base-private.h"
54 #include "MagickCore/semaphore.h"
55 #include "MagickCore/splay-tree.h"
56 #include "MagickCore/string_.h"
57 #include "MagickCore/string-private.h"
58 #include "MagickCore/token.h"
59 #include "MagickCore/utility.h"
60 #include "MagickCore/utility-private.h"
61 #include "MagickCore/xml-tree.h"
66 #define LocaleFilename "locale.xml"
67 #define MaxRecursionDepth 200
72 #if defined(__CYGWIN__)
73 typedef struct _locale_t
82 "<?xml version=\"1.0\"?>"
84 " <locale name=\"C\">"
86 " <Message name=\"\">"
93 *locale_semaphore = (SemaphoreInfo *) NULL;
96 *locale_cache = (SplayTreeInfo *) NULL;
98 #if defined(MAGICKCORE_HAVE_STRTOD_L)
99 static volatile locale_t
100 c_locale = (locale_t) NULL;
104 Forward declarations.
106 static MagickBooleanType
107 IsLocaleTreeInstantiated(ExceptionInfo *),
108 LoadLocaleCache(const char *,const char *,const char *,const size_t,
111 #if defined(MAGICKCORE_HAVE_STRTOD_L)
113 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
117 + A c q u i r e C L o c a l e %
121 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
123 % AcquireCLocale() allocates the C locale object, or (locale_t) 0 with
124 % errno set if it cannot be acquired.
126 % The format of the AcquireCLocale method is:
128 % locale_t AcquireCLocale(void)
131 static locale_t AcquireCLocale(void)
133 #if defined(MAGICKCORE_HAVE_NEWLOCALE)
134 if (c_locale == (locale_t) NULL)
135 c_locale=newlocale(LC_ALL_MASK,"C",(locale_t) 0);
136 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
137 if (c_locale == (locale_t) NULL)
138 c_locale=_create_locale(LC_ALL,"C");
145 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
149 % A c q u i r e L o c a l e S p l a y T r e e %
153 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
155 % AcquireLocaleSplayTree() caches one or more locale configurations which
156 % provides a mapping between locale attributes and a locale tag.
158 % The format of the AcquireLocaleSplayTree method is:
160 % SplayTreeInfo *AcquireLocaleSplayTree(const char *filename,
161 % ExceptionInfo *exception)
163 % A description of each parameter follows:
165 % o filename: the font file tag.
167 % o locale: the actual locale.
169 % o exception: return any errors or warnings in this structure.
173 static void *DestroyLocaleNode(void *locale_info)
178 p=(LocaleInfo *) locale_info;
179 if (p->path != (char *) NULL)
180 p->path=DestroyString(p->path);
181 if (p->tag != (char *) NULL)
182 p->tag=DestroyString(p->tag);
183 if (p->message != (char *) NULL)
184 p->message=DestroyString(p->message);
185 return(RelinquishMagickMemory(p));
188 static SplayTreeInfo *AcquireLocaleSplayTree(const char *filename,
189 const char *locale,ExceptionInfo *exception)
203 locale_cache=NewSplayTree(CompareSplayTreeString,(void *(*)(void *)) NULL,
205 if (locale_cache == (SplayTreeInfo *) NULL)
206 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
208 options=GetLocaleOptions(filename,exception);
209 option=(const StringInfo *) GetNextValueInLinkedList(options);
210 #if !defined(MAGICKCORE_ZERO_CONFIGURATION_SUPPORT)
211 while (option != (const StringInfo *) NULL)
213 status&=LoadLocaleCache((const char *) GetStringInfoDatum(option),
214 GetStringInfoPath(option),locale,0,exception);
215 option=(const StringInfo *) GetNextValueInLinkedList(options);
218 options=DestroyLocaleOptions(options);
219 #if !defined(MAGICKCORE_ZERO_CONFIGURATION_SUPPORT)
220 if (GetNumberOfNodesInSplayTree(locale_cache) == 0)
222 options=GetLocaleOptions("english.xml",exception);
223 option=(const StringInfo *) GetNextValueInLinkedList(options);
224 while (option != (const StringInfo *) NULL)
226 status&=LoadLocaleCache((const char *) GetStringInfoDatum(option),
227 GetStringInfoPath(option),locale,0,exception);
228 option=(const StringInfo *) GetNextValueInLinkedList(options);
230 options=DestroyLocaleOptions(options);
233 if (GetNumberOfNodesInSplayTree(locale_cache) == 0)
234 status&=LoadLocaleCache(LocaleMap,"built-in",locale,0,exception);
235 return(locale_cache);
238 #if defined(MAGICKCORE_HAVE_STRTOD_L)
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)
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 *restrict format,va_list operands)
340 #if 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_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,const char *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 *restrict string,
426 const size_t length,const char *restrict format,va_list operands)
431 #if 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_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 *restrict string,
475 const size_t length,const char *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,
684 size_t *number_messages,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)
759 if ((tag == (const char *) NULL) || (*tag == '\0'))
761 exception=AcquireExceptionInfo();
762 (void) FormatLocaleString(name,MaxTextExtent,"%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)
812 assert(filename != (const char *) NULL);
813 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
814 assert(exception != (ExceptionInfo *) NULL);
815 (void) CopyMagickString(path,filename,MaxTextExtent);
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,MaxTextExtent,"%s%s",element,filename);
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,(unsigned char *) blob);
848 SetStringInfoPath(xml,filename);
849 (void) AppendValueToLinkedList(messages,xml);
853 ResetLinkedListIterator(messages);
858 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
862 % G e t L o c a l e V a l u e %
866 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
868 % GetLocaleValue() returns the message associated with the locale info.
870 % The format of the GetLocaleValue method is:
872 % const char *GetLocaleValue(const LocaleInfo *locale_info)
874 % A description of each parameter follows:
876 % o locale_info: The locale info.
879 MagickExport const char *GetLocaleValue(const LocaleInfo *locale_info)
881 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
882 assert(locale_info != (LocaleInfo *) NULL);
883 assert(locale_info->signature == MagickSignature);
884 return(locale_info->message);
888 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
892 + I s L o c a l e T r e e I n s t a n t i a t e d %
896 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
898 % IsLocaleTreeInstantiated() determines if the locale tree is instantiated.
899 % If not, it instantiates the tree and returns it.
901 % The format of the IsLocaleInstantiated method is:
903 % MagickBooleanType IsLocaleTreeInstantiated(ExceptionInfo *exception)
905 % A description of each parameter follows.
907 % o exception: return any errors or warnings in this structure.
910 static MagickBooleanType IsLocaleTreeInstantiated(ExceptionInfo *exception)
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);
941 return(locale_cache != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
945 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
949 + I n t e r p r e t L o c a l e V a l u e %
953 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
955 % InterpretLocaleValue() interprets the string as a floating point number in
956 % the "C" locale and returns its value as a double. If sentinal is not a null
957 % pointer, the method also sets the value pointed by sentinal to point to the
958 % first character after the number.
960 % The format of the InterpretLocaleValue method is:
962 % double InterpretLocaleValue(const char *value,char **sentinal)
964 % A description of each parameter follows:
966 % o value: the string value.
968 % o sentinal: if sentinal is not NULL, a pointer to the character after the
969 % last character used in the conversion is stored in the location
970 % referenced by sentinal.
973 MagickExport double InterpretLocaleValue(const char *restrict string,
974 char **restrict sentinal)
982 if ((*string == '0') && ((string[1] | 0x20)=='x'))
983 value=(double) strtoul(string,&q,16);
986 #if defined(MAGICKCORE_HAVE_STRTOD_L)
990 locale=AcquireCLocale();
991 if (locale == (locale_t) NULL)
992 value=strtod(string,&q);
994 value=strtod_l(string,&q,locale);
996 value=strtod(string,&q);
999 if (sentinal != (char **) NULL)
1005 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1009 % L i s t L o c a l e I n f o %
1013 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1015 % ListLocaleInfo() lists the locale info to a file.
1017 % The format of the ListLocaleInfo method is:
1019 % MagickBooleanType ListLocaleInfo(FILE *file,ExceptionInfo *exception)
1021 % A description of each parameter follows.
1023 % o file: An pointer to a FILE.
1025 % o exception: return any errors or warnings in this structure.
1028 MagickExport MagickBooleanType ListLocaleInfo(FILE *file,
1029 ExceptionInfo *exception)
1043 if (file == (const FILE *) NULL)
1046 locale_info=GetLocaleInfoList("*",&number_messages,exception);
1047 if (locale_info == (const LocaleInfo **) NULL)
1048 return(MagickFalse);
1049 path=(const char *) NULL;
1050 for (i=0; i < (ssize_t) number_messages; i++)
1052 if (locale_info[i]->stealth != MagickFalse)
1054 if ((path == (const char *) NULL) ||
1055 (LocaleCompare(path,locale_info[i]->path) != 0))
1057 if (locale_info[i]->path != (char *) NULL)
1058 (void) FormatLocaleFile(file,"\nPath: %s\n\n",locale_info[i]->path);
1059 (void) FormatLocaleFile(file,"Tag/Message\n");
1060 (void) FormatLocaleFile(file,
1061 "-------------------------------------------------"
1062 "------------------------------\n");
1064 path=locale_info[i]->path;
1065 (void) FormatLocaleFile(file,"%s\n",locale_info[i]->tag);
1066 if (locale_info[i]->message != (char *) NULL)
1067 (void) FormatLocaleFile(file," %s",locale_info[i]->message);
1068 (void) FormatLocaleFile(file,"\n");
1070 (void) fflush(file);
1071 locale_info=(const LocaleInfo **)
1072 RelinquishMagickMemory((void *) locale_info);
1077 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1081 + L o a d L o c a l e L i s t %
1085 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1087 % LoadLocaleCache() loads the locale configurations which provides a mapping
1088 % between locale attributes and a locale name.
1090 % The format of the LoadLocaleCache method is:
1092 % MagickBooleanType LoadLocaleCache(const char *xml,const char *filename,
1093 % const size_t depth,ExceptionInfo *exception)
1095 % A description of each parameter follows:
1097 % o xml: The locale list in XML format.
1099 % o filename: The locale list filename.
1101 % o depth: depth of <include /> statements.
1103 % o exception: return any errors or warnings in this structure.
1107 static void ChopLocaleComponents(char *path,const size_t components)
1117 p=path+strlen(path)-1;
1120 for (count=0; (count < (ssize_t) components) && (p > path); p--)
1126 if (count < (ssize_t) components)
1130 static void LocaleFatalErrorHandler(
1131 const ExceptionType magick_unused(severity),
1132 const char *reason,const char *description)
1134 if (reason == (char *) NULL)
1136 (void) FormatLocaleFile(stderr,"%s: %s",GetClientName(),reason);
1137 if (description != (char *) NULL)
1138 (void) FormatLocaleFile(stderr," (%s)",description);
1139 (void) FormatLocaleFile(stderr,".\n");
1140 (void) fflush(stderr);
1144 static inline size_t MagickMin(const size_t x,const size_t y)
1151 static MagickBooleanType LoadLocaleCache(const char *xml,const char *filename,
1152 const char *locale,const size_t depth,ExceptionInfo *exception)
1155 keyword[MaxTextExtent],
1156 message[MaxTextExtent],
1176 Read the locale configure file.
1178 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1179 "Loading locale configure file \"%s\" ...",filename);
1180 if (xml == (const char *) NULL)
1181 return(MagickFalse);
1182 if (locale_cache == (SplayTreeInfo *) NULL)
1184 locale_cache=NewSplayTree(CompareSplayTreeString,(void *(*)(void *)) NULL,
1186 if (locale_cache == (SplayTreeInfo *) NULL)
1187 return(MagickFalse);
1190 locale_info=(LocaleInfo *) NULL;
1194 fatal_handler=SetFatalErrorHandler(LocaleFatalErrorHandler);
1195 token=AcquireString(xml);
1196 for (q=(char *) xml; *q != '\0'; )
1201 GetMagickToken(q,&q,token);
1204 (void) CopyMagickString(keyword,token,MaxTextExtent);
1205 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
1210 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
1212 GetMagickToken(q,&q,token);
1213 while (isspace((int) ((unsigned char) *q)) != 0)
1218 if (LocaleNCompare(keyword,"<!--",4) == 0)
1223 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
1225 GetMagickToken(q,&q,token);
1226 while (isspace((int) ((unsigned char) *q)) != 0)
1231 if (LocaleCompare(keyword,"<include") == 0)
1236 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
1238 (void) CopyMagickString(keyword,token,MaxTextExtent);
1239 GetMagickToken(q,&q,token);
1242 GetMagickToken(q,&q,token);
1243 if (LocaleCompare(keyword,"locale") == 0)
1245 if (LocaleCompare(locale,token) != 0)
1249 if (LocaleCompare(keyword,"file") == 0)
1252 (void) ThrowMagickException(exception,GetMagickModule(),
1253 ConfigureError,"IncludeElementNestedTooDeeply","`%s'",token);
1257 path[MaxTextExtent],
1261 GetPathComponent(filename,HeadPath,path);
1263 (void) ConcatenateMagickString(path,DirectorySeparator,
1265 if (*token == *DirectorySeparator)
1266 (void) CopyMagickString(path,token,MaxTextExtent);
1268 (void) ConcatenateMagickString(path,token,MaxTextExtent);
1269 xml=FileToString(path,~0UL,exception);
1270 if (xml != (char *) NULL)
1272 status=LoadLocaleCache(xml,path,locale,depth+1,exception);
1273 xml=(char *) RelinquishMagickMemory(xml);
1280 if (LocaleCompare(keyword,"<locale") == 0)
1285 while ((*token != '>') && (*q != '\0'))
1287 (void) CopyMagickString(keyword,token,MaxTextExtent);
1288 GetMagickToken(q,&q,token);
1291 GetMagickToken(q,&q,token);
1295 if (LocaleCompare(keyword,"</locale>") == 0)
1297 ChopLocaleComponents(tag,1);
1298 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1301 if (LocaleCompare(keyword,"<localemap>") == 0)
1303 if (LocaleCompare(keyword,"</localemap>") == 0)
1305 if (LocaleCompare(keyword,"<message") == 0)
1310 while ((*token != '>') && (*q != '\0'))
1312 (void) CopyMagickString(keyword,token,MaxTextExtent);
1313 GetMagickToken(q,&q,token);
1316 GetMagickToken(q,&q,token);
1317 if (LocaleCompare(keyword,"name") == 0)
1319 (void) ConcatenateMagickString(tag,token,MaxTextExtent);
1320 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1323 for (p=(char *) q; (*q != '<') && (*q != '\0'); q++) ;
1324 while (isspace((int) ((unsigned char) *p)) != 0)
1327 while ((isspace((int) ((unsigned char) *q)) != 0) && (q > p))
1329 (void) CopyMagickString(message,p,MagickMin((size_t) (q-p+2),
1331 locale_info=(LocaleInfo *) AcquireMagickMemory(sizeof(*locale_info));
1332 if (locale_info == (LocaleInfo *) NULL)
1333 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1334 (void) ResetMagickMemory(locale_info,0,sizeof(*locale_info));
1335 locale_info->path=ConstantString(filename);
1336 locale_info->tag=ConstantString(tag);
1337 locale_info->message=ConstantString(message);
1338 locale_info->signature=MagickSignature;
1339 status=AddValueToSplayTree(locale_cache,locale_info->tag,locale_info);
1340 if (status == MagickFalse)
1341 (void) ThrowMagickException(exception,GetMagickModule(),
1342 ResourceLimitError,"MemoryAllocationFailed","`%s'",
1344 (void) ConcatenateMagickString(tag,message,MaxTextExtent);
1345 (void) ConcatenateMagickString(tag,"\n",MaxTextExtent);
1349 if (LocaleCompare(keyword,"</message>") == 0)
1351 ChopLocaleComponents(tag,2);
1352 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1355 if (*keyword == '<')
1360 if (*(keyword+1) == '?')
1362 if (*(keyword+1) == '/')
1364 ChopLocaleComponents(tag,1);
1366 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1369 token[strlen(token)-1]='\0';
1370 (void) CopyMagickString(token,token+1,MaxTextExtent);
1371 (void) ConcatenateMagickString(tag,token,MaxTextExtent);
1372 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1375 GetMagickToken(q,(const char **) NULL,token);
1379 token=(char *) RelinquishMagickMemory(token);
1380 (void) SetFatalErrorHandler(fatal_handler);
1385 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1389 + L o c a l e C o m p o n e n t G e n e s i s %
1393 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1395 % LocaleComponentGenesis() instantiates the locale component.
1397 % The format of the LocaleComponentGenesis method is:
1399 % MagickBooleanType LocaleComponentGenesis(void)
1402 MagickPrivate MagickBooleanType LocaleComponentGenesis(void)
1404 locale_semaphore=AcquireSemaphoreInfo();
1409 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1413 + L o c a l e C o m p o n e n t T e r m i n u s %
1417 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1419 % LocaleComponentTerminus() destroys the locale component.
1421 % The format of the LocaleComponentTerminus method is:
1423 % LocaleComponentTerminus(void)
1426 MagickPrivate void LocaleComponentTerminus(void)
1428 if (locale_semaphore == (SemaphoreInfo *) NULL)
1429 ActivateSemaphoreInfo(&locale_semaphore);
1430 LockSemaphoreInfo(locale_semaphore);
1431 if (locale_cache != (SplayTreeInfo *) NULL)
1432 locale_cache=DestroySplayTree(locale_cache);
1433 #if defined(MAGICKCORE_HAVE_STRTOD_L)
1436 UnlockSemaphoreInfo(locale_semaphore);
1437 RelinquishSemaphoreInfo(&locale_semaphore);