2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % L OOO CCCC AAA L EEEEE %
8 % L O O C AAAAA L EEE %
10 % LLLLL OOO CCCC A A LLLLL EEEEE %
13 % MagickCore Image Locale Methods %
20 % Copyright 1999-2012 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #include "MagickCore/studio.h"
43 #include "MagickCore/blob.h"
44 #include "MagickCore/client.h"
45 #include "MagickCore/configure.h"
46 #include "MagickCore/exception.h"
47 #include "MagickCore/exception-private.h"
48 #include "MagickCore/hashmap.h"
49 #include "MagickCore/locale_.h"
50 #include "MagickCore/locale-private.h"
51 #include "MagickCore/log.h"
52 #include "MagickCore/memory_.h"
53 #include "MagickCore/nt-base-private.h"
54 #include "MagickCore/semaphore.h"
55 #include "MagickCore/splay-tree.h"
56 #include "MagickCore/string_.h"
57 #include "MagickCore/string-private.h"
58 #include "MagickCore/token.h"
59 #include "MagickCore/utility.h"
60 #include "MagickCore/utility-private.h"
61 #include "MagickCore/xml-tree.h"
66 #define LocaleFilename "locale.xml"
67 #define MaxRecursionDepth 200
72 #if defined(__CYGWIN__)
73 typedef struct _locale_t *locale_t;
81 "<?xml version=\"1.0\"?>"
83 " <locale name=\"C\">"
85 " <Message name=\"\">"
92 *locale_semaphore = (SemaphoreInfo *) NULL;
95 *locale_list = (SplayTreeInfo *) NULL;
97 #if defined(MAGICKCORE_HAVE_STRTOD_L)
98 static volatile locale_t
99 c_locale = (locale_t) NULL;
102 static volatile MagickBooleanType
103 instantiate_locale = MagickFalse;
106 Forward declarations.
108 static MagickBooleanType
109 InitializeLocaleList(ExceptionInfo *),
110 LoadLocaleLists(const char *,const char *,ExceptionInfo *);
112 #if defined(MAGICKCORE_HAVE_STRTOD_L)
114 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
118 + A c q u i r e C L o c a l e %
122 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
124 % AcquireCLocale() allocates the C locale object, or (locale_t) 0 with
125 % errno set if it cannot be acquired.
127 % The format of the AcquireCLocale method is:
129 % locale_t AcquireCLocale(void)
132 static locale_t AcquireCLocale(void)
134 #if defined(MAGICKCORE_HAVE_NEWLOCALE)
135 if (c_locale == (locale_t) NULL)
136 c_locale=newlocale(LC_ALL_MASK,"C",(locale_t) 0);
137 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
138 if (c_locale == (locale_t) NULL)
139 c_locale=_create_locale(LC_ALL,"C");
145 #if defined(MAGICKCORE_HAVE_STRTOD_L)
147 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
151 + D e s t r o y C L o c a l e %
155 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
157 % DestroyCLocale() releases the resources allocated for a locale object
158 % returned by a call to the AcquireCLocale() method.
160 % The format of the DestroyCLocale method is:
162 % void DestroyCLocale(void)
165 static void DestroyCLocale(void)
167 #if defined(MAGICKCORE_HAVE_NEWLOCALE)
168 if (c_locale != (locale_t) NULL)
169 freelocale(c_locale);
170 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
171 if (c_locale != (locale_t) NULL)
172 _free_locale(c_locale);
174 c_locale=(locale_t) NULL;
179 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
183 % D e s t r o y L o c a l e O p t i o n s %
187 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
189 % DestroyLocaleOptions() releases memory associated with an locale
192 % The format of the DestroyProfiles method is:
194 % LinkedListInfo *DestroyLocaleOptions(Image *image)
196 % A description of each parameter follows:
198 % o image: the image.
202 static void *DestroyOptions(void *message)
204 return(DestroyStringInfo((StringInfo *) message));
207 MagickExport LinkedListInfo *DestroyLocaleOptions(LinkedListInfo *messages)
209 assert(messages != (LinkedListInfo *) NULL);
210 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
211 return(DestroyLinkedList(messages,DestroyOptions));
215 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
219 + F o r m a t L o c a l e F i l e %
223 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
225 % FormatLocaleFile() prints formatted output of a variable argument list to a
226 % file in the "C" locale.
228 % The format of the FormatLocaleFile method is:
230 % ssize_t FormatLocaleFile(FILE *file,const char *format,...)
232 % A description of each parameter follows.
236 % o format: A file describing the format to use to write the remaining
241 MagickPrivate ssize_t FormatLocaleFileList(FILE *file,
242 const char *restrict format,va_list operands)
247 #if defined(MAGICKCORE_HAVE_VFPRINTF_L)
252 locale=AcquireCLocale();
253 if (locale == (locale_t) NULL)
254 n=(ssize_t) vfprintf(file,format,operands);
256 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
257 n=(ssize_t) vfprintf_l(file,format,locale,operands);
259 n=(ssize_t) vfprintf_l(file,locale,format,operands);
263 #if defined(MAGICKCORE_HAVE_USELOCALE)
269 locale=AcquireCLocale();
270 if (locale == (locale_t) NULL)
271 n=(ssize_t) vfprintf(file,format,operands);
274 previous_locale=uselocale(locale);
275 n=(ssize_t) vfprintf(file,format,operands);
276 uselocale(previous_locale);
280 n=(ssize_t) vfprintf(file,format,operands);
286 MagickExport ssize_t FormatLocaleFile(FILE *file,const char *restrict format,
295 va_start(operands,format);
296 n=FormatLocaleFileList(file,format,operands);
302 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
306 + F o r m a t L o c a l e S t r i n g %
310 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
312 % FormatLocaleString() prints formatted output of a variable argument list to
313 % a string buffer in the "C" locale.
315 % The format of the FormatLocaleString method is:
317 % ssize_t FormatLocaleString(char *string,const size_t length,
318 % const char *format,...)
320 % A description of each parameter follows.
322 % o string: FormatLocaleString() returns the formatted string in this
325 % o length: the maximum length of the string.
327 % o format: A string describing the format to use to write the remaining
332 MagickPrivate ssize_t FormatLocaleStringList(char *restrict string,
333 const size_t length,const char *restrict format,va_list operands)
338 #if defined(MAGICKCORE_HAVE_VSNPRINTF_L)
343 locale=AcquireCLocale();
344 if (locale == (locale_t) NULL)
345 n=(ssize_t) vsnprintf(string,length,format,operands);
347 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
348 n=(ssize_t) vsnprintf_l(string,length,format,locale,operands);
350 n=(ssize_t) vsnprintf_l(string,length,locale,format,operands);
353 #elif defined(MAGICKCORE_HAVE_VSNPRINTF)
354 #if defined(MAGICKCORE_HAVE_USELOCALE)
360 locale=AcquireCLocale();
361 if (locale == (locale_t) NULL)
362 n=(ssize_t) vsnprintf(string,length,format,operands);
365 previous_locale=uselocale(locale);
366 n=(ssize_t) vsnprintf(string,length,format,operands);
367 uselocale(previous_locale);
371 n=(ssize_t) vsnprintf(string,length,format,operands);
374 n=(ssize_t) vsprintf(string,format,operands);
377 string[length-1]='\0';
381 MagickExport ssize_t FormatLocaleString(char *restrict string,
382 const size_t length,const char *restrict format,...)
390 va_start(operands,format);
391 n=FormatLocaleStringList(string,length,format,operands);
397 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
401 + G e t L o c a l e I n f o _ %
405 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
407 % GetLocaleInfo_() searches the locale list for the specified tag and if
408 % found returns attributes for that element.
410 % The format of the GetLocaleInfo method is:
412 % const LocaleInfo *GetLocaleInfo_(const char *tag,
413 % ExceptionInfo *exception)
415 % A description of each parameter follows:
417 % o tag: the locale tag.
419 % o exception: return any errors or warnings in this structure.
422 MagickExport const LocaleInfo *GetLocaleInfo_(const char *tag,
423 ExceptionInfo *exception)
425 assert(exception != (ExceptionInfo *) NULL);
426 if ((locale_list == (SplayTreeInfo *) NULL) ||
427 (instantiate_locale == MagickFalse))
428 if (InitializeLocaleList(exception) == MagickFalse)
429 return((const LocaleInfo *) NULL);
430 if ((locale_list == (SplayTreeInfo *) NULL) ||
431 (GetNumberOfNodesInSplayTree(locale_list) == 0))
432 return((const LocaleInfo *) NULL);
433 if ((tag == (const char *) NULL) || (LocaleCompare(tag,"*") == 0))
435 ResetSplayTreeIterator(locale_list);
436 return((const LocaleInfo *) GetNextValueInSplayTree(locale_list));
438 return((const LocaleInfo *) GetValueFromSplayTree(locale_list,tag));
442 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
446 % G e t L o c a l e I n f o L i s t %
450 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
452 % GetLocaleInfoList() returns any locale messages that match the
455 % The format of the GetLocaleInfoList function is:
457 % const LocaleInfo **GetLocaleInfoList(const char *pattern,
458 % size_t *number_messages,ExceptionInfo *exception)
460 % A description of each parameter follows:
462 % o pattern: Specifies a pointer to a text string containing a pattern.
464 % o number_messages: This integer returns the number of locale messages in
467 % o exception: return any errors or warnings in this structure.
471 #if defined(__cplusplus) || defined(c_plusplus)
475 static int LocaleInfoCompare(const void *x,const void *y)
481 p=(const LocaleInfo **) x,
482 q=(const LocaleInfo **) y;
483 if (LocaleCompare((*p)->path,(*q)->path) == 0)
484 return(LocaleCompare((*p)->tag,(*q)->tag));
485 return(LocaleCompare((*p)->path,(*q)->path));
488 #if defined(__cplusplus) || defined(c_plusplus)
492 MagickExport const LocaleInfo **GetLocaleInfoList(const char *pattern,
493 size_t *number_messages,ExceptionInfo *exception)
498 register const LocaleInfo
505 Allocate locale list.
507 assert(pattern != (char *) NULL);
508 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
509 assert(number_messages != (size_t *) NULL);
511 p=GetLocaleInfo_("*",exception);
512 if (p == (const LocaleInfo *) NULL)
513 return((const LocaleInfo **) NULL);
514 messages=(const LocaleInfo **) AcquireQuantumMemory((size_t)
515 GetNumberOfNodesInSplayTree(locale_list)+1UL,sizeof(*messages));
516 if (messages == (const LocaleInfo **) NULL)
517 return((const LocaleInfo **) NULL);
519 Generate locale list.
521 LockSemaphoreInfo(locale_semaphore);
522 ResetSplayTreeIterator(locale_list);
523 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_list);
524 for (i=0; p != (const LocaleInfo *) NULL; )
526 if ((p->stealth == MagickFalse) &&
527 (GlobExpression(p->tag,pattern,MagickTrue) != MagickFalse))
529 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_list);
531 UnlockSemaphoreInfo(locale_semaphore);
532 qsort((void *) messages,(size_t) i,sizeof(*messages),LocaleInfoCompare);
533 messages[i]=(LocaleInfo *) NULL;
534 *number_messages=(size_t) i;
539 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
543 % G e t L o c a l e L i s t %
547 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
549 % GetLocaleList() returns any locale messages that match the specified
552 % The format of the GetLocaleList function is:
554 % char **GetLocaleList(const char *pattern,size_t *number_messages,
555 % Exceptioninfo *exception)
557 % A description of each parameter follows:
559 % o pattern: Specifies a pointer to a text string containing a pattern.
561 % o number_messages: This integer returns the number of messages in the
564 % o exception: return any errors or warnings in this structure.
568 #if defined(__cplusplus) || defined(c_plusplus)
572 static int LocaleTagCompare(const void *x,const void *y)
580 return(LocaleCompare(*p,*q));
583 #if defined(__cplusplus) || defined(c_plusplus)
587 MagickExport char **GetLocaleList(const char *pattern,
588 size_t *number_messages,ExceptionInfo *exception)
593 register const LocaleInfo
600 Allocate locale list.
602 assert(pattern != (char *) NULL);
603 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
604 assert(number_messages != (size_t *) NULL);
606 p=GetLocaleInfo_("*",exception);
607 if (p == (const LocaleInfo *) NULL)
608 return((char **) NULL);
609 messages=(char **) AcquireQuantumMemory((size_t)
610 GetNumberOfNodesInSplayTree(locale_list)+1UL,sizeof(*messages));
611 if (messages == (char **) NULL)
612 return((char **) NULL);
613 LockSemaphoreInfo(locale_semaphore);
614 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_list);
615 for (i=0; p != (const LocaleInfo *) NULL; )
617 if ((p->stealth == MagickFalse) &&
618 (GlobExpression(p->tag,pattern,MagickTrue) != MagickFalse))
619 messages[i++]=ConstantString(p->tag);
620 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_list);
622 UnlockSemaphoreInfo(locale_semaphore);
623 qsort((void *) messages,(size_t) i,sizeof(*messages),LocaleTagCompare);
624 messages[i]=(char *) NULL;
625 *number_messages=(size_t) i;
630 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
634 % G e t L o c a l e M e s s a g e %
638 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
640 % GetLocaleMessage() returns a message in the current locale that matches the
643 % The format of the GetLocaleMessage method is:
645 % const char *GetLocaleMessage(const char *tag)
647 % A description of each parameter follows:
649 % o tag: Return a message that matches this tag in the current locale.
652 MagickExport const char *GetLocaleMessage(const char *tag)
663 if ((tag == (const char *) NULL) || (*tag == '\0'))
665 exception=AcquireExceptionInfo();
666 (void) FormatLocaleString(name,MaxTextExtent,"%s/",tag);
667 locale_info=GetLocaleInfo_(name,exception);
668 exception=DestroyExceptionInfo(exception);
669 if (locale_info != (const LocaleInfo *) NULL)
670 return(locale_info->message);
675 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
679 % G e t L o c a l e O p t i o n s %
683 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
685 % GetLocaleOptions() returns any Magick configuration messages associated
686 % with the specified filename.
688 % The format of the GetLocaleOptions method is:
690 % LinkedListInfo *GetLocaleOptions(const char *filename,
691 % ExceptionInfo *exception)
693 % A description of each parameter follows:
695 % o filename: the locale file tag.
697 % o exception: return any errors or warnings in this structure.
700 MagickExport LinkedListInfo *GetLocaleOptions(const char *filename,
701 ExceptionInfo *exception)
716 assert(filename != (const char *) NULL);
717 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
718 assert(exception != (ExceptionInfo *) NULL);
719 (void) CopyMagickString(path,filename,MaxTextExtent);
721 Load XML from configuration files to linked-list.
723 messages=NewLinkedList(0);
724 paths=GetConfigurePaths(filename,exception);
725 if (paths != (LinkedListInfo *) NULL)
727 ResetLinkedListIterator(paths);
728 element=(const char *) GetNextValueInLinkedList(paths);
729 while (element != (const char *) NULL)
731 (void) FormatLocaleString(path,MaxTextExtent,"%s%s",element,filename);
732 (void) LogMagickEvent(LocaleEvent,GetMagickModule(),
733 "Searching for locale file: \"%s\"",path);
734 xml=ConfigureFileToStringInfo(path);
735 if (xml != (StringInfo *) NULL)
736 (void) AppendValueToLinkedList(messages,xml);
737 element=(const char *) GetNextValueInLinkedList(paths);
739 paths=DestroyLinkedList(paths,RelinquishMagickMemory);
741 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
746 blob=(char *) NTResourceToBlob(filename);
747 if (blob != (char *) NULL)
749 xml=AcquireStringInfo(0);
750 SetStringInfoLength(xml,strlen(blob)+1);
751 SetStringInfoDatum(xml,(unsigned char *) blob);
752 SetStringInfoPath(xml,filename);
753 (void) AppendValueToLinkedList(messages,xml);
757 ResetLinkedListIterator(messages);
762 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
766 % G e t L o c a l e V a l u e %
770 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
772 % GetLocaleValue() returns the message associated with the locale info.
774 % The format of the GetLocaleValue method is:
776 % const char *GetLocaleValue(const LocaleInfo *locale_info)
778 % A description of each parameter follows:
780 % o locale_info: The locale info.
783 MagickExport const char *GetLocaleValue(const LocaleInfo *locale_info)
785 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
786 assert(locale_info != (LocaleInfo *) NULL);
787 assert(locale_info->signature == MagickSignature);
788 return(locale_info->message);
792 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
796 + I n i t i a l i z e L o c a l e L i s t %
800 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
802 % InitializeLocaleList() initializes the locale list.
804 % The format of the InitializeLocaleList method is:
806 % MagickBooleanType InitializeLocaleList(ExceptionInfo *exception)
808 % A description of each parameter follows.
810 % o exception: return any errors or warnings in this structure.
813 static MagickBooleanType InitializeLocaleList(ExceptionInfo *exception)
815 if ((locale_list == (SplayTreeInfo *) NULL) &&
816 (instantiate_locale == MagickFalse))
818 if (locale_semaphore == (SemaphoreInfo *) NULL)
819 AcquireSemaphoreInfo(&locale_semaphore);
820 LockSemaphoreInfo(locale_semaphore);
821 if ((locale_list == (SplayTreeInfo *) NULL) &&
822 (instantiate_locale == MagickFalse))
830 locale=(char *) NULL;
831 p=setlocale(LC_CTYPE,(const char *) NULL);
832 if (p != (const char *) NULL)
833 locale=ConstantString(p);
834 if (locale == (char *) NULL)
835 locale=GetEnvironmentValue("LC_ALL");
836 if (locale == (char *) NULL)
837 locale=GetEnvironmentValue("LC_MESSAGES");
838 if (locale == (char *) NULL)
839 locale=GetEnvironmentValue("LC_CTYPE");
840 if (locale == (char *) NULL)
841 locale=GetEnvironmentValue("LANG");
842 if (locale == (char *) NULL)
843 locale=ConstantString("C");
844 (void) LoadLocaleLists(LocaleFilename,locale,exception);
845 locale=DestroyString(locale);
846 instantiate_locale=MagickTrue;
848 UnlockSemaphoreInfo(locale_semaphore);
850 return(locale_list != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
854 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
858 + I n t e r p r e t L o c a l e V a l u e %
862 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
864 % InterpretLocaleValue() interprets the string as a floating point number in
865 % the "C" locale and returns its value as a double. If sentinal is not a null
866 % pointer, the method also sets the value pointed by sentinal to point to the
867 % first character after the number.
869 % The format of the InterpretLocaleValue method is:
871 % double InterpretLocaleValue(const char *value,char **sentinal)
873 % A description of each parameter follows:
875 % o value: the string value.
877 % o sentinal: if sentinal is not NULL, a pointer to the character after the
878 % last character used in the conversion is stored in the location
879 % referenced by sentinal.
882 MagickExport double InterpretLocaleValue(const char *restrict string,
883 char **restrict sentinal)
891 if ((*string == '0') && ((string[1] | 0x20)=='x'))
892 value=(double) strtoul(string,&q,16);
895 #if defined(MAGICKCORE_HAVE_STRTOD_L)
899 locale=AcquireCLocale();
900 if (locale == (locale_t) NULL)
901 value=strtod(string,&q);
903 value=strtod_l(string,&q,locale);
905 value=strtod(string,&q);
908 if (sentinal != (char **) NULL)
914 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
918 % L i s t L o c a l e I n f o %
922 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
924 % ListLocaleInfo() lists the locale info to a file.
926 % The format of the ListLocaleInfo method is:
928 % MagickBooleanType ListLocaleInfo(FILE *file,ExceptionInfo *exception)
930 % A description of each parameter follows.
932 % o file: An pointer to a FILE.
934 % o exception: return any errors or warnings in this structure.
937 MagickExport MagickBooleanType ListLocaleInfo(FILE *file,
938 ExceptionInfo *exception)
952 if (file == (const FILE *) NULL)
955 locale_info=GetLocaleInfoList("*",&number_messages,exception);
956 if (locale_info == (const LocaleInfo **) NULL)
958 path=(const char *) NULL;
959 for (i=0; i < (ssize_t) number_messages; i++)
961 if (locale_info[i]->stealth != MagickFalse)
963 if ((path == (const char *) NULL) ||
964 (LocaleCompare(path,locale_info[i]->path) != 0))
966 if (locale_info[i]->path != (char *) NULL)
967 (void) FormatLocaleFile(file,"\nPath: %s\n\n",locale_info[i]->path);
968 (void) FormatLocaleFile(file,"Tag/Message\n");
969 (void) FormatLocaleFile(file,
970 "-------------------------------------------------"
971 "------------------------------\n");
973 path=locale_info[i]->path;
974 (void) FormatLocaleFile(file,"%s\n",locale_info[i]->tag);
975 if (locale_info[i]->message != (char *) NULL)
976 (void) FormatLocaleFile(file," %s",locale_info[i]->message);
977 (void) FormatLocaleFile(file,"\n");
980 locale_info=(const LocaleInfo **)
981 RelinquishMagickMemory((void *) locale_info);
986 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
990 + L o a d L o c a l e L i s t %
994 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
996 % LoadLocaleList() loads the locale configuration file which provides a mapping
997 % between locale attributes and a locale name.
999 % The format of the LoadLocaleList method is:
1001 % MagickBooleanType LoadLocaleList(const char *xml,const char *filename,
1002 % const size_t depth,ExceptionInfo *exception)
1004 % A description of each parameter follows:
1006 % o xml: The locale list in XML format.
1008 % o filename: The locale list filename.
1010 % o depth: depth of <include /> statements.
1012 % o exception: return any errors or warnings in this structure.
1016 static void ChopLocaleComponents(char *path,const size_t components)
1026 p=path+strlen(path)-1;
1029 for (count=0; (count < (ssize_t) components) && (p > path); p--)
1035 if (count < (ssize_t) components)
1039 static void *DestroyLocaleNode(void *locale_info)
1044 p=(LocaleInfo *) locale_info;
1045 if (p->path != (char *) NULL)
1046 p->path=DestroyString(p->path);
1047 if (p->tag != (char *) NULL)
1048 p->tag=DestroyString(p->tag);
1049 if (p->message != (char *) NULL)
1050 p->message=DestroyString(p->message);
1051 return(RelinquishMagickMemory(p));
1054 static void LocaleFatalErrorHandler(
1055 const ExceptionType magick_unused(severity),
1056 const char *reason,const char *description)
1058 if (reason == (char *) NULL)
1060 (void) FormatLocaleFile(stderr,"%s: %s",GetClientName(),reason);
1061 if (description != (char *) NULL)
1062 (void) FormatLocaleFile(stderr," (%s)",description);
1063 (void) FormatLocaleFile(stderr,".\n");
1064 (void) fflush(stderr);
1069 static MagickBooleanType LoadLocaleList(const char *xml,const char *filename,
1070 const char *locale,const size_t depth,ExceptionInfo *exception)
1073 keyword[MaxTextExtent],
1074 message[MaxTextExtent],
1094 Read the locale configure file.
1096 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1097 "Loading locale configure file \"%s\" ...",filename);
1098 if (xml == (const char *) NULL)
1099 return(MagickFalse);
1100 if (locale_list == (SplayTreeInfo *) NULL)
1102 locale_list=NewSplayTree(CompareSplayTreeString,(void *(*)(void *)) NULL,
1104 if (locale_list == (SplayTreeInfo *) NULL)
1105 return(MagickFalse);
1108 locale_info=(LocaleInfo *) NULL;
1112 fatal_handler=SetFatalErrorHandler(LocaleFatalErrorHandler);
1113 token=AcquireString(xml);
1114 for (q=(char *) xml; *q != '\0'; )
1119 GetMagickToken(q,&q,token);
1122 (void) CopyMagickString(keyword,token,MaxTextExtent);
1123 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
1128 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
1130 GetMagickToken(q,&q,token);
1131 while (isspace((int) ((unsigned char) *q)) != 0)
1136 if (LocaleNCompare(keyword,"<!--",4) == 0)
1141 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
1143 GetMagickToken(q,&q,token);
1144 while (isspace((int) ((unsigned char) *q)) != 0)
1149 if (LocaleCompare(keyword,"<include") == 0)
1154 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
1156 (void) CopyMagickString(keyword,token,MaxTextExtent);
1157 GetMagickToken(q,&q,token);
1160 GetMagickToken(q,&q,token);
1161 if (LocaleCompare(keyword,"locale") == 0)
1163 if (LocaleCompare(locale,token) != 0)
1167 if (LocaleCompare(keyword,"file") == 0)
1170 (void) ThrowMagickException(exception,GetMagickModule(),
1171 ConfigureError,"IncludeElementNestedTooDeeply","'%s'",token);
1175 path[MaxTextExtent],
1179 GetPathComponent(filename,HeadPath,path);
1181 (void) ConcatenateMagickString(path,DirectorySeparator,
1183 if (*token == *DirectorySeparator)
1184 (void) CopyMagickString(path,token,MaxTextExtent);
1186 (void) ConcatenateMagickString(path,token,MaxTextExtent);
1187 xml=FileToString(path,~0,exception);
1188 if (xml != (char *) NULL)
1190 status=LoadLocaleList(xml,path,locale,depth+1,exception);
1191 xml=(char *) RelinquishMagickMemory(xml);
1198 if (LocaleCompare(keyword,"<locale") == 0)
1203 while ((*token != '>') && (*q != '\0'))
1205 (void) CopyMagickString(keyword,token,MaxTextExtent);
1206 GetMagickToken(q,&q,token);
1209 GetMagickToken(q,&q,token);
1213 if (LocaleCompare(keyword,"</locale>") == 0)
1215 ChopLocaleComponents(tag,1);
1216 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1219 if (LocaleCompare(keyword,"<localemap>") == 0)
1221 if (LocaleCompare(keyword,"</localemap>") == 0)
1223 if (LocaleCompare(keyword,"<message") == 0)
1228 while ((*token != '>') && (*q != '\0'))
1230 (void) CopyMagickString(keyword,token,MaxTextExtent);
1231 GetMagickToken(q,&q,token);
1234 GetMagickToken(q,&q,token);
1235 if (LocaleCompare(keyword,"name") == 0)
1237 (void) ConcatenateMagickString(tag,token,MaxTextExtent);
1238 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1241 for (p=(char *) q; (*q != '<') && (*q != '\0'); q++) ;
1242 while (isspace((int) ((unsigned char) *p)) != 0)
1245 while ((isspace((int) ((unsigned char) *q)) != 0) && (q > p))
1247 (void) CopyMagickString(message,p,(size_t) (q-p+2));
1248 locale_info=(LocaleInfo *) AcquireMagickMemory(sizeof(*locale_info));
1249 if (locale_info == (LocaleInfo *) NULL)
1250 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1251 (void) ResetMagickMemory(locale_info,0,sizeof(*locale_info));
1252 locale_info->path=ConstantString(filename);
1253 locale_info->tag=ConstantString(tag);
1254 locale_info->message=ConstantString(message);
1255 locale_info->signature=MagickSignature;
1256 status=AddValueToSplayTree(locale_list,locale_info->tag,locale_info);
1257 if (status == MagickFalse)
1258 (void) ThrowMagickException(exception,GetMagickModule(),
1259 ResourceLimitError,"MemoryAllocationFailed","'%s'",
1261 (void) ConcatenateMagickString(tag,message,MaxTextExtent);
1262 (void) ConcatenateMagickString(tag,"\n",MaxTextExtent);
1266 if (LocaleCompare(keyword,"</message>") == 0)
1268 ChopLocaleComponents(tag,2);
1269 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1272 if (*keyword == '<')
1277 if (*(keyword+1) == '?')
1279 if (*(keyword+1) == '/')
1281 ChopLocaleComponents(tag,1);
1283 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1286 token[strlen(token)-1]='\0';
1287 (void) CopyMagickString(token,token+1,MaxTextExtent);
1288 (void) ConcatenateMagickString(tag,token,MaxTextExtent);
1289 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1292 GetMagickToken(q,(const char **) NULL,token);
1296 token=(char *) RelinquishMagickMemory(token);
1297 (void) SetFatalErrorHandler(fatal_handler);
1302 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1306 % L o a d L o c a l e L i s t s %
1310 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1312 % LoadLocaleList() loads one or more locale configuration file which
1313 % provides a mapping between locale attributes and a locale tag.
1315 % The format of the LoadLocaleLists method is:
1317 % MagickBooleanType LoadLocaleLists(const char *filename,
1318 % ExceptionInfo *exception)
1320 % A description of each parameter follows:
1322 % o filename: the font file tag.
1324 % o locale: the actual locale.
1326 % o exception: return any errors or warnings in this structure.
1329 static MagickBooleanType LoadLocaleLists(const char *filename,
1330 const char *locale,ExceptionInfo *exception)
1332 #if defined(MAGICKCORE_ZERO_CONFIGURATION_SUPPORT)
1333 return(LoadLocaleList(LocaleMap,"built-in",locale,0,exception));
1345 options=GetLocaleOptions(filename,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);
1354 if ((locale_list == (SplayTreeInfo *) NULL) ||
1355 (GetNumberOfNodesInSplayTree(locale_list) == 0))
1357 options=GetLocaleOptions("english.xml",exception);
1358 option=(const StringInfo *) GetNextValueInLinkedList(options);
1359 while (option != (const StringInfo *) NULL)
1361 status|=LoadLocaleList((const char *) GetStringInfoDatum(option),
1362 GetStringInfoPath(option),locale,0,exception);
1363 option=(const StringInfo *) GetNextValueInLinkedList(options);
1365 options=DestroyLocaleOptions(options);
1367 if ((locale_list == (SplayTreeInfo *) NULL) ||
1368 (GetNumberOfNodesInSplayTree(locale_list) == 0))
1369 status|=LoadLocaleList(LocaleMap,"built-in",locale,0,exception);
1370 return(status != 0 ? MagickTrue : MagickFalse);
1375 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1379 + L o c a l e C o m p o n e n t G e n e s i s %
1383 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1385 % LocaleComponentGenesis() instantiates the locale component.
1387 % The format of the LocaleComponentGenesis method is:
1389 % MagickBooleanType LocaleComponentGenesis(void)
1392 MagickPrivate MagickBooleanType LocaleComponentGenesis(void)
1394 AcquireSemaphoreInfo(&locale_semaphore);
1399 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1403 + L o c a l e C o m p o n e n t T e r m i n u s %
1407 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1409 % LocaleComponentTerminus() destroys the locale component.
1411 % The format of the LocaleComponentTerminus method is:
1413 % LocaleComponentTerminus(void)
1416 MagickPrivate void LocaleComponentTerminus(void)
1418 if (locale_semaphore == (SemaphoreInfo *) NULL)
1419 AcquireSemaphoreInfo(&locale_semaphore);
1420 LockSemaphoreInfo(locale_semaphore);
1421 if (locale_list != (SplayTreeInfo *) NULL)
1422 locale_list=DestroySplayTree(locale_list);
1423 #if defined(MAGICKCORE_HAVE_STRTOD_L)
1426 instantiate_locale=MagickFalse;
1427 UnlockSemaphoreInfo(locale_semaphore);
1428 DestroySemaphoreInfo(&locale_semaphore);