MagickCore  7.0.10
locale.c
Go to the documentation of this file.
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 % %
4 % %
5 % %
6 % L OOO CCCC AAA L EEEEE %
7 % L O O C A A L E %
8 % L O O C AAAAA L EEE %
9 % L O O C A A L E %
10 % LLLLL OOO CCCC A A LLLLL EEEEE %
11 % %
12 % %
13 % MagickCore Image Locale Methods %
14 % %
15 % Software Design %
16 % Cristy %
17 % July 2003 %
18 % %
19 % %
20 % Copyright 1999-2021 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
22 % %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
25 % %
26 % https://imagemagick.org/script/license.php %
27 % %
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. %
33 % %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35 %
36 %
37 */
38 
39 /*
40  Include declarations.
41 */
42 #include "MagickCore/studio.h"
43 #include "MagickCore/blob.h"
44 #include "MagickCore/client.h"
45 #include "MagickCore/configure.h"
46 #include "MagickCore/exception.h"
49 #include "MagickCore/linked-list.h"
50 #include "MagickCore/locale_.h"
52 #include "MagickCore/log.h"
53 #include "MagickCore/memory_.h"
56 #include "MagickCore/semaphore.h"
57 #include "MagickCore/splay-tree.h"
58 #include "MagickCore/string_.h"
60 #include "MagickCore/token.h"
61 #include "MagickCore/utility.h"
63 #include "MagickCore/xml-tree.h"
65 
66 /*
67  Define declarations.
68 */
69 #if (defined(MAGICKCORE_HAVE_NEWLOCALE) || defined(MAGICKCORE_WINDOWS_SUPPORT)) && !defined(__MINGW32__)
70 # define MAGICKCORE_LOCALE_SUPPORT
71 #endif
72 #define LocaleFilename "locale.xml"
73 
74 /*
75  Static declarations.
76 */
77 static const char
79  "<?xml version=\"1.0\"?>"
80  "<localemap>"
81  " <locale name=\"C\">"
82  " <Exception>"
83  " <Message name=\"\">"
84  " </Message>"
85  " </Exception>"
86  " </locale>"
87  "</localemap>";
88 
89 static SemaphoreInfo
91 
92 static SplayTreeInfo
94 
95 #if defined(MAGICKCORE_LOCALE_SUPPORT)
96 static volatile locale_t
97  c_locale = (locale_t) NULL;
98 #endif
99 
100 /*
101  Forward declarations.
102 */
103 static MagickBooleanType
105  LoadLocaleCache(SplayTreeInfo *,const char *,const char *,const char *,
106  const size_t,ExceptionInfo *);
107 
108 #if defined(MAGICKCORE_LOCALE_SUPPORT)
109 /*
110 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
111 % %
112 % %
113 % %
114 + A c q u i r e C L o c a l e %
115 % %
116 % %
117 % %
118 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
119 %
120 % AcquireCLocale() allocates the C locale object, or (locale_t) 0 with
121 % errno set if it cannot be acquired.
122 %
123 % The format of the AcquireCLocale method is:
124 %
125 % locale_t AcquireCLocale(void)
126 %
127 */
128 static locale_t AcquireCLocale(void)
129 {
130 #if defined(MAGICKCORE_HAVE_NEWLOCALE)
131  if (c_locale == (locale_t) NULL)
132  c_locale=newlocale(LC_ALL_MASK,"C",(locale_t) 0);
133 #elif defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__MINGW32__)
134  if (c_locale == (locale_t) NULL)
135  c_locale=_create_locale(LC_ALL,"C");
136 #endif
137  return(c_locale);
138 }
139 #endif
140 
141 /*
142 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
143 % %
144 % %
145 % %
146 % A c q u i r e L o c a l e S p l a y T r e e %
147 % %
148 % %
149 % %
150 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
151 %
152 % AcquireLocaleSplayTree() caches one or more locale configurations which
153 % provides a mapping between locale attributes and a locale tag.
154 %
155 % The format of the AcquireLocaleSplayTree method is:
156 %
157 % SplayTreeInfo *AcquireLocaleSplayTree(const char *filename,
158 % ExceptionInfo *exception)
159 %
160 % A description of each parameter follows:
161 %
162 % o filename: the font file tag.
163 %
164 % o locale: the actual locale.
165 %
166 % o exception: return any errors or warnings in this structure.
167 %
168 */
169 
170 static void *DestroyLocaleNode(void *locale_info)
171 {
172  LocaleInfo
173  *p;
174 
175  p=(LocaleInfo *) locale_info;
176  if (p->path != (char *) NULL)
177  p->path=DestroyString(p->path);
178  if (p->tag != (char *) NULL)
179  p->tag=DestroyString(p->tag);
180  if (p->message != (char *) NULL)
182  return(RelinquishMagickMemory(p));
183 }
184 
185 static SplayTreeInfo *AcquireLocaleSplayTree(const char *filename,
186  const char *locale,ExceptionInfo *exception)
187 {
189  status;
190 
192  *cache;
193 
194  cache=NewSplayTree(CompareSplayTreeString,(void *(*)(void *)) NULL,
196  status=MagickTrue;
197 #if !MAGICKCORE_ZERO_CONFIGURATION_SUPPORT
198  {
199  const StringInfo
200  *option;
201 
203  *options;
204 
205  options=GetLocaleOptions(filename,exception);
206  option=(const StringInfo *) GetNextValueInLinkedList(options);
207  while (option != (const StringInfo *) NULL)
208  {
209  status&=LoadLocaleCache(cache,(const char *)
210  GetStringInfoDatum(option),GetStringInfoPath(option),locale,0,
211  exception);
212  option=(const StringInfo *) GetNextValueInLinkedList(options);
213  }
214  options=DestroyLocaleOptions(options);
215  if (GetNumberOfNodesInSplayTree(cache) == 0)
216  {
217  options=GetLocaleOptions("english.xml",exception);
218  option=(const StringInfo *) GetNextValueInLinkedList(options);
219  while (option != (const StringInfo *) NULL)
220  {
221  status&=LoadLocaleCache(cache,(const char *)
222  GetStringInfoDatum(option),GetStringInfoPath(option),locale,0,
223  exception);
224  option=(const StringInfo *) GetNextValueInLinkedList(options);
225  }
226  options=DestroyLocaleOptions(options);
227  }
228  }
229 #endif
230  if (GetNumberOfNodesInSplayTree(cache) == 0)
231  status&=LoadLocaleCache(cache,LocaleMap,"built-in",locale,0,
232  exception);
233  return(cache);
234 }
235 
236 #if defined(MAGICKCORE_LOCALE_SUPPORT)
237 /*
238 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
239 % %
240 % %
241 % %
242 + D e s t r o y C L o c a l e %
243 % %
244 % %
245 % %
246 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
247 %
248 % DestroyCLocale() releases the resources allocated for a locale object
249 % returned by a call to the AcquireCLocale() method.
250 %
251 % The format of the DestroyCLocale method is:
252 %
253 % void DestroyCLocale(void)
254 %
255 */
256 static void DestroyCLocale(void)
257 {
258  if (c_locale != (locale_t) NULL)
259  freelocale(c_locale);
260  c_locale=(locale_t) NULL;
261 }
262 #endif
263 
264 /*
265 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
266 % %
267 % %
268 % %
269 % D e s t r o y L o c a l e O p t i o n s %
270 % %
271 % %
272 % %
273 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
274 %
275 % DestroyLocaleOptions() releases memory associated with an locale
276 % messages.
277 %
278 % The format of the DestroyProfiles method is:
279 %
280 % LinkedListInfo *DestroyLocaleOptions(Image *image)
281 %
282 % A description of each parameter follows:
283 %
284 % o image: the image.
285 %
286 */
287 
288 static void *DestroyOptions(void *message)
289 {
290  return(DestroyStringInfo((StringInfo *) message));
291 }
292 
294 {
295  assert(messages != (LinkedListInfo *) NULL);
296  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
297  return(DestroyLinkedList(messages,DestroyOptions));
298 }
299 
300 /*
301 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
302 % %
303 % %
304 % %
305 + F o r m a t L o c a l e F i l e %
306 % %
307 % %
308 % %
309 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
310 %
311 % FormatLocaleFile() prints formatted output of a variable argument list to a
312 % file in the "C" locale.
313 %
314 % The format of the FormatLocaleFile method is:
315 %
316 % ssize_t FormatLocaleFile(FILE *file,const char *format,...)
317 %
318 % A description of each parameter follows.
319 %
320 % o file: the file.
321 %
322 % o format: A file describing the format to use to write the remaining
323 % arguments.
324 %
325 */
326 
328  const char *magick_restrict format,va_list operands)
329 {
330  ssize_t
331  n;
332 
333 #if defined(MAGICKCORE_LOCALE_SUPPORT) && defined(MAGICKCORE_HAVE_VFPRINTF_L)
334  {
335  locale_t
336  locale;
337 
338  locale=AcquireCLocale();
339  if (locale == (locale_t) NULL)
340  n=(ssize_t) vfprintf(file,format,operands);
341  else
342 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
343  n=(ssize_t) vfprintf_l(file,format,locale,operands);
344 #else
345  n=(ssize_t) vfprintf_l(file,locale,format,operands);
346 #endif
347  }
348 #else
349 #if defined(MAGICKCORE_LOCALE_SUPPORT) && defined(MAGICKCORE_HAVE_USELOCALE)
350  {
351  locale_t
352  locale,
353  previous_locale;
354 
355  locale=AcquireCLocale();
356  if (locale == (locale_t) NULL)
357  n=(ssize_t) vfprintf(file,format,operands);
358  else
359  {
360  previous_locale=uselocale(locale);
361  n=(ssize_t) vfprintf(file,format,operands);
362  uselocale(previous_locale);
363  }
364  }
365 #else
366  n=(ssize_t) vfprintf(file,format,operands);
367 #endif
368 #endif
369  return(n);
370 }
371 
372 MagickExport ssize_t FormatLocaleFile(FILE *file,
373  const char *magick_restrict format,...)
374 {
375  ssize_t
376  n;
377 
378  va_list
379  operands;
380 
381  va_start(operands,format);
382  n=FormatLocaleFileList(file,format,operands);
383  va_end(operands);
384  return(n);
385 }
386 
387 /*
388 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
389 % %
390 % %
391 % %
392 + F o r m a t L o c a l e S t r i n g %
393 % %
394 % %
395 % %
396 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
397 %
398 % FormatLocaleString() prints formatted output of a variable argument list to
399 % a string buffer in the "C" locale.
400 %
401 % The format of the FormatLocaleString method is:
402 %
403 % ssize_t FormatLocaleString(char *string,const size_t length,
404 % const char *format,...)
405 %
406 % A description of each parameter follows.
407 %
408 % o string: FormatLocaleString() returns the formatted string in this
409 % character buffer.
410 %
411 % o length: the maximum length of the string.
412 %
413 % o format: A string describing the format to use to write the remaining
414 % arguments.
415 %
416 */
417 
419  const size_t length,const char *magick_restrict format,va_list operands)
420 {
421  ssize_t
422  n;
423 
424 #if defined(MAGICKCORE_LOCALE_SUPPORT) && defined(MAGICKCORE_HAVE_VSNPRINTF_L)
425  {
426  locale_t
427  locale;
428 
429  locale=AcquireCLocale();
430  if (locale == (locale_t) NULL)
431  n=(ssize_t) vsnprintf(string,length,format,operands);
432  else
433 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
434  n=(ssize_t) vsnprintf_l(string,length,format,locale,operands);
435 #else
436  n=(ssize_t) vsnprintf_l(string,length,locale,format,operands);
437 #endif
438  }
439 #elif defined(MAGICKCORE_HAVE_VSNPRINTF)
440 #if defined(MAGICKCORE_LOCALE_SUPPORT) && defined(MAGICKCORE_HAVE_USELOCALE)
441  {
442  locale_t
443  locale,
444  previous_locale;
445 
446  locale=AcquireCLocale();
447  if (locale == (locale_t) NULL)
448  n=(ssize_t) vsnprintf(string,length,format,operands);
449  else
450  {
451  previous_locale=uselocale(locale);
452  n=(ssize_t) vsnprintf(string,length,format,operands);
453  uselocale(previous_locale);
454  }
455  }
456 #else
457  n=(ssize_t) vsnprintf(string,length,format,operands);
458 #endif
459 #else
460  n=(ssize_t) vsprintf(string,format,operands);
461 #endif
462  if (n < 0)
463  string[length-1]='\0';
464  return(n);
465 }
466 
468  const size_t length,const char *magick_restrict format,...)
469 {
470  ssize_t
471  n;
472 
473  va_list
474  operands;
475 
476  va_start(operands,format);
477  n=FormatLocaleStringList(string,length,format,operands);
478  va_end(operands);
479  return(n);
480 }
481 
482 /*
483 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
484 % %
485 % %
486 % %
487 + G e t L o c a l e I n f o _ %
488 % %
489 % %
490 % %
491 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
492 %
493 % GetLocaleInfo_() searches the locale list for the specified tag and if
494 % found returns attributes for that element.
495 %
496 % The format of the GetLocaleInfo method is:
497 %
498 % const LocaleInfo *GetLocaleInfo_(const char *tag,
499 % ExceptionInfo *exception)
500 %
501 % A description of each parameter follows:
502 %
503 % o tag: the locale tag.
504 %
505 % o exception: return any errors or warnings in this structure.
506 %
507 */
508 MagickExport const LocaleInfo *GetLocaleInfo_(const char *tag,
509  ExceptionInfo *exception)
510 {
511  const LocaleInfo
512  *locale_info;
513 
514  assert(exception != (ExceptionInfo *) NULL);
515  if (IsLocaleTreeInstantiated(exception) == MagickFalse)
516  return((const LocaleInfo *) NULL);
518  if ((tag == (const char *) NULL) || (LocaleCompare(tag,"*") == 0))
519  {
521  locale_info=(const LocaleInfo *) GetNextValueInSplayTree(locale_cache);
523  return(locale_info);
524  }
525  locale_info=(const LocaleInfo *) GetValueFromSplayTree(locale_cache,tag);
527  return(locale_info);
528 }
529 
530 /*
531 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
532 % %
533 % %
534 % %
535 % G e t L o c a l e I n f o L i s t %
536 % %
537 % %
538 % %
539 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
540 %
541 % GetLocaleInfoList() returns any locale messages that match the
542 % specified pattern.
543 %
544 % The format of the GetLocaleInfoList function is:
545 %
546 % const LocaleInfo **GetLocaleInfoList(const char *pattern,
547 % size_t *number_messages,ExceptionInfo *exception)
548 %
549 % A description of each parameter follows:
550 %
551 % o pattern: Specifies a pointer to a text string containing a pattern.
552 %
553 % o number_messages: This integer returns the number of locale messages in
554 % the list.
555 %
556 % o exception: return any errors or warnings in this structure.
557 %
558 */
559 
560 #if defined(__cplusplus) || defined(c_plusplus)
561 extern "C" {
562 #endif
563 
564 static int LocaleInfoCompare(const void *x,const void *y)
565 {
566  const LocaleInfo
567  **p,
568  **q;
569 
570  p=(const LocaleInfo **) x,
571  q=(const LocaleInfo **) y;
572  if (LocaleCompare((*p)->path,(*q)->path) == 0)
573  return(LocaleCompare((*p)->tag,(*q)->tag));
574  return(LocaleCompare((*p)->path,(*q)->path));
575 }
576 
577 #if defined(__cplusplus) || defined(c_plusplus)
578 }
579 #endif
580 
581 MagickExport const LocaleInfo **GetLocaleInfoList(const char *pattern,
582  size_t *number_messages,ExceptionInfo *exception)
583 {
584  const LocaleInfo
585  **messages;
586 
587  const LocaleInfo
588  *p;
589 
590  ssize_t
591  i;
592 
593  /*
594  Allocate locale list.
595  */
596  assert(pattern != (char *) NULL);
597  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
598  assert(number_messages != (size_t *) NULL);
599  *number_messages=0;
600  p=GetLocaleInfo_("*",exception);
601  if (p == (const LocaleInfo *) NULL)
602  return((const LocaleInfo **) NULL);
603  messages=(const LocaleInfo **) AcquireQuantumMemory((size_t)
604  GetNumberOfNodesInSplayTree(locale_cache)+1UL,sizeof(*messages));
605  if (messages == (const LocaleInfo **) NULL)
606  return((const LocaleInfo **) NULL);
607  /*
608  Generate locale list.
609  */
613  for (i=0; p != (const LocaleInfo *) NULL; )
614  {
615  if ((p->stealth == MagickFalse) &&
616  (GlobExpression(p->tag,pattern,MagickTrue) != MagickFalse))
617  messages[i++]=p;
619  }
621  qsort((void *) messages,(size_t) i,sizeof(*messages),LocaleInfoCompare);
622  messages[i]=(LocaleInfo *) NULL;
623  *number_messages=(size_t) i;
624  return(messages);
625 }
626 
627 /*
628 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
629 % %
630 % %
631 % %
632 % G e t L o c a l e L i s t %
633 % %
634 % %
635 % %
636 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
637 %
638 % GetLocaleList() returns any locale messages that match the specified
639 % pattern.
640 %
641 % The format of the GetLocaleList function is:
642 %
643 % char **GetLocaleList(const char *pattern,size_t *number_messages,
644 % Exceptioninfo *exception)
645 %
646 % A description of each parameter follows:
647 %
648 % o pattern: Specifies a pointer to a text string containing a pattern.
649 %
650 % o number_messages: This integer returns the number of messages in the
651 % list.
652 %
653 % o exception: return any errors or warnings in this structure.
654 %
655 */
656 
657 #if defined(__cplusplus) || defined(c_plusplus)
658 extern "C" {
659 #endif
660 
661 static int LocaleTagCompare(const void *x,const void *y)
662 {
663  char
664  **p,
665  **q;
666 
667  p=(char **) x;
668  q=(char **) y;
669  return(LocaleCompare(*p,*q));
670 }
671 
672 #if defined(__cplusplus) || defined(c_plusplus)
673 }
674 #endif
675 
676 MagickExport char **GetLocaleList(const char *pattern,size_t *number_messages,
677  ExceptionInfo *exception)
678 {
679  char
680  **messages;
681 
682  const LocaleInfo
683  *p;
684 
685  ssize_t
686  i;
687 
688  /*
689  Allocate locale list.
690  */
691  assert(pattern != (char *) NULL);
692  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
693  assert(number_messages != (size_t *) NULL);
694  *number_messages=0;
695  p=GetLocaleInfo_("*",exception);
696  if (p == (const LocaleInfo *) NULL)
697  return((char **) NULL);
698  messages=(char **) AcquireQuantumMemory((size_t)
699  GetNumberOfNodesInSplayTree(locale_cache)+1UL,sizeof(*messages));
700  if (messages == (char **) NULL)
701  return((char **) NULL);
704  for (i=0; p != (const LocaleInfo *) NULL; )
705  {
706  if ((p->stealth == MagickFalse) &&
707  (GlobExpression(p->tag,pattern,MagickTrue) != MagickFalse))
708  messages[i++]=ConstantString(p->tag);
710  }
712  qsort((void *) messages,(size_t) i,sizeof(*messages),LocaleTagCompare);
713  messages[i]=(char *) NULL;
714  *number_messages=(size_t) i;
715  return(messages);
716 }
717 
718 /*
719 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
720 % %
721 % %
722 % %
723 % G e t L o c a l e M e s s a g e %
724 % %
725 % %
726 % %
727 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
728 %
729 % GetLocaleMessage() returns a message in the current locale that matches the
730 % supplied tag.
731 %
732 % The format of the GetLocaleMessage method is:
733 %
734 % const char *GetLocaleMessage(const char *tag)
735 %
736 % A description of each parameter follows:
737 %
738 % o tag: Return a message that matches this tag in the current locale.
739 %
740 */
741 MagickExport const char *GetLocaleMessage(const char *tag)
742 {
743  char
744  name[MagickLocaleExtent];
745 
746  const LocaleInfo
747  *locale_info;
748 
750  *exception;
751 
752  if ((tag == (const char *) NULL) || (*tag == '\0'))
753  return(tag);
754  exception=AcquireExceptionInfo();
755  (void) FormatLocaleString(name,MagickLocaleExtent,"%s/",tag);
756  locale_info=GetLocaleInfo_(name,exception);
757  exception=DestroyExceptionInfo(exception);
758  if (locale_info != (const LocaleInfo *) NULL)
759  return(locale_info->message);
760  return(tag);
761 }
762 
763 /*
764 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
765 % %
766 % %
767 % %
768 % G e t L o c a l e O p t i o n s %
769 % %
770 % %
771 % %
772 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
773 %
774 % GetLocaleOptions() returns any Magick configuration messages associated
775 % with the specified filename.
776 %
777 % The format of the GetLocaleOptions method is:
778 %
779 % LinkedListInfo *GetLocaleOptions(const char *filename,
780 % ExceptionInfo *exception)
781 %
782 % A description of each parameter follows:
783 %
784 % o filename: the locale file tag.
785 %
786 % o exception: return any errors or warnings in this structure.
787 %
788 */
790  ExceptionInfo *exception)
791 {
792  char
793  path[MagickPathExtent];
794 
795  const char
796  *element;
797 
799  *messages,
800  *paths;
801 
802  StringInfo
803  *xml;
804 
805  assert(filename != (const char *) NULL);
806  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
807  assert(exception != (ExceptionInfo *) NULL);
808  (void) CopyMagickString(path,filename,MagickPathExtent);
809  /*
810  Load XML from configuration files to linked-list.
811  */
812  messages=NewLinkedList(0);
813  paths=GetConfigurePaths(filename,exception);
814  if (paths != (LinkedListInfo *) NULL)
815  {
817  element=(const char *) GetNextValueInLinkedList(paths);
818  while (element != (const char *) NULL)
819  {
820  (void) FormatLocaleString(path,MagickPathExtent,"%s%s",element,
821  filename);
823  "Searching for locale file: \"%s\"",path);
824  xml=ConfigureFileToStringInfo(path);
825  if (xml != (StringInfo *) NULL)
826  (void) AppendValueToLinkedList(messages,xml);
827  element=(const char *) GetNextValueInLinkedList(paths);
828  }
830  }
831 #if defined(MAGICKCORE_WINDOWS_SUPPORT)
832  {
833  char
834  *blob;
835 
836  blob=(char *) NTResourceToBlob(filename);
837  if (blob != (char *) NULL)
838  {
839  xml=AcquireStringInfo(0);
840  SetStringInfoLength(xml,strlen(blob)+1);
841  SetStringInfoDatum(xml,(const unsigned char *) blob);
842  blob=(char *) RelinquishMagickMemory(blob);
843  SetStringInfoPath(xml,filename);
844  (void) AppendValueToLinkedList(messages,xml);
845  }
846  }
847 #endif
848  ResetLinkedListIterator(messages);
849  return(messages);
850 }
851 
852 /*
853 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
854 % %
855 % %
856 % %
857 % G e t L o c a l e V a l u e %
858 % %
859 % %
860 % %
861 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
862 %
863 % GetLocaleValue() returns the message associated with the locale info.
864 %
865 % The format of the GetLocaleValue method is:
866 %
867 % const char *GetLocaleValue(const LocaleInfo *locale_info)
868 %
869 % A description of each parameter follows:
870 %
871 % o locale_info: The locale info.
872 %
873 */
874 MagickExport const char *GetLocaleValue(const LocaleInfo *locale_info)
875 {
876  (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
877  assert(locale_info != (LocaleInfo *) NULL);
878  assert(locale_info->signature == MagickCoreSignature);
879  return(locale_info->message);
880 }
881 
882 /*
883 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
884 % %
885 % %
886 % %
887 + I s L o c a l e T r e e I n s t a n t i a t e d %
888 % %
889 % %
890 % %
891 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
892 %
893 % IsLocaleTreeInstantiated() determines if the locale tree is instantiated.
894 % If not, it instantiates the tree and returns it.
895 %
896 % The format of the IsLocaleInstantiated method is:
897 %
898 % MagickBooleanType IsLocaleTreeInstantiated(ExceptionInfo *exception)
899 %
900 % A description of each parameter follows.
901 %
902 % o exception: return any errors or warnings in this structure.
903 %
904 */
906 {
907  if (locale_cache == (SplayTreeInfo *) NULL)
908  {
909  if (locale_semaphore == (SemaphoreInfo *) NULL)
912  if (locale_cache == (SplayTreeInfo *) NULL)
913  {
914  char
915  *locale;
916 
917  const char
918  *p;
919 
920  locale=(char *) NULL;
921  p=setlocale(LC_CTYPE,(const char *) NULL);
922  if (p != (const char *) NULL)
923  locale=ConstantString(p);
924  if (locale == (char *) NULL)
925  locale=GetEnvironmentValue("LC_ALL");
926  if (locale == (char *) NULL)
927  locale=GetEnvironmentValue("LC_MESSAGES");
928  if (locale == (char *) NULL)
929  locale=GetEnvironmentValue("LC_CTYPE");
930  if (locale == (char *) NULL)
931  locale=GetEnvironmentValue("LANG");
932  if (locale == (char *) NULL)
933  locale=ConstantString("C");
935  locale=DestroyString(locale);
936  }
938  }
939  return(locale_cache != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
940 }
941 
942 /*
943 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
944 % %
945 % %
946 % %
947 + I n t e r p r e t L o c a l e V a l u e %
948 % %
949 % %
950 % %
951 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
952 %
953 % InterpretLocaleValue() interprets the string as a floating point number in
954 % the "C" locale and returns its value as a double. If sentinal is not a null
955 % pointer, the method also sets the value pointed by sentinal to point to the
956 % first character after the number.
957 %
958 % The format of the InterpretLocaleValue method is:
959 %
960 % double InterpretLocaleValue(const char *value,char **sentinal)
961 %
962 % A description of each parameter follows:
963 %
964 % o value: the string value.
965 %
966 % o sentinal: if sentinal is not NULL, a pointer to the character after the
967 % last character used in the conversion is stored in the location
968 % referenced by sentinal.
969 %
970 */
972  char **magick_restrict sentinal)
973 {
974  char
975  *q;
976 
977  double
978  value;
979 
980  if ((*string == '0') && ((string[1] | 0x20)=='x'))
981  value=(double) strtoul(string,&q,16);
982  else
983  {
984 #if defined(MAGICKCORE_LOCALE_SUPPORT) && defined(MAGICKCORE_HAVE_STRTOD_L)
985  locale_t
986  locale;
987 
988  locale=AcquireCLocale();
989  if (locale == (locale_t) NULL)
990  value=strtod(string,&q);
991  else
992  value=strtod_l(string,&q,locale);
993 #else
994  value=strtod(string,&q);
995 #endif
996  }
997  if (sentinal != (char **) NULL)
998  *sentinal=q;
999  return(value);
1000 }
1001 
1002 /*
1003 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1004 % %
1005 % %
1006 % %
1007 % L i s t L o c a l e I n f o %
1008 % %
1009 % %
1010 % %
1011 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1012 %
1013 % ListLocaleInfo() lists the locale info to a file.
1014 %
1015 % The format of the ListLocaleInfo method is:
1016 %
1017 % MagickBooleanType ListLocaleInfo(FILE *file,ExceptionInfo *exception)
1018 %
1019 % A description of each parameter follows.
1020 %
1021 % o file: An pointer to a FILE.
1022 %
1023 % o exception: return any errors or warnings in this structure.
1024 %
1025 */
1027  ExceptionInfo *exception)
1028 {
1029  const char
1030  *path;
1031 
1032  const LocaleInfo
1033  **locale_info;
1034 
1035  ssize_t
1036  i;
1037 
1038  size_t
1039  number_messages;
1040 
1041  if (file == (const FILE *) NULL)
1042  file=stdout;
1043  number_messages=0;
1044  locale_info=GetLocaleInfoList("*",&number_messages,exception);
1045  if (locale_info == (const LocaleInfo **) NULL)
1046  return(MagickFalse);
1047  path=(const char *) NULL;
1048  for (i=0; i < (ssize_t) number_messages; i++)
1049  {
1050  if (locale_info[i]->stealth != MagickFalse)
1051  continue;
1052  if ((path == (const char *) NULL) ||
1053  (LocaleCompare(path,locale_info[i]->path) != 0))
1054  {
1055  if (locale_info[i]->path != (char *) NULL)
1056  (void) FormatLocaleFile(file,"\nPath: %s\n\n",locale_info[i]->path);
1057  (void) FormatLocaleFile(file,"Tag/Message\n");
1058  (void) FormatLocaleFile(file,
1059  "-------------------------------------------------"
1060  "------------------------------\n");
1061  }
1062  path=locale_info[i]->path;
1063  (void) FormatLocaleFile(file,"%s\n",locale_info[i]->tag);
1064  if (locale_info[i]->message != (char *) NULL)
1065  (void) FormatLocaleFile(file," %s",locale_info[i]->message);
1066  (void) FormatLocaleFile(file,"\n");
1067  }
1068  (void) fflush(file);
1069  locale_info=(const LocaleInfo **)
1070  RelinquishMagickMemory((void *) locale_info);
1071  return(MagickTrue);
1072 }
1073 
1074 /*
1075 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1076 % %
1077 % %
1078 % %
1079 + L o a d L o c a l e C a c h e %
1080 % %
1081 % %
1082 % %
1083 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1084 %
1085 % LoadLocaleCache() loads the locale configurations which provides a mapping
1086 % between locale attributes and a locale name.
1087 %
1088 % The format of the LoadLocaleCache method is:
1089 %
1090 % MagickBooleanType LoadLocaleCache(SplayTreeInfo *cache,const char *xml,
1091 % const char *filename,const size_t depth,ExceptionInfo *exception)
1092 %
1093 % A description of each parameter follows:
1094 %
1095 % o xml: The locale list in XML format.
1096 %
1097 % o filename: The locale list filename.
1098 %
1099 % o depth: depth of <include /> statements.
1100 %
1101 % o exception: return any errors or warnings in this structure.
1102 %
1103 */
1104 
1105 static void ChopLocaleComponents(char *path,const size_t components)
1106 {
1107  char
1108  *p;
1109 
1110  ssize_t
1111  count;
1112 
1113  if (*path == '\0')
1114  return;
1115  p=path+strlen(path)-1;
1116  if (*p == '/')
1117  *p='\0';
1118  for (count=0; (count < (ssize_t) components) && (p > path); p--)
1119  if (*p == '/')
1120  {
1121  *p='\0';
1122  count++;
1123  }
1124  if (count < (ssize_t) components)
1125  *path='\0';
1126 }
1127 
1129  const ExceptionType magick_unused(severity),
1130  const char *reason,const char *description)
1131 {
1132  magick_unreferenced(severity);
1133 
1134  if (reason == (char *) NULL)
1135  return;
1136  (void) FormatLocaleFile(stderr,"%s: %s",GetClientName(),reason);
1137  if (description != (char *) NULL)
1138  (void) FormatLocaleFile(stderr," (%s)",description);
1139  (void) FormatLocaleFile(stderr,".\n");
1140  (void) fflush(stderr);
1141  exit(1);
1142 }
1143 
1144 static MagickBooleanType LoadLocaleCache(SplayTreeInfo *cache,const char *xml,
1145  const char *filename,const char *locale,const size_t depth,ExceptionInfo *exception)
1146 {
1147  char
1148  keyword[MagickLocaleExtent],
1149  message[MagickLocaleExtent],
1150  tag[MagickLocaleExtent],
1151  *token;
1152 
1153  const char
1154  *q;
1155 
1157  fatal_handler;
1158 
1159  LocaleInfo
1160  *locale_info;
1161 
1163  status;
1164 
1165  char
1166  *p;
1167 
1168  size_t
1169  extent;
1170 
1171  /*
1172  Read the locale configure file.
1173  */
1175  "Loading locale configure file \"%s\" ...",filename);
1176  if (xml == (const char *) NULL)
1177  return(MagickFalse);
1178  status=MagickTrue;
1179  locale_info=(LocaleInfo *) NULL;
1180  *tag='\0';
1181  *message='\0';
1182  *keyword='\0';
1184  token=AcquireString(xml);
1185  extent=strlen(token)+MagickPathExtent;
1186  for (q=(char *) xml; *q != '\0'; )
1187  {
1188  /*
1189  Interpret XML.
1190  */
1191  (void) GetNextToken(q,&q,extent,token);
1192  if (*token == '\0')
1193  break;
1194  (void) CopyMagickString(keyword,token,MagickLocaleExtent);
1195  if (LocaleNCompare(keyword,"<!DOCTYPE",9) == 0)
1196  {
1197  /*
1198  Doctype element.
1199  */
1200  while ((LocaleNCompare(q,"]>",2) != 0) && (*q != '\0'))
1201  {
1202  (void) GetNextToken(q,&q,extent,token);
1203  while (isspace((int) ((unsigned char) *q)) != 0)
1204  q++;
1205  }
1206  continue;
1207  }
1208  if (LocaleNCompare(keyword,"<!--",4) == 0)
1209  {
1210  /*
1211  Comment element.
1212  */
1213  while ((LocaleNCompare(q,"->",2) != 0) && (*q != '\0'))
1214  {
1215  (void) GetNextToken(q,&q,extent,token);
1216  while (isspace((int) ((unsigned char) *q)) != 0)
1217  q++;
1218  }
1219  continue;
1220  }
1221  if (LocaleCompare(keyword,"<include") == 0)
1222  {
1223  /*
1224  Include element.
1225  */
1226  while (((*token != '/') && (*(token+1) != '>')) && (*q != '\0'))
1227  {
1228  (void) CopyMagickString(keyword,token,MagickLocaleExtent);
1229  (void) GetNextToken(q,&q,extent,token);
1230  if (*token != '=')
1231  continue;
1232  (void) GetNextToken(q,&q,extent,token);
1233  if (LocaleCompare(keyword,"locale") == 0)
1234  {
1235  if (LocaleCompare(locale,token) != 0)
1236  break;
1237  continue;
1238  }
1239  if (LocaleCompare(keyword,"file") == 0)
1240  {
1241  if (depth > MagickMaxRecursionDepth)
1242  (void) ThrowMagickException(exception,GetMagickModule(),
1243  ConfigureError,"IncludeElementNestedTooDeeply","`%s'",token);
1244  else
1245  {
1246  char
1247  path[MagickPathExtent],
1248  *file_xml;
1249 
1250  *path='\0';
1251  GetPathComponent(filename,HeadPath,path);
1252  if (*path != '\0')
1255  if (*token == *DirectorySeparator)
1256  (void) CopyMagickString(path,token,MagickPathExtent);
1257  else
1258  (void) ConcatenateMagickString(path,token,MagickPathExtent);
1259  file_xml=FileToXML(path,~0UL);
1260  if (file_xml != (char *) NULL)
1261  {
1262  status&=LoadLocaleCache(cache,file_xml,path,locale,
1263  depth+1,exception);
1264  file_xml=DestroyString(file_xml);
1265  }
1266  }
1267  }
1268  }
1269  continue;
1270  }
1271  if (LocaleCompare(keyword,"<locale") == 0)
1272  {
1273  /*
1274  Locale element.
1275  */
1276  while ((*token != '>') && (*q != '\0'))
1277  {
1278  (void) CopyMagickString(keyword,token,MagickLocaleExtent);
1279  (void) GetNextToken(q,&q,extent,token);
1280  if (*token != '=')
1281  continue;
1282  (void) GetNextToken(q,&q,extent,token);
1283  }
1284  continue;
1285  }
1286  if (LocaleCompare(keyword,"</locale>") == 0)
1287  {
1288  ChopLocaleComponents(tag,1);
1290  continue;
1291  }
1292  if (LocaleCompare(keyword,"<localemap>") == 0)
1293  continue;
1294  if (LocaleCompare(keyword,"</localemap>") == 0)
1295  continue;
1296  if (LocaleCompare(keyword,"<message") == 0)
1297  {
1298  /*
1299  Message element.
1300  */
1301  while ((*token != '>') && (*q != '\0'))
1302  {
1303  (void) CopyMagickString(keyword,token,MagickLocaleExtent);
1304  (void) GetNextToken(q,&q,extent,token);
1305  if (*token != '=')
1306  continue;
1307  (void) GetNextToken(q,&q,extent,token);
1308  if (LocaleCompare(keyword,"name") == 0)
1309  {
1310  (void) ConcatenateMagickString(tag,token,MagickLocaleExtent);
1312  }
1313  }
1314  for (p=(char *) q; (*q != '<') && (*q != '\0'); q++) ;
1315  while (isspace((int) ((unsigned char) *p)) != 0)
1316  p++;
1317  q--;
1318  while ((isspace((int) ((unsigned char) *q)) != 0) && (q > p))
1319  q--;
1320  (void) CopyMagickString(message,p,MagickMin((size_t) (q-p+2),
1322  locale_info=(LocaleInfo *) AcquireCriticalMemory(sizeof(*locale_info));
1323  (void) memset(locale_info,0,sizeof(*locale_info));
1324  locale_info->path=ConstantString(filename);
1325  locale_info->tag=ConstantString(tag);
1326  locale_info->message=ConstantString(message);
1327  locale_info->signature=MagickCoreSignature;
1328  status=AddValueToSplayTree(cache,locale_info->tag,locale_info);
1329  if (status == MagickFalse)
1330  (void) ThrowMagickException(exception,GetMagickModule(),
1331  ResourceLimitError,"MemoryAllocationFailed","`%s'",
1332  locale_info->tag);
1333  (void) ConcatenateMagickString(tag,message,MagickLocaleExtent);
1335  q++;
1336  continue;
1337  }
1338  if (LocaleCompare(keyword,"</message>") == 0)
1339  {
1340  ChopLocaleComponents(tag,2);
1342  continue;
1343  }
1344  if (*keyword == '<')
1345  {
1346  /*
1347  Subpath element.
1348  */
1349  if (*(keyword+1) == '?')
1350  continue;
1351  if (*(keyword+1) == '/')
1352  {
1353  ChopLocaleComponents(tag,1);
1354  if (*tag != '\0')
1356  continue;
1357  }
1358  token[strlen(token)-1]='\0';
1359  (void) CopyMagickString(token,token+1,MagickLocaleExtent);
1360  (void) ConcatenateMagickString(tag,token,MagickLocaleExtent);
1362  continue;
1363  }
1364  (void) GetNextToken(q,(const char **) NULL,extent,token);
1365  if (*token != '=')
1366  continue;
1367  }
1368  token=(char *) RelinquishMagickMemory(token);
1369  (void) SetFatalErrorHandler(fatal_handler);
1370  return(status != 0 ? MagickTrue : MagickFalse);
1371 }
1372 
1373 /*
1374 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1375 % %
1376 % %
1377 % %
1378 % L o c a l e C o m p a r e %
1379 % %
1380 % %
1381 % %
1382 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1383 %
1384 % LocaleCompare() performs a case-insensitive comparison of two strings
1385 % byte-by-byte, according to the ordering of the current locale encoding.
1386 % LocaleCompare returns an integer greater than, equal to, or less than 0,
1387 % if the string pointed to by p is greater than, equal to, or less than the
1388 % string pointed to by q respectively. The sign of a non-zero return value
1389 % is determined by the sign of the difference between the values of the first
1390 % pair of bytes that differ in the strings being compared.
1391 %
1392 % The format of the LocaleCompare method is:
1393 %
1394 % int LocaleCompare(const char *p,const char *q)
1395 %
1396 % A description of each parameter follows:
1397 %
1398 % o p: A pointer to a character string.
1399 %
1400 % o q: A pointer to a character string to compare to p.
1401 %
1402 */
1403 MagickExport int LocaleCompare(const char *p,const char *q)
1404 {
1405  if (p == (char *) NULL)
1406  {
1407  if (q == (char *) NULL)
1408  return(0);
1409  return(-1);
1410  }
1411  if (q == (char *) NULL)
1412  return(1);
1413  {
1414  const unsigned char
1415  *r = (const unsigned char *) p,
1416  *s = (const unsigned char *) q;
1417 
1418  for ( ; (*r != '\0') && (*s != '\0') && ((*r == *s) ||
1419  (LocaleLowercase((int) *r) == LocaleLowercase((int) *s))); r++, s++);
1420  return(LocaleLowercase((int) *r)-LocaleLowercase((int) *s));
1421  }
1422 }
1423 
1424 /*
1425 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1426 % %
1427 % %
1428 % %
1429 % L o c a l e L o w e r %
1430 % %
1431 % %
1432 % %
1433 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1434 %
1435 % LocaleLower() transforms all of the characters in the supplied
1436 % null-terminated string, changing all uppercase letters to lowercase.
1437 %
1438 % The format of the LocaleLower method is:
1439 %
1440 % void LocaleLower(char *string)
1441 %
1442 % A description of each parameter follows:
1443 %
1444 % o string: A pointer to the string to convert to lower-case Locale.
1445 %
1446 */
1447 MagickExport void LocaleLower(char *string)
1448 {
1449  char
1450  *q;
1451 
1452  assert(string != (char *) NULL);
1453  for (q=string; *q != '\0'; q++)
1454  *q=(char) LocaleLowercase((int) *q);
1455 }
1456 
1457 /*
1458 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1459 % %
1460 % %
1461 % %
1462 % L o c a l e L o w e r c a s e %
1463 % %
1464 % %
1465 % %
1466 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1467 %
1468 % LocaleLowercase() convert to lowercase.
1469 %
1470 % The format of the LocaleLowercase method is:
1471 %
1472 % void LocaleLowercase(const int c)
1473 %
1474 % A description of each parameter follows:
1475 %
1476 % o If c is a uppercase letter, return its lowercase equivalent.
1477 %
1478 */
1480 {
1481  if ((c == EOF) || (c != (unsigned char) c))
1482  return(c);
1483 #if defined(MAGICKCORE_LOCALE_SUPPORT)
1484  if (c_locale != (locale_t) NULL)
1485  return(tolower_l((int) ((unsigned char) c),c_locale));
1486 #endif
1487  return(tolower((int) ((unsigned char) c)));
1488 }
1489 
1490 /*
1491 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1492 % %
1493 % %
1494 % %
1495 % L o c a l e N C o m p a r e %
1496 % %
1497 % %
1498 % %
1499 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1500 %
1501 % LocaleNCompare() performs a case-insensitive comparison of two strings
1502 % byte-by-byte, according to the ordering of the current locale encoding.
1503 %
1504 % LocaleNCompare returns an integer greater than, equal to, or less than 0,
1505 % if the string pointed to by p is greater than, equal to, or less than the
1506 % string pointed to by q respectively. The sign of a non-zero return value
1507 % is determined by the sign of the difference between the values of the first
1508 % pair of bytes that differ in the strings being compared.
1509 %
1510 % The LocaleNCompare method makes the same comparison as LocaleCompare but
1511 % looks at a maximum of n bytes. Bytes following a null byte are not
1512 % compared.
1513 %
1514 % The format of the LocaleNCompare method is:
1515 %
1516 % int LocaleNCompare(const char *p,const char *q,const size_t n)
1517 %
1518 % A description of each parameter follows:
1519 %
1520 % o p: A pointer to a character string.
1521 %
1522 % o q: A pointer to a character string to compare to p.
1523 %
1524 % o length: the number of characters to compare in strings p and q.
1525 %
1526 */
1527 MagickExport int LocaleNCompare(const char *p,const char *q,const size_t length)
1528 {
1529  if (p == (char *) NULL)
1530  {
1531  if (q == (char *) NULL)
1532  return(0);
1533  return(-1);
1534  }
1535  if (q == (char *) NULL)
1536  return(1);
1537  if (length == 0)
1538  return(0);
1539  {
1540  const unsigned char
1541  *s = (const unsigned char *) p,
1542  *t = (const unsigned char *) q;
1543 
1544  size_t
1545  n = length;
1546 
1547  for (n--; (*s != '\0') && (*t != '\0') && (n != 0) && ((*s == *t) ||
1548  (LocaleLowercase((int) *s) == LocaleLowercase((int) *t))); s++, t++, n--);
1549  return(LocaleLowercase((int) *s)-LocaleLowercase((int) *t));
1550  }
1551 }
1552 
1553 /*
1554 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1555 % %
1556 % %
1557 % %
1558 % L o c a l e U p p e r %
1559 % %
1560 % %
1561 % %
1562 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1563 %
1564 % LocaleUpper() transforms all of the characters in the supplied
1565 % null-terminated string, changing all lowercase letters to uppercase.
1566 %
1567 % The format of the LocaleUpper method is:
1568 %
1569 % void LocaleUpper(char *string)
1570 %
1571 % A description of each parameter follows:
1572 %
1573 % o string: A pointer to the string to convert to upper-case Locale.
1574 %
1575 */
1576 MagickExport void LocaleUpper(char *string)
1577 {
1578  char
1579  *q;
1580 
1581  assert(string != (char *) NULL);
1582  for (q=string; *q != '\0'; q++)
1583  *q=(char) LocaleUppercase((int) *q);
1584 }
1585 
1586 /*
1587 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1588 % %
1589 % %
1590 % %
1591 % L o c a l e U p p e r c a s e %
1592 % %
1593 % %
1594 % %
1595 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1596 %
1597 % LocaleUppercase() convert to uppercase.
1598 %
1599 % The format of the LocaleUppercase method is:
1600 %
1601 % void LocaleUppercase(const int c)
1602 %
1603 % A description of each parameter follows:
1604 %
1605 % o If c is a lowercase letter, return its uppercase equivalent.
1606 %
1607 */
1609 {
1610  if (c == EOF)
1611  return(c);
1612 #if defined(MAGICKCORE_LOCALE_SUPPORT)
1613  if (c_locale != (locale_t) NULL)
1614  return(toupper_l((int) ((unsigned char) c),c_locale));
1615 #endif
1616  return(toupper((int) ((unsigned char) c)));
1617 }
1618 
1619 /*
1620 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1621 % %
1622 % %
1623 % %
1624 + L o c a l e C o m p o n e n t G e n e s i s %
1625 % %
1626 % %
1627 % %
1628 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1629 %
1630 % LocaleComponentGenesis() instantiates the locale component.
1631 %
1632 % The format of the LocaleComponentGenesis method is:
1633 %
1634 % MagickBooleanType LocaleComponentGenesis(void)
1635 %
1636 */
1638 {
1639  if (locale_semaphore == (SemaphoreInfo *) NULL)
1641 #if defined(MAGICKCORE_LOCALE_SUPPORT)
1642  (void) AcquireCLocale();
1643 #endif
1644  return(MagickTrue);
1645 }
1646 
1647 /*
1648 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1649 % %
1650 % %
1651 % %
1652 + L o c a l e C o m p o n e n t T e r m i n u s %
1653 % %
1654 % %
1655 % %
1656 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1657 %
1658 % LocaleComponentTerminus() destroys the locale component.
1659 %
1660 % The format of the LocaleComponentTerminus method is:
1661 %
1662 % LocaleComponentTerminus(void)
1663 %
1664 */
1666 {
1667  if (locale_semaphore == (SemaphoreInfo *) NULL)
1670  if (locale_cache != (SplayTreeInfo *) NULL)
1672 #if defined(MAGICKCORE_LOCALE_SUPPORT)
1673  DestroyCLocale();
1674 #endif
1677 }
#define magick_restrict
Definition: MagickCore.h:41
static void * DestroyLocaleNode(void *locale_info)
Definition: locale.c:170
#define MagickMaxRecursionDepth
Definition: studio.h:352
MagickExport MagickBooleanType AddValueToSplayTree(SplayTreeInfo *splay_tree, const void *key, const void *value)
Definition: splay-tree.c:154
static void ChopLocaleComponents(char *path, const size_t components)
Definition: locale.c:1105
MagickExport size_t ConcatenateMagickString(char *magick_restrict destination, const char *magick_restrict source, const size_t length)
Definition: string.c:392
MagickPrivate ssize_t FormatLocaleStringList(char *magick_restrict string, const size_t length, const char *magick_restrict format, va_list operands)
Definition: locale.c:418
MagickExport void SetStringInfoPath(StringInfo *string_info, const char *path)
Definition: string.c:1774
MagickExport void UnlockSemaphoreInfo(SemaphoreInfo *semaphore_info)
Definition: semaphore.c:449
MagickExport void ResetLinkedListIterator(LinkedListInfo *list_info)
Definition: linked-list.c:959
MagickPrivate void LocaleComponentTerminus(void)
Definition: locale.c:1665
MagickExport int LocaleLowercase(const int c)
Definition: locale.c:1479
MagickExport LinkedListInfo * DestroyLinkedList(LinkedListInfo *list_info, void *(*relinquish_value)(void *))
Definition: linked-list.c:219
MagickExport SemaphoreInfo * AcquireSemaphoreInfo(void)
Definition: semaphore.c:192
static const char * LocaleMap
Definition: locale.c:78
static void * DestroyOptions(void *message)
Definition: locale.c:288
#define LocaleFilename
Definition: locale.c:72
MagickExport ExceptionInfo * AcquireExceptionInfo(void)
Definition: exception.c:115
static SplayTreeInfo * AcquireLocaleSplayTree(const char *filename, const char *locale, ExceptionInfo *exception)
Definition: locale.c:185
MagickExport MagickBooleanType AppendValueToLinkedList(LinkedListInfo *list_info, const void *value)
Definition: linked-list.c:111
MagickExport ssize_t FormatLocaleString(char *magick_restrict string, const size_t length, const char *magick_restrict format,...)
Definition: locale.c:467
MagickExport LinkedListInfo * GetLocaleOptions(const char *filename, ExceptionInfo *exception)
Definition: locale.c:789
static int LocaleTagCompare(const void *x, const void *y)
Definition: locale.c:661
MagickExport size_t CopyMagickString(char *magick_restrict destination, const char *magick_restrict source, const size_t length)
Definition: string.c:719
Definition: log.h:52
MagickExport void * GetNextValueInLinkedList(LinkedListInfo *list_info)
Definition: linked-list.c:305
ExceptionType
Definition: exception.h:27
MagickExport const void * GetNextValueInSplayTree(SplayTreeInfo *splay_tree)
Definition: splay-tree.c:823
#define MagickCoreSignature
void(* FatalErrorHandler)(const ExceptionType, const char *, const char *)
Definition: exception.h:130
MagickExport int LocaleUppercase(const int c)
Definition: locale.c:1608
MagickExport void LockSemaphoreInfo(SemaphoreInfo *semaphore_info)
Definition: semaphore.c:293
MagickExport unsigned char * GetStringInfoDatum(const StringInfo *string_info)
Definition: string.c:1168
MagickPrivate ssize_t FormatLocaleFileList(FILE *file, const char *magick_restrict format, va_list operands)
Definition: locale.c:327
MagickExport void GetPathComponent(const char *path, PathType type, char *component)
Definition: utility.c:1223
MagickExport ssize_t FormatLocaleFile(FILE *file, const char *magick_restrict format,...)
Definition: locale.c:372
MagickBooleanType
Definition: magick-type.h:173
#define DirectorySeparator
Definition: studio.h:267
unsigned int MagickStatusType
Definition: magick-type.h:129
MagickExport char * AcquireString(const char *source)
Definition: string.c:94
MagickExport void LocaleLower(char *string)
Definition: locale.c:1447
MagickExport MagickBooleanType GlobExpression(const char *magick_restrict expression, const char *magick_restrict pattern, const MagickBooleanType case_insensitive)
Definition: token.c:355
char * message
Definition: locale_.h:30
MagickExport void * AcquireCriticalMemory(const size_t size)
Definition: memory.c:626
MagickExport StringInfo * DestroyStringInfo(StringInfo *string_info)
Definition: string.c:803
MagickExport void * AcquireQuantumMemory(const size_t count, const size_t quantum)
Definition: memory.c:665
MagickExport int LocaleNCompare(const char *p, const char *q, const size_t length)
Definition: locale.c:1527
MagickExport magick_hot_spot size_t GetNextToken(const char *magick_restrict start, const char **magick_restrict end, const size_t extent, char *magick_restrict token)
Definition: token.c:174
#define magick_unused(x)
static MagickBooleanType LoadLocaleCache(SplayTreeInfo *, const char *, const char *, const char *, const size_t, ExceptionInfo *)
Definition: locale.c:1144
MagickExport SplayTreeInfo * DestroySplayTree(SplayTreeInfo *splay_tree)
Definition: splay-tree.c:682
#define MagickPathExtent
char * tag
Definition: locale_.h:30
MagickExport SplayTreeInfo * NewSplayTree(int(*compare)(const void *, const void *), void *(*relinquish_key)(void *), void *(*relinquish_value)(void *))
Definition: splay-tree.c:1141
size_t signature
Definition: locale_.h:38
MagickExport MagickBooleanType ThrowMagickException(ExceptionInfo *exception, const char *module, const char *function, const size_t line, const ExceptionType severity, const char *tag, const char *format,...)
Definition: exception.c:1145
MagickExport MagickBooleanType LogMagickEvent(const LogEventType type, const char *module, const char *function, const size_t line, const char *format,...)
Definition: log.c:1660
MagickExport MagickBooleanType ListLocaleInfo(FILE *file, ExceptionInfo *exception)
Definition: locale.c:1026
MagickExport char ** GetLocaleList(const char *pattern, size_t *number_messages, ExceptionInfo *exception)
Definition: locale.c:676
#define MagickLocaleExtent
MagickExport FatalErrorHandler SetFatalErrorHandler(FatalErrorHandler handler)
Definition: exception.c:910
static SemaphoreInfo * locale_semaphore
Definition: locale.c:90
static int LocaleInfoCompare(const void *x, const void *y)
Definition: locale.c:564
MagickExport char * GetEnvironmentValue(const char *name)
Definition: string.c:1135
MagickExport StringInfo * ConfigureFileToStringInfo(const char *filename)
Definition: string.c:558
MagickExport LinkedListInfo * NewLinkedList(const size_t capacity)
Definition: linked-list.c:713
static void LocaleFatalErrorHandler(const ExceptionType magick_unused(severity), const char *reason, const char *description)
Definition: locale.c:1128
static SplayTreeInfo * locale_cache
Definition: locale.c:93
MagickExport const void * GetValueFromSplayTree(SplayTreeInfo *splay_tree, const void *key)
Definition: splay-tree.c:921
MagickExport const char * GetClientName(void)
Definition: client.c:65
MagickExport StringInfo * AcquireStringInfo(const size_t length)
Definition: string.c:151
MagickExport int LocaleCompare(const char *p, const char *q)
Definition: locale.c:1403
MagickExport LinkedListInfo * DestroyLocaleOptions(LinkedListInfo *messages)
Definition: locale.c:293
#define GetMagickModule()
Definition: log.h:28
MagickExport int CompareSplayTreeString(const void *target, const void *source)
Definition: splay-tree.c:412
MagickExport LinkedListInfo * GetConfigurePaths(const char *filename, ExceptionInfo *exception)
Definition: configure.c:734
MagickExport const char * GetStringInfoPath(const StringInfo *string_info)
Definition: string.c:1255
char * path
Definition: locale_.h:30
MagickExport const char * GetLocaleMessage(const char *tag)
Definition: locale.c:741
MagickPrivate MagickBooleanType LocaleComponentGenesis(void)
Definition: locale.c:1637
MagickExport void SetStringInfoLength(StringInfo *string_info, const size_t length)
Definition: string.c:1698
MagickExport char * DestroyString(char *string)
Definition: string.c:776
MagickExport void ActivateSemaphoreInfo(SemaphoreInfo **semaphore_info)
Definition: semaphore.c:98
MagickExport const LocaleInfo * GetLocaleInfo_(const char *tag, ExceptionInfo *exception)
Definition: locale.c:508
#define MagickMin(x, y)
Definition: image-private.h:37
MagickExport void ResetSplayTreeIterator(SplayTreeInfo *splay_tree)
Definition: splay-tree.c:1472
MagickExport void * RelinquishMagickMemory(void *memory)
Definition: memory.c:1162
#define magick_unreferenced(x)
MagickExport size_t GetNumberOfNodesInSplayTree(const SplayTreeInfo *splay_tree)
Definition: splay-tree.c:976
static MagickBooleanType IsLocaleTreeInstantiated(ExceptionInfo *)
Definition: locale.c:905
MagickExport const char * GetLocaleValue(const LocaleInfo *locale_info)
Definition: locale.c:874
#define MagickPrivate
MagickPrivate char * FileToXML(const char *, const size_t)
Definition: xml-tree.c:598
MagickBooleanType stealth
Definition: locale_.h:35
#define MagickExport
MagickExport const LocaleInfo ** GetLocaleInfoList(const char *pattern, size_t *number_messages, ExceptionInfo *exception)
Definition: locale.c:581
MagickExport void RelinquishSemaphoreInfo(SemaphoreInfo **semaphore_info)
Definition: semaphore.c:351
MagickExport void LocaleUpper(char *string)
Definition: locale.c:1576
MagickExport char * ConstantString(const char *source)
Definition: string.c:666
MagickExport void SetStringInfoDatum(StringInfo *string_info, const unsigned char *source)
Definition: string.c:1665
MagickExport ExceptionInfo * DestroyExceptionInfo(ExceptionInfo *exception)
Definition: exception.c:418
MagickExport double InterpretLocaleValue(const char *magick_restrict string, char **magick_restrict sentinal)
Definition: locale.c:971