| 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++) |
| 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) |
| 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 | } |