2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % RRRR EEEEE GGG IIIII SSSSS TTTTT RRRR Y Y %
7 % R R E G I SS T R R Y Y %
8 % RRRR EEE G GGG I SSS T RRRR Y %
9 % R R E G G I SS T R R Y %
10 % R R EEEEE GGG IIIII SSSSS T R R Y %
13 % MagickCore Registry Methods %
20 % Copyright 1999-2009 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
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. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
43 #include "magick/studio.h"
44 #include "magick/exception.h"
45 #include "magick/exception-private.h"
46 #include "magick/image.h"
47 #include "magick/list.h"
48 #include "magick/memory_.h"
49 #include "magick/registry.h"
50 #include "magick/splay-tree.h"
51 #include "magick/string_.h"
52 #include "magick/utility.h"
57 typedef struct _RegistryInfo
73 *registry = (SplayTreeInfo *) NULL;
76 *registry_semaphore = (SemaphoreInfo *) NULL;
78 static volatile MagickBooleanType
79 instantiate_registry = MagickFalse;
82 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
86 % D e f i n e I m a g e R e g i s t r y %
90 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
92 % DefineImageRegistry() associates a key/value pair with the image registry.
94 % The format of the DefineImageRegistry method is:
96 % MagickBooleanType DefineImageRegistry(const RegistryType type,
97 % const char *option,ExceptionInfo *exception)
99 % A description of each parameter follows:
103 % o option: the option.
105 % o exception: the exception.
108 MagickExport MagickBooleanType DefineImageRegistry(const RegistryType type,
109 const char *option,ExceptionInfo *exception)
113 value[MaxTextExtent];
118 assert(option != (const char *) NULL);
119 (void) CopyMagickString(key,option,MaxTextExtent);
120 for (p=key; *p != '\0'; p++)
125 (void) CopyMagickString(value,p+1,MaxTextExtent);
127 return(SetImageRegistry(type,key,value,exception));
131 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
135 % D e l e t e I m a g e R e g i s t r y %
139 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
141 % DeleteImageRegistry() deletes a key from the image registry.
143 % The format of the DeleteImageRegistry method is:
145 % MagickBooleanType DeleteImageRegistry(const char *key)
147 % A description of each parameter follows:
149 % o key: the registry.
152 MagickExport MagickBooleanType DeleteImageRegistry(const char *key)
154 if (IsEventLogging() != MagickFalse)
155 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",key);
156 if (registry == (void *) NULL)
158 return(DeleteNodeFromSplayTree(registry,key));
162 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
166 % D e s t r o y R e g i s t r y C o m p o n e n t %
170 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
172 % DestroyRegistryComponent() destroys the registry component.
174 % The format of the DestroyDefines method is:
176 % void DestroyRegistryComponent(void)
179 MagickExport void DestroyRegistryComponent(void)
181 AcquireSemaphoreInfo(®istry_semaphore);
182 if (IsEventLogging() != MagickFalse)
183 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
184 if (registry != (void *) NULL)
185 registry=DestroySplayTree(registry);
186 instantiate_registry=MagickFalse;
187 RelinquishSemaphoreInfo(registry_semaphore);
188 DestroySemaphoreInfo(®istry_semaphore);
192 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
196 % G e t I m a g e R e g i s t r y %
200 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
202 % GetImageRegistry() returns a value associated with an image registry key.
204 % The format of the GetImageRegistry method is:
206 % void *GetImageRegistry(const RegistryType type,const char *key,
207 % ExceptionInfo *exception)
209 % A description of each parameter follows:
215 % o exception: the exception.
218 MagickExport void *GetImageRegistry(const RegistryType type,const char *key,
219 ExceptionInfo *exception)
227 if (IsEventLogging() != MagickFalse)
228 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",key);
229 if (registry == (void *) NULL)
230 return((void *) NULL);
231 registry_info=(RegistryInfo *) GetValueFromSplayTree(registry,key);
232 if (registry_info == (void *) NULL)
234 (void) ThrowMagickException(exception,GetMagickModule(),RegistryError,
235 "UnableToGetRegistryID","`%s'",key);
236 return((void *) NULL);
241 case ImageRegistryType:
243 if (type == registry_info->type)
244 value=(void *) CloneImageList((Image *) registry_info->value,exception);
247 case ImageInfoRegistryType:
249 if (type == registry_info->type)
250 value=(void *) CloneImageInfo((ImageInfo *) registry_info->value);
253 case StringRegistryType:
255 switch (registry_info->type)
257 case ImageRegistryType:
259 value=(Image *) ConstantString(((Image *)
260 registry_info->value)->filename);
263 case ImageInfoRegistryType:
265 value=(Image *) ConstantString(((ImageInfo *)
266 registry_info->value)->filename);
269 case StringRegistryType:
271 value=(void *) ConstantString((char *) registry_info->value);
286 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
290 % G e t N e x t I m a g e R e g i s t r y %
294 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
296 % GetNextImageRegistry() gets the next image registry value.
298 % The format of the GetNextImageRegistry method is:
300 % char *GetNextImageRegistry(void)
303 MagickExport char *GetNextImageRegistry(void)
305 if (IsEventLogging() != MagickFalse)
306 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
307 if (registry == (void *) NULL)
308 return((char *) NULL);
309 return((char *) GetNextKeyInSplayTree(registry));
313 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
317 + I n s t a n t i a t e R e g i s t r y C o m p o n e n t %
321 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
323 % InstantiateRegistryComponent() instantiates the registry component.
325 % The format of the InstantiateRegistryComponent method is:
327 % MagickBooleanType InstantiateRegistryComponent(void)
330 MagickExport MagickBooleanType InstantiateRegistryComponent(void)
332 AcquireSemaphoreInfo(®istry_semaphore);
333 RelinquishSemaphoreInfo(registry_semaphore);
338 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
342 % R e m o v e I m a g e R e g i s t r y %
346 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
348 % RemoveImageRegistry() removes a key from the image registry and returns its
351 % The format of the RemoveImageRegistry method is:
353 % void *RemoveImageRegistry(const char *key)
355 % A description of each parameter follows:
357 % o key: the registry.
360 MagickExport void *RemoveImageRegistry(const char *key)
362 if (IsEventLogging() != MagickFalse)
363 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",key);
364 if (registry == (void *) NULL)
365 return((void *) NULL);
366 return(RemoveNodeFromSplayTree(registry,key));
370 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
374 % 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 %
378 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
380 % ResetImageRegistryIterator() resets the registry iterator. Use it in
381 % conjunction with GetNextImageRegistry() to iterate over all the values
382 % in the image registry.
384 % The format of the ResetImageRegistryIterator method is:
386 % ResetImageRegistryIterator(void)
389 MagickExport void ResetImageRegistryIterator(void)
391 if (IsEventLogging() != MagickFalse)
392 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
393 if (registry == (void *) NULL)
395 ResetSplayTreeIterator(registry);
399 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
403 % S e t I m a g e R e g i s t r y %
407 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
409 % SetImageRegistry() associates a value with an image registry key.
411 % The format of the SetImageRegistry method is:
413 % MagickBooleanType SetImageRegistry(const RegistryType type,
414 % const char *key,const void *value,ExceptionInfo *exception)
416 % A description of each parameter follows:
422 % o value: the value.
424 % o exception: the exception.
428 static void *DestroyRegistryNode(void *registry_info)
430 register RegistryInfo
433 p=(RegistryInfo *) registry_info;
436 case StringRegistryType:
439 p->value=RelinquishMagickMemory(p->value);
442 case ImageRegistryType:
444 p->value=(void *) DestroyImageList((Image *) p->value);
447 case ImageInfoRegistryType:
449 p->value=(void *) DestroyImageInfo((ImageInfo *) p->value);
453 return(RelinquishMagickMemory(p));
456 MagickExport MagickBooleanType SetImageRegistry(const RegistryType type,
457 const char *key,const void *value,ExceptionInfo *exception)
468 if (IsEventLogging() != MagickFalse)
469 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",key);
470 clone_value=(void *) NULL;
473 case StringRegistryType:
479 string=(const char *) value;
480 clone_value=(void *) ConstantString(string);
483 case ImageRegistryType:
488 image=(const Image *) value;
489 if (image->signature != MagickSignature)
491 (void) ThrowMagickException(exception,GetMagickModule(),RegistryError,
492 "UnableToSetRegistry","%s",key);
495 clone_value=(void *) CloneImageList(image,exception);
498 case ImageInfoRegistryType:
503 image_info=(const ImageInfo *) value;
504 if (image_info->signature != MagickSignature)
506 (void) ThrowMagickException(exception,GetMagickModule(),RegistryError,
507 "UnableToSetRegistry","%s",key);
510 clone_value=(void *) CloneImageInfo(image_info);
514 if (clone_value == (void *) NULL)
516 registry_info=(RegistryInfo *) AcquireMagickMemory(sizeof(*registry_info));
517 if (registry_info == (RegistryInfo *) NULL)
518 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
519 (void) ResetMagickMemory(registry_info,0,sizeof(*registry_info));
520 registry_info->type=type;
521 registry_info->value=clone_value;
522 registry_info->signature=MagickSignature;
523 if ((registry == (SplayTreeInfo *) NULL) &&
524 (instantiate_registry == MagickFalse))
526 AcquireSemaphoreInfo(®istry_semaphore);
527 if ((registry == (SplayTreeInfo *) NULL) &&
528 (instantiate_registry == MagickFalse))
530 registry=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
531 DestroyRegistryNode);
532 instantiate_registry=MagickTrue;
534 RelinquishSemaphoreInfo(registry_semaphore);
536 status=AddValueToSplayTree(registry,ConstantString(key),registry_info);