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 % G e t I m a g e R e g i s t r y %
170 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
172 % GetImageRegistry() returns a value associated with an image registry key.
174 % The format of the GetImageRegistry method is:
176 % void *GetImageRegistry(const RegistryType type,const char *key,
177 % ExceptionInfo *exception)
179 % A description of each parameter follows:
185 % o exception: the exception.
188 MagickExport void *GetImageRegistry(const RegistryType type,const char *key,
189 ExceptionInfo *exception)
197 if (IsEventLogging() != MagickFalse)
198 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",key);
199 if (registry == (void *) NULL)
200 return((void *) NULL);
201 registry_info=(RegistryInfo *) GetValueFromSplayTree(registry,key);
202 if (registry_info == (void *) NULL)
204 (void) ThrowMagickException(exception,GetMagickModule(),RegistryError,
205 "UnableToGetRegistryID","`%s'",key);
206 return((void *) NULL);
211 case ImageRegistryType:
213 if (type == registry_info->type)
214 value=(void *) CloneImageList((Image *) registry_info->value,exception);
217 case ImageInfoRegistryType:
219 if (type == registry_info->type)
220 value=(void *) CloneImageInfo((ImageInfo *) registry_info->value);
223 case StringRegistryType:
225 switch (registry_info->type)
227 case ImageRegistryType:
229 value=(Image *) ConstantString(((Image *)
230 registry_info->value)->filename);
233 case ImageInfoRegistryType:
235 value=(Image *) ConstantString(((ImageInfo *)
236 registry_info->value)->filename);
239 case StringRegistryType:
241 value=(void *) ConstantString((char *) registry_info->value);
256 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
260 % G e t N e x t I m a g e R e g i s t r y %
264 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
266 % GetNextImageRegistry() gets the next image registry value.
268 % The format of the GetNextImageRegistry method is:
270 % char *GetNextImageRegistry(void)
273 MagickExport char *GetNextImageRegistry(void)
275 if (IsEventLogging() != MagickFalse)
276 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
277 if (registry == (void *) NULL)
278 return((char *) NULL);
279 return((char *) GetNextKeyInSplayTree(registry));
283 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
287 + R e g i s t r y C o m p o n e n t G e n e s i s %
291 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
293 % RegistryComponentGenesis() instantiates the registry component.
295 % The format of the RegistryComponentGenesis method is:
297 % MagickBooleanType RegistryComponentGenesis(void)
300 MagickExport MagickBooleanType RegistryComponentGenesis(void)
302 assert(registry_semaphore == (SemaphoreInfo *) NULL);
303 registry_semaphore=AllocateSemaphoreInfo();
308 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
312 % 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 %
316 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
318 % RegistryComponentTerminus() destroys the registry component.
320 % The format of the DestroyDefines method is:
322 % void RegistryComponentTerminus(void)
325 MagickExport void RegistryComponentTerminus(void)
327 if (registry_semaphore == (SemaphoreInfo *) NULL)
328 AcquireSemaphoreInfo(®istry_semaphore);
329 (void) LockSemaphoreInfo(registry_semaphore);
330 if (IsEventLogging() != MagickFalse)
331 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
332 if (registry != (void *) NULL)
333 registry=DestroySplayTree(registry);
334 instantiate_registry=MagickFalse;
335 (void) UnlockSemaphoreInfo(registry_semaphore);
336 DestroySemaphoreInfo(®istry_semaphore);
340 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
344 % R e m o v e I m a g e R e g i s t r y %
348 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
350 % RemoveImageRegistry() removes a key from the image registry and returns its
353 % The format of the RemoveImageRegistry method is:
355 % void *RemoveImageRegistry(const char *key)
357 % A description of each parameter follows:
359 % o key: the registry.
362 MagickExport void *RemoveImageRegistry(const char *key)
364 if (IsEventLogging() != MagickFalse)
365 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",key);
366 if (registry == (void *) NULL)
367 return((void *) NULL);
368 return(RemoveNodeFromSplayTree(registry,key));
372 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
376 % 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 %
380 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
382 % ResetImageRegistryIterator() resets the registry iterator. Use it in
383 % conjunction with GetNextImageRegistry() to iterate over all the values
384 % in the image registry.
386 % The format of the ResetImageRegistryIterator method is:
388 % ResetImageRegistryIterator(void)
391 MagickExport void ResetImageRegistryIterator(void)
393 if (IsEventLogging() != MagickFalse)
394 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
395 if (registry == (void *) NULL)
397 ResetSplayTreeIterator(registry);
401 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
405 % S e t I m a g e R e g i s t r y %
409 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
411 % SetImageRegistry() associates a value with an image registry key.
413 % The format of the SetImageRegistry method is:
415 % MagickBooleanType SetImageRegistry(const RegistryType type,
416 % const char *key,const void *value,ExceptionInfo *exception)
418 % A description of each parameter follows:
424 % o value: the value.
426 % o exception: the exception.
430 static void *DestroyRegistryNode(void *registry_info)
432 register RegistryInfo
435 p=(RegistryInfo *) registry_info;
438 case StringRegistryType:
441 p->value=RelinquishMagickMemory(p->value);
444 case ImageRegistryType:
446 p->value=(void *) DestroyImageList((Image *) p->value);
449 case ImageInfoRegistryType:
451 p->value=(void *) DestroyImageInfo((ImageInfo *) p->value);
455 return(RelinquishMagickMemory(p));
458 MagickExport MagickBooleanType SetImageRegistry(const RegistryType type,
459 const char *key,const void *value,ExceptionInfo *exception)
470 if (IsEventLogging() != MagickFalse)
471 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",key);
472 clone_value=(void *) NULL;
475 case StringRegistryType:
481 string=(const char *) value;
482 clone_value=(void *) ConstantString(string);
485 case ImageRegistryType:
490 image=(const Image *) value;
491 if (image->signature != MagickSignature)
493 (void) ThrowMagickException(exception,GetMagickModule(),RegistryError,
494 "UnableToSetRegistry","%s",key);
497 clone_value=(void *) CloneImageList(image,exception);
500 case ImageInfoRegistryType:
505 image_info=(const ImageInfo *) value;
506 if (image_info->signature != MagickSignature)
508 (void) ThrowMagickException(exception,GetMagickModule(),RegistryError,
509 "UnableToSetRegistry","%s",key);
512 clone_value=(void *) CloneImageInfo(image_info);
516 if (clone_value == (void *) NULL)
518 registry_info=(RegistryInfo *) AcquireMagickMemory(sizeof(*registry_info));
519 if (registry_info == (RegistryInfo *) NULL)
520 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
521 (void) ResetMagickMemory(registry_info,0,sizeof(*registry_info));
522 registry_info->type=type;
523 registry_info->value=clone_value;
524 registry_info->signature=MagickSignature;
525 if ((registry == (SplayTreeInfo *) NULL) &&
526 (instantiate_registry == MagickFalse))
528 if (registry_semaphore == (SemaphoreInfo *) NULL)
529 AcquireSemaphoreInfo(®istry_semaphore);
530 (void) LockSemaphoreInfo(registry_semaphore);
531 if ((registry == (SplayTreeInfo *) NULL) &&
532 (instantiate_registry == MagickFalse))
534 registry=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
535 DestroyRegistryNode);
536 instantiate_registry=MagickTrue;
538 (void) UnlockSemaphoreInfo(registry_semaphore);
540 status=AddValueToSplayTree(registry,ConstantString(key),registry_info);