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