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"
62 #include "MagickCore/xml-tree-private.h"
67 #define LocaleFilename "locale.xml"
68 #define MaxRecursionDepth 200
73 #if defined(__CYGWIN__)
74 typedef struct _locale_t
83 "<?xml version=\"1.0\"?>"
85 " <locale name=\"C\">"
87 " <Message name=\"\">"
94 *locale_semaphore = (SemaphoreInfo *) NULL;
97 *locale_cache = (SplayTreeInfo *) NULL;
99 #if defined(MAGICKCORE_HAVE_STRTOD_L)
100 static volatile locale_t
101 c_locale = (locale_t) NULL;
105 Forward declarations.
107 static MagickBooleanType
108 IsLocaleTreeInstantiated(ExceptionInfo *),
109 LoadLocaleCache(const char *,const char *,const char *,const size_t,
112 #if defined(MAGICKCORE_HAVE_STRTOD_L)
114 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
118 + A c q u i r e C L o c a l e %
122 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
124 % AcquireCLocale() allocates the C locale object, or (locale_t) 0 with
125 % errno set if it cannot be acquired.
127 % The format of the AcquireCLocale method is:
129 % locale_t AcquireCLocale(void)
132 static locale_t AcquireCLocale(void)
134 #if defined(MAGICKCORE_HAVE_NEWLOCALE)
135 if (c_locale == (locale_t) NULL)
136 c_locale=newlocale(LC_ALL_MASK,"C",(locale_t) 0);
137 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
138 if (c_locale == (locale_t) NULL)
139 c_locale=_create_locale(LC_ALL,"C");
146 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
150 % A c q u i r e L o c a l e S p l a y T r e e %
154 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
156 % AcquireLocaleSplayTree() caches one or more locale configurations which
157 % provides a mapping between locale attributes and a locale tag.
159 % The format of the AcquireLocaleSplayTree method is:
161 % SplayTreeInfo *AcquireLocaleSplayTree(const char *filename,
162 % ExceptionInfo *exception)
164 % A description of each parameter follows:
166 % o filename: the font file tag.
168 % o locale: the actual locale.
170 % o exception: return any errors or warnings in this structure.
174 static void *DestroyLocaleNode(void *locale_info)
179 p=(LocaleInfo *) locale_info;
180 if (p->path != (char *) NULL)
181 p->path=DestroyString(p->path);
182 if (p->tag != (char *) NULL)
183 p->tag=DestroyString(p->tag);
184 if (p->message != (char *) NULL)
185 p->message=DestroyString(p->message);
186 return(RelinquishMagickMemory(p));
189 static SplayTreeInfo *AcquireLocaleSplayTree(const char *filename,
190 const char *locale,ExceptionInfo *exception)
198 locale_cache=NewSplayTree(CompareSplayTreeString,(void *(*)(void *)) NULL,
200 if (locale_cache == (SplayTreeInfo *) NULL)
201 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
203 #if !defined(MAGICKCORE_ZERO_CONFIGURATION_SUPPORT)
211 options=GetLocaleOptions(filename,exception);
212 option=(const StringInfo *) GetNextValueInLinkedList(options);
213 while (option != (const StringInfo *) NULL)
215 status&=LoadLocaleCache((const char *) GetStringInfoDatum(option),
216 GetStringInfoPath(option),locale,0,exception);
217 option=(const StringInfo *) GetNextValueInLinkedList(options);
219 options=DestroyLocaleOptions(options);
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);
234 if (GetNumberOfNodesInSplayTree(locale_cache) == 0)
235 status&=LoadLocaleCache(LocaleMap,"built-in",locale,0,exception);
236 return(locale_cache);
239 #if defined(MAGICKCORE_HAVE_STRTOD_L)
241 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
245 + D e s t r o y C L o c a l e %
249 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
251 % DestroyCLocale() releases the resources allocated for a locale object
252 % returned by a call to the AcquireCLocale() method.
254 % The format of the DestroyCLocale method is:
256 % void DestroyCLocale(void)
259 static void DestroyCLocale(void)
261 #if defined(MAGICKCORE_HAVE_NEWLOCALE)
262 if (c_locale != (locale_t) NULL)
263 freelocale(c_locale);
264 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
265 if (c_locale != (locale_t) NULL)
266 _free_locale(c_locale);
268 c_locale=(locale_t) NULL;
273 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
277 % D e s t r o y L o c a l e O p t i o n s %
281 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
283 % DestroyLocaleOptions() releases memory associated with an locale
286 % The format of the DestroyProfiles method is:
288 % LinkedListInfo *DestroyLocaleOptions(Image *image)
290 % A description of each parameter follows:
292 % o image: the image.
296 static void *DestroyOptions(void *message)
298 return(DestroyStringInfo((StringInfo *) message));
301 MagickExport LinkedListInfo *DestroyLocaleOptions(LinkedListInfo *messages)
303 assert(messages != (LinkedListInfo *) NULL);
304 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
305 return(DestroyLinkedList(messages,DestroyOptions));
309 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
313 + F o r m a t L o c a l e F i l e %
317 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
319 % FormatLocaleFile() prints formatted output of a variable argument list to a
320 % file in the "C" locale.
322 % The format of the FormatLocaleFile method is:
324 % ssize_t FormatLocaleFile(FILE *file,const char *format,...)
326 % A description of each parameter follows.
330 % o format: A file describing the format to use to write the remaining
335 MagickPrivate ssize_t FormatLocaleFileList(FILE *file,
336 const char *restrict format,va_list operands)
341 #if defined(MAGICKCORE_HAVE_VFPRINTF_L)
346 locale=AcquireCLocale();
347 if (locale == (locale_t) NULL)
348 n=(ssize_t) vfprintf(file,format,operands);
350 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
351 n=(ssize_t) vfprintf_l(file,format,locale,operands);
353 n=(ssize_t) vfprintf_l(file,locale,format,operands);
357 #if defined(MAGICKCORE_HAVE_USELOCALE)
363 locale=AcquireCLocale();
364 if (locale == (locale_t) NULL)
365 n=(ssize_t) vfprintf(file,format,operands);
368 previous_locale=uselocale(locale);
369 n=(ssize_t) vfprintf(file,format,operands);
370 uselocale(previous_locale);
374 n=(ssize_t) vfprintf(file,format,operands);
380 MagickExport ssize_t FormatLocaleFile(FILE *file,const char *restrict format,
389 va_start(operands,format);
390 n=FormatLocaleFileList(file,format,operands);
396 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
400 + F o r m a t L o c a l e S t r i n g %
404 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
406 % FormatLocaleString() prints formatted output of a variable argument list to
407 % a string buffer in the "C" locale.
409 % The format of the FormatLocaleString method is:
411 % ssize_t FormatLocaleString(char *string,const size_t length,
412 % const char *format,...)
414 % A description of each parameter follows.
416 % o string: FormatLocaleString() returns the formatted string in this
419 % o length: the maximum length of the string.
421 % o format: A string describing the format to use to write the remaining
426 MagickPrivate ssize_t FormatLocaleStringList(char *restrict string,
427 const size_t length,const char *restrict format,va_list operands)
432 #if defined(MAGICKCORE_HAVE_VSNPRINTF_L)
437 locale=AcquireCLocale();
438 if (locale == (locale_t) NULL)
439 n=(ssize_t) vsnprintf(string,length,format,operands);
441 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
442 n=(ssize_t) vsnprintf_l(string,length,format,locale,operands);
444 n=(ssize_t) vsnprintf_l(string,length,locale,format,operands);
447 #elif defined(MAGICKCORE_HAVE_VSNPRINTF)
448 #if defined(MAGICKCORE_HAVE_USELOCALE)
454 locale=AcquireCLocale();
455 if (locale == (locale_t) NULL)
456 n=(ssize_t) vsnprintf(string,length,format,operands);
459 previous_locale=uselocale(locale);
460 n=(ssize_t) vsnprintf(string,length,format,operands);
461 uselocale(previous_locale);
465 n=(ssize_t) vsnprintf(string,length,format,operands);
468 n=(ssize_t) vsprintf(string,format,operands);
471 string[length-1]='\0';
475 MagickExport ssize_t FormatLocaleString(char *restrict string,
476 const size_t length,const char *restrict format,...)
484 va_start(operands,format);
485 n=FormatLocaleStringList(string,length,format,operands);
491 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
495 + G e t L o c a l e I n f o _ %
499 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
501 % GetLocaleInfo_() searches the locale list for the specified tag and if
502 % found returns attributes for that element.
504 % The format of the GetLocaleInfo method is:
506 % const LocaleInfo *GetLocaleInfo_(const char *tag,
507 % ExceptionInfo *exception)
509 % A description of each parameter follows:
511 % o tag: the locale tag.
513 % o exception: return any errors or warnings in this structure.
516 MagickExport const LocaleInfo *GetLocaleInfo_(const char *tag,
517 ExceptionInfo *exception)
522 assert(exception != (ExceptionInfo *) NULL);
523 if (IsLocaleTreeInstantiated(exception) == MagickFalse)
524 return((const LocaleInfo *) NULL);
525 LockSemaphoreInfo(locale_semaphore);
526 if ((tag == (const char *) NULL) || (LocaleCompare(tag,"*") == 0))
528 ResetSplayTreeIterator(locale_cache);
529 locale_info=(const LocaleInfo *) GetNextValueInSplayTree(locale_cache);
530 UnlockSemaphoreInfo(locale_semaphore);
533 locale_info=(const LocaleInfo *) GetValueFromSplayTree(locale_cache,tag);
534 UnlockSemaphoreInfo(locale_semaphore);
539 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
543 % G e t L o c a l e I n f o L i s t %
547 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
549 % GetLocaleInfoList() returns any locale messages that match the
552 % The format of the GetLocaleInfoList function is:
554 % const LocaleInfo **GetLocaleInfoList(const char *pattern,
555 % size_t *number_messages,ExceptionInfo *exception)
557 % A description of each parameter follows:
559 % o pattern: Specifies a pointer to a text string containing a pattern.
561 % o number_messages: This integer returns the number of locale messages in
564 % o exception: return any errors or warnings in this structure.
568 #if defined(__cplusplus) || defined(c_plusplus)
572 static int LocaleInfoCompare(const void *x,const void *y)
578 p=(const LocaleInfo **) x,
579 q=(const LocaleInfo **) y;
580 if (LocaleCompare((*p)->path,(*q)->path) == 0)
581 return(LocaleCompare((*p)->tag,(*q)->tag));
582 return(LocaleCompare((*p)->path,(*q)->path));
585 #if defined(__cplusplus) || defined(c_plusplus)
589 MagickExport const LocaleInfo **GetLocaleInfoList(const char *pattern,
590 size_t *number_messages,ExceptionInfo *exception)
595 register const LocaleInfo
602 Allocate locale list.
604 assert(pattern != (char *) NULL);
605 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
606 assert(number_messages != (size_t *) NULL);
608 p=GetLocaleInfo_("*",exception);
609 if (p == (const LocaleInfo *) NULL)
610 return((const LocaleInfo **) NULL);
611 messages=(const LocaleInfo **) AcquireQuantumMemory((size_t)
612 GetNumberOfNodesInSplayTree(locale_cache)+1UL,sizeof(*messages));
613 if (messages == (const LocaleInfo **) NULL)
614 return((const LocaleInfo **) NULL);
616 Generate locale list.
618 LockSemaphoreInfo(locale_semaphore);
619 ResetSplayTreeIterator(locale_cache);
620 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_cache);
621 for (i=0; p != (const LocaleInfo *) NULL; )
623 if ((p->stealth == MagickFalse) &&
624 (GlobExpression(p->tag,pattern,MagickTrue) != MagickFalse))
626 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_cache);
628 UnlockSemaphoreInfo(locale_semaphore);
629 qsort((void *) messages,(size_t) i,sizeof(*messages),LocaleInfoCompare);
630 messages[i]=(LocaleInfo *) NULL;
631 *number_messages=(size_t) i;
636 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
640 % G e t L o c a l e L i s t %
644 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
646 % GetLocaleList() returns any locale messages that match the specified
649 % The format of the GetLocaleList function is:
651 % char **GetLocaleList(const char *pattern,size_t *number_messages,
652 % Exceptioninfo *exception)
654 % A description of each parameter follows:
656 % o pattern: Specifies a pointer to a text string containing a pattern.
658 % o number_messages: This integer returns the number of messages in the
661 % o exception: return any errors or warnings in this structure.
665 #if defined(__cplusplus) || defined(c_plusplus)
669 static int LocaleTagCompare(const void *x,const void *y)
677 return(LocaleCompare(*p,*q));
680 #if defined(__cplusplus) || defined(c_plusplus)
684 MagickExport char **GetLocaleList(const char *pattern,
685 size_t *number_messages,ExceptionInfo *exception)
690 register const LocaleInfo
697 Allocate locale list.
699 assert(pattern != (char *) NULL);
700 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
701 assert(number_messages != (size_t *) NULL);
703 p=GetLocaleInfo_("*",exception);
704 if (p == (const LocaleInfo *) NULL)
705 return((char **) NULL);
706 messages=(char **) AcquireQuantumMemory((size_t)
707 GetNumberOfNodesInSplayTree(locale_cache)+1UL,sizeof(*messages));
708 if (messages == (char **) NULL)
709 return((char **) NULL);
710 LockSemaphoreInfo(locale_semaphore);
711 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_cache);
712 for (i=0; p != (const LocaleInfo *) NULL; )
714 if ((p->stealth == MagickFalse) &&
715 (GlobExpression(p->tag,pattern,MagickTrue) != MagickFalse))
716 messages[i++]=ConstantString(p->tag);
717 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_cache);
719 UnlockSemaphoreInfo(locale_semaphore);
720 qsort((void *) messages,(size_t) i,sizeof(*messages),LocaleTagCompare);
721 messages[i]=(char *) NULL;
722 *number_messages=(size_t) i;
727 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
731 % G e t L o c a l e M e s s a g e %
735 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
737 % GetLocaleMessage() returns a message in the current locale that matches the
740 % The format of the GetLocaleMessage method is:
742 % const char *GetLocaleMessage(const char *tag)
744 % A description of each parameter follows:
746 % o tag: Return a message that matches this tag in the current locale.
749 MagickExport const char *GetLocaleMessage(const char *tag)
760 if ((tag == (const char *) NULL) || (*tag == '\0'))
762 exception=AcquireExceptionInfo();
763 (void) FormatLocaleString(name,MaxTextExtent,"%s/",tag);
764 locale_info=GetLocaleInfo_(name,exception);
765 exception=DestroyExceptionInfo(exception);
766 if (locale_info != (const LocaleInfo *) NULL)
767 return(locale_info->message);
772 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
776 % G e t L o c a l e O p t i o n s %
780 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
782 % GetLocaleOptions() returns any Magick configuration messages associated
783 % with the specified filename.
785 % The format of the GetLocaleOptions method is:
787 % LinkedListInfo *GetLocaleOptions(const char *filename,
788 % ExceptionInfo *exception)
790 % A description of each parameter follows:
792 % o filename: the locale file tag.
794 % o exception: return any errors or warnings in this structure.
797 MagickExport LinkedListInfo *GetLocaleOptions(const char *filename,
798 ExceptionInfo *exception)
813 assert(filename != (const char *) NULL);
814 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
815 assert(exception != (ExceptionInfo *) NULL);
816 (void) CopyMagickString(path,filename,MaxTextExtent);
818 Load XML from configuration files to linked-list.
820 messages=NewLinkedList(0);
821 paths=GetConfigurePaths(filename,exception);
822 if (paths != (LinkedListInfo *) NULL)
824 ResetLinkedListIterator(paths);
825 element=(const char *) GetNextValueInLinkedList(paths);
826 while (element != (const char *) NULL)
828 (void) FormatLocaleString(path,MaxTextExtent,"%s%s",element,filename);
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,(unsigned char *) 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 == MagickSignature);
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_semaphore == (SemaphoreInfo *) NULL)
914 ActivateSemaphoreInfo(&locale_semaphore);
915 LockSemaphoreInfo(locale_semaphore);
916 if (locale_cache == (SplayTreeInfo *) NULL)
924 locale=(char *) NULL;
925 p=setlocale(LC_CTYPE,(const char *) NULL);
926 if (p != (const char *) NULL)
927 locale=ConstantString(p);
928 if (locale == (char *) NULL)
929 locale=GetEnvironmentValue("LC_ALL");
930 if (locale == (char *) NULL)
931 locale=GetEnvironmentValue("LC_MESSAGES");
932 if (locale == (char *) NULL)
933 locale=GetEnvironmentValue("LC_CTYPE");
934 if (locale == (char *) NULL)
935 locale=GetEnvironmentValue("LANG");
936 if (locale == (char *) NULL)
937 locale=ConstantString("C");
938 locale_cache=AcquireLocaleSplayTree(LocaleFilename,locale,exception);
939 locale=DestroyString(locale);
941 UnlockSemaphoreInfo(locale_semaphore);
942 return(locale_cache != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
946 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
950 + I n t e r p r e t L o c a l e V a l u e %
954 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
956 % InterpretLocaleValue() interprets the string as a floating point number in
957 % the "C" locale and returns its value as a double. If sentinal is not a null
958 % pointer, the method also sets the value pointed by sentinal to point to the
959 % first character after the number.
961 % The format of the InterpretLocaleValue method is:
963 % double InterpretLocaleValue(const char *value,char **sentinal)
965 % A description of each parameter follows:
967 % o value: the string value.
969 % o sentinal: if sentinal is not NULL, a pointer to the character after the
970 % last character used in the conversion is stored in the location
971 % referenced by sentinal.
974 MagickExport double InterpretLocaleValue(const char *restrict string,
975 char **restrict sentinal)
983 if ((*string == '0') && ((string[1] | 0x20)=='x'))
984 value=(double) strtoul(string,&q,16);
987 #if defined(MAGICKCORE_HAVE_STRTOD_L)
991 locale=AcquireCLocale();
992 if (locale == (locale_t) NULL)
993 value=strtod(string,&q);
995 value=strtod_l(string,&q,locale);
997 value=strtod(string,&q);
1000 if (sentinal != (char **) NULL)
1006 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1010 % L i s t L o c a l e I n f o %
1014 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1016 % ListLocaleInfo() lists the locale info to a file.
1018 % The format of the ListLocaleInfo method is:
1020 % MagickBooleanType ListLocaleInfo(FILE *file,ExceptionInfo *exception)
1022 % A description of each parameter follows.
1024 % o file: An pointer to a FILE.
1026 % o exception: return any errors or warnings in this structure.
1029 MagickExport MagickBooleanType ListLocaleInfo(FILE *file,
1030 ExceptionInfo *exception)
1044 if (file == (const FILE *) NULL)
1047 locale_info=GetLocaleInfoList("*",&number_messages,exception);
1048 if (locale_info == (const LocaleInfo **) NULL)
1049 return(MagickFalse);
1050 path=(const char *) NULL;
1051 for (i=0; i < (ssize_t) number_messages; i++)
1053 if (locale_info[i]->stealth != MagickFalse)
1055 if ((path == (const char *) NULL) ||
1056 (LocaleCompare(path,locale_info[i]->path) != 0))
1058 if (locale_info[i]->path != (char *) NULL)
1059 (void) FormatLocaleFile(file,"\nPath: %s\n\n",locale_info[i]->path);
1060 (void) FormatLocaleFile(file,"Tag/Message\n");
1061 (void) FormatLocaleFile(file,
1062 "-------------------------------------------------"
1063 "------------------------------\n");
1065 path=locale_info[i]->path;
1066 (void) FormatLocaleFile(file,"%s\n",locale_info[i]->tag);
1067 if (locale_info[i]->message != (char *) NULL)
1068 (void) FormatLocaleFile(file," %s",locale_info[i]->message);
1069 (void) FormatLocaleFile(file,"\n");
1071 (void) fflush(file);
1072 locale_info=(const LocaleInfo **)
1073 RelinquishMagickMemory((void *) locale_info);
1078 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1082 + L o a d L o c a l e L i s t %
1086 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1088 % LoadLocaleCache() loads the locale configurations which provides a mapping
1089 % between locale attributes and a locale name.
1091 % The format of the LoadLocaleCache method is:
1093 % MagickBooleanType LoadLocaleCache(const char *xml,const char *filename,
1094 % const size_t depth,ExceptionInfo *exception)
1096 % A description of each parameter follows:
1098 % o xml: The locale list in XML format.
1100 % o filename: The locale list filename.
1102 % o depth: depth of <include /> statements.
1104 % o exception: return any errors or warnings in this structure.
1108 static void ChopLocaleComponents(char *path,const size_t components)
1118 p=path+strlen(path)-1;
1121 for (count=0; (count < (ssize_t) components) && (p > path); p--)
1127 if (count < (ssize_t) components)
1131 static void LocaleFatalErrorHandler(
1132 const ExceptionType magick_unused(severity),
1133 const char *reason,const char *description)
1135 if (reason == (char *) NULL)
1137 (void) FormatLocaleFile(stderr,"%s: %s",GetClientName(),reason);
1138 if (description != (char *) NULL)
1139 (void) FormatLocaleFile(stderr," (%s)",description);
1140 (void) FormatLocaleFile(stderr,".\n");
1141 (void) fflush(stderr);
1145 static inline size_t MagickMin(const size_t x,const size_t y)
1152 static MagickBooleanType LoadLocaleCache(const char *xml,const char *filename,
1153 const char *locale,const size_t depth,ExceptionInfo *exception)
1156 keyword[MaxTextExtent],
1157 message[MaxTextExtent],
1177 Read the locale configure file.
1179 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1180 "Loading locale configure file \"%s\" ...",filename);
1181 if (xml == (const char *) NULL)
1182 return(MagickFalse);
1183 if (locale_cache == (SplayTreeInfo *) NULL)
1185 locale_cache=NewSplayTree(CompareSplayTreeString,(void *(*)(void *)) NULL,
1187 if (locale_cache == (SplayTreeInfo *) NULL)
1188 return(MagickFalse);
1191 locale_info=(LocaleInfo *) NULL;
1195 fatal_handler=SetFatalErrorHandler(LocaleFatalErrorHandler);
1196 token=AcquireString(xml);
1197 for (q=(char *) xml; *q != '\0'; )
1202 GetMagickToken(q,&q,token);
1205 (void) CopyMagickString(keyword,token,MaxTextExtent);
1206 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
1211 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
1213 GetMagickToken(q,&q,token);
1214 while (isspace((int) ((unsigned char) *q)) != 0)
1219 if (LocaleNCompare(keyword,"<!--",4) == 0)
1224 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
1226 GetMagickToken(q,&q,token);
1227 while (isspace((int) ((unsigned char) *q)) != 0)
1232 if (LocaleCompare(keyword,"<include") == 0)
1237 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
1239 (void) CopyMagickString(keyword,token,MaxTextExtent);
1240 GetMagickToken(q,&q,token);
1243 GetMagickToken(q,&q,token);
1244 if (LocaleCompare(keyword,"locale") == 0)
1246 if (LocaleCompare(locale,token) != 0)
1250 if (LocaleCompare(keyword,"file") == 0)
1253 (void) ThrowMagickException(exception,GetMagickModule(),
1254 ConfigureError,"IncludeElementNestedTooDeeply","`%s'",token);
1258 path[MaxTextExtent],
1262 GetPathComponent(filename,HeadPath,path);
1264 (void) ConcatenateMagickString(path,DirectorySeparator,
1266 if (*token == *DirectorySeparator)
1267 (void) CopyMagickString(path,token,MaxTextExtent);
1269 (void) ConcatenateMagickString(path,token,MaxTextExtent);
1270 xml=FileToXML(path,~0UL);
1271 if (xml != (char *) NULL)
1273 status=LoadLocaleCache(xml,path,locale,depth+1,exception);
1274 xml=(char *) RelinquishMagickMemory(xml);
1281 if (LocaleCompare(keyword,"<locale") == 0)
1286 while ((*token != '>') && (*q != '\0'))
1288 (void) CopyMagickString(keyword,token,MaxTextExtent);
1289 GetMagickToken(q,&q,token);
1292 GetMagickToken(q,&q,token);
1296 if (LocaleCompare(keyword,"</locale>") == 0)
1298 ChopLocaleComponents(tag,1);
1299 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1302 if (LocaleCompare(keyword,"<localemap>") == 0)
1304 if (LocaleCompare(keyword,"</localemap>") == 0)
1306 if (LocaleCompare(keyword,"<message") == 0)
1311 while ((*token != '>') && (*q != '\0'))
1313 (void) CopyMagickString(keyword,token,MaxTextExtent);
1314 GetMagickToken(q,&q,token);
1317 GetMagickToken(q,&q,token);
1318 if (LocaleCompare(keyword,"name") == 0)
1320 (void) ConcatenateMagickString(tag,token,MaxTextExtent);
1321 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1324 for (p=(char *) q; (*q != '<') && (*q != '\0'); q++) ;
1325 while (isspace((int) ((unsigned char) *p)) != 0)
1328 while ((isspace((int) ((unsigned char) *q)) != 0) && (q > p))
1330 (void) CopyMagickString(message,p,MagickMin((size_t) (q-p+2),
1332 locale_info=(LocaleInfo *) AcquireMagickMemory(sizeof(*locale_info));
1333 if (locale_info == (LocaleInfo *) NULL)
1334 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1335 (void) ResetMagickMemory(locale_info,0,sizeof(*locale_info));
1336 locale_info->path=ConstantString(filename);
1337 locale_info->tag=ConstantString(tag);
1338 locale_info->message=ConstantString(message);
1339 locale_info->signature=MagickSignature;
1340 status=AddValueToSplayTree(locale_cache,locale_info->tag,locale_info);
1341 if (status == MagickFalse)
1342 (void) ThrowMagickException(exception,GetMagickModule(),
1343 ResourceLimitError,"MemoryAllocationFailed","`%s'",
1345 (void) ConcatenateMagickString(tag,message,MaxTextExtent);
1346 (void) ConcatenateMagickString(tag,"\n",MaxTextExtent);
1350 if (LocaleCompare(keyword,"</message>") == 0)
1352 ChopLocaleComponents(tag,2);
1353 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1356 if (*keyword == '<')
1361 if (*(keyword+1) == '?')
1363 if (*(keyword+1) == '/')
1365 ChopLocaleComponents(tag,1);
1367 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1370 token[strlen(token)-1]='\0';
1371 (void) CopyMagickString(token,token+1,MaxTextExtent);
1372 (void) ConcatenateMagickString(tag,token,MaxTextExtent);
1373 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1376 GetMagickToken(q,(const char **) NULL,token);
1380 token=(char *) RelinquishMagickMemory(token);
1381 (void) SetFatalErrorHandler(fatal_handler);
1386 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1390 + L o c a l e C o m p o n e n t G e n e s i s %
1394 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1396 % LocaleComponentGenesis() instantiates the locale component.
1398 % The format of the LocaleComponentGenesis method is:
1400 % MagickBooleanType LocaleComponentGenesis(void)
1403 MagickPrivate MagickBooleanType LocaleComponentGenesis(void)
1405 locale_semaphore=AcquireSemaphoreInfo();
1410 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1414 + L o c a l e C o m p o n e n t T e r m i n u s %
1418 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1420 % LocaleComponentTerminus() destroys the locale component.
1422 % The format of the LocaleComponentTerminus method is:
1424 % LocaleComponentTerminus(void)
1427 MagickPrivate void LocaleComponentTerminus(void)
1429 if (locale_semaphore == (SemaphoreInfo *) NULL)
1430 ActivateSemaphoreInfo(&locale_semaphore);
1431 LockSemaphoreInfo(locale_semaphore);
1432 if (locale_cache != (SplayTreeInfo *) NULL)
1433 locale_cache=DestroySplayTree(locale_cache);
1434 #if defined(MAGICKCORE_HAVE_STRTOD_L)
1437 UnlockSemaphoreInfo(locale_semaphore);
1438 RelinquishSemaphoreInfo(&locale_semaphore);