IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jul 10, 2006, 10:15:08 AM (20 years ago)
Author:
Paul Price
Message:

Reverting psString to previous version (with some small improvements) that didn't cause memory leaks.

File:
1 edited

Legend:

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

    r7831 r7852  
    1313 *  @author David Robbins, MHPCC
    1414 *
    15  *  @version $Revision: 1.34 $ $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 $
    1717 *
    1818 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     
    2626#include "psError.h"
    2727#include "psAssert.h"
     28#include "psAbort.h"
    2829
    2930#include "psErrorText.h"
     
    218219// given the input string, search for all copies of the key, and replace with the replacement value
    219220// the input string may be freed if not needed
    220 char *psStringSubstitute(char *input,
    221                          const char *replace,
    222                          const char *key)
     221psString psStringSubstitute(psString input,
     222                            const char *replace,
     223                            const char *key)
    223224{
    224225    if (input == NULL ) {
     
    231232        return NULL;
    232233    }
    233     if (replace == NULL) {
     234    if (key == NULL || strlen(key) == 0) {
    234235        return NULL;
    235236    }
    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
    243247    while (true) {
    244         //        char *p = strstr (input, key);
    245         char *p = strstr (in, key);
     248        char *p = strstr(input, key);
    246249        if (!p) {
    247             //            return input;
    248             //            strcpy(input, in);
    249             //            psFree(in);
    250             return in;
     250            return input;
    251251        }
    252252
     
    255255
    256256        // 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
    260259
    261260        // 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);
    265262
    266263        // 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        }
    269268
    270269        // copy the remainder to the end of the replacement
    271270        strcpy(&output[Nc], p + strlen(key));
    272271
    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;
    287278}
    288279
Note: See TracChangeset for help on using the changeset viewer.