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/token.h"
57 #include "MagickCore/utility.h"
58 #include "MagickCore/utility-private.h"
59 #include "MagickCore/xml-tree.h"
64 #define LocaleFilename "locale.xml"
65 #define MaxRecursionDepth 200
70 #if defined(__CYGWIN__)
71 typedef struct _locale_t *locale_t;
79 "<?xml version=\"1.0\"?>"
81 " <locale name=\"C\">"
83 " <Message name=\"\">"
90 *locale_semaphore = (SemaphoreInfo *) NULL;
93 *locale_list = (SplayTreeInfo *) NULL;
95 #if defined(MAGICKCORE_HAVE_STRTOD_L)
96 static volatile locale_t
97 c_locale = (locale_t) NULL;
100 static volatile MagickBooleanType
101 instantiate_locale = MagickFalse;
104 Forward declarations.
106 static MagickBooleanType
107 InitializeLocaleList(ExceptionInfo *),
108 LoadLocaleLists(const char *,const char *,ExceptionInfo *);
110 #if defined(MAGICKCORE_HAVE_STRTOD_L)
112 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
116 + A c q u i r e C L o c a l e %
120 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
122 % AcquireCLocale() allocates the C locale object, or (locale_t) 0 with
123 % errno set if it cannot be acquired.
125 % The format of the AcquireCLocale method is:
127 % locale_t AcquireCLocale(void)
130 static locale_t AcquireCLocale(void)
132 #if defined(MAGICKCORE_HAVE_NEWLOCALE)
133 if (c_locale == (locale_t) NULL)
134 c_locale=newlocale(LC_ALL_MASK,"C",(locale_t) 0);
135 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
136 if (c_locale == (locale_t) NULL)
137 c_locale=_create_locale(LC_ALL,"C");
143 #if defined(MAGICKCORE_HAVE_STRTOD_L)
145 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
149 + D e s t r o y C L o c a l e %
153 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
155 % DestroyCLocale() releases the resources allocated for a locale object
156 % returned by a call to the AcquireCLocale() method.
158 % The format of the DestroyCLocale method is:
160 % void DestroyCLocale(void)
163 static void DestroyCLocale(void)
165 #if defined(MAGICKCORE_HAVE_NEWLOCALE)
166 if (c_locale != (locale_t) NULL)
167 freelocale(c_locale);
168 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
169 if (c_locale != (locale_t) NULL)
170 _free_locale(c_locale);
172 c_locale=(locale_t) NULL;
177 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
181 % D e s t r o y L o c a l e O p t i o n s %
185 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
187 % DestroyLocaleOptions() releases memory associated with an locale
190 % The format of the DestroyProfiles method is:
192 % LinkedListInfo *DestroyLocaleOptions(Image *image)
194 % A description of each parameter follows:
196 % o image: the image.
200 static void *DestroyOptions(void *message)
202 return(DestroyStringInfo((StringInfo *) message));
205 MagickExport LinkedListInfo *DestroyLocaleOptions(LinkedListInfo *messages)
207 assert(messages != (LinkedListInfo *) NULL);
208 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
209 return(DestroyLinkedList(messages,DestroyOptions));
213 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
217 + F o r m a t L o c a l e F i l e %
221 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
223 % FormatLocaleFile() prints formatted output of a variable argument list to a
224 % file in the "C" locale.
226 % The format of the FormatLocaleFile method is:
228 % ssize_t FormatLocaleFile(FILE *file,const char *format,...)
230 % A description of each parameter follows.
234 % o format: A file describing the format to use to write the remaining
239 MagickExport ssize_t FormatLocaleFileList(FILE *file,
240 const char *restrict format,va_list operands)
245 #if defined(MAGICKCORE_HAVE_VFPRINTF_L)
250 locale=AcquireCLocale();
251 if (locale == (locale_t) NULL)
252 n=(ssize_t) vfprintf(file,format,operands);
254 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
255 n=(ssize_t) vfprintf_l(file,format,locale,operands);
257 n=(ssize_t) vfprintf_l(file,locale,format,operands);
261 #if defined(MAGICKCORE_HAVE_USELOCALE)
267 locale=AcquireCLocale();
268 if (locale == (locale_t) NULL)
269 n=(ssize_t) vfprintf(file,format,operands);
272 previous_locale=uselocale(locale);
273 n=(ssize_t) vfprintf(file,format,operands);
274 uselocale(previous_locale);
278 n=(ssize_t) vfprintf(file,format,operands);
284 MagickExport ssize_t FormatLocaleFile(FILE *file,const char *restrict format,
293 va_start(operands,format);
294 n=FormatLocaleFileList(file,format,operands);
300 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
304 + F o r m a t L o c a l e S t r i n g %
308 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
310 % FormatLocaleString() prints formatted output of a variable argument list to
311 % a string buffer in the "C" locale.
313 % The format of the FormatLocaleString method is:
315 % ssize_t FormatLocaleString(char *string,const size_t length,
316 % const char *format,...)
318 % A description of each parameter follows.
320 % o string: FormatLocaleString() returns the formatted string in this
323 % o length: the maximum length of the string.
325 % o format: A string describing the format to use to write the remaining
330 MagickExport ssize_t FormatLocaleStringList(char *restrict string,
331 const size_t length,const char *restrict format,va_list operands)
336 #if defined(MAGICKCORE_HAVE_VSNPRINTF_L)
341 locale=AcquireCLocale();
342 if (locale == (locale_t) NULL)
343 n=(ssize_t) vsnprintf(string,length,format,operands);
345 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
346 n=(ssize_t) vsnprintf_l(string,length,format,locale,operands);
348 n=(ssize_t) vsnprintf_l(string,length,locale,format,operands);
351 #elif defined(MAGICKCORE_HAVE_VSNPRINTF)
352 #if defined(MAGICKCORE_HAVE_USELOCALE)
358 locale=AcquireCLocale();
359 if (locale == (locale_t) NULL)
360 n=(ssize_t) vsnprintf(string,length,format,operands);
363 previous_locale=uselocale(locale);
364 n=(ssize_t) vsnprintf(string,length,format,operands);
365 uselocale(previous_locale);
369 n=(ssize_t) vsnprintf(string,length,format,operands);
372 n=(ssize_t) vsprintf(string,format,operands);
375 string[length-1]='\0';
379 MagickExport ssize_t FormatLocaleString(char *restrict string,
380 const size_t length,const char *restrict format,...)
388 va_start(operands,format);
389 n=FormatLocaleStringList(string,length,format,operands);
395 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
399 + G e t L o c a l e I n f o _ %
403 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
405 % GetLocaleInfo_() searches the locale list for the specified tag and if
406 % found returns attributes for that element.
408 % The format of the GetLocaleInfo method is:
410 % const LocaleInfo *GetLocaleInfo_(const char *tag,
411 % ExceptionInfo *exception)
413 % A description of each parameter follows:
415 % o tag: the locale tag.
417 % o exception: return any errors or warnings in this structure.
420 MagickExport const LocaleInfo *GetLocaleInfo_(const char *tag,
421 ExceptionInfo *exception)
423 assert(exception != (ExceptionInfo *) NULL);
424 if ((locale_list == (SplayTreeInfo *) NULL) ||
425 (instantiate_locale == MagickFalse))
426 if (InitializeLocaleList(exception) == MagickFalse)
427 return((const LocaleInfo *) NULL);
428 if ((locale_list == (SplayTreeInfo *) NULL) ||
429 (GetNumberOfNodesInSplayTree(locale_list) == 0))
430 return((const LocaleInfo *) NULL);
431 if ((tag == (const char *) NULL) || (LocaleCompare(tag,"*") == 0))
433 ResetSplayTreeIterator(locale_list);
434 return((const LocaleInfo *) GetNextValueInSplayTree(locale_list));
436 return((const LocaleInfo *) GetValueFromSplayTree(locale_list,tag));
440 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
444 % G e t L o c a l e I n f o L i s t %
448 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
450 % GetLocaleInfoList() returns any locale messages that match the
453 % The format of the GetLocaleInfoList function is:
455 % const LocaleInfo **GetLocaleInfoList(const char *pattern,
456 % size_t *number_messages,ExceptionInfo *exception)
458 % A description of each parameter follows:
460 % o pattern: Specifies a pointer to a text string containing a pattern.
462 % o number_messages: This integer returns the number of locale messages in
465 % o exception: return any errors or warnings in this structure.
469 #if defined(__cplusplus) || defined(c_plusplus)
473 static int LocaleInfoCompare(const void *x,const void *y)
479 p=(const LocaleInfo **) x,
480 q=(const LocaleInfo **) y;
481 if (LocaleCompare((*p)->path,(*q)->path) == 0)
482 return(LocaleCompare((*p)->tag,(*q)->tag));
483 return(LocaleCompare((*p)->path,(*q)->path));
486 #if defined(__cplusplus) || defined(c_plusplus)
490 MagickExport const LocaleInfo **GetLocaleInfoList(const char *pattern,
491 size_t *number_messages,ExceptionInfo *exception)
496 register const LocaleInfo
503 Allocate locale list.
505 assert(pattern != (char *) NULL);
506 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
507 assert(number_messages != (size_t *) NULL);
509 p=GetLocaleInfo_("*",exception);
510 if (p == (const LocaleInfo *) NULL)
511 return((const LocaleInfo **) NULL);
512 messages=(const LocaleInfo **) AcquireQuantumMemory((size_t)
513 GetNumberOfNodesInSplayTree(locale_list)+1UL,sizeof(*messages));
514 if (messages == (const LocaleInfo **) NULL)
515 return((const LocaleInfo **) NULL);
517 Generate locale list.
519 LockSemaphoreInfo(locale_semaphore);
520 ResetSplayTreeIterator(locale_list);
521 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_list);
522 for (i=0; p != (const LocaleInfo *) NULL; )
524 if ((p->stealth == MagickFalse) &&
525 (GlobExpression(p->tag,pattern,MagickTrue) != MagickFalse))
527 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_list);
529 UnlockSemaphoreInfo(locale_semaphore);
530 qsort((void *) messages,(size_t) i,sizeof(*messages),LocaleInfoCompare);
531 messages[i]=(LocaleInfo *) NULL;
532 *number_messages=(size_t) i;
537 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
541 % G e t L o c a l e L i s t %
545 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
547 % GetLocaleList() returns any locale messages that match the specified
550 % The format of the GetLocaleList function is:
552 % char **GetLocaleList(const char *pattern,size_t *number_messages,
553 % Exceptioninfo *exception)
555 % A description of each parameter follows:
557 % o pattern: Specifies a pointer to a text string containing a pattern.
559 % o number_messages: This integer returns the number of messages in the
562 % o exception: return any errors or warnings in this structure.
566 #if defined(__cplusplus) || defined(c_plusplus)
570 static int LocaleTagCompare(const void *x,const void *y)
578 return(LocaleCompare(*p,*q));
581 #if defined(__cplusplus) || defined(c_plusplus)
585 MagickExport char **GetLocaleList(const char *pattern,
586 size_t *number_messages,ExceptionInfo *exception)
591 register const LocaleInfo
598 Allocate locale list.
600 assert(pattern != (char *) NULL);
601 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
602 assert(number_messages != (size_t *) NULL);
604 p=GetLocaleInfo_("*",exception);
605 if (p == (const LocaleInfo *) NULL)
606 return((char **) NULL);
607 messages=(char **) AcquireQuantumMemory((size_t)
608 GetNumberOfNodesInSplayTree(locale_list)+1UL,sizeof(*messages));
609 if (messages == (char **) NULL)
610 return((char **) NULL);
611 LockSemaphoreInfo(locale_semaphore);
612 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_list);
613 for (i=0; p != (const LocaleInfo *) NULL; )
615 if ((p->stealth == MagickFalse) &&
616 (GlobExpression(p->tag,pattern,MagickTrue) != MagickFalse))
617 messages[i++]=ConstantString(p->tag);
618 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_list);
620 UnlockSemaphoreInfo(locale_semaphore);
621 qsort((void *) messages,(size_t) i,sizeof(*messages),LocaleTagCompare);
622 messages[i]=(char *) NULL;
623 *number_messages=(size_t) i;
628 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
632 % G e t L o c a l e M e s s a g e %
636 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
638 % GetLocaleMessage() returns a message in the current locale that matches the
641 % The format of the GetLocaleMessage method is:
643 % const char *GetLocaleMessage(const char *tag)
645 % A description of each parameter follows:
647 % o tag: Return a message that matches this tag in the current locale.
650 MagickExport const char *GetLocaleMessage(const char *tag)
661 if ((tag == (const char *) NULL) || (*tag == '\0'))
663 exception=AcquireExceptionInfo();
664 (void) FormatLocaleString(name,MaxTextExtent,"%s/",tag);
665 locale_info=GetLocaleInfo_(name,exception);
666 exception=DestroyExceptionInfo(exception);
667 if (locale_info != (const LocaleInfo *) NULL)
668 return(locale_info->message);
673 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
677 % G e t L o c a l e O p t i o n s %
681 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
683 % GetLocaleOptions() returns any Magick configuration messages associated
684 % with the specified filename.
686 % The format of the GetLocaleOptions method is:
688 % LinkedListInfo *GetLocaleOptions(const char *filename,
689 % ExceptionInfo *exception)
691 % A description of each parameter follows:
693 % o filename: the locale file tag.
695 % o exception: return any errors or warnings in this structure.
698 MagickExport LinkedListInfo *GetLocaleOptions(const char *filename,
699 ExceptionInfo *exception)
714 assert(filename != (const char *) NULL);
715 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
716 assert(exception != (ExceptionInfo *) NULL);
717 (void) CopyMagickString(path,filename,MaxTextExtent);
719 Load XML from configuration files to linked-list.
721 messages=NewLinkedList(0);
722 paths=GetConfigurePaths(filename,exception);
723 if (paths != (LinkedListInfo *) NULL)
725 ResetLinkedListIterator(paths);
726 element=(const char *) GetNextValueInLinkedList(paths);
727 while (element != (const char *) NULL)
729 (void) FormatLocaleString(path,MaxTextExtent,"%s%s",element,filename);
730 (void) LogMagickEvent(LocaleEvent,GetMagickModule(),
731 "Searching for locale file: \"%s\"",path);
732 xml=ConfigureFileToStringInfo(path);
733 if (xml != (StringInfo *) NULL)
734 (void) AppendValueToLinkedList(messages,xml);
735 element=(const char *) GetNextValueInLinkedList(paths);
737 paths=DestroyLinkedList(paths,RelinquishMagickMemory);
739 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
744 blob=(char *) NTResourceToBlob(filename);
745 if (blob != (char *) NULL)
747 xml=StringToStringInfo(blob);
748 (void) AppendValueToLinkedList(messages,xml);
749 blob=DestroyString(blob);
753 ResetLinkedListIterator(messages);
758 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
762 % G e t L o c a l e V a l u e %
766 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
768 % GetLocaleValue() returns the message associated with the locale info.
770 % The format of the GetLocaleValue method is:
772 % const char *GetLocaleValue(const LocaleInfo *locale_info)
774 % A description of each parameter follows:
776 % o locale_info: The locale info.
779 MagickExport const char *GetLocaleValue(const LocaleInfo *locale_info)
781 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
782 assert(locale_info != (LocaleInfo *) NULL);
783 assert(locale_info->signature == MagickSignature);
784 return(locale_info->message);
788 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
792 + I n i t i a l i z e L o c a l e L i s t %
796 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
798 % InitializeLocaleList() initializes the locale list.
800 % The format of the InitializeLocaleList method is:
802 % MagickBooleanType InitializeLocaleList(ExceptionInfo *exception)
804 % A description of each parameter follows.
806 % o exception: return any errors or warnings in this structure.
809 static MagickBooleanType InitializeLocaleList(ExceptionInfo *exception)
811 if ((locale_list == (SplayTreeInfo *) NULL) &&
812 (instantiate_locale == MagickFalse))
814 if (locale_semaphore == (SemaphoreInfo *) NULL)
815 AcquireSemaphoreInfo(&locale_semaphore);
816 LockSemaphoreInfo(locale_semaphore);
817 if ((locale_list == (SplayTreeInfo *) NULL) &&
818 (instantiate_locale == MagickFalse))
826 locale=(char *) NULL;
827 p=setlocale(LC_CTYPE,(const char *) NULL);
828 if (p != (const char *) NULL)
829 locale=ConstantString(p);
830 if (locale == (char *) NULL)
831 locale=GetEnvironmentValue("LC_ALL");
832 if (locale == (char *) NULL)
833 locale=GetEnvironmentValue("LC_MESSAGES");
834 if (locale == (char *) NULL)
835 locale=GetEnvironmentValue("LC_CTYPE");
836 if (locale == (char *) NULL)
837 locale=GetEnvironmentValue("LANG");
838 if (locale == (char *) NULL)
839 locale=ConstantString("C");
840 (void) LoadLocaleLists(LocaleFilename,locale,exception);
841 locale=DestroyString(locale);
842 instantiate_locale=MagickTrue;
844 UnlockSemaphoreInfo(locale_semaphore);
846 return(locale_list != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
850 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
854 + I n t e r p r e t L o c a l e V a l u e %
858 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
860 % InterpretLocaleValue() interprets the string as a floating point number in
861 % the "C" locale and returns its value as a double. If sentinal is not a null
862 % pointer, the method also sets the value pointed by sentinal to point to the
863 % first character after the number.
865 % The format of the InterpretLocaleValue method is:
867 % double InterpretLocaleValue(const char *value,char **sentinal)
869 % A description of each parameter follows:
871 % o value: the string value.
873 % o sentinal: if sentinal is not NULL, a pointer to the character after the
874 % last character used in the conversion is stored in the location
875 % referenced by sentinal.
878 MagickExport double InterpretLocaleValue(const char *restrict string,
879 char **restrict sentinal)
884 #if defined(MAGICKCORE_HAVE_STRTOD_L)
889 locale=AcquireCLocale();
890 if (locale == (locale_t) NULL)
891 value=strtod(string,sentinal);
893 value=strtod_l(string,sentinal,locale);
896 value=strtod(string,sentinal);
902 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
906 % L i s t L o c a l e I n f o %
910 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
912 % ListLocaleInfo() lists the locale info to a file.
914 % The format of the ListLocaleInfo method is:
916 % MagickBooleanType ListLocaleInfo(FILE *file,ExceptionInfo *exception)
918 % A description of each parameter follows.
920 % o file: An pointer to a FILE.
922 % o exception: return any errors or warnings in this structure.
925 MagickExport MagickBooleanType ListLocaleInfo(FILE *file,
926 ExceptionInfo *exception)
940 if (file == (const FILE *) NULL)
943 locale_info=GetLocaleInfoList("*",&number_messages,exception);
944 if (locale_info == (const LocaleInfo **) NULL)
946 path=(const char *) NULL;
947 for (i=0; i < (ssize_t) number_messages; i++)
949 if (locale_info[i]->stealth != MagickFalse)
951 if ((path == (const char *) NULL) ||
952 (LocaleCompare(path,locale_info[i]->path) != 0))
954 if (locale_info[i]->path != (char *) NULL)
955 (void) FormatLocaleFile(file,"\nPath: %s\n\n",locale_info[i]->path);
956 (void) FormatLocaleFile(file,"Tag/Message\n");
957 (void) FormatLocaleFile(file,
958 "-------------------------------------------------"
959 "------------------------------\n");
961 path=locale_info[i]->path;
962 (void) FormatLocaleFile(file,"%s\n",locale_info[i]->tag);
963 if (locale_info[i]->message != (char *) NULL)
964 (void) FormatLocaleFile(file," %s",locale_info[i]->message);
965 (void) FormatLocaleFile(file,"\n");
968 locale_info=(const LocaleInfo **)
969 RelinquishMagickMemory((void *) locale_info);
974 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
978 + L o a d L o c a l e L i s t %
982 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
984 % LoadLocaleList() loads the locale configuration file which provides a mapping
985 % between locale attributes and a locale name.
987 % The format of the LoadLocaleList method is:
989 % MagickBooleanType LoadLocaleList(const char *xml,const char *filename,
990 % const size_t depth,ExceptionInfo *exception)
992 % A description of each parameter follows:
994 % o xml: The locale list in XML format.
996 % o filename: The locale list filename.
998 % o depth: depth of <include /> statements.
1000 % o exception: return any errors or warnings in this structure.
1004 static void ChopLocaleComponents(char *path,const size_t components)
1014 p=path+strlen(path)-1;
1017 for (count=0; (count < (ssize_t) components) && (p > path); p--)
1023 if (count < (ssize_t) components)
1027 static void *DestroyLocaleNode(void *locale_info)
1032 p=(LocaleInfo *) locale_info;
1033 if (p->path != (char *) NULL)
1034 p->path=DestroyString(p->path);
1035 if (p->tag != (char *) NULL)
1036 p->tag=DestroyString(p->tag);
1037 if (p->message != (char *) NULL)
1038 p->message=DestroyString(p->message);
1039 return(RelinquishMagickMemory(p));
1042 static void LocaleFatalErrorHandler(
1043 const ExceptionType magick_unused(severity),
1044 const char *reason,const char *description)
1046 if (reason == (char *) NULL)
1048 (void) FormatLocaleFile(stderr,"%s: %s",GetClientName(),reason);
1049 if (description != (char *) NULL)
1050 (void) FormatLocaleFile(stderr," (%s)",description);
1051 (void) FormatLocaleFile(stderr,".\n");
1052 (void) fflush(stderr);
1057 static MagickBooleanType LoadLocaleList(const char *xml,const char *filename,
1058 const char *locale,const size_t depth,ExceptionInfo *exception)
1061 keyword[MaxTextExtent],
1062 message[MaxTextExtent],
1082 Read the locale configure file.
1084 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1085 "Loading locale configure file \"%s\" ...",filename);
1086 if (xml == (const char *) NULL)
1087 return(MagickFalse);
1088 if (locale_list == (SplayTreeInfo *) NULL)
1090 locale_list=NewSplayTree(CompareSplayTreeString,(void *(*)(void *)) NULL,
1092 if (locale_list == (SplayTreeInfo *) NULL)
1093 return(MagickFalse);
1096 locale_info=(LocaleInfo *) NULL;
1100 fatal_handler=SetFatalErrorHandler(LocaleFatalErrorHandler);
1101 token=AcquireString(xml);
1102 for (q=(char *) xml; *q != '\0'; )
1107 GetMagickToken(q,&q,token);
1110 (void) CopyMagickString(keyword,token,MaxTextExtent);
1111 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
1116 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
1118 GetMagickToken(q,&q,token);
1119 while (isspace((int) ((unsigned char) *q)) != 0)
1124 if (LocaleNCompare(keyword,"<!--",4) == 0)
1129 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
1131 GetMagickToken(q,&q,token);
1132 while (isspace((int) ((unsigned char) *q)) != 0)
1137 if (LocaleCompare(keyword,"<include") == 0)
1142 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
1144 (void) CopyMagickString(keyword,token,MaxTextExtent);
1145 GetMagickToken(q,&q,token);
1148 GetMagickToken(q,&q,token);
1149 if (LocaleCompare(keyword,"locale") == 0)
1151 if (LocaleCompare(locale,token) != 0)
1155 if (LocaleCompare(keyword,"file") == 0)
1158 (void) ThrowMagickException(exception,GetMagickModule(),
1159 ConfigureError,"IncludeElementNestedTooDeeply","`%s'",token);
1163 path[MaxTextExtent],
1167 GetPathComponent(filename,HeadPath,path);
1169 (void) ConcatenateMagickString(path,DirectorySeparator,
1171 if (*token == *DirectorySeparator)
1172 (void) CopyMagickString(path,token,MaxTextExtent);
1174 (void) ConcatenateMagickString(path,token,MaxTextExtent);
1175 xml=FileToString(path,~0,exception);
1176 if (xml != (char *) NULL)
1178 status=LoadLocaleList(xml,path,locale,depth+1,exception);
1179 xml=(char *) RelinquishMagickMemory(xml);
1186 if (LocaleCompare(keyword,"<locale") == 0)
1191 while ((*token != '>') && (*q != '\0'))
1193 (void) CopyMagickString(keyword,token,MaxTextExtent);
1194 GetMagickToken(q,&q,token);
1197 GetMagickToken(q,&q,token);
1201 if (LocaleCompare(keyword,"</locale>") == 0)
1203 ChopLocaleComponents(tag,1);
1204 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1207 if (LocaleCompare(keyword,"<localemap>") == 0)
1209 if (LocaleCompare(keyword,"</localemap>") == 0)
1211 if (LocaleCompare(keyword,"<message") == 0)
1216 while ((*token != '>') && (*q != '\0'))
1218 (void) CopyMagickString(keyword,token,MaxTextExtent);
1219 GetMagickToken(q,&q,token);
1222 GetMagickToken(q,&q,token);
1223 if (LocaleCompare(keyword,"name") == 0)
1225 (void) ConcatenateMagickString(tag,token,MaxTextExtent);
1226 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1229 for (p=(char *) q; (*q != '<') && (*q != '\0'); q++) ;
1230 while (isspace((int) ((unsigned char) *p)) != 0)
1233 while ((isspace((int) ((unsigned char) *q)) != 0) && (q > p))
1235 (void) CopyMagickString(message,p,(size_t) (q-p+2));
1236 locale_info=(LocaleInfo *) AcquireMagickMemory(sizeof(*locale_info));
1237 if (locale_info == (LocaleInfo *) NULL)
1238 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1239 (void) ResetMagickMemory(locale_info,0,sizeof(*locale_info));
1240 locale_info->path=ConstantString(filename);
1241 locale_info->tag=ConstantString(tag);
1242 locale_info->message=ConstantString(message);
1243 locale_info->signature=MagickSignature;
1244 status=AddValueToSplayTree(locale_list,locale_info->tag,locale_info);
1245 if (status == MagickFalse)
1246 (void) ThrowMagickException(exception,GetMagickModule(),
1247 ResourceLimitError,"MemoryAllocationFailed","`%s'",
1249 (void) ConcatenateMagickString(tag,message,MaxTextExtent);
1250 (void) ConcatenateMagickString(tag,"\n",MaxTextExtent);
1254 if (LocaleCompare(keyword,"</message>") == 0)
1256 ChopLocaleComponents(tag,2);
1257 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1260 if (*keyword == '<')
1265 if (*(keyword+1) == '?')
1267 if (*(keyword+1) == '/')
1269 ChopLocaleComponents(tag,1);
1271 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1274 token[strlen(token)-1]='\0';
1275 (void) CopyMagickString(token,token+1,MaxTextExtent);
1276 (void) ConcatenateMagickString(tag,token,MaxTextExtent);
1277 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1280 GetMagickToken(q,(const char **) NULL,token);
1284 token=(char *) RelinquishMagickMemory(token);
1285 (void) SetFatalErrorHandler(fatal_handler);
1290 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1294 % L o a d L o c a l e L i s t s %
1298 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1300 % LoadLocaleList() loads one or more locale configuration file which
1301 % provides a mapping between locale attributes and a locale tag.
1303 % The format of the LoadLocaleLists method is:
1305 % MagickBooleanType LoadLocaleLists(const char *filename,
1306 % ExceptionInfo *exception)
1308 % A description of each parameter follows:
1310 % o filename: the font file tag.
1312 % o locale: the actual locale.
1314 % o exception: return any errors or warnings in this structure.
1317 static MagickBooleanType LoadLocaleLists(const char *filename,
1318 const char *locale,ExceptionInfo *exception)
1320 #if defined(MAGICKCORE_EMBEDDABLE_SUPPORT)
1321 return(LoadLocaleList(LocaleMap,"built-in",locale,0,exception));
1333 options=GetLocaleOptions(filename,exception);
1334 option=(const StringInfo *) GetNextValueInLinkedList(options);
1335 while (option != (const StringInfo *) NULL)
1337 status|=LoadLocaleList((const char *) GetStringInfoDatum(option),
1338 GetStringInfoPath(option),locale,0,exception);
1339 option=(const StringInfo *) GetNextValueInLinkedList(options);
1341 options=DestroyLocaleOptions(options);
1342 if ((locale_list == (SplayTreeInfo *) NULL) ||
1343 (GetNumberOfNodesInSplayTree(locale_list) == 0))
1345 options=GetLocaleOptions("english.xml",exception);
1346 option=(const StringInfo *) GetNextValueInLinkedList(options);
1347 while (option != (const StringInfo *) NULL)
1349 status|=LoadLocaleList((const char *) GetStringInfoDatum(option),
1350 GetStringInfoPath(option),locale,0,exception);
1351 option=(const StringInfo *) GetNextValueInLinkedList(options);
1353 options=DestroyLocaleOptions(options);
1355 if ((locale_list == (SplayTreeInfo *) NULL) ||
1356 (GetNumberOfNodesInSplayTree(locale_list) == 0))
1357 status|=LoadLocaleList(LocaleMap,"built-in",locale,0,exception);
1358 return(status != 0 ? MagickTrue : MagickFalse);
1363 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1367 + L o c a l e C o m p o n e n t G e n e s i s %
1371 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1373 % LocaleComponentGenesis() instantiates the locale component.
1375 % The format of the LocaleComponentGenesis method is:
1377 % MagickBooleanType LocaleComponentGenesis(void)
1380 MagickPrivate MagickBooleanType LocaleComponentGenesis(void)
1382 AcquireSemaphoreInfo(&locale_semaphore);
1387 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1391 + L o c a l e C o m p o n e n t T e r m i n u s %
1395 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1397 % LocaleComponentTerminus() destroys the locale component.
1399 % The format of the LocaleComponentTerminus method is:
1401 % LocaleComponentTerminus(void)
1404 MagickPrivate void LocaleComponentTerminus(void)
1406 if (locale_semaphore == (SemaphoreInfo *) NULL)
1407 AcquireSemaphoreInfo(&locale_semaphore);
1408 LockSemaphoreInfo(locale_semaphore);
1409 #if defined(MAGICKCORE_HAVE_STRTOD_L)
1412 instantiate_locale=MagickFalse;
1413 UnlockSemaphoreInfo(locale_semaphore);
1414 DestroySemaphoreInfo(&locale_semaphore);