2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % L OOO CCCC AAA L EEEEE %
8 % L O O C AAAAA L EEE %
10 % LLLLL OOO CCCC A A LLLLL EEEEE %
13 % MagickCore Image Locale Methods %
20 % Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #include "magick/studio.h"
43 #include "magick/blob.h"
44 #include "magick/client.h"
45 #include "magick/configure.h"
46 #include "magick/exception.h"
47 #include "magick/exception-private.h"
48 #include "magick/hashmap.h"
49 #include "magick/locale_.h"
50 #include "magick/log.h"
51 #include "magick/memory_.h"
52 #include "magick/semaphore.h"
53 #include "magick/splay-tree.h"
54 #include "magick/string_.h"
55 #include "magick/token.h"
56 #include "magick/utility.h"
57 #include "magick/xml-tree.h"
62 #define LocaleFilename "locale.xml"
63 #define MaxRecursionDepth 200
68 #if defined(__CYGWIN__)
69 typedef struct _locale_t *locale_t;
77 "<?xml version=\"1.0\"?>"
79 " <locale name=\"C\">"
81 " <Message name=\"\">"
88 *locale_semaphore = (SemaphoreInfo *) NULL;
91 *locale_list = (SplayTreeInfo *) NULL;
93 static volatile locale_t
94 c_locale = (locale_t) NULL;
96 static volatile MagickBooleanType
97 instantiate_locale = MagickFalse;
100 Forward declarations.
102 static MagickBooleanType
103 InitializeLocaleList(ExceptionInfo *),
104 LoadLocaleLists(const char *,const char *,ExceptionInfo *);
107 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
111 + A c q u i r e C L o c a l e %
115 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
117 % AcquireCLocale() allocates the C locale object, or (locale_t) 0 with
118 % errno set if it cannot be acquired.
120 % The format of the AcquireCLocale method is:
122 % locale_t AcquireCLocale(void)
125 static locale_t AcquireCLocale(void)
127 #if defined(MAGICKCORE_HAVE_NEWLOCALE)
128 if (c_locale == (locale_t) NULL)
129 c_locale=newlocale(LC_ALL_MASK,"C",(locale_t) 0);
130 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
131 if (c_locale == (locale_t) NULL)
132 c_locale=_create_locale(LC_ALL,"C");
138 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
142 + D e s t r o y C L o c a l e %
146 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
148 % DestroyCLocale() releases the resources allocated for a locale object
149 % returned by a call to the AcquireCLocale() method.
151 % The format of the DestroyCLocale method is:
153 % void DestroyCLocale(void)
156 static void DestroyCLocale(void)
158 #if defined(MAGICKCORE_HAVE_NEWLOCALE)
159 if (c_locale != (locale_t) NULL)
160 freelocale(c_locale);
161 #elif defined(MAGICKCORE_WINDOWS_SUPPORT)
162 if (c_locale != (locale_t) NULL)
163 _free_locale(c_locale);
165 c_locale=(locale_t) NULL;
169 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
173 % D e s t r o y L o c a l e O p t i o n s %
177 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
179 % DestroyLocaleOptions() releases memory associated with an locale
182 % The format of the DestroyProfiles method is:
184 % LinkedListInfo *DestroyLocaleOptions(Image *image)
186 % A description of each parameter follows:
188 % o image: the image.
192 static void *DestroyOptions(void *message)
194 return(DestroyStringInfo((StringInfo *) message));
197 MagickExport LinkedListInfo *DestroyLocaleOptions(LinkedListInfo *messages)
199 assert(messages != (LinkedListInfo *) NULL);
200 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
201 return(DestroyLinkedList(messages,DestroyOptions));
205 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
209 + F o r m a t L o c a l e F i l e %
213 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
215 % FormatLocaleFile() prints formatted output of a variable argument list to a
216 % file in the "C" locale.
218 % The format of the FormatLocaleFile method is:
220 % ssize_t FormatLocaleFile(FILE *file,const char *format,...)
222 % A description of each parameter follows.
226 % o format: A file describing the format to use to write the remaining
231 MagickExport ssize_t FormatLocaleFileList(FILE *file,
232 const char *restrict format,va_list operands)
237 #if defined(MAGICKCORE_HAVE_VFPRINTF_L)
242 locale=AcquireCLocale();
243 if (locale == (locale_t) NULL)
244 n=vfprintf(file,format,operands);
246 n=vfprintf_l(file,format,locale,operands);
249 #if defined(MAGICKCORE_HAVE_USELOCALE)
255 locale=AcquireCLocale();
256 if (locale == (locale_t) NULL)
257 n=vfprintf(file,format,operands);
260 previous_locale=uselocale(locale);
261 n=vfprintf(file,format,operands);
262 uselocale(previous_locale);
266 n=fprintf(file,format,operands);
272 MagickExport ssize_t FormatLocaleFile(FILE *file,const char *restrict format,
281 va_start(operands,format);
282 n=(ssize_t) FormatLocaleFileList(file,format,operands);
288 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
292 + F o r m a t L o c a l e S t r i n g %
296 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
298 % FormatLocaleString() prints formatted output of a variable argument list to
299 % a string buffer in the "C" locale.
301 % The format of the FormatLocaleString method is:
303 % ssize_t FormatLocaleString(char *string,const size_t length,
304 % const char *format,...)
306 % A description of each parameter follows.
308 % o string: FormatLocaleString() returns the formatted string in this
311 % o length: the maximum length of the string.
313 % o format: A string describing the format to use to write the remaining
318 MagickExport ssize_t FormatLocaleStringList(char *restrict string,
319 const size_t length,const char *restrict format,va_list operands)
324 #if defined(MAGICKCORE_HAVE_VSNPRINTF_L)
325 n=vsnprintf_l(string,length,format,(locale_t) NULL,operands);
326 #elif defined(MAGICKCORE_HAVE_VSNPRINTF)
327 #if defined(MAGICKCORE_HAVE_USELOCALE)
333 locale=AcquireCLocale();
334 if (locale == (locale_t) NULL)
335 n=vsnprintf(string,length,format,operands);
338 previous_locale=uselocale(locale);
339 n=vsnprintf(string,length,format,operands);
340 uselocale(previous_locale);
344 n=vsnprintf(string,length,format,operands);
347 n=vsprintf(string,format,operands);
350 string[length-1]='\0';
354 MagickExport ssize_t FormatLocaleString(char *restrict string,
355 const size_t length,const char *restrict format,...)
363 va_start(operands,format);
364 n=(ssize_t) FormatLocaleStringList(string,length,format,operands);
370 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
374 + G e t L o c a l e I n f o _ %
378 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
380 % GetLocaleInfo_() searches the locale list for the specified tag and if
381 % found returns attributes for that element.
383 % The format of the GetLocaleInfo method is:
385 % const LocaleInfo *GetLocaleInfo_(const char *tag,
386 % ExceptionInfo *exception)
388 % A description of each parameter follows:
390 % o tag: the locale tag.
392 % o exception: return any errors or warnings in this structure.
395 MagickExport const LocaleInfo *GetLocaleInfo_(const char *tag,
396 ExceptionInfo *exception)
398 assert(exception != (ExceptionInfo *) NULL);
399 if ((locale_list == (SplayTreeInfo *) NULL) ||
400 (instantiate_locale == MagickFalse))
401 if (InitializeLocaleList(exception) == MagickFalse)
402 return((const LocaleInfo *) NULL);
403 if ((locale_list == (SplayTreeInfo *) NULL) ||
404 (GetNumberOfNodesInSplayTree(locale_list) == 0))
405 return((const LocaleInfo *) NULL);
406 if ((tag == (const char *) NULL) || (LocaleCompare(tag,"*") == 0))
408 ResetSplayTreeIterator(locale_list);
409 return((const LocaleInfo *) GetNextValueInSplayTree(locale_list));
411 return((const LocaleInfo *) GetValueFromSplayTree(locale_list,tag));
415 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
419 % G e t L o c a l e I n f o L i s t %
423 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
425 % GetLocaleInfoList() returns any locale messages that match the
428 % The format of the GetLocaleInfoList function is:
430 % const LocaleInfo **GetLocaleInfoList(const char *pattern,
431 % size_t *number_messages,ExceptionInfo *exception)
433 % A description of each parameter follows:
435 % o pattern: Specifies a pointer to a text string containing a pattern.
437 % o number_messages: This integer returns the number of locale messages in
440 % o exception: return any errors or warnings in this structure.
444 #if defined(__cplusplus) || defined(c_plusplus)
448 static int LocaleInfoCompare(const void *x,const void *y)
454 p=(const LocaleInfo **) x,
455 q=(const LocaleInfo **) y;
456 if (LocaleCompare((*p)->path,(*q)->path) == 0)
457 return(LocaleCompare((*p)->tag,(*q)->tag));
458 return(LocaleCompare((*p)->path,(*q)->path));
461 #if defined(__cplusplus) || defined(c_plusplus)
465 MagickExport const LocaleInfo **GetLocaleInfoList(const char *pattern,
466 size_t *number_messages,ExceptionInfo *exception)
471 register const LocaleInfo
478 Allocate locale list.
480 assert(pattern != (char *) NULL);
481 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
482 assert(number_messages != (size_t *) NULL);
484 p=GetLocaleInfo_("*",exception);
485 if (p == (const LocaleInfo *) NULL)
486 return((const LocaleInfo **) NULL);
487 messages=(const LocaleInfo **) AcquireQuantumMemory((size_t)
488 GetNumberOfNodesInSplayTree(locale_list)+1UL,sizeof(*messages));
489 if (messages == (const LocaleInfo **) NULL)
490 return((const LocaleInfo **) NULL);
492 Generate locale list.
494 LockSemaphoreInfo(locale_semaphore);
495 ResetSplayTreeIterator(locale_list);
496 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_list);
497 for (i=0; p != (const LocaleInfo *) NULL; )
499 if ((p->stealth == MagickFalse) &&
500 (GlobExpression(p->tag,pattern,MagickTrue) != MagickFalse))
502 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_list);
504 UnlockSemaphoreInfo(locale_semaphore);
505 qsort((void *) messages,(size_t) i,sizeof(*messages),LocaleInfoCompare);
506 messages[i]=(LocaleInfo *) NULL;
507 *number_messages=(size_t) i;
512 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
516 % G e t L o c a l e L i s t %
520 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
522 % GetLocaleList() returns any locale messages that match the specified
525 % The format of the GetLocaleList function is:
527 % char **GetLocaleList(const char *pattern,size_t *number_messages,
528 % Exceptioninfo *exception)
530 % A description of each parameter follows:
532 % o pattern: Specifies a pointer to a text string containing a pattern.
534 % o number_messages: This integer returns the number of messages in the
537 % o exception: return any errors or warnings in this structure.
541 #if defined(__cplusplus) || defined(c_plusplus)
545 static int LocaleTagCompare(const void *x,const void *y)
553 return(LocaleCompare(*p,*q));
556 #if defined(__cplusplus) || defined(c_plusplus)
560 MagickExport char **GetLocaleList(const char *pattern,
561 size_t *number_messages,ExceptionInfo *exception)
566 register const LocaleInfo
573 Allocate locale list.
575 assert(pattern != (char *) NULL);
576 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
577 assert(number_messages != (size_t *) NULL);
579 p=GetLocaleInfo_("*",exception);
580 if (p == (const LocaleInfo *) NULL)
581 return((char **) NULL);
582 messages=(char **) AcquireQuantumMemory((size_t)
583 GetNumberOfNodesInSplayTree(locale_list)+1UL,sizeof(*messages));
584 if (messages == (char **) NULL)
585 return((char **) NULL);
586 LockSemaphoreInfo(locale_semaphore);
587 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_list);
588 for (i=0; p != (const LocaleInfo *) NULL; )
590 if ((p->stealth == MagickFalse) &&
591 (GlobExpression(p->tag,pattern,MagickTrue) != MagickFalse))
592 messages[i++]=ConstantString(p->tag);
593 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_list);
595 UnlockSemaphoreInfo(locale_semaphore);
596 qsort((void *) messages,(size_t) i,sizeof(*messages),LocaleTagCompare);
597 messages[i]=(char *) NULL;
598 *number_messages=(size_t) i;
603 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
607 % G e t L o c a l e M e s s a g e %
611 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
613 % GetLocaleMessage() returns a message in the current locale that matches the
616 % The format of the GetLocaleMessage method is:
618 % const char *GetLocaleMessage(const char *tag)
620 % A description of each parameter follows:
622 % o tag: Return a message that matches this tag in the current locale.
625 MagickExport const char *GetLocaleMessage(const char *tag)
636 if ((tag == (const char *) NULL) || (*tag == '\0'))
638 exception=AcquireExceptionInfo();
639 (void) FormatLocaleString(name,MaxTextExtent,"%s/",tag);
640 locale_info=GetLocaleInfo_(name,exception);
641 exception=DestroyExceptionInfo(exception);
642 if (locale_info != (const LocaleInfo *) NULL)
643 return(locale_info->message);
648 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
652 % G e t L o c a l e O p t i o n s %
656 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
658 % GetLocaleOptions() returns any Magick configuration messages associated
659 % with the specified filename.
661 % The format of the GetLocaleOptions method is:
663 % LinkedListInfo *GetLocaleOptions(const char *filename,
664 % ExceptionInfo *exception)
666 % A description of each parameter follows:
668 % o filename: the locale file tag.
670 % o exception: return any errors or warnings in this structure.
673 MagickExport LinkedListInfo *GetLocaleOptions(const char *filename,
674 ExceptionInfo *exception)
689 assert(filename != (const char *) NULL);
690 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
691 assert(exception != (ExceptionInfo *) NULL);
692 (void) CopyMagickString(path,filename,MaxTextExtent);
694 Load XML from configuration files to linked-list.
696 messages=NewLinkedList(0);
697 paths=GetConfigurePaths(filename,exception);
698 if (paths != (LinkedListInfo *) NULL)
700 ResetLinkedListIterator(paths);
701 element=(const char *) GetNextValueInLinkedList(paths);
702 while (element != (const char *) NULL)
704 (void) FormatLocaleString(path,MaxTextExtent,"%s%s",element,filename);
705 (void) LogMagickEvent(LocaleEvent,GetMagickModule(),
706 "Searching for locale file: \"%s\"",path);
707 xml=ConfigureFileToStringInfo(path);
708 if (xml != (StringInfo *) NULL)
709 (void) AppendValueToLinkedList(messages,xml);
710 element=(const char *) GetNextValueInLinkedList(paths);
712 paths=DestroyLinkedList(paths,RelinquishMagickMemory);
714 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
719 blob=(char *) NTResourceToBlob(filename);
720 if (blob != (char *) NULL)
722 xml=StringToStringInfo(blob);
723 (void) AppendValueToLinkedList(messages,xml);
724 blob=DestroyString(blob);
728 ResetLinkedListIterator(messages);
733 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
737 % G e t L o c a l e V a l u e %
741 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
743 % GetLocaleValue() returns the message associated with the locale info.
745 % The format of the GetLocaleValue method is:
747 % const char *GetLocaleValue(const LocaleInfo *locale_info)
749 % A description of each parameter follows:
751 % o locale_info: The locale info.
754 MagickExport const char *GetLocaleValue(const LocaleInfo *locale_info)
756 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
757 assert(locale_info != (LocaleInfo *) NULL);
758 assert(locale_info->signature == MagickSignature);
759 return(locale_info->message);
763 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
767 + I n i t i a l i z e L o c a l e L i s t %
771 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
773 % InitializeLocaleList() initializes the locale list.
775 % The format of the InitializeLocaleList method is:
777 % MagickBooleanType InitializeLocaleList(ExceptionInfo *exception)
779 % A description of each parameter follows.
781 % o exception: return any errors or warnings in this structure.
784 static MagickBooleanType InitializeLocaleList(ExceptionInfo *exception)
786 if ((locale_list == (SplayTreeInfo *) NULL) &&
787 (instantiate_locale == MagickFalse))
789 if (locale_semaphore == (SemaphoreInfo *) NULL)
790 AcquireSemaphoreInfo(&locale_semaphore);
791 LockSemaphoreInfo(locale_semaphore);
792 if ((locale_list == (SplayTreeInfo *) NULL) &&
793 (instantiate_locale == MagickFalse))
801 locale=(char *) NULL;
802 p=setlocale(LC_CTYPE,(const char *) NULL);
803 if (p != (const char *) NULL)
804 locale=ConstantString(p);
805 if (locale == (char *) NULL)
806 locale=GetEnvironmentValue("LC_ALL");
807 if (locale == (char *) NULL)
808 locale=GetEnvironmentValue("LC_MESSAGES");
809 if (locale == (char *) NULL)
810 locale=GetEnvironmentValue("LC_CTYPE");
811 if (locale == (char *) NULL)
812 locale=GetEnvironmentValue("LANG");
813 if (locale == (char *) NULL)
814 locale=ConstantString("C");
815 (void) LoadLocaleLists(LocaleFilename,locale,exception);
816 locale=DestroyString(locale);
817 instantiate_locale=MagickTrue;
819 UnlockSemaphoreInfo(locale_semaphore);
821 return(locale_list != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
825 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
829 + I n t e r p r e t L o c a l e V a l u e %
833 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
835 % InterpretLocaleValue() interprets the string as a floating point number in
836 % the "C" locale and returns its value as a double. If sentinal is not a null
837 % pointer, the method also sets the value pointed by sentinal to point to the
838 % first character after the number.
840 % The format of the InterpretLocaleValue method is:
842 % double InterpretLocaleValue(const char *value,char **sentinal)
844 % A description of each parameter follows:
846 % o value: the string value.
848 % o sentinal: if sentinal is not NULL, a pointer to the character after the
849 % last character used in the conversion is stored in the location
850 % referenced by sentinal.
853 MagickExport double InterpretLocaleValue(const char *restrict string,
854 char **restrict sentinal)
859 #if defined(MAGICKCORE_HAVE_STRTOD_L)
864 locale=AcquireCLocale();
865 if (locale == (locale_t) NULL)
866 value=strtod(string,sentinal);
868 value=strtod_l(string,sentinal,locale);
871 value=strtod(string,sentinal);
877 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
881 % L i s t L o c a l e I n f o %
885 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
887 % ListLocaleInfo() lists the locale info to a file.
889 % The format of the ListLocaleInfo method is:
891 % MagickBooleanType ListLocaleInfo(FILE *file,ExceptionInfo *exception)
893 % A description of each parameter follows.
895 % o file: An pointer to a FILE.
897 % o exception: return any errors or warnings in this structure.
900 MagickExport MagickBooleanType ListLocaleInfo(FILE *file,
901 ExceptionInfo *exception)
915 if (file == (const FILE *) NULL)
918 locale_info=GetLocaleInfoList("*",&number_messages,exception);
919 if (locale_info == (const LocaleInfo **) NULL)
921 path=(const char *) NULL;
922 for (i=0; i < (ssize_t) number_messages; i++)
924 if (locale_info[i]->stealth != MagickFalse)
926 if ((path == (const char *) NULL) ||
927 (LocaleCompare(path,locale_info[i]->path) != 0))
929 if (locale_info[i]->path != (char *) NULL)
930 (void) FormatLocaleFile(file,"\nPath: %s\n\n",locale_info[i]->path);
931 (void) FormatLocaleFile(file,"Tag/Message\n");
932 (void) FormatLocaleFile(file,
933 "-------------------------------------------------"
934 "------------------------------\n");
936 path=locale_info[i]->path;
937 (void) FormatLocaleFile(file,"%s\n",locale_info[i]->tag);
938 if (locale_info[i]->message != (char *) NULL)
939 (void) FormatLocaleFile(file," %s",locale_info[i]->message);
940 (void) FormatLocaleFile(file,"\n");
943 locale_info=(const LocaleInfo **)
944 RelinquishMagickMemory((void *) locale_info);
949 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
953 + L o a d L o c a l e L i s t %
957 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
959 % LoadLocaleList() loads the locale configuration file which provides a mapping
960 % between locale attributes and a locale name.
962 % The format of the LoadLocaleList method is:
964 % MagickBooleanType LoadLocaleList(const char *xml,const char *filename,
965 % const size_t depth,ExceptionInfo *exception)
967 % A description of each parameter follows:
969 % o xml: The locale list in XML format.
971 % o filename: The locale list filename.
973 % o depth: depth of <include /> statements.
975 % o exception: return any errors or warnings in this structure.
979 static void ChopLocaleComponents(char *path,const size_t components)
989 p=path+strlen(path)-1;
992 for (count=0; (count < (ssize_t) components) && (p > path); p--)
998 if (count < (ssize_t) components)
1002 static void *DestroyLocaleNode(void *locale_info)
1007 p=(LocaleInfo *) locale_info;
1008 if (p->path != (char *) NULL)
1009 p->path=DestroyString(p->path);
1010 if (p->tag != (char *) NULL)
1011 p->tag=DestroyString(p->tag);
1012 if (p->message != (char *) NULL)
1013 p->message=DestroyString(p->message);
1014 return(RelinquishMagickMemory(p));
1017 static void LocaleFatalErrorHandler(
1018 const ExceptionType magick_unused(severity),
1019 const char *reason,const char *description)
1021 if (reason == (char *) NULL)
1023 (void) FormatLocaleFile(stderr,"%s: %s",GetClientName(),reason);
1024 if (description != (char *) NULL)
1025 (void) FormatLocaleFile(stderr," (%s)",description);
1026 (void) FormatLocaleFile(stderr,".\n");
1027 (void) fflush(stderr);
1032 static MagickBooleanType LoadLocaleList(const char *xml,const char *filename,
1033 const char *locale,const size_t depth,ExceptionInfo *exception)
1036 keyword[MaxTextExtent],
1037 message[MaxTextExtent],
1057 Read the locale configure file.
1059 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1060 "Loading locale configure file \"%s\" ...",filename);
1061 if (xml == (const char *) NULL)
1062 return(MagickFalse);
1063 if (locale_list == (SplayTreeInfo *) NULL)
1065 locale_list=NewSplayTree(CompareSplayTreeString,(void *(*)(void *)) NULL,
1067 if (locale_list == (SplayTreeInfo *) NULL)
1068 return(MagickFalse);
1071 locale_info=(LocaleInfo *) NULL;
1075 fatal_handler=SetFatalErrorHandler(LocaleFatalErrorHandler);
1076 token=AcquireString(xml);
1077 for (q=(char *) xml; *q != '\0'; )
1082 GetMagickToken(q,&q,token);
1085 (void) CopyMagickString(keyword,token,MaxTextExtent);
1086 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
1091 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
1093 GetMagickToken(q,&q,token);
1094 while (isspace((int) ((unsigned char) *q)) != 0)
1099 if (LocaleNCompare(keyword,"<!--",4) == 0)
1104 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
1106 GetMagickToken(q,&q,token);
1107 while (isspace((int) ((unsigned char) *q)) != 0)
1112 if (LocaleCompare(keyword,"<include") == 0)
1117 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
1119 (void) CopyMagickString(keyword,token,MaxTextExtent);
1120 GetMagickToken(q,&q,token);
1123 GetMagickToken(q,&q,token);
1124 if (LocaleCompare(keyword,"locale") == 0)
1126 if (LocaleCompare(locale,token) != 0)
1130 if (LocaleCompare(keyword,"file") == 0)
1133 (void) ThrowMagickException(exception,GetMagickModule(),
1134 ConfigureError,"IncludeElementNestedTooDeeply","`%s'",token);
1138 path[MaxTextExtent],
1142 GetPathComponent(filename,HeadPath,path);
1144 (void) ConcatenateMagickString(path,DirectorySeparator,
1146 if (*token == *DirectorySeparator)
1147 (void) CopyMagickString(path,token,MaxTextExtent);
1149 (void) ConcatenateMagickString(path,token,MaxTextExtent);
1150 xml=FileToString(path,~0,exception);
1151 if (xml != (char *) NULL)
1153 status=LoadLocaleList(xml,path,locale,depth+1,exception);
1154 xml=(char *) RelinquishMagickMemory(xml);
1161 if (LocaleCompare(keyword,"<locale") == 0)
1166 while ((*token != '>') && (*q != '\0'))
1168 (void) CopyMagickString(keyword,token,MaxTextExtent);
1169 GetMagickToken(q,&q,token);
1172 GetMagickToken(q,&q,token);
1176 if (LocaleCompare(keyword,"</locale>") == 0)
1178 ChopLocaleComponents(tag,1);
1179 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1182 if (LocaleCompare(keyword,"<localemap>") == 0)
1184 if (LocaleCompare(keyword,"</localemap>") == 0)
1186 if (LocaleCompare(keyword,"<message") == 0)
1191 while ((*token != '>') && (*q != '\0'))
1193 (void) CopyMagickString(keyword,token,MaxTextExtent);
1194 GetMagickToken(q,&q,token);
1197 GetMagickToken(q,&q,token);
1198 if (LocaleCompare(keyword,"name") == 0)
1200 (void) ConcatenateMagickString(tag,token,MaxTextExtent);
1201 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1204 for (p=(char *) q; (*q != '<') && (*q != '\0'); q++) ;
1205 while (isspace((int) ((unsigned char) *p)) != 0)
1208 while ((isspace((int) ((unsigned char) *q)) != 0) && (q > p))
1210 (void) CopyMagickString(message,p,(size_t) (q-p+2));
1211 locale_info=(LocaleInfo *) AcquireMagickMemory(sizeof(*locale_info));
1212 if (locale_info == (LocaleInfo *) NULL)
1213 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1214 (void) ResetMagickMemory(locale_info,0,sizeof(*locale_info));
1215 locale_info->path=ConstantString(filename);
1216 locale_info->tag=ConstantString(tag);
1217 locale_info->message=ConstantString(message);
1218 locale_info->signature=MagickSignature;
1219 status=AddValueToSplayTree(locale_list,locale_info->tag,locale_info);
1220 if (status == MagickFalse)
1221 (void) ThrowMagickException(exception,GetMagickModule(),
1222 ResourceLimitError,"MemoryAllocationFailed","`%s'",
1224 (void) ConcatenateMagickString(tag,message,MaxTextExtent);
1225 (void) ConcatenateMagickString(tag,"\n",MaxTextExtent);
1229 if (LocaleCompare(keyword,"</message>") == 0)
1231 ChopLocaleComponents(tag,2);
1232 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1235 if (*keyword == '<')
1240 if (*(keyword+1) == '?')
1242 if (*(keyword+1) == '/')
1244 ChopLocaleComponents(tag,1);
1246 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1249 token[strlen(token)-1]='\0';
1250 (void) CopyMagickString(token,token+1,MaxTextExtent);
1251 (void) ConcatenateMagickString(tag,token,MaxTextExtent);
1252 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1255 GetMagickToken(q,(const char **) NULL,token);
1259 token=(char *) RelinquishMagickMemory(token);
1260 (void) SetFatalErrorHandler(fatal_handler);
1265 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1269 % L o a d L o c a l e L i s t s %
1273 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1275 % LoadLocaleList() loads one or more locale configuration file which
1276 % provides a mapping between locale attributes and a locale tag.
1278 % The format of the LoadLocaleLists method is:
1280 % MagickBooleanType LoadLocaleLists(const char *filename,
1281 % ExceptionInfo *exception)
1283 % A description of each parameter follows:
1285 % o filename: the font file tag.
1287 % o locale: the actual locale.
1289 % o exception: return any errors or warnings in this structure.
1292 static MagickBooleanType LoadLocaleLists(const char *filename,
1293 const char *locale,ExceptionInfo *exception)
1295 #if defined(MAGICKCORE_EMBEDDABLE_SUPPORT)
1296 return(LoadLocaleList(LocaleMap,"built-in",locale,0,exception));
1308 options=GetLocaleOptions(filename,exception);
1309 option=(const StringInfo *) GetNextValueInLinkedList(options);
1310 while (option != (const StringInfo *) NULL)
1312 status|=LoadLocaleList((const char *) GetStringInfoDatum(option),
1313 GetStringInfoPath(option),locale,0,exception);
1314 option=(const StringInfo *) GetNextValueInLinkedList(options);
1316 options=DestroyLocaleOptions(options);
1317 if ((locale_list == (SplayTreeInfo *) NULL) ||
1318 (GetNumberOfNodesInSplayTree(locale_list) == 0))
1320 options=GetLocaleOptions("english.xml",exception);
1321 option=(const StringInfo *) GetNextValueInLinkedList(options);
1322 while (option != (const StringInfo *) NULL)
1324 status|=LoadLocaleList((const char *) GetStringInfoDatum(option),
1325 GetStringInfoPath(option),locale,0,exception);
1326 option=(const StringInfo *) GetNextValueInLinkedList(options);
1328 options=DestroyLocaleOptions(options);
1330 if ((locale_list == (SplayTreeInfo *) NULL) ||
1331 (GetNumberOfNodesInSplayTree(locale_list) == 0))
1332 status|=LoadLocaleList(LocaleMap,"built-in",locale,0,exception);
1333 return(status != 0 ? MagickTrue : MagickFalse);
1338 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1342 + L o c a l e C o m p o n e n t G e n e s i s %
1346 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1348 % LocaleComponentGenesis() instantiates the locale component.
1350 % The format of the LocaleComponentGenesis method is:
1352 % MagickBooleanType LocaleComponentGenesis(void)
1355 MagickExport MagickBooleanType LocaleComponentGenesis(void)
1357 AcquireSemaphoreInfo(&locale_semaphore);
1362 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1366 + L o c a l e C o m p o n e n t T e r m i n u s %
1370 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1372 % LocaleComponentTerminus() destroys the locale component.
1374 % The format of the LocaleComponentTerminus method is:
1376 % LocaleComponentTerminus(void)
1379 MagickExport void LocaleComponentTerminus(void)
1381 if (locale_semaphore == (SemaphoreInfo *) NULL)
1382 AcquireSemaphoreInfo(&locale_semaphore);
1383 LockSemaphoreInfo(locale_semaphore);
1385 instantiate_locale=MagickFalse;
1386 UnlockSemaphoreInfo(locale_semaphore);
1387 DestroySemaphoreInfo(&locale_semaphore);