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 "magick/studio.h"
43 #include "magick/blob.h"
44 #include "magick/client.h"
45 #include "magick/configure.h"
46 #include "magick/exception.h"
47 #include "magick/exception-private.h"
48 #include "magick/hashmap.h"
49 #include "magick/locale_.h"
50 #include "magick/log.h"
51 #include "magick/memory_.h"
52 #include "magick/semaphore.h"
53 #include "magick/splay-tree.h"
54 #include "magick/string_.h"
55 #include "magick/token.h"
56 #include "magick/utility.h"
57 #include "magick/xml-tree.h"
62 #define LocaleFilename "locale.xml"
63 #define MaxRecursionDepth 200
68 #if defined(__CYGWIN__)
69 typedef struct _locale_t *locale_t;
77 "<?xml version=\"1.0\"?>"
79 " <locale name=\"C\">"
81 " <Message name=\"\">"
88 *locale_semaphore = (SemaphoreInfo *) NULL;
91 *locale_list = (SplayTreeInfo *) NULL;
93 #if defined(MAGICKCORE_HAVE_STRTOD_L)
94 static volatile locale_t
95 c_locale = (locale_t) NULL;
98 static volatile MagickBooleanType
99 instantiate_locale = MagickFalse;
102 Forward declarations.
104 static MagickBooleanType
105 InitializeLocaleList(ExceptionInfo *),
106 LoadLocaleLists(const char *,const char *,ExceptionInfo *);
108 #if defined(MAGICKCORE_HAVE_STRTOD_L)
110 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
114 + A c q u i r e C L o c a l e %
118 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
120 % AcquireCLocale() allocates the C locale object, or (locale_t) 0 with
121 % errno set if it cannot be acquired.
123 % The format of the AcquireCLocale method is:
125 % locale_t AcquireCLocale(void)
128 static locale_t AcquireCLocale(void)
130 #if defined(MAGICKCORE_HAVE_NEWLOCALE)
131 if (c_locale == (locale_t) NULL)
132 c_locale=newlocale(LC_ALL_MASK,"C",(locale_t) 0);
133 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
134 if (c_locale == (locale_t) NULL)
135 c_locale=_create_locale(LC_ALL,"C");
141 #if defined(MAGICKCORE_HAVE_STRTOD_L)
143 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
147 + D e s t r o y C L o c a l e %
151 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
153 % DestroyCLocale() releases the resources allocated for a locale object
154 % returned by a call to the AcquireCLocale() method.
156 % The format of the DestroyCLocale method is:
158 % void DestroyCLocale(void)
161 static void DestroyCLocale(void)
163 #if defined(MAGICKCORE_HAVE_NEWLOCALE)
164 if (c_locale != (locale_t) NULL)
165 freelocale(c_locale);
166 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
167 if (c_locale != (locale_t) NULL)
168 _free_locale(c_locale);
170 c_locale=(locale_t) NULL;
175 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
179 % D e s t r o y L o c a l e O p t i o n s %
183 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
185 % DestroyLocaleOptions() releases memory associated with an locale
188 % The format of the DestroyProfiles method is:
190 % LinkedListInfo *DestroyLocaleOptions(Image *image)
192 % A description of each parameter follows:
194 % o image: the image.
198 static void *DestroyOptions(void *message)
200 return(DestroyStringInfo((StringInfo *) message));
203 MagickExport LinkedListInfo *DestroyLocaleOptions(LinkedListInfo *messages)
205 assert(messages != (LinkedListInfo *) NULL);
206 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
207 return(DestroyLinkedList(messages,DestroyOptions));
211 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
215 + F o r m a t L o c a l e F i l e %
219 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
221 % FormatLocaleFile() prints formatted output of a variable argument list to a
222 % file in the "C" locale.
224 % The format of the FormatLocaleFile method is:
226 % ssize_t FormatLocaleFile(FILE *file,const char *format,...)
228 % A description of each parameter follows.
232 % o format: A file describing the format to use to write the remaining
237 MagickExport ssize_t FormatLocaleFileList(FILE *file,
238 const char *restrict format,va_list operands)
243 #if defined(MAGICKCORE_HAVE_VFPRINTF_L)
248 locale=AcquireCLocale();
249 if (locale == (locale_t) NULL)
250 n=(ssize_t) vfprintf(file,format,operands);
252 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
253 n=(ssize_t) vfprintf_l(file,format,locale,operands);
255 n=(ssize_t) vfprintf_l(file,locale,format,operands);
259 #if defined(MAGICKCORE_HAVE_USELOCALE)
265 locale=AcquireCLocale();
266 if (locale == (locale_t) NULL)
267 n=(ssize_t) vfprintf(file,format,operands);
270 previous_locale=uselocale(locale);
271 n=(ssize_t) vfprintf(file,format,operands);
272 uselocale(previous_locale);
276 n=(ssize_t) vfprintf(file,format,operands);
282 MagickExport ssize_t FormatLocaleFile(FILE *file,const char *restrict format,
291 va_start(operands,format);
292 n=FormatLocaleFileList(file,format,operands);
298 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
302 + F o r m a t L o c a l e S t r i n g %
306 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
308 % FormatLocaleString() prints formatted output of a variable argument list to
309 % a string buffer in the "C" locale.
311 % The format of the FormatLocaleString method is:
313 % ssize_t FormatLocaleString(char *string,const size_t length,
314 % const char *format,...)
316 % A description of each parameter follows.
318 % o string: FormatLocaleString() returns the formatted string in this
321 % o length: the maximum length of the string.
323 % o format: A string describing the format to use to write the remaining
328 MagickExport ssize_t FormatLocaleStringList(char *restrict string,
329 const size_t length,const char *restrict format,va_list operands)
334 #if defined(MAGICKCORE_HAVE_VSNPRINTF_L)
339 locale=AcquireCLocale();
340 if (locale == (locale_t) NULL)
341 n=(ssize_t) vsnprintf(string,length,format,operands);
343 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
344 n=(ssize_t) vsnprintf_l(string,length,format,locale,operands);
346 n=(ssize_t) vsnprintf_l(string,length,locale,format,operands);
349 #elif defined(MAGICKCORE_HAVE_VSNPRINTF)
350 #if defined(MAGICKCORE_HAVE_USELOCALE)
356 locale=AcquireCLocale();
357 if (locale == (locale_t) NULL)
358 n=(ssize_t) vsnprintf(string,length,format,operands);
361 previous_locale=uselocale(locale);
362 n=(ssize_t) vsnprintf(string,length,format,operands);
363 uselocale(previous_locale);
367 n=(ssize_t) vsnprintf(string,length,format,operands);
370 n=(ssize_t) vsprintf(string,format,operands);
373 string[length-1]='\0';
377 MagickExport ssize_t FormatLocaleString(char *restrict string,
378 const size_t length,const char *restrict format,...)
386 va_start(operands,format);
387 n=FormatLocaleStringList(string,length,format,operands);
393 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
397 + G e t L o c a l e I n f o _ %
401 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
403 % GetLocaleInfo_() searches the locale list for the specified tag and if
404 % found returns attributes for that element.
406 % The format of the GetLocaleInfo method is:
408 % const LocaleInfo *GetLocaleInfo_(const char *tag,
409 % ExceptionInfo *exception)
411 % A description of each parameter follows:
413 % o tag: the locale tag.
415 % o exception: return any errors or warnings in this structure.
418 MagickExport const LocaleInfo *GetLocaleInfo_(const char *tag,
419 ExceptionInfo *exception)
421 assert(exception != (ExceptionInfo *) NULL);
422 if ((locale_list == (SplayTreeInfo *) NULL) ||
423 (instantiate_locale == MagickFalse))
424 if (InitializeLocaleList(exception) == MagickFalse)
425 return((const LocaleInfo *) NULL);
426 if ((locale_list == (SplayTreeInfo *) NULL) ||
427 (GetNumberOfNodesInSplayTree(locale_list) == 0))
428 return((const LocaleInfo *) NULL);
429 if ((tag == (const char *) NULL) || (LocaleCompare(tag,"*") == 0))
431 ResetSplayTreeIterator(locale_list);
432 return((const LocaleInfo *) GetNextValueInSplayTree(locale_list));
434 return((const LocaleInfo *) GetValueFromSplayTree(locale_list,tag));
438 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
442 % G e t L o c a l e I n f o L i s t %
446 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
448 % GetLocaleInfoList() returns any locale messages that match the
451 % The format of the GetLocaleInfoList function is:
453 % const LocaleInfo **GetLocaleInfoList(const char *pattern,
454 % size_t *number_messages,ExceptionInfo *exception)
456 % A description of each parameter follows:
458 % o pattern: Specifies a pointer to a text string containing a pattern.
460 % o number_messages: This integer returns the number of locale messages in
463 % o exception: return any errors or warnings in this structure.
467 #if defined(__cplusplus) || defined(c_plusplus)
471 static int LocaleInfoCompare(const void *x,const void *y)
477 p=(const LocaleInfo **) x,
478 q=(const LocaleInfo **) y;
479 if (LocaleCompare((*p)->path,(*q)->path) == 0)
480 return(LocaleCompare((*p)->tag,(*q)->tag));
481 return(LocaleCompare((*p)->path,(*q)->path));
484 #if defined(__cplusplus) || defined(c_plusplus)
488 MagickExport const LocaleInfo **GetLocaleInfoList(const char *pattern,
489 size_t *number_messages,ExceptionInfo *exception)
494 register const LocaleInfo
501 Allocate locale list.
503 assert(pattern != (char *) NULL);
504 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
505 assert(number_messages != (size_t *) NULL);
507 p=GetLocaleInfo_("*",exception);
508 if (p == (const LocaleInfo *) NULL)
509 return((const LocaleInfo **) NULL);
510 messages=(const LocaleInfo **) AcquireQuantumMemory((size_t)
511 GetNumberOfNodesInSplayTree(locale_list)+1UL,sizeof(*messages));
512 if (messages == (const LocaleInfo **) NULL)
513 return((const LocaleInfo **) NULL);
515 Generate locale list.
517 LockSemaphoreInfo(locale_semaphore);
518 ResetSplayTreeIterator(locale_list);
519 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_list);
520 for (i=0; p != (const LocaleInfo *) NULL; )
522 if ((p->stealth == MagickFalse) &&
523 (GlobExpression(p->tag,pattern,MagickTrue) != MagickFalse))
525 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_list);
527 UnlockSemaphoreInfo(locale_semaphore);
528 qsort((void *) messages,(size_t) i,sizeof(*messages),LocaleInfoCompare);
529 messages[i]=(LocaleInfo *) NULL;
530 *number_messages=(size_t) i;
535 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
539 % G e t L o c a l e L i s t %
543 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
545 % GetLocaleList() returns any locale messages that match the specified
548 % The format of the GetLocaleList function is:
550 % char **GetLocaleList(const char *pattern,size_t *number_messages,
551 % Exceptioninfo *exception)
553 % A description of each parameter follows:
555 % o pattern: Specifies a pointer to a text string containing a pattern.
557 % o number_messages: This integer returns the number of messages in the
560 % o exception: return any errors or warnings in this structure.
564 #if defined(__cplusplus) || defined(c_plusplus)
568 static int LocaleTagCompare(const void *x,const void *y)
576 return(LocaleCompare(*p,*q));
579 #if defined(__cplusplus) || defined(c_plusplus)
583 MagickExport char **GetLocaleList(const char *pattern,
584 size_t *number_messages,ExceptionInfo *exception)
589 register const LocaleInfo
596 Allocate locale list.
598 assert(pattern != (char *) NULL);
599 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
600 assert(number_messages != (size_t *) NULL);
602 p=GetLocaleInfo_("*",exception);
603 if (p == (const LocaleInfo *) NULL)
604 return((char **) NULL);
605 messages=(char **) AcquireQuantumMemory((size_t)
606 GetNumberOfNodesInSplayTree(locale_list)+1UL,sizeof(*messages));
607 if (messages == (char **) NULL)
608 return((char **) NULL);
609 LockSemaphoreInfo(locale_semaphore);
610 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_list);
611 for (i=0; p != (const LocaleInfo *) NULL; )
613 if ((p->stealth == MagickFalse) &&
614 (GlobExpression(p->tag,pattern,MagickTrue) != MagickFalse))
615 messages[i++]=ConstantString(p->tag);
616 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_list);
618 UnlockSemaphoreInfo(locale_semaphore);
619 qsort((void *) messages,(size_t) i,sizeof(*messages),LocaleTagCompare);
620 messages[i]=(char *) NULL;
621 *number_messages=(size_t) i;
626 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
630 % G e t L o c a l e M e s s a g e %
634 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
636 % GetLocaleMessage() returns a message in the current locale that matches the
639 % The format of the GetLocaleMessage method is:
641 % const char *GetLocaleMessage(const char *tag)
643 % A description of each parameter follows:
645 % o tag: Return a message that matches this tag in the current locale.
648 MagickExport const char *GetLocaleMessage(const char *tag)
659 if ((tag == (const char *) NULL) || (*tag == '\0'))
661 exception=AcquireExceptionInfo();
662 (void) FormatLocaleString(name,MaxTextExtent,"%s/",tag);
663 locale_info=GetLocaleInfo_(name,exception);
664 exception=DestroyExceptionInfo(exception);
665 if (locale_info != (const LocaleInfo *) NULL)
666 return(locale_info->message);
671 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
675 % G e t L o c a l e O p t i o n s %
679 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
681 % GetLocaleOptions() returns any Magick configuration messages associated
682 % with the specified filename.
684 % The format of the GetLocaleOptions method is:
686 % LinkedListInfo *GetLocaleOptions(const char *filename,
687 % ExceptionInfo *exception)
689 % A description of each parameter follows:
691 % o filename: the locale file tag.
693 % o exception: return any errors or warnings in this structure.
696 MagickExport LinkedListInfo *GetLocaleOptions(const char *filename,
697 ExceptionInfo *exception)
712 assert(filename != (const char *) NULL);
713 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
714 assert(exception != (ExceptionInfo *) NULL);
715 (void) CopyMagickString(path,filename,MaxTextExtent);
717 Load XML from configuration files to linked-list.
719 messages=NewLinkedList(0);
720 paths=GetConfigurePaths(filename,exception);
721 if (paths != (LinkedListInfo *) NULL)
723 ResetLinkedListIterator(paths);
724 element=(const char *) GetNextValueInLinkedList(paths);
725 while (element != (const char *) NULL)
727 (void) FormatLocaleString(path,MaxTextExtent,"%s%s",element,filename);
728 (void) LogMagickEvent(LocaleEvent,GetMagickModule(),
729 "Searching for locale file: \"%s\"",path);
730 xml=ConfigureFileToStringInfo(path);
731 if (xml != (StringInfo *) NULL)
732 (void) AppendValueToLinkedList(messages,xml);
733 element=(const char *) GetNextValueInLinkedList(paths);
735 paths=DestroyLinkedList(paths,RelinquishMagickMemory);
737 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
742 blob=(char *) NTResourceToBlob(filename);
743 if (blob != (char *) NULL)
745 xml=StringToStringInfo(blob);
746 (void) AppendValueToLinkedList(messages,xml);
747 blob=DestroyString(blob);
751 ResetLinkedListIterator(messages);
756 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
760 % G e t L o c a l e V a l u e %
764 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
766 % GetLocaleValue() returns the message associated with the locale info.
768 % The format of the GetLocaleValue method is:
770 % const char *GetLocaleValue(const LocaleInfo *locale_info)
772 % A description of each parameter follows:
774 % o locale_info: The locale info.
777 MagickExport const char *GetLocaleValue(const LocaleInfo *locale_info)
779 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
780 assert(locale_info != (LocaleInfo *) NULL);
781 assert(locale_info->signature == MagickSignature);
782 return(locale_info->message);
786 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
790 + I n i t i a l i z e L o c a l e L i s t %
794 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
796 % InitializeLocaleList() initializes the locale list.
798 % The format of the InitializeLocaleList method is:
800 % MagickBooleanType InitializeLocaleList(ExceptionInfo *exception)
802 % A description of each parameter follows.
804 % o exception: return any errors or warnings in this structure.
807 static MagickBooleanType InitializeLocaleList(ExceptionInfo *exception)
809 if ((locale_list == (SplayTreeInfo *) NULL) &&
810 (instantiate_locale == MagickFalse))
812 if (locale_semaphore == (SemaphoreInfo *) NULL)
813 AcquireSemaphoreInfo(&locale_semaphore);
814 LockSemaphoreInfo(locale_semaphore);
815 if ((locale_list == (SplayTreeInfo *) NULL) &&
816 (instantiate_locale == MagickFalse))
824 locale=(char *) NULL;
825 p=setlocale(LC_CTYPE,(const char *) NULL);
826 if (p != (const char *) NULL)
827 locale=ConstantString(p);
828 if (locale == (char *) NULL)
829 locale=GetEnvironmentValue("LC_ALL");
830 if (locale == (char *) NULL)
831 locale=GetEnvironmentValue("LC_MESSAGES");
832 if (locale == (char *) NULL)
833 locale=GetEnvironmentValue("LC_CTYPE");
834 if (locale == (char *) NULL)
835 locale=GetEnvironmentValue("LANG");
836 if (locale == (char *) NULL)
837 locale=ConstantString("C");
838 (void) LoadLocaleLists(LocaleFilename,locale,exception);
839 locale=DestroyString(locale);
840 instantiate_locale=MagickTrue;
842 UnlockSemaphoreInfo(locale_semaphore);
844 return(locale_list != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
848 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
852 + I n t e r p r e t L o c a l e V a l u e %
856 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
858 % InterpretLocaleValue() interprets the string as a floating point number in
859 % the "C" locale and returns its value as a double. If sentinal is not a null
860 % pointer, the method also sets the value pointed by sentinal to point to the
861 % first character after the number.
863 % The format of the InterpretLocaleValue method is:
865 % double InterpretLocaleValue(const char *value,char **sentinal)
867 % A description of each parameter follows:
869 % o value: the string value.
871 % o sentinal: if sentinal is not NULL, a pointer to the character after the
872 % last character used in the conversion is stored in the location
873 % referenced by sentinal.
876 MagickExport double InterpretLocaleValue(const char *restrict string,
877 char **restrict sentinal)
882 #if defined(MAGICKCORE_HAVE_STRTOD_L)
887 locale=AcquireCLocale();
888 if (locale == (locale_t) NULL)
889 value=strtod(string,sentinal);
891 value=strtod_l(string,sentinal,locale);
894 value=strtod(string,sentinal);
900 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
904 % L i s t L o c a l e I n f o %
908 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
910 % ListLocaleInfo() lists the locale info to a file.
912 % The format of the ListLocaleInfo method is:
914 % MagickBooleanType ListLocaleInfo(FILE *file,ExceptionInfo *exception)
916 % A description of each parameter follows.
918 % o file: An pointer to a FILE.
920 % o exception: return any errors or warnings in this structure.
923 MagickExport MagickBooleanType ListLocaleInfo(FILE *file,
924 ExceptionInfo *exception)
938 if (file == (const FILE *) NULL)
941 locale_info=GetLocaleInfoList("*",&number_messages,exception);
942 if (locale_info == (const LocaleInfo **) NULL)
944 path=(const char *) NULL;
945 for (i=0; i < (ssize_t) number_messages; i++)
947 if (locale_info[i]->stealth != MagickFalse)
949 if ((path == (const char *) NULL) ||
950 (LocaleCompare(path,locale_info[i]->path) != 0))
952 if (locale_info[i]->path != (char *) NULL)
953 (void) FormatLocaleFile(file,"\nPath: %s\n\n",locale_info[i]->path);
954 (void) FormatLocaleFile(file,"Tag/Message\n");
955 (void) FormatLocaleFile(file,
956 "-------------------------------------------------"
957 "------------------------------\n");
959 path=locale_info[i]->path;
960 (void) FormatLocaleFile(file,"%s\n",locale_info[i]->tag);
961 if (locale_info[i]->message != (char *) NULL)
962 (void) FormatLocaleFile(file," %s",locale_info[i]->message);
963 (void) FormatLocaleFile(file,"\n");
966 locale_info=(const LocaleInfo **)
967 RelinquishMagickMemory((void *) locale_info);
972 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
976 + L o a d L o c a l e L i s t %
980 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
982 % LoadLocaleList() loads the locale configuration file which provides a mapping
983 % between locale attributes and a locale name.
985 % The format of the LoadLocaleList method is:
987 % MagickBooleanType LoadLocaleList(const char *xml,const char *filename,
988 % const size_t depth,ExceptionInfo *exception)
990 % A description of each parameter follows:
992 % o xml: The locale list in XML format.
994 % o filename: The locale list filename.
996 % o depth: depth of <include /> statements.
998 % o exception: return any errors or warnings in this structure.
1002 static void ChopLocaleComponents(char *path,const size_t components)
1012 p=path+strlen(path)-1;
1015 for (count=0; (count < (ssize_t) components) && (p > path); p--)
1021 if (count < (ssize_t) components)
1025 static void *DestroyLocaleNode(void *locale_info)
1030 p=(LocaleInfo *) locale_info;
1031 if (p->path != (char *) NULL)
1032 p->path=DestroyString(p->path);
1033 if (p->tag != (char *) NULL)
1034 p->tag=DestroyString(p->tag);
1035 if (p->message != (char *) NULL)
1036 p->message=DestroyString(p->message);
1037 return(RelinquishMagickMemory(p));
1040 static void LocaleFatalErrorHandler(
1041 const ExceptionType magick_unused(severity),
1042 const char *reason,const char *description)
1044 if (reason == (char *) NULL)
1046 (void) FormatLocaleFile(stderr,"%s: %s",GetClientName(),reason);
1047 if (description != (char *) NULL)
1048 (void) FormatLocaleFile(stderr," (%s)",description);
1049 (void) FormatLocaleFile(stderr,".\n");
1050 (void) fflush(stderr);
1055 static MagickBooleanType LoadLocaleList(const char *xml,const char *filename,
1056 const char *locale,const size_t depth,ExceptionInfo *exception)
1059 keyword[MaxTextExtent],
1060 message[MaxTextExtent],
1080 Read the locale configure file.
1082 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1083 "Loading locale configure file \"%s\" ...",filename);
1084 if (xml == (const char *) NULL)
1085 return(MagickFalse);
1086 if (locale_list == (SplayTreeInfo *) NULL)
1088 locale_list=NewSplayTree(CompareSplayTreeString,(void *(*)(void *)) NULL,
1090 if (locale_list == (SplayTreeInfo *) NULL)
1091 return(MagickFalse);
1094 locale_info=(LocaleInfo *) NULL;
1098 fatal_handler=SetFatalErrorHandler(LocaleFatalErrorHandler);
1099 token=AcquireString(xml);
1100 for (q=(char *) xml; *q != '\0'; )
1105 GetMagickToken(q,&q,token);
1108 (void) CopyMagickString(keyword,token,MaxTextExtent);
1109 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
1114 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
1116 GetMagickToken(q,&q,token);
1117 while (isspace((int) ((unsigned char) *q)) != 0)
1122 if (LocaleNCompare(keyword,"<!--",4) == 0)
1127 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
1129 GetMagickToken(q,&q,token);
1130 while (isspace((int) ((unsigned char) *q)) != 0)
1135 if (LocaleCompare(keyword,"<include") == 0)
1140 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
1142 (void) CopyMagickString(keyword,token,MaxTextExtent);
1143 GetMagickToken(q,&q,token);
1146 GetMagickToken(q,&q,token);
1147 if (LocaleCompare(keyword,"locale") == 0)
1149 if (LocaleCompare(locale,token) != 0)
1153 if (LocaleCompare(keyword,"file") == 0)
1156 (void) ThrowMagickException(exception,GetMagickModule(),
1157 ConfigureError,"IncludeElementNestedTooDeeply","`%s'",token);
1161 path[MaxTextExtent],
1165 GetPathComponent(filename,HeadPath,path);
1167 (void) ConcatenateMagickString(path,DirectorySeparator,
1169 if (*token == *DirectorySeparator)
1170 (void) CopyMagickString(path,token,MaxTextExtent);
1172 (void) ConcatenateMagickString(path,token,MaxTextExtent);
1173 xml=FileToString(path,~0,exception);
1174 if (xml != (char *) NULL)
1176 status=LoadLocaleList(xml,path,locale,depth+1,exception);
1177 xml=(char *) RelinquishMagickMemory(xml);
1184 if (LocaleCompare(keyword,"<locale") == 0)
1189 while ((*token != '>') && (*q != '\0'))
1191 (void) CopyMagickString(keyword,token,MaxTextExtent);
1192 GetMagickToken(q,&q,token);
1195 GetMagickToken(q,&q,token);
1199 if (LocaleCompare(keyword,"</locale>") == 0)
1201 ChopLocaleComponents(tag,1);
1202 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1205 if (LocaleCompare(keyword,"<localemap>") == 0)
1207 if (LocaleCompare(keyword,"</localemap>") == 0)
1209 if (LocaleCompare(keyword,"<message") == 0)
1214 while ((*token != '>') && (*q != '\0'))
1216 (void) CopyMagickString(keyword,token,MaxTextExtent);
1217 GetMagickToken(q,&q,token);
1220 GetMagickToken(q,&q,token);
1221 if (LocaleCompare(keyword,"name") == 0)
1223 (void) ConcatenateMagickString(tag,token,MaxTextExtent);
1224 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1227 for (p=(char *) q; (*q != '<') && (*q != '\0'); q++) ;
1228 while (isspace((int) ((unsigned char) *p)) != 0)
1231 while ((isspace((int) ((unsigned char) *q)) != 0) && (q > p))
1233 (void) CopyMagickString(message,p,(size_t) (q-p+2));
1234 locale_info=(LocaleInfo *) AcquireMagickMemory(sizeof(*locale_info));
1235 if (locale_info == (LocaleInfo *) NULL)
1236 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1237 (void) ResetMagickMemory(locale_info,0,sizeof(*locale_info));
1238 locale_info->path=ConstantString(filename);
1239 locale_info->tag=ConstantString(tag);
1240 locale_info->message=ConstantString(message);
1241 locale_info->signature=MagickSignature;
1242 status=AddValueToSplayTree(locale_list,locale_info->tag,locale_info);
1243 if (status == MagickFalse)
1244 (void) ThrowMagickException(exception,GetMagickModule(),
1245 ResourceLimitError,"MemoryAllocationFailed","`%s'",
1247 (void) ConcatenateMagickString(tag,message,MaxTextExtent);
1248 (void) ConcatenateMagickString(tag,"\n",MaxTextExtent);
1252 if (LocaleCompare(keyword,"</message>") == 0)
1254 ChopLocaleComponents(tag,2);
1255 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1258 if (*keyword == '<')
1263 if (*(keyword+1) == '?')
1265 if (*(keyword+1) == '/')
1267 ChopLocaleComponents(tag,1);
1269 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1272 token[strlen(token)-1]='\0';
1273 (void) CopyMagickString(token,token+1,MaxTextExtent);
1274 (void) ConcatenateMagickString(tag,token,MaxTextExtent);
1275 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1278 GetMagickToken(q,(const char **) NULL,token);
1282 token=(char *) RelinquishMagickMemory(token);
1283 (void) SetFatalErrorHandler(fatal_handler);
1288 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1292 % L o a d L o c a l e L i s t s %
1296 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1298 % LoadLocaleList() loads one or more locale configuration file which
1299 % provides a mapping between locale attributes and a locale tag.
1301 % The format of the LoadLocaleLists method is:
1303 % MagickBooleanType LoadLocaleLists(const char *filename,
1304 % ExceptionInfo *exception)
1306 % A description of each parameter follows:
1308 % o filename: the font file tag.
1310 % o locale: the actual locale.
1312 % o exception: return any errors or warnings in this structure.
1315 static MagickBooleanType LoadLocaleLists(const char *filename,
1316 const char *locale,ExceptionInfo *exception)
1318 #if defined(MAGICKCORE_EMBEDDABLE_SUPPORT)
1319 return(LoadLocaleList(LocaleMap,"built-in",locale,0,exception));
1331 options=GetLocaleOptions(filename,exception);
1332 option=(const StringInfo *) GetNextValueInLinkedList(options);
1333 while (option != (const StringInfo *) NULL)
1335 status|=LoadLocaleList((const char *) GetStringInfoDatum(option),
1336 GetStringInfoPath(option),locale,0,exception);
1337 option=(const StringInfo *) GetNextValueInLinkedList(options);
1339 options=DestroyLocaleOptions(options);
1340 if ((locale_list == (SplayTreeInfo *) NULL) ||
1341 (GetNumberOfNodesInSplayTree(locale_list) == 0))
1343 options=GetLocaleOptions("english.xml",exception);
1344 option=(const StringInfo *) GetNextValueInLinkedList(options);
1345 while (option != (const StringInfo *) NULL)
1347 status|=LoadLocaleList((const char *) GetStringInfoDatum(option),
1348 GetStringInfoPath(option),locale,0,exception);
1349 option=(const StringInfo *) GetNextValueInLinkedList(options);
1351 options=DestroyLocaleOptions(options);
1353 if ((locale_list == (SplayTreeInfo *) NULL) ||
1354 (GetNumberOfNodesInSplayTree(locale_list) == 0))
1355 status|=LoadLocaleList(LocaleMap,"built-in",locale,0,exception);
1356 return(status != 0 ? MagickTrue : MagickFalse);
1361 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1365 + L o c a l e C o m p o n e n t G e n e s i s %
1369 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1371 % LocaleComponentGenesis() instantiates the locale component.
1373 % The format of the LocaleComponentGenesis method is:
1375 % MagickBooleanType LocaleComponentGenesis(void)
1378 MagickExport MagickBooleanType LocaleComponentGenesis(void)
1380 AcquireSemaphoreInfo(&locale_semaphore);
1385 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1389 + L o c a l e C o m p o n e n t T e r m i n u s %
1393 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1395 % LocaleComponentTerminus() destroys the locale component.
1397 % The format of the LocaleComponentTerminus method is:
1399 % LocaleComponentTerminus(void)
1402 MagickExport void LocaleComponentTerminus(void)
1404 if (locale_semaphore == (SemaphoreInfo *) NULL)
1405 AcquireSemaphoreInfo(&locale_semaphore);
1406 LockSemaphoreInfo(locale_semaphore);
1407 #if defined(MAGICKCORE_HAVE_STRTOD_L)
1410 instantiate_locale=MagickFalse;
1411 UnlockSemaphoreInfo(locale_semaphore);
1412 DestroySemaphoreInfo(&locale_semaphore);