registry.c

Go to the documentation of this file.
00001 /*
00002 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00003 %                                                                             %
00004 %                                                                             %
00005 %                                                                             %
00006 %           RRRR    EEEEE    GGG   IIIII  SSSSS  TTTTT  RRRR   Y   Y          %
00007 %           R   R   E       G        I    SS       T    R   R   Y Y           %
00008 %           RRRR    EEE     G GGG    I     SSS     T    RRRR     Y            %
00009 %           R R     E       G   G    I       SS    T    R R      Y            %
00010 %           R  R    EEEEE    GGG   IIIII  SSSSS    T    R  R     Y            %
00011 %                                                                             %
00012 %                                                                             %
00013 %                       MagickCore Registry Methods                           %
00014 %                                                                             %
00015 %                              Software Design                                %
00016 %                                John Cristy                                  %
00017 %                                 March 2000                                  %
00018 %                                                                             %
00019 %                                                                             %
00020 %  Copyright 1999-2009 ImageMagick Studio LLC, a non-profit organization      %
00021 %  dedicated to making software imaging solutions freely available.           %
00022 %                                                                             %
00023 %  You may not use this file except in compliance with the License.  You may  %
00024 %  obtain a copy of the License at                                            %
00025 %                                                                             %
00026 %    http://www.imagemagick.org/script/license.php                            %
00027 %                                                                             %
00028 %  Unless required by applicable law or agreed to in writing, software        %
00029 %  distributed under the License is distributed on an "AS IS" BASIS,          %
00030 %  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   %
00031 %  See the License for the specific language governing permissions and        %
00032 %  limitations under the License.                                             %
00033 %                                                                             %
00034 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00035 %
00036 %
00037 %
00038 */
00039 
00040 /*
00041   Include declarations.
00042 */
00043 #include "magick/studio.h"
00044 #include "magick/exception.h"
00045 #include "magick/exception-private.h"
00046 #include "magick/image.h"
00047 #include "magick/list.h"
00048 #include "magick/memory_.h"
00049 #include "magick/registry.h"
00050 #include "magick/splay-tree.h"
00051 #include "magick/string_.h"
00052 #include "magick/utility.h"
00053 
00054 /*
00055   Typedef declarations.
00056 */
00057 typedef struct _RegistryInfo
00058 {
00059   RegistryType
00060     type;
00061 
00062   void
00063     *value;
00064 
00065   unsigned long
00066     signature;
00067 } RegistryInfo;
00068 
00069 /*
00070   Static declarations.
00071 */
00072 static SplayTreeInfo
00073   *registry = (SplayTreeInfo *) NULL;
00074 
00075 static SemaphoreInfo
00076   *registry_semaphore = (SemaphoreInfo *) NULL;
00077 
00078 static volatile MagickBooleanType
00079   instantiate_registry = MagickFalse;
00080 
00081 /*
00082 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00083 %                                                                             %
00084 %                                                                             %
00085 %                                                                             %
00086 %   D e f i n e I m a g e R e g i s t r y                                     %
00087 %                                                                             %
00088 %                                                                             %
00089 %                                                                             %
00090 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00091 %
00092 %  DefineImageRegistry() associates a key/value pair with the image registry.
00093 %
00094 %  The format of the DefineImageRegistry method is:
00095 %
00096 %      MagickBooleanType DefineImageRegistry(const RegistryType type,
00097 %        const char *option,ExceptionInfo *exception)
00098 %
00099 %  A description of each parameter follows:
00100 %
00101 %    o type: the type.
00102 %
00103 %    o option: the option.
00104 %
00105 %    o exception: the exception.
00106 %
00107 */
00108 MagickExport MagickBooleanType DefineImageRegistry(const RegistryType type,
00109   const char *option,ExceptionInfo *exception)
00110 {
00111   char
00112     key[MaxTextExtent],
00113     value[MaxTextExtent];
00114 
00115   register char
00116     *p;
00117 
00118   assert(option != (const char *) NULL);
00119   (void) CopyMagickString(key,option,MaxTextExtent);
00120   for (p=key; *p != '\0'; p++)
00121     if (*p == '=')
00122       break;
00123   *value='\0';
00124   if (*p == '=')
00125     (void) CopyMagickString(value,p+1,MaxTextExtent);
00126   *p='\0';
00127   return(SetImageRegistry(type,key,value,exception));
00128 }
00129 
00130 /*
00131 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00132 %                                                                             %
00133 %                                                                             %
00134 %                                                                             %
00135 %   D e l e t e I m a g e R e g i s t r y                                     %
00136 %                                                                             %
00137 %                                                                             %
00138 %                                                                             %
00139 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00140 %
00141 %  DeleteImageRegistry() deletes a key from the image registry.
00142 %
00143 %  The format of the DeleteImageRegistry method is:
00144 %
00145 %      MagickBooleanType DeleteImageRegistry(const char *key)
00146 %
00147 %  A description of each parameter follows:
00148 %
00149 %    o key: the registry.
00150 %
00151 */
00152 MagickExport MagickBooleanType DeleteImageRegistry(const char *key)
00153 {
00154   if (IsEventLogging() != MagickFalse)
00155     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",key);
00156   if (registry == (void *) NULL)
00157     return(MagickFalse);
00158   return(DeleteNodeFromSplayTree(registry,key));
00159 }
00160 
00161 /*
00162 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00163 %                                                                             %
00164 %                                                                             %
00165 %                                                                             %
00166 %   G e t I m a g e R e g i s t r y                                           %
00167 %                                                                             %
00168 %                                                                             %
00169 %                                                                             %
00170 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00171 %
00172 %  GetImageRegistry() returns a value associated with an image registry key.
00173 %
00174 %  The format of the GetImageRegistry method is:
00175 %
00176 %      void *GetImageRegistry(const RegistryType type,const char *key,
00177 %        ExceptionInfo *exception)
00178 %
00179 %  A description of each parameter follows:
00180 %
00181 %    o type: the type.
00182 %
00183 %    o key: the key.
00184 %
00185 %    o exception: the exception.
00186 %
00187 */
00188 MagickExport void *GetImageRegistry(const RegistryType type,const char *key,
00189   ExceptionInfo *exception)
00190 {
00191   void
00192     *value;
00193 
00194   RegistryInfo
00195     *registry_info;
00196 
00197   if (IsEventLogging() != MagickFalse)
00198     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",key);
00199   if (registry == (void *) NULL)
00200     return((void *) NULL);
00201   registry_info=(RegistryInfo *) GetValueFromSplayTree(registry,key);
00202   if (registry_info == (void *) NULL)
00203     {
00204       (void) ThrowMagickException(exception,GetMagickModule(),RegistryError,
00205         "UnableToGetRegistryID","`%s'",key);
00206       return((void *) NULL);
00207     }
00208   value=(void *) NULL;
00209   switch (type)
00210   {
00211     case ImageRegistryType:
00212     {
00213       if (type == registry_info->type)
00214         value=(void *) CloneImageList((Image *) registry_info->value,exception);
00215       break;
00216     }
00217     case ImageInfoRegistryType:
00218     {
00219       if (type == registry_info->type)
00220         value=(void *) CloneImageInfo((ImageInfo *) registry_info->value);
00221       break;
00222     }
00223     case StringRegistryType:
00224     {
00225       switch (registry_info->type)
00226       {
00227         case ImageRegistryType:
00228         {
00229           value=(Image *) ConstantString(((Image *)
00230             registry_info->value)->filename);
00231           break;
00232         }
00233         case ImageInfoRegistryType:
00234         {
00235           value=(Image *) ConstantString(((ImageInfo *)
00236             registry_info->value)->filename);
00237           break;
00238         }
00239         case StringRegistryType:
00240         {
00241           value=(void *) ConstantString((char *) registry_info->value);
00242           break;
00243         }
00244         default:
00245           break;
00246       }
00247       break;
00248     }
00249     default:
00250       break;
00251   }
00252   return(value);
00253 }
00254 
00255 /*
00256 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00257 %                                                                             %
00258 %                                                                             %
00259 %                                                                             %
00260 %   G e t N e x t I m a g e R e g i s t r y                                   %
00261 %                                                                             %
00262 %                                                                             %
00263 %                                                                             %
00264 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00265 %
00266 %  GetNextImageRegistry() gets the next image registry value.
00267 %
00268 %  The format of the GetNextImageRegistry method is:
00269 %
00270 %      char *GetNextImageRegistry(void)
00271 %
00272 */
00273 MagickExport char *GetNextImageRegistry(void)
00274 {
00275   if (IsEventLogging() != MagickFalse)
00276     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
00277   if (registry == (void *) NULL)
00278     return((char *) NULL);
00279   return((char *) GetNextKeyInSplayTree(registry));
00280 }
00281 
00282 /*
00283 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00284 %                                                                             %
00285 %                                                                             %
00286 %                                                                             %
00287 +   R e g i s t r y C o m p o n e n t G e n e s i s                           %
00288 %                                                                             %
00289 %                                                                             %
00290 %                                                                             %
00291 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00292 %
00293 %  RegistryComponentGenesis() instantiates the registry component.
00294 %
00295 %  The format of the RegistryComponentGenesis method is:
00296 %
00297 %      MagickBooleanType RegistryComponentGenesis(void)
00298 %
00299 */
00300 MagickExport MagickBooleanType RegistryComponentGenesis(void)
00301 {
00302   AcquireSemaphoreInfo(&registry_semaphore);
00303   return(MagickTrue);
00304 }
00305 
00306 /*
00307 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00308 %                                                                             %
00309 %                                                                             %
00310 %                                                                             %
00311 %   R e g i s t r y C o m p o n e n t T e r m i n u s                         %
00312 %                                                                             %
00313 %                                                                             %
00314 %                                                                             %
00315 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00316 %
00317 %  RegistryComponentTerminus() destroys the registry component.
00318 %
00319 %  The format of the DestroyDefines method is:
00320 %
00321 %      void RegistryComponentTerminus(void)
00322 %
00323 */
00324 MagickExport void RegistryComponentTerminus(void)
00325 {
00326   if (registry_semaphore == (SemaphoreInfo *) NULL)
00327     AcquireSemaphoreInfo(&registry_semaphore);
00328   (void) LockSemaphoreInfo(registry_semaphore);
00329   if (IsEventLogging() != MagickFalse)
00330     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
00331   if (registry != (void *) NULL)
00332     registry=DestroySplayTree(registry);
00333   instantiate_registry=MagickFalse;
00334   (void) UnlockSemaphoreInfo(registry_semaphore);
00335   DestroySemaphoreInfo(&registry_semaphore);
00336 }
00337 
00338 /*
00339 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00340 %                                                                             %
00341 %                                                                             %
00342 %                                                                             %
00343 %   R e m o v e I m a g e R e g i s t r y                                     %
00344 %                                                                             %
00345 %                                                                             %
00346 %                                                                             %
00347 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00348 %
00349 %  RemoveImageRegistry() removes a key from the image registry and returns its
00350 %  value.
00351 %
00352 %  The format of the RemoveImageRegistry method is:
00353 %
00354 %      void *RemoveImageRegistry(const char *key)
00355 %
00356 %  A description of each parameter follows:
00357 %
00358 %    o key: the registry.
00359 %
00360 */
00361 MagickExport void *RemoveImageRegistry(const char *key)
00362 {
00363   if (IsEventLogging() != MagickFalse)
00364     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",key);
00365   if (registry == (void *) NULL)
00366     return((void *) NULL);
00367   return(RemoveNodeFromSplayTree(registry,key));
00368 }
00369 
00370 /*
00371 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00372 %                                                                             %
00373 %                                                                             %
00374 %                                                                             %
00375 %   R e s e t I m a g e R e g i s t r y I t e r a t o r                       %
00376 %                                                                             %
00377 %                                                                             %
00378 %                                                                             %
00379 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00380 %
00381 %  ResetImageRegistryIterator() resets the registry iterator.  Use it in
00382 %  conjunction with GetNextImageRegistry() to iterate over all the values
00383 %  in the image registry.
00384 %
00385 %  The format of the ResetImageRegistryIterator method is:
00386 %
00387 %      ResetImageRegistryIterator(void)
00388 %
00389 */
00390 MagickExport void ResetImageRegistryIterator(void)
00391 {
00392   if (IsEventLogging() != MagickFalse)
00393     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
00394   if (registry == (void *) NULL)
00395     return;
00396   ResetSplayTreeIterator(registry);
00397 }
00398 
00399 /*
00400 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00401 %                                                                             %
00402 %                                                                             %
00403 %                                                                             %
00404 %   S e t I m a g e R e g i s t r y                                           %
00405 %                                                                             %
00406 %                                                                             %
00407 %                                                                             %
00408 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00409 %
00410 %  SetImageRegistry() associates a value with an image registry key.
00411 %
00412 %  The format of the SetImageRegistry method is:
00413 %
00414 %      MagickBooleanType SetImageRegistry(const RegistryType type,
00415 %        const char *key,const void *value,ExceptionInfo *exception)
00416 %
00417 %  A description of each parameter follows:
00418 %
00419 %    o type: the type.
00420 %
00421 %    o key: the key.
00422 %
00423 %    o value: the value.
00424 %
00425 %    o exception: the exception.
00426 %
00427 */
00428 
00429 static void *DestroyRegistryNode(void *registry_info)
00430 {
00431   register RegistryInfo
00432     *p;
00433 
00434   p=(RegistryInfo *) registry_info;
00435   switch (p->type)
00436   {
00437     case StringRegistryType:
00438     default:
00439     {
00440       p->value=RelinquishMagickMemory(p->value);
00441       break;
00442     }
00443     case ImageRegistryType:
00444     {
00445       p->value=(void *) DestroyImageList((Image *) p->value);
00446       break;
00447     }
00448     case ImageInfoRegistryType:
00449     {
00450       p->value=(void *) DestroyImageInfo((ImageInfo *) p->value);
00451       break;
00452     }
00453   }
00454   return(RelinquishMagickMemory(p));
00455 }
00456 
00457 MagickExport MagickBooleanType SetImageRegistry(const RegistryType type,
00458   const char *key,const void *value,ExceptionInfo *exception)
00459 {
00460   MagickBooleanType
00461     status;
00462 
00463   RegistryInfo
00464     *registry_info;
00465 
00466   void
00467     *clone_value;
00468 
00469   if (IsEventLogging() != MagickFalse)
00470     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",key);
00471   clone_value=(void *) NULL;
00472   switch (type)
00473   {
00474     case StringRegistryType:
00475     default:
00476     {
00477       const char
00478         *string;
00479 
00480       string=(const char *) value;
00481       clone_value=(void *) ConstantString(string);
00482       break;
00483     }
00484     case ImageRegistryType:
00485     {
00486       const Image
00487         *image;
00488 
00489       image=(const Image *) value;
00490       if (image->signature != MagickSignature)
00491         {
00492           (void) ThrowMagickException(exception,GetMagickModule(),RegistryError,
00493             "UnableToSetRegistry","%s",key);
00494           return(MagickFalse);
00495         }
00496       clone_value=(void *) CloneImageList(image,exception);
00497       break;
00498     }
00499     case ImageInfoRegistryType:
00500     {
00501       const ImageInfo
00502         *image_info;
00503 
00504       image_info=(const ImageInfo *) value;
00505       if (image_info->signature != MagickSignature)
00506         {
00507           (void) ThrowMagickException(exception,GetMagickModule(),RegistryError,
00508             "UnableToSetRegistry","%s",key);
00509           return(MagickFalse);
00510         }
00511       clone_value=(void *) CloneImageInfo(image_info);
00512       break;
00513     }
00514   }
00515   if (clone_value == (void *) NULL)
00516     return(MagickFalse);
00517   registry_info=(RegistryInfo *) AcquireMagickMemory(sizeof(*registry_info));
00518   if (registry_info == (RegistryInfo *) NULL)
00519     ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
00520   (void) ResetMagickMemory(registry_info,0,sizeof(*registry_info));
00521   registry_info->type=type;
00522   registry_info->value=clone_value;
00523   registry_info->signature=MagickSignature;
00524   if ((registry == (SplayTreeInfo *) NULL) &&
00525       (instantiate_registry == MagickFalse))
00526     {
00527       if (registry_semaphore == (SemaphoreInfo *) NULL)
00528         AcquireSemaphoreInfo(&registry_semaphore);
00529       (void) LockSemaphoreInfo(registry_semaphore);
00530       if ((registry == (SplayTreeInfo *) NULL) &&
00531           (instantiate_registry == MagickFalse))
00532         {
00533           registry=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
00534             DestroyRegistryNode);
00535           instantiate_registry=MagickTrue;
00536         }
00537       (void) UnlockSemaphoreInfo(registry_semaphore);
00538     }
00539   status=AddValueToSplayTree(registry,ConstantString(key),registry_info);
00540   return(status);
00541 }

Generated on 21 Nov 2009 for MagickCore by  doxygen 1.6.1