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-2011 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=StringToStringInfo(blob);
749 (void) AppendValueToLinkedList(messages,xml);
750 blob=DestroyString(blob);
754 ResetLinkedListIterator(messages);
759 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
763 % G e t L o c a l e V a l u e %
767 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
769 % GetLocaleValue() returns the message associated with the locale info.
771 % The format of the GetLocaleValue method is:
773 % const char *GetLocaleValue(const LocaleInfo *locale_info)
775 % A description of each parameter follows:
777 % o locale_info: The locale info.
780 MagickExport const char *GetLocaleValue(const LocaleInfo *locale_info)
782 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
783 assert(locale_info != (LocaleInfo *) NULL);
784 assert(locale_info->signature == MagickSignature);
785 return(locale_info->message);
789 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
793 + I n i t i a l i z e L o c a l e L i s t %
797 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
799 % InitializeLocaleList() initializes the locale list.
801 % The format of the InitializeLocaleList method is:
803 % MagickBooleanType InitializeLocaleList(ExceptionInfo *exception)
805 % A description of each parameter follows.
807 % o exception: return any errors or warnings in this structure.
810 static MagickBooleanType InitializeLocaleList(ExceptionInfo *exception)
812 if ((locale_list == (SplayTreeInfo *) NULL) &&
813 (instantiate_locale == MagickFalse))
815 if (locale_semaphore == (SemaphoreInfo *) NULL)
816 AcquireSemaphoreInfo(&locale_semaphore);
817 LockSemaphoreInfo(locale_semaphore);
818 if ((locale_list == (SplayTreeInfo *) NULL) &&
819 (instantiate_locale == MagickFalse))
827 locale=(char *) NULL;
828 p=setlocale(LC_CTYPE,(const char *) NULL);
829 if (p != (const char *) NULL)
830 locale=ConstantString(p);
831 if (locale == (char *) NULL)
832 locale=GetEnvironmentValue("LC_ALL");
833 if (locale == (char *) NULL)
834 locale=GetEnvironmentValue("LC_MESSAGES");
835 if (locale == (char *) NULL)
836 locale=GetEnvironmentValue("LC_CTYPE");
837 if (locale == (char *) NULL)
838 locale=GetEnvironmentValue("LANG");
839 if (locale == (char *) NULL)
840 locale=ConstantString("C");
841 (void) LoadLocaleLists(LocaleFilename,locale,exception);
842 locale=DestroyString(locale);
843 instantiate_locale=MagickTrue;
845 UnlockSemaphoreInfo(locale_semaphore);
847 return(locale_list != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
851 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
855 + I n t e r p r e t L o c a l e V a l u e %
859 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
861 % InterpretLocaleValue() interprets the string as a floating point number in
862 % the "C" locale and returns its value as a double. If sentinal is not a null
863 % pointer, the method also sets the value pointed by sentinal to point to the
864 % first character after the number.
866 % The format of the InterpretLocaleValue method is:
868 % double InterpretLocaleValue(const char *value,char **sentinal)
870 % A description of each parameter follows:
872 % o value: the string value.
874 % o sentinal: if sentinal is not NULL, a pointer to the character after the
875 % last character used in the conversion is stored in the location
876 % referenced by sentinal.
879 MagickExport double InterpretLocaleValue(const char *restrict string,
880 char **restrict sentinal)
885 #if defined(MAGICKCORE_HAVE_STRTOD_L)
890 locale=AcquireCLocale();
891 if (locale == (locale_t) NULL)
892 value=strtod(string,sentinal);
894 value=strtod_l(string,sentinal,locale);
897 value=strtod(string,sentinal);
903 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
907 % L i s t L o c a l e I n f o %
911 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
913 % ListLocaleInfo() lists the locale info to a file.
915 % The format of the ListLocaleInfo method is:
917 % MagickBooleanType ListLocaleInfo(FILE *file,ExceptionInfo *exception)
919 % A description of each parameter follows.
921 % o file: An pointer to a FILE.
923 % o exception: return any errors or warnings in this structure.
926 MagickExport MagickBooleanType ListLocaleInfo(FILE *file,
927 ExceptionInfo *exception)
941 if (file == (const FILE *) NULL)
944 locale_info=GetLocaleInfoList("*",&number_messages,exception);
945 if (locale_info == (const LocaleInfo **) NULL)
947 path=(const char *) NULL;
948 for (i=0; i < (ssize_t) number_messages; i++)
950 if (locale_info[i]->stealth != MagickFalse)
952 if ((path == (const char *) NULL) ||
953 (LocaleCompare(path,locale_info[i]->path) != 0))
955 if (locale_info[i]->path != (char *) NULL)
956 (void) FormatLocaleFile(file,"\nPath: %s\n\n",locale_info[i]->path);
957 (void) FormatLocaleFile(file,"Tag/Message\n");
958 (void) FormatLocaleFile(file,
959 "-------------------------------------------------"
960 "------------------------------\n");
962 path=locale_info[i]->path;
963 (void) FormatLocaleFile(file,"%s\n",locale_info[i]->tag);
964 if (locale_info[i]->message != (char *) NULL)
965 (void) FormatLocaleFile(file," %s",locale_info[i]->message);
966 (void) FormatLocaleFile(file,"\n");
969 locale_info=(const LocaleInfo **)
970 RelinquishMagickMemory((void *) locale_info);
975 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
979 + L o a d L o c a l e L i s t %
983 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
985 % LoadLocaleList() loads the locale configuration file which provides a mapping
986 % between locale attributes and a locale name.
988 % The format of the LoadLocaleList method is:
990 % MagickBooleanType LoadLocaleList(const char *xml,const char *filename,
991 % const size_t depth,ExceptionInfo *exception)
993 % A description of each parameter follows:
995 % o xml: The locale list in XML format.
997 % o filename: The locale list filename.
999 % o depth: depth of <include /> statements.
1001 % o exception: return any errors or warnings in this structure.
1005 static void ChopLocaleComponents(char *path,const size_t components)
1015 p=path+strlen(path)-1;
1018 for (count=0; (count < (ssize_t) components) && (p > path); p--)
1024 if (count < (ssize_t) components)
1028 static void *DestroyLocaleNode(void *locale_info)
1033 p=(LocaleInfo *) locale_info;
1034 if (p->path != (char *) NULL)
1035 p->path=DestroyString(p->path);
1036 if (p->tag != (char *) NULL)
1037 p->tag=DestroyString(p->tag);
1038 if (p->message != (char *) NULL)
1039 p->message=DestroyString(p->message);
1040 return(RelinquishMagickMemory(p));
1043 static void LocaleFatalErrorHandler(
1044 const ExceptionType magick_unused(severity),
1045 const char *reason,const char *description)
1047 if (reason == (char *) NULL)
1049 (void) FormatLocaleFile(stderr,"%s: %s",GetClientName(),reason);
1050 if (description != (char *) NULL)
1051 (void) FormatLocaleFile(stderr," (%s)",description);
1052 (void) FormatLocaleFile(stderr,".\n");
1053 (void) fflush(stderr);
1058 static MagickBooleanType LoadLocaleList(const char *xml,const char *filename,
1059 const char *locale,const size_t depth,ExceptionInfo *exception)
1062 keyword[MaxTextExtent],
1063 message[MaxTextExtent],
1083 Read the locale configure file.
1085 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1086 "Loading locale configure file \"%s\" ...",filename);
1087 if (xml == (const char *) NULL)
1088 return(MagickFalse);
1089 if (locale_list == (SplayTreeInfo *) NULL)
1091 locale_list=NewSplayTree(CompareSplayTreeString,(void *(*)(void *)) NULL,
1093 if (locale_list == (SplayTreeInfo *) NULL)
1094 return(MagickFalse);
1097 locale_info=(LocaleInfo *) NULL;
1101 fatal_handler=SetFatalErrorHandler(LocaleFatalErrorHandler);
1102 token=AcquireString(xml);
1103 for (q=(char *) xml; *q != '\0'; )
1108 GetMagickToken(q,&q,token);
1111 (void) CopyMagickString(keyword,token,MaxTextExtent);
1112 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
1117 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
1119 GetMagickToken(q,&q,token);
1120 while (isspace((int) ((unsigned char) *q)) != 0)
1125 if (LocaleNCompare(keyword,"<!--",4) == 0)
1130 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
1132 GetMagickToken(q,&q,token);
1133 while (isspace((int) ((unsigned char) *q)) != 0)
1138 if (LocaleCompare(keyword,"<include") == 0)
1143 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
1145 (void) CopyMagickString(keyword,token,MaxTextExtent);
1146 GetMagickToken(q,&q,token);
1149 GetMagickToken(q,&q,token);
1150 if (LocaleCompare(keyword,"locale") == 0)
1152 if (LocaleCompare(locale,token) != 0)
1156 if (LocaleCompare(keyword,"file") == 0)
1159 (void) ThrowMagickException(exception,GetMagickModule(),
1160 ConfigureError,"IncludeElementNestedTooDeeply","`%s'",token);
1164 path[MaxTextExtent],
1168 GetPathComponent(filename,HeadPath,path);
1170 (void) ConcatenateMagickString(path,DirectorySeparator,
1172 if (*token == *DirectorySeparator)
1173 (void) CopyMagickString(path,token,MaxTextExtent);
1175 (void) ConcatenateMagickString(path,token,MaxTextExtent);
1176 xml=FileToString(path,~0,exception);
1177 if (xml != (char *) NULL)
1179 status=LoadLocaleList(xml,path,locale,depth+1,exception);
1180 xml=(char *) RelinquishMagickMemory(xml);
1187 if (LocaleCompare(keyword,"<locale") == 0)
1192 while ((*token != '>') && (*q != '\0'))
1194 (void) CopyMagickString(keyword,token,MaxTextExtent);
1195 GetMagickToken(q,&q,token);
1198 GetMagickToken(q,&q,token);
1202 if (LocaleCompare(keyword,"</locale>") == 0)
1204 ChopLocaleComponents(tag,1);
1205 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1208 if (LocaleCompare(keyword,"<localemap>") == 0)
1210 if (LocaleCompare(keyword,"</localemap>") == 0)
1212 if (LocaleCompare(keyword,"<message") == 0)
1217 while ((*token != '>') && (*q != '\0'))
1219 (void) CopyMagickString(keyword,token,MaxTextExtent);
1220 GetMagickToken(q,&q,token);
1223 GetMagickToken(q,&q,token);
1224 if (LocaleCompare(keyword,"name") == 0)
1226 (void) ConcatenateMagickString(tag,token,MaxTextExtent);
1227 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1230 for (p=(char *) q; (*q != '<') && (*q != '\0'); q++) ;
1231 while (isspace((int) ((unsigned char) *p)) != 0)
1234 while ((isspace((int) ((unsigned char) *q)) != 0) && (q > p))
1236 (void) CopyMagickString(message,p,(size_t) (q-p+2));
1237 locale_info=(LocaleInfo *) AcquireMagickMemory(sizeof(*locale_info));
1238 if (locale_info == (LocaleInfo *) NULL)
1239 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1240 (void) ResetMagickMemory(locale_info,0,sizeof(*locale_info));
1241 locale_info->path=ConstantString(filename);
1242 locale_info->tag=ConstantString(tag);
1243 locale_info->message=ConstantString(message);
1244 locale_info->signature=MagickSignature;
1245 status=AddValueToSplayTree(locale_list,locale_info->tag,locale_info);
1246 if (status == MagickFalse)
1247 (void) ThrowMagickException(exception,GetMagickModule(),
1248 ResourceLimitError,"MemoryAllocationFailed","`%s'",
1250 (void) ConcatenateMagickString(tag,message,MaxTextExtent);
1251 (void) ConcatenateMagickString(tag,"\n",MaxTextExtent);
1255 if (LocaleCompare(keyword,"</message>") == 0)
1257 ChopLocaleComponents(tag,2);
1258 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1261 if (*keyword == '<')
1266 if (*(keyword+1) == '?')
1268 if (*(keyword+1) == '/')
1270 ChopLocaleComponents(tag,1);
1272 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1275 token[strlen(token)-1]='\0';
1276 (void) CopyMagickString(token,token+1,MaxTextExtent);
1277 (void) ConcatenateMagickString(tag,token,MaxTextExtent);
1278 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1281 GetMagickToken(q,(const char **) NULL,token);
1285 token=(char *) RelinquishMagickMemory(token);
1286 (void) SetFatalErrorHandler(fatal_handler);
1291 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1295 % L o a d L o c a l e L i s t s %
1299 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1301 % LoadLocaleList() loads one or more locale configuration file which
1302 % provides a mapping between locale attributes and a locale tag.
1304 % The format of the LoadLocaleLists method is:
1306 % MagickBooleanType LoadLocaleLists(const char *filename,
1307 % ExceptionInfo *exception)
1309 % A description of each parameter follows:
1311 % o filename: the font file tag.
1313 % o locale: the actual locale.
1315 % o exception: return any errors or warnings in this structure.
1318 static MagickBooleanType LoadLocaleLists(const char *filename,
1319 const char *locale,ExceptionInfo *exception)
1321 #if defined(MAGICKCORE_ZERO_CONFIGURATION_SUPPORT)
1322 return(LoadLocaleList(LocaleMap,"built-in",locale,0,exception));
1334 options=GetLocaleOptions(filename,exception);
1335 option=(const StringInfo *) GetNextValueInLinkedList(options);
1336 while (option != (const StringInfo *) NULL)
1338 status|=LoadLocaleList((const char *) GetStringInfoDatum(option),
1339 GetStringInfoPath(option),locale,0,exception);
1340 option=(const StringInfo *) GetNextValueInLinkedList(options);
1342 options=DestroyLocaleOptions(options);
1343 if ((locale_list == (SplayTreeInfo *) NULL) ||
1344 (GetNumberOfNodesInSplayTree(locale_list) == 0))
1346 options=GetLocaleOptions("english.xml",exception);
1347 option=(const StringInfo *) GetNextValueInLinkedList(options);
1348 while (option != (const StringInfo *) NULL)
1350 status|=LoadLocaleList((const char *) GetStringInfoDatum(option),
1351 GetStringInfoPath(option),locale,0,exception);
1352 option=(const StringInfo *) GetNextValueInLinkedList(options);
1354 options=DestroyLocaleOptions(options);
1356 if ((locale_list == (SplayTreeInfo *) NULL) ||
1357 (GetNumberOfNodesInSplayTree(locale_list) == 0))
1358 status|=LoadLocaleList(LocaleMap,"built-in",locale,0,exception);
1359 return(status != 0 ? MagickTrue : MagickFalse);
1364 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1368 + L o c a l e C o m p o n e n t G e n e s i s %
1372 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1374 % LocaleComponentGenesis() instantiates the locale component.
1376 % The format of the LocaleComponentGenesis method is:
1378 % MagickBooleanType LocaleComponentGenesis(void)
1381 MagickPrivate MagickBooleanType LocaleComponentGenesis(void)
1383 AcquireSemaphoreInfo(&locale_semaphore);
1388 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1392 + L o c a l e C o m p o n e n t T e r m i n u s %
1396 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1398 % LocaleComponentTerminus() destroys the locale component.
1400 % The format of the LocaleComponentTerminus method is:
1402 % LocaleComponentTerminus(void)
1405 MagickPrivate void LocaleComponentTerminus(void)
1407 if (locale_semaphore == (SemaphoreInfo *) NULL)
1408 AcquireSemaphoreInfo(&locale_semaphore);
1409 LockSemaphoreInfo(locale_semaphore);
1410 #if defined(MAGICKCORE_HAVE_STRTOD_L)
1413 instantiate_locale=MagickFalse;
1414 UnlockSemaphoreInfo(locale_semaphore);
1415 DestroySemaphoreInfo(&locale_semaphore);