]> granicus.if.org Git - imagemagick/blob - magick/registry.c
(no commit message)
[imagemagick] / magick / registry.c
1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
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            %
11 %                                                                             %
12 %                                                                             %
13 %                       MagickCore Registry Methods                           %
14 %                                                                             %
15 %                              Software Design                                %
16 %                                John Cristy                                  %
17 %                                 March 2000                                  %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2009 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 %    http://www.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 \f
40 /*
41   Include declarations.
42 */
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"
53 \f
54 /*
55   Typedef declarations.
56 */
57 typedef struct _RegistryInfo
58 {
59   RegistryType
60     type;
61
62   void
63     *value;
64
65   unsigned long
66     signature;
67 } RegistryInfo;
68 \f
69 /*
70   Static declarations.
71 */
72 static SplayTreeInfo
73   *registry = (SplayTreeInfo *) NULL;
74 \f
75 /*
76 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
77 %                                                                             %
78 %                                                                             %
79 %                                                                             %
80 %   D e f i n e I m a g e R e g i s t r y                                     %
81 %                                                                             %
82 %                                                                             %
83 %                                                                             %
84 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
85 %
86 %  DefineImageRegistry() associates a key/value pair with the image registry.
87 %
88 %  The format of the DefineImageRegistry method is:
89 %
90 %      MagickBooleanType DefineImageRegistry(const RegistryType type,
91 %        const char *option,ExceptionInfo *exception)
92 %
93 %  A description of each parameter follows:
94 %
95 %    o type: the type.
96 %
97 %    o option: the option.
98 %
99 %    o exception: the exception.
100 %
101 */
102 MagickExport MagickBooleanType DefineImageRegistry(const RegistryType type,
103   const char *option,ExceptionInfo *exception)
104 {
105   char
106     key[MaxTextExtent],
107     value[MaxTextExtent];
108
109   register char
110     *p;
111
112   assert(option != (const char *) NULL);
113   (void) CopyMagickString(key,option,MaxTextExtent);
114   for (p=key; *p != '\0'; p++)
115     if (*p == '=')
116       break;
117   *value='\0';
118   if (*p == '=')
119     (void) CopyMagickString(value,p+1,MaxTextExtent);
120   *p='\0';
121   return(SetImageRegistry(type,key,value,exception));
122 }
123 \f
124 /*
125 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
126 %                                                                             %
127 %                                                                             %
128 %                                                                             %
129 %   D e l e t e I m a g e R e g i s t r y                                     %
130 %                                                                             %
131 %                                                                             %
132 %                                                                             %
133 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
134 %
135 %  DeleteImageRegistry() deletes a key from the image registry.
136 %
137 %  The format of the DeleteImageRegistry method is:
138 %
139 %      MagickBooleanType DeleteImageRegistry(const char *key)
140 %
141 %  A description of each parameter follows:
142 %
143 %    o key: the registry.
144 %
145 */
146 MagickExport MagickBooleanType DeleteImageRegistry(const char *key)
147 {
148   if (IsEventLogging() != MagickFalse)
149     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",key);
150   if (registry == (void *) NULL)
151     return(MagickFalse);
152   return(DeleteNodeFromSplayTree(registry,key));
153 }
154 \f
155 /*
156 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
157 %                                                                             %
158 %                                                                             %
159 %                                                                             %
160 %   D e s t r o y I m a g e R e g i s t r y                                   %
161 %                                                                             %
162 %                                                                             %
163 %                                                                             %
164 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
165 %
166 %  DestroyImageRegistry() releases memory associated with the image registry.
167 %
168 %  The format of the DestroyDefines method is:
169 %
170 %      void DestroyImageRegistry(void)
171 %
172 */
173 MagickExport void DestroyImageRegistry(void)
174 {
175   if (IsEventLogging() != MagickFalse)
176     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
177   if (registry != (void *) NULL)
178     registry=DestroySplayTree(registry);
179 }
180 \f
181 /*
182 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
183 %                                                                             %
184 %                                                                             %
185 %                                                                             %
186 %   G e t I m a g e R e g i s t r y                                           %
187 %                                                                             %
188 %                                                                             %
189 %                                                                             %
190 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
191 %
192 %  GetImageRegistry() returns a value associated with an image registry key.
193 %
194 %  The format of the GetImageRegistry method is:
195 %
196 %      void *GetImageRegistry(const RegistryType type,const char *key,
197 %        ExceptionInfo *exception)
198 %
199 %  A description of each parameter follows:
200 %
201 %    o type: the type.
202 %
203 %    o key: the key.
204 %
205 %    o exception: the exception.
206 %
207 */
208 MagickExport void *GetImageRegistry(const RegistryType type,const char *key,
209   ExceptionInfo *exception)
210 {
211   void
212     *value;
213
214   RegistryInfo
215     *registry_info;
216
217   if (IsEventLogging() != MagickFalse)
218     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",key);
219   if (registry == (void *) NULL)
220     return((void *) NULL);
221   registry_info=(RegistryInfo *) GetValueFromSplayTree(registry,key);
222   if (registry_info == (void *) NULL)
223     {
224       (void) ThrowMagickException(exception,GetMagickModule(),RegistryError,
225         "UnableToGetRegistryID","`%s'",key);
226       return((void *) NULL);
227     }
228   value=(void *) NULL;
229   switch (type)
230   {
231     case ImageRegistryType:
232     {
233       if (type == registry_info->type)
234         value=(void *) CloneImageList((Image *) registry_info->value,exception);
235       break;
236     }
237     case ImageInfoRegistryType:
238     {
239       if (type == registry_info->type)
240         value=(void *) CloneImageInfo((ImageInfo *) registry_info->value);
241       break;
242     }
243     case StringRegistryType:
244     {
245       switch (registry_info->type)
246       {
247         case ImageRegistryType:
248         {
249           value=(Image *) ConstantString(((Image *)
250             registry_info->value)->filename);
251           break;
252         }
253         case ImageInfoRegistryType:
254         {
255           value=(Image *) ConstantString(((ImageInfo *)
256             registry_info->value)->filename);
257           break;
258         }
259         case StringRegistryType:
260         {
261           value=(void *) ConstantString((char *) registry_info->value);
262           break;
263         }
264         default:
265           break;
266       }
267       break;
268     }
269     default:
270       break;
271   }
272   return(value);
273 }
274 \f
275 /*
276 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
277 %                                                                             %
278 %                                                                             %
279 %                                                                             %
280 %   G e t N e x t I m a g e R e g i s t r y                                   %
281 %                                                                             %
282 %                                                                             %
283 %                                                                             %
284 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
285 %
286 %  GetNextImageRegistry() gets the next image registry value.
287 %
288 %  The format of the GetNextImageRegistry method is:
289 %
290 %      char *GetNextImageRegistry(void)
291 %
292 */
293 MagickExport char *GetNextImageRegistry(void)
294 {
295   if (IsEventLogging() != MagickFalse)
296     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
297   if (registry == (void *) NULL)
298     return((char *) NULL);
299   return((char *) GetNextKeyInSplayTree(registry));
300 }
301 \f
302 /*
303 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
304 %                                                                             %
305 %                                                                             %
306 %                                                                             %
307 %   R e m o v e I m a g e R e g i s t r y                                     %
308 %                                                                             %
309 %                                                                             %
310 %                                                                             %
311 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
312 %
313 %  RemoveImageRegistry() removes a key from the image registry and returns its
314 %  value.
315 %
316 %  The format of the RemoveImageRegistry method is:
317 %
318 %      void *RemoveImageRegistry(const char *key)
319 %
320 %  A description of each parameter follows:
321 %
322 %    o key: the registry.
323 %
324 */
325 MagickExport void *RemoveImageRegistry(const char *key)
326 {
327   if (IsEventLogging() != MagickFalse)
328     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",key);
329   if (registry == (void *) NULL)
330     return((void *) NULL);
331   return(RemoveNodeFromSplayTree(registry,key));
332 }
333 \f
334 /*
335 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
336 %                                                                             %
337 %                                                                             %
338 %                                                                             %
339 %   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                       %
340 %                                                                             %
341 %                                                                             %
342 %                                                                             %
343 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
344 %
345 %  ResetImageRegistryIterator() resets the registry iterator.  Use it in
346 %  conjunction with GetNextImageRegistry() to iterate over all the values
347 %  in the image registry.
348 %
349 %  The format of the ResetImageRegistryIterator method is:
350 %
351 %      ResetImageRegistryIterator(void)
352 %
353 */
354 MagickExport void ResetImageRegistryIterator(void)
355 {
356   if (IsEventLogging() != MagickFalse)
357     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
358   if (registry == (void *) NULL)
359     return;
360   ResetSplayTreeIterator(registry);
361 }
362 \f
363 /*
364 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
365 %                                                                             %
366 %                                                                             %
367 %                                                                             %
368 %   S e t I m a g e R e g i s t r y                                           %
369 %                                                                             %
370 %                                                                             %
371 %                                                                             %
372 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
373 %
374 %  SetImageRegistry() associates a value with an image registry key.
375 %
376 %  The format of the SetImageRegistry method is:
377 %
378 %      MagickBooleanType SetImageRegistry(const RegistryType type,
379 %        const char *key,const void *value,ExceptionInfo *exception)
380 %
381 %  A description of each parameter follows:
382 %
383 %    o type: the type.
384 %
385 %    o key: the key.
386 %
387 %    o value: the value.
388 %
389 %    o exception: the exception.
390 %
391 */
392
393 static void *DestroyRegistryNode(void *registry_info)
394 {
395   register RegistryInfo
396     *p;
397        
398   p=(RegistryInfo *) registry_info;
399   switch (p->type)
400   {
401     case StringRegistryType:
402     default:
403     {
404       p->value=RelinquishMagickMemory(p->value);
405       break;
406     }
407     case ImageRegistryType:
408     {
409       p->value=(void *) DestroyImageList((Image *) p->value);
410       break;
411     }
412     case ImageInfoRegistryType:
413     {
414       p->value=(void *) DestroyImageInfo((ImageInfo *) p->value);
415       break;
416     }
417   } 
418   return(RelinquishMagickMemory(p));
419 }
420
421 MagickExport MagickBooleanType SetImageRegistry(const RegistryType type,
422   const char *key,const void *value,ExceptionInfo *exception)
423 {
424   MagickBooleanType
425     status;
426
427   RegistryInfo
428     *registry_info;
429
430   void
431     *clone_value;
432
433   if (IsEventLogging() != MagickFalse)
434     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",key);
435   clone_value=(void *) NULL;
436   switch (type)
437   {
438     case StringRegistryType:
439     default:
440     {
441       const char
442         *string;
443
444       string=(const char *) value;
445       clone_value=(void *) ConstantString(string);
446       break;
447     }
448     case ImageRegistryType:
449     {
450       const Image
451         *image;
452
453       image=(const Image *) value;
454       if (image->signature != MagickSignature)
455         {
456           (void) ThrowMagickException(exception,GetMagickModule(),RegistryError,
457             "UnableToSetRegistry","%s",key);
458           return(MagickFalse);
459         }
460       clone_value=(void *) CloneImageList(image,exception);
461       break;
462     }
463     case ImageInfoRegistryType:
464     {
465       const ImageInfo
466         *image_info;
467
468       image_info=(const ImageInfo *) value;
469       if (image_info->signature != MagickSignature)
470         {
471           (void) ThrowMagickException(exception,GetMagickModule(),RegistryError,
472             "UnableToSetRegistry","%s",key);
473           return(MagickFalse);
474         }
475       clone_value=(void *) CloneImageInfo(image_info);
476       break;
477     }
478   }
479   if (clone_value == (void *) NULL)
480     return(MagickFalse);
481   registry_info=(RegistryInfo *) AcquireMagickMemory(sizeof(*registry_info));
482   if (registry_info == (RegistryInfo *) NULL)
483     ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
484   (void) ResetMagickMemory(registry_info,0,sizeof(*registry_info));
485   registry_info->type=type;
486   registry_info->value=clone_value;
487   registry_info->signature=MagickSignature;
488   if (registry == (void *) NULL)
489     registry=NewSplayTree(CompareSplayTreeString,RelinquishMagickMemory,
490       DestroyRegistryNode);
491   status=AddValueToSplayTree(registry,ConstantString(key),registry_info);
492   return(status);
493 }