summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVarun Chandramohan <varunc@linux.vnet.ibm.com>2009-06-20 09:37:54 +0530
committerJosh Stone <jistone@redhat.com>2009-06-22 14:09:04 -0700
commit7e788c262ae119a9e1b378605ce649cf1ae3f6cb (patch)
tree2d884ab619352d3e599ffc18eb694d92f09cc088
parent0c4598c3428d896f0947853c54c3e0b4a71a91e6 (diff)
downloadsystemtap-steved-7e788c262ae119a9e1b378605ce649cf1ae3f6cb.tar.gz
systemtap-steved-7e788c262ae119a9e1b378605ce649cf1ae3f6cb.tar.xz
systemtap-steved-7e788c262ae119a9e1b378605ce649cf1ae3f6cb.zip
Add str_replace() To Tapsets
This patch adds a search and replace string functionality to existing tapsets. The functionality is as follows: The function takes in a parent string and searches for a substring as specified by the user. If substring not found, the parent string is returned. If substring is found, it is replaced by another string and returned. NOTE: The function will search and replace all the occurrence of substrings in a parent string when matched. Signed-off-by: Varun Chandramohan <varunc@linux.vnet.ibm.com> Signed-off-by: Josh Stone <jistone@redhat.com>
-rw-r--r--tapset/string.stp33
1 files changed, 33 insertions, 0 deletions
diff --git a/tapset/string.stp b/tapset/string.stp
index cc842929..e4a5fd39 100644
--- a/tapset/string.stp
+++ b/tapset/string.stp
@@ -98,6 +98,39 @@ function tokenize:string(input:string, delim:string)
}
%}
+/** @addtogroup library
+ * @code str_replace:string (prnt_str:string, srch_str:string, rplc_str:string) @endcode
+ * @param prnt_str The parent string.
+ * @param srch_str The substring which is used to search in the parent string prnt_str.
+ * @param rplc_str The substring which is used to replace the searched string srch_str.
+ * @return Returns the parent string with substrings replaced. Else returns parent string.
+ */
+function str_replace:string (prnt_str:string, srch_str:string, rplc_str:string)
+%{
+ char *ptr = THIS->prnt_str;
+ char *ptr_base = THIS->prnt_str;
+ int strlen_prnt_str = strlen(THIS->prnt_str);
+ int strlen_srch_str = strlen(THIS->srch_str);
+ int strlen_rplc_str = strlen(THIS->rplc_str);
+
+ if(strlen_srch_str == 0) {
+ strlcat(THIS->__retvalue, ptr_base, MAXSTRINGLEN);
+ return;
+ }
+
+ while((ptr = strstr(ptr, THIS->srch_str)) != NULL) {
+
+ *ptr = '\0';
+ strlcat(THIS->__retvalue, ptr_base, MAXSTRINGLEN);
+ strlcat(THIS->__retvalue, THIS->rplc_str, MAXSTRINGLEN);
+ ptr = ptr + strlen_srch_str;
+ ptr_base = ptr;
+ }
+
+ strlcat(THIS->__retvalue, ptr_base, MAXSTRINGLEN);
+ return;
+%}
+
/*
* strtol - Convert a string to a long
* str String to convert