IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jul 6, 2006, 12:26:13 PM (20 years ago)
Author:
drobbin
Message:

Added test for psStringSubstitute. Ran test, fixed errors. Fxn now returns a fresh string to allow for strings longer than the original input.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/sys/psString.c

    r7766 r7831  
    1313 *  @author David Robbins, MHPCC
    1414 *
    15  *  @version $Revision: 1.33 $ $Name: not supported by cvs2svn $
    16  *  @date $Date: 2006-06-30 02:20:06 $
     15 *  @version $Revision: 1.34 $ $Name: not supported by cvs2svn $
     16 *  @date $Date: 2006-07-06 22:26:13 $
    1717 *
    1818 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     
    218218// given the input string, search for all copies of the key, and replace with the replacement value
    219219// the input string may be freed if not needed
    220 char *psStringSubstitute(char *input, const char *replace, const char *key)
    221 {
    222     if (key == NULL || strlen(key) == 0) {
    223         return input;
    224     }
    225 
     220char *psStringSubstitute(char *input,
     221                         const char *replace,
     222                         const char *key)
     223{
     224    if (input == NULL ) {
     225        psError(PS_ERR_BAD_PARAMETER_NULL, true,
     226                "Invalid string in psStringSubstitute.  Input cannot be NULL or empty.\n");
     227        return NULL;
     228    } else if (strlen(input) == 0) {
     229        psError(PS_ERR_BAD_PARAMETER_NULL, true,
     230                "Invalid string in psStringSubstitute.  Input cannot be NULL or empty.\n");
     231        return NULL;
     232    }
     233    if (replace == NULL) {
     234        return NULL;
     235    }
     236    if (key == NULL ) {
     237        return NULL;
     238    } else if (strlen(key) == 0) {
     239        return NULL;
     240    }
     241
     242    psString in = psStringCopy(input);
    226243    while (true) {
    227         char *p = strstr (input, key);
     244        //        char *p = strstr (input, key);
     245        char *p = strstr (in, key);
    228246        if (!p) {
    229             return input;
     247            //            return input;
     248            //            strcpy(input, in);
     249            //            psFree(in);
     250            return in;
    230251        }
    231252
     
    234255
    235256        // this is safe since we will subtract strlen(key) elements from input
    236         char *output = psAlloc(strlen(input) + strlen(replace) + 1);
    237         int Nc = p - input;
     257        //        char *output = psAlloc(strlen(input) + strlen(replace) + 1);
     258        //        int Nc = p - input;
     259        int Nc = p - in;
    238260
    239261        // copy the first segement into 'output'
    240         strncpy(output, input, Nc);
     262        psString output = psStringNCopy(in, strlen(in)+strlen(replace)+1);
     263
     264        //        strncpy(output, input, Nc);
    241265
    242266        // copy the key replacement to the start of the key
     
    247271        strcpy(&output[Nc], p + strlen(key));
    248272
    249         psFree(input);
    250         input = output;
    251     }
    252     return input;
     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;
    253287}
    254288
Note: See TracChangeset for help on using the changeset viewer.