IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Apr 17, 2006, 12:00:35 PM (20 years ago)
Author:
magnier
Message:

variety of small changes related to inconsistencies between psModules and psLib

File:
1 edited

Legend:

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

    r6278 r6874  
    1313 *  @author David Robbins, MHPCC
    1414 *
    15  *  @version $Revision: 1.24 $ $Name: not supported by cvs2svn $
    16  *  @date $Date: 2006-02-01 20:40:56 $
     15 *  @version $Revision: 1.25 $ $Name: not supported by cvs2svn $
     16 *  @date $Date: 2006-04-17 22:00:03 $
    1717 *
    1818 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     
    214214}
    215215
     216// given the input string, search for all copies of the key, and replace with the replacement value
     217// the input string may be freed if not needed
     218char *psStringSubstitute (char *input, char *replace, char *key)
     219{
     220
     221    char *p;
     222
     223    if (key == NULL)
     224        return input;
     225    if (strlen(key) == 0)
     226        return input;
     227
     228    while (true) {
     229        p = strstr (input, key);
     230        if (p == NULL)
     231            return input;
     232
     233        // we have input = xxxkeyxxx
     234        // we want output = xxxreplacexxx
     235
     236        // this is safe since we will subtract strlen(key) elements from input
     237        char *output = psAlloc(strlen(input) + strlen(replace) + 1);
     238        int Nc = p - input;
     239
     240        // copy the first segement into 'output'
     241        strncpy (output, input, Nc);
     242
     243        // copy the key replacement to the start of the key
     244
     245        strcpy (&output[Nc], replace);
     246        Nc += strlen (replace);
     247
     248        // copy the remainder to the end of the replacement
     249        strcpy (&output[Nc], p + strlen(key));
     250
     251        psFree (input);
     252        input = output;
     253    }
     254    return input;
     255}
     256
Note: See TracChangeset for help on using the changeset viewer.