]> granicus.if.org Git - imagemagick/blobdiff - magick/locale.c
(no commit message)
[imagemagick] / magick / locale.c
index 291431a5b409f0ba625f3e19940e47a86ced1470..4cbe278eb3f017ae991991234f74b91f250be0cb 100644 (file)
@@ -17,7 +17,7 @@
 %                                 July 2003                                   %
 %                                                                             %
 %                                                                             %
-%  Copyright 1999-2009 ImageMagick Studio LLC, a non-profit organization      %
+%  Copyright 1999-2011 ImageMagick Studio LLC, a non-profit organization      %
 %  dedicated to making software imaging solutions freely available.           %
 %                                                                             %
 %  You may not use this file except in compliance with the License.  You may  %
@@ -98,34 +98,6 @@ static MagickBooleanType
 %                                                                             %
 %                                                                             %
 %                                                                             %
-+   D e s t r o y L o c a l e F a c i l i t y                                 %
-%                                                                             %
-%                                                                             %
-%                                                                             %
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%
-%  DestroyLocaleFacility() destroys the locale facility.
-%
-%  The format of the DestroyLocaleFacility method is:
-%
-%      DestroyLocaleFacility(void)
-%
-*/
-MagickExport void DestroyLocaleFacility(void)
-{
-  AcquireSemaphoreInfo(&locale_semaphore);
-  if (locale_list != (SplayTreeInfo *) NULL)
-    locale_list=DestroySplayTree(locale_list);
-  instantiate_locale=MagickFalse;
-  RelinquishSemaphoreInfo(locale_semaphore);
-  DestroySemaphoreInfo(&locale_semaphore);
-}
-\f
-/*
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%                                                                             %
-%                                                                             %
-%                                                                             %
 %   D e s t r o y L o c a l e O p t i o n s                                   %
 %                                                                             %
 %                                                                             %
@@ -219,7 +191,7 @@ MagickExport const LocaleInfo *GetLocaleInfo_(const char *tag,
 %  The format of the GetLocaleInfoList function is:
 %
 %      const LocaleInfo **GetLocaleInfoList(const char *pattern,
-%        unsigned long *number_messages,ExceptionInfo *exception)
+%        size_t *number_messages,ExceptionInfo *exception)
 %
 %  A description of each parameter follows:
 %
@@ -254,7 +226,7 @@ static int LocaleInfoCompare(const void *x,const void *y)
 #endif
 
 MagickExport const LocaleInfo **GetLocaleInfoList(const char *pattern,
-  unsigned long *number_messages,ExceptionInfo *exception)
+  size_t *number_messages,ExceptionInfo *exception)
 {
   const LocaleInfo
     **messages;
@@ -262,7 +234,7 @@ MagickExport const LocaleInfo **GetLocaleInfoList(const char *pattern,
   register const LocaleInfo
     *p;
 
-  register long
+  register ssize_t
     i;
 
   /*
@@ -270,7 +242,7 @@ MagickExport const LocaleInfo **GetLocaleInfoList(const char *pattern,
   */
   assert(pattern != (char *) NULL);
   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
-  assert(number_messages != (unsigned long *) NULL);
+  assert(number_messages != (size_t *) NULL);
   *number_messages=0;
   p=GetLocaleInfo_("*",exception);
   if (p == (const LocaleInfo *) NULL)
@@ -282,7 +254,7 @@ MagickExport const LocaleInfo **GetLocaleInfoList(const char *pattern,
   /*
     Generate locale list.
   */
-  AcquireSemaphoreInfo(&locale_semaphore);
+  LockSemaphoreInfo(locale_semaphore);
   ResetSplayTreeIterator(locale_list);
   p=(const LocaleInfo *) GetNextValueInSplayTree(locale_list);
   for (i=0; p != (const LocaleInfo *) NULL; )
@@ -292,10 +264,10 @@ MagickExport const LocaleInfo **GetLocaleInfoList(const char *pattern,
       messages[i++]=p;
     p=(const LocaleInfo *) GetNextValueInSplayTree(locale_list);
   }
-  RelinquishSemaphoreInfo(locale_semaphore);
+  UnlockSemaphoreInfo(locale_semaphore);
   qsort((void *) messages,(size_t) i,sizeof(*messages),LocaleInfoCompare);
   messages[i]=(LocaleInfo *) NULL;
-  *number_messages=(unsigned long) i;
+  *number_messages=(size_t) i;
   return(messages);
 }
 \f
