Index: /trunk/psLib/src/sys/psString.c
===================================================================
--- /trunk/psLib/src/sys/psString.c	(revision 7830)
+++ /trunk/psLib/src/sys/psString.c	(revision 7831)
@@ -13,6 +13,6 @@
  *  @author David Robbins, MHPCC
  *
- *  @version $Revision: 1.33 $ $Name: not supported by cvs2svn $
- *  @date $Date: 2006-06-30 02:20:06 $
+ *  @version $Revision: 1.34 $ $Name: not supported by cvs2svn $
+ *  @date $Date: 2006-07-06 22:26:13 $
  *
  *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -218,14 +218,35 @@
 // given the input string, search for all copies of the key, and replace with the replacement value
 // the input string may be freed if not needed
-char *psStringSubstitute(char *input, const char *replace, const char *key)
-{
-    if (key == NULL || strlen(key) == 0) {
-        return input;
-    }
-
+char *psStringSubstitute(char *input,
+                         const char *replace,
+                         const char *key)
+{
+    if (input == NULL ) {
+        psError(PS_ERR_BAD_PARAMETER_NULL, true,
+                "Invalid string in psStringSubstitute.  Input cannot be NULL or empty.\n");
+        return NULL;
+    } else if (strlen(input) == 0) {
+        psError(PS_ERR_BAD_PARAMETER_NULL, true,
+                "Invalid string in psStringSubstitute.  Input cannot be NULL or empty.\n");
+        return NULL;
+    }
+    if (replace == NULL) {
+        return NULL;
+    }
+    if (key == NULL ) {
+        return NULL;
+    } else if (strlen(key) == 0) {
+        return NULL;
+    }
+
+    psString in = psStringCopy(input);
     while (true) {
-        char *p = strstr (input, key);
+        //        char *p = strstr (input, key);
+        char *p = strstr (in, key);
         if (!p) {
-            return input;
+            //            return input;
+            //            strcpy(input, in);
+            //            psFree(in);
+            return in;
         }
 
@@ -234,9 +255,12 @@
 
         // this is safe since we will subtract strlen(key) elements from input
-        char *output = psAlloc(strlen(input) + strlen(replace) + 1);
-        int Nc = p - input;
+        //        char *output = psAlloc(strlen(input) + strlen(replace) + 1);
+        //        int Nc = p - input;
+        int Nc = p - in;
 
         // copy the first segement into 'output'
-        strncpy(output, input, Nc);
+        psString output = psStringNCopy(in, strlen(in)+strlen(replace)+1);
+
+        //        strncpy(output, input, Nc);
 
         // copy the key replacement to the start of the key
@@ -247,8 +271,18 @@
         strcpy(&output[Nc], p + strlen(key));
 
-        psFree(input);
-        input = output;
-    }
-    return input;
+        //        psFree(input);
+        //        input = output;
+        //        psFree(in);
+        //        strcpy(in, "\0");
+        //in = psStringCopy("\0");
+        psFree(in);
+        //        input = output;
+        //        strcpy(in, output);
+        in = psStringCopy(output);
+        psFree(output);
+    }
+    //    strcpy(input, in);
+    //    psFree(in);
+    return in;
 }
 
Index: /trunk/psLib/test/sys/tst_psString.c
===================================================================
--- /trunk/psLib/test/sys/tst_psString.c	(revision 7830)
+++ /trunk/psLib/test/sys/tst_psString.c	(revision 7831)
@@ -20,6 +20,6 @@
  *  @author  Eric Van Alst, MHPCC
  *
- *  @version $Revision: 1.8 $  $Name: not supported by cvs2svn $
- *  @date  $Date: 2006-06-08 01:06:36 $
+ *  @version $Revision: 1.9 $  $Name: not supported by cvs2svn $
+ *  @date  $Date: 2006-07-06 22:26:13 $
  *
  *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
@@ -53,4 +53,5 @@
 static psS32 testStrSplit00(void);
 static psS32 testNULLStrings(void);
+static psS32 testStrSub(void);
 
 testDescription tests[] = {
@@ -74,4 +75,5 @@
                               {testStrSplit00,15, "Test String Splitting", 0, false},
                               {testNULLStrings,666, "Test NULL String Error Handling", 0, false},
+                              {testStrSub,16, "Test String Substitute", 0, false},
                               {NULL}
                           };
@@ -710,2 +712,82 @@
 }
 
