blob: a12aece9703f8d9b8073ca5fe27638b39e6eb85b [file] [log] [blame]
cristy3ed852e2009-09-05 21:47:34 +00001/*
2%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3% %
4% %
5% %
cristy670aa3c2011-11-03 00:54:00 +00006% SSSSS TTTTT RRRR IIIII N N GGGG %
7% SS T R R I NN N G %
8% SSS T RRRR I N N N G GGG %
9% SS T R R I N NN G G %
10% SSSSS T R R IIIII N N GGGG %
cristy3ed852e2009-09-05 21:47:34 +000011% %
12% %
cristy670aa3c2011-11-03 00:54:00 +000013% MagickCore String Methods %
cristy3ed852e2009-09-05 21:47:34 +000014% %
cristy670aa3c2011-11-03 00:54:00 +000015% Software Design %
16% John Cristy %
17% August 2003 %
cristy3ed852e2009-09-05 21:47:34 +000018% %
19% %
cristy45ef08f2012-12-07 13:13:34 +000020% Copyright 1999-2013 ImageMagick Studio LLC, a non-profit organization %
cristy3ed852e2009-09-05 21:47:34 +000021% dedicated to making software imaging solutions freely available. %
22% %
cristy670aa3c2011-11-03 00:54:00 +000023% You may not use this file except in compliance with the license. You may %
cristy99bbf2c2011-09-26 18:27:50 +000024% obtain a copy of the license at %
cristy3ed852e2009-09-05 21:47:34 +000025% %
26% http://www.imagemagick.org/script/license.php %
27% %
cristy99bbf2c2011-09-26 18:27:50 +000028% 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. %
cristy670aa3c2011-11-03 00:54:00 +000031% See the license for the specific language governing permissions and %
cristy99bbf2c2011-09-26 18:27:50 +000032% limitations under the license. %
cristy3ed852e2009-09-05 21:47:34 +000033% %
34%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35%
36%
37*/
38
39/*
cristy99bbf2c2011-09-26 18:27:50 +000040 include declarations.
cristy3ed852e2009-09-05 21:47:34 +000041*/
cristy4c08aed2011-07-01 19:47:50 +000042#include "MagickCore/studio.h"
43#include "MagickCore/blob.h"
44#include "MagickCore/blob-private.h"
45#include "MagickCore/exception.h"
46#include "MagickCore/exception-private.h"
47#include "MagickCore/list.h"
48#include "MagickCore/locale_.h"
49#include "MagickCore/log.h"
50#include "MagickCore/memory_.h"
cristyd2d11ec2012-03-28 13:53:49 +000051#include "MagickCore/nt-base-private.h"
cristy4c08aed2011-07-01 19:47:50 +000052#include "MagickCore/property.h"
53#include "MagickCore/resource_.h"
54#include "MagickCore/signature-private.h"
55#include "MagickCore/string_.h"
anthonyb1d483a2012-04-14 12:53:56 +000056#include "MagickCore/string-private.h"
cristy18c6c272011-09-23 14:40:37 +000057#include "MagickCore/utility-private.h"
cristy3ed852e2009-09-05 21:47:34 +000058
59/*
cristy99bbf2c2011-09-26 18:27:50 +000060 static declarations.
cristy3ed852e2009-09-05 21:47:34 +000061*/
62#if !defined(MAGICKCORE_HAVE_STRCASECMP) || !defined(MAGICKCORE_HAVE_STRNCASECMP)
63static const unsigned char
cristy99bbf2c2011-09-26 18:27:50 +000064 asciimap[] =
cristy3ed852e2009-09-05 21:47:34 +000065 {
66 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
67 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
68 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
69 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
70 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
71 0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
72 0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73,
73 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
74 0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b,
75 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
76 0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83,
77 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
78 0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b,
79 0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
80 0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3,
81 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
82 0xc0, 0xe1, 0xe2, 0xe3, 0xe4, 0xc5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb,
83 0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
84 0xf8, 0xf9, 0xfa, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3,
85 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
86 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb,
87 0xfc, 0xfd, 0xfe, 0xff,
88 };
89#endif
90
91/*
92%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
93% %
94% %
95% %
96% A c q u i r e S t r i n g %
97% %
98% %
99% %
100%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
101%
anthony104f8932012-05-13 01:54:53 +0000102% AcquireString() returns an new extented string, containing a clone of the
103% given string.
cristy3ed852e2009-09-05 21:47:34 +0000104%
anthony104f8932012-05-13 01:54:53 +0000105% An extended string is the string length, plus an extra MaxTextExtent space
106% to allow for the string to be activally worked on.
107%
108% The returned string shoud be freed using DestoryString().
anthonyb1d483a2012-04-14 12:53:56 +0000109%
cristy3ed852e2009-09-05 21:47:34 +0000110% The format of the AcquireString method is:
111%
112% char *AcquireString(const char *source)
113%
114% A description of each parameter follows:
115%
116% o source: A character string.
117%
118*/
119MagickExport char *AcquireString(const char *source)
120{
121 char
122 *destination;
123
124 size_t
125 length;
126
127 length=0;
128 if (source != (char *) NULL)
129 length+=strlen(source);
cristy54aad5e2010-09-03 16:02:04 +0000130 if (~length < MaxTextExtent)
131 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
132 destination=(char *) AcquireQuantumMemory(length+MaxTextExtent,
133 sizeof(*destination));
cristy3ed852e2009-09-05 21:47:34 +0000134 if (destination == (char *) NULL)
135 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
136 *destination='\0';
137 if (source != (char *) NULL)
cristy54aad5e2010-09-03 16:02:04 +0000138 (void) memcpy(destination,source,length*sizeof(*destination));
139 destination[length]='\0';
cristy3ed852e2009-09-05 21:47:34 +0000140 return(destination);
141}
142
143/*
144%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
145% %
146% %
147% %
148% A c q u i r e S t r i n g I n f o %
149% %
150% %
151% %
152%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
153%
154% AcquireStringInfo() allocates the StringInfo structure.
155%
156% The format of the AcquireStringInfo method is:
157%
158% StringInfo *AcquireStringInfo(const size_t length)
159%
160% A description of each parameter follows:
161%
162% o length: the string length.
163%
164*/
165MagickExport StringInfo *AcquireStringInfo(const size_t length)
166{
167 StringInfo
168 *string_info;
169
cristy73bd4a52010-10-05 11:24:23 +0000170 string_info=(StringInfo *) AcquireMagickMemory(sizeof(*string_info));
cristy3ed852e2009-09-05 21:47:34 +0000171 if (string_info == (StringInfo *) NULL)
172 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
173 (void) ResetMagickMemory(string_info,0,sizeof(*string_info));
174 string_info->signature=MagickSignature;
175 string_info->length=length;
176 if (string_info->length != 0)
177 {
178 string_info->datum=(unsigned char *) NULL;
cristy37e0b382011-06-07 13:31:21 +0000179 if (~string_info->length >= (MaxTextExtent-1))
cristy3ed852e2009-09-05 21:47:34 +0000180 string_info->datum=(unsigned char *) AcquireQuantumMemory(
181 string_info->length+MaxTextExtent,sizeof(*string_info->datum));
182 if (string_info->datum == (unsigned char *) NULL)
183 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
184 }
185 return(string_info);
186}
187
188/*
189%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
190% %
191% %
192% %
cristy8723e4b2011-09-01 13:11:19 +0000193% B l o b T o S t r i n g I n f o %
194% %
195% %
196% %
197%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
198%
anthony104f8932012-05-13 01:54:53 +0000199% BlobToStringInfo() returns the contents of a blob as a StringInfo structure
200% with MaxTextExtent extra space.
cristy8723e4b2011-09-01 13:11:19 +0000201%
202% The format of the BlobToStringInfo method is:
203%
204% StringInfo *BlobToStringInfo(const void *blob,const size_t length)
205%
206% A description of each parameter follows:
207%
208% o blob: the blob.
209%
210% o length: the length of the blob.
211%
212*/
213MagickExport StringInfo *BlobToStringInfo(const void *blob,const size_t length)
214{
215 StringInfo
216 *string_info;
217
218 string_info=AcquireStringInfo(0);
cristyf8316342011-09-01 13:55:00 +0000219 string_info->length=length;
cristya10e0742011-09-01 13:50:43 +0000220 if (~string_info->length >= (MaxTextExtent-1))
cristyf8316342011-09-01 13:55:00 +0000221 string_info->datum=(unsigned char *) AcquireQuantumMemory(
222 string_info->length+MaxTextExtent,sizeof(*string_info->datum));
cristy8723e4b2011-09-01 13:11:19 +0000223 if (string_info->datum == (unsigned char *) NULL)
224 {
225 string_info=DestroyStringInfo(string_info);
226 return((StringInfo *) NULL);
227 }
228 if (blob != (const void *) NULL)
229 (void) memcpy(string_info->datum,blob,length);
230 return(string_info);
231}
232
233/*
234%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
235% %
236% %
237% %
cristy3ed852e2009-09-05 21:47:34 +0000238% C l o n e S t r i n g %
239% %
240% %
241% %
242%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
243%
anthony104f8932012-05-13 01:54:53 +0000244% CloneString() replaces or frees the destination string to make it
245% a clone of the input string plus MaxTextExtent more space so the string may
246% be worked on on.
cristy3ed852e2009-09-05 21:47:34 +0000247%
anthony104f8932012-05-13 01:54:53 +0000248% If source is a NULL pointer the destination string will be freed and set to
249% a NULL pointer. A pointer to the stored in the destination is also returned.
anthony72feaa62012-01-17 06:46:23 +0000250%
anthony104f8932012-05-13 01:54:53 +0000251% When finished the non-NULL string should be freed using DestoryString()
252% or using CloneString() with a NULL pointed for the source.
anthony06762232012-04-29 11:45:40 +0000253%
cristy3ed852e2009-09-05 21:47:34 +0000254% The format of the CloneString method is:
255%
256% char *CloneString(char **destination,const char *source)
257%
258% A description of each parameter follows:
259%
260% o destination: A pointer to a character string.
261%
262% o source: A character string.
263%
264*/
265MagickExport char *CloneString(char **destination,const char *source)
266{
267 size_t
268 length;
269
270 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
271 assert(destination != (char **) NULL);
272 if (source == (const char *) NULL)
273 {
274 if (*destination != (char *) NULL)
275 *destination=DestroyString(*destination);
276 return(*destination);
277 }
278 if (*destination == (char *) NULL)
279 {
280 *destination=AcquireString(source);
281 return(*destination);
282 }
283 length=strlen(source);
284 if (~length < MaxTextExtent)
285 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
286 *destination=(char *) ResizeQuantumMemory(*destination,length+MaxTextExtent,
cristyb936f702011-03-25 15:33:43 +0000287 sizeof(**destination));
cristy3ed852e2009-09-05 21:47:34 +0000288 if (*destination == (char *) NULL)
289 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
cristy54aad5e2010-09-03 16:02:04 +0000290 if (length != 0)
cristyf7e6ab42011-03-25 12:32:09 +0000291 (void) memcpy(*destination,source,length*sizeof(**destination));
cristy208cacf2010-09-03 02:24:42 +0000292 (*destination)[length]='\0';
cristy3ed852e2009-09-05 21:47:34 +0000293 return(*destination);
294}
295
296/*
297%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
298% %
299% %
300% %
301% C l o n e S t r i n g I n f o %
302% %
303% %
304% %
305%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
306%
307% CloneStringInfo() clones a copy of the StringInfo structure.
308%
309% The format of the CloneStringInfo method is:
310%
311% StringInfo *CloneStringInfo(const StringInfo *string_info)
312%
313% A description of each parameter follows:
314%
315% o string_info: the string info.
316%
317*/
318MagickExport StringInfo *CloneStringInfo(const StringInfo *string_info)
319{
320 StringInfo
321 *clone_info;
322
323 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
324 assert(string_info != (StringInfo *) NULL);
325 assert(string_info->signature == MagickSignature);
326 clone_info=AcquireStringInfo(string_info->length);
327 if (string_info->length != 0)
cristy54aad5e2010-09-03 16:02:04 +0000328 (void) memcpy(clone_info->datum,string_info->datum,string_info->length+1);
cristy3ed852e2009-09-05 21:47:34 +0000329 return(clone_info);
330}
331
332/*
333%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
334% %
335% %
336% %
337% C o m p a r e S t r i n g I n f o %
338% %
339% %
340% %
341%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
342%
343% CompareStringInfo() compares the two datums target and source. It returns
344% an integer less than, equal to, or greater than zero if target is found,
345% respectively, to be less than, to match, or be greater than source.
346%
347% The format of the CompareStringInfo method is:
348%
349% int CompareStringInfo(const StringInfo *target,const StringInfo *source)
350%
351% A description of each parameter follows:
352%
353% o target: the target string.
354%
355% o source: the source string.
356%
357*/
358
359static inline size_t MagickMin(const size_t x,const size_t y)
360{
361 if (x < y)
362 return(x);
363 return(y);
364}
365
366MagickExport int CompareStringInfo(const StringInfo *target,
367 const StringInfo *source)
368{
369 int
370 status;
371
372 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
373 assert(target != (StringInfo *) NULL);
374 assert(target->signature == MagickSignature);
375 assert(source != (StringInfo *) NULL);
376 assert(source->signature == MagickSignature);
377 status=memcmp(target->datum,source->datum,MagickMin(target->length,
378 source->length));
379 if (status != 0)
380 return(status);
381 if (target->length == source->length)
382 return(0);
383 return(target->length < source->length ? -1 : 1);
384}
385
386/*
387%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
388% %
389% %
390% %
391% C o n c a t e n a t e M a g i c k S t r i n g %
392% %
393% %
394% %
395%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
396%
397% ConcatenateMagickString() concatenates the source string to the destination
398% string. The destination buffer is always null-terminated even if the
399% string must be truncated.
400%
401% The format of the ConcatenateMagickString method is:
402%
403% size_t ConcatenateMagickString(char *destination,const char *source,
404% const size_t length)
405%
406% A description of each parameter follows:
407%
408% o destination: the destination string.
409%
410% o source: the source string.
411%
412% o length: the length of the destination string.
413%
414*/
415MagickExport size_t ConcatenateMagickString(char *destination,
416 const char *source,const size_t length)
417{
418 register char
419 *q;
420
421 register const char
422 *p;
423
424 register size_t
425 i;
426
427 size_t
428 count;
429
430 assert(destination != (char *) NULL);
431 assert(source != (const char *) NULL);
432 assert(length >= 1);
433 p=source;
434 q=destination;
435 i=length;
436 while ((i-- != 0) && (*q != '\0'))
437 q++;
438 count=(size_t) (q-destination);
439 i=length-count;
440 if (i == 0)
441 return(count+strlen(p));
442 while (*p != '\0')
443 {
444 if (i != 1)
445 {
446 *q++=(*p);
447 i--;
448 }
449 p++;
450 }
451 *q='\0';
452 return(count+(p-source));
453}
454
455/*
456%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
457% %
458% %
459% %
460% C o n c a t e n a t e S t r i n g %
461% %
462% %
463% %
464%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
465%
466% ConcatenateString() appends a copy of string source, including the
467% terminating null character, to the end of string destination.
468%
469% The format of the ConcatenateString method is:
470%
471% MagickBooleanType ConcatenateString(char **destination,
472% const char *source)
473%
474% A description of each parameter follows:
475%
476% o destination: A pointer to a character string.
477%
478% o source: A character string.
479%
480*/
481MagickExport MagickBooleanType ConcatenateString(char **destination,
482 const char *source)
483{
484 size_t
cristy54aad5e2010-09-03 16:02:04 +0000485 destination_length,
cristy3ed852e2009-09-05 21:47:34 +0000486 length,
487 source_length;
488
489 assert(destination != (char **) NULL);
490 if (source == (const char *) NULL)
491 return(MagickTrue);
492 if (*destination == (char *) NULL)
493 {
494 *destination=AcquireString(source);
495 return(MagickTrue);
496 }
cristy54aad5e2010-09-03 16:02:04 +0000497 destination_length=strlen(*destination);
cristy3ed852e2009-09-05 21:47:34 +0000498 source_length=strlen(source);
cristy54aad5e2010-09-03 16:02:04 +0000499 length=destination_length;
cristy3ed852e2009-09-05 21:47:34 +0000500 if (~length < source_length)
501 ThrowFatalException(ResourceLimitFatalError,"UnableToConcatenateString");
502 length+=source_length;
503 if (~length < MaxTextExtent)
504 ThrowFatalException(ResourceLimitFatalError,"UnableToConcatenateString");
505 *destination=(char *) ResizeQuantumMemory(*destination,length+MaxTextExtent,
cristycf1667c2011-03-25 15:35:41 +0000506 sizeof(**destination));
cristy3ed852e2009-09-05 21:47:34 +0000507 if (*destination == (char *) NULL)
508 ThrowFatalException(ResourceLimitFatalError,"UnableToConcatenateString");
cristy54aad5e2010-09-03 16:02:04 +0000509 if (source_length != 0)
510 (void) memcpy((*destination)+destination_length,source,source_length);
511 (*destination)[length]='\0';
cristy3ed852e2009-09-05 21:47:34 +0000512 return(MagickTrue);
513}
514
515/*
516%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
517% %
518% %
519% %
520% C o n c a t e n a t e S t r i n g I n f o %
521% %
522% %
523% %
524%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
525%
526% ConcatenateStringInfo() concatenates the source string to the destination
527% string.
528%
529% The format of the ConcatenateStringInfo method is:
530%
531% void ConcatenateStringInfo(StringInfo *string_info,
532% const StringInfo *source)
533%
534% A description of each parameter follows:
535%
536% o string_info: the string info.
537%
538% o source: the source string.
539%
540*/
541MagickExport void ConcatenateStringInfo(StringInfo *string_info,
542 const StringInfo *source)
543{
544 size_t
545 length;
546
547 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
548 assert(string_info != (StringInfo *) NULL);
549 assert(string_info->signature == MagickSignature);
550 assert(source != (const StringInfo *) NULL);
551 length=string_info->length;
552 if (~length < source->length)
553 ThrowFatalException(ResourceLimitFatalError,"UnableToConcatenateString");
554 SetStringInfoLength(string_info,length+source->length);
cristy54aad5e2010-09-03 16:02:04 +0000555 (void) memcpy(string_info->datum+length,source->datum,source->length);
cristy3ed852e2009-09-05 21:47:34 +0000556}
557
558/*
559%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
560% %
561% %
562% %
563% C o n f i g u r e F i l e T o S t r i n g I n f o %
564% %
565% %
566% %
567%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
568%
569% ConfigureFileToStringInfo() returns the contents of a configure file as a
570% string.
571%
572% The format of the ConfigureFileToStringInfo method is:
573%
574% StringInfo *ConfigureFileToStringInfo(const char *filename)
575% ExceptionInfo *exception)
576%
577% A description of each parameter follows:
578%
579% o filename: the filename.
580%
581*/
cristy99bbf2c2011-09-26 18:27:50 +0000582MagickExport StringInfo *ConfigureFileToStringInfo(const char *filename)
cristy3ed852e2009-09-05 21:47:34 +0000583{
584 char
585 *string;
586
587 int
588 file;
589
590 MagickOffsetType
591 offset;
592
593 size_t
594 length;
595
596 StringInfo
597 *string_info;
598
599 void
600 *map;
601
602 assert(filename != (const char *) NULL);
cristy18c6c272011-09-23 14:40:37 +0000603 file=open_utf8(filename,O_RDONLY | O_BINARY,0);
cristy3ed852e2009-09-05 21:47:34 +0000604 if (file == -1)
605 return((StringInfo *) NULL);
cristy7f317702011-02-18 20:40:28 +0000606 offset=(MagickOffsetType) lseek(file,0,SEEK_END);
cristy3ed852e2009-09-05 21:47:34 +0000607 if ((offset < 0) || (offset != (MagickOffsetType) ((ssize_t) offset)))
608 {
609 file=close(file)-1;
610 return((StringInfo *) NULL);
611 }
612 length=(size_t) offset;
613 string=(char *) NULL;
cristy37e0b382011-06-07 13:31:21 +0000614 if (~length >= (MaxTextExtent-1))
cristy3ed852e2009-09-05 21:47:34 +0000615 string=(char *) AcquireQuantumMemory(length+MaxTextExtent,sizeof(*string));
616 if (string == (char *) NULL)
617 {
618 file=close(file)-1;
619 return((StringInfo *) NULL);
620 }
621 map=MapBlob(file,ReadMode,0,length);
622 if (map != (void *) NULL)
623 {
cristy54aad5e2010-09-03 16:02:04 +0000624 (void) memcpy(string,map,length);
cristy3ed852e2009-09-05 21:47:34 +0000625 (void) UnmapBlob(map,length);
626 }
627 else
628 {
629 register size_t
630 i;
631
632 ssize_t
633 count;
634
cristy7f317702011-02-18 20:40:28 +0000635 (void) lseek(file,0,SEEK_SET);
cristy3ed852e2009-09-05 21:47:34 +0000636 for (i=0; i < length; i+=count)
637 {
638 count=read(file,string+i,(size_t) MagickMin(length-i,(size_t)
639 SSIZE_MAX));
640 if (count <= 0)
641 {
642 count=0;
643 if (errno != EINTR)
644 break;
645 }
646 }
647 if (i < length)
648 {
649 file=close(file)-1;
650 string=DestroyString(string);
651 return((StringInfo *) NULL);
652 }
653 }
654 string[length]='\0';
655 file=close(file)-1;
656 string_info=AcquireStringInfo(0);
657 (void) CopyMagickString(string_info->path,filename,MaxTextExtent);
658 string_info->length=length;
659 string_info->datum=(unsigned char *) string;
660 return(string_info);
661}
662
663/*
664%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
665% %
666% %
667% %
668% C o n s t a n t S t r i n g %
669% %
670% %
671% %
672%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
673%
anthony104f8932012-05-13 01:54:53 +0000674% ConstantString() allocates exactly the needed memory for a string and
675% copies the source string to that memory location. A NULL string pointer
676% will allocate an empty string containing just the NUL character.
cristy3ed852e2009-09-05 21:47:34 +0000677%
anthony104f8932012-05-13 01:54:53 +0000678% When finished the string should be freed using DestoryString()
anthony06762232012-04-29 11:45:40 +0000679%
cristy3ed852e2009-09-05 21:47:34 +0000680% The format of the ConstantString method is:
681%
682% char *ConstantString(const char *source)
683%
684% A description of each parameter follows:
685%
686% o source: A character string.
687%
688*/
689MagickExport char *ConstantString(const char *source)
690{
691 char
692 *destination;
693
694 size_t
695 length;
696
697 length=0;
698 if (source != (char *) NULL)
699 length+=strlen(source);
700 destination=(char *) NULL;
701 if (~length >= 1UL)
702 destination=(char *) AcquireQuantumMemory(length+1UL,sizeof(*destination));
703 if (destination == (char *) NULL)
704 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
705 *destination='\0';
706 if (source != (char *) NULL)
cristy54aad5e2010-09-03 16:02:04 +0000707 (void) memcpy(destination,source,length*sizeof(*destination));
708 destination[length]='\0';
cristy3ed852e2009-09-05 21:47:34 +0000709 return(destination);
710}
711
712/*
713%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
714% %
715% %
716% %
717% C o p y M a g i c k S t r i n g %
718% %
719% %
720% %
721%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
722%
anthony06762232012-04-29 11:45:40 +0000723% CopyMagickString() copies the source string to the destination string, with
724% out exceeding the given pre-declared length.
725%
726% The destination buffer is always null-terminated even if the string must be
727% truncated. The return value is the minimum of the source string length or
728% the length parameter.
cristy3ed852e2009-09-05 21:47:34 +0000729%
730% The format of the CopyMagickString method is:
731%
732% size_t CopyMagickString(const char *destination,char *source,
733% const size_t length)
734%
735% A description of each parameter follows:
736%
737% o destination: the destination string.
738%
739% o source: the source string.
740%
741% o length: the length of the destination string.
742%
743*/
744MagickExport size_t CopyMagickString(char *destination,const char *source,
745 const size_t length)
746{
747 register char
748 *q;
749
750 register const char
751 *p;
752
753 register size_t
754 n;
755
cristy77e3fcc2011-10-11 00:04:41 +0000756 if (source == (const char *) NULL)
757 return(0);
cristy3ed852e2009-09-05 21:47:34 +0000758 p=source;
759 q=destination;
760 for (n=length; n > 4; n-=4)
761 {
762 *q=(*p++);
763 if (*q == '\0')
764 return((size_t) (p-source-1));
765 q++;
766 *q=(*p++);
767 if (*q == '\0')
768 return((size_t) (p-source-1));
769 q++;
770 *q=(*p++);
771 if (*q == '\0')
772 return((size_t) (p-source-1));
773 q++;
774 *q=(*p++);
775 if (*q == '\0')
776 return((size_t) (p-source-1));
777 q++;
778 }
779 if (n != 0)
780 for (n--; n != 0; n--)
781 {
782 *q=(*p++);
783 if (*q == '\0')
784 return((size_t) (p-source-1));
785 q++;
786 }
787 if (length != 0)
788 *q='\0';
789 return((size_t) (p-source-1));
790}
791
792/*
793%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
794% %
795% %
796% %
797% D e s t r o y S t r i n g %
798% %
799% %
800% %
801%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
802%
803% DestroyString() destroys memory associated with a string.
804%
805% The format of the DestroyString method is:
806%
807% char *DestroyString(char *string)
808%
809% A description of each parameter follows:
810%
811% o string: the string.
812%
813*/
814MagickExport char *DestroyString(char *string)
815{
816 return((char *) RelinquishMagickMemory(string));
817}
818
819/*
820%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
821% %
822% %
823% %
824% D e s t r o y S t r i n g I n f o %
825% %
826% %
827% %
828%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
829%
830% DestroyStringInfo() destroys memory associated with the StringInfo structure.
831%
832% The format of the DestroyStringInfo method is:
833%
834% StringInfo *DestroyStringInfo(StringInfo *string_info)
835%
836% A description of each parameter follows:
837%
838% o string_info: the string info.
839%
840*/
841MagickExport StringInfo *DestroyStringInfo(StringInfo *string_info)
842{
843 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
844 assert(string_info != (StringInfo *) NULL);
845 assert(string_info->signature == MagickSignature);
846 if (string_info->datum != (unsigned char *) NULL)
847 string_info->datum=(unsigned char *) RelinquishMagickMemory(
848 string_info->datum);
849 string_info->signature=(~MagickSignature);
850 string_info=(StringInfo *) RelinquishMagickMemory(string_info);
851 return(string_info);
852}
853
854/*
855%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
856% %
857% %
858% %
859% D e s t r o y S t r i n g L i s t %
860% %
861% %
862% %
863%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
864%
865% DestroyStringList() zeros memory associated with a string list.
866%
867% The format of the DestroyStringList method is:
868%
869% char **DestroyStringList(char **list)
870%
871% A description of each parameter follows:
872%
873% o list: the string list.
874%
875*/
876MagickExport char **DestroyStringList(char **list)
877{
cristybb503372010-05-27 20:51:26 +0000878 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +0000879 i;
880
881 assert(list != (char **) NULL);
882 for (i=0; list[i] != (char *) NULL; i++)
883 list[i]=DestroyString(list[i]);
884 list=(char **) RelinquishMagickMemory(list);
885 return(list);
886}
887
888/*
889%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
890% %
891% %
892% %
893% E s c a p e S t r i n g %
894% %
895% %
896% %
897%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
898%
899% EscapeString() allocates memory for a backslash-escaped version of a
900% source text string, copies the escaped version of the text to that
901% memory location while adding backslash characters, and returns the
902% escaped string.
903%
904% The format of the EscapeString method is:
905%
906% char *EscapeString(const char *source,const char escape)
907%
908% A description of each parameter follows:
909%
910% o allocate_string: Method EscapeString returns the escaped string.
911%
912% o source: A character string.
913%
914% o escape: the quoted string termination character to escape (e.g. '"').
915%
916*/
917MagickExport char *EscapeString(const char *source,const char escape)
918{
919 char
920 *destination;
921
922 register char
923 *q;
924
925 register const char
926 *p;
927
928 size_t
929 length;
930
931 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
932 assert(source != (const char *) NULL);
933 length=strlen(source);
934 for (p=source; *p != '\0'; p++)
935 if ((*p == '\\') || (*p == escape))
936 {
937 if (~length < 1)
938 ThrowFatalException(ResourceLimitFatalError,"UnableToEscapeString");
939 length++;
940 }
941 destination=(char *) NULL;
cristy37e0b382011-06-07 13:31:21 +0000942 if (~length >= (MaxTextExtent-1))
cristy3ed852e2009-09-05 21:47:34 +0000943 destination=(char *) AcquireQuantumMemory(length+MaxTextExtent,
944 sizeof(*destination));
945 if (destination == (char *) NULL)
946 ThrowFatalException(ResourceLimitFatalError,"UnableToEscapeString");
947 *destination='\0';
948 if (source != (char *) NULL)
949 {
950 q=destination;
951 for (p=source; *p != '\0'; p++)
952 {
953 if ((*p == '\\') || (*p == escape))
954 *q++='\\';
955 *q++=(*p);
956 }
957 *q='\0';
958 }
959 return(destination);
960}
961
962/*
963%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
964% %
965% %
966% %
967% F i l e T o S t r i n g %
968% %
969% %
970% %
971%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
972%
973% FileToString() returns the contents of a file as a string.
974%
975% The format of the FileToString method is:
976%
977% char *FileToString(const char *filename,const size_t extent,
978% ExceptionInfo *exception)
979%
980% A description of each parameter follows:
981%
982% o filename: the filename.
983%
984% o extent: Maximum length of the string.
985%
986% o exception: return any errors or warnings in this structure.
987%
988*/
989MagickExport char *FileToString(const char *filename,const size_t extent,
990 ExceptionInfo *exception)
991{
992 size_t
993 length;
994
995 assert(filename != (const char *) NULL);
996 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
997 assert(exception != (ExceptionInfo *) NULL);
998 return((char *) FileToBlob(filename,extent,&length,exception));
999}
1000
1001/*
1002%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1003% %
1004% %
1005% %
1006% F i l e T o S t r i n g I n f o %
1007% %
1008% %
1009% %
1010%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1011%
1012% FileToStringInfo() returns the contents of a file as a string.
1013%
1014% The format of the FileToStringInfo method is:
1015%
1016% StringInfo *FileToStringInfo(const char *filename,const size_t extent,
1017% ExceptionInfo *exception)
1018%
1019% A description of each parameter follows:
1020%
1021% o filename: the filename.
1022%
1023% o extent: Maximum length of the string.
1024%
1025% o exception: return any errors or warnings in this structure.
1026%
1027*/
1028MagickExport StringInfo *FileToStringInfo(const char *filename,
1029 const size_t extent,ExceptionInfo *exception)
1030{
1031 StringInfo
1032 *string_info;
1033
1034 assert(filename != (const char *) NULL);
1035 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
1036 assert(exception != (ExceptionInfo *) NULL);
1037 string_info=AcquireStringInfo(0);
1038 (void) CopyMagickString(string_info->path,filename,MaxTextExtent);
1039 string_info->datum=FileToBlob(filename,extent,&string_info->length,exception);
1040 if (string_info->datum == (unsigned char *) NULL)
1041 {
1042 string_info=DestroyStringInfo(string_info);
1043 return((StringInfo *) NULL);
1044 }
1045 return(string_info);
1046}
1047
1048/*
1049%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1050% %
1051% %
1052% %
1053% F o r m a t M a g i c k S i z e %
1054% %
1055% %
1056% %
1057%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1058%
1059% FormatMagickSize() converts a size to a human readable format, for example,
cristy2ce15c92010-03-12 14:03:41 +00001060% 14k, 234m, 2.7g, or 3.0t. Scaling is done by repetitively dividing by
cristyc15ce492009-12-01 19:18:23 +00001061% 1000.
cristy3ed852e2009-09-05 21:47:34 +00001062%
1063% The format of the FormatMagickSize method is:
1064%
cristybb503372010-05-27 20:51:26 +00001065% ssize_t FormatMagickSize(const MagickSizeType size,char *format)
cristy3ed852e2009-09-05 21:47:34 +00001066%
1067% A description of each parameter follows:
1068%
1069% o size: convert this size to a human readable format.
1070%
cristyb9080c92009-12-01 20:13:26 +00001071% o bi: use power of two rather than power of ten.
1072%
cristy3ed852e2009-09-05 21:47:34 +00001073% o format: human readable format.
1074%
1075*/
cristy99bbf2c2011-09-26 18:27:50 +00001076MagickExport ssize_t FormatMagickSize(const MagickSizeType size,
cristyb9080c92009-12-01 20:13:26 +00001077 const MagickBooleanType bi,char *format)
cristy3ed852e2009-09-05 21:47:34 +00001078{
cristyb9080c92009-12-01 20:13:26 +00001079 const char
1080 **units;
1081
cristy3ed852e2009-09-05 21:47:34 +00001082 double
cristyb9080c92009-12-01 20:13:26 +00001083 bytes,
cristy3ed852e2009-09-05 21:47:34 +00001084 length;
1085
cristybb503372010-05-27 20:51:26 +00001086 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001087 i,
1088 j;
1089
cristy9d314ff2011-03-09 01:30:28 +00001090 ssize_t
1091 count;
1092
cristy3ed852e2009-09-05 21:47:34 +00001093 static const char
cristyb9080c92009-12-01 20:13:26 +00001094 *bi_units[] =
1095 {
cristy2ce15c92010-03-12 14:03:41 +00001096 "", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi", (char *) NULL
cristyb9080c92009-12-01 20:13:26 +00001097 },
1098 *traditional_units[] =
cristy9bf9da32009-09-27 16:48:34 +00001099 {
cristy2ce15c92010-03-12 14:03:41 +00001100 "", "K", "M", "G", "T", "P", "E", "Z", "Y", (char *) NULL
cristy9bf9da32009-09-27 16:48:34 +00001101 };
cristy3ed852e2009-09-05 21:47:34 +00001102
cristyb9080c92009-12-01 20:13:26 +00001103 bytes=1000.0;
1104 units=traditional_units;
1105 if (bi != MagickFalse)
1106 {
1107 bytes=1024.0;
1108 units=bi_units;
1109 }
cristy3ed852e2009-09-05 21:47:34 +00001110#if defined(_MSC_VER) && (_MSC_VER == 1200)
1111 length=(double) ((MagickOffsetType) size);
1112#else
1113 length=(double) size;
1114#endif
cristyb9080c92009-12-01 20:13:26 +00001115 for (i=0; (length >= bytes) && (units[i+1] != (const char *) NULL); i++)
1116 length/=bytes;
cristyc3b9b362013-05-25 17:16:34 +00001117 count=0;
cristy9bf9da32009-09-27 16:48:34 +00001118 for (j=2; j < 12; j++)
cristy3ed852e2009-09-05 21:47:34 +00001119 {
cristyb51dff52011-05-19 16:55:47 +00001120 count=FormatLocaleString(format,MaxTextExtent,"%.*g%sB",(int) (i+j),length,
cristy3ed852e2009-09-05 21:47:34 +00001121 units[i]);
1122 if (strchr(format,'+') == (char *) NULL)
1123 break;
1124 }
1125 return(count);
1126}
1127
1128/*
1129%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1130% %
1131% %
1132% %
cristy3ed852e2009-09-05 21:47:34 +00001133% F o r m a t M a g i c k T i m e %
1134% %
1135% %
1136% %
1137%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1138%
1139% FormatMagickTime() returns the specified time in the Internet date/time
1140% format and the length of the timestamp.
1141%
1142% The format of the FormatMagickTime method is:
1143%
cristybb503372010-05-27 20:51:26 +00001144% ssize_t FormatMagickTime(const time_t time,const size_t length,
cristy3ed852e2009-09-05 21:47:34 +00001145% char *timestamp)
1146%
1147% A description of each parameter follows.
1148%
1149% o time: the time since the Epoch (00:00:00 UTC, January 1, 1970),
1150% measured in seconds.
1151%
1152% o length: the maximum length of the string.
1153%
1154% o timestamp: Return the Internet date/time here.
1155%
1156*/
cristybb503372010-05-27 20:51:26 +00001157MagickExport ssize_t FormatMagickTime(const time_t time,const size_t length,
cristy3ed852e2009-09-05 21:47:34 +00001158 char *timestamp)
1159{
cristybb503372010-05-27 20:51:26 +00001160 ssize_t
cristy3ed852e2009-09-05 21:47:34 +00001161 count;
1162
1163 struct tm
1164 gm_time,
1165 local_time;
1166
1167 time_t
1168 timezone;
1169
1170 assert(timestamp != (char *) NULL);
1171 (void) ResetMagickMemory(&local_time,0,sizeof(local_time));
1172 (void) ResetMagickMemory(&gm_time,0,sizeof(gm_time));
1173#if defined(MAGICKCORE_HAVE_LOCALTIME_R)
1174 (void) localtime_r(&time,&local_time);
1175#else
1176 {
cristybc3392a2009-10-06 03:15:57 +00001177 struct tm
cristy3ed852e2009-09-05 21:47:34 +00001178 *my_time;
1179
1180 my_time=localtime(&time);
1181 if (my_time != (struct tm *) NULL)
1182 (void) memcpy(&local_time,my_time,sizeof(local_time));
1183 }
1184#endif
1185#if defined(MAGICKCORE_HAVE_GMTIME_R)
1186 (void) gmtime_r(&time,&gm_time);
1187#else
1188 {
cristybc3392a2009-10-06 03:15:57 +00001189 struct tm
cristy3ed852e2009-09-05 21:47:34 +00001190 *my_time;
1191
1192 my_time=gmtime(&time);
1193 if (my_time != (struct tm *) NULL)
1194 (void) memcpy(&gm_time,my_time,sizeof(gm_time));
1195 }
1196#endif
1197 timezone=(time_t) ((local_time.tm_min-gm_time.tm_min)/60+
1198 local_time.tm_hour-gm_time.tm_hour+24*((local_time.tm_year-
1199 gm_time.tm_year) != 0 ? (local_time.tm_year-gm_time.tm_year) :
1200 (local_time.tm_yday-gm_time.tm_yday)));
cristyb51dff52011-05-19 16:55:47 +00001201 count=FormatLocaleString(timestamp,length,
cristy3ed852e2009-09-05 21:47:34 +00001202 "%04d-%02d-%02dT%02d:%02d:%02d%+03ld:00",local_time.tm_year+1900,
1203 local_time.tm_mon+1,local_time.tm_mday,local_time.tm_hour,
cristyf1d91242010-05-28 02:23:19 +00001204 local_time.tm_min,local_time.tm_sec,(long) timezone);
cristy3ed852e2009-09-05 21:47:34 +00001205 return(count);
1206}
1207
1208/*
1209%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1210% %
1211% %
1212% %
1213% G e t E n v i r o n m e n t V a l u e %
1214% %
1215% %
1216% %
1217%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1218%
1219% GetEnvironmentValue() returns the environment string that matches the
1220% specified name.
1221%
1222% The format of the GetEnvironmentValue method is:
1223%
1224% char *GetEnvironmentValue(const char *name)
1225%
1226% A description of each parameter follows:
1227%
1228% o name: the environment name.
1229%
1230*/
cristy99bbf2c2011-09-26 18:27:50 +00001231MagickExport char *GetEnvironmentValue(const char *name)
cristy3ed852e2009-09-05 21:47:34 +00001232{
1233 const char
1234 *environment;
1235
1236 environment=getenv(name);
1237 if (environment == (const char *) NULL)
1238 return((char *) NULL);
1239 return(ConstantString(environment));
1240}
1241
1242/*
1243%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1244% %
1245% %
1246% %
1247% G e t S t r i n g I n f o D a t u m %
1248% %
1249% %
1250% %
1251%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1252%
1253% GetStringInfoDatum() returns the datum associated with the string.
1254%
1255% The format of the GetStringInfoDatum method is:
1256%
1257% unsigned char *GetStringInfoDatum(const StringInfo *string_info)
1258%
1259% A description of each parameter follows:
1260%
1261% o string_info: the string info.
1262%
1263*/
1264MagickExport unsigned char *GetStringInfoDatum(const StringInfo *string_info)
1265{
1266 assert(string_info != (StringInfo *) NULL);
1267 assert(string_info->signature == MagickSignature);
1268 return(string_info->datum);
1269}
1270
1271/*
1272%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1273% %
1274% %
1275% %
1276% G e t S t r i n g I n f o L e n g t h %
1277% %
1278% %
1279% %
1280%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1281%
1282% GetStringInfoLength() returns the string length.
1283%
1284% The format of the GetStringInfoLength method is:
1285%
1286% size_t GetStringInfoLength(const StringInfo *string_info)
1287%
1288% A description of each parameter follows:
1289%
1290% o string_info: the string info.
1291%
1292*/
1293MagickExport size_t GetStringInfoLength(const StringInfo *string_info)
1294{
1295 assert(string_info != (StringInfo *) NULL);
1296 assert(string_info->signature == MagickSignature);
1297 return(string_info->length);
1298}
1299
1300/*
1301%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1302% %
1303% %
1304% %
1305% G e t S t r i n g I n f o P a t h %
1306% %
1307% %
1308% %
1309%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1310%
1311% GetStringInfoPath() returns the path associated with the string.
1312%
1313% The format of the GetStringInfoPath method is:
1314%
1315% const char *GetStringInfoPath(const StringInfo *string_info)
1316%
1317% A description of each parameter follows:
1318%
1319% o string_info: the string info.
1320%
1321*/
1322MagickExport const char *GetStringInfoPath(const StringInfo *string_info)
1323{
1324 assert(string_info != (StringInfo *) NULL);
1325 assert(string_info->signature == MagickSignature);
1326 return(string_info->path);
1327}
1328
1329/*
1330%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1331% %
1332% %
1333% %
cristydbdd0e32011-11-04 23:29:40 +00001334+ I n t e r p r e t S i P r e f i x V a l u e %
1335% %
1336% %
1337% %
1338%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1339%
1340% InterpretSiPrefixValue() converts the initial portion of the string to a
1341% double representation. It also recognizes SI prefixes (e.g. B, KB, MiB,
1342% etc.).
1343%
1344% The format of the InterpretSiPrefixValue method is:
1345%
1346% double InterpretSiPrefixValue(const char *value,char **sentinal)
1347%
1348% A description of each parameter follows:
1349%
1350% o value: the string value.
1351%
1352% o sentinal: if sentinal is not NULL, return a pointer to the character
1353% after the last character used in the conversion.
1354%
1355*/
1356MagickExport double InterpretSiPrefixValue(const char *restrict string,
1357 char **restrict sentinal)
1358{
1359 char
1360 *q;
1361
1362 double
1363 value;
1364
cristydbdd0e32011-11-04 23:29:40 +00001365 value=InterpretLocaleValue(string,&q);
1366 if (q != string)
1367 {
1368 if ((*q >= 'E') && (*q <= 'z'))
1369 {
1370 double
1371 e;
1372
cristy0c2684f2011-11-05 21:39:43 +00001373 switch ((int) ((unsigned char) *q))
1374 {
1375 case 'y': e=(-24.0); break;
1376 case 'z': e=(-21.0); break;
1377 case 'a': e=(-18.0); break;
1378 case 'f': e=(-15.0); break;
1379 case 'p': e=(-12.0); break;
1380 case 'n': e=(-9.0); break;
1381 case 'u': e=(-6.0); break;
1382 case 'm': e=(-3.0); break;
1383 case 'c': e=(-2.0); break;
1384 case 'd': e=(-1.0); break;
1385 case 'h': e=2.0; break;
1386 case 'k': e=3.0; break;
1387 case 'K': e=3.0; break;
1388 case 'M': e=6.0; break;
1389 case 'G': e=9.0; break;
1390 case 'T': e=12.0; break;
1391 case 'P': e=15.0; break;
1392 case 'E': e=18.0; break;
1393 case 'Z': e=21.0; break;
1394 case 'Y': e=24.0; break;
1395 default: e=0.0; break;
1396 }
cristydbdd0e32011-11-04 23:29:40 +00001397 if (e >= MagickEpsilon)
1398 {
1399 if (q[1] == 'i')
1400 {
1401 value*=pow(2.0,e/0.3);
1402 q+=2;
1403 }
1404 else
1405 {
1406 value*=pow(10.0,e);
1407 q++;
1408 }
1409 }
1410 }
cristyc0f1ed12011-11-09 14:50:15 +00001411 if (*q == 'B')
cristy099a7352011-11-09 14:31:35 +00001412 q++;
cristydbdd0e32011-11-04 23:29:40 +00001413 }
1414 if (sentinal != (char **) NULL)
1415 *sentinal=q;
1416 return(value);
1417}
1418
1419/*
1420%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1421% %
1422% %
1423% %
anthony6f201312012-03-30 04:08:15 +00001424% I s S t r i n g T r u e %
1425% %
1426% %
1427% %
1428%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1429%
1430% IsStringTrue() returns MagickTrue if the value is "true", "on", "yes" or
1431% "1". Any other string or undefined returns MagickFalse.
1432%
1433% Typically this is used to look at strings (options or artifacts) which
1434% has a default value of "false", when not defined.
1435%
1436% The format of the IsStringTrue method is:
1437%
1438% MagickBooleanType IsStringTrue(const char *value)
1439%
1440% A description of each parameter follows:
1441%
1442% o value: Specifies a pointer to a character array.
1443%
1444*/
1445MagickExport MagickBooleanType IsStringTrue(const char *value)
1446{
1447 if (value == (const char *) NULL)
1448 return(MagickFalse);
1449 if (LocaleCompare(value,"true") == 0)
1450 return(MagickTrue);
1451 if (LocaleCompare(value,"on") == 0)
1452 return(MagickTrue);
1453 if (LocaleCompare(value,"yes") == 0)
1454 return(MagickTrue);
1455 if (LocaleCompare(value,"1") == 0)
1456 return(MagickTrue);
1457 return(MagickFalse);
1458}
1459
1460/*
1461%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1462% %
1463% %
1464% %
1465% I s S t r i n g N o t F a l s e %
1466% %
1467% %
1468% %
1469%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1470%
1471% IsStringNotFalse() returns MagickTrue, unless the string specifically
1472% has a value that makes this false. that is if it has a value of
1473% "false", "off", "no" or "0".
1474%
1475% Typically this is used to look at strings (options or artifacts) which
1476% has a default value of "true", when it has not been defined.
1477%
1478% The format of the IsStringNotFalse method is:
1479%
1480% MagickBooleanType IsStringNotFalse(const char *value)
1481%
1482% A description of each parameter follows:
1483%
1484% o value: Specifies a pointer to a character array.
1485%
1486*/
1487MagickExport MagickBooleanType IsStringNotFalse(const char *value)
1488{
1489 if (value == (const char *) NULL)
1490 return(MagickTrue);
1491 if (LocaleCompare(value,"false") == 0)
1492 return(MagickFalse);
1493 if (LocaleCompare(value,"off") == 0)
1494 return(MagickFalse);
1495 if (LocaleCompare(value,"no") == 0)
1496 return(MagickFalse);
1497 if (LocaleCompare(value,"0") == 0)
1498 return(MagickFalse);
1499 return(MagickTrue);
1500}
1501
1502/*
1503%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1504% %
1505% %
1506% %
cristy3ed852e2009-09-05 21:47:34 +00001507% L o c a l e C o m p a r e %
1508% %
1509% %
1510% %
1511%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1512%
1513% LocaleCompare() performs a case-insensitive comparison of two strings
1514% byte-by-byte, according to the ordering of the current locale encoding.
1515% LocaleCompare returns an integer greater than, equal to, or less than 0,
1516% if the string pointed to by p is greater than, equal to, or less than the
1517% string pointed to by q respectively. The sign of a non-zero return value
cristy7a40ba82011-01-08 20:31:18 +00001518% is determined by the sign of the difference between the values of the first
1519% pair of bytes that differ in the strings being compared.
cristy3ed852e2009-09-05 21:47:34 +00001520%
1521% The format of the LocaleCompare method is:
1522%
cristyde58b412010-02-18 03:53:40 +00001523% int LocaleCompare(const char *p,const char *q)
cristy3ed852e2009-09-05 21:47:34 +00001524%
1525% A description of each parameter follows:
1526%
1527% o p: A pointer to a character string.
1528%
1529% o q: A pointer to a character string to compare to p.
1530%
1531*/
cristyde58b412010-02-18 03:53:40 +00001532MagickExport int LocaleCompare(const char *p,const char *q)
cristy3ed852e2009-09-05 21:47:34 +00001533{
1534 if ((p == (char *) NULL) && (q == (char *) NULL))
1535 return(0);
1536 if (p == (char *) NULL)
1537 return(-1);
1538 if (q == (char *) NULL)
1539 return(1);
1540#if defined(MAGICKCORE_HAVE_STRCASECMP)
cristy27397b22010-02-18 17:30:43 +00001541 return(strcasecmp(p,q));
cristy3ed852e2009-09-05 21:47:34 +00001542#else
1543 {
cristyde58b412010-02-18 03:53:40 +00001544 register int
cristy3ed852e2009-09-05 21:47:34 +00001545 c,
cristya72c2d12010-02-18 01:20:28 +00001546 d;
cristy3ed852e2009-09-05 21:47:34 +00001547
cristya72c2d12010-02-18 01:20:28 +00001548 for ( ; ; )
cristy3ed852e2009-09-05 21:47:34 +00001549 {
cristyde58b412010-02-18 03:53:40 +00001550 c=(int) *((unsigned char *) p);
1551 d=(int) *((unsigned char *) q);
1552 if ((c == 0) || (AsciiMap[c] != AsciiMap[d]))
cristy3ed852e2009-09-05 21:47:34 +00001553 break;
cristya72c2d12010-02-18 01:20:28 +00001554 p++;
1555 q++;
cristy3ed852e2009-09-05 21:47:34 +00001556 }
cristyde58b412010-02-18 03:53:40 +00001557 return(AsciiMap[c]-(int) AsciiMap[d]);
cristy3ed852e2009-09-05 21:47:34 +00001558 }
1559#endif
1560}
1561
1562/*
1563%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1564% %
1565% %
1566% %
1567% L o c a l e L o w e r %
1568% %
1569% %
1570% %
1571%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1572%
1573% LocaleLower() transforms all of the characters in the supplied
1574% null-terminated string, changing all uppercase letters to lowercase.
1575%
1576% The format of the LocaleLower method is:
1577%
1578% void LocaleLower(char *string)
1579%
1580% A description of each parameter follows:
1581%
1582% o string: A pointer to the string to convert to lower-case Locale.
1583%
1584*/
1585MagickExport void LocaleLower(char *string)
1586{
1587 register char
1588 *q;
1589
1590 assert(string != (char *) NULL);
1591 for (q=string; *q != '\0'; q++)
1592 *q=(char) tolower((int) *q);
1593}
1594
1595/*
1596%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1597% %
1598% %
1599% %
1600% L o c a l e N C o m p a r e %
1601% %
1602% %
1603% %
1604%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1605%
anthony06762232012-04-29 11:45:40 +00001606% LocaleNCompare() performs a case-insensitive comparison of two strings
1607% byte-by-byte, according to the ordering of the current locale encoding.
1608%
1609% LocaleNCompare returns an integer greater than, equal to, or less than 0,
1610% if the string pointed to by p is greater than, equal to, or less than the
1611% string pointed to by q respectively. The sign of a non-zero return value
1612% is determined by the sign of the difference between the values of the first
1613% pair of bytes that differ in the strings being compared.
1614%
1615% The LocaleNCompare method makes the same comparison as LocaleCompare but
1616% looks at a maximum of n bytes. Bytes following a null byte are not
1617% compared.
cristy3ed852e2009-09-05 21:47:34 +00001618%
1619% The format of the LocaleNCompare method is:
1620%
cristyde58b412010-02-18 03:53:40 +00001621% int LocaleNCompare(const char *p,const char *q,const size_t n)
cristy3ed852e2009-09-05 21:47:34 +00001622%
1623% A description of each parameter follows:
1624%
1625% o p: A pointer to a character string.
1626%
1627% o q: A pointer to a character string to compare to p.
1628%
cristy7a40ba82011-01-08 20:31:18 +00001629% o length: the number of characters to compare in strings p and q.
cristy3ed852e2009-09-05 21:47:34 +00001630%
1631*/
cristyde58b412010-02-18 03:53:40 +00001632MagickExport int LocaleNCompare(const char *p,const char *q,const size_t length)
cristy3ed852e2009-09-05 21:47:34 +00001633{
cristy78c21692011-10-06 14:57:26 +00001634 if ((p == (char *) NULL) && (q == (char *) NULL))
1635 return(0);
cristy3ed852e2009-09-05 21:47:34 +00001636 if (p == (char *) NULL)
1637 return(-1);
1638 if (q == (char *) NULL)
1639 return(1);
1640#if defined(MAGICKCORE_HAVE_STRNCASECMP)
cristy27397b22010-02-18 17:30:43 +00001641 return(strncasecmp(p,q,length));
cristy3ed852e2009-09-05 21:47:34 +00001642#else
1643 {
cristyde58b412010-02-18 03:53:40 +00001644 register int
cristy3ed852e2009-09-05 21:47:34 +00001645 c,
1646 d;
1647
cristyb6af4a52009-10-06 13:56:23 +00001648 register size_t
cristyc4cded12010-02-18 14:40:57 +00001649 i;
cristyb6af4a52009-10-06 13:56:23 +00001650
cristyc4cded12010-02-18 14:40:57 +00001651 for (i=length; i != 0; i--)
cristy3ed852e2009-09-05 21:47:34 +00001652 {
cristyde58b412010-02-18 03:53:40 +00001653 c=(int) *((unsigned char *) p);
1654 d=(int) *((unsigned char *) q);
cristy3ed852e2009-09-05 21:47:34 +00001655 if (AsciiMap[c] != AsciiMap[d])
cristyde58b412010-02-18 03:53:40 +00001656 return(AsciiMap[c]-(int) AsciiMap[d]);
1657 if (c == 0)
1658 return(0);
cristy3ed852e2009-09-05 21:47:34 +00001659 p++;
1660 q++;
1661 }
cristyde58b412010-02-18 03:53:40 +00001662 return(0);
cristy3ed852e2009-09-05 21:47:34 +00001663 }
1664#endif
1665}
1666
1667/*
1668%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1669% %
1670% %
1671% %
1672% L o c a l e U p p e r %
1673% %
1674% %
1675% %
1676%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1677%
1678% LocaleUpper() transforms all of the characters in the supplied
1679% null-terminated string, changing all lowercase letters to uppercase.
1680%
1681% The format of the LocaleUpper method is:
1682%
1683% void LocaleUpper(char *string)
1684%
1685% A description of each parameter follows:
1686%
1687% o string: A pointer to the string to convert to upper-case Locale.
1688%
1689*/
1690MagickExport void LocaleUpper(char *string)
1691{
1692 register char
1693 *q;
1694
1695 assert(string != (char *) NULL);
1696 for (q=string; *q != '\0'; q++)
1697 *q=(char) toupper((int) *q);
1698}
1699
1700/*
1701%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1702% %
1703% %
1704% %
1705% P r i n t S t r i n g I n f o %
1706% %
1707% %
1708% %
1709%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1710%
1711% PrintStringInfo() prints the string.
1712%
1713% The format of the PrintStringInfo method is:
1714%
1715% void PrintStringInfo(FILE *file,const char *id,
1716% const StringInfo *string_info)
1717%
1718% A description of each parameter follows:
1719%
1720% o file: the file, typically stdout.
1721%
1722% o id: the string id.
1723%
1724% o string_info: the string info.
1725%
1726*/
1727MagickExport void PrintStringInfo(FILE *file,const char *id,
1728 const StringInfo *string_info)
1729{
1730 register const char
1731 *p;
1732
1733 register size_t
1734 i,
1735 j;
1736
1737 assert(id != (const char *) NULL);
1738 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",id);
1739 assert(string_info != (StringInfo *) NULL);
1740 assert(string_info->signature == MagickSignature);
1741 p=(char *) string_info->datum;
1742 for (i=0; i < string_info->length; i++)
1743 {
1744 if (((int) ((unsigned char) *p) < 32) &&
1745 (isspace((int) ((unsigned char) *p)) == 0))
1746 break;
1747 p++;
1748 }
1749 if (i == string_info->length)
1750 {
cristy53ea28b2012-06-10 22:11:59 +00001751 for (i=0; i < string_info->length; i++)
1752 (void) fputc(string_info->datum[i],file);
cristy3ed852e2009-09-05 21:47:34 +00001753 (void) fputc('\n',file);
1754 return;
1755 }
1756 /*
1757 Convert string to a HEX list.
1758 */
1759 p=(char *) string_info->datum;
1760 for (i=0; i < string_info->length; i+=0x14)
1761 {
cristyb51dff52011-05-19 16:55:47 +00001762 (void) FormatLocaleFile(file,"0x%08lx: ",(unsigned long) (0x14*i));
cristy3ed852e2009-09-05 21:47:34 +00001763 for (j=1; j <= MagickMin(string_info->length-i,0x14); j++)
1764 {
cristyb51dff52011-05-19 16:55:47 +00001765 (void) FormatLocaleFile(file,"%02lx",(unsigned long) (*(p+j)) & 0xff);
cristy3ed852e2009-09-05 21:47:34 +00001766 if ((j % 0x04) == 0)
1767 (void) fputc(' ',file);
1768 }
1769 for ( ; j <= 0x14; j++)
1770 {
1771 (void) fputc(' ',file);
1772 (void) fputc(' ',file);
1773 if ((j % 0x04) == 0)
1774 (void) fputc(' ',file);
1775 }
1776 (void) fputc(' ',file);
1777 for (j=1; j <= MagickMin(string_info->length-i,0x14); j++)
1778 {
1779 if (isprint((int) ((unsigned char) *p)) != 0)
1780 (void) fputc(*p,file);
1781 else
1782 (void) fputc('-',file);
1783 p++;
1784 }
1785 (void) fputc('\n',file);
1786 }
1787}
1788
1789/*
1790%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1791% %
1792% %
1793% %
1794% R e s e t S t r i n g I n f o %
1795% %
1796% %
1797% %
1798%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1799%
1800% ResetStringInfo() reset the string to all null bytes.
1801%
1802% The format of the ResetStringInfo method is:
1803%
1804% void ResetStringInfo(StringInfo *string_info)
1805%
1806% A description of each parameter follows:
1807%
1808% o string_info: the string info.
1809%
1810*/
1811MagickExport void ResetStringInfo(StringInfo *string_info)
1812{
1813 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1814 assert(string_info != (StringInfo *) NULL);
1815 assert(string_info->signature == MagickSignature);
1816 (void) ResetMagickMemory(string_info->datum,0,string_info->length);
1817}
1818
1819/*
1820%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1821% %
1822% %
1823% %
1824% S e t S t r i n g I n f o %
1825% %
1826% %
1827% %
1828%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1829%
1830% SetStringInfo() copies the source string to the destination string.
1831%
1832% The format of the SetStringInfo method is:
1833%
1834% void SetStringInfo(StringInfo *string_info,const StringInfo *source)
1835%
1836% A description of each parameter follows:
1837%
1838% o string_info: the string info.
1839%
1840% o source: the source string.
1841%
1842*/
1843MagickExport void SetStringInfo(StringInfo *string_info,
1844 const StringInfo *source)
1845{
1846 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1847 assert(string_info != (StringInfo *) NULL);
1848 assert(string_info->signature == MagickSignature);
1849 assert(source != (StringInfo *) NULL);
1850 assert(source->signature == MagickSignature);
1851 if (string_info->length == 0)
1852 return;
1853 (void) ResetMagickMemory(string_info->datum,0,string_info->length);
cristy54aad5e2010-09-03 16:02:04 +00001854 (void) memcpy(string_info->datum,source->datum,MagickMin(string_info->length,
1855 source->length));
cristy3ed852e2009-09-05 21:47:34 +00001856}
1857
1858/*
1859%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1860% %
1861% %
1862% %
1863% S e t S t r i n g I n f o D a t u m %
1864% %
1865% %
1866% %
1867%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1868%
1869% SetStringInfoDatum() copies bytes from the source string for the length of
1870% the destination string.
1871%
1872% The format of the SetStringInfoDatum method is:
1873%
1874% void SetStringInfoDatum(StringInfo *string_info,
1875% const unsigned char *source)
1876%
1877% A description of each parameter follows:
1878%
1879% o string_info: the string info.
1880%
1881% o source: the source string.
1882%
1883*/
1884MagickExport void SetStringInfoDatum(StringInfo *string_info,
1885 const unsigned char *source)
1886{
1887 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1888 assert(string_info != (StringInfo *) NULL);
1889 assert(string_info->signature == MagickSignature);
1890 if (string_info->length != 0)
cristy54aad5e2010-09-03 16:02:04 +00001891 (void) memcpy(string_info->datum,source,string_info->length);
cristy3ed852e2009-09-05 21:47:34 +00001892}
1893
1894/*
1895%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1896% %
1897% %
1898% %
1899% S e t S t r i n g I n f o L e n g t h %
1900% %
1901% %
1902% %
1903%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1904%
1905% SetStringInfoLength() set the string length to the specified value.
1906%
1907% The format of the SetStringInfoLength method is:
1908%
1909% void SetStringInfoLength(StringInfo *string_info,const size_t length)
1910%
1911% A description of each parameter follows:
1912%
1913% o string_info: the string info.
1914%
1915% o length: the string length.
1916%
1917*/
1918MagickExport void SetStringInfoLength(StringInfo *string_info,
1919 const size_t length)
1920{
1921 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1922 assert(string_info != (StringInfo *) NULL);
1923 assert(string_info->signature == MagickSignature);
1924 if (~length < MaxTextExtent)
1925 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1926 string_info->length=length;
1927 if (string_info->datum == (unsigned char *) NULL)
1928 string_info->datum=(unsigned char *) AcquireQuantumMemory(length+
1929 MaxTextExtent,sizeof(*string_info->datum));
1930 else
1931 string_info->datum=(unsigned char *) ResizeQuantumMemory(string_info->datum,
1932 length+MaxTextExtent,sizeof(*string_info->datum));
1933 if (string_info->datum == (unsigned char *) NULL)
1934 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1935}
1936
1937/*
1938%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1939% %
1940% %
1941% %
1942% S e t S t r i n g I n f o D a t u m %
1943% %
1944% %
1945% %
1946%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1947%
1948% SetStringInfoPath() sets the path associated with the string.
1949%
1950% The format of the SetStringInfoPath method is:
1951%
1952% void SetStringInfoPath(StringInfo *string_info,const char *path)
1953%
1954% A description of each parameter follows:
1955%
1956% o string_info: the string info.
1957%
1958% o path: the path.
1959%
1960*/
1961MagickExport void SetStringInfoPath(StringInfo *string_info,const char *path)
1962{
1963 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1964 assert(string_info != (StringInfo *) NULL);
1965 assert(string_info->signature == MagickSignature);
1966 assert(path != (const char *) NULL);
1967 (void) CopyMagickString(string_info->path,path,MaxTextExtent);
1968}
1969
1970/*
1971%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1972% %
1973% %
1974% %
1975% S p l i t S t r i n g I n f o %
1976% %
1977% %
1978% %
1979%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1980%
1981% SplitStringInfo() splits a string into two and returns it.
1982%
1983% The format of the SplitStringInfo method is:
1984%
1985% StringInfo *SplitStringInfo(StringInfo *string_info,const size_t offset)
1986%
1987% A description of each parameter follows:
1988%
1989% o string_info: the string info.
1990%
1991*/
1992MagickExport StringInfo *SplitStringInfo(StringInfo *string_info,
1993 const size_t offset)
1994{
1995 StringInfo
1996 *split_info;
1997
1998 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
1999 assert(string_info != (StringInfo *) NULL);
2000 assert(string_info->signature == MagickSignature);
2001 if (offset > string_info->length)
2002 return((StringInfo *) NULL);
2003 split_info=AcquireStringInfo(offset);
2004 SetStringInfo(split_info,string_info);
cristy1bd862c2011-05-21 13:44:38 +00002005 (void) memmove(string_info->datum,string_info->datum+offset,
cristy3ed852e2009-09-05 21:47:34 +00002006 string_info->length-offset+MaxTextExtent);
2007 SetStringInfoLength(string_info,string_info->length-offset);
2008 return(split_info);
2009}
2010
2011/*
2012%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2013% %
2014% %
2015% %
2016% S t r i n g I n f o T o S t r i n g %
2017% %
2018% %
2019% %
2020%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2021%
2022% StringInfoToString() converts a string info string to a C string.
2023%
2024% The format of the StringInfoToString method is:
2025%
2026% char *StringInfoToString(const StringInfo *string_info)
2027%
2028% A description of each parameter follows:
2029%
2030% o string_info: the string.
2031%
2032*/
2033MagickExport char *StringInfoToString(const StringInfo *string_info)
2034{
2035 char
2036 *string;
2037
2038 size_t
2039 length;
2040
2041 string=(char *) NULL;
2042 length=string_info->length;
cristy37e0b382011-06-07 13:31:21 +00002043 if (~length >= (MaxTextExtent-1))
cristy3ed852e2009-09-05 21:47:34 +00002044 string=(char *) AcquireQuantumMemory(length+MaxTextExtent,sizeof(*string));
cristy208cacf2010-09-03 02:24:42 +00002045 if (string == (char *) NULL)
2046 return((char *) NULL);
cristy54aad5e2010-09-03 16:02:04 +00002047 (void) memcpy(string,(char *) string_info->datum,length*sizeof(*string));
cristy208cacf2010-09-03 02:24:42 +00002048 string[length]='\0';
cristy3ed852e2009-09-05 21:47:34 +00002049 return(string);
2050}
2051
2052/*
2053%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2054% %
2055% %
2056% %
anthonyb1d483a2012-04-14 12:53:56 +00002057% S t r i n g I n f o T o H e x S t r i n g %
2058% %
2059% %
2060% %
2061%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2062%
2063% StringInfoToHexString() converts a string info string to a C string.
2064%
2065% The format of the StringInfoToHexString method is:
2066%
2067% char *StringInfoToHexString(const StringInfo *string_info)
2068%
2069% A description of each parameter follows:
2070%
2071% o string_info: the string.
2072%
2073*/
2074MagickExport char *StringInfoToHexString(const StringInfo *string_info)
2075{
2076 char
2077 *string;
2078
2079 register const unsigned char
2080 *p;
2081
2082 register ssize_t
2083 i;
2084
2085 register unsigned char
2086 *q;
2087
2088 size_t
2089 length;
2090
2091 unsigned char
2092 hex_digits[16];
2093
2094 length=string_info->length;
2095 if (~length < MaxTextExtent)
2096 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
2097 string=(char *) AcquireQuantumMemory(length+MaxTextExtent,2*sizeof(*string));
2098 if (string == (char *) NULL)
2099 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
2100 hex_digits[0]='0';
2101 hex_digits[1]='1';
2102 hex_digits[2]='2';
2103 hex_digits[3]='3';
2104 hex_digits[4]='4';
2105 hex_digits[5]='5';
2106 hex_digits[6]='6';
2107 hex_digits[7]='7';
2108 hex_digits[8]='8';
2109 hex_digits[9]='9';
2110 hex_digits[10]='a';
2111 hex_digits[11]='b';
2112 hex_digits[12]='c';
2113 hex_digits[13]='d';
2114 hex_digits[14]='e';
2115 hex_digits[15]='f';
2116 p=string_info->datum;
2117 q=(unsigned char *) string;
2118 for (i=0; i < (ssize_t) string_info->length; i++)
2119 {
2120 *q++=hex_digits[(*p >> 4) & 0x0f];
2121 *q++=hex_digits[*p & 0x0f];
2122 p++;
2123 }
2124 *q='\0';
2125 return(string);
2126}
2127
2128/*
2129%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2130% %
2131% %
2132% %
cristy3ed852e2009-09-05 21:47:34 +00002133% S t r i n g T o A r g v %
2134% %
2135% %
2136% %
2137%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2138%
2139% StringToArgv() converts a text string into command line arguments.
anthony31f1bf72012-01-30 12:37:22 +00002140% The 'argv' array of arguments, is returned while the number of arguments
2141% is returned via the provided integer variable pointer.
2142%
2143% Simple 'word' tokenizer, which allows for each word to be optionally
2144% quoted. However it will not allow use of partial quotes, or escape
2145% characters.
cristy3ed852e2009-09-05 21:47:34 +00002146%
2147% The format of the StringToArgv method is:
2148%
2149% char **StringToArgv(const char *text,int *argc)
2150%
2151% A description of each parameter follows:
2152%
2153% o argv: Method StringToArgv returns the string list unless an error
2154% occurs, otherwise NULL.
2155%
2156% o text: Specifies the string to segment into a list.
2157%
2158% o argc: This integer pointer returns the number of arguments in the
2159% list.
2160%
2161*/
2162MagickExport char **StringToArgv(const char *text,int *argc)
2163{
2164 char
2165 **argv;
2166
2167 register const char
2168 *p,
2169 *q;
2170
cristybb503372010-05-27 20:51:26 +00002171 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002172 i;
2173
2174 *argc=0;
2175 if (text == (char *) NULL)
2176 return((char **) NULL);
2177 /*
2178 Determine the number of arguments.
2179 */
2180 for (p=text; *p != '\0'; )
2181 {
2182 while (isspace((int) ((unsigned char) *p)) != 0)
2183 p++;
cristya20c9042011-05-19 13:22:18 +00002184 if (*p == '\0')
cristy74895d32011-01-22 21:30:47 +00002185 break;
cristy3ed852e2009-09-05 21:47:34 +00002186 (*argc)++;
2187 if (*p == '"')
2188 for (p++; (*p != '"') && (*p != '\0'); p++) ;
2189 if (*p == '\'')
2190 for (p++; (*p != '\'') && (*p != '\0'); p++) ;
2191 while ((isspace((int) ((unsigned char) *p)) == 0) && (*p != '\0'))
2192 p++;
2193 }
2194 (*argc)++;
cristy1b26e1f2011-03-25 15:40:56 +00002195 argv=(char **) AcquireQuantumMemory((size_t) (*argc+1UL),sizeof(*argv));
cristy3ed852e2009-09-05 21:47:34 +00002196 if (argv == (char **) NULL)
2197 ThrowFatalException(ResourceLimitFatalError,"UnableToConvertStringToARGV");
2198 /*
2199 Convert string to an ASCII list.
2200 */
2201 argv[0]=AcquireString("magick");
2202 p=text;
cristybb503372010-05-27 20:51:26 +00002203 for (i=1; i < (ssize_t) *argc; i++)
cristy3ed852e2009-09-05 21:47:34 +00002204 {
2205 while (isspace((int) ((unsigned char) *p)) != 0)
2206 p++;
2207 q=p;
2208 if (*q == '"')
2209 {
2210 p++;
2211 for (q++; (*q != '"') && (*q != '\0'); q++) ;
2212 }
2213 else
2214 if (*q == '\'')
2215 {
cristy06b15f42011-01-22 21:36:24 +00002216 p++;
cristy3ed852e2009-09-05 21:47:34 +00002217 for (q++; (*q != '\'') && (*q != '\0'); q++) ;
cristy3ed852e2009-09-05 21:47:34 +00002218 }
2219 else
2220 while ((isspace((int) ((unsigned char) *q)) == 0) && (*q != '\0'))
2221 q++;
2222 argv[i]=(char *) AcquireQuantumMemory((size_t) (q-p)+MaxTextExtent,
2223 sizeof(**argv));
2224 if (argv[i] == (char *) NULL)
2225 {
2226 for (i--; i >= 0; i--)
2227 argv[i]=DestroyString(argv[i]);
2228 argv=(char **) RelinquishMagickMemory(argv);
2229 ThrowFatalException(ResourceLimitFatalError,
2230 "UnableToConvertStringToARGV");
2231 }
cristy54aad5e2010-09-03 16:02:04 +00002232 (void) memcpy(argv[i],p,(size_t) (q-p));
cristy208cacf2010-09-03 02:24:42 +00002233 argv[i][q-p]='\0';
cristy3ed852e2009-09-05 21:47:34 +00002234 p=q;
2235 while ((isspace((int) ((unsigned char) *p)) == 0) && (*p != '\0'))
2236 p++;
2237 }
2238 argv[i]=(char *) NULL;
2239 return(argv);
2240}
2241
2242/*
2243%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2244% %
2245% %
2246% %
anthonyb1d483a2012-04-14 12:53:56 +00002247% S t r i n g T o A r r a y O f D o u b l e s %
cristy3ed852e2009-09-05 21:47:34 +00002248% %
2249% %
2250% %
2251%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2252%
anthonyb1d483a2012-04-14 12:53:56 +00002253% StringToArrayOfDoubles() converts a string of space or comma seperated
2254% numbers into array of floating point numbers (doubles). Any number that
2255% failes to parse properly will produce a syntax error. As will two commas
2256% without a number between them. However a final comma at the end will
2257% not be regarded as an error so as to simplify automatic list generation.
cristy3ed852e2009-09-05 21:47:34 +00002258%
anthonyb1d483a2012-04-14 12:53:56 +00002259% A NULL value is returned on syntax or memory errors.
cristy3ed852e2009-09-05 21:47:34 +00002260%
anthonyb1d483a2012-04-14 12:53:56 +00002261% Use RelinquishMagickMemory() to free returned array when finished.
2262%
2263% The format of the StringToArrayOfDoubles method is:
2264%
cristy0a887dc2012-08-15 22:58:36 +00002265% double *StringToArrayOfDoubles(const char *string,size_t *count,
2266% ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002267%
2268% A description of each parameter follows:
2269%
anthonyb1d483a2012-04-14 12:53:56 +00002270% o string: the string containing the comma/space seperated values.
2271%
2272% o count: returns number of arguments in returned array
2273%
cristyb86be3d2012-04-14 14:53:30 +00002274% o exception: return 'memory failure' exceptions
cristy3ed852e2009-09-05 21:47:34 +00002275%
2276*/
cristy0a887dc2012-08-15 22:58:36 +00002277MagickExport double *StringToArrayOfDoubles(const char *string,ssize_t *count,
2278 ExceptionInfo *exception)
cristy3ed852e2009-09-05 21:47:34 +00002279{
anthonyb1d483a2012-04-14 12:53:56 +00002280 char
2281 *q;
2282
cristy0a887dc2012-08-15 22:58:36 +00002283 const char
2284 *p;
2285
anthonyb1d483a2012-04-14 12:53:56 +00002286 double
2287 *array;
2288
cristybb503372010-05-27 20:51:26 +00002289 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002290 i;
2291
cristy0a887dc2012-08-15 22:58:36 +00002292 /*
2293 Determine count of values, and check syntax.
2294 */
cristyb0de93f2013-05-03 13:39:25 +00002295 assert(exception != (ExceptionInfo *) NULL);
2296 assert(exception->signature == MagickSignature);
anthonyb1d483a2012-04-14 12:53:56 +00002297 *count=0;
anthonyb1d483a2012-04-14 12:53:56 +00002298 i=0;
cristy0a887dc2012-08-15 22:58:36 +00002299 p=string;
2300 while (*p != '\0')
cristy3ed852e2009-09-05 21:47:34 +00002301 {
cristy0a887dc2012-08-15 22:58:36 +00002302 (void) StringToDouble(p,&q); /* get value - ignores leading space */
2303 if (p == q)
2304 return((double *) NULL); /* no value found */
2305 p=q;
2306 i++; /* increment value count */
2307 while (isspace((int) ((unsigned char) *p)) != 0)
2308 p++; /* skip spaces */
2309 if (*p == ',')
2310 p++; /* skip comma */
2311 while (isspace((int) ((unsigned char) *p)) != 0)
2312 p++; /* and more spaces */
cristy3ed852e2009-09-05 21:47:34 +00002313 }
cristy0a887dc2012-08-15 22:58:36 +00002314 /*
2315 Allocate floating point argument list.
2316 */
anthonyb1d483a2012-04-14 12:53:56 +00002317 *count=i;
cristy0a887dc2012-08-15 22:58:36 +00002318 array=(double *) AcquireQuantumMemory((size_t) i,sizeof(*array));
2319 if (array == (double *) NULL)
anthonyb1d483a2012-04-14 12:53:56 +00002320 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
cristy0a887dc2012-08-15 22:58:36 +00002321 /*
2322 Fill in the floating point values.
2323 */
anthonyb1d483a2012-04-14 12:53:56 +00002324 i=0;
cristy0a887dc2012-08-15 22:58:36 +00002325 p=string;
2326 while ((*p != '\0') && (i < *count))
2327 {
anthonyb1d483a2012-04-14 12:53:56 +00002328 array[i++]=StringToDouble(p,&q);
2329 p=q;
cristy0a887dc2012-08-15 22:58:36 +00002330 while ((isspace((int) ((unsigned char) *p)) != 0) || (*p == ','))
2331 p++;
anthonyb1d483a2012-04-14 12:53:56 +00002332 }
anthonyb1d483a2012-04-14 12:53:56 +00002333 return(array);
cristy3ed852e2009-09-05 21:47:34 +00002334}
2335
2336/*
2337%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2338% %
2339% %
2340% %
anthonyb1d483a2012-04-14 12:53:56 +00002341+ S t r i n g T o k e n %
cristy3ed852e2009-09-05 21:47:34 +00002342% %
2343% %
2344% %
2345%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2346%
anthonyb1d483a2012-04-14 12:53:56 +00002347% StringToken() Looks for any one of given delimiters and splits the string
2348% into two separate strings by replacing the delimiter character found with a
2349% nul character.
2350%
2351% The given string pointer is changed to point to the string following the
2352% delimiter character found, or NULL. A pointer to the start of the
2353% string is returned, representing the token before the delimiter.
2354%
2355% In may ways this is equivent to the strtok() C library function, but with
2356% multiple delimiter characters rather than a delimiter string.
cristy3ed852e2009-09-05 21:47:34 +00002357%
2358% The format of the StringToken method is:
2359%
2360% char *StringToken(const char *delimiters,char **string)
2361%
2362% A description of each parameter follows:
2363%
2364% o delimiters: one or more delimiters.
2365%
2366% o string: return the first token in the string. If none is found, return
2367% NULL.
2368%
2369*/
2370MagickExport char *StringToken(const char *delimiters,char **string)
2371{
2372 char
2373 *q;
2374
2375 register char
2376 *p;
2377
2378 register const char
2379 *r;
2380
2381 register int
2382 c,
2383 d;
2384
2385 p=(*string);
2386 if (p == (char *) NULL)
2387 return((char *) NULL);
anthonyb1d483a2012-04-14 12:53:56 +00002388 q=p;
2389 for ( ; ; )
cristy3ed852e2009-09-05 21:47:34 +00002390 {
2391 c=(*p++);
2392 r=delimiters;
2393 do
2394 {
2395 d=(*r++);
2396 if (c == d)
2397 {
2398 if (c == '\0')
2399 p=(char *) NULL;
2400 else
2401 p[-1]='\0';
2402 *string=p;
2403 return(q);
2404 }
2405 } while (d != '\0');
2406 }
2407}
2408
2409/*
2410%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2411% %
2412% %
2413% %
2414% S t r i n g T o L i s t %
2415% %
2416% %
2417% %
2418%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2419%
2420% StringToList() converts a text string into a list by segmenting the text
2421% string at each carriage return discovered. The list is converted to HEX
2422% characters if any control characters are discovered within the text string.
2423%
2424% The format of the StringToList method is:
2425%
2426% char **StringToList(const char *text)
2427%
2428% A description of each parameter follows:
2429%
cristy3ed852e2009-09-05 21:47:34 +00002430% o text: Specifies the string to segment into a list.
2431%
2432*/
2433MagickExport char **StringToList(const char *text)
2434{
2435 char
2436 **textlist;
2437
2438 register const char
2439 *p;
2440
cristybb503372010-05-27 20:51:26 +00002441 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002442 i;
2443
cristybb503372010-05-27 20:51:26 +00002444 size_t
cristy3ed852e2009-09-05 21:47:34 +00002445 lines;
2446
2447 if (text == (char *) NULL)
2448 return((char **) NULL);
2449 for (p=text; *p != '\0'; p++)
2450 if (((int) ((unsigned char) *p) < 32) &&
2451 (isspace((int) ((unsigned char) *p)) == 0))
2452 break;
2453 if (*p == '\0')
2454 {
2455 register const char
2456 *q;
2457
2458 /*
2459 Convert string to an ASCII list.
2460 */
2461 lines=1;
2462 for (p=text; *p != '\0'; p++)
2463 if (*p == '\n')
2464 lines++;
2465 textlist=(char **) AcquireQuantumMemory((size_t) lines+1UL,
2466 sizeof(*textlist));
2467 if (textlist == (char **) NULL)
2468 ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
2469 p=text;
cristybb503372010-05-27 20:51:26 +00002470 for (i=0; i < (ssize_t) lines; i++)
cristy3ed852e2009-09-05 21:47:34 +00002471 {
2472 for (q=p; *q != '\0'; q++)
2473 if ((*q == '\r') || (*q == '\n'))
2474 break;
2475 textlist[i]=(char *) AcquireQuantumMemory((size_t) (q-p)+MaxTextExtent,
cristy2e25ee62011-03-25 15:43:12 +00002476 sizeof(**textlist));
cristy3ed852e2009-09-05 21:47:34 +00002477 if (textlist[i] == (char *) NULL)
2478 ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
cristy54aad5e2010-09-03 16:02:04 +00002479 (void) memcpy(textlist[i],p,(size_t) (q-p));
cristy208cacf2010-09-03 02:24:42 +00002480 textlist[i][q-p]='\0';
cristy3ed852e2009-09-05 21:47:34 +00002481 if (*q == '\r')
2482 q++;
2483 p=q+1;
2484 }
2485 }
2486 else
2487 {
2488 char
2489 hex_string[MaxTextExtent];
2490
2491 register char
2492 *q;
2493
cristybb503372010-05-27 20:51:26 +00002494 register ssize_t
cristy3ed852e2009-09-05 21:47:34 +00002495 j;
2496
2497 /*
2498 Convert string to a HEX list.
2499 */
cristybb503372010-05-27 20:51:26 +00002500 lines=(size_t) (strlen(text)/0x14)+1;
cristy3ed852e2009-09-05 21:47:34 +00002501 textlist=(char **) AcquireQuantumMemory((size_t) lines+1UL,
2502 sizeof(*textlist));
2503 if (textlist == (char **) NULL)
2504 ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
2505 p=text;
cristybb503372010-05-27 20:51:26 +00002506 for (i=0; i < (ssize_t) lines; i++)
cristy3ed852e2009-09-05 21:47:34 +00002507 {
2508 textlist[i]=(char *) AcquireQuantumMemory(2UL*MaxTextExtent,
cristy2e25ee62011-03-25 15:43:12 +00002509 sizeof(**textlist));
cristy3ed852e2009-09-05 21:47:34 +00002510 if (textlist[i] == (char *) NULL)
2511 ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
cristyb51dff52011-05-19 16:55:47 +00002512 (void) FormatLocaleString(textlist[i],MaxTextExtent,"0x%08lx: ",
cristyf1d91242010-05-28 02:23:19 +00002513 (long) (0x14*i));
cristy3ed852e2009-09-05 21:47:34 +00002514 q=textlist[i]+strlen(textlist[i]);
cristybb503372010-05-27 20:51:26 +00002515 for (j=1; j <= (ssize_t) MagickMin(strlen(p),0x14); j++)
cristy3ed852e2009-09-05 21:47:34 +00002516 {
cristyb51dff52011-05-19 16:55:47 +00002517 (void) FormatLocaleString(hex_string,MaxTextExtent,"%02x",*(p+j));
cristy3ed852e2009-09-05 21:47:34 +00002518 (void) CopyMagickString(q,hex_string,MaxTextExtent);
2519 q+=2;
2520 if ((j % 0x04) == 0)
2521 *q++=' ';
2522 }
2523 for ( ; j <= 0x14; j++)
2524 {
2525 *q++=' ';
2526 *q++=' ';
2527 if ((j % 0x04) == 0)
2528 *q++=' ';
2529 }
2530 *q++=' ';
cristybb503372010-05-27 20:51:26 +00002531 for (j=1; j <= (ssize_t) MagickMin(strlen(p),0x14); j++)
cristy3ed852e2009-09-05 21:47:34 +00002532 {
2533 if (isprint((int) ((unsigned char) *p)) != 0)
2534 *q++=(*p);
2535 else
2536 *q++='-';
2537 p++;
2538 }
2539 *q='\0';
2540 }
2541 }
2542 textlist[i]=(char *) NULL;
2543 return(textlist);
2544}
2545
2546/*
2547%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2548% %
2549% %
2550% %
2551% S t r i n g T o S t r i n g I n f o %
2552% %
2553% %
2554% %
2555%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2556%
cristybca58f32011-08-21 14:48:50 +00002557% StringToStringInfo() converts a string to a StringInfo type.
cristy3ed852e2009-09-05 21:47:34 +00002558%
2559% The format of the StringToStringInfo method is:
2560%
2561% StringInfo *StringToStringInfo(const char *string)
2562%
2563% A description of each parameter follows:
2564%
2565% o string: The string.
2566%
2567*/
2568MagickExport StringInfo *StringToStringInfo(const char *string)
2569{
2570 StringInfo
2571 *string_info;
2572
2573 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
2574 assert(string != (const char *) NULL);
cristybca58f32011-08-21 14:48:50 +00002575 string_info=AcquireStringInfo(strlen(string));
cristy3ed852e2009-09-05 21:47:34 +00002576 SetStringInfoDatum(string_info,(const unsigned char *) string);
2577 return(string_info);
2578}
2579
2580/*
2581%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2582% %
2583% %
2584% %
2585% S t r i p S t r i n g %
2586% %
2587% %
2588% %
2589%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2590%
2591% StripString() strips any whitespace or quotes from the beginning and end of
2592% a string of characters.
2593%
2594% The format of the StripString method is:
2595%
2596% void StripString(char *message)
2597%
2598% A description of each parameter follows:
2599%
2600% o message: Specifies an array of characters.
2601%
2602*/
2603MagickExport void StripString(char *message)
2604{
2605 register char
2606 *p,
2607 *q;
2608
2609 size_t
2610 length;
2611
2612 assert(message != (char *) NULL);
2613 if (*message == '\0')
2614 return;
2615 length=strlen(message);
2616 p=message;
2617 while (isspace((int) ((unsigned char) *p)) != 0)
2618 p++;
2619 if ((*p == '\'') || (*p == '"'))
2620 p++;
2621 q=message+length-1;
2622 while ((isspace((int) ((unsigned char) *q)) != 0) && (q > p))
2623 q--;
2624 if (q > p)
2625 if ((*q == '\'') || (*q == '"'))
2626 q--;
cristya63c1ba2011-06-02 20:33:43 +00002627 (void) memmove(message,p,(size_t) (q-p+1));
cristy3ed852e2009-09-05 21:47:34 +00002628 message[q-p+1]='\0';
2629 for (p=message; *p != '\0'; p++)
2630 if (*p == '\n')
2631 *p=' ';
2632}
2633
2634/*
2635%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2636% %
2637% %
2638% %
2639% S u b s t i t u t e S t r i n g %
2640% %
2641% %
2642% %
2643%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2644%
cristyf1b72c12009-10-06 13:25:21 +00002645% SubstituteString() performs string substitution on a string, replacing the
2646% string with the substituted version. Buffer must be allocated from the heap.
cristybc3392a2009-10-06 03:15:57 +00002647% If the string is matched and status, MagickTrue is returned otherwise
2648% MagickFalse.
cristy3ed852e2009-09-05 21:47:34 +00002649%
2650% The format of the SubstituteString method is:
2651%
cristyf1b72c12009-10-06 13:25:21 +00002652% MagickBooleanType SubstituteString(char **string,const char *search,
cristy3ed852e2009-09-05 21:47:34 +00002653% const char *replace)
2654%
2655% A description of each parameter follows:
2656%
cristyf1b72c12009-10-06 13:25:21 +00002657% o string: the string to perform replacements on; replaced with new
cristy3ed852e2009-09-05 21:47:34 +00002658% allocation if a replacement is made.
2659%
cristybc3392a2009-10-06 03:15:57 +00002660% o search: search for this string.
cristy3ed852e2009-09-05 21:47:34 +00002661%
cristybc3392a2009-10-06 03:15:57 +00002662% o replace: replace any matches with this string.
cristy3ed852e2009-09-05 21:47:34 +00002663%
2664*/
cristyf1b72c12009-10-06 13:25:21 +00002665MagickExport MagickBooleanType SubstituteString(char **string,
cristy3ed852e2009-09-05 21:47:34 +00002666 const char *search,const char *replace)
2667{
cristybc3392a2009-10-06 03:15:57 +00002668 MagickBooleanType
2669 status;
cristy3ed852e2009-09-05 21:47:34 +00002670
cristybc3392a2009-10-06 03:15:57 +00002671 register char
2672 *p;
cristy3ed852e2009-09-05 21:47:34 +00002673
cristy3ed852e2009-09-05 21:47:34 +00002674 size_t
cristybc3392a2009-10-06 03:15:57 +00002675 extent,
2676 replace_extent,
2677 search_extent;
cristy3ed852e2009-09-05 21:47:34 +00002678
cristyf1b72c12009-10-06 13:25:21 +00002679 ssize_t
2680 offset;
2681
cristybc3392a2009-10-06 03:15:57 +00002682 status=MagickFalse;
2683 search_extent=0,
2684 replace_extent=0;
cristyf1b72c12009-10-06 13:25:21 +00002685 for (p=strchr(*string,*search); p != (char *) NULL; p=strchr(p+1,*search))
cristy3ed852e2009-09-05 21:47:34 +00002686 {
cristyf1b72c12009-10-06 13:25:21 +00002687 if (search_extent == 0)
2688 search_extent=strlen(search);
2689 if (strncmp(p,search,search_extent) != 0)
cristybc3392a2009-10-06 03:15:57 +00002690 continue;
cristy3ed852e2009-09-05 21:47:34 +00002691 /*
cristybc3392a2009-10-06 03:15:57 +00002692 We found a match.
cristy3ed852e2009-09-05 21:47:34 +00002693 */
cristyf1b72c12009-10-06 13:25:21 +00002694 status=MagickTrue;
cristybc3392a2009-10-06 03:15:57 +00002695 if (replace_extent == 0)
2696 replace_extent=strlen(replace);
2697 if (replace_extent > search_extent)
cristy3ed852e2009-09-05 21:47:34 +00002698 {
cristybc3392a2009-10-06 03:15:57 +00002699 /*
2700 Make room for the replacement string.
2701 */
cristyde58b412010-02-18 03:53:40 +00002702 offset=(ssize_t) (p-(*string));
cristye08c3b82009-10-06 14:58:14 +00002703 extent=strlen(*string)+replace_extent-search_extent+1;
cristyf1b72c12009-10-06 13:25:21 +00002704 *string=(char *) ResizeQuantumMemory(*string,extent+MaxTextExtent,
2705 sizeof(*p));
2706 if (*string == (char *) NULL)
cristy3ed852e2009-09-05 21:47:34 +00002707 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
cristyf1b72c12009-10-06 13:25:21 +00002708 p=(*string)+offset;
cristy3ed852e2009-09-05 21:47:34 +00002709 }
cristy3ed852e2009-09-05 21:47:34 +00002710 /*
cristybc3392a2009-10-06 03:15:57 +00002711 Replace string.
cristy3ed852e2009-09-05 21:47:34 +00002712 */
cristybc3392a2009-10-06 03:15:57 +00002713 if (search_extent != replace_extent)
cristy0a9b3722010-10-23 18:45:49 +00002714 (void) CopyMagickMemory(p+replace_extent,p+search_extent,
2715 strlen(p+search_extent)+1);
2716 (void) CopyMagickMemory(p,replace,replace_extent);
cristyf1b72c12009-10-06 13:25:21 +00002717 p+=replace_extent-1;
cristy3ed852e2009-09-05 21:47:34 +00002718 }
cristybc3392a2009-10-06 03:15:57 +00002719 return(status);
cristy3ed852e2009-09-05 21:47:34 +00002720}