summaryrefslogtreecommitdiffstats
path: root/tools/lib
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
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')
-rw-r--r--tools/lib/h5tools_str.c49
-rw-r--r--tools/lib/h5tools_str.h2
2 files changed, 51 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;
+}
diff --git a/tools/lib/h5tools_str.h b/tools/lib/h5tools_str.h
index 9527a56..38697c6 100644
--- a/tools/lib/h5tools_str.h
+++ b/tools/lib/h5tools_str.h
@@ -47,5 +47,7 @@ H5TOOLS_DLL void h5tools_str_sprint_region(h5tools_str_t *str, const h5tool_
H5TOOLS_DLL char *h5tools_str_sprint(h5tools_str_t *str, const h5tool_format_t *info,
hid_t container, hid_t type, void *vp,
h5tools_context_t *ctx);
+H5TOOLS_DLL char *h5tools_str_replace ( const char *string, const char *substr,
+ const char *replacement );
#endif /* H5TOOLS_STR_H__ */