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;
104 Forward declarations.
106 static MagickBooleanType
107 InitializeLocaleList(ExceptionInfo *),
108 LoadLocaleLists(const char *,const char *,ExceptionInfo *);
110 #if defined(MAGICKCORE_HAVE_STRTOD_L)
112 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
116 + A c q u i r e C L o c a l e %
120 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
122 % AcquireCLocale() allocates the C locale object, or (locale_t) 0 with
123 % errno set if it cannot be acquired.
125 % The format of the AcquireCLocale method is:
127 % locale_t AcquireCLocale(void)
130 static locale_t AcquireCLocale(void)
132 #if defined(MAGICKCORE_HAVE_NEWLOCALE)
133 if (c_locale == (locale_t) NULL)
134 c_locale=newlocale(LC_ALL_MASK,"C",(locale_t) 0);
135 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
136 if (c_locale == (locale_t) NULL)
137 c_locale=_create_locale(LC_ALL,"C");
143 #if defined(MAGICKCORE_HAVE_STRTOD_L)
145 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
149 + D e s t r o y C L o c a l e %
153 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
155 % DestroyCLocale() releases the resources allocated for a locale object
156 % returned by a call to the AcquireCLocale() method.
158 % The format of the DestroyCLocale method is:
160 % void DestroyCLocale(void)
163 static void DestroyCLocale(void)
165 #if defined(MAGICKCORE_HAVE_NEWLOCALE)
166 if (c_locale != (locale_t) NULL)
167 freelocale(c_locale);
168 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
169 if (c_locale != (locale_t) NULL)
170 _free_locale(c_locale);
172 c_locale=(locale_t) NULL;
177 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
181 % D e s t r o y L o c a l e O p t i o n s %
185 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
187 % DestroyLocaleOptions() releases memory associated with an locale
190 % The format of the DestroyProfiles method is:
192 % LinkedListInfo *DestroyLocaleOptions(Image *image)
194 % A description of each parameter follows:
196 % o image: the image.
200 static void *DestroyOptions(void *message)
202 return(DestroyStringInfo((StringInfo *) message));
205 MagickExport LinkedListInfo *DestroyLocaleOptions(LinkedListInfo *messages)
207 assert(messages != (LinkedListInfo *) NULL);
208 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
209 return(DestroyLinkedList(messages,DestroyOptions));
213 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
217 + F o r m a t L o c a l e F i l e %
221 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
223 % FormatLocaleFile() prints formatted output of a variable argument list to a
224 % file in the "C" locale.
226 % The format of the FormatLocaleFile method is:
228 % ssize_t FormatLocaleFile(FILE *file,const char *format,...)
230 % A description of each parameter follows.
234 % o format: A file describing the format to use to write the remaining
239 MagickPrivate ssize_t FormatLocaleFileList(FILE *file,
240 const char *restrict format,va_list operands)
245 #if defined(MAGICKCORE_HAVE_VFPRINTF_L)
250 locale=AcquireCLocale();
251 if (locale == (locale_t) NULL)
252 n=(ssize_t) vfprintf(file,format,operands);
254 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
255 n=(ssize_t) vfprintf_l(file,format,locale,operands);
257 n=(ssize_t) vfprintf_l(file,locale,format,operands);
261 #if defined(MAGICKCORE_HAVE_USELOCALE)
267 locale=AcquireCLocale();
268 if (locale == (locale_t) NULL)
269 n=(ssize_t) vfprintf(file,format,operands);
272 previous_locale=uselocale(locale);
273 n=(ssize_t) vfprintf(file,format,operands);
274 uselocale(previous_locale);
278 n=(ssize_t) vfprintf(file,format,operands);
284 MagickExport ssize_t FormatLocaleFile(FILE *file,const char *restrict format,
293 va_start(operands,format);
294 n=FormatLocaleFileList(file,format,operands);
300 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
304 + F o r m a t L o c a l e S t r i n g %
308 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
310 % FormatLocaleString() prints formatted output of a variable argument list to
311 % a string buffer in the "C" locale.
313 % The format of the FormatLocaleString method is:
315 % ssize_t FormatLocaleString(char *string,const size_t length,
316 % const char *format,...)
318 % A description of each parameter follows.
320 % o string: FormatLocaleString() returns the formatted string in this
323 % o length: the maximum length of the string.
325 % o format: A string describing the format to use to write the remaining
330 MagickPrivate ssize_t FormatLocaleStringList(char *restrict string,
331 const size_t length,const char *restrict format,va_list operands)
336 #if defined(MAGICKCORE_HAVE_VSNPRINTF_L)
341 locale=AcquireCLocale();
342 if (locale == (locale_t) NULL)
343 n=(ssize_t) vsnprintf(string,length,format,operands);
345 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
346 n=(ssize_t) vsnprintf_l(string,length,format,locale,operands);
348 n=(ssize_t) vsnprintf_l(string,length,locale,format,operands);
351 #elif defined(MAGICKCORE_HAVE_VSNPRINTF)
352 #if defined(MAGICKCORE_HAVE_USELOCALE)
358 locale=AcquireCLocale();
359 if (locale == (locale_t) NULL)
360 n=(ssize_t) vsnprintf(string,length,format,operands);
363 previous_locale=uselocale(locale);
364 n=(ssize_t) vsnprintf(string,length,format,operands);
365 uselocale(previous_locale);
369 n=(ssize_t) vsnprintf(string,length,format,operands);
372 n=(ssize_t) vsprintf(string,format,operands);
375 string[length-1]='\0';
379 MagickExport ssize_t FormatLocaleString(char *restrict string,
380 const size_t length,const char *restrict format,...)
388 va_start(operands,format);
389 n=FormatLocaleStringList(string,length,format,operands);
395 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
399 + G e t L o c a l e I n f o _ %
403 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
405 % GetLocaleInfo_() searches the locale list for the specified tag and if
406 % found returns attributes for that element.
408 % The format of the GetLocaleInfo method is:
410 % const LocaleInfo *GetLocaleInfo_(const char *tag,
411 % ExceptionInfo *exception)
413 % A description of each parameter follows:
415 % o tag: the locale tag.
417 % o exception: return any errors or warnings in this structure.
420 MagickExport const LocaleInfo *GetLocaleInfo_(const char *tag,
421 ExceptionInfo *exception)
423 assert(exception != (ExceptionInfo *) NULL);
424 if (locale_list == (SplayTreeInfo *) NULL)
425 if (InitializeLocaleList(exception) == MagickFalse)
426 return((const LocaleInfo *) NULL);
427 if ((tag == (const char *) NULL) || (LocaleCompare(tag,"*") == 0))
429 ResetSplayTreeIterator(locale_list);
430 return((const LocaleInfo *) GetNextValueInSplayTree(locale_list));
432 return((const LocaleInfo *) GetValueFromSplayTree(locale_list,tag));
436 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
440 % G e t L o c a l e I n f o L i s t %
444 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
446 % GetLocaleInfoList() returns any locale messages that match the
449 % The format of the GetLocaleInfoList function is:
451 % const LocaleInfo **GetLocaleInfoList(const char *pattern,
452 % size_t *number_messages,ExceptionInfo *exception)
454 % A description of each parameter follows:
456 % o pattern: Specifies a pointer to a text string containing a pattern.
458 % o number_messages: This integer returns the number of locale messages in
461 % o exception: return any errors or warnings in this structure.
465 #if defined(__cplusplus) || defined(c_plusplus)
469 static int LocaleInfoCompare(const void *x,const void *y)
475 p=(const LocaleInfo **) x,
476 q=(const LocaleInfo **) y;
477 if (LocaleCompare((*p)->path,(*q)->path) == 0)
478 return(LocaleCompare((*p)->tag,(*q)->tag));
479 return(LocaleCompare((*p)->path,(*q)->path));
482 #if defined(__cplusplus) || defined(c_plusplus)
486 MagickExport const LocaleInfo **GetLocaleInfoList(const char *pattern,
487 size_t *number_messages,ExceptionInfo *exception)
492 register const LocaleInfo
499 Allocate locale list.
501 assert(pattern != (char *) NULL);
502 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
503 assert(number_messages != (size_t *) NULL);
505 p=GetLocaleInfo_("*",exception);
506 if (p == (const LocaleInfo *) NULL)
507 return((const LocaleInfo **) NULL);
508 messages=(const LocaleInfo **) AcquireQuantumMemory((size_t)
509 GetNumberOfNodesInSplayTree(locale_list)+1UL,sizeof(*messages));
510 if (messages == (const LocaleInfo **) NULL)
511 return((const LocaleInfo **) NULL);
513 Generate locale list.
515 LockSemaphoreInfo(locale_semaphore);
516 ResetSplayTreeIterator(locale_list);
517 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_list);
518 for (i=0; p != (const LocaleInfo *) NULL; )
520 if ((p->stealth == MagickFalse) &&
521 (GlobExpression(p->tag,pattern,MagickTrue) != MagickFalse))
523 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_list);
525 UnlockSemaphoreInfo(locale_semaphore);
526 qsort((void *) messages,(size_t) i,sizeof(*messages),LocaleInfoCompare);
527 messages[i]=(LocaleInfo *) NULL;
528 *number_messages=(size_t) i;
533 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
537 % G e t L o c a l e L i s t %
541 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
543 % GetLocaleList() returns any locale messages that match the specified
546 % The format of the GetLocaleList function is:
548 % char **GetLocaleList(const char *pattern,size_t *number_messages,
549 % Exceptioninfo *exception)
551 % A description of each parameter follows:
553 % o pattern: Specifies a pointer to a text string containing a pattern.
555 % o number_messages: This integer returns the number of messages in the
558 % o exception: return any errors or warnings in this structure.
562 #if defined(__cplusplus) || defined(c_plusplus)
566 static int LocaleTagCompare(const void *x,const void *y)
574 return(LocaleCompare(*p,*q));
577 #if defined(__cplusplus) || defined(c_plusplus)
581 MagickExport char **GetLocaleList(const char *pattern,
582 size_t *number_messages,ExceptionInfo *exception)
587 register const LocaleInfo
594 Allocate locale list.
596 assert(pattern != (char *) NULL);
597 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
598 assert(number_messages != (size_t *) NULL);
600 p=GetLocaleInfo_("*",exception);
601 if (p == (const LocaleInfo *) NULL)
602 return((char **) NULL);
603 messages=(char **) AcquireQuantumMemory((size_t)
604 GetNumberOfNodesInSplayTree(locale_list)+1UL,sizeof(*messages));
605 if (messages == (char **) NULL)
606 return((char **) NULL);
607 LockSemaphoreInfo(locale_semaphore);
608 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_list);
609 for (i=0; p != (const LocaleInfo *) NULL; )
611 if ((p->stealth == MagickFalse) &&
612 (GlobExpression(p->tag,pattern,MagickTrue) != MagickFalse))
613 messages[i++]=ConstantString(p->tag);
614 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_list);
616 UnlockSemaphoreInfo(locale_semaphore);
617 qsort((void *) messages,(size_t) i,sizeof(*messages),LocaleTagCompare);
618 messages[i]=(char *) NULL;
619 *number_messages=(size_t) i;
624 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
628 % G e t L o c a l e M e s s a g e %
632 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
634 % GetLocaleMessage() returns a message in the current locale that matches the
637 % The format of the GetLocaleMessage method is:
639 % const char *GetLocaleMessage(const char *tag)
641 % A description of each parameter follows:
643 % o tag: Return a message that matches this tag in the current locale.
646 MagickExport const char *GetLocaleMessage(const char *tag)
657 if ((tag == (const char *) NULL) || (*tag == '\0'))
659 exception=AcquireExceptionInfo();
660 (void) FormatLocaleString(name,MaxTextExtent,"%s/",tag);
661 locale_info=GetLocaleInfo_(name,exception);
662 exception=DestroyExceptionInfo(exception);
663 if (locale_info != (const LocaleInfo *) NULL)
664 return(locale_info->message);
669 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
673 % G e t L o c a l e O p t i o n s %
677 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
679 % GetLocaleOptions() returns any Magick configuration messages associated
680 % with the specified filename.
682 % The format of the GetLocaleOptions method is:
684 % LinkedListInfo *GetLocaleOptions(const char *filename,
685 % ExceptionInfo *exception)
687 % A description of each parameter follows:
689 % o filename: the locale file tag.
691 % o exception: return any errors or warnings in this structure.
694 MagickExport LinkedListInfo *GetLocaleOptions(const char *filename,
695 ExceptionInfo *exception)
710 assert(filename != (const char *) NULL);
711 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
712 assert(exception != (ExceptionInfo *) NULL);
713 (void) CopyMagickString(path,filename,MaxTextExtent);
715 Load XML from configuration files to linked-list.
717 messages=NewLinkedList(0);
718 paths=GetConfigurePaths(filename,exception);
719 if (paths != (LinkedListInfo *) NULL)
721 ResetLinkedListIterator(paths);
722 element=(const char *) GetNextValueInLinkedList(paths);
723 while (element != (const char *) NULL)
725 (void) FormatLocaleString(path,MaxTextExtent,"%s%s",element,filename);
726 (void) LogMagickEvent(LocaleEvent,GetMagickModule(),
727 "Searching for locale file: \"%s\"",path);
728 xml=ConfigureFileToStringInfo(path);
729 if (xml != (StringInfo *) NULL)
730 (void) AppendValueToLinkedList(messages,xml);
731 element=(const char *) GetNextValueInLinkedList(paths);
733 paths=DestroyLinkedList(paths,RelinquishMagickMemory);
735 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
740 blob=(char *) NTResourceToBlob(filename);
741 if (blob != (char *) NULL)
743 xml=AcquireStringInfo(0);
744 SetStringInfoLength(xml,strlen(blob)+1);
745 SetStringInfoDatum(xml,(unsigned char *) blob);
746 SetStringInfoPath(xml,filename);
747 (void) AppendValueToLinkedList(messages,xml);
751 ResetLinkedListIterator(messages);
756 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
760 % G e t L o c a l e V a l u e %
764 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
766 % GetLocaleValue() returns the message associated with the locale info.
768 % The format of the GetLocaleValue method is:
770 % const char *GetLocaleValue(const LocaleInfo *locale_info)
772 % A description of each parameter follows:
774 % o locale_info: The locale info.
777 MagickExport const char *GetLocaleValue(const LocaleInfo *locale_info)
779 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
780 assert(locale_info != (LocaleInfo *) NULL);
781 assert(locale_info->signature == MagickSignature);
782 return(locale_info->message);
786 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
790 + I n i t i a l i z e L o c a l e L i s t %
794 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
796 % InitializeLocaleList() initializes the locale list.
798 % The format of the InitializeLocaleList method is:
800 % MagickBooleanType InitializeLocaleList(ExceptionInfo *exception)
802 % A description of each parameter follows.
804 % o exception: return any errors or warnings in this structure.
807 static MagickBooleanType InitializeLocaleList(ExceptionInfo *exception)
809 if (locale_semaphore == (SemaphoreInfo *) NULL)
810 ActivateSemaphoreInfo(&locale_semaphore);
811 LockSemaphoreInfo(locale_semaphore);
812 if (locale_list == (SplayTreeInfo *) NULL)
820 locale=(char *) NULL;
821 p=setlocale(LC_CTYPE,(const char *) NULL);
822 if (p != (const char *) NULL)
823 locale=ConstantString(p);
824 if (locale == (char *) NULL)
825 locale=GetEnvironmentValue("LC_ALL");
826 if (locale == (char *) NULL)
827 locale=GetEnvironmentValue("LC_MESSAGES");
828 if (locale == (char *) NULL)
829 locale=GetEnvironmentValue("LC_CTYPE");
830 if (locale == (char *) NULL)
831 locale=GetEnvironmentValue("LANG");
832 if (locale == (char *) NULL)
833 locale=ConstantString("C");
834 (void) LoadLocaleLists(LocaleFilename,locale,exception);
835 locale=DestroyString(locale);
837 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)
879 if ((*string == '0') && ((string[1] | 0x20)=='x'))
880 value=(double) strtoul(string,&q,16);
883 #if defined(MAGICKCORE_HAVE_STRTOD_L)
887 locale=AcquireCLocale();
888 if (locale == (locale_t) NULL)
889 value=strtod(string,&q);
891 value=strtod_l(string,&q,locale);
893 value=strtod(string,&q);
896 if (sentinal != (char **) NULL)
902 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
906 % L i s t L o c a l e I n f o %
910 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
912 % ListLocaleInfo() lists the locale info to a file.
914 % The format of the ListLocaleInfo method is:
916 % MagickBooleanType ListLocaleInfo(FILE *file,ExceptionInfo *exception)
918 % A description of each parameter follows.
920 % o file: An pointer to a FILE.
922 % o exception: return any errors or warnings in this structure.
925 MagickExport MagickBooleanType ListLocaleInfo(FILE *file,
926 ExceptionInfo *exception)
940 if (file == (const FILE *) NULL)
943 locale_info=GetLocaleInfoList("*",&number_messages,exception);
944 if (locale_info == (const LocaleInfo **) NULL)
946 path=(const char *) NULL;
947 for (i=0; i < (ssize_t) number_messages; i++)
949 if (locale_info[i]->stealth != MagickFalse)
951 if ((path == (const char *) NULL) ||
952 (LocaleCompare(path,locale_info[i]->path) != 0))
954 if (locale_info[i]->path != (char *) NULL)
955 (void) FormatLocaleFile(file,"\nPath: %s\n\n",locale_info[i]->path);
956 (void) FormatLocaleFile(file,"Tag/Message\n");
957 (void) FormatLocaleFile(file,
958 "-------------------------------------------------"
959 "------------------------------\n");
961 path=locale_info[i]->path;
962 (void) FormatLocaleFile(file,"%s\n",locale_info[i]->tag);
963 if (locale_info[i]->message != (char *) NULL)
964 (void) FormatLocaleFile(file," %s",locale_info[i]->message);
965 (void) FormatLocaleFile(file,"\n");
968 locale_info=(const LocaleInfo **)
969 RelinquishMagickMemory((void *) locale_info);
974 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
978 + L o a d L o c a l e L i s t %
982 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
984 % LoadLocaleList() loads the locale configuration file which provides a mapping
985 % between locale attributes and a locale name.
987 % The format of the LoadLocaleList method is:
989 % MagickBooleanType LoadLocaleList(const char *xml,const char *filename,
990 % const size_t depth,ExceptionInfo *exception)
992 % A description of each parameter follows:
994 % o xml: The locale list in XML format.
996 % o filename: The locale list filename.
998 % o depth: depth of <include /> statements.
1000 % o exception: return any errors or warnings in this structure.
1004 static void ChopLocaleComponents(char *path,const size_t components)
1014 p=path+strlen(path)-1;
1017 for (count=0; (count < (ssize_t) components) && (p > path); p--)
1023 if (count < (ssize_t) components)
1027 static void *DestroyLocaleNode(void *locale_info)
1032 p=(LocaleInfo *) locale_info;
1033 if (p->path != (char *) NULL)
1034 p->path=DestroyString(p->path);
1035 if (p->tag != (char *) NULL)
1036 p->tag=DestroyString(p->tag);
1037 if (p->message != (char *) NULL)
1038 p->message=DestroyString(p->message);
1039 return(RelinquishMagickMemory(p));
1042 static void LocaleFatalErrorHandler(
1043 const ExceptionType magick_unused(severity),
1044 const char *reason,const char *description)
1046 if (reason == (char *) NULL)
1048 (void) FormatLocaleFile(stderr,"%s: %s",GetClientName(),reason);
1049 if (description != (char *) NULL)
1050 (void) FormatLocaleFile(stderr," (%s)",description);
1051 (void) FormatLocaleFile(stderr,".\n");
1052 (void) fflush(stderr);
1056 static inline size_t MagickMin(const size_t x,const size_t y)
1063 static MagickBooleanType LoadLocaleList(const char *xml,const char *filename,
1064 const char *locale,const size_t depth,ExceptionInfo *exception)
1067 keyword[MaxTextExtent],
1068 message[MaxTextExtent],
1088 Read the locale configure file.
1090 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1091 "Loading locale configure file \"%s\" ...",filename);
1092 if (xml == (const char *) NULL)
1093 return(MagickFalse);
1094 if (locale_list == (SplayTreeInfo *) NULL)
1096 locale_list=NewSplayTree(CompareSplayTreeString,(void *(*)(void *)) NULL,
1098 if (locale_list == (SplayTreeInfo *) NULL)
1099 return(MagickFalse);
1102 locale_info=(LocaleInfo *) NULL;
1106 fatal_handler=SetFatalErrorHandler(LocaleFatalErrorHandler);
1107 token=AcquireString(xml);
1108 for (q=(char *) xml; *q != '\0'; )
1113 GetMagickToken(q,&q,token);
1116 (void) CopyMagickString(keyword,token,MaxTextExtent);
1117 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
1122 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
1124 GetMagickToken(q,&q,token);
1125 while (isspace((int) ((unsigned char) *q)) != 0)
1130 if (LocaleNCompare(keyword,"<!--",4) == 0)
1135 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
1137 GetMagickToken(q,&q,token);
1138 while (isspace((int) ((unsigned char) *q)) != 0)
1143 if (LocaleCompare(keyword,"<include") == 0)
1148 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
1150 (void) CopyMagickString(keyword,token,MaxTextExtent);
1151 GetMagickToken(q,&q,token);
1154 GetMagickToken(q,&q,token);
1155 if (LocaleCompare(keyword,"locale") == 0)
1157 if (LocaleCompare(locale,token) != 0)
1161 if (LocaleCompare(keyword,"file") == 0)
1164 (void) ThrowMagickException(exception,GetMagickModule(),
1165 ConfigureError,"IncludeElementNestedTooDeeply","`%s'",token);
1169 path[MaxTextExtent],
1173 GetPathComponent(filename,HeadPath,path);
1175 (void) ConcatenateMagickString(path,DirectorySeparator,
1177 if (*token == *DirectorySeparator)
1178 (void) CopyMagickString(path,token,MaxTextExtent);
1180 (void) ConcatenateMagickString(path,token,MaxTextExtent);
1181 xml=FileToString(path,~0UL,exception);
1182 if (xml != (char *) NULL)
1184 status=LoadLocaleList(xml,path,locale,depth+1,exception);
1185 xml=(char *) RelinquishMagickMemory(xml);
1192 if (LocaleCompare(keyword,"<locale") == 0)
1197 while ((*token != '>') && (*q != '\0'))
1199 (void) CopyMagickString(keyword,token,MaxTextExtent);
1200 GetMagickToken(q,&q,token);
1203 GetMagickToken(q,&q,token);
1207 if (LocaleCompare(keyword,"</locale>") == 0)
1209 ChopLocaleComponents(tag,1);
1210 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1213 if (LocaleCompare(keyword,"<localemap>") == 0)
1215 if (LocaleCompare(keyword,"</localemap>") == 0)
1217 if (LocaleCompare(keyword,"<message") == 0)
1222 while ((*token != '>') && (*q != '\0'))
1224 (void) CopyMagickString(keyword,token,MaxTextExtent);
1225 GetMagickToken(q,&q,token);
1228 GetMagickToken(q,&q,token);
1229 if (LocaleCompare(keyword,"name") == 0)
1231 (void) ConcatenateMagickString(tag,token,MaxTextExtent);
1232 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1235 for (p=(char *) q; (*q != '<') && (*q != '\0'); q++) ;
1236 while (isspace((int) ((unsigned char) *p)) != 0)
1239 while ((isspace((int) ((unsigned char) *q)) != 0) && (q > p))
1241 (void) CopyMagickString(message,p,MagickMin((size_t) (q-p+2),
1243 locale_info=(LocaleInfo *) AcquireMagickMemory(sizeof(*locale_info));
1244 if (locale_info == (LocaleInfo *) NULL)
1245 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1246 (void) ResetMagickMemory(locale_info,0,sizeof(*locale_info));
1247 locale_info->path=ConstantString(filename);
1248 locale_info->tag=ConstantString(tag);
1249 locale_info->message=ConstantString(message);
1250 locale_info->signature=MagickSignature;
1251 status=AddValueToSplayTree(locale_list,locale_info->tag,locale_info);
1252 if (status == MagickFalse)
1253 (void) ThrowMagickException(exception,GetMagickModule(),
1254 ResourceLimitError,"MemoryAllocationFailed","`%s'",
1256 (void) ConcatenateMagickString(tag,message,MaxTextExtent);
1257 (void) ConcatenateMagickString(tag,"\n",MaxTextExtent);
1261 if (LocaleCompare(keyword,"</message>") == 0)
1263 ChopLocaleComponents(tag,2);
1264 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1267 if (*keyword == '<')
1272 if (*(keyword+1) == '?')
1274 if (*(keyword+1) == '/')
1276 ChopLocaleComponents(tag,1);
1278 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1281 token[strlen(token)-1]='\0';
1282 (void) CopyMagickString(token,token+1,MaxTextExtent);
1283 (void) ConcatenateMagickString(tag,token,MaxTextExtent);
1284 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1287 GetMagickToken(q,(const char **) NULL,token);
1291 token=(char *) RelinquishMagickMemory(token);
1292 (void) SetFatalErrorHandler(fatal_handler);
1297 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1301 % L o a d L o c a l e L i s t s %
1305 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1307 % LoadLocaleList() loads one or more locale configuration file which
1308 % provides a mapping between locale attributes and a locale tag.
1310 % The format of the LoadLocaleLists method is:
1312 % MagickBooleanType LoadLocaleLists(const char *filename,
1313 % ExceptionInfo *exception)
1315 % A description of each parameter follows:
1317 % o filename: the font file tag.
1319 % o locale: the actual locale.
1321 % o exception: return any errors or warnings in this structure.
1324 static MagickBooleanType LoadLocaleLists(const char *filename,
1325 const char *locale,ExceptionInfo *exception)
1327 #if defined(MAGICKCORE_ZERO_CONFIGURATION_SUPPORT)
1328 return(LoadLocaleList(LocaleMap,"built-in",locale,0,exception));
1340 options=GetLocaleOptions(filename,exception);
1341 option=(const StringInfo *) GetNextValueInLinkedList(options);
1342 while (option != (const StringInfo *) NULL)
1344 status&=LoadLocaleList((const char *) GetStringInfoDatum(option),
1345 GetStringInfoPath(option),locale,0,exception);
1346 option=(const StringInfo *) GetNextValueInLinkedList(options);
1348 options=DestroyLocaleOptions(options);
1349 if ((locale_list == (SplayTreeInfo *) NULL) ||
1350 (GetNumberOfNodesInSplayTree(locale_list) == 0))
1352 options=GetLocaleOptions("english.xml",exception);
1353 option=(const StringInfo *) GetNextValueInLinkedList(options);
1354 while (option != (const StringInfo *) NULL)
1356 status&=LoadLocaleList((const char *) GetStringInfoDatum(option),
1357 GetStringInfoPath(option),locale,0,exception);
1358 option=(const StringInfo *) GetNextValueInLinkedList(options);
1360 options=DestroyLocaleOptions(options);
1362 if ((locale_list == (SplayTreeInfo *) NULL) ||
1363 (GetNumberOfNodesInSplayTree(locale_list) == 0))
1364 status&=LoadLocaleList(LocaleMap,"built-in",locale,0,exception);
1365 return(status != 0 ? MagickTrue : MagickFalse);
1370 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1374 + L o c a l e C o m p o n e n t G e n e s i s %
1378 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1380 % LocaleComponentGenesis() instantiates the locale component.
1382 % The format of the LocaleComponentGenesis method is:
1384 % MagickBooleanType LocaleComponentGenesis(void)
1387 MagickPrivate MagickBooleanType LocaleComponentGenesis(void)
1389 locale_semaphore=AcquireSemaphoreInfo();
1394 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1398 + L o c a l e C o m p o n e n t T e r m i n u s %
1402 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1404 % LocaleComponentTerminus() destroys the locale component.
1406 % The format of the LocaleComponentTerminus method is:
1408 % LocaleComponentTerminus(void)
1411 MagickPrivate void LocaleComponentTerminus(void)
1413 if (locale_semaphore == (SemaphoreInfo *) NULL)
1414 ActivateSemaphoreInfo(&locale_semaphore);
1415 LockSemaphoreInfo(locale_semaphore);
1416 if (locale_list != (SplayTreeInfo *) NULL)
1417 locale_list=DestroySplayTree(locale_list);
1418 #if defined(MAGICKCORE_HAVE_STRTOD_L)
1421 UnlockSemaphoreInfo(locale_semaphore);
1422 RelinquishSemaphoreInfo(&locale_semaphore);