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 static volatile locale_t
94 c_locale = (locale_t) NULL;
96 static volatile MagickBooleanType
97 instantiate_locale = MagickFalse;
100 Forward declarations.
102 static MagickBooleanType
103 InitializeLocaleList(ExceptionInfo *),
104 LoadLocaleLists(const char *,const char *,ExceptionInfo *);
107 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
111 + A c q u i r e C L o c a l e %
115 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
117 % AcquireCLocale() allocates the C locale object, or (locale_t) 0 with
118 % errno set if it cannot be acquired.
120 % The format of the AcquireCLocale method is:
122 % locale_t AcquireCLocale(void)
125 static locale_t AcquireCLocale(void)
127 #if defined(MAGICKCORE_HAVE_NEWLOCALE)
128 if (c_locale == (locale_t) NULL)
129 c_locale=newlocale(LC_ALL_MASK,"C",(locale_t) 0);
130 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
131 if (c_locale == (locale_t) NULL)
132 c_locale=_create_locale(LC_ALL,"C");
138 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
142 + D e s t r o y C L o c a l e %
146 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
148 % DestroyCLocale() releases the resources allocated for a locale object
149 % returned by a call to the AcquireCLocale() method.
151 % The format of the DestroyCLocale method is:
153 % void DestroyCLocale(void)
156 static void DestroyCLocale(void)
158 #if defined(MAGICKCORE_HAVE_NEWLOCALE)
159 if (c_locale != (locale_t) NULL)
160 freelocale(c_locale);
161 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
162 if (c_locale != (locale_t) NULL)
163 _free_locale(c_locale);
165 c_locale=(locale_t) NULL;
169 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
173 % D e s t r o y L o c a l e O p t i o n s %
177 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
179 % DestroyLocaleOptions() releases memory associated with an locale
182 % The format of the DestroyProfiles method is:
184 % LinkedListInfo *DestroyLocaleOptions(Image *image)
186 % A description of each parameter follows:
188 % o image: the image.
192 static void *DestroyOptions(void *message)
194 return(DestroyStringInfo((StringInfo *) message));
197 MagickExport LinkedListInfo *DestroyLocaleOptions(LinkedListInfo *messages)
199 assert(messages != (LinkedListInfo *) NULL);
200 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
201 return(DestroyLinkedList(messages,DestroyOptions));
205 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
209 + F o r m a t L o c a l e F i l e %
213 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
215 % FormatLocaleFile() prints formatted output of a variable argument list to a
216 % file in the "C" locale.
218 % The format of the FormatLocaleFile method is:
220 % ssize_t FormatLocaleFile(FILE *file,const char *format,...)
222 % A description of each parameter follows.
226 % o format: A file describing the format to use to write the remaining
231 MagickExport ssize_t FormatLocaleFileList(FILE *file,
232 const char *restrict format,va_list operands)
237 #if defined(MAGICKCORE_HAVE_VFPRINTF_L)
242 locale=AcquireCLocale();
243 if (locale == (locale_t) NULL)
244 n=vfprintf(file,format,operands);
246 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
247 n=vfprintf_l(file,format,locale,operands);
249 n=vfprintf_l(file,locale,format,operands);
253 #if defined(MAGICKCORE_HAVE_USELOCALE)
259 locale=AcquireCLocale();
260 if (locale == (locale_t) NULL)
261 n=vfprintf(file,format,operands);
264 previous_locale=uselocale(locale);
265 n=vfprintf(file,format,operands);
266 uselocale(previous_locale);
270 n=fprintf(file,format,operands);
276 MagickExport ssize_t FormatLocaleFile(FILE *file,const char *restrict format,
285 va_start(operands,format);
286 n=(ssize_t) FormatLocaleFileList(file,format,operands);
292 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
296 + F o r m a t L o c a l e S t r i n g %
300 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
302 % FormatLocaleString() prints formatted output of a variable argument list to
303 % a string buffer in the "C" locale.
305 % The format of the FormatLocaleString method is:
307 % ssize_t FormatLocaleString(char *string,const size_t length,
308 % const char *format,...)
310 % A description of each parameter follows.
312 % o string: FormatLocaleString() returns the formatted string in this
315 % o length: the maximum length of the string.
317 % o format: A string describing the format to use to write the remaining
322 MagickExport ssize_t FormatLocaleStringList(char *restrict string,
323 const size_t length,const char *restrict format,va_list operands)
328 #if defined(MAGICKCORE_HAVE_VSNPRINTF_L)
333 locale=AcquireCLocale();
334 if (locale == (locale_t) NULL)
335 n=vsnprintf(string,length,format,operands);
337 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
338 n=vsnprintf_l(string,length,format,locale,operands);
340 n=vsnprintf_l(string,length,locale,format,operands);
343 #elif defined(MAGICKCORE_HAVE_VSNPRINTF)
344 #if defined(MAGICKCORE_HAVE_USELOCALE)
350 locale=AcquireCLocale();
351 if (locale == (locale_t) NULL)
352 n=vsnprintf(string,length,format,operands);
355 previous_locale=uselocale(locale);
356 n=vsnprintf(string,length,format,operands);
357 uselocale(previous_locale);
361 n=vsnprintf(string,length,format,operands);
364 n=vsprintf(string,format,operands);
367 string[length-1]='\0';
371 MagickExport ssize_t FormatLocaleString(char *restrict string,
372 const size_t length,const char *restrict format,...)
380 va_start(operands,format);
381 n=(ssize_t) FormatLocaleStringList(string,length,format,operands);
387 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
391 + G e t L o c a l e I n f o _ %
395 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
397 % GetLocaleInfo_() searches the locale list for the specified tag and if
398 % found returns attributes for that element.
400 % The format of the GetLocaleInfo method is:
402 % const LocaleInfo *GetLocaleInfo_(const char *tag,
403 % ExceptionInfo *exception)
405 % A description of each parameter follows:
407 % o tag: the locale tag.
409 % o exception: return any errors or warnings in this structure.
412 MagickExport const LocaleInfo *GetLocaleInfo_(const char *tag,
413 ExceptionInfo *exception)
415 assert(exception != (ExceptionInfo *) NULL);
416 if ((locale_list == (SplayTreeInfo *) NULL) ||
417 (instantiate_locale == MagickFalse))
418 if (InitializeLocaleList(exception) == MagickFalse)
419 return((const LocaleInfo *) NULL);
420 if ((locale_list == (SplayTreeInfo *) NULL) ||
421 (GetNumberOfNodesInSplayTree(locale_list) == 0))
422 return((const LocaleInfo *) NULL);
423 if ((tag == (const char *) NULL) || (LocaleCompare(tag,"*") == 0))
425 ResetSplayTreeIterator(locale_list);
426 return((const LocaleInfo *) GetNextValueInSplayTree(locale_list));
428 return((const LocaleInfo *) GetValueFromSplayTree(locale_list,tag));
432 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
436 % G e t L o c a l e I n f o L i s t %
440 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
442 % GetLocaleInfoList() returns any locale messages that match the
445 % The format of the GetLocaleInfoList function is:
447 % const LocaleInfo **GetLocaleInfoList(const char *pattern,
448 % size_t *number_messages,ExceptionInfo *exception)
450 % A description of each parameter follows:
452 % o pattern: Specifies a pointer to a text string containing a pattern.
454 % o number_messages: This integer returns the number of locale messages in
457 % o exception: return any errors or warnings in this structure.
461 #if defined(__cplusplus) || defined(c_plusplus)
465 static int LocaleInfoCompare(const void *x,const void *y)
471 p=(const LocaleInfo **) x,
472 q=(const LocaleInfo **) y;
473 if (LocaleCompare((*p)->path,(*q)->path) == 0)
474 return(LocaleCompare((*p)->tag,(*q)->tag));
475 return(LocaleCompare((*p)->path,(*q)->path));
478 #if defined(__cplusplus) || defined(c_plusplus)
482 MagickExport const LocaleInfo **GetLocaleInfoList(const char *pattern,
483 size_t *number_messages,ExceptionInfo *exception)
488 register const LocaleInfo
495 Allocate locale list.
497 assert(pattern != (char *) NULL);
498 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
499 assert(number_messages != (size_t *) NULL);
501 p=GetLocaleInfo_("*",exception);
502 if (p == (const LocaleInfo *) NULL)
503 return((const LocaleInfo **) NULL);
504 messages=(const LocaleInfo **) AcquireQuantumMemory((size_t)
505 GetNumberOfNodesInSplayTree(locale_list)+1UL,sizeof(*messages));
506 if (messages == (const LocaleInfo **) NULL)
507 return((const LocaleInfo **) NULL);
509 Generate locale list.
511 LockSemaphoreInfo(locale_semaphore);
512 ResetSplayTreeIterator(locale_list);
513 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_list);
514 for (i=0; p != (const LocaleInfo *) NULL; )
516 if ((p->stealth == MagickFalse) &&
517 (GlobExpression(p->tag,pattern,MagickTrue) != MagickFalse))
519 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_list);
521 UnlockSemaphoreInfo(locale_semaphore);
522 qsort((void *) messages,(size_t) i,sizeof(*messages),LocaleInfoCompare);
523 messages[i]=(LocaleInfo *) NULL;
524 *number_messages=(size_t) i;
529 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
533 % G e t L o c a l e L i s t %
537 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
539 % GetLocaleList() returns any locale messages that match the specified
542 % The format of the GetLocaleList function is:
544 % char **GetLocaleList(const char *pattern,size_t *number_messages,
545 % Exceptioninfo *exception)
547 % A description of each parameter follows:
549 % o pattern: Specifies a pointer to a text string containing a pattern.
551 % o number_messages: This integer returns the number of messages in the
554 % o exception: return any errors or warnings in this structure.
558 #if defined(__cplusplus) || defined(c_plusplus)
562 static int LocaleTagCompare(const void *x,const void *y)
570 return(LocaleCompare(*p,*q));
573 #if defined(__cplusplus) || defined(c_plusplus)
577 MagickExport char **GetLocaleList(const char *pattern,
578 size_t *number_messages,ExceptionInfo *exception)
583 register const LocaleInfo
590 Allocate locale list.
592 assert(pattern != (char *) NULL);
593 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
594 assert(number_messages != (size_t *) NULL);
596 p=GetLocaleInfo_("*",exception);
597 if (p == (const LocaleInfo *) NULL)
598 return((char **) NULL);
599 messages=(char **) AcquireQuantumMemory((size_t)
600 GetNumberOfNodesInSplayTree(locale_list)+1UL,sizeof(*messages));
601 if (messages == (char **) NULL)
602 return((char **) NULL);
603 LockSemaphoreInfo(locale_semaphore);
604 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_list);
605 for (i=0; p != (const LocaleInfo *) NULL; )
607 if ((p->stealth == MagickFalse) &&
608 (GlobExpression(p->tag,pattern,MagickTrue) != MagickFalse))
609 messages[i++]=ConstantString(p->tag);
610 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_list);
612 UnlockSemaphoreInfo(locale_semaphore);
613 qsort((void *) messages,(size_t) i,sizeof(*messages),LocaleTagCompare);
614 messages[i]=(char *) NULL;
615 *number_messages=(size_t) i;
620 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
624 % G e t L o c a l e M e s s a g e %
628 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
630 % GetLocaleMessage() returns a message in the current locale that matches the
633 % The format of the GetLocaleMessage method is:
635 % const char *GetLocaleMessage(const char *tag)
637 % A description of each parameter follows:
639 % o tag: Return a message that matches this tag in the current locale.
642 MagickExport const char *GetLocaleMessage(const char *tag)
653 if ((tag == (const char *) NULL) || (*tag == '\0'))
655 exception=AcquireExceptionInfo();
656 (void) FormatLocaleString(name,MaxTextExtent,"%s/",tag);
657 locale_info=GetLocaleInfo_(name,exception);
658 exception=DestroyExceptionInfo(exception);
659 if (locale_info != (const LocaleInfo *) NULL)
660 return(locale_info->message);
665 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
669 % G e t L o c a l e O p t i o n s %
673 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
675 % GetLocaleOptions() returns any Magick configuration messages associated
676 % with the specified filename.
678 % The format of the GetLocaleOptions method is:
680 % LinkedListInfo *GetLocaleOptions(const char *filename,
681 % ExceptionInfo *exception)
683 % A description of each parameter follows:
685 % o filename: the locale file tag.
687 % o exception: return any errors or warnings in this structure.
690 MagickExport LinkedListInfo *GetLocaleOptions(const char *filename,
691 ExceptionInfo *exception)
706 assert(filename != (const char *) NULL);
707 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
708 assert(exception != (ExceptionInfo *) NULL);
709 (void) CopyMagickString(path,filename,MaxTextExtent);
711 Load XML from configuration files to linked-list.
713 messages=NewLinkedList(0);
714 paths=GetConfigurePaths(filename,exception);
715 if (paths != (LinkedListInfo *) NULL)
717 ResetLinkedListIterator(paths);
718 element=(const char *) GetNextValueInLinkedList(paths);
719 while (element != (const char *) NULL)
721 (void) FormatLocaleString(path,MaxTextExtent,"%s%s",element,filename);
722 (void) LogMagickEvent(LocaleEvent,GetMagickModule(),
723 "Searching for locale file: \"%s\"",path);
724 xml=ConfigureFileToStringInfo(path);
725 if (xml != (StringInfo *) NULL)
726 (void) AppendValueToLinkedList(messages,xml);
727 element=(const char *) GetNextValueInLinkedList(paths);
729 paths=DestroyLinkedList(paths,RelinquishMagickMemory);
731 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
736 blob=(char *) NTResourceToBlob(filename);
737 if (blob != (char *) NULL)
739 xml=StringToStringInfo(blob);
740 (void) AppendValueToLinkedList(messages,xml);
741 blob=DestroyString(blob);
745 ResetLinkedListIterator(messages);
750 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
754 % G e t L o c a l e V a l u e %
758 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
760 % GetLocaleValue() returns the message associated with the locale info.
762 % The format of the GetLocaleValue method is:
764 % const char *GetLocaleValue(const LocaleInfo *locale_info)
766 % A description of each parameter follows:
768 % o locale_info: The locale info.
771 MagickExport const char *GetLocaleValue(const LocaleInfo *locale_info)
773 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
774 assert(locale_info != (LocaleInfo *) NULL);
775 assert(locale_info->signature == MagickSignature);
776 return(locale_info->message);
780 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
784 + I n i t i a l i z e L o c a l e L i s t %
788 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
790 % InitializeLocaleList() initializes the locale list.
792 % The format of the InitializeLocaleList method is:
794 % MagickBooleanType InitializeLocaleList(ExceptionInfo *exception)
796 % A description of each parameter follows.
798 % o exception: return any errors or warnings in this structure.
801 static MagickBooleanType InitializeLocaleList(ExceptionInfo *exception)
803 if ((locale_list == (SplayTreeInfo *) NULL) &&
804 (instantiate_locale == MagickFalse))
806 if (locale_semaphore == (SemaphoreInfo *) NULL)
807 AcquireSemaphoreInfo(&locale_semaphore);
808 LockSemaphoreInfo(locale_semaphore);
809 if ((locale_list == (SplayTreeInfo *) NULL) &&
810 (instantiate_locale == MagickFalse))
818 locale=(char *) NULL;
819 p=setlocale(LC_CTYPE,(const char *) NULL);
820 if (p != (const char *) NULL)
821 locale=ConstantString(p);
822 if (locale == (char *) NULL)
823 locale=GetEnvironmentValue("LC_ALL");
824 if (locale == (char *) NULL)
825 locale=GetEnvironmentValue("LC_MESSAGES");
826 if (locale == (char *) NULL)
827 locale=GetEnvironmentValue("LC_CTYPE");
828 if (locale == (char *) NULL)
829 locale=GetEnvironmentValue("LANG");
830 if (locale == (char *) NULL)
831 locale=ConstantString("C");
832 (void) LoadLocaleLists(LocaleFilename,locale,exception);
833 locale=DestroyString(locale);
834 instantiate_locale=MagickTrue;
836 UnlockSemaphoreInfo(locale_semaphore);
838 return(locale_list != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
842 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
846 + I n t e r p r e t L o c a l e V a l u e %
850 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
852 % InterpretLocaleValue() interprets the string as a floating point number in
853 % the "C" locale and returns its value as a double. If sentinal is not a null
854 % pointer, the method also sets the value pointed by sentinal to point to the
855 % first character after the number.
857 % The format of the InterpretLocaleValue method is:
859 % double InterpretLocaleValue(const char *value,char **sentinal)
861 % A description of each parameter follows:
863 % o value: the string value.
865 % o sentinal: if sentinal is not NULL, a pointer to the character after the
866 % last character used in the conversion is stored in the location
867 % referenced by sentinal.
870 MagickExport double InterpretLocaleValue(const char *restrict string,
871 char **restrict sentinal)
876 #if defined(MAGICKCORE_HAVE_STRTOD_L)
881 locale=AcquireCLocale();
882 if (locale == (locale_t) NULL)
883 value=strtod(string,sentinal);
885 value=strtod_l(string,sentinal,locale);
888 value=strtod(string,sentinal);
894 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
898 % L i s t L o c a l e I n f o %
902 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
904 % ListLocaleInfo() lists the locale info to a file.
906 % The format of the ListLocaleInfo method is:
908 % MagickBooleanType ListLocaleInfo(FILE *file,ExceptionInfo *exception)
910 % A description of each parameter follows.
912 % o file: An pointer to a FILE.
914 % o exception: return any errors or warnings in this structure.
917 MagickExport MagickBooleanType ListLocaleInfo(FILE *file,
918 ExceptionInfo *exception)
932 if (file == (const FILE *) NULL)
935 locale_info=GetLocaleInfoList("*",&number_messages,exception);
936 if (locale_info == (const LocaleInfo **) NULL)
938 path=(const char *) NULL;
939 for (i=0; i < (ssize_t) number_messages; i++)
941 if (locale_info[i]->stealth != MagickFalse)
943 if ((path == (const char *) NULL) ||
944 (LocaleCompare(path,locale_info[i]->path) != 0))
946 if (locale_info[i]->path != (char *) NULL)
947 (void) FormatLocaleFile(file,"\nPath: %s\n\n",locale_info[i]->path);
948 (void) FormatLocaleFile(file,"Tag/Message\n");
949 (void) FormatLocaleFile(file,
950 "-------------------------------------------------"
951 "------------------------------\n");
953 path=locale_info[i]->path;
954 (void) FormatLocaleFile(file,"%s\n",locale_info[i]->tag);
955 if (locale_info[i]->message != (char *) NULL)
956 (void) FormatLocaleFile(file," %s",locale_info[i]->message);
957 (void) FormatLocaleFile(file,"\n");
960 locale_info=(const LocaleInfo **)
961 RelinquishMagickMemory((void *) locale_info);
966 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
970 + L o a d L o c a l e L i s t %
974 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
976 % LoadLocaleList() loads the locale configuration file which provides a mapping
977 % between locale attributes and a locale name.
979 % The format of the LoadLocaleList method is:
981 % MagickBooleanType LoadLocaleList(const char *xml,const char *filename,
982 % const size_t depth,ExceptionInfo *exception)
984 % A description of each parameter follows:
986 % o xml: The locale list in XML format.
988 % o filename: The locale list filename.
990 % o depth: depth of <include /> statements.
992 % o exception: return any errors or warnings in this structure.
996 static void ChopLocaleComponents(char *path,const size_t components)
1006 p=path+strlen(path)-1;
1009 for (count=0; (count < (ssize_t) components) && (p > path); p--)
1015 if (count < (ssize_t) components)
1019 static void *DestroyLocaleNode(void *locale_info)
1024 p=(LocaleInfo *) locale_info;
1025 if (p->path != (char *) NULL)
1026 p->path=DestroyString(p->path);
1027 if (p->tag != (char *) NULL)
1028 p->tag=DestroyString(p->tag);
1029 if (p->message != (char *) NULL)
1030 p->message=DestroyString(p->message);
1031 return(RelinquishMagickMemory(p));
1034 static void LocaleFatalErrorHandler(
1035 const ExceptionType magick_unused(severity),
1036 const char *reason,const char *description)
1038 if (reason == (char *) NULL)
1040 (void) FormatLocaleFile(stderr,"%s: %s",GetClientName(),reason);
1041 if (description != (char *) NULL)
1042 (void) FormatLocaleFile(stderr," (%s)",description);
1043 (void) FormatLocaleFile(stderr,".\n");
1044 (void) fflush(stderr);
1049 static MagickBooleanType LoadLocaleList(const char *xml,const char *filename,
1050 const char *locale,const size_t depth,ExceptionInfo *exception)
1053 keyword[MaxTextExtent],
1054 message[MaxTextExtent],
1074 Read the locale configure file.
1076 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1077 "Loading locale configure file \"%s\" ...",filename);
1078 if (xml == (const char *) NULL)
1079 return(MagickFalse);
1080 if (locale_list == (SplayTreeInfo *) NULL)
1082 locale_list=NewSplayTree(CompareSplayTreeString,(void *(*)(void *)) NULL,
1084 if (locale_list == (SplayTreeInfo *) NULL)
1085 return(MagickFalse);
1088 locale_info=(LocaleInfo *) NULL;
1092 fatal_handler=SetFatalErrorHandler(LocaleFatalErrorHandler);
1093 token=AcquireString(xml);
1094 for (q=(char *) xml; *q != '\0'; )
1099 GetMagickToken(q,&q,token);
1102 (void) CopyMagickString(keyword,token,MaxTextExtent);
1103 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
1108 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
1110 GetMagickToken(q,&q,token);
1111 while (isspace((int) ((unsigned char) *q)) != 0)
1116 if (LocaleNCompare(keyword,"<!--",4) == 0)
1121 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
1123 GetMagickToken(q,&q,token);
1124 while (isspace((int) ((unsigned char) *q)) != 0)
1129 if (LocaleCompare(keyword,"<include") == 0)
1134 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
1136 (void) CopyMagickString(keyword,token,MaxTextExtent);
1137 GetMagickToken(q,&q,token);
1140 GetMagickToken(q,&q,token);
1141 if (LocaleCompare(keyword,"locale") == 0)
1143 if (LocaleCompare(locale,token) != 0)
1147 if (LocaleCompare(keyword,"file") == 0)
1150 (void) ThrowMagickException(exception,GetMagickModule(),
1151 ConfigureError,"IncludeElementNestedTooDeeply","`%s'",token);
1155 path[MaxTextExtent],
1159 GetPathComponent(filename,HeadPath,path);
1161 (void) ConcatenateMagickString(path,DirectorySeparator,
1163 if (*token == *DirectorySeparator)
1164 (void) CopyMagickString(path,token,MaxTextExtent);
1166 (void) ConcatenateMagickString(path,token,MaxTextExtent);
1167 xml=FileToString(path,~0,exception);
1168 if (xml != (char *) NULL)
1170 status=LoadLocaleList(xml,path,locale,depth+1,exception);
1171 xml=(char *) RelinquishMagickMemory(xml);
1178 if (LocaleCompare(keyword,"<locale") == 0)
1183 while ((*token != '>') && (*q != '\0'))
1185 (void) CopyMagickString(keyword,token,MaxTextExtent);
1186 GetMagickToken(q,&q,token);
1189 GetMagickToken(q,&q,token);
1193 if (LocaleCompare(keyword,"</locale>") == 0)
1195 ChopLocaleComponents(tag,1);
1196 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1199 if (LocaleCompare(keyword,"<localemap>") == 0)
1201 if (LocaleCompare(keyword,"</localemap>") == 0)
1203 if (LocaleCompare(keyword,"<message") == 0)
1208 while ((*token != '>') && (*q != '\0'))
1210 (void) CopyMagickString(keyword,token,MaxTextExtent);
1211 GetMagickToken(q,&q,token);
1214 GetMagickToken(q,&q,token);
1215 if (LocaleCompare(keyword,"name") == 0)
1217 (void) ConcatenateMagickString(tag,token,MaxTextExtent);
1218 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1221 for (p=(char *) q; (*q != '<') && (*q != '\0'); q++) ;
1222 while (isspace((int) ((unsigned char) *p)) != 0)
1225 while ((isspace((int) ((unsigned char) *q)) != 0) && (q > p))
1227 (void) CopyMagickString(message,p,(size_t) (q-p+2));
1228 locale_info=(LocaleInfo *) AcquireMagickMemory(sizeof(*locale_info));
1229 if (locale_info == (LocaleInfo *) NULL)
1230 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1231 (void) ResetMagickMemory(locale_info,0,sizeof(*locale_info));
1232 locale_info->path=ConstantString(filename);
1233 locale_info->tag=ConstantString(tag);
1234 locale_info->message=ConstantString(message);
1235 locale_info->signature=MagickSignature;
1236 status=AddValueToSplayTree(locale_list,locale_info->tag,locale_info);
1237 if (status == MagickFalse)
1238 (void) ThrowMagickException(exception,GetMagickModule(),
1239 ResourceLimitError,"MemoryAllocationFailed","`%s'",
1241 (void) ConcatenateMagickString(tag,message,MaxTextExtent);
1242 (void) ConcatenateMagickString(tag,"\n",MaxTextExtent);
1246 if (LocaleCompare(keyword,"</message>") == 0)
1248 ChopLocaleComponents(tag,2);
1249 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1252 if (*keyword == '<')
1257 if (*(keyword+1) == '?')
1259 if (*(keyword+1) == '/')
1261 ChopLocaleComponents(tag,1);
1263 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1266 token[strlen(token)-1]='\0';
1267 (void) CopyMagickString(token,token+1,MaxTextExtent);
1268 (void) ConcatenateMagickString(tag,token,MaxTextExtent);
1269 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1272 GetMagickToken(q,(const char **) NULL,token);
1276 token=(char *) RelinquishMagickMemory(token);
1277 (void) SetFatalErrorHandler(fatal_handler);
1282 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1286 % L o a d L o c a l e L i s t s %
1290 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1292 % LoadLocaleList() loads one or more locale configuration file which
1293 % provides a mapping between locale attributes and a locale tag.
1295 % The format of the LoadLocaleLists method is:
1297 % MagickBooleanType LoadLocaleLists(const char *filename,
1298 % ExceptionInfo *exception)
1300 % A description of each parameter follows:
1302 % o filename: the font file tag.
1304 % o locale: the actual locale.
1306 % o exception: return any errors or warnings in this structure.
1309 static MagickBooleanType LoadLocaleLists(const char *filename,
1310 const char *locale,ExceptionInfo *exception)
1312 #if defined(MAGICKCORE_EMBEDDABLE_SUPPORT)
1313 return(LoadLocaleList(LocaleMap,"built-in",locale,0,exception));
1325 options=GetLocaleOptions(filename,exception);
1326 option=(const StringInfo *) GetNextValueInLinkedList(options);
1327 while (option != (const StringInfo *) NULL)
1329 status|=LoadLocaleList((const char *) GetStringInfoDatum(option),
1330 GetStringInfoPath(option),locale,0,exception);
1331 option=(const StringInfo *) GetNextValueInLinkedList(options);
1333 options=DestroyLocaleOptions(options);
1334 if ((locale_list == (SplayTreeInfo *) NULL) ||
1335 (GetNumberOfNodesInSplayTree(locale_list) == 0))
1337 options=GetLocaleOptions("english.xml",exception);
1338 option=(const StringInfo *) GetNextValueInLinkedList(options);
1339 while (option != (const StringInfo *) NULL)
1341 status|=LoadLocaleList((const char *) GetStringInfoDatum(option),
1342 GetStringInfoPath(option),locale,0,exception);
1343 option=(const StringInfo *) GetNextValueInLinkedList(options);
1345 options=DestroyLocaleOptions(options);
1347 if ((locale_list == (SplayTreeInfo *) NULL) ||
1348 (GetNumberOfNodesInSplayTree(locale_list) == 0))
1349 status|=LoadLocaleList(LocaleMap,"built-in",locale,0,exception);
1350 return(status != 0 ? MagickTrue : MagickFalse);
1355 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1359 + L o c a l e C o m p o n e n t G e n e s i s %
1363 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1365 % LocaleComponentGenesis() instantiates the locale component.
1367 % The format of the LocaleComponentGenesis method is:
1369 % MagickBooleanType LocaleComponentGenesis(void)
1372 MagickExport MagickBooleanType LocaleComponentGenesis(void)
1374 AcquireSemaphoreInfo(&locale_semaphore);
1379 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1383 + L o c a l e C o m p o n e n t T e r m i n u s %
1387 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1389 % LocaleComponentTerminus() destroys the locale component.
1391 % The format of the LocaleComponentTerminus method is:
1393 % LocaleComponentTerminus(void)
1396 MagickExport void LocaleComponentTerminus(void)
1398 if (locale_semaphore == (SemaphoreInfo *) NULL)
1399 AcquireSemaphoreInfo(&locale_semaphore);
1400 LockSemaphoreInfo(locale_semaphore);
1402 instantiate_locale=MagickFalse;
1403 UnlockSemaphoreInfo(locale_semaphore);
1404 DestroySemaphoreInfo(&locale_semaphore);