IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Dec 4, 2006, 12:15:04 PM (20 years ago)
Author:
Paul Price
Message:

Changing API for psStringSubstitute to something less prone to user mistake

File:
1 edited

Legend:

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

    r10286 r10446  
    1313 *  @author David Robbins, MHPCC
    1414 *
    15  *  @version $Revision: 1.46 $ $Name: not supported by cvs2svn $
    16  *  @date $Date: 2006-11-29 21:33:09 $
     15 *  @version $Revision: 1.47 $ $Name: not supported by cvs2svn $
     16 *  @date $Date: 2006-12-04 22:15:04 $
    1717 *
    1818 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     
    275275// given the input string, search for all copies of the key, and replace with the replacement value
    276276// the input string may be freed if not needed
    277 psString psStringSubstitute(psString input,
    278                             const char *replace,
    279                             const char *key)
    280 {
     277ssize_t psStringSubstitute(psString *input,
     278                           const char *replace,
     279                           const char *key)
     280{
     281    PS_ASSERT_PTR_NON_NULL(input, 0);
     282
    281283    // Empty input gives empty output
    282     if (!input || strlen(input) == 0) {
    283         return input;
     284    if (!*input || strlen(*input) == 0) {
     285        return 0;
    284286    }
    285287    // No key gives the same as the input
    286288    if (!key || strlen(key) == 0) {
    287         return input;
    288     }
    289     if (!psMemCheckString(input)) {
     289        return strlen(*input);
     290    }
     291    if (!psMemCheckString(*input)) {
    290292        psError(PS_ERR_BAD_PARAMETER_TYPE, true, "This function requires a psString as input.\n");
    291         return NULL;
     293        return 0;
    292294    }
    293295
     
    302304    }
    303305
    304     char *search = input;               // Search this string
     306    char *search = *input;              // Search this string
    305307    while (true) {
    306308        char *found = strstr(search, key); // Found occurence of the key
    307309        if (!found) {
    308             return input;
     310            return strlen(*input);
    309311        }
    310312
     
    313315
    314316        // this is safe since we will subtract strlen(key) elements from input
    315         psString output = psStringAlloc(strlen(input) + replaceLength + 1); // Output string
    316         int numChar = found - input;    // Number of characters to copy
     317        psString output = psStringAlloc(strlen(*input) + replaceLength + 1); // Output string
     318        int numChar = found - *input;    // Number of characters to copy
    317319
    318320        // copy the first segement into 'output'
    319         strncpy(output, input, numChar);
     321        strncpy(output, *input, numChar);
    320322
    321323        // copy the key replacement to the start of the key
     
    328330        strcpy(output + numChar, found + strlen(key));
    329331
    330         psFree(input);
    331         input = output;
     332        psFree(*input);
     333        *input = output;
    332334        search = output + numChar;
    333335    }
    334336
    335337    psAbort(__func__, "Should never get here.\n");
    336     return NULL;
     338    return 0;
    337339}
    338340
Note: See TracChangeset for help on using the changeset viewer.