string.c

Go to the documentation of this file.
00001 /*
00002 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00003 %                                                                             %
00004 %                                                                             %
00005 %                                                                             %
00006 %                  SSSSS   TTTTT  RRRR   IIIII  N   N   GGGG                  %
00007 %                  SS        T    R   R    I    NN  N  G                      %
00008 %                   SSS      T    RRRR     I    N N N  G GGG                  %
00009 %                     SS     T    R R      I    N  NN  G   G                  %
00010 %                  SSSSS     T    R  R   IIIII  N   N   GGGG                  %
00011 %                                                                             %
00012 %                                                                             %
00013 %                        MagickCore String Methods                            %
00014 %                                                                             %
00015 %                             Software Design                                 %
00016 %                               John Cristy                                   %
00017 %                               August 2003                                   %
00018 %                                                                             %
00019 %                                                                             %
00020 %  Copyright 1999-2010 ImageMagick Studio LLC, a non-profit organization      %
00021 %  dedicated to making software imaging solutions freely available.           %
00022 %                                                                             %
00023 %  You may not use this file except in compliance with the License.  You may  %
00024 %  obtain a copy of the License at                                            %
00025 %                                                                             %
00026 %    http://www.imagemagick.org/script/license.php                            %
00027 %                                                                             %
00028 %  Unless required by applicable law or agreed to in writing, software        %
00029 %  distributed under the License is distributed on an "AS IS" BASIS,          %
00030 %  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   %
00031 %  See the License for the specific language governing permissions and        %
00032 %  limitations under the License.                                             %
00033 %                                                                             %
00034 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00035 %
00036 %
00037 */
00038 
00039 /*
00040   Include declarations.
00041 */
00042 #include "magick/studio.h"
00043 #include "magick/blob.h"
00044 #include "magick/blob-private.h"
00045 #include "magick/exception.h"
00046 #include "magick/exception-private.h"
00047 #include "magick/list.h"
00048 #include "magick/log.h"
00049 #include "magick/memory_.h"
00050 #include "magick/property.h"
00051 #include "magick/resource_.h"
00052 #include "magick/signature-private.h"
00053 #include "magick/string_.h"
00054 
00055 /*
00056   Static declarations.
00057 */
00058 #if !defined(MAGICKCORE_HAVE_STRCASECMP) || !defined(MAGICKCORE_HAVE_STRNCASECMP)
00059 static const unsigned char
00060   AsciiMap[] =
00061   {
00062     0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
00063     0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
00064     0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
00065     0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f,
00066     0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x3b,
00067     0x3c, 0x3d, 0x3e, 0x3f, 0x40, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
00068     0x68, 0x69, 0x6a, 0x6b, 0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73,
00069     0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x5b, 0x5c, 0x5d, 0x5e, 0x5f,
00070     0x60, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b,
00071     0x6c, 0x6d, 0x6e, 0x6f, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75, 0x76, 0x77,
00072     0x78, 0x79, 0x7a, 0x7b, 0x7c, 0x7d, 0x7e, 0x7f, 0x80, 0x81, 0x82, 0x83,
00073     0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
00074     0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0x9b,
00075     0x9c, 0x9d, 0x9e, 0x9f, 0xa0, 0xa1, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
00076     0xa8, 0xa9, 0xaa, 0xab, 0xac, 0xad, 0xae, 0xaf, 0xb0, 0xb1, 0xb2, 0xb3,
00077     0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xbb, 0xbc, 0xbd, 0xbe, 0xbf,
00078     0xc0, 0xe1, 0xe2, 0xe3, 0xe4, 0xc5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb,
00079     0xec, 0xed, 0xee, 0xef, 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7,
00080     0xf8, 0xf9, 0xfa, 0xdb, 0xdc, 0xdd, 0xde, 0xdf, 0xe0, 0xe1, 0xe2, 0xe3,
00081     0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xeb, 0xec, 0xed, 0xee, 0xef,
00082     0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb,
00083     0xfc, 0xfd, 0xfe, 0xff,
00084   };
00085 #endif
00086 
00087 /*
00088 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00089 %                                                                             %
00090 %                                                                             %
00091 %                                                                             %
00092 %   A c q u i r e S t r i n g                                                 %
00093 %                                                                             %
00094 %                                                                             %
00095 %                                                                             %
00096 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00097 %
00098 %  AcquireString() allocates memory for a string and copies the source string
00099 %  to that memory location (and returns it).
00100 %
00101 %  The format of the AcquireString method is:
00102 %
00103 %      char *AcquireString(const char *source)
00104 %
00105 %  A description of each parameter follows:
00106 %
00107 %    o source: A character string.
00108 %
00109 */
00110 MagickExport char *AcquireString(const char *source)
00111 {
00112   char
00113     *destination;
00114 
00115   size_t
00116     length;
00117 
00118   length=0;
00119   if (source != (char *) NULL)
00120     length+=strlen(source);
00121   destination=(char *) NULL;
00122   if (~length >= MaxTextExtent)
00123     destination=(char *) AcquireQuantumMemory(length+MaxTextExtent,
00124       sizeof(*destination));
00125   if (destination == (char *) NULL)
00126     ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
00127   *destination='\0';
00128   if (source != (char *) NULL)
00129     (void) CopyMagickString(destination,source,(length+1)*sizeof(*destination));
00130   return(destination);
00131 }
00132 
00133 /*
00134 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00135 %                                                                             %
00136 %                                                                             %
00137 %                                                                             %
00138 %   A c q u i r e S t r i n g I n f o                                         %
00139 %                                                                             %
00140 %                                                                             %
00141 %                                                                             %
00142 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00143 %
00144 %  AcquireStringInfo() allocates the StringInfo structure.
00145 %
00146 %  The format of the AcquireStringInfo method is:
00147 %
00148 %      StringInfo *AcquireStringInfo(const size_t length)
00149 %
00150 %  A description of each parameter follows:
00151 %
00152 %    o length: the string length.
00153 %
00154 */
00155 MagickExport StringInfo *AcquireStringInfo(const size_t length)
00156 {
00157   StringInfo
00158     *string_info;
00159 
00160   string_info=(StringInfo *) AcquireMagickMemory(sizeof(*string_info));
00161   if (string_info == (StringInfo *) NULL)
00162     ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
00163   (void) ResetMagickMemory(string_info,0,sizeof(*string_info));
00164   string_info->signature=MagickSignature;
00165   string_info->length=length;
00166   if (string_info->length != 0)
00167     {
00168       string_info->datum=(unsigned char *) NULL;
00169       if (~string_info->length >= MaxTextExtent)
00170         string_info->datum=(unsigned char *) AcquireQuantumMemory(
00171           string_info->length+MaxTextExtent,sizeof(*string_info->datum));
00172       if (string_info->datum == (unsigned char *) NULL)
00173         ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
00174     }
00175   return(string_info);
00176 }
00177 
00178 /*
00179 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00180 %                                                                             %
00181 %                                                                             %
00182 %                                                                             %
00183 %   C l o n e S t r i n g                                                     %
00184 %                                                                             %
00185 %                                                                             %
00186 %                                                                             %
00187 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00188 %
00189 %  CloneString() allocates memory for the destination string and copies
00190 %  the source string to that memory location.
00191 %
00192 %  The format of the CloneString method is:
00193 %
00194 %      char *CloneString(char **destination,const char *source)
00195 %
00196 %  A description of each parameter follows:
00197 %
00198 %    o destination:  A pointer to a character string.
00199 %
00200 %    o source: A character string.
00201 %
00202 */
00203 MagickExport char *CloneString(char **destination,const char *source)
00204 {
00205   size_t
00206     length;
00207 
00208   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
00209   assert(destination != (char **) NULL);
00210   if (source == (const char *) NULL)
00211     {
00212       if (*destination != (char *) NULL)
00213         *destination=DestroyString(*destination);
00214       return(*destination);
00215     }
00216   if (*destination == (char *) NULL)
00217     {
00218       *destination=AcquireString(source);
00219       return(*destination);
00220     }
00221   length=strlen(source);
00222   if (~length < MaxTextExtent)
00223     ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
00224   *destination=(char *) ResizeQuantumMemory(*destination,length+MaxTextExtent,
00225     sizeof(*destination));
00226   if (*destination == (char *) NULL)
00227     ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
00228   (void) CopyMagickString(*destination,source,(length+1)*sizeof(*destination));
00229   return(*destination);
00230 }
00231 
00232 /*
00233 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00234 %                                                                             %
00235 %                                                                             %
00236 %                                                                             %
00237 %   C l o n e S t r i n g I n f o                                             %
00238 %                                                                             %
00239 %                                                                             %
00240 %                                                                             %
00241 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00242 %
00243 %  CloneStringInfo() clones a copy of the StringInfo structure.
00244 %
00245 %  The format of the CloneStringInfo method is:
00246 %
00247 %      StringInfo *CloneStringInfo(const StringInfo *string_info)
00248 %
00249 %  A description of each parameter follows:
00250 %
00251 %    o string_info: the string info.
00252 %
00253 */
00254 MagickExport StringInfo *CloneStringInfo(const StringInfo *string_info)
00255 {
00256   StringInfo
00257     *clone_info;
00258 
00259   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
00260   assert(string_info != (StringInfo *) NULL);
00261   assert(string_info->signature == MagickSignature);
00262   clone_info=AcquireStringInfo(string_info->length);
00263   if (string_info->length != 0)
00264     (void) CopyMagickMemory(clone_info->datum,string_info->datum,
00265       string_info->length+1);
00266   return(clone_info);
00267 }
00268 
00269 /*
00270 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00271 %                                                                             %
00272 %                                                                             %
00273 %                                                                             %
00274 %   C o m p a r e S t r i n g I n f o                                         %
00275 %                                                                             %
00276 %                                                                             %
00277 %                                                                             %
00278 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00279 %
00280 %  CompareStringInfo() compares the two datums target and source.  It returns
00281 %  an integer less than, equal to, or greater than zero if target is found,
00282 %  respectively, to be less than, to match, or be greater than source.
00283 %
00284 %  The format of the CompareStringInfo method is:
00285 %
00286 %      int CompareStringInfo(const StringInfo *target,const StringInfo *source)
00287 %
00288 %  A description of each parameter follows:
00289 %
00290 %    o target: the target string.
00291 %
00292 %    o source: the source string.
00293 %
00294 */
00295 
00296 static inline size_t MagickMin(const size_t x,const size_t y)
00297 {
00298   if (x < y)
00299     return(x);
00300   return(y);
00301 }
00302 
00303 MagickExport int CompareStringInfo(const StringInfo *target,
00304   const StringInfo *source)
00305 {
00306   int
00307     status;
00308 
00309   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
00310   assert(target != (StringInfo *) NULL);
00311   assert(target->signature == MagickSignature);
00312   assert(source != (StringInfo *) NULL);
00313   assert(source->signature == MagickSignature);
00314   status=memcmp(target->datum,source->datum,MagickMin(target->length,
00315     source->length));
00316   if (status != 0)
00317     return(status);
00318   if (target->length == source->length)
00319     return(0);
00320   return(target->length < source->length ? -1 : 1);
00321 }
00322 
00323 /*
00324 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00325 %                                                                             %
00326 %                                                                             %
00327 %                                                                             %
00328 %   C o n c a t e n a t e M a g i c k S t r i n g                             %
00329 %                                                                             %
00330 %                                                                             %
00331 %                                                                             %
00332 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00333 %
00334 %  ConcatenateMagickString() concatenates the source string to the destination
00335 %  string.  The destination buffer is always null-terminated even if the
00336 %  string must be truncated.
00337 %
00338 %  The format of the ConcatenateMagickString method is:
00339 %
00340 %      size_t ConcatenateMagickString(char *destination,const char *source,
00341 %        const size_t length)
00342 %
00343 %  A description of each parameter follows:
00344 %
00345 %    o destination: the destination string.
00346 %
00347 %    o source: the source string.
00348 %
00349 %    o length: the length of the destination string.
00350 %
00351 */
00352 MagickExport size_t ConcatenateMagickString(char *destination,
00353   const char *source,const size_t length)
00354 {
00355   register char
00356     *q;
00357 
00358   register const char
00359     *p;
00360 
00361   register size_t
00362     i;
00363 
00364   size_t
00365     count;
00366 
00367   assert(destination != (char *) NULL);
00368   assert(source != (const char *) NULL);
00369   assert(length >= 1);
00370   p=source;
00371   q=destination;
00372   i=length;
00373   while ((i-- != 0) && (*q != '\0'))
00374     q++;
00375   count=(size_t) (q-destination);
00376   i=length-count;
00377   if (i == 0)
00378     return(count+strlen(p));
00379   while (*p != '\0')
00380   {
00381     if (i != 1)
00382       {
00383         *q++=(*p);
00384         i--;
00385       }
00386     p++;
00387   }
00388   *q='\0';
00389   return(count+(p-source));
00390 }
00391 
00392 /*
00393 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00394 %                                                                             %
00395 %                                                                             %
00396 %                                                                             %
00397 %   C o n c a t e n a t e S t r i n g                                         %
00398 %                                                                             %
00399 %                                                                             %
00400 %                                                                             %
00401 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00402 %
00403 %  ConcatenateString() appends a copy of string source, including the
00404 %  terminating null character, to the end of string destination.
00405 %
00406 %  The format of the ConcatenateString method is:
00407 %
00408 %      MagickBooleanType ConcatenateString(char **destination,
00409 %        const char *source)
00410 %
00411 %  A description of each parameter follows:
00412 %
00413 %    o destination:  A pointer to a character string.
00414 %
00415 %    o source: A character string.
00416 %
00417 */
00418 MagickExport MagickBooleanType ConcatenateString(char **destination,
00419   const char *source)
00420 {
00421   size_t
00422     length,
00423     source_length;
00424 
00425   assert(destination != (char **) NULL);
00426   if (source == (const char *) NULL)
00427     return(MagickTrue);
00428   if (*destination == (char *) NULL)
00429     {
00430       *destination=AcquireString(source);
00431       return(MagickTrue);
00432     }
00433   length=strlen(*destination);
00434   source_length=strlen(source);
00435   if (~length < source_length)
00436     ThrowFatalException(ResourceLimitFatalError,"UnableToConcatenateString");
00437   length+=source_length;
00438   if (~length < MaxTextExtent)
00439     ThrowFatalException(ResourceLimitFatalError,"UnableToConcatenateString");
00440   *destination=(char *) ResizeQuantumMemory(*destination,length+MaxTextExtent,
00441     sizeof(*destination));
00442   if (*destination == (char *) NULL)
00443     ThrowFatalException(ResourceLimitFatalError,"UnableToConcatenateString");
00444   (void) ConcatenateMagickString(*destination,source,(length+1)*
00445     sizeof(*destination));
00446   return(MagickTrue);
00447 }
00448 
00449 /*
00450 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00451 %                                                                             %
00452 %                                                                             %
00453 %                                                                             %
00454 %   C o n c a t e n a t e S t r i n g I n f o                                 %
00455 %                                                                             %
00456 %                                                                             %
00457 %                                                                             %
00458 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00459 %
00460 %  ConcatenateStringInfo() concatenates the source string to the destination
00461 %  string.
00462 %
00463 %  The format of the ConcatenateStringInfo method is:
00464 %
00465 %      void ConcatenateStringInfo(StringInfo *string_info,
00466 %        const StringInfo *source)
00467 %
00468 %  A description of each parameter follows:
00469 %
00470 %    o string_info: the string info.
00471 %
00472 %    o source: the source string.
00473 %
00474 */
00475 MagickExport void ConcatenateStringInfo(StringInfo *string_info,
00476   const StringInfo *source)
00477 {
00478   size_t
00479     length;
00480 
00481   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
00482   assert(string_info != (StringInfo *) NULL);
00483   assert(string_info->signature == MagickSignature);
00484   assert(source != (const StringInfo *) NULL);
00485   length=string_info->length;
00486   if (~length < source->length)
00487     ThrowFatalException(ResourceLimitFatalError,"UnableToConcatenateString");
00488   SetStringInfoLength(string_info,length+source->length);
00489   (void) CopyMagickMemory(string_info->datum+length,source->datum,
00490     source->length);
00491 }
00492 
00493 /*
00494 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00495 %                                                                             %
00496 %                                                                             %
00497 %                                                                             %
00498 %   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                         %
00499 %                                                                             %
00500 %                                                                             %
00501 %                                                                             %
00502 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00503 %
00504 %  ConfigureFileToStringInfo() returns the contents of a configure file as a
00505 %  string.
00506 %
00507 %  The format of the ConfigureFileToStringInfo method is:
00508 %
00509 %      StringInfo *ConfigureFileToStringInfo(const char *filename)
00510 %        ExceptionInfo *exception)
00511 %
00512 %  A description of each parameter follows:
00513 %
00514 %    o filename: the filename.
00515 %
00516 */
00517 MagickExport StringInfo *ConfigureFileToStringInfo(const char *filename)
00518 {
00519   char
00520     *string;
00521 
00522   int
00523     file;
00524 
00525   MagickOffsetType
00526     offset;
00527 
00528   size_t
00529     length;
00530 
00531   StringInfo
00532     *string_info;
00533 
00534   void
00535     *map;
00536 
00537   assert(filename != (const char *) NULL);
00538   file=open(filename,O_RDONLY | O_BINARY);
00539   if (file == -1)
00540     return((StringInfo *) NULL);
00541   offset=(MagickOffsetType) MagickSeek(file,0,SEEK_END);
00542   if ((offset < 0) || (offset != (MagickOffsetType) ((ssize_t) offset)))
00543     {
00544       file=close(file)-1;
00545       return((StringInfo *) NULL);
00546     }
00547   length=(size_t) offset;
00548   string=(char *) NULL;
00549   if (~length > MaxTextExtent)
00550     string=(char *) AcquireQuantumMemory(length+MaxTextExtent,sizeof(*string));
00551   if (string == (char *) NULL)
00552     {
00553       file=close(file)-1;
00554       return((StringInfo *) NULL);
00555     }
00556   map=MapBlob(file,ReadMode,0,length);
00557   if (map != (void *) NULL)
00558     {
00559       (void) CopyMagickMemory(string,map,length);
00560       (void) UnmapBlob(map,length);
00561     }
00562   else
00563     {
00564       register size_t
00565         i;
00566 
00567       ssize_t
00568         count;
00569 
00570       (void) MagickSeek(file,0,SEEK_SET);
00571       for (i=0; i < length; i+=count)
00572       {
00573         count=read(file,string+i,(size_t) MagickMin(length-i,(size_t)
00574           SSIZE_MAX));
00575         if (count <= 0)
00576           {
00577             count=0;
00578             if (errno != EINTR)
00579               break;
00580           }
00581       }
00582       if (i < length)
00583         {
00584           file=close(file)-1;
00585           string=DestroyString(string);
00586           return((StringInfo *) NULL);
00587         }
00588     }
00589   string[length]='\0';
00590   file=close(file)-1;
00591   string_info=AcquireStringInfo(0);
00592   (void) CopyMagickString(string_info->path,filename,MaxTextExtent);
00593   string_info->length=length;
00594   string_info->datum=(unsigned char *) string;
00595   return(string_info);
00596 }
00597 
00598 /*
00599 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00600 %                                                                             %
00601 %                                                                             %
00602 %                                                                             %
00603 %   C o n s t a n t S t r i n g                                               %
00604 %                                                                             %
00605 %                                                                             %
00606 %                                                                             %
00607 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00608 %
00609 %  ConstantString() allocates memory for a string and copies the source string
00610 %  to that memory location (and returns it).  Use it for strings that you do
00611 %  do not expect to change over its lifetime.
00612 %
00613 %  The format of the ConstantString method is:
00614 %
00615 %      char *ConstantString(const char *source)
00616 %
00617 %  A description of each parameter follows:
00618 %
00619 %    o source: A character string.
00620 %
00621 */
00622 MagickExport char *ConstantString(const char *source)
00623 {
00624   char
00625     *destination;
00626 
00627   size_t
00628     length;
00629 
00630   length=0;
00631   if (source != (char *) NULL)
00632     length+=strlen(source);
00633   destination=(char *) NULL;
00634   if (~length >= 1UL)
00635     destination=(char *) AcquireQuantumMemory(length+1UL,sizeof(*destination));
00636   if (destination == (char *) NULL)
00637     ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
00638   *destination='\0';
00639   if (source != (char *) NULL)
00640     (void) CopyMagickString(destination,source,(length+1)*sizeof(*destination));
00641   return(destination);
00642 }
00643 
00644 /*
00645 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00646 %                                                                             %
00647 %                                                                             %
00648 %                                                                             %
00649 %   C o p y M a g i c k S t r i n g                                           %
00650 %                                                                             %
00651 %                                                                             %
00652 %                                                                             %
00653 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00654 %
00655 %  CopyMagickString() copies the source string to the destination string.  The
00656 %  destination buffer is always null-terminated even if the string must be
00657 %  truncated.  The return value is the minimum of the source string length
00658 %  or the length parameter.
00659 %
00660 %  The format of the CopyMagickString method is:
00661 %
00662 %      size_t CopyMagickString(const char *destination,char *source,
00663 %        const size_t length)
00664 %
00665 %  A description of each parameter follows:
00666 %
00667 %    o destination: the destination string.
00668 %
00669 %    o source: the source string.
00670 %
00671 %    o length: the length of the destination string.
00672 %
00673 */
00674 MagickExport size_t CopyMagickString(char *destination,const char *source,
00675   const size_t length)
00676 {
00677   register char
00678     *q;
00679 
00680   register const char
00681     *p;
00682 
00683   register size_t
00684     n;
00685 
00686   p=source;
00687   q=destination;
00688   for (n=length; n > 4; n-=4)
00689   {
00690     *q=(*p++);
00691     if (*q == '\0')
00692       return((size_t) (p-source-1));
00693     q++;
00694     *q=(*p++);
00695     if (*q == '\0')
00696       return((size_t) (p-source-1));
00697     q++;
00698     *q=(*p++);
00699     if (*q == '\0')
00700       return((size_t) (p-source-1));
00701     q++;
00702     *q=(*p++);
00703     if (*q == '\0')
00704       return((size_t) (p-source-1));
00705     q++;
00706   }
00707   if (n != 0)
00708     for (n--; n != 0; n--)
00709     {
00710       *q=(*p++);
00711       if (*q == '\0')
00712         return((size_t) (p-source-1));
00713       q++;
00714     }
00715   if (length != 0)
00716     *q='\0';
00717   return((size_t) (p-source-1));
00718 }
00719 
00720 /*
00721 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00722 %                                                                             %
00723 %                                                                             %
00724 %                                                                             %
00725 %   D e s t r o y S t r i n g                                                 %
00726 %                                                                             %
00727 %                                                                             %
00728 %                                                                             %
00729 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00730 %
00731 %  DestroyString() destroys memory associated with a string.
00732 %
00733 %  The format of the DestroyString method is:
00734 %
00735 %      char *DestroyString(char *string)
00736 %
00737 %  A description of each parameter follows:
00738 %
00739 %    o string: the string.
00740 %
00741 */
00742 MagickExport char *DestroyString(char *string)
00743 {
00744   return((char *) RelinquishMagickMemory(string));
00745 }
00746 
00747 /*
00748 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00749 %                                                                             %
00750 %                                                                             %
00751 %                                                                             %
00752 %   D e s t r o y S t r i n g I n f o                                         %
00753 %                                                                             %
00754 %                                                                             %
00755 %                                                                             %
00756 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00757 %
00758 %  DestroyStringInfo() destroys memory associated with the StringInfo structure.
00759 %
00760 %  The format of the DestroyStringInfo method is:
00761 %
00762 %      StringInfo *DestroyStringInfo(StringInfo *string_info)
00763 %
00764 %  A description of each parameter follows:
00765 %
00766 %    o string_info: the string info.
00767 %
00768 */
00769 MagickExport StringInfo *DestroyStringInfo(StringInfo *string_info)
00770 {
00771   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
00772   assert(string_info != (StringInfo *) NULL);
00773   assert(string_info->signature == MagickSignature);
00774   if (string_info->datum != (unsigned char *) NULL)
00775     string_info->datum=(unsigned char *) RelinquishMagickMemory(
00776       string_info->datum);
00777   string_info->signature=(~MagickSignature);
00778   string_info=(StringInfo *) RelinquishMagickMemory(string_info);
00779   return(string_info);
00780 }
00781 
00782 /*
00783 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00784 %                                                                             %
00785 %                                                                             %
00786 %                                                                             %
00787 %   D e s t r o y S t r i n g L i s t                                         %
00788 %                                                                             %
00789 %                                                                             %
00790 %                                                                             %
00791 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00792 %
00793 %  DestroyStringList() zeros memory associated with a string list.
00794 %
00795 %  The format of the DestroyStringList method is:
00796 %
00797 %      char **DestroyStringList(char **list)
00798 %
00799 %  A description of each parameter follows:
00800 %
00801 %    o list: the string list.
00802 %
00803 */
00804 MagickExport char **DestroyStringList(char **list)
00805 {
00806   register long
00807     i;
00808 
00809   assert(list != (char **) NULL);
00810   for (i=0; list[i] != (char *) NULL; i++)
00811     list[i]=DestroyString(list[i]);
00812   list=(char **) RelinquishMagickMemory(list);
00813   return(list);
00814 }
00815 
00816 /*
00817 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00818 %                                                                             %
00819 %                                                                             %
00820 %                                                                             %
00821 %   E s c a p e S t r i n g                                                   %
00822 %                                                                             %
00823 %                                                                             %
00824 %                                                                             %
00825 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00826 %
00827 %  EscapeString() allocates memory for a backslash-escaped version of a
00828 %  source text string, copies the escaped version of the text to that
00829 %  memory location while adding backslash characters, and returns the
00830 %  escaped string.
00831 %
00832 %  The format of the EscapeString method is:
00833 %
00834 %      char *EscapeString(const char *source,const char escape)
00835 %
00836 %  A description of each parameter follows:
00837 %
00838 %    o allocate_string:  Method EscapeString returns the escaped string.
00839 %
00840 %    o source: A character string.
00841 %
00842 %    o escape: the quoted string termination character to escape (e.g. '"').
00843 %
00844 */
00845 MagickExport char *EscapeString(const char *source,const char escape)
00846 {
00847   char
00848     *destination;
00849 
00850   register char
00851     *q;
00852 
00853   register const char
00854     *p;
00855 
00856   size_t
00857     length;
00858 
00859   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
00860   assert(source != (const char *) NULL);
00861   length=strlen(source);
00862   for (p=source; *p != '\0'; p++)
00863     if ((*p == '\\') || (*p == escape))
00864       {
00865         if (~length < 1)
00866           ThrowFatalException(ResourceLimitFatalError,"UnableToEscapeString");
00867         length++;
00868       }
00869   destination=(char *) NULL;
00870   if (~length >= MaxTextExtent)
00871     destination=(char *) AcquireQuantumMemory(length+MaxTextExtent,
00872       sizeof(*destination));
00873   if (destination == (char *) NULL)
00874     ThrowFatalException(ResourceLimitFatalError,"UnableToEscapeString");
00875   *destination='\0';
00876   if (source != (char *) NULL)
00877     {
00878       q=destination;
00879       for (p=source; *p != '\0'; p++)
00880       {
00881         if ((*p == '\\') || (*p == escape))
00882           *q++='\\';
00883         *q++=(*p);
00884       }
00885       *q='\0';
00886     }
00887   return(destination);
00888 }
00889 
00890 /*
00891 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00892 %                                                                             %
00893 %                                                                             %
00894 %                                                                             %
00895 %   F i l e T o S t r i n g                                                   %
00896 %                                                                             %
00897 %                                                                             %
00898 %                                                                             %
00899 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00900 %
00901 %  FileToString() returns the contents of a file as a string.
00902 %
00903 %  The format of the FileToString method is:
00904 %
00905 %      char *FileToString(const char *filename,const size_t extent,
00906 %        ExceptionInfo *exception)
00907 %
00908 %  A description of each parameter follows:
00909 %
00910 %    o filename: the filename.
00911 %
00912 %    o extent: Maximum length of the string.
00913 %
00914 %    o exception: return any errors or warnings in this structure.
00915 %
00916 */
00917 MagickExport char *FileToString(const char *filename,const size_t extent,
00918   ExceptionInfo *exception)
00919 {
00920   size_t
00921     length;
00922 
00923   assert(filename != (const char *) NULL);
00924   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
00925   assert(exception != (ExceptionInfo *) NULL);
00926   return((char *) FileToBlob(filename,extent,&length,exception));
00927 }
00928 
00929 /*
00930 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00931 %                                                                             %
00932 %                                                                             %
00933 %                                                                             %
00934 %   F i l e T o S t r i n g I n f o                                           %
00935 %                                                                             %
00936 %                                                                             %
00937 %                                                                             %
00938 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00939 %
00940 %  FileToStringInfo() returns the contents of a file as a string.
00941 %
00942 %  The format of the FileToStringInfo method is:
00943 %
00944 %      StringInfo *FileToStringInfo(const char *filename,const size_t extent,
00945 %        ExceptionInfo *exception)
00946 %
00947 %  A description of each parameter follows:
00948 %
00949 %    o filename: the filename.
00950 %
00951 %    o extent: Maximum length of the string.
00952 %
00953 %    o exception: return any errors or warnings in this structure.
00954 %
00955 */
00956 MagickExport StringInfo *FileToStringInfo(const char *filename,
00957   const size_t extent,ExceptionInfo *exception)
00958 {
00959   StringInfo
00960     *string_info;
00961 
00962   assert(filename != (const char *) NULL);
00963   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",filename);
00964   assert(exception != (ExceptionInfo *) NULL);
00965   string_info=AcquireStringInfo(0);
00966   (void) CopyMagickString(string_info->path,filename,MaxTextExtent);
00967   string_info->datum=FileToBlob(filename,extent,&string_info->length,exception);
00968   if (string_info->datum == (unsigned char *) NULL)
00969     {
00970       string_info=DestroyStringInfo(string_info);
00971       return((StringInfo *) NULL);
00972     }
00973   return(string_info);
00974 }
00975 
00976 /*
00977 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00978 %                                                                             %
00979 %                                                                             %
00980 %                                                                             %
00981 %  F o r m a t M a g i c k S i z e                                            %
00982 %                                                                             %
00983 %                                                                             %
00984 %                                                                             %
00985 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
00986 %
00987 %  FormatMagickSize() converts a size to a human readable format, for example,
00988 %  14kb, 234mb, 2.7gb, or 3.0tb.  Scaling is done by repetitively dividing by
00989 %  1024.
00990 %
00991 %  The format of the FormatMagickSize method is:
00992 %
00993 %      long FormatMagickSize(const MagickSizeType size,char *format)
00994 %
00995 %  A description of each parameter follows:
00996 %
00997 %    o size:  convert this size to a human readable format.
00998 %
00999 %    o format:  human readable format.
01000 %
01001 */
01002 MagickExport long FormatMagickSize(const MagickSizeType size,char *format)
01003 {
01004   double
01005     length;
01006 
01007   long
01008     count;
01009 
01010   register long
01011     i,
01012     j;
01013 
01014   static const char
01015     *units[] = { "b", "kb", "mb", "gb", "tb", "pb", "eb", (char *) NULL };
01016 
01017 #if defined(_MSC_VER) && (_MSC_VER == 1200)
01018   length=(double) ((MagickOffsetType) size);
01019 #else
01020   length=(double) size;
01021 #endif
01022   for (i=0; (length >= 1024.0) && (units[i+1] != (const char *) NULL); i++)
01023     length/=1024.0;
01024   for (j=2; j < 10; j++)
01025   {
01026     count=FormatMagickString(format,MaxTextExtent,"%.*g%s",(int) (i+j),length,
01027       units[i]);
01028     if (strchr(format,'+') == (char *) NULL)
01029       break;
01030   }
01031   return(count);
01032 }
01033 
01034 /*
01035 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01036 %                                                                             %
01037 %                                                                             %
01038 %                                                                             %
01039 %  F o r m a t M a g i c k S t r i n g                                        %
01040 %                                                                             %
01041 %                                                                             %
01042 %                                                                             %
01043 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01044 %
01045 %  FormatMagickString() prints formatted output of a variable argument list.
01046 %
01047 %  The format of the FormatMagickString method is:
01048 %
01049 %      long FormatMagickString(char *string,const size_t length,
01050 %        const char *format,...)
01051 %
01052 %  A description of each parameter follows.
01053 %
01054 %   o string:  FormatMagickString() returns the formatted string in this
01055 %     character buffer.
01056 %
01057 %   o length: the maximum length of the string.
01058 %
01059 %   o format:  A string describing the format to use to write the remaining
01060 %     arguments.
01061 %
01062 */
01063 
01064 MagickExport long FormatMagickStringList(char *string,const size_t length,
01065   const char *format,va_list operands)
01066 {
01067   int
01068     n;
01069 
01070 #if defined(MAGICKCORE_HAVE_VSNPRINTF)
01071   n=vsnprintf(string,length,format,operands);
01072 #else
01073   n=vsprintf(string,format,operands);
01074 #endif
01075   if (n < 0)
01076     string[length-1]='\0';
01077   return((long) n);
01078 }
01079 
01080 MagickExport long FormatMagickString(char *string,const size_t length,
01081   const char *format,...)
01082 {
01083   long
01084     n;
01085 
01086   va_list
01087     operands;
01088 
01089   va_start(operands,format);
01090   n=(long) FormatMagickStringList(string,length,format,operands);
01091   va_end(operands);
01092   return(n);
01093 }
01094 
01095 /*
01096 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01097 %                                                                             %
01098 %                                                                             %
01099 %                                                                             %
01100 %  F o r m a t M a g i c k T i m e                                            %
01101 %                                                                             %
01102 %                                                                             %
01103 %                                                                             %
01104 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01105 %
01106 %  FormatMagickTime() returns the specified time in the Internet date/time
01107 %  format and the length of the timestamp.
01108 %
01109 %  The format of the FormatMagickTime method is:
01110 %
01111 %      long FormatMagickTime(const time_t time,const size_t length,
01112 %        char *timestamp)
01113 %
01114 %  A description of each parameter follows.
01115 %
01116 %   o time:  the time since the Epoch (00:00:00 UTC, January 1, 1970),
01117 %     measured in seconds.
01118 %
01119 %   o length: the maximum length of the string.
01120 %
01121 %   o timestamp:  Return the Internet date/time here.
01122 %
01123 */
01124 MagickExport long FormatMagickTime(const time_t time,const size_t length,
01125   char *timestamp)
01126 {
01127   long
01128     count;
01129 
01130   struct tm
01131     gm_time,
01132     local_time;
01133 
01134   time_t
01135     timezone;
01136 
01137   assert(timestamp != (char *) NULL);
01138 #if defined(MAGICKCORE_HAVE_LOCALTIME_R)
01139   (void) localtime_r(&time,&local_time);
01140 #else
01141   (void) memcpy(&local_time,localtime(&time),sizeof(local_time));
01142 #endif
01143 #if defined(MAGICKCORE_HAVE_GMTIME_R)
01144   (void) gmtime_r(&time,&gm_time);
01145 #else
01146   (void) memcpy(&gm_time,gmtime(&time),sizeof(gm_time));
01147 #endif
01148   timezone=(time_t) ((local_time.tm_min-gm_time.tm_min)/60+
01149     local_time.tm_hour-gm_time.tm_hour+24*((local_time.tm_year-
01150     gm_time.tm_year) != 0 ? (local_time.tm_year-gm_time.tm_year) :
01151     (local_time.tm_yday-gm_time.tm_yday)));
01152   count=FormatMagickString(timestamp,length,
01153     "%04d-%02d-%02dT%02d:%02d:%02d%+03ld:00",local_time.tm_year+1900,
01154     local_time.tm_mon+1,local_time.tm_mday,local_time.tm_hour,
01155     local_time.tm_min,local_time.tm_sec,(long) timezone);
01156   return(count);
01157 }
01158 
01159 /*
01160 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01161 %                                                                             %
01162 %                                                                             %
01163 %                                                                             %
01164 %   G e t E n v i r o n m e n t V a l u e                                     %
01165 %                                                                             %
01166 %                                                                             %
01167 %                                                                             %
01168 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01169 %
01170 %  GetEnvironmentValue() returns the environment string that matches the
01171 %  specified name.
01172 %
01173 %  The format of the GetEnvironmentValue method is:
01174 %
01175 %      char *GetEnvironmentValue(const char *name)
01176 %
01177 %  A description of each parameter follows:
01178 %
01179 %    o name: the environment name.
01180 %
01181 */
01182 MagickExport char *GetEnvironmentValue(const char *name)
01183 {
01184   const char
01185     *environment;
01186 
01187   environment=getenv(name);
01188   if (environment == (const char *) NULL)
01189     return((char *) NULL);
01190   return(ConstantString(environment));
01191 }
01192 
01193 /*
01194 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01195 %                                                                             %
01196 %                                                                             %
01197 %                                                                             %
01198 %   G e t S t r i n g I n f o D a t u m                                       %
01199 %                                                                             %
01200 %                                                                             %
01201 %                                                                             %
01202 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01203 %
01204 %  GetStringInfoDatum() returns the datum associated with the string.
01205 %
01206 %  The format of the GetStringInfoDatum method is:
01207 %
01208 %      unsigned char *GetStringInfoDatum(const StringInfo *string_info)
01209 %
01210 %  A description of each parameter follows:
01211 %
01212 %    o string_info: the string info.
01213 %
01214 */
01215 MagickExport unsigned char *GetStringInfoDatum(const StringInfo *string_info)
01216 {
01217   assert(string_info != (StringInfo *) NULL);
01218   assert(string_info->signature == MagickSignature);
01219   return(string_info->datum);
01220 }
01221 
01222 /*
01223 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01224 %                                                                             %
01225 %                                                                             %
01226 %                                                                             %
01227 %   G e t S t r i n g I n f o L e n g t h                                     %
01228 %                                                                             %
01229 %                                                                             %
01230 %                                                                             %
01231 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01232 %
01233 %  GetStringInfoLength() returns the string length.
01234 %
01235 %  The format of the GetStringInfoLength method is:
01236 %
01237 %      size_t GetStringInfoLength(const StringInfo *string_info)
01238 %
01239 %  A description of each parameter follows:
01240 %
01241 %    o string_info: the string info.
01242 %
01243 */
01244 MagickExport size_t GetStringInfoLength(const StringInfo *string_info)
01245 {
01246   assert(string_info != (StringInfo *) NULL);
01247   assert(string_info->signature == MagickSignature);
01248   return(string_info->length);
01249 }
01250 
01251 /*
01252 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01253 %                                                                             %
01254 %                                                                             %
01255 %                                                                             %
01256 %   G e t S t r i n g I n f o P a t h                                         %
01257 %                                                                             %
01258 %                                                                             %
01259 %                                                                             %
01260 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01261 %
01262 %  GetStringInfoPath() returns the path associated with the string.
01263 %
01264 %  The format of the GetStringInfoPath method is:
01265 %
01266 %      const char *GetStringInfoPath(const StringInfo *string_info)
01267 %
01268 %  A description of each parameter follows:
01269 %
01270 %    o string_info: the string info.
01271 %
01272 */
01273 MagickExport const char *GetStringInfoPath(const StringInfo *string_info)
01274 {
01275   assert(string_info != (StringInfo *) NULL);
01276   assert(string_info->signature == MagickSignature);
01277   return(string_info->path);
01278 }
01279 
01280 /*
01281 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01282 %                                                                             %
01283 %                                                                             %
01284 %                                                                             %
01285 %   L o c a l e C o m p a r e                                                 %
01286 %                                                                             %
01287 %                                                                             %
01288 %                                                                             %
01289 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01290 %
01291 %  LocaleCompare() performs a case-insensitive comparison of two strings
01292 %  byte-by-byte, according to the ordering of the current locale encoding.
01293 %  LocaleCompare returns an integer greater than, equal to, or less than 0,
01294 %  if the string pointed to by p is greater than, equal to, or less than the
01295 %  string pointed to by q respectively.  The sign of a non-zero return value
01296 %  is determined by the sign of the difference between the values of the first< %  pair of bytes that differ in the strings being compared.
01297 %
01298 %  The format of the LocaleCompare method is:
01299 %
01300 %      long LocaleCompare(const char *p,const char *q)
01301 %
01302 %  A description of each parameter follows:
01303 %
01304 %    o p: A pointer to a character string.
01305 %
01306 %    o q: A pointer to a character string to compare to p.
01307 %
01308 */
01309 MagickExport long LocaleCompare(const char *p,const char *q)
01310 {
01311   if ((p == (char *) NULL) && (q == (char *) NULL))
01312     return(0);
01313   if (p == (char *) NULL)
01314     return(-1);
01315   if (q == (char *) NULL)
01316     return(1);
01317 #if defined(MAGICKCORE_HAVE_STRCASECMP)
01318   return((long) strcasecmp(p,q));
01319 #else
01320   {
01321     register unsigned char
01322       c,
01323       d;
01324 
01325     for ( ; ; )
01326     {
01327       c=(unsigned char) *p;
01328       d=(unsigned char) *q;
01329       if ((c == '\0') || (AsciiMap[c] != AsciiMap[d]))
01330         break;
01331       p++;
01332       q++;
01333     }
01334     return((long) AsciiMap[c]-AsciiMap[d]);
01335   }
01336 #endif
01337 }
01338 
01339 /*
01340 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01341 %                                                                             %
01342 %                                                                             %
01343 %                                                                             %
01344 %   L o c a l e L o w e r                                                     %
01345 %                                                                             %
01346 %                                                                             %
01347 %                                                                             %
01348 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01349 %
01350 %  LocaleLower() transforms all of the characters in the supplied
01351 %  null-terminated string, changing all uppercase letters to lowercase.
01352 %
01353 %  The format of the LocaleLower method is:
01354 %
01355 %      void LocaleLower(char *string)
01356 %
01357 %  A description of each parameter follows:
01358 %
01359 %    o string: A pointer to the string to convert to lower-case Locale.
01360 %
01361 */
01362 MagickExport void LocaleLower(char *string)
01363 {
01364   register char
01365     *q;
01366 
01367   assert(string != (char *) NULL);
01368   for (q=string; *q != '\0'; q++)
01369     *q=(char) tolower((int) *q);
01370 }
01371 
01372 /*
01373 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01374 %                                                                             %
01375 %                                                                             %
01376 %                                                                             %
01377 %   L o c a l e N C o m p a r e                                               %
01378 %                                                                             %
01379 %                                                                             %
01380 %                                                                             %
01381 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01382 %
01383 %  LocaleNCompare() performs a case-insensitive comparison of two
01384 %  strings byte-by-byte, according to the ordering of the current locale
01385 %  encoding. LocaleNCompare returns an integer greater than, equal to, or
01386 %  less than 0, if the string pointed to by p is greater than, equal to, or
01387 %  less than the string pointed to by q respectively.  The sign of a non-zero
01388 %  return value is determined by the sign of the difference between the
01389 %  values of the first pair of bytes that differ in the strings being
01390 %  compared.  The LocaleNCompare method makes the same comparison as
01391 %  LocaleCompare but looks at a maximum of n bytes.  Bytes following a
01392 %  null byte are not compared.
01393 %
01394 %  The format of the LocaleNCompare method is:
01395 %
01396 %      long LocaleNCompare(const char *p,const char *q,const size_t n)
01397 %
01398 %  A description of each parameter follows:
01399 %
01400 %    o p: A pointer to a character string.
01401 %
01402 %    o q: A pointer to a character string to compare to p.
01403 %
01404 %    o length: the number of characters to compare in strings p & q.
01405 %
01406 */
01407 MagickExport long LocaleNCompare(const char *p,const char *q,
01408   const size_t length)
01409 {
01410   if (p == (char *) NULL)
01411     return(-1);
01412   if (q == (char *) NULL)
01413     return(1);
01414 #if defined(MAGICKCORE_HAVE_STRNCASECMP)
01415   return((long) strncasecmp(p,q,length));
01416 #else
01417   {
01418     register size_t
01419       n;
01420 
01421     register unsigned char
01422       c,
01423       d;
01424 
01425     for (n=length; n != 0; n--)
01426     {
01427       c=(unsigned char) *p;
01428       d=(unsigned char) *q;
01429       if (AsciiMap[c] != AsciiMap[d])
01430         return(AsciiMap[c]-AsciiMap[d]);
01431       if (c == '\0')
01432         return(0L);
01433       p++;
01434       q++;
01435     }
01436     return(0L);
01437   }
01438 #endif
01439 }
01440 
01441 /*
01442 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01443 %                                                                             %
01444 %                                                                             %
01445 %                                                                             %
01446 %   L o c a l e U p p e r                                                     %
01447 %                                                                             %
01448 %                                                                             %
01449 %                                                                             %
01450 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01451 %
01452 %  LocaleUpper() transforms all of the characters in the supplied
01453 %  null-terminated string, changing all lowercase letters to uppercase.
01454 %
01455 %  The format of the LocaleUpper method is:
01456 %
01457 %      void LocaleUpper(char *string)
01458 %
01459 %  A description of each parameter follows:
01460 %
01461 %    o string: A pointer to the string to convert to upper-case Locale.
01462 %
01463 */
01464 MagickExport void LocaleUpper(char *string)
01465 {
01466   register char
01467     *q;
01468 
01469   assert(string != (char *) NULL);
01470   for (q=string; *q != '\0'; q++)
01471     *q=(char) toupper((int) *q);
01472 }
01473 
01474 /*
01475 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01476 %                                                                             %
01477 %                                                                             %
01478 %                                                                             %
01479 %   P r i n t S t r i n g I n f o                                             %
01480 %                                                                             %
01481 %                                                                             %
01482 %                                                                             %
01483 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01484 %
01485 %  PrintStringInfo() prints the string.
01486 %
01487 %  The format of the PrintStringInfo method is:
01488 %
01489 %      void PrintStringInfo(FILE *file,const char *id,
01490 %        const StringInfo *string_info)
01491 %
01492 %  A description of each parameter follows:
01493 %
01494 %    o file: the file, typically stdout.
01495 %
01496 %    o id: the string id.
01497 %
01498 %    o string_info: the string info.
01499 %
01500 */
01501 MagickExport void PrintStringInfo(FILE *file,const char *id,
01502   const StringInfo *string_info)
01503 {
01504   register const char
01505     *p;
01506 
01507   register size_t
01508     i,
01509     j;
01510 
01511   assert(id != (const char *) NULL);
01512   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",id);
01513   assert(string_info != (StringInfo *) NULL);
01514   assert(string_info->signature == MagickSignature);
01515   p=(char *) string_info->datum;
01516   for (i=0; i < string_info->length; i++)
01517   {
01518     if (((int) ((unsigned char) *p) < 32) &&
01519         (isspace((int) ((unsigned char) *p)) == 0))
01520       break;
01521     p++;
01522   }
01523   if (i == string_info->length)
01524     {
01525       (void) fputs((char *) string_info->datum,file);
01526       (void) fputc('\n',file);
01527       return;
01528     }
01529   /*
01530     Convert string to a HEX list.
01531   */
01532   p=(char *) string_info->datum;
01533   for (i=0; i < string_info->length; i+=0x14)
01534   {
01535     (void) fprintf(file,"0x%08lx: ",(unsigned long) (0x14*i));
01536     for (j=1; j <= MagickMin(string_info->length-i,0x14); j++)
01537     {
01538       (void) fprintf(file,"%02lx",(unsigned long) (*(p+j)) & 0xff);
01539       if ((j % 0x04) == 0)
01540         (void) fputc(' ',file);
01541     }
01542     for ( ; j <= 0x14; j++)
01543     {
01544       (void) fputc(' ',file);
01545       (void) fputc(' ',file);
01546       if ((j % 0x04) == 0)
01547         (void) fputc(' ',file);
01548     }
01549     (void) fputc(' ',file);
01550     for (j=1; j <= MagickMin(string_info->length-i,0x14); j++)
01551     {
01552       if (isprint((int) ((unsigned char) *p)) != 0)
01553         (void) fputc(*p,file);
01554       else
01555         (void) fputc('-',file);
01556       p++;
01557     }
01558     (void) fputc('\n',file);
01559   }
01560 }
01561 
01562 /*
01563 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01564 %                                                                             %
01565 %                                                                             %
01566 %                                                                             %
01567 %   R e s e t S t r i n g I n f o                                             %
01568 %                                                                             %
01569 %                                                                             %
01570 %                                                                             %
01571 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01572 %
01573 %  ResetStringInfo() reset the string to all null bytes.
01574 %
01575 %  The format of the ResetStringInfo method is:
01576 %
01577 %      void ResetStringInfo(StringInfo *string_info)
01578 %
01579 %  A description of each parameter follows:
01580 %
01581 %    o string_info: the string info.
01582 %
01583 */
01584 MagickExport void ResetStringInfo(StringInfo *string_info)
01585 {
01586   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
01587   assert(string_info != (StringInfo *) NULL);
01588   assert(string_info->signature == MagickSignature);
01589   (void) ResetMagickMemory(string_info->datum,0,string_info->length);
01590 }
01591 
01592 /*
01593 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01594 %                                                                             %
01595 %                                                                             %
01596 %                                                                             %
01597 %   S e t S t r i n g I n f o                                                 %
01598 %                                                                             %
01599 %                                                                             %
01600 %                                                                             %
01601 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01602 %
01603 %  SetStringInfo() copies the source string to the destination string.
01604 %
01605 %  The format of the SetStringInfo method is:
01606 %
01607 %      void SetStringInfo(StringInfo *string_info,const StringInfo *source)
01608 %
01609 %  A description of each parameter follows:
01610 %
01611 %    o string_info: the string info.
01612 %
01613 %    o source: the source string.
01614 %
01615 */
01616 MagickExport void SetStringInfo(StringInfo *string_info,
01617   const StringInfo *source)
01618 {
01619   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
01620   assert(string_info != (StringInfo *) NULL);
01621   assert(string_info->signature == MagickSignature);
01622   assert(source != (StringInfo *) NULL);
01623   assert(source->signature == MagickSignature);
01624   if (string_info->length == 0)
01625     return;
01626   (void) ResetMagickMemory(string_info->datum,0,string_info->length);
01627   (void) CopyMagickMemory(string_info->datum,source->datum,MagickMin(
01628     string_info->length,source->length));
01629 }
01630 
01631 /*
01632 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01633 %                                                                             %
01634 %                                                                             %
01635 %                                                                             %
01636 %   S e t S t r i n g I n f o D a t u m                                       %
01637 %                                                                             %
01638 %                                                                             %
01639 %                                                                             %
01640 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01641 %
01642 %  SetStringInfoDatum() copies bytes from the source string for the length of
01643 %  the destination string.
01644 %
01645 %  The format of the SetStringInfoDatum method is:
01646 %
01647 %      void SetStringInfoDatum(StringInfo *string_info,
01648 %        const unsigned char *source)
01649 %
01650 %  A description of each parameter follows:
01651 %
01652 %    o string_info: the string info.
01653 %
01654 %    o source: the source string.
01655 %
01656 */
01657 MagickExport void SetStringInfoDatum(StringInfo *string_info,
01658   const unsigned char *source)
01659 {
01660   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
01661   assert(string_info != (StringInfo *) NULL);
01662   assert(string_info->signature == MagickSignature);
01663   if (string_info->length != 0)
01664     (void) CopyMagickMemory(string_info->datum,source,string_info->length);
01665 }
01666 
01667 /*
01668 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01669 %                                                                             %
01670 %                                                                             %
01671 %                                                                             %
01672 %   S e t S t r i n g I n f o L e n g t h                                     %
01673 %                                                                             %
01674 %                                                                             %
01675 %                                                                             %
01676 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01677 %
01678 %  SetStringInfoLength() set the string length to the specified value.
01679 %
01680 %  The format of the SetStringInfoLength method is:
01681 %
01682 %      void SetStringInfoLength(StringInfo *string_info,const size_t length)
01683 %
01684 %  A description of each parameter follows:
01685 %
01686 %    o string_info: the string info.
01687 %
01688 %    o length: the string length.
01689 %
01690 */
01691 MagickExport void SetStringInfoLength(StringInfo *string_info,
01692   const size_t length)
01693 {
01694   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
01695   assert(string_info != (StringInfo *) NULL);
01696   assert(string_info->signature == MagickSignature);
01697   if (~length < MaxTextExtent)
01698     ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
01699   string_info->length=length;
01700   if (string_info->datum == (unsigned char *) NULL)
01701     string_info->datum=(unsigned char *) AcquireQuantumMemory(length+
01702       MaxTextExtent,sizeof(*string_info->datum));
01703   else
01704     string_info->datum=(unsigned char *) ResizeQuantumMemory(string_info->datum,
01705       length+MaxTextExtent,sizeof(*string_info->datum));
01706   if (string_info->datum == (unsigned char *) NULL)
01707     ThrowFatalException(ResourceLimitFatalError,"MemoryAllocationFailed");
01708 }
01709 
01710 /*
01711 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01712 %                                                                             %
01713 %                                                                             %
01714 %                                                                             %
01715 %   S e t S t r i n g I n f o D a t u m                                       %
01716 %                                                                             %
01717 %                                                                             %
01718 %                                                                             %
01719 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01720 %
01721 %  SetStringInfoPath() sets the path associated with the string.
01722 %
01723 %  The format of the SetStringInfoPath method is:
01724 %
01725 %      void SetStringInfoPath(StringInfo *string_info,const char *path)
01726 %
01727 %  A description of each parameter follows:
01728 %
01729 %    o string_info: the string info.
01730 %
01731 %    o path: the path.
01732 %
01733 */
01734 MagickExport void SetStringInfoPath(StringInfo *string_info,const char *path)
01735 {
01736   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
01737   assert(string_info != (StringInfo *) NULL);
01738   assert(string_info->signature == MagickSignature);
01739   assert(path != (const char *) NULL);
01740   (void) CopyMagickString(string_info->path,path,MaxTextExtent);
01741 }
01742 
01743 /*
01744 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01745 %                                                                             %
01746 %                                                                             %
01747 %                                                                             %
01748 %   S p l i t S t r i n g I n f o                                             %
01749 %                                                                             %
01750 %                                                                             %
01751 %                                                                             %
01752 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01753 %
01754 %  SplitStringInfo() splits a string into two and returns it.
01755 %
01756 %  The format of the SplitStringInfo method is:
01757 %
01758 %      StringInfo *SplitStringInfo(StringInfo *string_info,const size_t offset)
01759 %
01760 %  A description of each parameter follows:
01761 %
01762 %    o string_info: the string info.
01763 %
01764 */
01765 MagickExport StringInfo *SplitStringInfo(StringInfo *string_info,
01766   const size_t offset)
01767 {
01768   StringInfo
01769     *split_info;
01770 
01771   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
01772   assert(string_info != (StringInfo *) NULL);
01773   assert(string_info->signature == MagickSignature);
01774   if (offset > string_info->length)
01775     return((StringInfo *) NULL);
01776   split_info=AcquireStringInfo(offset);
01777   SetStringInfo(split_info,string_info);
01778   (void) CopyMagickMemory(string_info->datum,string_info->datum+offset,
01779     string_info->length-offset+MaxTextExtent);
01780   SetStringInfoLength(string_info,string_info->length-offset);
01781   return(split_info);
01782 }
01783 
01784 /*
01785 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01786 %                                                                             %
01787 %                                                                             %
01788 %                                                                             %
01789 %   S t r i n g I n f o T o S t r i n g                                       %
01790 %                                                                             %
01791 %                                                                             %
01792 %                                                                             %
01793 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01794 %
01795 %  StringInfoToString() converts a string info string to a C string.
01796 %
01797 %  The format of the StringInfoToString method is:
01798 %
01799 %      char *StringInfoToString(const StringInfo *string_info)
01800 %
01801 %  A description of each parameter follows:
01802 %
01803 %    o string_info: the string.
01804 %
01805 */
01806 MagickExport char *StringInfoToString(const StringInfo *string_info)
01807 {
01808   char
01809     *string;
01810 
01811   size_t
01812     length;
01813 
01814   string=(char *) NULL;
01815   length=string_info->length;
01816   if (~length >= MaxTextExtent)
01817     string=(char *) AcquireQuantumMemory(length+MaxTextExtent,sizeof(*string));
01818   if (string != (char *) NULL)
01819     (void) CopyMagickString(string,(char *) string_info->datum,
01820       (length+1)*sizeof(*string));
01821   return(string);
01822 }
01823 
01824 /*
01825 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01826 %                                                                             %
01827 %                                                                             %
01828 %                                                                             %
01829 %  S t r i n g T o A r g v                                                    %
01830 %                                                                             %
01831 %                                                                             %
01832 %                                                                             %
01833 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01834 %
01835 %  StringToArgv() converts a text string into command line arguments.
01836 %
01837 %  The format of the StringToArgv method is:
01838 %
01839 %      char **StringToArgv(const char *text,int *argc)
01840 %
01841 %  A description of each parameter follows:
01842 %
01843 %    o argv:  Method StringToArgv returns the string list unless an error
01844 %      occurs, otherwise NULL.
01845 %
01846 %    o text:  Specifies the string to segment into a list.
01847 %
01848 %    o argc:  This integer pointer returns the number of arguments in the
01849 %      list.
01850 %
01851 */
01852 MagickExport char **StringToArgv(const char *text,int *argc)
01853 {
01854   char
01855     **argv;
01856 
01857   register const char
01858     *p,
01859     *q;
01860 
01861   register long
01862     i;
01863 
01864   *argc=0;
01865   if (text == (char *) NULL)
01866     return((char **) NULL);
01867   /*
01868     Determine the number of arguments.
01869   */
01870   for (p=text; *p != '\0'; )
01871   {
01872     while (isspace((int) ((unsigned char) *p)) != 0)
01873       p++;
01874     (*argc)++;
01875     if (*p == '"')
01876       for (p++; (*p != '"') && (*p != '\0'); p++) ;
01877     if (*p == '\'')
01878       for (p++; (*p != '\'') && (*p != '\0'); p++) ;
01879     while ((isspace((int) ((unsigned char) *p)) == 0) && (*p != '\0'))
01880       p++;
01881   }
01882   (*argc)++;
01883   argv=(char **) AcquireQuantumMemory((size_t) (*argc+1UL),sizeof(*argv));
01884   if (argv == (char **) NULL)
01885     ThrowFatalException(ResourceLimitFatalError,"UnableToConvertStringToARGV");
01886   /*
01887     Convert string to an ASCII list.
01888   */
01889   argv[0]=AcquireString("magick");
01890   p=text;
01891   for (i=1; i < (long) *argc; i++)
01892   {
01893     while (isspace((int) ((unsigned char) *p)) != 0)
01894       p++;
01895     q=p;
01896     if (*q == '"')
01897       {
01898         p++;
01899         for (q++; (*q != '"') && (*q != '\0'); q++) ;
01900       }
01901     else
01902       if (*q == '\'')
01903         {
01904           for (q++; (*q != '\'') && (*q != '\0'); q++) ;
01905           q++;
01906         }
01907       else
01908         while ((isspace((int) ((unsigned char) *q)) == 0) && (*q != '\0'))
01909           q++;
01910     argv[i]=(char *) AcquireQuantumMemory((size_t) (q-p)+MaxTextExtent,
01911       sizeof(**argv));
01912     if (argv[i] == (char *) NULL)
01913       {
01914         for (i--; i >= 0; i--)
01915           argv[i]=DestroyString(argv[i]);
01916         argv=(char **) RelinquishMagickMemory(argv);
01917         ThrowFatalException(ResourceLimitFatalError,
01918           "UnableToConvertStringToARGV");
01919       }
01920     (void) CopyMagickString(argv[i],p,(size_t) (q-p+1));
01921     p=q;
01922     while ((isspace((int) ((unsigned char) *p)) == 0) && (*p != '\0'))
01923       p++;
01924   }
01925   argv[i]=(char *) NULL;
01926   return(argv);
01927 }
01928 
01929 /*
01930 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01931 %                                                                             %
01932 %                                                                             %
01933 %                                                                             %
01934 %  S t r i n g T o D o u b l e                                                %
01935 %                                                                             %
01936 %                                                                             %
01937 %                                                                             %
01938 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01939 %
01940 %  StringToDouble() converts a text string to a double.  If the string contains
01941 %  a percent sign (e.g. 50%) that percentage of the interval is returned.
01942 %
01943 %  The format of the StringToDouble method is:
01944 %
01945 %      double StringToDouble(const char *string,const double interval)
01946 %
01947 %  A description of each parameter follows:
01948 %
01949 %    o string:  Specifies the string representing a value.
01950 %
01951 %    o interval:  Specifies the interval; used for obtaining a percentage.
01952 %
01953 */
01954 MagickExport double StringToDouble(const char *string,const double interval)
01955 {
01956   char
01957     *q;
01958 
01959   double
01960     value;
01961 
01962   assert(string != (char *) NULL);
01963   value=strtod(string,&q);
01964   switch (tolower((int) ((unsigned char) *q)))
01965   {
01966     case '%': value*=pow(1024.0,0)*interval/100.0; break;
01967     case 'k': value*=pow(1024.0,1); break;
01968     case 'm': value*=pow(1024.0,2); break;
01969     case 'g': value*=pow(1024.0,3); break;
01970     case 't': value*=pow(1024.0,4); break;
01971     case 'p': value*=pow(1024.0,5); break;
01972     case 'e': value*=pow(1024.0,6); break;
01973     case 'z': value*=pow(1024.0,7); break;
01974     case 'y': value*=pow(1024.0,8); break;
01975     default:  break;
01976   }
01977   return(value);
01978 }
01979 
01980 /*
01981 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01982 %                                                                             %
01983 %                                                                             %
01984 %                                                                             %
01985 %   S t r i n g I n f o T o H e x S t r i n g                                 %
01986 %                                                                             %
01987 %                                                                             %
01988 %                                                                             %
01989 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
01990 %
01991 %  StringInfoToHexString() converts a string info string to a C string.
01992 %
01993 %  The format of the StringInfoToHexString method is:
01994 %
01995 %      char *StringInfoToHexString(const StringInfo *string_info)
01996 %
01997 %  A description of each parameter follows:
01998 %
01999 %    o string_info: the string.
02000 %
02001 */
02002 MagickExport char *StringInfoToHexString(const StringInfo *string_info)
02003 {
02004   char
02005     *string;
02006 
02007   register const unsigned char
02008     *p;
02009 
02010   register long
02011     i;
02012 
02013   register unsigned char
02014     *q;
02015 
02016   size_t
02017     length;
02018 
02019   unsigned char
02020     hex_digits[16];
02021 
02022   length=string_info->length;
02023   if (~length < MaxTextExtent)
02024     ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
02025   string=(char *) AcquireQuantumMemory(length+MaxTextExtent,2*sizeof(*string));
02026   if (string == (char *) NULL)
02027     ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
02028   hex_digits[0]='0';
02029   hex_digits[1]='1';
02030   hex_digits[2]='2';
02031   hex_digits[3]='3';
02032   hex_digits[4]='4';
02033   hex_digits[5]='5';
02034   hex_digits[6]='6';
02035   hex_digits[7]='7';
02036   hex_digits[8]='8';
02037   hex_digits[9]='9';
02038   hex_digits[10]='a';
02039   hex_digits[11]='b';
02040   hex_digits[12]='c';
02041   hex_digits[13]='d';
02042   hex_digits[14]='e';
02043   hex_digits[15]='f';
02044   p=string_info->datum;
02045   q=(unsigned char *) string;
02046   for (i=0; i < (long) string_info->length; i++)
02047   {
02048     *q++=hex_digits[(*p >> 4) & 0x0f];
02049     *q++=hex_digits[*p & 0x0f];
02050     p++;
02051   }
02052   *q='\0';
02053   return(string);
02054 }
02055 
02056 /*
02057 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
02058 %                                                                             %
02059 %                                                                             %
02060 %                                                                             %
02061 %   S t r i n g T o k e n                                                     %
02062 %                                                                             %
02063 %                                                                             %
02064 %                                                                             %
02065 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
02066 %
02067 %  StringToken() extracts a token a from the string.
02068 %
02069 %  The format of the StringToken method is:
02070 %
02071 %      char *StringToken(const char *delimiters,char **string)
02072 %
02073 %  A description of each parameter follows:
02074 %
02075 %    o delimiters: one or more delimiters.
02076 %
02077 %    o string: return the first token in the string.  If none is found, return
02078 %      NULL.
02079 %
02080 */
02081 MagickExport char *StringToken(const char *delimiters,char **string)
02082 {
02083   char
02084     *q;
02085 
02086   register char
02087     *p;
02088 
02089   register const char
02090     *r;
02091 
02092   register int
02093     c,
02094     d;
02095 
02096   p=(*string);
02097   if (p == (char *) NULL)
02098     return((char *) NULL);
02099   for (q=p; ; )
02100   {
02101     c=(*p++);
02102     r=delimiters;
02103     do
02104     {
02105       d=(*r++);
02106       if (c == d)
02107         {
02108           if (c == '\0')
02109             p=(char *) NULL;
02110           else
02111             p[-1]='\0';
02112           *string=p;
02113           return(q);
02114         }
02115     } while (d != '\0');
02116   }
02117 }
02118 
02119 /*
02120 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
02121 %                                                                             %
02122 %                                                                             %
02123 %                                                                             %
02124 %  S t r i n g T o L i s t                                                    %
02125 %                                                                             %
02126 %                                                                             %
02127 %                                                                             %
02128 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
02129 %
02130 %  StringToList() converts a text string into a list by segmenting the text
02131 %  string at each carriage return discovered.  The list is converted to HEX
02132 %  characters if any control characters are discovered within the text string.
02133 %
02134 %  The format of the StringToList method is:
02135 %
02136 %      char **StringToList(const char *text)
02137 %
02138 %  A description of each parameter follows:
02139 %
02140 %    o list:  Method StringToList returns the string list unless an error
02141 %      occurs, otherwise NULL.
02142 %
02143 %    o text:  Specifies the string to segment into a list.
02144 %
02145 */
02146 MagickExport char **StringToList(const char *text)
02147 {
02148   char
02149     **textlist;
02150 
02151   register const char
02152     *p;
02153 
02154   register long
02155     i;
02156 
02157   unsigned long
02158     lines;
02159 
02160   if (text == (char *) NULL)
02161     return((char **) NULL);
02162   for (p=text; *p != '\0'; p++)
02163     if (((int) ((unsigned char) *p) < 32) &&
02164         (isspace((int) ((unsigned char) *p)) == 0))
02165       break;
02166   if (*p == '\0')
02167     {
02168       register const char
02169         *q;
02170 
02171       /*
02172         Convert string to an ASCII list.
02173       */
02174       lines=1;
02175       for (p=text; *p != '\0'; p++)
02176         if (*p == '\n')
02177           lines++;
02178       textlist=(char **) AcquireQuantumMemory((size_t) lines+1UL,
02179         sizeof(*textlist));
02180       if (textlist == (char **) NULL)
02181         ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
02182       p=text;
02183       for (i=0; i < (long) lines; i++)
02184       {
02185         for (q=p; *q != '\0'; q++)
02186           if ((*q == '\r') || (*q == '\n'))
02187             break;
02188         textlist[i]=(char *) AcquireQuantumMemory((size_t) (q-p)+MaxTextExtent,
02189           sizeof(*textlist));
02190         if (textlist[i] == (char *) NULL)
02191           ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
02192         (void) CopyMagickString(textlist[i],p,(size_t) (q-p+1));
02193         if (*q == '\r')
02194           q++;
02195         p=q+1;
02196       }
02197     }
02198   else
02199     {
02200       char
02201         hex_string[MaxTextExtent];
02202 
02203       register char
02204         *q;
02205 
02206       register long
02207         j;
02208 
02209       /*
02210         Convert string to a HEX list.
02211       */
02212       lines=(unsigned long) (strlen(text)/0x14)+1;
02213       textlist=(char **) AcquireQuantumMemory((size_t) lines+1UL,
02214         sizeof(*textlist));
02215       if (textlist == (char **) NULL)
02216         ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
02217       p=text;
02218       for (i=0; i < (long) lines; i++)
02219       {
02220         textlist[i]=(char *) AcquireQuantumMemory(2UL*MaxTextExtent,
02221           sizeof(*textlist));
02222         if (textlist[i] == (char *) NULL)
02223           ThrowFatalException(ResourceLimitFatalError,"UnableToConvertText");
02224         (void) FormatMagickString(textlist[i],MaxTextExtent,"0x%08lx: ",0x14*i);
02225         q=textlist[i]+strlen(textlist[i]);
02226         for (j=1; j <= (long) MagickMin(strlen(p),0x14); j++)
02227         {
02228           (void) FormatMagickString(hex_string,MaxTextExtent,"%02x",*(p+j));
02229           (void) CopyMagickString(q,hex_string,MaxTextExtent);
02230           q+=2;
02231           if ((j % 0x04) == 0)
02232             *q++=' ';
02233         }
02234         for ( ; j <= 0x14; j++)
02235         {
02236           *q++=' ';
02237           *q++=' ';
02238           if ((j % 0x04) == 0)
02239             *q++=' ';
02240         }
02241         *q++=' ';
02242         for (j=1; j <= (long) MagickMin(strlen(p),0x14); j++)
02243         {
02244           if (isprint((int) ((unsigned char) *p)) != 0)
02245             *q++=(*p);
02246           else
02247             *q++='-';
02248           p++;
02249         }
02250         *q='\0';
02251       }
02252     }
02253   textlist[i]=(char *) NULL;
02254   return(textlist);
02255 }
02256 
02257 /*
02258 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
02259 %                                                                             %
02260 %                                                                             %
02261 %                                                                             %
02262 %   S t r i n g T o S t r i n g I n f o                                       %
02263 %                                                                             %
02264 %                                                                             %
02265 %                                                                             %
02266 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
02267 %
02268 %  StringToStringInfo() returns the contents of a file as a string.
02269 %
02270 %  The format of the StringToStringInfo method is:
02271 %
02272 %      StringInfo *StringToStringInfo(const char *string)
02273 %
02274 %  A description of each parameter follows:
02275 %
02276 %    o string:  The string.
02277 %
02278 */
02279 MagickExport StringInfo *StringToStringInfo(const char *string)
02280 {
02281   StringInfo
02282     *string_info;
02283 
02284   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
02285   assert(string != (const char *) NULL);
02286   string_info=AcquireStringInfo(strlen(string)+1);
02287   SetStringInfoDatum(string_info,(const unsigned char *) string);
02288   return(string_info);
02289 }
02290 
02291 /*
02292 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
02293 %                                                                             %
02294 %                                                                             %
02295 %                                                                             %
02296 %   S t r i p S t r i n g                                                     %
02297 %                                                                             %
02298 %                                                                             %
02299 %                                                                             %
02300 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
02301 %
02302 %  StripString() strips any whitespace or quotes from the beginning and end of
02303 %  a string of characters.
02304 %
02305 %  The format of the StripString method is:
02306 %
02307 %      void StripString(char *message)
02308 %
02309 %  A description of each parameter follows:
02310 %
02311 %    o message: Specifies an array of characters.
02312 %
02313 */
02314 MagickExport void StripString(char *message)
02315 {
02316   register char
02317     *p,
02318     *q;
02319 
02320   size_t
02321     length;
02322 
02323   assert(message != (char *) NULL);
02324   if (*message == '\0')
02325     return;
02326   length=strlen(message);
02327   p=message;
02328   while (isspace((int) ((unsigned char) *p)) != 0)
02329     p++;
02330   if ((*p == '\'') || (*p == '"'))
02331     p++;
02332   q=message+length-1;
02333   while ((isspace((int) ((unsigned char) *q)) != 0) && (q > p))
02334     q--;
02335   if (q > p)
02336     if ((*q == '\'') || (*q == '"'))
02337       q--;
02338   (void) CopyMagickMemory(message,p,(size_t) (q-p+1));
02339   message[q-p+1]='\0';
02340   for (p=message; *p != '\0'; p++)
02341     if (*p == '\n')
02342       *p=' ';
02343 }
02344 
02345 /*
02346 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
02347 %                                                                             %
02348 %                                                                             %
02349 %                                                                             %
02350 %   S u b s t i t u t e S t r i n g                                           %
02351 %                                                                             %
02352 %                                                                             %
02353 %                                                                             %
02354 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
02355 %
02356 %  SubstituteString() performs string substitution on a buffer, replacing the
02357 %  buffer with the substituted version. Buffer must be allocate from the heap.
02358 %
02359 %  The format of the SubstituteString method is:
02360 %
02361 %      MagickBooleanType SubstituteString(char **buffer,const char *search,
02362 %        const char *replace)
02363 %
02364 %  A description of each parameter follows:
02365 %
02366 %    o buffer: the buffer to perform replacements on. Replaced with new
02367 %      allocation if a replacement is made.
02368 %
02369 %    o search: String to search for.
02370 %
02371 %    o replace: Replacement string.
02372 %
02373 */
02374 MagickExport MagickBooleanType SubstituteString(char **buffer,
02375   const char *search,const char *replace)
02376 {
02377   char
02378     *result;
02379 
02380   const char
02381     *match,
02382     *source;
02383 
02384   MagickOffsetType
02385     destination_offset;
02386 
02387   size_t
02388     copy_length,
02389     length,
02390     replace_length,
02391     result_length,
02392     search_length;
02393 
02394   (void) LogMagickEvent(TraceEvent,GetMagickModule(),"...");
02395   assert(buffer != (char **) NULL);
02396   assert(*buffer != (char *) NULL);
02397   assert(search != (const char *) NULL);
02398   assert(replace != (const char *) NULL);
02399   if (strcmp(search,replace) == 0)
02400     return(MagickTrue);
02401   source=(*buffer);
02402   match=strstr(source,search);
02403   if (match == (char *) NULL)
02404     return(MagickFalse);
02405   result=(char *) NULL;
02406   length=strlen(source);
02407   if (~length >= MaxTextExtent)
02408     result=(char *) AcquireQuantumMemory(length+MaxTextExtent,sizeof(*result));
02409   if (result == (char *) NULL)
02410     ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
02411   *result='\0';
02412   result_length=0;
02413   destination_offset=0;
02414   replace_length=strlen(replace);
02415   for (search_length=strlen(search); match != (char *) NULL; )
02416   {
02417     /*
02418       Copy portion before match.
02419     */
02420     copy_length=(size_t) (match-source);
02421     if (copy_length != 0)
02422       {
02423         result_length+=copy_length;
02424         if ((result_length+MaxTextExtent) >= length)
02425           {
02426             length+=copy_length;
02427             result=(char *) ResizeQuantumMemory(result,length+MaxTextExtent,
02428               sizeof(*result));
02429             if (result == (char *) NULL)
02430               ThrowFatalException(ResourceLimitFatalError,
02431                 "UnableToAcquireString");
02432           }
02433         (void) CopyMagickString(result+destination_offset,source,copy_length+1);
02434         destination_offset+=copy_length;
02435       }
02436     /*
02437       Copy replacement.
02438     */
02439     result_length+=replace_length;
02440     if ((result_length+MaxTextExtent) >= length)
02441       {
02442         length+=replace_length;
02443         result=(char *) ResizeQuantumMemory(result,length+MaxTextExtent,
02444           sizeof(*result));
02445         if (result == (char *) NULL)
02446           ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
02447       }
02448     (void) ConcatenateMagickString(result+destination_offset,replace,length);
02449     destination_offset+=replace_length;
02450     /*
02451       Find next match.
02452     */
02453     source=match;
02454     source+=search_length;
02455     match=strstr(source,search);
02456   }
02457   /*
02458     Copy remaining string.
02459   */
02460   copy_length=strlen(source);
02461   result_length+=copy_length;
02462   if ((result_length+MaxTextExtent) >= length)
02463     {
02464       length+=copy_length;
02465       result=(char *) ResizeQuantumMemory(result,length+MaxTextExtent,
02466         sizeof(*result));
02467       if (result == (char *) NULL)
02468         ThrowFatalException(ResourceLimitFatalError,"UnableToAcquireString");
02469     }
02470   (void) ConcatenateMagickString(result+destination_offset,source,(size_t)
02471     (length+MaxTextExtent-destination_offset));
02472   (void) RelinquishMagickMemory(*buffer);
02473   *buffer=result;
02474   return(MagickTrue);
02475 }

Generated on Thu Jul 2 12:03:22 2009 for MagickCore by  doxygen 1.5.8