Changeset 303 for WizardsToolkit/trunk

Show
Ignore:
Timestamp:
10/05/09 20:12:02 (5 months ago)
Author:
cristy
Message:
 
Files:
1 modified

Legend:

Unmodified
Added
Removed
  • WizardsToolkit/trunk/wizard/string.c

    r230 r303  
    21672167% 
    21682168%  SubstituteString() performs string substitution on a buffer, replacing the 
    2169 %  buffer with the substituted version. Buffer must be allocate from the heap. 
     2169%  buffer with the substituted version. Buffer must be allocated from the heap. 
     2170%  If the string is matched and status, WizardTrue is returned otherwise 
     2171%  WizardFalse. 
    21702172% 
    21712173%  The format of the SubstituteString method is: 
     
    21762178%  A description of each parameter follows: 
    21772179% 
    2178 %    o buffer: The buffer to perform replacements on. Replaced with new 
     2180%    o buffer: the buffer to perform replacements on;  replaced with new 
    21792181%      allocation if a replacement is made. 
    21802182% 
    2181 %    o search: String to search for. 
    2182 % 
    2183 %    o replace: Replacement string. 
     2183%    o search: search for this string. 
     2184% 
     2185%    o replace: replace any matches with this string. 
    21842186% 
    21852187*/ 
     
    21872189  const char *search,const char *replace) 
    21882190{ 
    2189   char 
    2190     *result; 
    2191  
    2192   const char 
    2193     *match, 
    2194     *source; 
     2191  WizardBooleanType 
     2192    status; 
     2193 
     2194  register char 
     2195    *p; 
     2196 
     2197  register size_t 
     2198    i; 
    21952199 
    21962200  size_t 
    2197     length, 
    2198     copy_length, 
    2199     replace_length, 
    2200     result_length, 
    2201     search_length; 
    2202  
    2203   WizardOffsetType 
    2204     destination_offset; 
    2205  
    2206   (void) LogWizardEvent(TraceEvent,GetWizardModule(),"..."); 
    2207   assert(buffer != (char **) NULL); 
    2208   assert(*buffer != (char *) NULL); 
    2209   assert(search != (const char *) NULL); 
    2210   assert(replace != (const char *) NULL); 
    2211   if (strcmp(search,replace) == 0) 
    2212     return(WizardTrue); 
    2213   source=(*buffer); 
    2214   match=strstr(source,search); 
    2215   if (match == (char *) NULL) 
    2216     return(WizardFalse); 
    2217   result=(char *) NULL; 
    2218   length=strlen(source); 
    2219   if (~length >= MaxTextExtent) 
    2220     result=(char *) AcquireQuantumMemory(length+MaxTextExtent,sizeof(*result)); 
    2221   if (result == (char *) NULL) 
    2222     ThrowFatalException(ResourceFatalError,"memory allocation failed `%s'"); 
    2223   *result='\0'; 
    2224   result_length=0; 
    2225   destination_offset=0; 
    2226   replace_length=strlen(replace); 
    2227   for (search_length=strlen(search); match != (char *) NULL; ) 
     2201    extent, 
     2202    replace_extent, 
     2203    search_extent; 
     2204 
     2205  status=WizardFalse; 
     2206  search_extent=0, 
     2207  replace_extent=0; 
     2208  p=(*buffer); 
     2209  for (i=0; *(p+i) != '\0'; i++) 
    22282210  { 
     2211    if (*(p+i) != *search) 
     2212      continue; 
     2213    if (strcmp(p+i,search) != 0) 
     2214      continue; 
    22292215    /* 
    2230       Copy portion before match. 
     2216      We found a match. 
    22312217    */ 
    2232     copy_length=(size_t) (match-source); 
    2233     if (copy_length != 0) 
     2218    if (search_extent == 0) 
     2219      search_extent=strlen(search); 
     2220    if (replace_extent == 0) 
     2221      replace_extent=strlen(replace); 
     2222    if (replace_extent > search_extent) 
    22342223      { 
    2235         result_length+=copy_length; 
    2236         if ((result_length+MaxTextExtent) >= length) 
    2237           { 
    2238             length+=copy_length; 
    2239             result=(char *) ResizeQuantumMemory(result, 
    2240               (length+MaxTextExtent),sizeof(*result)); 
    2241             if (result == (char *) NULL) 
    2242               ThrowFatalException(ResourceFatalError, 
    2243                 "memory allocation failed `%s'"); 
    2244           } 
    2245         (void) CopyWizardString(result+destination_offset,source,copy_length+1); 
    2246         destination_offset+=copy_length; 
    2247       } 
    2248     /* 
    2249       Copy replacement. 
    2250     */ 
    2251     result_length+=replace_length; 
    2252     if ((result_length+MaxTextExtent) >= length) 
    2253       { 
    2254         length+=replace_length; 
    2255         result=(char *) ResizeQuantumMemory(result, 
    2256           length+MaxTextExtent,sizeof(*result)); 
    2257         if (result == (char *) NULL) 
     2224        /* 
     2225          Make room for the replacement string. 
     2226        */ 
     2227        extent=strlen(p)+replace_extent-search_extent; 
     2228        p=(char *) ResizeQuantumMemory(p,extent+MaxTextExtent,sizeof(*p)); 
     2229        *buffer=p; 
     2230        if (p == (char *) NULL) 
    22582231          ThrowFatalException(ResourceFatalError, 
    22592232            "memory allocation failed `%s'"); 
    22602233      } 
    2261     (void) ConcatenateWizardString(result+destination_offset,replace,length+ 
    2262       MaxTextExtent); 
    2263     destination_offset+=replace_length; 
    22642234    /* 
    2265       Find next match. 
     2235      Replace string. 
    22662236    */ 
    2267     source=match; 
    2268     source+=search_length; 
    2269     match=strstr(source,search); 
     2237    status=WizardTrue; 
     2238    if (search_extent != replace_extent) 
     2239      (void) CopyWizardMemory(p+replace_extent+i,p+search_extent+i, 
     2240        strlen(p+search_extent+i)+1); 
     2241    (void) CopyWizardMemory(p+i,replace,replace_extent); 
     2242    i+=replace_extent; 
    22702243  } 
    2271   /* 
    2272     Copy remaining string. 
    2273   */ 
    2274   copy_length=strlen(source); 
    2275   result_length+=copy_length; 
    2276   if ((result_length+MaxTextExtent) >= length) 
    2277     { 
    2278       length+=copy_length; 
    2279       result=(char *) ResizeQuantumMemory(result,length+MaxTextExtent, 
    2280         sizeof(*result)); 
    2281       if (result == (char *) NULL) 
    2282         ThrowFatalException(ResourceFatalError,"memory allocation failed `%s'"); 
    2283     } 
    2284   (void) ConcatenateWizardString(result+destination_offset,source,(size_t) 
    2285     (length+MaxTextExtent-destination_offset)); 
    2286   (void) RelinquishWizardMemory(*buffer); 
    2287   *buffer=result; 
    2288   return(WizardTrue); 
    2289 } 
     2244  return(status); 
     2245}