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-2014 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
82 "<?xml version=\"1.0\"?>"
84 " <locale name=\"C\">"
86 " <Message name=\"\">"
93 *locale_semaphore = (SemaphoreInfo *) NULL;
96 *locale_list = (SplayTreeInfo *) NULL;
98 #if defined(MAGICKCORE_HAVE_STRTOD_L)
99 static volatile locale_t
100 c_locale = (locale_t) NULL;
103 static volatile MagickBooleanType
104 instantiate_locale = MagickFalse;
107 Forward declarations.
109 static MagickBooleanType
110 InitializeLocaleList(ExceptionInfo *),
111 LoadLocaleLists(const char *,const char *,ExceptionInfo *);
113 #if defined(MAGICKCORE_HAVE_STRTOD_L)
115 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
119 + A c q u i r e C L o c a l e %
123 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
125 % AcquireCLocale() allocates the C locale object, or (locale_t) 0 with
126 % errno set if it cannot be acquired.
128 % The format of the AcquireCLocale method is:
130 % locale_t AcquireCLocale(void)
133 static locale_t AcquireCLocale(void)
135 #if defined(MAGICKCORE_HAVE_NEWLOCALE)
136 if (c_locale == (locale_t) NULL)
137 c_locale=newlocale(LC_ALL_MASK,"C",(locale_t) 0);
138 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
139 if (c_locale == (locale_t) NULL)
140 c_locale=_create_locale(LC_ALL,"C");
146 #if defined(MAGICKCORE_HAVE_STRTOD_L)
148 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
152 + D e s t r o y C L o c a l e %
156 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
158 % DestroyCLocale() releases the resources allocated for a locale object
159 % returned by a call to the AcquireCLocale() method.
161 % The format of the DestroyCLocale method is:
163 % void DestroyCLocale(void)
166 static void DestroyCLocale(void)
168 #if defined(MAGICKCORE_HAVE_NEWLOCALE)
169 if (c_locale != (locale_t) NULL)
170 freelocale(c_locale);
171 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
172 if (c_locale != (locale_t) NULL)
173 _free_locale(c_locale);
175 c_locale=(locale_t) NULL;
180 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
184 % D e s t r o y L o c a l e O p t i o n s %
188 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
190 % DestroyLocaleOptions() releases memory associated with an locale
193 % The format of the DestroyProfiles method is:
195 % LinkedListInfo *DestroyLocaleOptions(Image *image)
197 % A description of each parameter follows:
199 % o image: the image.
203 static void *DestroyOptions(void *message)
205 return(DestroyStringInfo((StringInfo *) message));
208 MagickExport LinkedListInfo *DestroyLocaleOptions(LinkedListInfo *messages)
210 assert(messages != (LinkedListInfo *) NULL);
211 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
212 return(DestroyLinkedList(messages,DestroyOptions));
216 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
220 + F o r m a t L o c a l e F i l e %
224 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
226 % FormatLocaleFile() prints formatted output of a variable argument list to a
227 % file in the "C" locale.
229 % The format of the FormatLocaleFile method is:
231 % ssize_t FormatLocaleFile(FILE *file,const char *format,...)
233 % A description of each parameter follows.
237 % o format: A file describing the format to use to write the remaining
242 MagickPrivate ssize_t FormatLocaleFileList(FILE *file,
243 const char *restrict format,va_list operands)
248 #if defined(MAGICKCORE_HAVE_VFPRINTF_L)
253 locale=AcquireCLocale();
254 if (locale == (locale_t) NULL)
255 n=(ssize_t) vfprintf(file,format,operands);
257 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
258 n=(ssize_t) vfprintf_l(file,format,locale,operands);
260 n=(ssize_t) vfprintf_l(file,locale,format,operands);
264 #if defined(MAGICKCORE_HAVE_USELOCALE)
270 locale=AcquireCLocale();
271 if (locale == (locale_t) NULL)
272 n=(ssize_t) vfprintf(file,format,operands);
275 previous_locale=uselocale(locale);
276 n=(ssize_t) vfprintf(file,format,operands);
277 uselocale(previous_locale);
281 n=(ssize_t) vfprintf(file,format,operands);
287 MagickExport ssize_t FormatLocaleFile(FILE *file,const char *restrict format,
296 va_start(operands,format);
297 n=FormatLocaleFileList(file,format,operands);
303 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
307 + F o r m a t L o c a l e S t r i n g %
311 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
313 % FormatLocaleString() prints formatted output of a variable argument list to
314 % a string buffer in the "C" locale.
316 % The format of the FormatLocaleString method is:
318 % ssize_t FormatLocaleString(char *string,const size_t length,
319 % const char *format,...)
321 % A description of each parameter follows.
323 % o string: FormatLocaleString() returns the formatted string in this
326 % o length: the maximum length of the string.
328 % o format: A string describing the format to use to write the remaining
333 MagickPrivate ssize_t FormatLocaleStringList(char *restrict string,
334 const size_t length,const char *restrict format,va_list operands)
339 #if defined(MAGICKCORE_HAVE_VSNPRINTF_L)
344 locale=AcquireCLocale();
345 if (locale == (locale_t) NULL)
346 n=(ssize_t) vsnprintf(string,length,format,operands);
348 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
349 n=(ssize_t) vsnprintf_l(string,length,format,locale,operands);
351 n=(ssize_t) vsnprintf_l(string,length,locale,format,operands);
354 #elif defined(MAGICKCORE_HAVE_VSNPRINTF)
355 #if defined(MAGICKCORE_HAVE_USELOCALE)
361 locale=AcquireCLocale();
362 if (locale == (locale_t) NULL)
363 n=(ssize_t) vsnprintf(string,length,format,operands);
366 previous_locale=uselocale(locale);
367 n=(ssize_t) vsnprintf(string,length,format,operands);
368 uselocale(previous_locale);
372 n=(ssize_t) vsnprintf(string,length,format,operands);
375 n=(ssize_t) vsprintf(string,format,operands);
378 string[length-1]='\0';
382 MagickExport ssize_t FormatLocaleString(char *restrict string,
383 const size_t length,const char *restrict format,...)
391 va_start(operands,format);
392 n=FormatLocaleStringList(string,length,format,operands);
398 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
402 + G e t L o c a l e I n f o _ %
406 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
408 % GetLocaleInfo_() searches the locale list for the specified tag and if
409 % found returns attributes for that element.
411 % The format of the GetLocaleInfo method is:
413 % const LocaleInfo *GetLocaleInfo_(const char *tag,
414 % ExceptionInfo *exception)
416 % A description of each parameter follows:
418 % o tag: the locale tag.
420 % o exception: return any errors or warnings in this structure.
423 MagickExport const LocaleInfo *GetLocaleInfo_(const char *tag,
424 ExceptionInfo *exception)
426 assert(exception != (ExceptionInfo *) NULL);
427 if ((locale_list == (SplayTreeInfo *) NULL) ||
428 (instantiate_locale == MagickFalse))
429 if (InitializeLocaleList(exception) == MagickFalse)
430 return((const LocaleInfo *) NULL);
431 if ((locale_list == (SplayTreeInfo *) NULL) ||
432 (GetNumberOfNodesInSplayTree(locale_list) == 0))
433 return((const LocaleInfo *) NULL);
434 if ((tag == (const char *) NULL) || (LocaleCompare(tag,"*") == 0))
436 ResetSplayTreeIterator(locale_list);
437 return((const LocaleInfo *) GetNextValueInSplayTree(locale_list));
439 return((const LocaleInfo *) GetValueFromSplayTree(locale_list,tag));
443 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
447 % G e t L o c a l e I n f o L i s t %
451 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
453 % GetLocaleInfoList() returns any locale messages that match the
456 % The format of the GetLocaleInfoList function is:
458 % const LocaleInfo **GetLocaleInfoList(const char *pattern,
459 % size_t *number_messages,ExceptionInfo *exception)
461 % A description of each parameter follows:
463 % o pattern: Specifies a pointer to a text string containing a pattern.
465 % o number_messages: This integer returns the number of locale messages in
468 % o exception: return any errors or warnings in this structure.
472 #if defined(__cplusplus) || defined(c_plusplus)
476 static int LocaleInfoCompare(const void *x,const void *y)
482 p=(const LocaleInfo **) x,
483 q=(const LocaleInfo **) y;
484 if (LocaleCompare((*p)->path,(*q)->path) == 0)
485 return(LocaleCompare((*p)->tag,(*q)->tag));
486 return(LocaleCompare((*p)->path,(*q)->path));
489 #if defined(__cplusplus) || defined(c_plusplus)
493 MagickExport const LocaleInfo **GetLocaleInfoList(const char *pattern,
494 size_t *number_messages,ExceptionInfo *exception)
499 register const LocaleInfo
506 Allocate locale list.
508 assert(pattern != (char *) NULL);
509 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
510 assert(number_messages != (size_t *) NULL);
512 p=GetLocaleInfo_("*",exception);
513 if (p == (const LocaleInfo *) NULL)
514 return((const LocaleInfo **) NULL);
515 messages=(const LocaleInfo **) AcquireQuantumMemory((size_t)
516 GetNumberOfNodesInSplayTree(locale_list)+1UL,sizeof(*messages));
517 if (messages == (const LocaleInfo **) NULL)
518 return((const LocaleInfo **) NULL);
520 Generate locale list.
522 LockSemaphoreInfo(locale_semaphore);
523 ResetSplayTreeIterator(locale_list);
524 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_list);
525 for (i=0; p != (const LocaleInfo *) NULL; )
527 if ((p->stealth == MagickFalse) &&
528 (GlobExpression(p->tag,pattern,MagickTrue) != MagickFalse))
530 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_list);
532 UnlockSemaphoreInfo(locale_semaphore);
533 qsort((void *) messages,(size_t) i,sizeof(*messages),LocaleInfoCompare);
534 messages[i]=(LocaleInfo *) NULL;
535 *number_messages=(size_t) i;
540 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
544 % G e t L o c a l e L i s t %
548 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
550 % GetLocaleList() returns any locale messages that match the specified
553 % The format of the GetLocaleList function is:
555 % char **GetLocaleList(const char *pattern,size_t *number_messages,
556 % Exceptioninfo *exception)
558 % A description of each parameter follows:
560 % o pattern: Specifies a pointer to a text string containing a pattern.
562 % o number_messages: This integer returns the number of messages in the
565 % o exception: return any errors or warnings in this structure.
569 #if defined(__cplusplus) || defined(c_plusplus)
573 static int LocaleTagCompare(const void *x,const void *y)
581 return(LocaleCompare(*p,*q));
584 #if defined(__cplusplus) || defined(c_plusplus)
588 MagickExport char **GetLocaleList(const char *pattern,
589 size_t *number_messages,ExceptionInfo *exception)
594 register const LocaleInfo
601 Allocate locale list.
603 assert(pattern != (char *) NULL);
604 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
605 assert(number_messages != (size_t *) NULL);
607 p=GetLocaleInfo_("*",exception);
608 if (p == (const LocaleInfo *) NULL)
609 return((char **) NULL);
610 messages=(char **) AcquireQuantumMemory((size_t)
611 GetNumberOfNodesInSplayTree(locale_list)+1UL,sizeof(*messages));
612 if (messages == (char **) NULL)
613 return((char **) NULL);
614 LockSemaphoreInfo(locale_semaphore);
615 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_list);
616 for (i=0; p != (const LocaleInfo *) NULL; )
618 if ((p->stealth == MagickFalse) &&
619 (GlobExpression(p->tag,pattern,MagickTrue) != MagickFalse))
620 messages[i++]=ConstantString(p->tag);
621 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_list);
623 UnlockSemaphoreInfo(locale_semaphore);
624 qsort((void *) messages,(size_t) i,sizeof(*messages),LocaleTagCompare);
625 messages[i]=(char *) NULL;
626 *number_messages=(size_t) i;
631 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
635 % G e t L o c a l e M e s s a g e %
639 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
641 % GetLocaleMessage() returns a message in the current locale that matches the
644 % The format of the GetLocaleMessage method is:
646 % const char *GetLocaleMessage(const char *tag)
648 % A description of each parameter follows:
650 % o tag: Return a message that matches this tag in the current locale.
653 MagickExport const char *GetLocaleMessage(const char *tag)
664 if ((tag == (const char *) NULL) || (*tag == '\0'))
666 exception=AcquireExceptionInfo();
667 (void) FormatLocaleString(name,MaxTextExtent,"%s/",tag);
668 locale_info=GetLocaleInfo_(name,exception);
669 exception=DestroyExceptionInfo(exception);
670 if (locale_info != (const LocaleInfo *) NULL)
671 return(locale_info->message);
676 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
680 % G e t L o c a l e O p t i o n s %
684 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
686 % GetLocaleOptions() returns any Magick configuration messages associated
687 % with the specified filename.
689 % The format of the GetLocaleOptions method is:
691 % LinkedListInfo *GetLocaleOptions(const char *filename,
692 % ExceptionInfo *exception)
694 % A description of each parameter follows:
696 % o filename: the locale file tag.
698 % o exception: return any errors or warnings in this structure.
701 MagickExport LinkedListInfo *GetLocaleOptions(const char *filename,
702 ExceptionInfo *exception)
717 assert(filename != (const char *) NULL);
718 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
719 assert(exception != (ExceptionInfo *) NULL);
720 (void) CopyMagickString(path,filename,MaxTextExtent);
722 Load XML from configuration files to linked-list.
724 messages=NewLinkedList(0);
725 paths=GetConfigurePaths(filename,exception);
726 if (paths != (LinkedListInfo *) NULL)
728 ResetLinkedListIterator(paths);
729 element=(const char *) GetNextValueInLinkedList(paths);
730 while (element != (const char *) NULL)
732 (void) FormatLocaleString(path,MaxTextExtent,"%s%s",element,filename);
733 (void) LogMagickEvent(LocaleEvent,GetMagickModule(),
734 "Searching for locale file: \"%s\"",path);
735 xml=ConfigureFileToStringInfo(path);
736 if (xml != (StringInfo *) NULL)
737 (void) AppendValueToLinkedList(messages,xml);
738 element=(const char *) GetNextValueInLinkedList(paths);
740 paths=DestroyLinkedList(paths,RelinquishMagickMemory);
742 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
747 blob=(char *) NTResourceToBlob(filename);
748 if (blob != (char *) NULL)
750 xml=AcquireStringInfo(0);
751 SetStringInfoLength(xml,strlen(blob)+1);
752 SetStringInfoDatum(xml,(unsigned char *) blob);
753 SetStringInfoPath(xml,filename);
754 (void) AppendValueToLinkedList(messages,xml);
758 ResetLinkedListIterator(messages);
763 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
767 % G e t L o c a l e V a l u e %
771 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
773 % GetLocaleValue() returns the message associated with the locale info.
775 % The format of the GetLocaleValue method is:
777 % const char *GetLocaleValue(const LocaleInfo *locale_info)
779 % A description of each parameter follows:
781 % o locale_info: The locale info.
784 MagickExport const char *GetLocaleValue(const LocaleInfo *locale_info)
786 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
787 assert(locale_info != (LocaleInfo *) NULL);
788 assert(locale_info->signature == MagickSignature);
789 return(locale_info->message);
793 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
797 + I n i t i a l i z e L o c a l e L i s t %
801 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
803 % InitializeLocaleList() initializes the locale list.
805 % The format of the InitializeLocaleList method is:
807 % MagickBooleanType InitializeLocaleList(ExceptionInfo *exception)
809 % A description of each parameter follows.
811 % o exception: return any errors or warnings in this structure.
814 static MagickBooleanType InitializeLocaleList(ExceptionInfo *exception)
816 if ((locale_list == (SplayTreeInfo *) NULL) &&
817 (instantiate_locale == MagickFalse))
819 if (locale_semaphore == (SemaphoreInfo *) NULL)
820 ActivateSemaphoreInfo(&locale_semaphore);
821 LockSemaphoreInfo(locale_semaphore);
822 if ((locale_list == (SplayTreeInfo *) NULL) ||
823 (instantiate_locale == MagickFalse))
831 locale=(char *) NULL;
832 p=setlocale(LC_CTYPE,(const char *) NULL);
833 if (p != (const char *) NULL)
834 locale=ConstantString(p);
835 if (locale == (char *) NULL)
836 locale=GetEnvironmentValue("LC_ALL");
837 if (locale == (char *) NULL)
838 locale=GetEnvironmentValue("LC_MESSAGES");
839 if (locale == (char *) NULL)
840 locale=GetEnvironmentValue("LC_CTYPE");
841 if (locale == (char *) NULL)
842 locale=GetEnvironmentValue("LANG");
843 if (locale == (char *) NULL)
844 locale=ConstantString("C");
845 (void) LoadLocaleLists(LocaleFilename,locale,exception);
846 locale=DestroyString(locale);
847 instantiate_locale=MagickTrue;
849 UnlockSemaphoreInfo(locale_semaphore);
851 return(locale_list != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
855 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
859 + I n t e r p r e t L o c a l e V a l u e %
863 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
865 % InterpretLocaleValue() interprets the string as a floating point number in
866 % the "C" locale and returns its value as a double. If sentinal is not a null
867 % pointer, the method also sets the value pointed by sentinal to point to the
868 % first character after the number.
870 % The format of the InterpretLocaleValue method is:
872 % double InterpretLocaleValue(const char *value,char **sentinal)
874 % A description of each parameter follows:
876 % o value: the string value.
878 % o sentinal: if sentinal is not NULL, a pointer to the character after the
879 % last character used in the conversion is stored in the location
880 % referenced by sentinal.
883 MagickExport double InterpretLocaleValue(const char *restrict string,
884 char **restrict sentinal)
892 if ((*string == '0') && ((string[1] | 0x20)=='x'))
893 value=(double) strtoul(string,&q,16);
896 #if defined(MAGICKCORE_HAVE_STRTOD_L)
900 locale=AcquireCLocale();
901 if (locale == (locale_t) NULL)
902 value=strtod(string,&q);
904 value=strtod_l(string,&q,locale);
906 value=strtod(string,&q);
909 if (sentinal != (char **) NULL)
915 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
919 % L i s t L o c a l e I n f o %
923 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
925 % ListLocaleInfo() lists the locale info to a file.
927 % The format of the ListLocaleInfo method is:
929 % MagickBooleanType ListLocaleInfo(FILE *file,ExceptionInfo *exception)
931 % A description of each parameter follows.
933 % o file: An pointer to a FILE.
935 % o exception: return any errors or warnings in this structure.
938 MagickExport MagickBooleanType ListLocaleInfo(FILE *file,
939 ExceptionInfo *exception)
953 if (file == (const FILE *) NULL)
956 locale_info=GetLocaleInfoList("*",&number_messages,exception);
957 if (locale_info == (const LocaleInfo **) NULL)
959 path=(const char *) NULL;
960 for (i=0; i < (ssize_t) number_messages; i++)
962 if (locale_info[i]->stealth != MagickFalse)
964 if ((path == (const char *) NULL) ||
965 (LocaleCompare(path,locale_info[i]->path) != 0))
967 if (locale_info[i]->path != (char *) NULL)
968 (void) FormatLocaleFile(file,"\nPath: %s\n\n",locale_info[i]->path);
969 (void) FormatLocaleFile(file,"Tag/Message\n");
970 (void) FormatLocaleFile(file,
971 "-------------------------------------------------"
972 "------------------------------\n");
974 path=locale_info[i]->path;
975 (void) FormatLocaleFile(file,"%s\n",locale_info[i]->tag);
976 if (locale_info[i]->message != (char *) NULL)
977 (void) FormatLocaleFile(file," %s",locale_info[i]->message);
978 (void) FormatLocaleFile(file,"\n");
981 locale_info=(const LocaleInfo **)
982 RelinquishMagickMemory((void *) locale_info);
987 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
991 + L o a d L o c a l e L i s t %
995 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
997 % LoadLocaleList() loads the locale configuration file which provides a mapping
998 % between locale attributes and a locale name.
1000 % The format of the LoadLocaleList method is:
1002 % MagickBooleanType LoadLocaleList(const char *xml,const char *filename,
1003 % const size_t depth,ExceptionInfo *exception)
1005 % A description of each parameter follows:
1007 % o xml: The locale list in XML format.
1009 % o filename: The locale list filename.
1011 % o depth: depth of <include /> statements.
1013 % o exception: return any errors or warnings in this structure.
1017 static void ChopLocaleComponents(char *path,const size_t components)
1027 p=path+strlen(path)-1;
1030 for (count=0; (count < (ssize_t) components) && (p > path); p--)
1036 if (count < (ssize_t) components)
1040 static void *DestroyLocaleNode(void *locale_info)
1045 p=(LocaleInfo *) locale_info;
1046 if (p->path != (char *) NULL)
1047 p->path=DestroyString(p->path);
1048 if (p->tag != (char *) NULL)
1049 p->tag=DestroyString(p->tag);
1050 if (p->message != (char *) NULL)
1051 p->message=DestroyString(p->message);
1052 return(RelinquishMagickMemory(p));
1055 static void LocaleFatalErrorHandler(
1056 const ExceptionType magick_unused(severity),
1057 const char *reason,const char *description)
1059 if (reason == (char *) NULL)
1061 (void) FormatLocaleFile(stderr,"%s: %s",GetClientName(),reason);
1062 if (description != (char *) NULL)
1063 (void) FormatLocaleFile(stderr," (%s)",description);
1064 (void) FormatLocaleFile(stderr,".\n");
1065 (void) fflush(stderr);
1069 static inline size_t MagickMin(const size_t x,const size_t y)
1076 static MagickBooleanType LoadLocaleList(const char *xml,const char *filename,
1077 const char *locale,const size_t depth,ExceptionInfo *exception)
1080 keyword[MaxTextExtent],
1081 message[MaxTextExtent],
1101 Read the locale configure file.
1103 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1104 "Loading locale configure file \"%s\" ...",filename);
1105 if (xml == (const char *) NULL)
1106 return(MagickFalse);
1107 if (locale_list == (SplayTreeInfo *) NULL)
1109 locale_list=NewSplayTree(CompareSplayTreeString,(void *(*)(void *)) NULL,
1111 if (locale_list == (SplayTreeInfo *) NULL)
1112 return(MagickFalse);
1115 locale_info=(LocaleInfo *) NULL;
1119 fatal_handler=SetFatalErrorHandler(LocaleFatalErrorHandler);
1120 token=AcquireString(xml);
1121 for (q=(char *) xml; *q != '\0'; )
1126 GetMagickToken(q,&q,token);
1129 (void) CopyMagickString(keyword,token,MaxTextExtent);
1130 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
1135 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
1137 GetMagickToken(q,&q,token);
1138 while (isspace((int) ((unsigned char) *q)) != 0)
1143 if (LocaleNCompare(keyword,"<!--",4) == 0)
1148 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
1150 GetMagickToken(q,&q,token);
1151 while (isspace((int) ((unsigned char) *q)) != 0)
1156 if (LocaleCompare(keyword,"<include") == 0)
1161 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
1163 (void) CopyMagickString(keyword,token,MaxTextExtent);
1164 GetMagickToken(q,&q,token);
1167 GetMagickToken(q,&q,token);
1168 if (LocaleCompare(keyword,"locale") == 0)
1170 if (LocaleCompare(locale,token) != 0)
1174 if (LocaleCompare(keyword,"file") == 0)
1177 (void) ThrowMagickException(exception,GetMagickModule(),
1178 ConfigureError,"IncludeElementNestedTooDeeply","`%s'",token);
1182 path[MaxTextExtent],
1186 GetPathComponent(filename,HeadPath,path);
1188 (void) ConcatenateMagickString(path,DirectorySeparator,
1190 if (*token == *DirectorySeparator)
1191 (void) CopyMagickString(path,token,MaxTextExtent);
1193 (void) ConcatenateMagickString(path,token,MaxTextExtent);
1194 xml=FileToString(path,~0UL,exception);
1195 if (xml != (char *) NULL)
1197 status=LoadLocaleList(xml,path,locale,depth+1,exception);
1198 xml=(char *) RelinquishMagickMemory(xml);
1205 if (LocaleCompare(keyword,"<locale") == 0)
1210 while ((*token != '>') && (*q != '\0'))
1212 (void) CopyMagickString(keyword,token,MaxTextExtent);
1213 GetMagickToken(q,&q,token);
1216 GetMagickToken(q,&q,token);
1220 if (LocaleCompare(keyword,"</locale>") == 0)
1222 ChopLocaleComponents(tag,1);
1223 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1226 if (LocaleCompare(keyword,"<localemap>") == 0)
1228 if (LocaleCompare(keyword,"</localemap>") == 0)
1230 if (LocaleCompare(keyword,"<message") == 0)
1235 while ((*token != '>') && (*q != '\0'))
1237 (void) CopyMagickString(keyword,token,MaxTextExtent);
1238 GetMagickToken(q,&q,token);
1241 GetMagickToken(q,&q,token);
1242 if (LocaleCompare(keyword,"name") == 0)
1244 (void) ConcatenateMagickString(tag,token,MaxTextExtent);
1245 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1248 for (p=(char *) q; (*q != '<') && (*q != '\0'); q++) ;
1249 while (isspace((int) ((unsigned char) *p)) != 0)
1252 while ((isspace((int) ((unsigned char) *q)) != 0) && (q > p))
1254 (void) CopyMagickString(message,p,MagickMin((size_t) (q-p+2),
1256 locale_info=(LocaleInfo *) AcquireMagickMemory(sizeof(*locale_info));
1257 if (locale_info == (LocaleInfo *) NULL)
1258 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1259 (void) ResetMagickMemory(locale_info,0,sizeof(*locale_info));
1260 locale_info->path=ConstantString(filename);
1261 locale_info->tag=ConstantString(tag);
1262 locale_info->message=ConstantString(message);
1263 locale_info->signature=MagickSignature;
1264 status=AddValueToSplayTree(locale_list,locale_info->tag,locale_info);
1265 if (status == MagickFalse)
1266 (void) ThrowMagickException(exception,GetMagickModule(),
1267 ResourceLimitError,"MemoryAllocationFailed","`%s'",
1269 (void) ConcatenateMagickString(tag,message,MaxTextExtent);
1270 (void) ConcatenateMagickString(tag,"\n",MaxTextExtent);
1274 if (LocaleCompare(keyword,"</message>") == 0)
1276 ChopLocaleComponents(tag,2);
1277 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1280 if (*keyword == '<')
1285 if (*(keyword+1) == '?')
1287 if (*(keyword+1) == '/')
1289 ChopLocaleComponents(tag,1);
1291 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1294 token[strlen(token)-1]='\0';
1295 (void) CopyMagickString(token,token+1,MaxTextExtent);
1296 (void) ConcatenateMagickString(tag,token,MaxTextExtent);
1297 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1300 GetMagickToken(q,(const char **) NULL,token);
1304 token=(char *) RelinquishMagickMemory(token);
1305 (void) SetFatalErrorHandler(fatal_handler);
1310 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1314 % L o a d L o c a l e L i s t s %
1318 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1320 % LoadLocaleList() loads one or more locale configuration file which
1321 % provides a mapping between locale attributes and a locale tag.
1323 % The format of the LoadLocaleLists method is:
1325 % MagickBooleanType LoadLocaleLists(const char *filename,
1326 % ExceptionInfo *exception)
1328 % A description of each parameter follows:
1330 % o filename: the font file tag.
1332 % o locale: the actual locale.
1334 % o exception: return any errors or warnings in this structure.
1337 static MagickBooleanType LoadLocaleLists(const char *filename,
1338 const char *locale,ExceptionInfo *exception)
1340 #if defined(MAGICKCORE_ZERO_CONFIGURATION_SUPPORT)
1341 return(LoadLocaleList(LocaleMap,"built-in",locale,0,exception));
1353 options=GetLocaleOptions(filename,exception);
1354 option=(const StringInfo *) GetNextValueInLinkedList(options);
1355 while (option != (const StringInfo *) NULL)
1357 status&=LoadLocaleList((const char *) GetStringInfoDatum(option),
1358 GetStringInfoPath(option),locale,0,exception);
1359 option=(const StringInfo *) GetNextValueInLinkedList(options);
1361 options=DestroyLocaleOptions(options);
1362 if ((locale_list == (SplayTreeInfo *) NULL) ||
1363 (GetNumberOfNodesInSplayTree(locale_list) == 0))
1365 options=GetLocaleOptions("english.xml",exception);
1366 option=(const StringInfo *) GetNextValueInLinkedList(options);
1367 while (option != (const StringInfo *) NULL)
1369 status&=LoadLocaleList((const char *) GetStringInfoDatum(option),
1370 GetStringInfoPath(option),locale,0,exception);
1371 option=(const StringInfo *) GetNextValueInLinkedList(options);
1373 options=DestroyLocaleOptions(options);
1375 if ((locale_list == (SplayTreeInfo *) NULL) ||
1376 (GetNumberOfNodesInSplayTree(locale_list) == 0))
1377 status&=LoadLocaleList(LocaleMap,"built-in",locale,0,exception);
1378 return(status != 0 ? MagickTrue : MagickFalse);
1383 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1387 + L o c a l e C o m p o n e n t G e n e s i s %
1391 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1393 % LocaleComponentGenesis() instantiates the locale component.
1395 % The format of the LocaleComponentGenesis method is:
1397 % MagickBooleanType LocaleComponentGenesis(void)
1400 MagickPrivate MagickBooleanType LocaleComponentGenesis(void)
1402 locale_semaphore=AcquireSemaphoreInfo();
1407 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1411 + L o c a l e C o m p o n e n t T e r m i n u s %
1415 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1417 % LocaleComponentTerminus() destroys the locale component.
1419 % The format of the LocaleComponentTerminus method is:
1421 % LocaleComponentTerminus(void)
1424 MagickPrivate void LocaleComponentTerminus(void)
1426 if (locale_semaphore == (SemaphoreInfo *) NULL)
1427 ActivateSemaphoreInfo(&locale_semaphore);
1428 LockSemaphoreInfo(locale_semaphore);
1429 if (locale_list != (SplayTreeInfo *) NULL)
1430 locale_list=DestroySplayTree(locale_list);
1431 #if defined(MAGICKCORE_HAVE_STRTOD_L)
1434 instantiate_locale=MagickFalse;
1435 UnlockSemaphoreInfo(locale_semaphore);
1436 RelinquishSemaphoreInfo(&locale_semaphore);