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