Changeset 7852 for trunk/psLib/src/sys/psString.c
- Timestamp:
- Jul 10, 2006, 10:15:08 AM (20 years ago)
- File:
-
- 1 edited
-
trunk/psLib/src/sys/psString.c (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/sys/psString.c
r7831 r7852 13 13 * @author David Robbins, MHPCC 14 14 * 15 * @version $Revision: 1.3 4$ $Name: not supported by cvs2svn $16 * @date $Date: 2006-07- 06 22:26:13$15 * @version $Revision: 1.35 $ $Name: not supported by cvs2svn $ 16 * @date $Date: 2006-07-10 20:15:08 $ 17 17 * 18 18 * Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii … … 26 26 #include "psError.h" 27 27 #include "psAssert.h" 28 #include "psAbort.h" 28 29 29 30 #include "psErrorText.h" … … 218 219 // given the input string, search for all copies of the key, and replace with the replacement value 219 220 // the input string may be freed if not needed 220 char *psStringSubstitute(char *input,221 const char *replace,222 const char *key)221 psString psStringSubstitute(psString input, 222 const char *replace, 223 const char *key) 223 224 { 224 225 if (input == NULL ) { … … 231 232 return NULL; 232 233 } 233 if ( replace == NULL) {234 if (key == NULL || strlen(key) == 0) { 234 235 return NULL; 235 236 } 236 if (key == NULL ) { 237 return NULL; 238 } else if (strlen(key) == 0) { 239 return NULL; 240 } 241 242 psString in = psStringCopy(input); 237 238 // replace == NULL is valid: it just means that we strip out the key 239 // without putting anything else in its place 240 size_t replaceLength; // Size of "replace" string 241 if (replace) { 242 replaceLength = strlen(replace); 243 } else { 244 replaceLength = 0; 245 } 246 243 247 while (true) { 244 // char *p = strstr (input, key); 245 char *p = strstr (in, key); 248 char *p = strstr(input, key); 246 249 if (!p) { 247 // return input; 248 // strcpy(input, in); 249 // psFree(in); 250 return in; 250 return input; 251 251 } 252 252 … … 255 255 256 256 // this is safe since we will subtract strlen(key) elements from input 257 // char *output = psAlloc(strlen(input) + strlen(replace) + 1); 258 // int Nc = p - input; 259 int Nc = p - in; 257 psString output = psAlloc(strlen(input) + replaceLength + 1); // Output string 258 int Nc = p - input; // Number of characters to copy 260 259 261 260 // copy the first segement into 'output' 262 psString output = psStringNCopy(in, strlen(in)+strlen(replace)+1); 263 264 // strncpy(output, input, Nc); 261 strncpy(output, input, Nc); 265 262 266 263 // copy the key replacement to the start of the key 267 strcpy(&output[Nc], replace); 268 Nc += strlen (replace); 264 if (replace && replaceLength > 0) { 265 strcpy(&output[Nc], replace); 266 Nc += replaceLength; 267 } 269 268 270 269 // copy the remainder to the end of the replacement 271 270 strcpy(&output[Nc], p + strlen(key)); 272 271 273 // psFree(input); 274 // input = output; 275 // psFree(in); 276 // strcpy(in, "\0"); 277 //in = psStringCopy("\0"); 278 psFree(in); 279 // input = output; 280 // strcpy(in, output); 281 in = psStringCopy(output); 282 psFree(output); 283 } 284 // strcpy(input, in); 285 // psFree(in); 286 return in; 272 psFree(input); 273 input = output; 274 } 275 276 psAbort(__func__, "Should never get here.\n"); 277 return NULL; 287 278 } 288 279
Note:
See TracChangeset
for help on using the changeset viewer.
