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