IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 6567


Ignore:
Timestamp:
Mar 9, 2006, 9:37:28 AM (20 years ago)
Author:
magnier
Message:

added psStringSubstitute

Location:
branches/rel10_ifa/psLib/src/sys
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/rel10_ifa/psLib/src/sys/psString.c

    r6278 r6567  
    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.24.4.1 $ $Name: not supported by cvs2svn $
     16 *  @date $Date: 2006-03-09 19:37:28 $
    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
  • branches/rel10_ifa/psLib/src/sys/psString.h

    r6278 r6567  
    1414 *  @author David Robbins, MHPCC
    1515 *
    16  *  @version $Revision: 1.18 $ $Name: not supported by cvs2svn $
    17  *  @date $Date: 2006-02-01 20:40:56 $
     16 *  @version $Revision: 1.18.4.1 $ $Name: not supported by cvs2svn $
     17 *  @date $Date: 2006-03-09 19:37:28 $
    1818 *
    1919 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     
    103103);
    104104
     105// given the input string, search for all copies of the key, and replace with the replacement value
     106// the input string may be freed if not needed
     107char *psStringSubstitute (
     108    char *input,    ///< input string to be modified
     109    char *replace,    ///< replacement value
     110    char *key    ///< string to be replaced in input
     111);
     112
    105113/** @} */// Doxygen - End of SystemGroup Functions
    106114
Note: See TracChangeset for help on using the changeset viewer.