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