summaryrefslogtreecommitdiffstats
path: root/tools/lib/h5tools_str.c
diff options
context:
space:
mode:
authorPeter Cao <xcao@hdfgroup.org>2012-03-12 21:27:11 (GMT)
committerPeter Cao <xcao@hdfgroup.org>2012-03-12 21:27:11 (GMT)
commitfcf96afeb24119690187ac3907d23070f0ba99d1 (patch)
treebc32b923260a7d42dca3f14df655c87762fcf48a /tools/lib/h5tools_str.c
parentd7f3dc097ef2c8ced8068879044ce6b9fdd9936e (diff)
downloadhdf5-fcf96afeb24119690187ac3907d23070f0ba99d1.zip
hdf5-fcf96afeb24119690187ac3907d23070f0ba99d1.tar.gz
hdf5-fcf96afeb24119690187ac3907d23070f0ba99d1.tar.bz2
[svn-r22052] - h5dump: Added capability for "-a" option to show attributes containing "/"
by using an escape character. For example, for a dataset "/dset" containing attribute "speed(m/h)", use "h5dump -a "/dset/speed(\/h)" to show the content of the attribute. See details at HDFFV-7523
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 b0a3ed8..795107a 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 = malloc ( strlen ( oldstr ) - strlen ( substr ) + strlen ( replacement ) + 1 );
+
+ if ( newstr == NULL ){
+ free (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 );
+ free (oldstr);
+ }
+
+ return newstr;
+}