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 locale_t
87 c_locale = (locale_t) NULL;
89 static volatile MagickBooleanType
90 instantiate_locale = MagickFalse;
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,
225 const char *restrict format,va_list operands)
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 *restrict format,
274 va_start(operands,format);
275 n=(ssize_t) FormatLocaleFileList(file,format,operands);
281 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
285 + F o r m a t L o c a l e S t r i n g %
289 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
291 % FormatLocaleString() prints formatted output of a variable argument list to
292 % a string buffer in the "C" locale.
294 % The format of the FormatLocaleString method is:
296 % ssize_t FormatLocaleString(char *string,const size_t length,
297 % const char *format,...)
299 % A description of each parameter follows.
301 % o string: FormatLocaleString() returns the formatted string in this
304 % o length: the maximum length of the string.
306 % o format: A string describing the format to use to write the remaining
311 MagickExport ssize_t FormatLocaleStringList(char *restrict string,
312 const size_t length,const char *restrict format,va_list operands)
317 #if defined(MAGICKCORE_HAVE_VSNPRINTF_L)
318 n=vsnprintf_l(string,length,format,(locale_t) NULL,operands);
319 #elif defined(MAGICKCORE_HAVE_VSNPRINTF)
320 #if defined(MAGICKCORE_HAVE_USELOCALE)
326 locale=AcquireCLocale();
327 if (locale == (locale_t) NULL)
328 n=vsnprintf(string,length,format,operands);
331 previous_locale=uselocale(locale);
332 n=vsnprintf(string,length,format,operands);
333 uselocale(previous_locale);
337 n=vsnprintf(string,length,format,operands);
340 n=vsprintf(string,format,operands);
343 string[length-1]='\0';
347 MagickExport ssize_t FormatLocaleString(char *restrict string,
348 const size_t length,const char *restrict format,...)
356 va_start(operands,format);
357 n=(ssize_t) FormatLocaleStringList(string,length,format,operands);
363 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
367 + G e t L o c a l e I n f o _ %
371 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
373 % GetLocaleInfo_() searches the locale list for the specified tag and if
374 % found returns attributes for that element.
376 % The format of the GetLocaleInfo method is:
378 % const LocaleInfo *GetLocaleInfo_(const char *tag,
379 % ExceptionInfo *exception)
381 % A description of each parameter follows:
383 % o tag: the locale tag.
385 % o exception: return any errors or warnings in this structure.
388 MagickExport const LocaleInfo *GetLocaleInfo_(const char *tag,
389 ExceptionInfo *exception)
391 assert(exception != (ExceptionInfo *) NULL);
392 if ((locale_list == (SplayTreeInfo *) NULL) ||
393 (instantiate_locale == MagickFalse))
394 if (InitializeLocaleList(exception) == MagickFalse)
395 return((const LocaleInfo *) NULL);
396 if ((locale_list == (SplayTreeInfo *) NULL) ||
397 (GetNumberOfNodesInSplayTree(locale_list) == 0))
398 return((const LocaleInfo *) NULL);
399 if ((tag == (const char *) NULL) || (LocaleCompare(tag,"*") == 0))
401 ResetSplayTreeIterator(locale_list);
402 return((const LocaleInfo *) GetNextValueInSplayTree(locale_list));
404 return((const LocaleInfo *) GetValueFromSplayTree(locale_list,tag));
408 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
412 % G e t L o c a l e I n f o L i s t %
416 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
418 % GetLocaleInfoList() returns any locale messages that match the
421 % The format of the GetLocaleInfoList function is:
423 % const LocaleInfo **GetLocaleInfoList(const char *pattern,
424 % size_t *number_messages,ExceptionInfo *exception)
426 % A description of each parameter follows:
428 % o pattern: Specifies a pointer to a text string containing a pattern.
430 % o number_messages: This integer returns the number of locale messages in
433 % o exception: return any errors or warnings in this structure.
437 #if defined(__cplusplus) || defined(c_plusplus)
441 static int LocaleInfoCompare(const void *x,const void *y)
447 p=(const LocaleInfo **) x,
448 q=(const LocaleInfo **) y;
449 if (LocaleCompare((*p)->path,(*q)->path) == 0)
450 return(LocaleCompare((*p)->tag,(*q)->tag));
451 return(LocaleCompare((*p)->path,(*q)->path));
454 #if defined(__cplusplus) || defined(c_plusplus)
458 MagickExport const LocaleInfo **GetLocaleInfoList(const char *pattern,
459 size_t *number_messages,ExceptionInfo *exception)
464 register const LocaleInfo
471 Allocate locale list.
473 assert(pattern != (char *) NULL);
474 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
475 assert(number_messages != (size_t *) NULL);
477 p=GetLocaleInfo_("*",exception);
478 if (p == (const LocaleInfo *) NULL)
479 return((const LocaleInfo **) NULL);
480 messages=(const LocaleInfo **) AcquireQuantumMemory((size_t)
481 GetNumberOfNodesInSplayTree(locale_list)+1UL,sizeof(*messages));
482 if (messages == (const LocaleInfo **) NULL)
483 return((const LocaleInfo **) NULL);
485 Generate locale list.
487 LockSemaphoreInfo(locale_semaphore);
488 ResetSplayTreeIterator(locale_list);
489 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_list);
490 for (i=0; p != (const LocaleInfo *) NULL; )
492 if ((p->stealth == MagickFalse) &&
493 (GlobExpression(p->tag,pattern,MagickTrue) != MagickFalse))
495 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_list);
497 UnlockSemaphoreInfo(locale_semaphore);
498 qsort((void *) messages,(size_t) i,sizeof(*messages),LocaleInfoCompare);
499 messages[i]=(LocaleInfo *) NULL;
500 *number_messages=(size_t) i;
505 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
509 % G e t L o c a l e L i s t %
513 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
515 % GetLocaleList() returns any locale messages that match the specified
518 % The format of the GetLocaleList function is:
520 % char **GetLocaleList(const char *pattern,size_t *number_messages,
521 % Exceptioninfo *exception)
523 % A description of each parameter follows:
525 % o pattern: Specifies a pointer to a text string containing a pattern.
527 % o number_messages: This integer returns the number of messages in the
530 % o exception: return any errors or warnings in this structure.
534 #if defined(__cplusplus) || defined(c_plusplus)
538 static int LocaleTagCompare(const void *x,const void *y)
546 return(LocaleCompare(*p,*q));
549 #if defined(__cplusplus) || defined(c_plusplus)
553 MagickExport char **GetLocaleList(const char *pattern,
554 size_t *number_messages,ExceptionInfo *exception)
559 register const LocaleInfo
566 Allocate locale list.
568 assert(pattern != (char *) NULL);
569 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
570 assert(number_messages != (size_t *) NULL);
572 p=GetLocaleInfo_("*",exception);
573 if (p == (const LocaleInfo *) NULL)
574 return((char **) NULL);
575 messages=(char **) AcquireQuantumMemory((size_t)
576 GetNumberOfNodesInSplayTree(locale_list)+1UL,sizeof(*messages));
577 if (messages == (char **) NULL)
578 return((char **) NULL);
579 LockSemaphoreInfo(locale_semaphore);
580 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_list);
581 for (i=0; p != (const LocaleInfo *) NULL; )
583 if ((p->stealth == MagickFalse) &&
584 (GlobExpression(p->tag,pattern,MagickTrue) != MagickFalse))
585 messages[i++]=ConstantString(p->tag);
586 p=(const LocaleInfo *) GetNextValueInSplayTree(locale_list);
588 UnlockSemaphoreInfo(locale_semaphore);
589 qsort((void *) messages,(size_t) i,sizeof(*messages),LocaleTagCompare);
590 messages[i]=(char *) NULL;
591 *number_messages=(size_t) i;
596 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
600 % G e t L o c a l e M e s s a g e %
604 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
606 % GetLocaleMessage() returns a message in the current locale that matches the
609 % The format of the GetLocaleMessage method is:
611 % const char *GetLocaleMessage(const char *tag)
613 % A description of each parameter follows:
615 % o tag: Return a message that matches this tag in the current locale.
618 MagickExport const char *GetLocaleMessage(const char *tag)
629 if ((tag == (const char *) NULL) || (*tag == '\0'))
631 exception=AcquireExceptionInfo();
632 (void) FormatLocaleString(name,MaxTextExtent,"%s/",tag);
633 locale_info=GetLocaleInfo_(name,exception);
634 exception=DestroyExceptionInfo(exception);
635 if (locale_info != (const LocaleInfo *) NULL)
636 return(locale_info->message);
641 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
645 % G e t L o c a l e O p t i o n s %
649 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
651 % GetLocaleOptions() returns any Magick configuration messages associated
652 % with the specified filename.
654 % The format of the GetLocaleOptions method is:
656 % LinkedListInfo *GetLocaleOptions(const char *filename,
657 % ExceptionInfo *exception)
659 % A description of each parameter follows:
661 % o filename: the locale file tag.
663 % o exception: return any errors or warnings in this structure.
666 MagickExport LinkedListInfo *GetLocaleOptions(const char *filename,
667 ExceptionInfo *exception)
682 assert(filename != (const char *) NULL);
683 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
684 assert(exception != (ExceptionInfo *) NULL);
685 (void) CopyMagickString(path,filename,MaxTextExtent);
687 Load XML from configuration files to linked-list.
689 messages=NewLinkedList(0);
690 paths=GetConfigurePaths(filename,exception);
691 if (paths != (LinkedListInfo *) NULL)
693 ResetLinkedListIterator(paths);
694 element=(const char *) GetNextValueInLinkedList(paths);
695 while (element != (const char *) NULL)
697 (void) FormatLocaleString(path,MaxTextExtent,"%s%s",element,filename);
698 (void) LogMagickEvent(LocaleEvent,GetMagickModule(),
699 "Searching for locale file: \"%s\"",path);
700 xml=ConfigureFileToStringInfo(path);
701 if (xml != (StringInfo *) NULL)
702 (void) AppendValueToLinkedList(messages,xml);
703 element=(const char *) GetNextValueInLinkedList(paths);
705 paths=DestroyLinkedList(paths,RelinquishMagickMemory);
707 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
712 blob=(char *) NTResourceToBlob(filename);
713 if (blob != (char *) NULL)
715 xml=StringToStringInfo(blob);
716 (void) AppendValueToLinkedList(messages,xml);
717 blob=DestroyString(blob);
721 ResetLinkedListIterator(messages);
726 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
730 % G e t L o c a l e V a l u e %
734 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
736 % GetLocaleValue() returns the message associated with the locale info.
738 % The format of the GetLocaleValue method is:
740 % const char *GetLocaleValue(const LocaleInfo *locale_info)
742 % A description of each parameter follows:
744 % o locale_info: The locale info.
747 MagickExport const char *GetLocaleValue(const LocaleInfo *locale_info)
749 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
750 assert(locale_info != (LocaleInfo *) NULL);
751 assert(locale_info->signature == MagickSignature);
752 return(locale_info->message);
756 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
760 + I n i t i a l i z e L o c a l e L i s t %
764 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
766 % InitializeLocaleList() initializes the locale list.
768 % The format of the InitializeLocaleList method is:
770 % MagickBooleanType InitializeLocaleList(ExceptionInfo *exception)
772 % A description of each parameter follows.
774 % o exception: return any errors or warnings in this structure.
777 static MagickBooleanType InitializeLocaleList(ExceptionInfo *exception)
779 if ((locale_list == (SplayTreeInfo *) NULL) &&
780 (instantiate_locale == MagickFalse))
782 if (locale_semaphore == (SemaphoreInfo *) NULL)
783 AcquireSemaphoreInfo(&locale_semaphore);
784 LockSemaphoreInfo(locale_semaphore);
785 if ((locale_list == (SplayTreeInfo *) NULL) &&
786 (instantiate_locale == MagickFalse))
794 locale=(char *) NULL;
795 p=setlocale(LC_CTYPE,(const char *) NULL);
796 if (p != (const char *) NULL)
797 locale=ConstantString(p);
798 if (locale == (char *) NULL)
799 locale=GetEnvironmentValue("LC_ALL");
800 if (locale == (char *) NULL)
801 locale=GetEnvironmentValue("LC_MESSAGES");
802 if (locale == (char *) NULL)
803 locale=GetEnvironmentValue("LC_CTYPE");
804 if (locale == (char *) NULL)
805 locale=GetEnvironmentValue("LANG");
806 if (locale == (char *) NULL)
807 locale=ConstantString("C");
808 (void) LoadLocaleLists(LocaleFilename,locale,exception);
809 locale=DestroyString(locale);
810 instantiate_locale=MagickTrue;
812 UnlockSemaphoreInfo(locale_semaphore);
814 return(locale_list != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
818 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
822 + I n t e r p r e t L o c a l e V a l u e %
826 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
828 % InterpretLocaleValue() interprets the string as a floating point number in
829 % the "C" locale and returns its value as a double. If sentinal is not a null
830 % pointer, the method also sets the value pointed by sentinal to point to the
831 % first character after the number.
833 % The format of the InterpretLocaleValue method is:
835 % double InterpretLocaleValue(const char *value,char **sentinal)
837 % A description of each parameter follows:
839 % o value: the string value.
841 % o sentinal: if sentinal is not NULL, a pointer to the character after the
842 % last character used in the conversion is stored in the location
843 % referenced by sentinal.
846 MagickExport double InterpretLocaleValue(const char *restrict string,
847 char **restrict sentinal)
852 #if defined(MAGICKCORE_HAVE_STRTOD_L)
857 locale=AcquireCLocale();
858 if (locale == (locale_t) NULL)
859 value=strtod(string,sentinal);
861 value=strtod_l(string,sentinal,locale);
864 value=strtod(string,sentinal);
870 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
874 % L i s t L o c a l e I n f o %
878 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
880 % ListLocaleInfo() lists the locale info to a file.
882 % The format of the ListLocaleInfo method is:
884 % MagickBooleanType ListLocaleInfo(FILE *file,ExceptionInfo *exception)
886 % A description of each parameter follows.
888 % o file: An pointer to a FILE.
890 % o exception: return any errors or warnings in this structure.
893 MagickExport MagickBooleanType ListLocaleInfo(FILE *file,
894 ExceptionInfo *exception)
908 if (file == (const FILE *) NULL)
911 locale_info=GetLocaleInfoList("*",&number_messages,exception);
912 if (locale_info == (const LocaleInfo **) NULL)
914 path=(const char *) NULL;
915 for (i=0; i < (ssize_t) number_messages; i++)
917 if (locale_info[i]->stealth != MagickFalse)
919 if ((path == (const char *) NULL) ||
920 (LocaleCompare(path,locale_info[i]->path) != 0))
922 if (locale_info[i]->path != (char *) NULL)
923 (void) FormatLocaleFile(file,"\nPath: %s\n\n",locale_info[i]->path);
924 (void) FormatLocaleFile(file,"Tag/Message\n");
925 (void) FormatLocaleFile(file,
926 "-------------------------------------------------"
927 "------------------------------\n");
929 path=locale_info[i]->path;
930 (void) FormatLocaleFile(file,"%s\n",locale_info[i]->tag);
931 if (locale_info[i]->message != (char *) NULL)
932 (void) FormatLocaleFile(file," %s",locale_info[i]->message);
933 (void) FormatLocaleFile(file,"\n");
936 locale_info=(const LocaleInfo **)
937 RelinquishMagickMemory((void *) locale_info);
942 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
946 + L o a d L o c a l e L i s t %
950 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
952 % LoadLocaleList() loads the locale configuration file which provides a mapping
953 % between locale attributes and a locale name.
955 % The format of the LoadLocaleList method is:
957 % MagickBooleanType LoadLocaleList(const char *xml,const char *filename,
958 % const size_t depth,ExceptionInfo *exception)
960 % A description of each parameter follows:
962 % o xml: The locale list in XML format.
964 % o filename: The locale list filename.
966 % o depth: depth of <include /> statements.
968 % o exception: return any errors or warnings in this structure.
972 static void ChopLocaleComponents(char *path,const size_t components)
982 p=path+strlen(path)-1;
985 for (count=0; (count < (ssize_t) components) && (p > path); p--)
991 if (count < (ssize_t) components)
995 static void *DestroyLocaleNode(void *locale_info)
1000 p=(LocaleInfo *) locale_info;
1001 if (p->path != (char *) NULL)
1002 p->path=DestroyString(p->path);
1003 if (p->tag != (char *) NULL)
1004 p->tag=DestroyString(p->tag);
1005 if (p->message != (char *) NULL)
1006 p->message=DestroyString(p->message);
1007 return(RelinquishMagickMemory(p));
1010 static void LocaleFatalErrorHandler(
1011 const ExceptionType magick_unused(severity),
1012 const char *reason,const char *description)
1014 if (reason == (char *) NULL)
1016 (void) FormatLocaleFile(stderr,"%s: %s",GetClientName(),reason);
1017 if (description != (char *) NULL)
1018 (void) FormatLocaleFile(stderr," (%s)",description);
1019 (void) FormatLocaleFile(stderr,".\n");
1020 (void) fflush(stderr);
1025 static MagickBooleanType LoadLocaleList(const char *xml,const char *filename,
1026 const char *locale,const size_t depth,ExceptionInfo *exception)
1029 keyword[MaxTextExtent],
1030 message[MaxTextExtent],
1050 Read the locale configure file.
1052 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1053 "Loading locale configure file \"%s\" ...",filename);
1054 if (xml == (const char *) NULL)
1055 return(MagickFalse);
1056 if (locale_list == (SplayTreeInfo *) NULL)
1058 locale_list=NewSplayTree(CompareSplayTreeString,(void *(*)(void *)) NULL,
1060 if (locale_list == (SplayTreeInfo *) NULL)
1061 return(MagickFalse);
1064 locale_info=(LocaleInfo *) NULL;
1068 fatal_handler=SetFatalErrorHandler(LocaleFatalErrorHandler);
1069 token=AcquireString(xml);
1070 for (q=(char *) xml; *q != '\0'; )
1075 GetMagickToken(q,&q,token);
1078 (void) CopyMagickString(keyword,token,MaxTextExtent);
1079 if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
1084 while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
1086 GetMagickToken(q,&q,token);
1087 while (isspace((int) ((unsigned char) *q)) != 0)
1092 if (LocaleNCompare(keyword,"<!--",4) == 0)
1097 while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
1099 GetMagickToken(q,&q,token);
1100 while (isspace((int) ((unsigned char) *q)) != 0)
1105 if (LocaleCompare(keyword,"<include") == 0)
1110 while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
1112 (void) CopyMagickString(keyword,token,MaxTextExtent);
1113 GetMagickToken(q,&q,token);
1116 GetMagickToken(q,&q,token);
1117 if (LocaleCompare(keyword,"locale") == 0)
1119 if (LocaleCompare(locale,token) != 0)
1123 if (LocaleCompare(keyword,"file") == 0)
1126 (void) ThrowMagickException(exception,GetMagickModule(),
1127 ConfigureError,"IncludeElementNestedTooDeeply","`%s'",token);
1131 path[MaxTextExtent],
1135 GetPathComponent(filename,HeadPath,path);
1137 (void) ConcatenateMagickString(path,DirectorySeparator,
1139 if (*token == *DirectorySeparator)
1140 (void) CopyMagickString(path,token,MaxTextExtent);
1142 (void) ConcatenateMagickString(path,token,MaxTextExtent);
1143 xml=FileToString(path,~0,exception);
1144 if (xml != (char *) NULL)
1146 status=LoadLocaleList(xml,path,locale,depth+1,exception);
1147 xml=(char *) RelinquishMagickMemory(xml);
1154 if (LocaleCompare(keyword,"<locale") == 0)
1159 while ((*token != '>') && (*q != '\0'))
1161 (void) CopyMagickString(keyword,token,MaxTextExtent);
1162 GetMagickToken(q,&q,token);
1165 GetMagickToken(q,&q,token);
1169 if (LocaleCompare(keyword,"</locale>") == 0)
1171 ChopLocaleComponents(tag,1);
1172 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1175 if (LocaleCompare(keyword,"<localemap>") == 0)
1177 if (LocaleCompare(keyword,"</localemap>") == 0)
1179 if (LocaleCompare(keyword,"<message") == 0)
1184 while ((*token != '>') && (*q != '\0'))
1186 (void) CopyMagickString(keyword,token,MaxTextExtent);
1187 GetMagickToken(q,&q,token);
1190 GetMagickToken(q,&q,token);
1191 if (LocaleCompare(keyword,"name") == 0)
1193 (void) ConcatenateMagickString(tag,token,MaxTextExtent);
1194 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1197 for (p=(char *) q; (*q != '<') && (*q != '\0'); q++) ;
1198 while (isspace((int) ((unsigned char) *p)) != 0)
1201 while ((isspace((int) ((unsigned char) *q)) != 0) && (q > p))
1203 (void) CopyMagickString(message,p,(size_t) (q-p+2));
1204 locale_info=(LocaleInfo *) AcquireMagickMemory(sizeof(*locale_info));
1205 if (locale_info == (LocaleInfo *) NULL)
1206 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1207 (void) ResetMagickMemory(locale_info,0,sizeof(*locale_info));
1208 locale_info->path=ConstantString(filename);
1209 locale_info->tag=ConstantString(tag);
1210 locale_info->message=ConstantString(message);
1211 locale_info->signature=MagickSignature;
1212 status=AddValueToSplayTree(locale_list,locale_info->tag,locale_info);
1213 if (status == MagickFalse)
1214 (void) ThrowMagickException(exception,GetMagickModule(),
1215 ResourceLimitError,"MemoryAllocationFailed","`%s'",
1217 (void) ConcatenateMagickString(tag,message,MaxTextExtent);
1218 (void) ConcatenateMagickString(tag,"\n",MaxTextExtent);
1222 if (LocaleCompare(keyword,"</message>") == 0)
1224 ChopLocaleComponents(tag,2);
1225 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1228 if (*keyword == '<')
1233 if (*(keyword+1) == '?')
1235 if (*(keyword+1) == '/')
1237 ChopLocaleComponents(tag,1);
1239 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1242 token[strlen(token)-1]='\0';
1243 (void) CopyMagickString(token,token+1,MaxTextExtent);
1244 (void) ConcatenateMagickString(tag,token,MaxTextExtent);
1245 (void) ConcatenateMagickString(tag,"/",MaxTextExtent);
1248 GetMagickToken(q,(const char **) NULL,token);
1252 token=(char *) RelinquishMagickMemory(token);
1253 (void) SetFatalErrorHandler(fatal_handler);
1258 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1262 % L o a d L o c a l e L i s t s %
1266 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1268 % LoadLocaleList() loads one or more locale configuration file which
1269 % provides a mapping between locale attributes and a locale tag.
1271 % The format of the LoadLocaleLists method is:
1273 % MagickBooleanType LoadLocaleLists(const char *filename,
1274 % ExceptionInfo *exception)
1276 % A description of each parameter follows:
1278 % o filename: the font file tag.
1280 % o locale: the actual locale.
1282 % o exception: return any errors or warnings in this structure.
1285 static MagickBooleanType LoadLocaleLists(const char *filename,
1286 const char *locale,ExceptionInfo *exception)
1288 #if defined(MAGICKCORE_EMBEDDABLE_SUPPORT)
1289 return(LoadLocaleList(LocaleMap,"built-in",locale,0,exception));
1301 options=GetLocaleOptions(filename,exception);
1302 option=(const StringInfo *) GetNextValueInLinkedList(options);
1303 while (option != (const StringInfo *) NULL)
1305 status|=LoadLocaleList((const char *) GetStringInfoDatum(option),
1306 GetStringInfoPath(option),locale,0,exception);
1307 option=(const StringInfo *) GetNextValueInLinkedList(options);
1309 options=DestroyLocaleOptions(options);
1310 if ((locale_list == (SplayTreeInfo *) NULL) ||
1311 (GetNumberOfNodesInSplayTree(locale_list) == 0))
1313 options=GetLocaleOptions("english.xml",exception);
1314 option=(const StringInfo *) GetNextValueInLinkedList(options);
1315 while (option != (const StringInfo *) NULL)
1317 status|=LoadLocaleList((const char *) GetStringInfoDatum(option),
1318 GetStringInfoPath(option),locale,0,exception);
1319 option=(const StringInfo *) GetNextValueInLinkedList(options);
1321 options=DestroyLocaleOptions(options);
1323 if ((locale_list == (SplayTreeInfo *) NULL) ||
1324 (GetNumberOfNodesInSplayTree(locale_list) == 0))
1325 status|=LoadLocaleList(LocaleMap,"built-in",locale,0,exception);
1326 return(status != 0 ? MagickTrue : MagickFalse);
1331 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1335 + L o c a l e C o m p o n e n t G e n e s i s %
1339 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1341 % LocaleComponentGenesis() instantiates the locale component.
1343 % The format of the LocaleComponentGenesis method is:
1345 % MagickBooleanType LocaleComponentGenesis(void)
1348 MagickExport MagickBooleanType LocaleComponentGenesis(void)
1350 AcquireSemaphoreInfo(&locale_semaphore);
1355 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1359 + L o c a l e C o m p o n e n t T e r m i n u s %
1363 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1365 % LocaleComponentTerminus() destroys the locale component.
1367 % The format of the LocaleComponentTerminus method is:
1369 % LocaleComponentTerminus(void)
1372 MagickExport void LocaleComponentTerminus(void)
1374 if (locale_semaphore == (SemaphoreInfo *) NULL)
1375 AcquireSemaphoreInfo(&locale_semaphore);
1376 LockSemaphoreInfo(locale_semaphore);
1378 instantiate_locale=MagickFalse;
1379 UnlockSemaphoreInfo(locale_semaphore);
1380 DestroySemaphoreInfo(&locale_semaphore);