@@ -315,7 +287,7 @@ MagickExport const LocaleInfo **GetLocaleInfoList(const char *pattern,
 %
 %  The format of the GetLocaleList function is:
 %
-%      char **GetLocaleList(const char *pattern,unsigned long *number_messages,
+%      char **GetLocaleList(const char *pattern,size_t *number_messages,
 %        Exceptioninfo *exception)
 %
 %  A description of each parameter follows:
@@ -349,7 +321,7 @@ static int LocaleTagCompare(const void *x,const void *y)
 #endif
 
 MagickExport char **GetLocaleList(const char *pattern,
-  unsigned long *number_messages,ExceptionInfo *exception)
+  size_t *number_messages,ExceptionInfo *exception)
 {
   char
     **messages;
@@ -357,7 +329,7 @@ MagickExport char **GetLocaleList(const char *pattern,
   register const LocaleInfo
     *p;
 
-  register long
+  register ssize_t
     i;
 
   /*
@@ -365,18 +337,16 @@ MagickExport char **GetLocaleList(const char *pattern,
   */
   assert(pattern != (char *) NULL);
   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",pattern);
-  assert(number_messages != (unsigned long *) NULL);
+  assert(number_messages != (size_t *) NULL);
   *number_messages=0;
   p=GetLocaleInfo_("*",exception);
   if (p == (const LocaleInfo *) NULL)
     return((char **) NULL);
-  AcquireSemaphoreInfo(&locale_semaphore);
-  RelinquishSemaphoreInfo(locale_semaphore);
   messages=(char **) AcquireQuantumMemory((size_t)
     GetNumberOfNodesInSplayTree(locale_list)+1UL,sizeof(*messages));
   if (messages == (char **) NULL)
     return((char **) NULL);
-  AcquireSemaphoreInfo(&locale_semaphore);
+  LockSemaphoreInfo(locale_semaphore);
   p=(const LocaleInfo *) GetNextValueInSplayTree(locale_list);
   for (i=0; p != (const LocaleInfo *) NULL; )
   {
@@ -385,10 +355,10 @@ MagickExport char **GetLocaleList(const char *pattern,
       messages[i++]=ConstantString(p->tag);
     p=(const LocaleInfo *) GetNextValueInSplayTree(locale_list);
   }
-  RelinquishSemaphoreInfo(locale_semaphore);
+  UnlockSemaphoreInfo(locale_semaphore);
   qsort((void *) messages,(size_t) i,sizeof(*messages),LocaleTagCompare);
   messages[i]=(char *) NULL;
-  *number_messages=(unsigned long) i;
+  *number_messages=(size_t) i;
   return(messages);
 }
 \f
@@ -504,7 +474,7 @@ MagickExport LinkedListInfo *GetLocaleOptions(const char *filename,
       }
       paths=DestroyLinkedList(paths,RelinquishMagickMemory);
     }
-#if defined(__WINDOWS__)
+#if defined(MAGICKCORE_WINDOWS_SUPPORT)
   {
     char
       *blob;
@@ -579,7 +549,9 @@ static MagickBooleanType InitializeLocaleList(ExceptionInfo *exception)
   if ((locale_list == (SplayTreeInfo *) NULL) &&
       (instantiate_locale == MagickFalse))
     {
-      AcquireSemaphoreInfo(&locale_semaphore);
+      if (locale_semaphore == (SemaphoreInfo *) NULL)
+        AcquireSemaphoreInfo(&locale_semaphore);
+      LockSemaphoreInfo(locale_semaphore);
       if ((locale_list == (SplayTreeInfo *) NULL) &&
           (instantiate_locale == MagickFalse))
         {
@@ -607,7 +579,7 @@ static MagickBooleanType InitializeLocaleList(ExceptionInfo *exception)
           locale=DestroyString(locale);
           instantiate_locale=MagickTrue;
         }
-      RelinquishSemaphoreInfo(locale_semaphore);
+      UnlockSemaphoreInfo(locale_semaphore);
     }
   return(locale_list != (SplayTreeInfo *) NULL ? MagickTrue : MagickFalse);
 }
@@ -617,31 +589,6 @@ static MagickBooleanType InitializeLocaleList(ExceptionInfo *exception)
 %                                                                             %
 %                                                                             %
 %                                                                             %
-+   I n s t a n t i a t e L o c a l e F a c i l i t y                         %
-%                                                                             %
-%                                                                             %
-%                                                                             %
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%
-%  InstantiateLocaleFacility() instantiates the locale facility.
-%
-%  The format of the InstantiateLocaleFacility method is:
-%
-%      MagickBooleanType InstantiateLocaleFacility(void)
-%
-*/
-MagickExport MagickBooleanType InstantiateLocaleFacility(void)
-{
-  AcquireSemaphoreInfo(&locale_semaphore);
-  RelinquishSemaphoreInfo(locale_semaphore);
-  return(MagickTrue);
-}
-\f
-/*
-%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-%                                                                             %
-%                                                                             %
-%                                                                             %
 %  L i s t L o c a l e I n f o                                                %
 %                                                                             %
 %                                                                             %
@@ -670,10 +617,10 @@ MagickExport MagickBooleanType ListLocaleInfo(FILE *file,
   const LocaleInfo
     **locale_info;
 
-  register long
+  register ssize_t
     i;
 
-  unsigned long
+  size_t
     number_messages;
 
   if (file == (const FILE *) NULL)
@@ -683,7 +630,7 @@ MagickExport MagickBooleanType ListLocaleInfo(FILE *file,
   if (locale_info == (const LocaleInfo **) NULL)
     return(MagickFalse);
   path=(const char *) NULL;
-  for (i=0; i < (long) number_messages; i++)
+  for (i=0; i < (ssize_t) number_messages; i++)
   {
     if (locale_info[i]->stealth != MagickFalse)
       continue;
@@ -725,7 +672,7 @@ MagickExport MagickBooleanType ListLocaleInfo(FILE *file,
 %  The format of the LoadLocaleList method is:
 %
 %      MagickBooleanType LoadLocaleList(const char *xml,const char *filename,
-%        const unsigned long depth,ExceptionInfo *exception)
+%        const size_t depth,ExceptionInfo *exception)
 %
 %  A description of each parameter follows:
 %
@@ -739,26 +686,26 @@ MagickExport MagickBooleanType ListLocaleInfo(FILE *file,
 %
 */
 
-static void ChopLocaleComponents(char *path,const unsigned long components)
+static void ChopLocaleComponents(char *path,const size_t components)
 {
-  long
-    count;
-
   register char
     *p;
 
+  ssize_t
+    count;
+
   if (*path == '\0')
     return;
   p=path+strlen(path)-1;
   if (*p == '/')
     *p='\0';
-  for (count=0; (count < (long) components) && (p > path); p--)
+  for (count=0; (count < (ssize_t) components) && (p > path); p--)
     if (*p == '/')
       {
         *p='\0';
         count++;
       }
-  if (count < (long) components)
+  if (count < (ssize_t) components)
     *path='\0';
 }
 
@@ -777,8 +724,23 @@ static void *DestroyLocaleNode(void *locale_info)
   return(RelinquishMagickMemory(p));
 }
 
+static void LocaleFatalErrorHandler(
+  const ExceptionType magick_unused(severity),
+  const char *reason,const char *description)
+{
+  if (reason == (char *) NULL)
+    return;
+  (void) fprintf(stderr,"%s: %s",GetClientName(),reason);
+  if (description != (char *) NULL)
+    (void) fprintf(stderr," (%s)",description);
+  (void) fprintf(stderr,".\n");
+  (void) fflush(stderr);
+  exit(1);
+}
+
+
 static MagickBooleanType LoadLocaleList(const char *xml,const char *filename,
-  const char *locale,const unsigned long depth,ExceptionInfo *exception)
+  const char *locale,const size_t depth,ExceptionInfo *exception)
 {
   char
     keyword[MaxTextExtent],
@@ -789,6 +751,9 @@ static MagickBooleanType LoadLocaleList(const char *xml,const char *filename,
   const char
     *q;
 
+  FatalErrorHandler
+    fatal_handler;
+
   LocaleInfo
     *locale_info;
 
@@ -810,17 +775,14 @@ static MagickBooleanType LoadLocaleList(const char *xml,const char *filename,
       locale_list=NewSplayTree(CompareSplayTreeString,(void *(*)(void *)) NULL,
         DestroyLocaleNode);
       if (locale_list == (SplayTreeInfo *) NULL)
-        {
-          ThrowFileException(exception,ResourceLimitError,
-            "MemoryAllocationFailed",filename);
-          return(MagickFalse);
-        }
+        return(MagickFalse);
     }
   status=MagickTrue;
   locale_info=(LocaleInfo *) NULL;
   *tag='\0';
   *message='\0';
   *keyword='\0';
+  fatal_handler=SetFatalErrorHandler(LocaleFatalErrorHandler);
   token=AcquireString(xml);
   for (q=(char *) xml; *q != '\0'; )
   {
@@ -1005,6 +967,7 @@ static MagickBooleanType LoadLocaleList(const char *xml,const char *filename,
       continue;
   }
   token=(char *) RelinquishMagickMemory(token);
+  (void) SetFatalErrorHandler(fatal_handler);
   return(status);
 }
 \f
@@ -1080,3 +1043,57 @@ static MagickBooleanType LoadLocaleLists(const char *filename,
   return(status != 0 ? MagickTrue : MagickFalse);
 #endif
 }
+\f
+/*
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%                                                                             %
+%                                                                             %
+%                                                                             %
++   L o c a l e C o m p o n e n t G e n e s i s                               %
+%                                                                             %
+%                                                                             %
+%                                                                             %
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%
+%  LocaleComponentGenesis() instantiates the locale component.
+%
+%  The format of the LocaleComponentGenesis method is:
+%
+%      MagickBooleanType LocaleComponentGenesis(void)
+%
+*/
+MagickExport MagickBooleanType LocaleComponentGenesis(void)
+{
+  AcquireSemaphoreInfo(&locale_semaphore);
+  return(MagickTrue);
+}
+\f
+/*
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%                                                                             %
+%                                                                             %
+%                                                                             %
++   L o c a l e C o m p o n e n t T e r m i n u s                             %
+%                                                                             %
+%                                                                             %
+%                                                                             %
+%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
+%
+%  LocaleComponentTerminus() destroys the locale component.
+%
+%  The format of the LocaleComponentTerminus method is:
+%
+%      LocaleComponentTerminus(void)
+%
+*/
+MagickExport void LocaleComponentTerminus(void)
+{
+  if (locale_semaphore == (SemaphoreInfo *) NULL)
+    AcquireSemaphoreInfo(&locale_semaphore);
+  LockSemaphoreInfo(locale_semaphore);
+  if (locale_list != (SplayTreeInfo *) NULL)
+    locale_list=DestroySplayTree(locale_list);
+  instantiate_locale=MagickFalse;
+  UnlockSemaphoreInfo(locale_semaphore);
+  DestroySemaphoreInfo(&locale_semaphore);
+}