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-2012 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/semaphore.h"
54 #include "MagickCore/splay-tree.h"
55 #include "MagickCore/string_.h"
56 #include "MagickCore/string-private.h"
57 #include "MagickCore/token.h"
58 #include "MagickCore/utility.h"
59 #include "MagickCore/utility-private.h"
60 #include "MagickCore/xml-tree.h"
65 #define LocaleFilename "locale.xml"
66 #define MaxRecursionDepth 200
71 #if defined(__CYGWIN__)
72 typedef struct _locale_t *locale_t;
80 "<?xml version=\"1.0\"?>"
82 " <locale name=\"C\">"
84 " <Message name=\"\">"
91 *locale_semaphore = (SemaphoreInfo *) NULL;
94 *locale_list = (SplayTreeInfo *) NULL;
96 #if defined(MAGICKCORE_HAVE_STRTOD_L)
97 static volatile locale_t
98 c_locale = (locale_t) NULL;
101 static volatile MagickBooleanType
102 instantiate_locale = MagickFalse;
105 Forward declarations.
107 static MagickBooleanType
108 InitializeLocaleList(ExceptionInfo *),
109 LoadLocaleLists(const char *,const char *,ExceptionInfo *);
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");
144 #if defined(MAGICKCORE_HAVE_STRTOD_L)
146 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
150 + D e s t r o y C L o c a l e %
154 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
156 % DestroyCLocale() releases the resources allocated for a locale object
157 % returned by a call to the AcquireCLocale() method.
159 % The format of the DestroyCLocale method is:
161 % void DestroyCLocale(void)
164 static void DestroyCLocale(void)
166 #if defined(MAGICKCORE_HAVE_NEWLOCALE)
167 if (c_locale != (locale_t) NULL)
168 freelocale(c_locale);
169 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
170 if (c_locale != (locale_t) NULL)
171 _free_locale(c_locale);
173 c_locale=(locale_t) NULL;
178 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
182 % D e s t r o y L o c a l e O p t i o n s %
186 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
188 % DestroyLocaleOptions() releases memory associated with an locale
191 % The format of the DestroyProfiles method is:
193 % LinkedListInfo *DestroyLocaleOptions(Image *image)
195 % A description of each parameter follows:
197 % o image: the image.
201 static void *DestroyOptions(void *message)
203 return(DestroyStringInfo((StringInfo *) message));
206 MagickExport LinkedListInfo *DestroyLocaleOptions(LinkedListInfo *messages)
208 assert(messages != (LinkedListInfo *) NULL);
209 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
210 return(DestroyLinkedList(messages,DestroyOptions));
214 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
218 + F o r m a t L o c a l e F i l e %
222 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
224 % FormatLocaleFile() prints formatted output of a variable argument list to a
225 % file in the "C" locale.
227 % The format of the FormatLocaleFile method is:
229 % ssize_t FormatLocaleFile(FILE *file,const char *format,...)
231 % A description of each parameter follows.
235 % o format: A file describing the format to use to write the remaining
240 MagickPrivate ssize_t FormatLocaleFileList(FILE *file,
241 const char *restrict format,va_list operands)
246 #if defined(MAGICKCORE_HAVE_VFPRINTF_L)
251 locale=AcquireCLocale();
252 if (locale == (locale_t) NULL)
253 n=(ssize_t) vfprintf(file,format,operands);
255 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
256 n=(ssize_t) vfprintf_l(file,format,locale,operands);
258 n=(ssize_t) vfprintf_l(file,locale,format,operands);
262 #if defined(MAGICKCORE_HAVE_USELOCALE)
268 locale=AcquireCLocale();
269 if (locale == (locale_t) NULL)
270 n=(ssize_t) vfprintf(file,format,operands);
273 previous_locale=uselocale(locale);
274 n=(ssize_t) vfprintf(file,format,operands);
275 uselocale(previous_locale);
279 n=(ssize_t) vfprintf(file,format,operands);
285 MagickExport ssize_t FormatLocaleFile(FILE *file,const char *restrict format,
294 va_start(operands,format);
295 n=FormatLocaleFileList(file,format,operands);
301 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
305 + F o r m a t L o c a l e S t r i n g %
309 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
311 % FormatLocaleString() prints formatted output of a variable argument list to
312 % a string buffer in the "C" locale.
314 % The format of the FormatLocaleString method is:
316 % ssize_t FormatLocaleString(char *string,const size_t length,
317 % const char *format,...)
319 % A description of each parameter follows.
321 % o string: FormatLocaleString() returns the formatted string in this
324 % o length: the maximum length of the string.
326 % o format: A string describing the format to use to write the remaining
331 MagickPrivate ssize_t FormatLocaleStringList(char *restrict string,
332 const size_t length,const char *restrict format,va_list operands)
337 #if defined(MAGICKCORE_HAVE_VSNPRINTF_L)
342 locale=AcquireCLocale();
343 if (locale == (locale_t) NULL)
344 n=(ssize_t) vsnprintf(string,length,format,operands);
346 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
347 n=(ssize_t) vsnprintf_l(string,length,format,locale,operands);
349 n=(ssize_t) vsnprintf_l(string,length,locale,format,operands);
352 #elif defined(MAGICKCORE_HAVE_VSNPRINTF)
353 #if defined(MAGICKCORE_HAVE_USELOCALE)
359 locale=AcquireCLocale();
360 if (locale == (locale_t) NULL)
361 n=(ssize_t) vsnprintf(string,length,format,operands);
364 previous_locale=uselocale(locale);
365 n=(ssize_t) vsnprintf(string,length,format,operands);
366 uselocale(previous_locale);
370 n=(ssize_t) vsnprintf(string,length,format,operands);
373 n=(ssize_t) vsprintf(string,format,operands);
376 string[length-1]='\0';
380 MagickExport ssize_t FormatLocaleString(char *restrict string,
381 const size_t length,const char *restrict format,...)
389 va_start(operands,format);
390 n=FormatLocaleStringList(string,length,format,operands);
396 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
400 + G e t L o c a l e I n f o _ %
404 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
406 % GetLocaleInfo_() searches the locale list for the specified tag and if
407 % found returns attributes for that element.
409 % The format of the GetLocaleInfo method is:
411 % const LocaleInfo *GetLocaleInfo_(const char *tag,
412 % ExceptionInfo *exception)
414 % A description of each parameter follows:
416 % o tag: the locale tag.
418 % o exception: return any errors or warnings in this structure.
421 MagickExport const LocaleInfo *GetLocaleInfo_(const char *tag,
422 ExceptionInfo *exception)
424 assert(exception != (ExceptionInfo *) NULL);
425 if ((locale_list == (SplayTreeInfo *) NULL) ||
426 (instantiate_locale == MagickFalse))
427 if (InitializeLocaleList(exception) == MagickFalse)
428 return((const LocaleInfo *) NULL);
429 if ((locale_list == (SplayTreeInfo *) NULL) ||
430 (GetNumberOfNodesInSplayTree(locale_list) == 0))
431 return((const LocaleInfo *) NULL);
432 if ((tag == (const char *) NULL) || (LocaleCompare(tag,"*") == 0))
434 ResetSplayTreeIterator(locale_list);
435 return((const LocaleInfo *) GetNextValueInSplayTree(locale_list));
437 return((const LocaleInfo *) GetValueFromSplayTree(locale_list,tag));
441 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
445 % G e t L o c a l e I n f o L i s t %
449 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
451 % GetLocaleInfoList() returns any locale messages that match the
454 % The format of the GetLocaleInfoList function is:
456 % const LocaleInfo **GetLocaleInfoList(const char *pattern,
457 % size_t *number_messages,ExceptionInfo *exception)
459 % A description of each parameter follows:
461 % o pattern: Specifies a pointer to a text string containing a pattern.
463 % o number_messages: This integer returns the number of locale messages in
466 % o exception: return any errors or warnings in this structure.
470 #if defined(__cplusplus) || defined(c_plusplus)
474 static int LocaleInfoCompare(const void *x,const void *y)
480 p=(const LocaleInfo **) x,
481 q=(const LocaleInfo **) y;
482 if (LocaleCompare((*p)->path,(*q)->path) == 0)
483 return(LocaleCompare((*p)->tag,(*q)->tag));
484 return(LocaleCompare((*p)->path,(*q)->path));
487 #if defined(__cplusplus) || defined(c_plusplus)
491 MagickExport const LocaleInfo **GetLocaleInfoList(const char *pattern,
492 size_t *number_messages,ExceptionInfo *exception)
497 register const LocaleInfo
504 Allocate locale list.
506 assert(pattern != (char *) NULL);
507 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
508 assert(number_messages != (size_t *) NULL);
510 p=GetLocaleInfo_("*",exception);
511 if (p == (const LocaleInfo *) NULL)
512 return((const LocaleInfo **) NULL);
513 messages=(const LocaleInfo **) AcquireQuantumMemory((size_t)
514 GetNumberOfNodesInSplayTree(locale_list)+1UL,sizeof(*messages));
515 if (messages == (const LocaleInfo **) NULL)
516 return((const LocaleInfo **) NULL);
518 Generate locale list.
520 LockSemaphoreInfo(locale_semaphore);
521 ResetSplayTreeIterator(locale_list);
522 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_list);
523 for (i=0; p != (const LocaleInfo *) NULL; )
525 if ((p->stealth == MagickFalse) &&
526 (GlobExpression(p->tag,pattern,MagickTrue) != MagickFalse))
528 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_list);
530 UnlockSemaphoreInfo(locale_semaphore);
531 qsort((void *) messages,(size_t) i,sizeof(*messages),LocaleInfoCompare);
532 messages[i]=(LocaleInfo *) NULL;
533 *number_messages=(size_t) i;
538 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
542 % G e t L o c a l e L i s t %
546 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
548 % GetLocaleList() returns any locale messages that match the specified
551 % The format of the GetLocaleList function is:
553 % char **GetLocaleList(const char *pattern,size_t *number_messages,
554 % 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 messages in the
563 % o exception: return any errors or warnings in this structure.
567 #if defined(__cplusplus) || defined(c_plusplus)
571 static int LocaleTagCompare(const void *x,const void *y)
579 return(LocaleCompare(*p,*q));
582 #if defined(__cplusplus) || defined(c_plusplus)
586 MagickExport char **GetLocaleList(const char *pattern,
587 size_t *number_messages,ExceptionInfo *exception)
592 register const LocaleInfo
599 Allocate locale list.
601 assert(pattern != (char *) NULL);
602 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
603 assert(number_messages != (size_t *) NULL);
605 p=GetLocaleInfo_("*",exception);
606 if (p == (const LocaleInfo *) NULL)
607 return((char **) NULL);
608 messages=(char **) AcquireQuantumMemory((size_t)
609 GetNumberOfNodesInSplayTree(locale_list)+1UL,sizeof(*messages));
610 if (messages == (char **) NULL)
611 return((char **) NULL);
612 LockSemaphoreInfo(locale_semaphore);
613 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_list);
614 for (i=0; p != (const LocaleInfo *) NULL; )
616 if ((p->stealth == MagickFalse) &&
617 (GlobExpression(p->tag,pattern,MagickTrue) != MagickFalse))
618 messages[i++]=ConstantString(p->tag);
619 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_list);
621 UnlockSemaphoreInfo(locale_semaphore);
622 qsort((void *) messages,(size_t) i,sizeof(*messages),LocaleTagCompare);
623 messages[i]=(char *) NULL;
624 *number_messages=(size_t) i;
629 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
633 % G e t L o c a l e M e s s a g e %
637 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
639 % GetLocaleMessage() returns a message in the current locale that matches the
642 % The format of the GetLocaleMessage method is:
644 % const char *GetLocaleMessage(const char *tag)
646 % A description of each parameter follows:
648 % o tag: Return a message that matches this tag in the current locale.
651 MagickExport const char *GetLocaleMessage(const char *tag)
662 if ((tag == (const char *) NULL) || (*tag == '\0'))
664 exception=AcquireExceptionInfo();
665 (void) FormatLocaleString(name,MaxTextExtent,"%s/",tag);
666 locale_info=GetLocaleInfo_(name,exception);
667 exception=DestroyExceptionInfo(exception);
668 if (locale_info != (const LocaleInfo *) NULL)
669 return(locale_info->message);
674 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
678 % G e t L o c a l e O p t i o n s %
682 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
684 % GetLocaleOptions() returns any Magick configuration messages associated
685 % with the specified filename.
687 % The format of the GetLocaleOptions method is:
689 % LinkedListInfo *GetLocaleOptions(const char *filename,
690 % ExceptionInfo *exception)
692 % A description of each parameter follows:
694 % o filename: the locale file tag.
696 % o exception: return any errors or warnings in this structure.
699 MagickExport LinkedListInfo *GetLocaleOptions(const char *filename,
700 ExceptionInfo *exception)
715 assert(filename != (const char *) NULL);
716 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
717 assert(exception != (ExceptionInfo *) NULL);
718 (void) CopyMagickString(path,filename,MaxTextExtent);
720 Load XML from configuration files to linked-list.
722 messages=NewLinkedList(0);
723 paths=GetConfigurePaths(filename,exception);
724 if (paths != (LinkedListInfo *) NULL)
726 ResetLinkedListIterator(paths);
727 element=(const char *) GetNextValueInLinkedList(paths);
728 while (element != (const char *) NULL)
730 (void) FormatLocaleString(path,MaxTextExtent,"%s%s",element,filename);
731 (void) LogMagickEvent(LocaleEvent,GetMagickModule(),
732 "Searching for locale file: \"%s\"",path);
733 xml=ConfigureFileToStringInfo(path);
734 if (xml != (StringInfo *) NULL)
735 (void) AppendValueToLinkedList(messages,xml);
736 element=(const char *) GetNextValueInLinkedList(paths);
738 paths=DestroyLinkedList(paths,RelinquishMagickMemory);
740 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
745 blob=(char *) NTResourceToBlob(filename);
746 if (blob != (char *) NULL)
748 xml=AcquireStringInfo(0);
749 SetStringInfoLength(xml,strlen(blob)+1);
750 SetStringInfoDatum(xml,(unsigned char *) blob);
751 SetStringInfoPath(xml,filename);
752 (void) AppendValueToLinkedList(messages,xml);
756 ResetLinkedListIterator(messages);
761 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
765 % G e t L o c a l e V a l u e %
769 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
771 % GetLocaleValue() returns the message associated with the locale info.
773 % The format of the GetLocaleValue method is:
775 % const char *GetLocaleValue(const LocaleInfo *locale_info)
777 % A description of each parameter follows:
779 % o locale_info: The locale info.
782 MagickExport const char *GetLocaleValue(const LocaleInfo *locale_info)
784 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
785 assert(locale_info != (LocaleInfo *) NULL);
786 assert(locale_info->signature == MagickSignature);
787 return(locale_info->message);
791 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
795 + I n i t i a l i z e L o c a l e L i s t %
799 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
801 % InitializeLocaleList() initializes the locale list.
803 % The format of the InitializeLocaleList method is:
805 % MagickBooleanType InitializeLocaleList(ExceptionInfo *exception)
807 % A description of each parameter follows.
809 % o exception: return any errors or warnings in this structure.
812 static MagickBooleanType InitializeLocaleList(ExceptionInfo *exception)
814 if ((locale_list == (SplayTreeInfo *) NULL) &&
815 (instantiate_locale == MagickFalse))
817 if (locale_semaphore == (SemaphoreInfo *) NULL)
818 AcquireSemaphoreInfo(&locale_semaphore);
819 LockSemaphoreInfo(locale_semaphore);
820 if ((locale_list == (SplayTreeInfo *) NULL) &&
821 (instantiate_locale == MagickFalse))
829 locale=(char *) NULL;
830 p=setlocale(LC_CTYPE,(const char *) NULL);
831 if (p != (const char *) NULL)
832 locale=ConstantString(p);
833 if (locale == (char *) NULL)
834 locale=GetEnvironmentValue("LC_ALL");
835 if (locale == (char *) NULL)
836 locale=GetEnvironmentValue("LC_MESSAGES");
837 if (locale == (char *) NULL)
838 locale=GetEnvironmentValue("LC_CTYPE");
839 if (locale == (char *) NULL)
840 locale=GetEnvironmentValue("LANG");
841 if (locale == (char *) NULL)
842 locale=ConstantString("C");
843 (void) LoadLocaleLists(LocaleFilename,locale,exception);
844 locale=DestroyString(locale);
845 instantiate_locale=MagickTrue;
847 UnlockSemaphoreInfo(locale_semaphore);
849 return(locale_list != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
853 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
857 + I n t e r p r e t L o c a l e V a l u e %
861 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
863 % InterpretLocaleValue() interprets the string as a floating point number in
864 % the "C" locale and returns its value as a double. If sentinal is not a null
865 % pointer, the method also sets the value pointed by sentinal to point to the
866 % first character after the number.
868 % The format of the InterpretLocaleValue method is:
870 % double InterpretLocaleValue(const char *value,char **sentinal)
872 % A description of each parameter follows:
874 % o value: the string value.
876 % o sentinal: if sentinal is not NULL, a pointer to the character after the
877 % last character used in the conversion is stored in the location
878 % referenced by sentinal.
881 MagickExport double InterpretLocaleValue(const char *restrict string,
882 char **restrict sentinal)
890 if ((*string == '0') && ((string[1] | 0x20)=='x'))
891 value=(double) strtoul(string,&q,16);
894 #if defined(MAGICKCORE_HAVE_STRTOD_L)
898 locale=AcquireCLocale();
899 if (locale == (locale_t) NULL)
900 value=strtod(string,&q);
902 value=strtod_l(string,&q,locale);
904 value=strtod(string,&q);
907 if (sentinal != (char **) NULL)
913 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
917 % L i s t L o c a l e I n f o %
921 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
923 % ListLocaleInfo() lists the locale info to a file.
925 % The format of the ListLocaleInfo method is:
927 % MagickBooleanType ListLocaleInfo(FILE *file,ExceptionInfo *exception)
929 % A description of each parameter follows.
931 % o file: An pointer to a FILE.
933 % o exception: return any errors or warnings in this structure.
936 MagickExport MagickBooleanType ListLocaleInfo(FILE *file,
937 ExceptionInfo *exception)
951 if (file == (const FILE *) NULL)
954 locale_info=GetLocaleInfoList("*",&number_messages,exception);
955 if (locale_info == (const LocaleInfo **) NULL)
957 path=(const char *) NULL;
958 for (i=0; i < (ssize_t) number_messages; i++)
960 if (locale_info[i]->stealth != MagickFalse)
962 if ((path == (const char *) NULL) ||
963 (LocaleCompare(path,locale_info[i]->path) != 0))
965 if (locale_info[i]->path != (char *) NULL)
966 (void) FormatLocaleFile(file,"\nPath: %s\n\n",locale_info[i]->path);
967 (void) FormatLocaleFile(file,"Tag/Message\n");
968 (void) FormatLocaleFile(file,
969 "-------------------------------------------------"
970 "------------------------------\n");
972 path=locale_info[i]->path;
973 (void) FormatLocaleFile(file,"%s\n",locale_info[i]->tag);
974 if (locale_info[i]->message != (char *) NULL)
975 (void) FormatLocaleFile(file," %s",locale_info[i]->message);
976 (void) FormatLocaleFile(file,"\n");
979 locale_info=(const LocaleInfo **)
980 RelinquishMagickMemory((void *) locale_info);
985 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
989 + L o a d L o c a l e L i s t %
993 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
995 % LoadLocaleList() loads the locale configuration file which provides a mapping
996 % between locale attributes and a locale name.
998 % The format of the LoadLocaleList method is:
1000 % MagickBooleanType LoadLocaleList(const char *xml,const char *filename,
1001 % const size_t depth,ExceptionInfo *exception)
1003 % A description of each parameter follows:
1005 % o xml: The locale list in XML format.
1007 % o filename: The locale list filename.
1009 % o depth: depth of <include /> statements.
1011 % o exception: return any errors or warnings in this structure.
1015 static void ChopLocaleComponents(char *path,const size_t components)
1025 p=path+strlen(path)-1;
1028 for (count=0; (count < (ssize_t) components) && (p > path); p--)
1034 if (count < (ssize_t) components)
1038 static void *DestroyLocaleNode(void *locale_info)
1043 p=(LocaleInfo *) locale_info;
1044 if (p->path != (char *) NULL)
1045 p->path=DestroyString(p->path);
1046 if (p->tag != (char *) NULL)
1047 p->tag=DestroyString(p->tag);
1048 if (p->message != (char *) NULL)
1049 p->message=DestroyString(p->message);
1050 return(RelinquishMagickMemory(p));
1053 static void LocaleFatalErrorHandler(
1054 const ExceptionType magick_unused(severity),
1055 const char *reason,const char *description)
1057 if (reason == (char *) NULL)
1059 (void) FormatLocaleFile(stderr,"%s: %s",GetClientName(),reason);
1060 if (description != (char *) NULL)
1061 (void) FormatLocaleFile(stderr," (%s)",description);
1062 (void) FormatLocaleFile(stderr,".\n");
1063 (void) fflush(stderr);
1068 static MagickBooleanType LoadLocaleList(const char *xml,const char *filename,
1069 const char *locale,const size_t depth,ExceptionInfo *exception)
1072 keyword[MaxTextExtent],
1073 message[MaxTextExtent],
1093 Read the locale configure file.
1095 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1096 "Loading locale configure file \"%s\" ...",filename);
1097 if (xml == (const char *) NULL)
1098 return(MagickFalse);
1099 if (locale_list == (SplayTreeInfo *) NULL)
1101 locale_list=NewSplayTree(CompareSplayTreeString,(void *(*)(void *)) NULL,
1103 if (locale_list == (SplayTreeInfo *) NULL)
1104 return(MagickFalse);
1107 locale_info=(LocaleInfo *) NULL;
1111 fatal_handler=SetFatalErrorHandler(LocaleFatalErrorHandler);
1112 token=AcquireString(xml);
1113 for (q=(char *) xml; *q != '\0'; )
1118 GetMagickToken(q,&q,token);
1121 (void) CopyMagickString(keyword,token,MaxTextExtent);
1122 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
1127 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
1129 GetMagickToken(q,&q,token);
1130 while (isspace((int) ((unsigned char) *q)) != 0)
1135 if (LocaleNCompare(keyword,"<!--",4) == 0)
1140 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
1142 GetMagickToken(q,&q,token);
1143 while (isspace((int) ((unsigned char) *q)) != 0)
1148 if (LocaleCompare(keyword,"<include") == 0)
1153 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
1155 (void) CopyMagickString(keyword,token,MaxTextExtent);
1156 GetMagickToken(q,&q,token);
1159 GetMagickToken(q,&q,token);
1160 if (LocaleCompare(keyword,"locale") == 0)
1162 if (LocaleCompare(locale,token) != 0)
1166 if (LocaleCompare(keyword,"file") == 0)
1169 (void) ThrowMagickException(exception,GetMagickModule(),
1170 ConfigureError,"IncludeElementNestedTooDeeply","`%s'",token);
1174 path[MaxTextExtent],
1178 GetPathComponent(filename,HeadPath,path);
1180 (void) ConcatenateMagickString(path,DirectorySeparator,
1182 if (*token == *DirectorySeparator)
1183 (void) CopyMagickString(path,token,MaxTextExtent);
1185 (void) ConcatenateMagickString(path,token,MaxTextExtent);
1186 xml=FileToString(path,~0,exception);
1187 if (xml != (char *) NULL)
1189 status=LoadLocaleList(xml,path,locale,depth+1,exception);
1190 xml=(char *) RelinquishMagickMemory(xml);
1197 if (LocaleCompare(keyword,"<locale") == 0)
1202 while ((*token != '>') && (*q != '\0'))
1204 (void) CopyMagickString(keyword,token,MaxTextExtent);
1205 GetMagickToken(q,&q,token);
1208 GetMagickToken(q,&q,token);
1212 if (LocaleCompare(keyword,"</locale>") == 0)
1214 ChopLocaleComponents(tag,1);
1215 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1218 if (LocaleCompare(keyword,"<localemap>") == 0)
1220 if (LocaleCompare(keyword,"</localemap>") == 0)
1222 if (LocaleCompare(keyword,"<message") == 0)
1227 while ((*token != '>') && (*q != '\0'))
1229 (void) CopyMagickString(keyword,token,MaxTextExtent);
1230 GetMagickToken(q,&q,token);
1233 GetMagickToken(q,&q,token);
1234 if (LocaleCompare(keyword,"name") == 0)
1236 (void) ConcatenateMagickString(tag,token,MaxTextExtent);
1237 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1240 for (p=(char *) q; (*q != '<') && (*q != '\0'); q++) ;
1241 while (isspace((int) ((unsigned char) *p)) != 0)
1244 while ((isspace((int) ((unsigned char) *q)) != 0) && (q > p))
1246 (void) CopyMagickString(message,p,(size_t) (q-p+2));
1247 locale_info=(LocaleInfo *) AcquireMagickMemory(sizeof(*locale_info));
1248 if (locale_info == (LocaleInfo *) NULL)
1249 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1250 (void) ResetMagickMemory(locale_info,0,sizeof(*locale_info));
1251 locale_info->path=ConstantString(filename);
1252 locale_info->tag=ConstantString(tag);
1253 locale_info->message=ConstantString(message);
1254 locale_info->signature=MagickSignature;
1255 status=AddValueToSplayTree(locale_list,locale_info->tag,locale_info);
1256 if (status == MagickFalse)
1257 (void) ThrowMagickException(exception,GetMagickModule(),
1258 ResourceLimitError,"MemoryAllocationFailed","`%s'",
1260 (void) ConcatenateMagickString(tag,message,MaxTextExtent);
1261 (void) ConcatenateMagickString(tag,"\n",MaxTextExtent);
1265 if (LocaleCompare(keyword,"</message>") == 0)
1267 ChopLocaleComponents(tag,2);
1268 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1271 if (*keyword == '<')
1276 if (*(keyword+1) == '?')
1278 if (*(keyword+1) == '/')
1280 ChopLocaleComponents(tag,1);
1282 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1285 token[strlen(token)-1]='\0';
1286 (void) CopyMagickString(token,token+1,MaxTextExtent);
1287 (void) ConcatenateMagickString(tag,token,MaxTextExtent);
1288 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1291 GetMagickToken(q,(const char **) NULL,token);
1295 token=(char *) RelinquishMagickMemory(token);
1296 (void) SetFatalErrorHandler(fatal_handler);
1301 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1305 % L o a d L o c a l e L i s t s %
1309 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1311 % LoadLocaleList() loads one or more locale configuration file which
1312 % provides a mapping between locale attributes and a locale tag.
1314 % The format of the LoadLocaleLists method is:
1316 % MagickBooleanType LoadLocaleLists(const char *filename,
1317 % ExceptionInfo *exception)
1319 % A description of each parameter follows:
1321 % o filename: the font file tag.
1323 % o locale: the actual locale.
1325 % o exception: return any errors or warnings in this structure.
1328 static MagickBooleanType LoadLocaleLists(const char *filename,
1329 const char *locale,ExceptionInfo *exception)
1331 #if defined(MAGICKCORE_ZERO_CONFIGURATION_SUPPORT)
1332 return(LoadLocaleList(LocaleMap,"built-in",locale,0,exception));
1344 options=GetLocaleOptions(filename,exception);
1345 option=(const StringInfo *) GetNextValueInLinkedList(options);
1346 while (option != (const StringInfo *) NULL)
1348 status|=LoadLocaleList((const char *) GetStringInfoDatum(option),
1349 GetStringInfoPath(option),locale,0,exception);
1350 option=(const StringInfo *) GetNextValueInLinkedList(options);
1352 options=DestroyLocaleOptions(options);
1353 if ((locale_list == (SplayTreeInfo *) NULL) ||
1354 (GetNumberOfNodesInSplayTree(locale_list) == 0))
1356 options=GetLocaleOptions("english.xml",exception);
1357 option=(const StringInfo *) GetNextValueInLinkedList(options);
1358 while (option != (const StringInfo *) NULL)
1360 status|=LoadLocaleList((const char *) GetStringInfoDatum(option),
1361 GetStringInfoPath(option),locale,0,exception);
1362 option=(const StringInfo *) GetNextValueInLinkedList(options);
1364 options=DestroyLocaleOptions(options);
1366 if ((locale_list == (SplayTreeInfo *) NULL) ||
1367 (GetNumberOfNodesInSplayTree(locale_list) == 0))
1368 status|=LoadLocaleList(LocaleMap,"built-in",locale,0,exception);
1369 return(status != 0 ? MagickTrue : MagickFalse);
1374 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1378 + L o c a l e C o m p o n e n t G e n e s i s %
1382 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1384 % LocaleComponentGenesis() instantiates the locale component.
1386 % The format of the LocaleComponentGenesis method is:
1388 % MagickBooleanType LocaleComponentGenesis(void)
1391 MagickPrivate MagickBooleanType LocaleComponentGenesis(void)
1393 AcquireSemaphoreInfo(&locale_semaphore);
1398 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1402 + L o c a l e C o m p o n e n t T e r m i n u s %
1406 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1408 % LocaleComponentTerminus() destroys the locale component.
1410 % The format of the LocaleComponentTerminus method is:
1412 % LocaleComponentTerminus(void)
1415 MagickPrivate void LocaleComponentTerminus(void)
1417 if (locale_semaphore == (SemaphoreInfo *) NULL)
1418 AcquireSemaphoreInfo(&locale_semaphore);
1419 LockSemaphoreInfo(locale_semaphore);
1420 #if defined(MAGICKCORE_HAVE_STRTOD_L)
1423 instantiate_locale=MagickFalse;
1424 UnlockSemaphoreInfo(locale_semaphore);
1425 DestroySemaphoreInfo(&locale_semaphore);