summaryrefslogtreecommitdiffstats
path: root/tools/lib/h5tools_str.c
diff options
context:
space:
mode:
authorAllen Byrne <byrn@hdfgroup.org>2012-07-13 16:31:52 (GMT)
committerAllen Byrne <byrn@hdfgroup.org>2012-07-13 16:31:52 (GMT)
commit8d05b733b91f4d74d191b5fd7138024e9546c66e (patch)
tree8e93814c8b299db65c8cd3397161a66a61657c6c /tools/lib/h5tools_str.c
parentd9ba5c234c9971b67f6922ee85cabb638fc0e186 (diff)
downloadhdf5-8d05b733b91f4d74d191b5fd7138024e9546c66e.zip
hdf5-8d05b733b91f4d74d191b5fd7138024e9546c66e.tar.gz
hdf5-8d05b733b91f4d74d191b5fd7138024e9546c66e.tar.bz2
[svn-r22572] Re-Merge attr name with slash changes from trunk
Tested: localinux with cmake
Diffstat (limited to 'tools/lib/h5tools_str.c')
-rw-r--r--tools/lib/h5tools_str.c49
1 files changed, 49 insertions, 0 deletions
diff --git a/tools/lib/h5tools_str.c b/tools/lib/h5tools_str.c
index 2bbafd8..c069ecc 100644
--- a/tools/lib/h5tools_str.c
+++ b/tools/lib/h5tools_str.c
@@ -1363,3 +1363,52 @@ h5tools_str_is_zero(const void *_mem, size_t size)
return TRUE;
}
+
+/*-------------------------------------------------------------------------
+ * Function: h5tools_str_replace
+ *
+ * Purpose: replace all occurrences of substring.
+ *
+ * Return: char *
+ *
+ * Programmer: Peter Cao
+ * March 8, 2012
+ *
+ * Notes:
+ * Applications need to call free() to free the memoery allocated for
+ * the return string
+ *
+ *-------------------------------------------------------------------------
+ */
+char *
+h5tools_str_replace ( const char *string, const char *substr, const char *replacement )
+{
+ char *tok = NULL;
+ char *newstr = NULL;
+ char *oldstr = NULL;
+ char *head = NULL;
+
+ if ( substr == NULL || replacement == NULL )
+ return strdup (string);
+
+ newstr = strdup (string);
+ head = newstr;
+ while ( (tok = strstr ( head, substr ))){
+ oldstr = newstr;
+ newstr = HDmalloc ( strlen ( oldstr ) - strlen ( substr ) + strlen ( replacement ) + 1 );
+
+ if ( newstr == NULL ){
+ HDfree (oldstr);
+ return NULL;
+ }
+ memcpy ( newstr, oldstr, tok - oldstr );
+ memcpy ( newstr + (tok - oldstr), replacement, strlen ( replacement ) );
+ memcpy ( newstr + (tok - oldstr) + strlen( replacement ), tok + strlen ( substr ), strlen ( oldstr ) - strlen ( substr ) - ( tok - oldstr ) );
+ memset ( newstr + strlen ( oldstr ) - strlen ( substr ) + strlen ( replacement ) , 0, 1 );
+ /* move back head right after the last replacement */
+ head = newstr + (tok - oldstr) + strlen( replacement );
+ HDfree (oldstr);
+ }
+
+ return newstr;
+}