+static psS32 testStrSub(void)
+{
+    psString input = NULL;
+    char str[35];
+    strncpy(str, "This is, a, test case, to check.", 35);
+
+    //Return str for NULL key
+    input = psStringSubstitute(str, ",", NULL);
+    if (input != NULL) {
+        psError(PS_ERR_BAD_PARAMETER_NULL, true,
+                "psStringSubstitute failed to return unchanged str for NULL key.\n");
+        psFree(input);
+        return 1;
+    }
+    //Return NULL for empty key
+    input = psStringSubstitute(str, ",", "");
+    if (input != NULL) {
+        psError(PS_ERR_BAD_PARAMETER_NULL, true,
+                "psStringSubstitute failed to return unchanged str for empty key.\n");
+        psFree(input);
+        return 2;
+    }
+    //Return NULL for NULL replace
+    input = psStringSubstitute(str, NULL, ",");
+    if (input != NULL) {
+        psError(PS_ERR_BAD_PARAMETER_NULL, true,
+                "psStringSubstitute failed to return unchanged str for NULL replace.\n");
+        psFree(input);
+        return 3;
+    }
+    //Return NULL for NULL input
+    psLogMsg(__func__,PS_LOG_INFO,"Following should generate error message");
+    input = psStringSubstitute(NULL, "", ",");
+    if (input != NULL) {
+        psError(PS_ERR_BAD_PARAMETER_NULL, true,
+                "psStringSubstitute failed to return NULL for NULL input.\n");
+        psFree(input);
+        return 4;
+    }
+    //Return empty string for empty input string
+    psLogMsg(__func__,PS_LOG_INFO,"Following should generate error message");
+    input = psStringSubstitute("", "", ",");
+    if (input != NULL) {
+        psError(PS_ERR_BAD_PARAMETER_NULL, true,
+                "psStringSubstitute failed to return empty string for empty input string.\n");
+        psFree(input);
+        return 5;
+    }
+    //Check valid test case, changing commas to exclamation points
+    input = psStringSubstitute(str, "!", ",");
+    if (strcmp(input, "This is! a! test case! to check.") != 0 ) {
+        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
+                "psStringSubstitute failed to return the correct output string.\n");
+        psFree(input);
+        return 7;
+    }
+    psFree(input);
+    input = NULL;
+    //Check valid test case, remove exclamation points
+    input = psStringSubstitute(str, "", ",");
+    if (strcmp(input, "This is a test case to check.") != 0 ) {
+        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
+                "psStringSubstitute failed to return the correct output string.\n");
+        psFree(input);
+        return 8;
+    }
+    psFree(input);
+    input = NULL;
+    //Check case where replacement is long.  Should still work now.
+    input = psStringSubstitute(str, "; This string is too long to fit in str(35 chars)", ".");
+    if (strcmp(input,"This is, a, test case, to check; This string is too long to fit in str(35 chars)") != 0 ) {
+        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
+                "psStringSubstitute failed to return the correct output string.\n");
+        psFree(input);
+        return 9;
+    }
+    psFree(input);
+
+    return 0;
+}
Index: /trunk/psLib/test/sys/verified/tst_psString.stderr
===================================================================
--- /trunk/psLib/test/sys/verified/tst_psString.stderr	(revision 7830)
+++ /trunk/psLib/test/sys/verified/tst_psString.stderr	(revision 7831)
@@ -175,2 +175,19 @@
 ---> TESTPOINT PASSED (psString{Test NULL String Error Handling} | tst_psString.c)
 
+/***************************** TESTPOINT ******************************************\
+*             TestFile: tst_psString.c                                             *
+*            TestPoint: psString{Test String Substitute}                           *
+*             TestType: Positive                                                   *
+\**********************************************************************************/
+
+<DATE><TIME>|<HOST>|I|testStrSub
+    Following should generate error message
+<DATE><TIME>|<HOST>|E|psStringSubstitute (FILE:LINENO)
+    Invalid string in psStringSubstitute.  Input cannot be NULL or empty.
+<DATE><TIME>|<HOST>|I|testStrSub
+    Following should generate error message
+<DATE><TIME>|<HOST>|E|psStringSubstitute (FILE:LINENO)
+    Invalid string in psStringSubstitute.  Input cannot be NULL or empty.
+
+---> TESTPOINT PASSED (psString{Test String Substitute} | tst_psString.c)
+
