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