2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % L OOO CCCC AAA L EEEEE %
8 % L O O C AAAAA L EEE %
10 % LLLLL OOO CCCC A A LLLLL EEEEE %
13 % MagickCore Image Locale Methods %
20 % Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #include "magick/studio.h"
43 #include "magick/blob.h"
44 #include "magick/client.h"
45 #include "magick/configure.h"
46 #include "magick/exception.h"
47 #include "magick/exception-private.h"
48 #include "magick/hashmap.h"
49 #include "magick/locale_.h"
50 #include "magick/log.h"
51 #include "magick/memory_.h"
52 #include "magick/semaphore.h"
53 #include "magick/splay-tree.h"
54 #include "magick/string_.h"
55 #include "magick/token.h"
56 #include "magick/utility.h"
57 #include "magick/xml-tree.h"
62 #define LocaleFilename "locale.xml"
63 #define MaxRecursionDepth 200
70 "<?xml version=\"1.0\"?>"
72 " <locale name=\"C\">"
74 " <Message name=\"\">"
81 *locale_semaphore = (SemaphoreInfo *) NULL;
84 *locale_list = (SplayTreeInfo *) NULL;
86 static volatile MagickBooleanType
87 instantiate_locale = MagickFalse;
89 static volatile locale_t
90 c_locale = (locale_t) NULL;
95 static MagickBooleanType
96 InitializeLocaleList(ExceptionInfo *),
97 LoadLocaleLists(const char *,const char *,ExceptionInfo *);
100 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
104 + A c q u i r e C L o c a l e %
108 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
110 % AcquireCLocale() allocates the C locale object, or (locale_t) 0 with
111 % errno set if it cannot be acquired.
113 % The format of the AcquireCLocale method is:
115 % locale_t AcquireCLocale(void)
118 static locale_t AcquireCLocale(void)
120 #if defined(MAGICKCORE_HAVE_NEWLOCALE)
121 if (c_locale == (locale_t) NULL)
122 c_locale=newlocale(LC_ALL_MASK,"C",(locale_t) 0);
123 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
124 if (c_locale == (locale_t) NULL)
125 c_locale=_create_locale(LC_ALL,"C");
131 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
135 + D e s t r o y C L o c a l e %
139 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
141 % DestroyCLocale() releases the resources allocated for a locale object
142 % returned by a call to the AcquireCLocale() method.
144 % The format of the DestroyCLocale method is:
146 % void DestroyCLocale(void)
149 static void DestroyCLocale(void)
151 #if defined(MAGICKCORE_HAVE_NEWLOCALE)
152 if (c_locale != (locale_t) NULL)
153 freelocale(c_locale);
154 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
155 if (c_locale != (locale_t) NULL)
156 _free_locale(c_locale);
158 c_locale=(locale_t) NULL;
162 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
166 % D e s t r o y L o c a l e O p t i o n s %
170 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
172 % DestroyLocaleOptions() releases memory associated with an locale
175 % The format of the DestroyProfiles method is:
177 % LinkedListInfo *DestroyLocaleOptions(Image *image)
179 % A description of each parameter follows:
181 % o image: the image.
185 static void *DestroyOptions(void *message)
187 return(DestroyStringInfo((StringInfo *) message));
190 MagickExport LinkedListInfo *DestroyLocaleOptions(LinkedListInfo *messages)
192 assert(messages != (LinkedListInfo *) NULL);
193 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
194 return(DestroyLinkedList(messages,DestroyOptions));
198 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
202 + F o r m a t L o c a l e F i l e %
206 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
208 % FormatLocaleFile() prints formatted output of a variable argument list to a
209 % file in the "C" locale.
211 % The format of the FormatLocaleFile method is:
213 % ssize_t FormatLocaleFile(FILE *file,const char *format,...)
215 % A description of each parameter follows.
219 % o format: A file describing the format to use to write the remaining
224 MagickExport ssize_t FormatLocaleFileList(FILE *file,const char *format,
230 #if defined(MAGICKCORE_HAVE_VFPRINTF_L)
235 locale=AcquireCLocale();
236 if (locale == (locale_t) NULL)
237 n=vfprintf(file,format,operands);
239 n=vfprintf_l(file,format,locale,operands);
242 #if defined(MAGICKCORE_HAVE_USELOCALE)
248 locale=AcquireCLocale();
249 if (locale == (locale_t) NULL)
250 n=vfprintf(file,format,operands);
253 previous_locale=uselocale(locale);
254 n=vfprintf(file,format,operands);
255 uselocale(previous_locale);
259 n=fprintf(file,format,operands);
265 MagickExport ssize_t FormatLocaleFile(FILE *file,const char *format,...)
273 va_start(operands,format);
274 n=(ssize_t) FormatLocaleFileList(file,format,operands);
280 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
284 + F o r m a t L o c a l e S t r i n g %
288 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
290 % FormatLocaleString() prints formatted output of a variable argument list to
291 % a string buffer in the "C" locale.
293 % The format of the FormatLocaleString method is:
295 % ssize_t FormatLocaleString(char *string,const size_t length,
296 % const char *format,...)
298 % A description of each parameter follows.
300 % o string: FormatLocaleString() returns the formatted string in this
303 % o length: the maximum length of the string.
305 % o format: A string describing the format to use to write the remaining
310 MagickExport ssize_t FormatLocaleStringList(char *string,const size_t length,
311 const char *format,va_list operands)
316 #if defined(MAGICKCORE_HAVE_VSNPRINTF_L)
317 n=vsnprintf_l(string,length,format,(locale_t) NULL,operands);
318 #elif defined(MAGICKCORE_HAVE_VSNPRINTF)
319 #if defined(MAGICKCORE_HAVE_USELOCALE)
325 locale=AcquireCLocale();
326 if (locale == (locale_t) NULL)
327 n=vsnprintf(string,length,format,operands);
330 previous_locale=uselocale(locale);
331 n=vsnprintf(string,length,format,operands);
332 uselocale(previous_locale);
336 n=vsnprintf(string,length,format,operands);
339 n=vsprintf(string,format,operands);
342 string[length-1]='\0';
346 MagickExport ssize_t FormatLocaleString(char *string,const size_t length,
347 const char *format,...)
355 va_start(operands,format);
356 n=(ssize_t) FormatLocaleStringList(string,length,format,operands);
362 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
366 + G e t L o c a l e I n f o _ %
370 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
372 % GetLocaleInfo_() searches the locale list for the specified tag and if
373 % found returns attributes for that element.
375 % The format of the GetLocaleInfo method is:
377 % const LocaleInfo *GetLocaleInfo_(const char *tag,
378 % ExceptionInfo *exception)
380 % A description of each parameter follows:
382 % o tag: the locale tag.
384 % o exception: return any errors or warnings in this structure.
387 MagickExport const LocaleInfo *GetLocaleInfo_(const char *tag,
388 ExceptionInfo *exception)
390 assert(exception != (ExceptionInfo *) NULL);
391 if ((locale_list == (SplayTreeInfo *) NULL) ||
392 (instantiate_locale == MagickFalse))
393 if (InitializeLocaleList(exception) == MagickFalse)
394 return((const LocaleInfo *) NULL);
395 if ((locale_list == (SplayTreeInfo *) NULL) ||
396 (GetNumberOfNodesInSplayTree(locale_list) == 0))
397 return((const LocaleInfo *) NULL);
398 if ((tag == (const char *) NULL) || (LocaleCompare(tag,"*") == 0))
400 ResetSplayTreeIterator(locale_list);
401 return((const LocaleInfo *) GetNextValueInSplayTree(locale_list));
403 return((const LocaleInfo *) GetValueFromSplayTree(locale_list,tag));
407 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
411 % G e t L o c a l e I n f o L i s t %
415 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
417 % GetLocaleInfoList() returns any locale messages that match the
420 % The format of the GetLocaleInfoList function is:
422 % const LocaleInfo **GetLocaleInfoList(const char *pattern,
423 % size_t *number_messages,ExceptionInfo *exception)
425 % A description of each parameter follows:
427 % o pattern: Specifies a pointer to a text string containing a pattern.
429 % o number_messages: This integer returns the number of locale messages in
432 % o exception: return any errors or warnings in this structure.
436 #if defined(__cplusplus) || defined(c_plusplus)
440 static int LocaleInfoCompare(const void *x,const void *y)
446 p=(const LocaleInfo **) x,
447 q=(const LocaleInfo **) y;
448 if (LocaleCompare((*p)->path,(*q)->path) == 0)
449 return(LocaleCompare((*p)->tag,(*q)->tag));
450 return(LocaleCompare((*p)->path,(*q)->path));
453 #if defined(__cplusplus) || defined(c_plusplus)
457 MagickExport const LocaleInfo **GetLocaleInfoList(const char *pattern,
458 size_t *number_messages,ExceptionInfo *exception)
463 register const LocaleInfo
470 Allocate locale list.
472 assert(pattern != (char *) NULL);
473 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
474 assert(number_messages != (size_t *) NULL);
476 p=GetLocaleInfo_("*",exception);
477 if (p == (const LocaleInfo *) NULL)
478 return((const LocaleInfo **) NULL);
479 messages=(const LocaleInfo **) AcquireQuantumMemory((size_t)
480 GetNumberOfNodesInSplayTree(locale_list)+1UL,sizeof(*messages));
481 if (messages == (const LocaleInfo **) NULL)
482 return((const LocaleInfo **) NULL);
484 Generate locale list.
486 LockSemaphoreInfo(locale_semaphore);
487 ResetSplayTreeIterator(locale_list);
488 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_list);
489 for (i=0; p != (const LocaleInfo *) NULL; )
491 if ((p->stealth == MagickFalse) &&
492 (GlobExpression(p->tag,pattern,MagickTrue) != MagickFalse))
494 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_list);
496 UnlockSemaphoreInfo(locale_semaphore);
497 qsort((void *) messages,(size_t) i,sizeof(*messages),LocaleInfoCompare);
498 messages[i]=(LocaleInfo *) NULL;
499 *number_messages=(size_t) i;
504 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
508 % G e t L o c a l e L i s t %
512 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
514 % GetLocaleList() returns any locale messages that match the specified
517 % The format of the GetLocaleList function is:
519 % char **GetLocaleList(const char *pattern,size_t *number_messages,
520 % Exceptioninfo *exception)
522 % A description of each parameter follows:
524 % o pattern: Specifies a pointer to a text string containing a pattern.
526 % o number_messages: This integer returns the number of messages in the
529 % o exception: return any errors or warnings in this structure.
533 #if defined(__cplusplus) || defined(c_plusplus)
537 static int LocaleTagCompare(const void *x,const void *y)
545 return(LocaleCompare(*p,*q));
548 #if defined(__cplusplus) || defined(c_plusplus)
552 MagickExport char **GetLocaleList(const char *pattern,
553 size_t *number_messages,ExceptionInfo *exception)
558 register const LocaleInfo
565 Allocate locale list.
567 assert(pattern != (char *) NULL);
568 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
569 assert(number_messages != (size_t *) NULL);
571 p=GetLocaleInfo_("*",exception);
572 if (p == (const LocaleInfo *) NULL)
573 return((char **) NULL);
574 messages=(char **) AcquireQuantumMemory((size_t)
575 GetNumberOfNodesInSplayTree(locale_list)+1UL,sizeof(*messages));
576 if (messages == (char **) NULL)
577 return((char **) NULL);
578 LockSemaphoreInfo(locale_semaphore);
579 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_list);
580 for (i=0; p != (const LocaleInfo *) NULL; )
582 if ((p->stealth == MagickFalse) &&
583 (GlobExpression(p->tag,pattern,MagickTrue) != MagickFalse))
584 messages[i++]=ConstantString(p->tag);
585 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_list);
587 UnlockSemaphoreInfo(locale_semaphore);
588 qsort((void *) messages,(size_t) i,sizeof(*messages),LocaleTagCompare);
589 messages[i]=(char *) NULL;
590 *number_messages=(size_t) i;
595 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
599 % G e t L o c a l e M e s s a g e %
603 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
605 % GetLocaleMessage() returns a message in the current locale that matches the
608 % The format of the GetLocaleMessage method is:
610 % const char *GetLocaleMessage(const char *tag)
612 % A description of each parameter follows:
614 % o tag: Return a message that matches this tag in the current locale.
617 MagickExport const char *GetLocaleMessage(const char *tag)
628 if ((tag == (const char *) NULL) || (*tag == '\0'))
630 exception=AcquireExceptionInfo();
631 (void) FormatLocaleString(name,MaxTextExtent,"%s/",tag);
632 locale_info=GetLocaleInfo_(name,exception);
633 exception=DestroyExceptionInfo(exception);
634 if (locale_info != (const LocaleInfo *) NULL)
635 return(locale_info->message);
640 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
644 % G e t L o c a l e O p t i o n s %
648 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
650 % GetLocaleOptions() returns any Magick configuration messages associated
651 % with the specified filename.
653 % The format of the GetLocaleOptions method is:
655 % LinkedListInfo *GetLocaleOptions(const char *filename,
656 % ExceptionInfo *exception)
658 % A description of each parameter follows:
660 % o filename: the locale file tag.
662 % o exception: return any errors or warnings in this structure.
665 MagickExport LinkedListInfo *GetLocaleOptions(const char *filename,
666 ExceptionInfo *exception)
681 assert(filename != (const char *) NULL);
682 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
683 assert(exception != (ExceptionInfo *) NULL);
684 (void) CopyMagickString(path,filename,MaxTextExtent);
686 Load XML from configuration files to linked-list.
688 messages=NewLinkedList(0);
689 paths=GetConfigurePaths(filename,exception);
690 if (paths != (LinkedListInfo *) NULL)
692 ResetLinkedListIterator(paths);
693 element=(const char *) GetNextValueInLinkedList(paths);
694 while (element != (const char *) NULL)
696 (void) FormatLocaleString(path,MaxTextExtent,"%s%s",element,filename);
697 (void) LogMagickEvent(LocaleEvent,GetMagickModule(),
698 "Searching for locale file: \"%s\"",path);
699 xml=ConfigureFileToStringInfo(path);
700 if (xml != (StringInfo *) NULL)
701 (void) AppendValueToLinkedList(messages,xml);
702 element=(const char *) GetNextValueInLinkedList(paths);
704 paths=DestroyLinkedList(paths,RelinquishMagickMemory);
706 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
711 blob=(char *) NTResourceToBlob(filename);
712 if (blob != (char *) NULL)
714 xml=StringToStringInfo(blob);
715 (void) AppendValueToLinkedList(messages,xml);
716 blob=DestroyString(blob);
720 ResetLinkedListIterator(messages);
725 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
729 % G e t L o c a l e V a l u e %
733 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
735 % GetLocaleValue() returns the message associated with the locale info.
737 % The format of the GetLocaleValue method is:
739 % const char *GetLocaleValue(const LocaleInfo *locale_info)
741 % A description of each parameter follows:
743 % o locale_info: The locale info.
746 MagickExport const char *GetLocaleValue(const LocaleInfo *locale_info)
748 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
749 assert(locale_info != (LocaleInfo *) NULL);
750 assert(locale_info->signature == MagickSignature);
751 return(locale_info->message);
755 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
759 + I n i t i a l i z e L o c a l e L i s t %
763 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
765 % InitializeLocaleList() initializes the locale list.
767 % The format of the InitializeLocaleList method is:
769 % MagickBooleanType InitializeLocaleList(ExceptionInfo *exception)
771 % A description of each parameter follows.
773 % o exception: return any errors or warnings in this structure.
776 static MagickBooleanType InitializeLocaleList(ExceptionInfo *exception)
778 if ((locale_list == (SplayTreeInfo *) NULL) &&
779 (instantiate_locale == MagickFalse))
781 if (locale_semaphore == (SemaphoreInfo *) NULL)
782 AcquireSemaphoreInfo(&locale_semaphore);
783 LockSemaphoreInfo(locale_semaphore);
784 if ((locale_list == (SplayTreeInfo *) NULL) &&
785 (instantiate_locale == MagickFalse))
793 locale=(char *) NULL;
794 p=setlocale(LC_CTYPE,(const char *) NULL);
795 if (p != (const char *) NULL)
796 locale=ConstantString(p);
797 if (locale == (char *) NULL)
798 locale=GetEnvironmentValue("LC_ALL");
799 if (locale == (char *) NULL)
800 locale=GetEnvironmentValue("LC_MESSAGES");
801 if (locale == (char *) NULL)
802 locale=GetEnvironmentValue("LC_CTYPE");
803 if (locale == (char *) NULL)
804 locale=GetEnvironmentValue("LANG");
805 if (locale == (char *) NULL)
806 locale=ConstantString("C");
807 (void) LoadLocaleLists(LocaleFilename,locale,exception);
808 locale=DestroyString(locale);
809 instantiate_locale=MagickTrue;
811 UnlockSemaphoreInfo(locale_semaphore);
813 return(locale_list != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
817 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
821 % L i s t L o c a l e I n f o %
825 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
827 % ListLocaleInfo() lists the locale info to a file.
829 % The format of the ListLocaleInfo method is:
831 % MagickBooleanType ListLocaleInfo(FILE *file,ExceptionInfo *exception)
833 % A description of each parameter follows.
835 % o file: An pointer to a FILE.
837 % o exception: return any errors or warnings in this structure.
840 MagickExport MagickBooleanType ListLocaleInfo(FILE *file,
841 ExceptionInfo *exception)
855 if (file == (const FILE *) NULL)
858 locale_info=GetLocaleInfoList("*",&number_messages,exception);
859 if (locale_info == (const LocaleInfo **) NULL)
861 path=(const char *) NULL;
862 for (i=0; i < (ssize_t) number_messages; i++)
864 if (locale_info[i]->stealth != MagickFalse)
866 if ((path == (const char *) NULL) ||
867 (LocaleCompare(path,locale_info[i]->path) != 0))
869 if (locale_info[i]->path != (char *) NULL)
870 (void) FormatLocaleFile(file,"\nPath: %s\n\n",locale_info[i]->path);
871 (void) FormatLocaleFile(file,"Tag/Message\n");
872 (void) FormatLocaleFile(file,
873 "-------------------------------------------------"
874 "------------------------------\n");
876 path=locale_info[i]->path;
877 (void) FormatLocaleFile(file,"%s\n",locale_info[i]->tag);
878 if (locale_info[i]->message != (char *) NULL)
879 (void) FormatLocaleFile(file," %s",locale_info[i]->message);
880 (void) FormatLocaleFile(file,"\n");
883 locale_info=(const LocaleInfo **)
884 RelinquishMagickMemory((void *) locale_info);
889 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
893 + L o a d L o c a l e L i s t %
897 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
899 % LoadLocaleList() loads the locale configuration file which provides a mapping
900 % between locale attributes and a locale name.
902 % The format of the LoadLocaleList method is:
904 % MagickBooleanType LoadLocaleList(const char *xml,const char *filename,
905 % const size_t depth,ExceptionInfo *exception)
907 % A description of each parameter follows:
909 % o xml: The locale list in XML format.
911 % o filename: The locale list filename.
913 % o depth: depth of <include /> statements.
915 % o exception: return any errors or warnings in this structure.
919 static void ChopLocaleComponents(char *path,const size_t components)
929 p=path+strlen(path)-1;
932 for (count=0; (count < (ssize_t) components) && (p > path); p--)
938 if (count < (ssize_t) components)
942 static void *DestroyLocaleNode(void *locale_info)
947 p=(LocaleInfo *) locale_info;
948 if (p->path != (char *) NULL)
949 p->path=DestroyString(p->path);
950 if (p->tag != (char *) NULL)
951 p->tag=DestroyString(p->tag);
952 if (p->message != (char *) NULL)
953 p->message=DestroyString(p->message);
954 return(RelinquishMagickMemory(p));
957 static void LocaleFatalErrorHandler(
958 const ExceptionType magick_unused(severity),
959 const char *reason,const char *description)
961 if (reason == (char *) NULL)
963 (void) FormatLocaleFile(stderr,"%s: %s",GetClientName(),reason);
964 if (description != (char *) NULL)
965 (void) FormatLocaleFile(stderr," (%s)",description);
966 (void) FormatLocaleFile(stderr,".\n");
967 (void) fflush(stderr);
972 static MagickBooleanType LoadLocaleList(const char *xml,const char *filename,
973 const char *locale,const size_t depth,ExceptionInfo *exception)
976 keyword[MaxTextExtent],
977 message[MaxTextExtent],
997 Read the locale configure file.
999 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1000 "Loading locale configure file \"%s\" ...",filename);
1001 if (xml == (const char *) NULL)
1002 return(MagickFalse);
1003 if (locale_list == (SplayTreeInfo *) NULL)
1005 locale_list=NewSplayTree(CompareSplayTreeString,(void *(*)(void *)) NULL,
1007 if (locale_list == (SplayTreeInfo *) NULL)
1008 return(MagickFalse);
1011 locale_info=(LocaleInfo *) NULL;
1015 fatal_handler=SetFatalErrorHandler(LocaleFatalErrorHandler);
1016 token=AcquireString(xml);
1017 for (q=(char *) xml; *q != '\0'; )
1022 GetMagickToken(q,&q,token);
1025 (void) CopyMagickString(keyword,token,MaxTextExtent);
1026 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
1031 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
1033 GetMagickToken(q,&q,token);
1034 while (isspace((int) ((unsigned char) *q)) != 0)
1039 if (LocaleNCompare(keyword,"<!--",4) == 0)
1044 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
1046 GetMagickToken(q,&q,token);
1047 while (isspace((int) ((unsigned char) *q)) != 0)
1052 if (LocaleCompare(keyword,"<include") == 0)
1057 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
1059 (void) CopyMagickString(keyword,token,MaxTextExtent);
1060 GetMagickToken(q,&q,token);
1063 GetMagickToken(q,&q,token);
1064 if (LocaleCompare(keyword,"locale") == 0)
1066 if (LocaleCompare(locale,token) != 0)
1070 if (LocaleCompare(keyword,"file") == 0)
1073 (void) ThrowMagickException(exception,GetMagickModule(),
1074 ConfigureError,"IncludeElementNestedTooDeeply","`%s'",token);
1078 path[MaxTextExtent],
1082 GetPathComponent(filename,HeadPath,path);
1084 (void) ConcatenateMagickString(path,DirectorySeparator,
1086 if (*token == *DirectorySeparator)
1087 (void) CopyMagickString(path,token,MaxTextExtent);
1089 (void) ConcatenateMagickString(path,token,MaxTextExtent);
1090 xml=FileToString(path,~0,exception);
1091 if (xml != (char *) NULL)
1093 status=LoadLocaleList(xml,path,locale,depth+1,exception);
1094 xml=(char *) RelinquishMagickMemory(xml);
1101 if (LocaleCompare(keyword,"<locale") == 0)
1106 while ((*token != '>') && (*q != '\0'))
1108 (void) CopyMagickString(keyword,token,MaxTextExtent);
1109 GetMagickToken(q,&q,token);
1112 GetMagickToken(q,&q,token);
1116 if (LocaleCompare(keyword,"</locale>") == 0)
1118 ChopLocaleComponents(tag,1);
1119 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1122 if (LocaleCompare(keyword,"<localemap>") == 0)
1124 if (LocaleCompare(keyword,"</localemap>") == 0)
1126 if (LocaleCompare(keyword,"<message") == 0)
1131 while ((*token != '>') && (*q != '\0'))
1133 (void) CopyMagickString(keyword,token,MaxTextExtent);
1134 GetMagickToken(q,&q,token);
1137 GetMagickToken(q,&q,token);
1138 if (LocaleCompare(keyword,"name") == 0)
1140 (void) ConcatenateMagickString(tag,token,MaxTextExtent);
1141 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1144 for (p=(char *) q; (*q != '<') && (*q != '\0'); q++) ;
1145 while (isspace((int) ((unsigned char) *p)) != 0)
1148 while ((isspace((int) ((unsigned char) *q)) != 0) && (q > p))
1150 (void) CopyMagickString(message,p,(size_t) (q-p+2));
1151 locale_info=(LocaleInfo *) AcquireMagickMemory(sizeof(*locale_info));
1152 if (locale_info == (LocaleInfo *) NULL)
1153 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1154 (void) ResetMagickMemory(locale_info,0,sizeof(*locale_info));
1155 locale_info->path=ConstantString(filename);
1156 locale_info->tag=ConstantString(tag);
1157 locale_info->message=ConstantString(message);
1158 locale_info->signature=MagickSignature;
1159 status=AddValueToSplayTree(locale_list,locale_info->tag,locale_info);
1160 if (status == MagickFalse)
1161 (void) ThrowMagickException(exception,GetMagickModule(),
1162 ResourceLimitError,"MemoryAllocationFailed","`%s'",
1164 (void) ConcatenateMagickString(tag,message,MaxTextExtent);
1165 (void) ConcatenateMagickString(tag,"\n",MaxTextExtent);
1169 if (LocaleCompare(keyword,"</message>") == 0)
1171 ChopLocaleComponents(tag,2);
1172 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1175 if (*keyword == '<')
1180 if (*(keyword+1) == '?')
1182 if (*(keyword+1) == '/')
1184 ChopLocaleComponents(tag,1);
1186 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1189 token[strlen(token)-1]='\0';
1190 (void) CopyMagickString(token,token+1,MaxTextExtent);
1191 (void) ConcatenateMagickString(tag,token,MaxTextExtent);
1192 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1195 GetMagickToken(q,(const char **) NULL,token);
1199 token=(char *) RelinquishMagickMemory(token);
1200 (void) SetFatalErrorHandler(fatal_handler);
1205 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1209 % L o a d L o c a l e L i s t s %
1213 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1215 % LoadLocaleList() loads one or more locale configuration file which
1216 % provides a mapping between locale attributes and a locale tag.
1218 % The format of the LoadLocaleLists method is:
1220 % MagickBooleanType LoadLocaleLists(const char *filename,
1221 % ExceptionInfo *exception)
1223 % A description of each parameter follows:
1225 % o filename: the font file tag.
1227 % o locale: the actual locale.
1229 % o exception: return any errors or warnings in this structure.
1232 static MagickBooleanType LoadLocaleLists(const char *filename,
1233 const char *locale,ExceptionInfo *exception)
1235 #if defined(MAGICKCORE_EMBEDDABLE_SUPPORT)
1236 return(LoadLocaleList(LocaleMap,"built-in",locale,0,exception));
1248 options=GetLocaleOptions(filename,exception);
1249 option=(const StringInfo *) GetNextValueInLinkedList(options);
1250 while (option != (const StringInfo *) NULL)
1252 status|=LoadLocaleList((const char *) GetStringInfoDatum(option),
1253 GetStringInfoPath(option),locale,0,exception);
1254 option=(const StringInfo *) GetNextValueInLinkedList(options);
1256 options=DestroyLocaleOptions(options);
1257 if ((locale_list == (SplayTreeInfo *) NULL) ||
1258 (GetNumberOfNodesInSplayTree(locale_list) == 0))
1260 options=GetLocaleOptions("english.xml",exception);
1261 option=(const StringInfo *) GetNextValueInLinkedList(options);
1262 while (option != (const StringInfo *) NULL)
1264 status|=LoadLocaleList((const char *) GetStringInfoDatum(option),
1265 GetStringInfoPath(option),locale,0,exception);
1266 option=(const StringInfo *) GetNextValueInLinkedList(options);
1268 options=DestroyLocaleOptions(options);
1270 if ((locale_list == (SplayTreeInfo *) NULL) ||
1271 (GetNumberOfNodesInSplayTree(locale_list) == 0))
1272 status|=LoadLocaleList(LocaleMap,"built-in",locale,0,exception);
1273 return(status != 0 ? MagickTrue : MagickFalse);
1278 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1282 + L o c a l e C o m p o n e n t G e n e s i s %
1286 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1288 % LocaleComponentGenesis() instantiates the locale component.
1290 % The format of the LocaleComponentGenesis method is:
1292 % MagickBooleanType LocaleComponentGenesis(void)
1295 MagickExport MagickBooleanType LocaleComponentGenesis(void)
1297 AcquireSemaphoreInfo(&locale_semaphore);
1302 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1306 + L o c a l e C o m p o n e n t T e r m i n u s %
1310 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1312 % LocaleComponentTerminus() destroys the locale component.
1314 % The format of the LocaleComponentTerminus method is:
1316 % LocaleComponentTerminus(void)
1319 MagickExport void LocaleComponentTerminus(void)
1321 if (locale_semaphore == (SemaphoreInfo *) NULL)
1322 AcquireSemaphoreInfo(&locale_semaphore);
1323 LockSemaphoreInfo(locale_semaphore);
1325 instantiate_locale=MagickFalse;
1326 UnlockSemaphoreInfo(locale_semaphore);
1327 DestroySemaphoreInfo(&locale_semaphore);
1331 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1335 + L o c a l e T o D o u b l e %
1339 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1341 % LocaleToDouble() interprets the string as a floating point number in the
1342 % "C" locale and returns its value as a double. If sentinal is not a null
1343 % pointer, the method also sets the value pointed by sentinal to point to the
1344 % first character after the number.
1346 % The format of the LocaleToDouble method is:
1348 % double LocaleToDouble(const char *value,char **sentinal)
1350 % A description of each parameter follows:
1352 % o value: the string value.
1354 % o sentinal: if sentinal is not NULL, a pointer to the character after the
1355 % last character used in the conversion is stored in the location
1356 % referenced by sentinal.
1359 MagickExport double LocaleToDouble(const char *string,char **sentinal)
1364 #if defined(MAGICKCORE_HAVE_STRTOD_L)
1369 locale=AcquireCLocale();
1370 if (locale == (locale_t) NULL)
1371 value=strtod(string,sentinal);
1373 value=strtod_l(string,sentinal,locale);
1376 value=strtod(string,sentinal);