2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % SSSSS TTTTT RRRR IIIII N N GGGG %
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 %
13 % MagickCore String Methods %
20 % Copyright 1999-2014 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the license. You may %
24 % obtain a copy of the license at %
26 % http://www.imagemagick.org/script/license.php %
28 % unless required by applicable law or agreed to in writing, software %
29 % distributed under the license is distributed on an "as is" basis, %
30 % without warranties or conditions of any kind, either express or implied. %
31 % See the license for the specific language governing permissions and %
32 % limitations under the license. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 #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"
51 #include "MagickCore/nt-base-private.h"
52 #include "MagickCore/property.h"
53 #include "MagickCore/resource_.h"
54 #include "MagickCore/signature-private.h"
55 #include "MagickCore/string_.h"
56 #include "MagickCore/string-private.h"
57 #include "MagickCore/utility-private.h"
62 #if !defined(MAGICKCORE_HAVE_STRCASECMP) || !defined(MAGICKCORE_HAVE_STRNCASECMP)
63 static const unsigned char
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,
92 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
96 % A c q u i r e S t r i n g %
100 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
102 % AcquireString() returns an new extented string, containing a clone of the
105 % An extended string is the string length, plus an extra MaxTextExtent space
106 % to allow for the string to be actively worked on.
108 % The returned string shoud be freed using DestoryString().
110 % The format of the AcquireString method is:
112 % char *AcquireString(const char *source)
114 % A description of each parameter follows:
116 % o source: A character string.
119 MagickExport char *AcquireString(const char *source)
128 if (source != (char *) NULL)
129 length+=strlen(source);
130 if (~length < MaxTextExtent)
131 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
132 destination=(char *) AcquireQuantumMemory(length+MaxTextExtent,
133 sizeof(*destination));
134 if (destination == (char *) NULL)
135 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
137 if (source != (char *) NULL)
138 (void) memcpy(destination,source,length*sizeof(*destination));
139 destination[length]='\0';
144 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
148 % A c q u i r e S t r i n g I n f o %
152 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
154 % AcquireStringInfo() allocates the StringInfo structure.
156 % The format of the AcquireStringInfo method is:
158 % StringInfo *AcquireStringInfo(const size_t length)
160 % A description of each parameter follows:
162 % o length: the string length.
165 MagickExport StringInfo *AcquireStringInfo(const size_t length)
170 string_info=(StringInfo *) AcquireMagickMemory(sizeof(*string_info));
171 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)
178 string_info->datum=(unsigned char *) NULL;
179 if (~string_info->length >= (MaxTextExtent-1))
180 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");
189 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
193 % B l o b T o S t r i n g I n f o %
197 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
199 % BlobToStringInfo() returns the contents of a blob as a StringInfo structure
200 % with MaxTextExtent extra space.
202 % The format of the BlobToStringInfo method is:
204 % StringInfo *BlobToStringInfo(const void *blob,const size_t length)
206 % A description of each parameter follows:
210 % o length: the length of the blob.
213 MagickExport StringInfo *BlobToStringInfo(const void *blob,const size_t length)
218 string_info=AcquireStringInfo(0);
219 string_info->length=length;
220 if (~string_info->length >= (MaxTextExtent-1))
221 string_info->datum=(unsigned char *) AcquireQuantumMemory(
222 string_info->length+MaxTextExtent,sizeof(*string_info->datum));
223 if (string_info->datum == (unsigned char *) NULL)
225 string_info=DestroyStringInfo(string_info);
226 return((StringInfo *) NULL);
228 if (blob != (const void *) NULL)
229 (void) memcpy(string_info->datum,blob,length);
234 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
238 % C l o n e S t r i n g %
242 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
244 % 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
248 % 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.
251 % When finished the non-NULL string should be freed using DestoryString()
252 % or using CloneString() with a NULL pointed for the source.
254 % The format of the CloneString method is:
256 % char *CloneString(char **destination,const char *source)
258 % A description of each parameter follows:
260 % o destination: A pointer to a character string.
262 % o source: A character string.
265 MagickExport char *CloneString(char **destination,const char *source)
270 assert(destination != (char **) NULL);
271 if (source == (const char *) NULL)
273 if (*destination != (char *) NULL)
274 *destination=DestroyString(*destination);
275 return(*destination);
277 if (*destination == (char *) NULL)
279 *destination=AcquireString(source);
280 return(*destination);
282 length=strlen(source);
283 if (~length < MaxTextExtent)
284 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
285 *destination=(char *) ResizeQuantumMemory(*destination,length+MaxTextExtent,
286 sizeof(**destination));
287 if (*destination == (char *) NULL)
288 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
290 (void) memcpy(*destination,source,length*sizeof(**destination));
291 (*destination)[length]='\0';
292 return(*destination);
296 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
300 % C l o n e S t r i n g I n f o %
304 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
306 % CloneStringInfo() clones a copy of the StringInfo structure.
308 % The format of the CloneStringInfo method is:
310 % StringInfo *CloneStringInfo(const StringInfo *string_info)
312 % A description of each parameter follows:
314 % o string_info: the string info.
317 MagickExport StringInfo *CloneStringInfo(const StringInfo *string_info)
322 assert(string_info != (StringInfo *) NULL);
323 assert(string_info->signature == MagickSignature);
324 clone_info=AcquireStringInfo(string_info->length);
325 if (string_info->length != 0)
326 (void) memcpy(clone_info->datum,string_info->datum,string_info->length+1);
331 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
335 % C o m p a r e S t r i n g I n f o %
339 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
341 % CompareStringInfo() compares the two datums target and source. It returns
342 % an integer less than, equal to, or greater than zero if target is found,
343 % respectively, to be less than, to match, or be greater than source.
345 % The format of the CompareStringInfo method is:
347 % int CompareStringInfo(const StringInfo *target,const StringInfo *source)
349 % A description of each parameter follows:
351 % o target: the target string.
353 % o source: the source string.
357 static inline size_t MagickMin(const size_t x,const size_t y)
364 MagickExport int CompareStringInfo(const StringInfo *target,
365 const StringInfo *source)
370 assert(target != (StringInfo *) NULL);
371 assert(target->signature == MagickSignature);
372 assert(source != (StringInfo *) NULL);
373 assert(source->signature == MagickSignature);
374 status=memcmp(target->datum,source->datum,MagickMin(target->length,
378 if (target->length == source->length)
380 return(target->length < source->length ? -1 : 1);
384 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
388 % C o n c a t e n a t e M a g i c k S t r i n g %
392 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
394 % ConcatenateMagickString() concatenates the source string to the destination
395 % string. The destination buffer is always null-terminated even if the
396 % string must be truncated.
398 % The format of the ConcatenateMagickString method is:
400 % size_t ConcatenateMagickString(char *destination,const char *source,
401 % const size_t length)
403 % A description of each parameter follows:
405 % o destination: the destination string.
407 % o source: the source string.
409 % o length: the length of the destination string.
412 MagickExport size_t ConcatenateMagickString(char *destination,
413 const char *source,const size_t length)
427 assert(destination != (char *) NULL);
428 assert(source != (const char *) NULL);
433 while ((i-- != 0) && (*q != '\0'))
435 count=(size_t) (q-destination);
438 return(count+strlen(p));
449 return(count+(p-source));
453 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
457 % C o n c a t e n a t e S t r i n g %
461 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
463 % ConcatenateString() appends a copy of string source, including the
464 % terminating null character, to the end of string destination.
466 % The format of the ConcatenateString method is:
468 % MagickBooleanType ConcatenateString(char **destination,
469 % const char *source)
471 % A description of each parameter follows:
473 % o destination: A pointer to a character string.
475 % o source: A character string.
478 MagickExport MagickBooleanType ConcatenateString(char **destination,
486 assert(destination != (char **) NULL);
487 if (source == (const char *) NULL)
489 if (*destination == (char *) NULL)
491 *destination=AcquireString(source);
494 destination_length=strlen(*destination);
495 source_length=strlen(source);
496 length=destination_length;
497 if (~length < source_length)
498 ThrowFatalException(ResourceLimitFatalError,"UnableToConcatenateString");
499 length+=source_length;
500 if (~length < MaxTextExtent)
501 ThrowFatalException(ResourceLimitFatalError,"UnableToConcatenateString");
502 *destination=(char *) ResizeQuantumMemory(*destination,length+MaxTextExtent,
503 sizeof(**destination));
504 if (*destination == (char *) NULL)
505 ThrowFatalException(ResourceLimitFatalError,"UnableToConcatenateString");
506 if (source_length != 0)
507 (void) memcpy((*destination)+destination_length,source,source_length);
508 (*destination)[length]='\0';
513 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
517 % C o n c a t e n a t e S t r i n g I n f o %
521 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
523 % ConcatenateStringInfo() concatenates the source string to the destination
526 % The format of the ConcatenateStringInfo method is:
528 % void ConcatenateStringInfo(StringInfo *string_info,
529 % const StringInfo *source)
531 % A description of each parameter follows:
533 % o string_info: the string info.
535 % o source: the source string.
538 MagickExport void ConcatenateStringInfo(StringInfo *string_info,
539 const StringInfo *source)
544 assert(string_info != (StringInfo *) NULL);
545 assert(string_info->signature == MagickSignature);
546 assert(source != (const StringInfo *) NULL);
547 length=string_info->length;
548 if (~length < source->length)
549 ThrowFatalException(ResourceLimitFatalError,"UnableToConcatenateString");
550 SetStringInfoLength(string_info,length+source->length);
551 (void) memcpy(string_info->datum+length,source->datum,source->length);
555 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
559 % 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 %
563 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
565 % ConfigureFileToStringInfo() returns the contents of a configure file as a
568 % The format of the ConfigureFileToStringInfo method is:
570 % StringInfo *ConfigureFileToStringInfo(const char *filename)
571 % ExceptionInfo *exception)
573 % A description of each parameter follows:
575 % o filename: the filename.
578 MagickExport StringInfo *ConfigureFileToStringInfo(const char *filename)
598 assert(filename != (const char *) NULL);
599 file=open_utf8(filename,O_RDONLY | O_BINARY,0);
601 return((StringInfo *) NULL);
602 offset=(MagickOffsetType) lseek(file,0,SEEK_END);
603 if ((offset < 0) || (offset != (MagickOffsetType) ((ssize_t) offset)))
606 return((StringInfo *) NULL);
608 length=(size_t) offset;
609 string=(char *) NULL;
610 if (~length >= (MaxTextExtent-1))
611 string=(char *) AcquireQuantumMemory(length+MaxTextExtent,sizeof(*string));
612 if (string == (char *) NULL)
615 return((StringInfo *) NULL);
617 map=MapBlob(file,ReadMode,0,length);
618 if (map != (void *) NULL)
620 (void) memcpy(string,map,length);
621 (void) UnmapBlob(map,length);
631 (void) lseek(file,0,SEEK_SET);
632 for (i=0; i < length; i+=count)
634 count=read(file,string+i,(size_t) MagickMin(length-i,(size_t)
646 string=DestroyString(string);
647 return((StringInfo *) NULL);
652 string_info=AcquireStringInfo(0);
653 (void) CopyMagickString(string_info->path,filename,MaxTextExtent);
654 string_info->length=length;
655 string_info->datum=(unsigned char *) string;
660 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
664 % C o n s t a n t S t r i n g %
668 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
670 % ConstantString() allocates exactly the needed memory for a string and
671 % copies the source string to that memory location. A NULL string pointer
672 % will allocate an empty string containing just the NUL character.
674 % When finished the string should be freed using DestoryString()
676 % The format of the ConstantString method is:
678 % char *ConstantString(const char *source)
680 % A description of each parameter follows:
682 % o source: A character string.
685 MagickExport char *ConstantString(const char *source)
694 if (source != (char *) NULL)
695 length+=strlen(source);
696 destination=(char *) NULL;
698 destination=(char *) AcquireQuantumMemory(length+1UL,sizeof(*destination));
699 if (destination == (char *) NULL)
700 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
702 if (source != (char *) NULL)
703 (void) memcpy(destination,source,length*sizeof(*destination));
704 destination[length]='\0';
709 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
713 % C o p y M a g i c k S t r i n g %
717 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
719 % CopyMagickString() copies the source string to the destination string, with
720 % out exceeding the given pre-declared length.
722 % The destination buffer is always null-terminated even if the string must be
723 % truncated. The return value is the minimum of the source string length or
724 % the length parameter.
726 % The format of the CopyMagickString method is:
728 % size_t CopyMagickString(const char *destination,char *source,
729 % const size_t length)
731 % A description of each parameter follows:
733 % o destination: the destination string.
735 % o source: the source string.
737 % o length: the length of the destination string.
740 MagickExport size_t CopyMagickString(char *destination,const char *source,
752 if (source == (const char *) NULL)
756 for (n=length; n > 4; n-=4)
760 return((size_t) (p-source-1));
764 return((size_t) (p-source-1));
768 return((size_t) (p-source-1));
772 return((size_t) (p-source-1));
776 for (n--; n != 0; n--)
780 return((size_t) (p-source-1));
785 return((size_t) (p-source-1));
789 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
793 % D e s t r o y S t r i n g %
797 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
799 % DestroyString() destroys memory associated with a string.
801 % The format of the DestroyString method is:
803 % char *DestroyString(char *string)
805 % A description of each parameter follows:
807 % o string: the string.
810 MagickExport char *DestroyString(char *string)
812 return((char *) RelinquishMagickMemory(string));
816 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
820 % D e s t r o y S t r i n g I n f o %
824 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
826 % DestroyStringInfo() destroys memory associated with the StringInfo structure.
828 % The format of the DestroyStringInfo method is:
830 % StringInfo *DestroyStringInfo(StringInfo *string_info)
832 % A description of each parameter follows:
834 % o string_info: the string info.
837 MagickExport StringInfo *DestroyStringInfo(StringInfo *string_info)
839 assert(string_info != (StringInfo *) NULL);
840 assert(string_info->signature == MagickSignature);
841 if (string_info->datum != (unsigned char *) NULL)
842 string_info->datum=(unsigned char *) RelinquishMagickMemory(
844 string_info->signature=(~MagickSignature);
845 string_info=(StringInfo *) RelinquishMagickMemory(string_info);
850 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
854 % D e s t r o y S t r i n g L i s t %
858 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
860 % DestroyStringList() zeros memory associated with a string list.
862 % The format of the DestroyStringList method is:
864 % char **DestroyStringList(char **list)
866 % A description of each parameter follows:
868 % o list: the string list.
871 MagickExport char **DestroyStringList(char **list)
876 assert(list != (char **) NULL);
877 for (i=0; list[i] != (char *) NULL; i++)
878 list[i]=DestroyString(list[i]);
879 list=(char **) RelinquishMagickMemory(list);
884 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
888 % E s c a p e S t r i n g %
892 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
894 % EscapeString() allocates memory for a backslash-escaped version of a
895 % source text string, copies the escaped version of the text to that
896 % memory location while adding backslash characters, and returns the
899 % The format of the EscapeString method is:
901 % char *EscapeString(const char *source,const char escape)
903 % A description of each parameter follows:
905 % o allocate_string: Method EscapeString returns the escaped string.
907 % o source: A character string.
909 % o escape: the quoted string termination character to escape (e.g. '"').
912 MagickExport char *EscapeString(const char *source,const char escape)
926 assert(source != (const char *) NULL);
927 length=strlen(source);
928 for (p=source; *p != '\0'; p++)
929 if ((*p == '\\') || (*p == escape))
932 ThrowFatalException(ResourceLimitFatalError,"UnableToEscapeString");
935 destination=(char *) NULL;
936 if (~length >= (MaxTextExtent-1))
937 destination=(char *) AcquireQuantumMemory(length+MaxTextExtent,
938 sizeof(*destination));
939 if (destination == (char *) NULL)
940 ThrowFatalException(ResourceLimitFatalError,"UnableToEscapeString");
942 if (source != (char *) NULL)
945 for (p=source; *p != '\0'; p++)
947 if ((*p == '\\') || (*p == escape))
957 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
961 % F i l e T o S t r i n g %
965 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
967 % FileToString() returns the contents of a file as a string.
969 % The format of the FileToString method is:
971 % char *FileToString(const char *filename,const size_t extent,
972 % ExceptionInfo *exception)
974 % A description of each parameter follows:
976 % o filename: the filename.
978 % o extent: Maximum length of the string.
980 % o exception: return any errors or warnings in this structure.
983 MagickExport char *FileToString(const char *filename,const size_t extent,
984 ExceptionInfo *exception)
989 assert(filename != (const char *) NULL);
990 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
991 assert(exception != (ExceptionInfo *) NULL);
992 return((char *) FileToBlob(filename,extent,&length,exception));
996 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1000 % F i l e T o S t r i n g I n f o %
1004 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1006 % FileToStringInfo() returns the contents of a file as a string.
1008 % The format of the FileToStringInfo method is:
1010 % StringInfo *FileToStringInfo(const char *filename,const size_t extent,
1011 % ExceptionInfo *exception)
1013 % A description of each parameter follows:
1015 % o filename: the filename.
1017 % o extent: Maximum length of the string.
1019 % o exception: return any errors or warnings in this structure.
1022 MagickExport StringInfo *FileToStringInfo(const char *filename,
1023 const size_t extent,ExceptionInfo *exception)
1028 assert(filename != (const char *) NULL);
1029 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
1030 assert(exception != (ExceptionInfo *) NULL);
1031 string_info=AcquireStringInfo(0);
1032 (void) CopyMagickString(string_info->path,filename,MaxTextExtent);
1033 string_info->datum=FileToBlob(filename,extent,&string_info->length,exception);
1034 if (string_info->datum == (unsigned char *) NULL)
1036 string_info=DestroyStringInfo(string_info);
1037 return((StringInfo *) NULL);
1039 return(string_info);
1043 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1047 % F o r m a t M a g i c k S i z e %
1051 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1053 % FormatMagickSize() converts a size to a human readable format, for example,
1054 % 14k, 234m, 2.7g, or 3.0t. Scaling is done by repetitively dividing by
1057 % The format of the FormatMagickSize method is:
1059 % ssize_t FormatMagickSize(const MagickSizeType size,char *format)
1061 % A description of each parameter follows:
1063 % o size: convert this size to a human readable format.
1065 % o bi: use power of two rather than power of ten.
1067 % o format: human readable format.
1070 MagickExport ssize_t FormatMagickSize(const MagickSizeType size,
1071 const MagickBooleanType bi,char *format)
1090 "", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi", (char *) NULL
1092 *traditional_units[] =
1094 "", "K", "M", "G", "T", "P", "E", "Z", "Y", (char *) NULL
1098 units=traditional_units;
1099 if (bi != MagickFalse)
1104 #if defined(_MSC_VER) && (_MSC_VER == 1200)
1105 length=(double) ((MagickOffsetType) size);
1107 length=(double) size;
1109 for (i=0; (length >= bytes) && (units[i+1] != (const char *) NULL); i++)
1112 for (j=2; j < 12; j++)
1114 count=FormatLocaleString(format,MaxTextExtent,"%.*g%sB",(int) (i+j),length,
1116 if (strchr(format,'+') == (char *) NULL)
1123 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1127 % F o r m a t M a g i c k T i m e %
1131 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1133 % FormatMagickTime() returns the specified time in the Internet date/time
1134 % format and the length of the timestamp.
1136 % The format of the FormatMagickTime method is:
1138 % ssize_t FormatMagickTime(const time_t time,const size_t length,
1141 % A description of each parameter follows.
1143 % o time: the time since the Epoch (00:00:00 UTC, January 1, 1970),
1144 % measured in seconds.
1146 % o length: the maximum length of the string.
1148 % o timestamp: Return the Internet date/time here.
1151 MagickExport ssize_t FormatMagickTime(const time_t time,const size_t length,
1164 assert(timestamp != (char *) NULL);
1165 (void) ResetMagickMemory(&local_time,0,sizeof(local_time));
1166 (void) ResetMagickMemory(&gm_time,0,sizeof(gm_time));
1167 #if defined(MAGICKCORE_HAVE_LOCALTIME_R)
1168 (void) localtime_r(&time,&local_time);
1174 my_time=localtime(&time);
1175 if (my_time != (struct tm *) NULL)
1176 (void) memcpy(&local_time,my_time,sizeof(local_time));
1179 #if defined(MAGICKCORE_HAVE_GMTIME_R)
1180 (void) gmtime_r(&time,&gm_time);
1186 my_time=gmtime(&time);
1187 if (my_time != (struct tm *) NULL)
1188 (void) memcpy(&gm_time,my_time,sizeof(gm_time));
1191 timezone=(time_t) ((local_time.tm_min-gm_time.tm_min)/60+
1192 local_time.tm_hour-gm_time.tm_hour+24*((local_time.tm_year-
1193 gm_time.tm_year) != 0 ? (local_time.tm_year-gm_time.tm_year) :
1194 (local_time.tm_yday-gm_time.tm_yday)));
1195 count=FormatLocaleString(timestamp,length,
1196 "%04d-%02d-%02dT%02d:%02d:%02d%+03ld:00",local_time.tm_year+1900,
1197 local_time.tm_mon+1,local_time.tm_mday,local_time.tm_hour,
1198 local_time.tm_min,local_time.tm_sec,(long) timezone);
1203 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1207 % G e t E n v i r o n m e n t V a l u e %
1211 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1213 % GetEnvironmentValue() returns the environment string that matches the
1216 % The format of the GetEnvironmentValue method is:
1218 % char *GetEnvironmentValue(const char *name)
1220 % A description of each parameter follows:
1222 % o name: the environment name.
1225 MagickExport char *GetEnvironmentValue(const char *name)
1230 environment=getenv(name);
1231 if (environment == (const char *) NULL)
1232 return((char *) NULL);
1233 return(ConstantString(environment));
1237 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1241 % G e t S t r i n g I n f o D a t u m %
1245 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1247 % GetStringInfoDatum() returns the datum associated with the string.
1249 % The format of the GetStringInfoDatum method is:
1251 % unsigned char *GetStringInfoDatum(const StringInfo *string_info)
1253 % A description of each parameter follows:
1255 % o string_info: the string info.
1258 MagickExport unsigned char *GetStringInfoDatum(const StringInfo *string_info)
1260 assert(string_info != (StringInfo *) NULL);
1261 assert(string_info->signature == MagickSignature);
1262 return(string_info->datum);
1266 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1270 % G e t S t r i n g I n f o L e n g t h %
1274 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1276 % GetStringInfoLength() returns the string length.
1278 % The format of the GetStringInfoLength method is:
1280 % size_t GetStringInfoLength(const StringInfo *string_info)
1282 % A description of each parameter follows:
1284 % o string_info: the string info.
1287 MagickExport size_t GetStringInfoLength(const StringInfo *string_info)
1289 assert(string_info != (StringInfo *) NULL);
1290 assert(string_info->signature == MagickSignature);
1291 return(string_info->length);
1295 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1299 % G e t S t r i n g I n f o P a t h %
1303 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1305 % GetStringInfoPath() returns the path associated with the string.
1307 % The format of the GetStringInfoPath method is:
1309 % const char *GetStringInfoPath(const StringInfo *string_info)
1311 % A description of each parameter follows:
1313 % o string_info: the string info.
1316 MagickExport const char *GetStringInfoPath(const StringInfo *string_info)
1318 assert(string_info != (StringInfo *) NULL);
1319 assert(string_info->signature == MagickSignature);
1320 return(string_info->path);
1324 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1328 + I n t e r p r e t S i P r e f i x V a l u e %
1332 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1334 % InterpretSiPrefixValue() converts the initial portion of the string to a
1335 % double representation. It also recognizes SI prefixes (e.g. B, KB, MiB,
1338 % The format of the InterpretSiPrefixValue method is:
1340 % double InterpretSiPrefixValue(const char *value,char **sentinal)
1342 % A description of each parameter follows:
1344 % o value: the string value.
1346 % o sentinal: if sentinal is not NULL, return a pointer to the character
1347 % after the last character used in the conversion.
1350 MagickExport double InterpretSiPrefixValue(const char *restrict string,
1351 char **restrict sentinal)
1359 value=InterpretLocaleValue(string,&q);
1362 if ((*q >= 'E') && (*q <= 'z'))
1367 switch ((int) ((unsigned char) *q))
1369 case 'y': e=(-24.0); break;
1370 case 'z': e=(-21.0); break;
1371 case 'a': e=(-18.0); break;
1372 case 'f': e=(-15.0); break;
1373 case 'p': e=(-12.0); break;
1374 case 'n': e=(-9.0); break;
1375 case 'u': e=(-6.0); break;
1376 case 'm': e=(-3.0); break;
1377 case 'c': e=(-2.0); break;
1378 case 'd': e=(-1.0); break;
1379 case 'h': e=2.0; break;
1380 case 'k': e=3.0; break;
1381 case 'K': e=3.0; break;
1382 case 'M': e=6.0; break;
1383 case 'G': e=9.0; break;
1384 case 'T': e=12.0; break;
1385 case 'P': e=15.0; break;
1386 case 'E': e=18.0; break;
1387 case 'Z': e=21.0; break;
1388 case 'Y': e=24.0; break;
1389 default: e=0.0; break;
1391 if (e >= MagickEpsilon)
1395 value*=pow(2.0,e/0.3);
1408 if (sentinal != (char **) NULL)
1414 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1418 % I s S t r i n g T r u e %
1422 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1424 % IsStringTrue() returns MagickTrue if the value is "true", "on", "yes" or
1425 % "1". Any other string or undefined returns MagickFalse.
1427 % Typically this is used to look at strings (options or artifacts) which
1428 % has a default value of "false", when not defined.
1430 % The format of the IsStringTrue method is:
1432 % MagickBooleanType IsStringTrue(const char *value)
1434 % A description of each parameter follows:
1436 % o value: Specifies a pointer to a character array.
1439 MagickExport MagickBooleanType IsStringTrue(const char *value)
1441 if (value == (const char *) NULL)
1442 return(MagickFalse);
1443 if (LocaleCompare(value,"true") == 0)
1445 if (LocaleCompare(value,"on") == 0)
1447 if (LocaleCompare(value,"yes") == 0)
1449 if (LocaleCompare(value,"1") == 0)
1451 return(MagickFalse);
1455 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1459 % I s S t r i n g N o t F a l s e %
1463 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1465 % IsStringNotFalse() returns MagickTrue, unless the string specifically
1466 % has a value that makes this false. that is if it has a value of
1467 % "false", "off", "no" or "0".
1469 % Typically this is used to look at strings (options or artifacts) which
1470 % has a default value of "true", when it has not been defined.
1472 % The format of the IsStringNotFalse method is:
1474 % MagickBooleanType IsStringNotFalse(const char *value)
1476 % A description of each parameter follows:
1478 % o value: Specifies a pointer to a character array.
1481 MagickExport MagickBooleanType IsStringNotFalse(const char *value)
1483 if (value == (const char *) NULL)
1485 if (LocaleCompare(value,"false") == 0)
1486 return(MagickFalse);
1487 if (LocaleCompare(value,"off") == 0)
1488 return(MagickFalse);
1489 if (LocaleCompare(value,"no") == 0)
1490 return(MagickFalse);
1491 if (LocaleCompare(value,"0") == 0)
1492 return(MagickFalse);
1497 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1501 % L o c a l e C o m p a r e %
1505 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1507 % LocaleCompare() performs a case-insensitive comparison of two strings
1508 % byte-by-byte, according to the ordering of the current locale encoding.
1509 % LocaleCompare returns an integer greater than, equal to, or less than 0,
1510 % if the string pointed to by p is greater than, equal to, or less than the
1511 % string pointed to by q respectively. The sign of a non-zero return value
1512 % is determined by the sign of the difference between the values of the first
1513 % pair of bytes that differ in the strings being compared.
1515 % The format of the LocaleCompare method is:
1517 % int LocaleCompare(const char *p,const char *q)
1519 % A description of each parameter follows:
1521 % o p: A pointer to a character string.
1523 % o q: A pointer to a character string to compare to p.
1526 MagickExport int LocaleCompare(const char *p,const char *q)
1528 if ((p == (char *) NULL) && (q == (char *) NULL))
1530 if (p == (char *) NULL)
1532 if (q == (char *) NULL)
1534 #if defined(MAGICKCORE_HAVE_STRCASECMP)
1535 return(strcasecmp(p,q));
1544 c=(int) *((unsigned char *) p);
1545 d=(int) *((unsigned char *) q);
1546 if ((c == 0) || (AsciiMap[c] != AsciiMap[d]))
1551 return(AsciiMap[c]-(int) AsciiMap[d]);
1557 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1561 % L o c a l e L o w e r %
1565 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1567 % LocaleLower() transforms all of the characters in the supplied
1568 % null-terminated string, changing all uppercase letters to lowercase.
1570 % The format of the LocaleLower method is:
1572 % void LocaleLower(char *string)
1574 % A description of each parameter follows:
1576 % o string: A pointer to the string to convert to lower-case Locale.
1579 MagickExport void LocaleLower(char *string)
1584 assert(string != (char *) NULL);
1585 for (q=string; *q != '\0'; q++)
1586 *q=(char) tolower((int) *q);
1590 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1594 % L o c a l e N C o m p a r e %
1598 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1600 % LocaleNCompare() performs a case-insensitive comparison of two strings
1601 % byte-by-byte, according to the ordering of the current locale encoding.
1603 % LocaleNCompare returns an integer greater than, equal to, or less than 0,
1604 % if the string pointed to by p is greater than, equal to, or less than the
1605 % string pointed to by q respectively. The sign of a non-zero return value
1606 % is determined by the sign of the difference between the values of the first
1607 % pair of bytes that differ in the strings being compared.
1609 % The LocaleNCompare method makes the same comparison as LocaleCompare but
1610 % looks at a maximum of n bytes. Bytes following a null byte are not
1613 % The format of the LocaleNCompare method is:
1615 % int LocaleNCompare(const char *p,const char *q,const size_t n)
1617 % A description of each parameter follows:
1619 % o p: A pointer to a character string.
1621 % o q: A pointer to a character string to compare to p.
1623 % o length: the number of characters to compare in strings p and q.
1626 MagickExport int LocaleNCompare(const char *p,const char *q,const size_t length)
1628 if ((p == (char *) NULL) && (q == (char *) NULL))
1630 if (p == (char *) NULL)
1632 if (q == (char *) NULL)
1634 #if defined(MAGICKCORE_HAVE_STRNCASECMP)
1635 return(strncasecmp(p,q,length));
1645 for (i=length; i != 0; i--)
1647 c=(int) *((unsigned char *) p);
1648 d=(int) *((unsigned char *) q);
1649 if (AsciiMap[c] != AsciiMap[d])
1650 return(AsciiMap[c]-(int) AsciiMap[d]);
1662 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1666 % L o c a l e U p p e r %
1670 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1672 % LocaleUpper() transforms all of the characters in the supplied
1673 % null-terminated string, changing all lowercase letters to uppercase.
1675 % The format of the LocaleUpper method is:
1677 % void LocaleUpper(char *string)
1679 % A description of each parameter follows:
1681 % o string: A pointer to the string to convert to upper-case Locale.
1684 MagickExport void LocaleUpper(char *string)
1689 assert(string != (char *) NULL);
1690 for (q=string; *q != '\0'; q++)
1691 *q=(char) toupper((int) *q);
1695 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1699 % P r i n t S t r i n g I n f o %
1703 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1705 % PrintStringInfo() prints the string.
1707 % The format of the PrintStringInfo method is:
1709 % void PrintStringInfo(FILE *file,const char *id,
1710 % const StringInfo *string_info)
1712 % A description of each parameter follows:
1714 % o file: the file, typically stdout.
1716 % o id: the string id.
1718 % o string_info: the string info.
1721 MagickExport void PrintStringInfo(FILE *file,const char *id,
1722 const StringInfo *string_info)
1731 assert(id != (const char *) NULL);
1732 assert(string_info != (StringInfo *) NULL);
1733 assert(string_info->signature == MagickSignature);
1734 p=(char *) string_info->datum;
1735 for (i=0; i < string_info->length; i++)
1737 if (((int) ((unsigned char) *p) < 32) &&
1738 (isspace((int) ((unsigned char) *p)) == 0))
1742 (void) FormatLocaleFile(file,"%s(%.20g): ",id,(double) string_info->length);
1743 if (i == string_info->length)
1745 for (i=0; i < string_info->length; i++)
1746 (void) fputc(string_info->datum[i],file);
1747 (void) fputc('\n',file);
1751 Convert string to a HEX list.
1753 p=(char *) string_info->datum;
1754 for (i=0; i < string_info->length; i+=0x14)
1756 (void) FormatLocaleFile(file,"0x%08lx: ",(unsigned long) (0x14*i));
1757 for (j=1; j <= MagickMin(string_info->length-i,0x14); j++)
1759 (void) FormatLocaleFile(file,"%02lx",(unsigned long) (*(p+j)) & 0xff);
1760 if ((j % 0x04) == 0)
1761 (void) fputc(' ',file);
1763 for ( ; j <= 0x14; j++)
1765 (void) fputc(' ',file);
1766 (void) fputc(' ',file);
1767 if ((j % 0x04) == 0)
1768 (void) fputc(' ',file);
1770 (void) fputc(' ',file);
1771 for (j=1; j <= MagickMin(string_info->length-i,0x14); j++)
1773 if (isprint((int) ((unsigned char) *p)) != 0)
1774 (void) fputc(*p,file);
1776 (void) fputc('-',file);
1779 (void) fputc('\n',file);
1784 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1788 % R e s e t S t r i n g I n f o %
1792 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1794 % ResetStringInfo() reset the string to all null bytes.
1796 % The format of the ResetStringInfo method is:
1798 % void ResetStringInfo(StringInfo *string_info)
1800 % A description of each parameter follows:
1802 % o string_info: the string info.
1805 MagickExport void ResetStringInfo(StringInfo *string_info)
1807 assert(string_info != (StringInfo *) NULL);
1808 assert(string_info->signature == MagickSignature);
1809 (void) ResetMagickMemory(string_info->datum,0,string_info->length);
1813 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1817 % S e t S t r i n g I n f o %
1821 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1823 % SetStringInfo() copies the source string to the destination string.
1825 % The format of the SetStringInfo method is:
1827 % void SetStringInfo(StringInfo *string_info,const StringInfo *source)
1829 % A description of each parameter follows:
1831 % o string_info: the string info.
1833 % o source: the source string.
1836 MagickExport void SetStringInfo(StringInfo *string_info,
1837 const StringInfo *source)
1839 assert(string_info != (StringInfo *) NULL);
1840 assert(string_info->signature == MagickSignature);
1841 assert(source != (StringInfo *) NULL);
1842 assert(source->signature == MagickSignature);
1843 if (string_info->length == 0)
1845 (void) ResetMagickMemory(string_info->datum,0,string_info->length);
1846 (void) memcpy(string_info->datum,source->datum,MagickMin(string_info->length,
1851 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1855 % S e t S t r i n g I n f o D a t u m %
1859 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1861 % SetStringInfoDatum() copies bytes from the source string for the length of
1862 % the destination string.
1864 % The format of the SetStringInfoDatum method is:
1866 % void SetStringInfoDatum(StringInfo *string_info,
1867 % const unsigned char *source)
1869 % A description of each parameter follows:
1871 % o string_info: the string info.
1873 % o source: the source string.
1876 MagickExport void SetStringInfoDatum(StringInfo *string_info,
1877 const unsigned char *source)
1879 assert(string_info != (StringInfo *) NULL);
1880 assert(string_info->signature == MagickSignature);
1881 if (string_info->length != 0)
1882 (void) memcpy(string_info->datum,source,string_info->length);
1886 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1890 % S e t S t r i n g I n f o L e n g t h %
1894 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1896 % SetStringInfoLength() set the string length to the specified value.
1898 % The format of the SetStringInfoLength method is:
1900 % void SetStringInfoLength(StringInfo *string_info,const size_t length)
1902 % A description of each parameter follows:
1904 % o string_info: the string info.
1906 % o length: the string length.
1909 MagickExport void SetStringInfoLength(StringInfo *string_info,
1910 const size_t length)
1912 assert(string_info != (StringInfo *) NULL);
1913 assert(string_info->signature == MagickSignature);
1914 if (~length < MaxTextExtent)
1915 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1916 string_info->length=length;
1917 if (string_info->datum == (unsigned char *) NULL)
1918 string_info->datum=(unsigned char *) AcquireQuantumMemory(length+
1919 MaxTextExtent,sizeof(*string_info->datum));
1921 string_info->datum=(unsigned char *) ResizeQuantumMemory(string_info->datum,
1922 length+MaxTextExtent,sizeof(*string_info->datum));
1923 if (string_info->datum == (unsigned char *) NULL)
1924 ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
1928 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1932 % S e t S t r i n g I n f o D a t u m %
1936 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1938 % SetStringInfoPath() sets the path associated with the string.
1940 % The format of the SetStringInfoPath method is:
1942 % void SetStringInfoPath(StringInfo *string_info,const char *path)
1944 % A description of each parameter follows:
1946 % o string_info: the string info.
1951 MagickExport void SetStringInfoPath(StringInfo *string_info,const char *path)
1953 assert(string_info != (StringInfo *) NULL);
1954 assert(string_info->signature == MagickSignature);
1955 assert(path != (const char *) NULL);
1956 (void) CopyMagickString(string_info->path,path,MaxTextExtent);
1960 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1964 % S p l i t S t r i n g I n f o %
1968 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
1970 % SplitStringInfo() splits a string into two and returns it.
1972 % The format of the SplitStringInfo method is:
1974 % StringInfo *SplitStringInfo(StringInfo *string_info,const size_t offset)
1976 % A description of each parameter follows:
1978 % o string_info: the string info.
1981 MagickExport StringInfo *SplitStringInfo(StringInfo *string_info,
1982 const size_t offset)
1987 assert(string_info != (StringInfo *) NULL);
1988 assert(string_info->signature == MagickSignature);
1989 if (offset > string_info->length)
1990 return((StringInfo *) NULL);
1991 split_info=AcquireStringInfo(offset);
1992 SetStringInfo(split_info,string_info);
1993 (void) memmove(string_info->datum,string_info->datum+offset,
1994 string_info->length-offset+MaxTextExtent);
1995 SetStringInfoLength(string_info,string_info->length-offset);
2000 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2004 % S t r i n g I n f o T o S t r i n g %
2008 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2010 % StringInfoToString() converts a string info string to a C string.
2012 % The format of the StringInfoToString method is:
2014 % char *StringInfoToString(const StringInfo *string_info)
2016 % A description of each parameter follows:
2018 % o string_info: the string.
2021 MagickExport char *StringInfoToString(const StringInfo *string_info)
2029 string=(char *) NULL;
2030 length=string_info->length;
2031 if (~length >= (MaxTextExtent-1))
2032 string=(char *) AcquireQuantumMemory(length+MaxTextExtent,sizeof(*string));
2033 if (string == (char *) NULL)
2034 return((char *) NULL);
2035 (void) memcpy(string,(char *) string_info->datum,length*sizeof(*string));
2036 string[length]='\0';
2041 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2045 % S t r i n g I n f o T o H e x S t r i n g %
2049 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2051 % StringInfoToHexString() converts a string info string to a C string.
2053 % The format of the StringInfoToHexString method is:
2055 % char *StringInfoToHexString(const StringInfo *string_info)
2057 % A description of each parameter follows:
2059 % o string_info: the string.
2062 MagickExport char *StringInfoToHexString(const StringInfo *string_info)
2067 register const unsigned char
2073 register unsigned char
2082 length=string_info->length;
2083 if (~length < MaxTextExtent)
2084 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
2085 string=(char *) AcquireQuantumMemory(length+MaxTextExtent,2*sizeof(*string));
2086 if (string == (char *) NULL)
2087 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
2104 p=string_info->datum;
2105 q=(unsigned char *) string;
2106 for (i=0; i < (ssize_t) string_info->length; i++)
2108 *q++=hex_digits[(*p >> 4) & 0x0f];
2109 *q++=hex_digits[*p & 0x0f];
2117 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2121 % S t r i n g T o A r g v %
2125 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2127 % StringToArgv() converts a text string into command line arguments.
2128 % The 'argv' array of arguments, is returned while the number of arguments
2129 % is returned via the provided integer variable pointer.
2131 % Simple 'word' tokenizer, which allows for each word to be optionally
2132 % quoted. However it will not allow use of partial quotes, or escape
2135 % The format of the StringToArgv method is:
2137 % char **StringToArgv(const char *text,int *argc)
2139 % A description of each parameter follows:
2141 % o argv: Method StringToArgv returns the string list unless an error
2142 % occurs, otherwise NULL.
2144 % o text: Specifies the string to segment into a list.
2146 % o argc: This integer pointer returns the number of arguments in the
2150 MagickExport char **StringToArgv(const char *text,int *argc)
2163 if (text == (char *) NULL)
2164 return((char **) NULL);
2166 Determine the number of arguments.
2168 for (p=text; *p != '\0'; )
2170 while (isspace((int) ((unsigned char) *p)) != 0)
2176 for (p++; (*p != '"') && (*p != '\0'); p++) ;
2178 for (p++; (*p != '\'') && (*p != '\0'); p++) ;
2179 while ((isspace((int) ((unsigned char) *p)) == 0) && (*p != '\0'))
2183 argv=(char **) AcquireQuantumMemory((size_t) (*argc+1UL),sizeof(*argv));
2184 if (argv == (char **) NULL)
2185 ThrowFatalException(ResourceLimitFatalError,"UnableToConvertStringToARGV");
2187 Convert string to an ASCII list.
2189 argv[0]=AcquireString("magick");
2191 for (i=1; i < (ssize_t) *argc; i++)
2193 while (isspace((int) ((unsigned char) *p)) != 0)
2199 for (q++; (*q != '"') && (*q != '\0'); q++) ;
2205 for (q++; (*q != '\'') && (*q != '\0'); q++) ;
2208 while ((isspace((int) ((unsigned char) *q)) == 0) && (*q != '\0'))
2210 argv[i]=(char *) AcquireQuantumMemory((size_t) (q-p)+MaxTextExtent,
2212 if (argv[i] == (char *) NULL)
2214 for (i--; i >= 0; i--)
2215 argv[i]=DestroyString(argv[i]);
2216 argv=(char **) RelinquishMagickMemory(argv);
2217 ThrowFatalException(ResourceLimitFatalError,
2218 "UnableToConvertStringToARGV");
2220 (void) memcpy(argv[i],p,(size_t) (q-p));
2223 while ((isspace((int) ((unsigned char) *p)) == 0) && (*p != '\0'))
2226 argv[i]=(char *) NULL;
2231 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2235 % S t r i n g T o A r r a y O f D o u b l e s %
2239 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2241 % StringToArrayOfDoubles() converts a string of space or comma separated
2242 % numbers into array of floating point numbers (doubles). Any number that
2243 % failes to parse properly will produce a syntax error. As will two commas
2244 % without a number between them. However a final comma at the end will
2245 % not be regarded as an error so as to simplify automatic list generation.
2247 % A NULL value is returned on syntax or memory errors.
2249 % Use RelinquishMagickMemory() to free returned array when finished.
2251 % The format of the StringToArrayOfDoubles method is:
2253 % double *StringToArrayOfDoubles(const char *string,size_t *count)
2255 % A description of each parameter follows:
2257 % o string: the string containing the comma/space separated values.
2259 % o count: returns number of arguments in returned array
2261 % o exception: return any errors or warnings in this structure.
2264 MagickExport double *StringToArrayOfDoubles(const char *string,ssize_t *count,
2265 ExceptionInfo *exception)
2280 Determine count of values, and check syntax.
2282 assert(exception != (ExceptionInfo *) NULL);
2283 assert(exception->signature == MagickSignature);
2289 (void) StringToDouble(p,&q); /* get value - ignores leading space */
2291 return((double *) NULL); /* no value found */
2293 i++; /* increment value count */
2294 while (isspace((int) ((unsigned char) *p)) != 0)
2295 p++; /* skip spaces */
2297 p++; /* skip comma */
2298 while (isspace((int) ((unsigned char) *p)) != 0)
2299 p++; /* and more spaces */
2302 Allocate floating point argument list.
2305 array=(double *) AcquireQuantumMemory((size_t) i,sizeof(*array));
2306 if (array == (double *) NULL)
2308 (void) ThrowMagickException(exception,GetMagickModule(),
2309 ResourceLimitError,"MemoryAllocationFailed","`%s'","");
2310 return((double *) NULL);
2313 Fill in the floating point values.
2317 while ((*p != '\0') && (i < *count))
2319 array[i++]=StringToDouble(p,&q);
2321 while ((isspace((int) ((unsigned char) *p)) != 0) || (*p == ','))
2328 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2332 + S t r i n g T o k e n %
2336 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2338 % StringToken() Looks for any one of given delimiters and splits the string
2339 % into two separate strings by replacing the delimiter character found with a
2342 % The given string pointer is changed to point to the string following the
2343 % delimiter character found, or NULL. A pointer to the start of the
2344 % string is returned, representing the token before the delimiter.
2346 % In may ways this is equivent to the strtok() C library function, but with
2347 % multiple delimiter characters rather than a delimiter string.
2349 % The format of the StringToken method is:
2351 % char *StringToken(const char *delimiters,char **string)
2353 % A description of each parameter follows:
2355 % o delimiters: one or more delimiters.
2357 % o string: return the first token in the string. If none is found, return
2361 MagickExport char *StringToken(const char *delimiters,char **string)
2377 if (p == (char *) NULL)
2378 return((char *) NULL);
2396 } while (d != '\0');
2401 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2405 % S t r i n g T o L i s t %
2409 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2411 % StringToList() converts a text string into a list by segmenting the text
2412 % string at each carriage return discovered. The list is converted to HEX
2413 % characters if any control characters are discovered within the text string.
2415 % The format of the StringToList method is:
2417 % char **StringToList(const char *text)
2419 % A description of each parameter follows:
2421 % o text: Specifies the string to segment into a list.
2424 MagickExport char **StringToList(const char *text)
2438 if (text == (char *) NULL)
2439 return((char **) NULL);
2440 for (p=text; *p != '\0'; p++)
2441 if (((int) ((unsigned char) *p) < 32) &&
2442 (isspace((int) ((unsigned char) *p)) == 0))
2450 Convert string to an ASCII list.
2453 for (p=text; *p != '\0'; p++)
2456 textlist=(char **) AcquireQuantumMemory((size_t) lines+1UL,
2458 if (textlist == (char **) NULL)
2459 ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
2461 for (i=0; i < (ssize_t) lines; i++)
2463 for (q=p; *q != '\0'; q++)
2464 if ((*q == '\r') || (*q == '\n'))
2466 textlist[i]=(char *) AcquireQuantumMemory((size_t) (q-p)+MaxTextExtent,
2467 sizeof(**textlist));
2468 if (textlist[i] == (char *) NULL)
2469 ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
2470 (void) memcpy(textlist[i],p,(size_t) (q-p));
2471 textlist[i][q-p]='\0';
2480 hex_string[MaxTextExtent];
2489 Convert string to a HEX list.
2491 lines=(size_t) (strlen(text)/0x14)+1;
2492 textlist=(char **) AcquireQuantumMemory((size_t) lines+1UL,
2494 if (textlist == (char **) NULL)
2495 ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
2497 for (i=0; i < (ssize_t) lines; i++)
2499 textlist[i]=(char *) AcquireQuantumMemory(2UL*MaxTextExtent,
2500 sizeof(**textlist));
2501 if (textlist[i] == (char *) NULL)
2502 ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
2503 (void) FormatLocaleString(textlist[i],MaxTextExtent,"0x%08lx: ",
2505 q=textlist[i]+strlen(textlist[i]);
2506 for (j=1; j <= (ssize_t) MagickMin(strlen(p),0x14); j++)
2508 (void) FormatLocaleString(hex_string,MaxTextExtent,"%02x",*(p+j));
2509 (void) CopyMagickString(q,hex_string,MaxTextExtent);
2511 if ((j % 0x04) == 0)
2514 for ( ; j <= 0x14; j++)
2518 if ((j % 0x04) == 0)
2522 for (j=1; j <= (ssize_t) MagickMin(strlen(p),0x14); j++)
2524 if (isprint((int) ((unsigned char) *p)) != 0)
2533 textlist[i]=(char *) NULL;
2538 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2542 % S t r i n g T o S t r i n g I n f o %
2546 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2548 % StringToStringInfo() converts a string to a StringInfo type.
2550 % The format of the StringToStringInfo method is:
2552 % StringInfo *StringToStringInfo(const char *string)
2554 % A description of each parameter follows:
2556 % o string: The string.
2559 MagickExport StringInfo *StringToStringInfo(const char *string)
2564 assert(string != (const char *) NULL);
2565 string_info=AcquireStringInfo(strlen(string));
2566 SetStringInfoDatum(string_info,(const unsigned char *) string);
2567 return(string_info);
2571 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2575 % S t r i p S t r i n g %
2579 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2581 % StripString() strips any whitespace or quotes from the beginning and end of
2582 % a string of characters.
2584 % The format of the StripString method is:
2586 % void StripString(char *message)
2588 % A description of each parameter follows:
2590 % o message: Specifies an array of characters.
2593 MagickExport void StripString(char *message)
2602 assert(message != (char *) NULL);
2603 if (*message == '\0')
2605 length=strlen(message);
2607 while (isspace((int) ((unsigned char) *p)) != 0)
2609 if ((*p == '\'') || (*p == '"'))
2612 while ((isspace((int) ((unsigned char) *q)) != 0) && (q > p))
2615 if ((*q == '\'') || (*q == '"'))
2617 (void) memmove(message,p,(size_t) (q-p+1));
2618 message[q-p+1]='\0';
2619 for (p=message; *p != '\0'; p++)
2625 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2629 % S u b s t i t u t e S t r i n g %
2633 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2635 % SubstituteString() performs string substitution on a string, replacing the
2636 % string with the substituted version. Buffer must be allocated from the heap.
2637 % If the string is matched and status, MagickTrue is returned otherwise
2640 % The format of the SubstituteString method is:
2642 % MagickBooleanType SubstituteString(char **string,const char *search,
2643 % const char *replace)
2645 % A description of each parameter follows:
2647 % o string: the string to perform replacements on; replaced with new
2648 % allocation if a replacement is made.
2650 % o search: search for this string.
2652 % o replace: replace any matches with this string.
2655 MagickExport MagickBooleanType SubstituteString(char **string,
2656 const char *search,const char *replace)
2675 for (p=strchr(*string,*search); p != (char *) NULL; p=strchr(p+1,*search))
2677 if (search_extent == 0)
2678 search_extent=strlen(search);
2679 if (strncmp(p,search,search_extent) != 0)
2685 if (replace_extent == 0)
2686 replace_extent=strlen(replace);
2687 if (replace_extent > search_extent)
2690 Make room for the replacement string.
2692 offset=(ssize_t) (p-(*string));
2693 extent=strlen(*string)+replace_extent-search_extent+1;
2694 *string=(char *) ResizeQuantumMemory(*string,extent+MaxTextExtent,
2696 if (*string == (char *) NULL)
2697 ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
2703 if (search_extent != replace_extent)
2704 (void) CopyMagickMemory(p+replace_extent,p+search_extent,
2705 strlen(p+search_extent)+1);
2706 (void) CopyMagickMemory(p,replace,replace_extent);
2707 p+=replace_extent-1;