码迷,mamicode.com
首页 > 其他好文 > 详细

LoadRunner中自定义C函数实现字符串替换

时间:2015-05-06 12:52:38      阅读:88      评论:0      收藏:0      [点我收藏+]

标签:

1.在globals.h 中定义一个函数ReplaceStr,实现字符串的替换:

int ReplaceStr(char* sSrc, char* sMatchStr, char* sReplaceStr)
{
        int StringLen;
        char caNewString[1024];
        char* findPos;
        merc_timer_handle_t timer_ReplaceStr = lr_start_timer();

        lr_debug_message(LR_MSG_CLASS_EXTENDED_LOG ,"Notify:Function ‘ReplaceStr‘ started.");
        findPos =(char *)strstr(sSrc, sMatchStr);
        if( (!findPos) || (!sMatchStr) ){
            lr_debug_message(LR_MSG_CLASS_EXTENDED_LOG ,"Notify:Function ‘ReplaceStr‘ ended with error!");
            return -1;
        }               

        while( findPos)
        {
                memset(caNewString, 0, sizeof(caNewString));
                StringLen = findPos - sSrc;
                strncpy(caNewString, sSrc, StringLen);
                strcat(caNewString, sReplaceStr);
                strcat(caNewString, findPos + strlen(sMatchStr));
                strcpy(sSrc, caNewString);

                findPos =(char *)strstr(sSrc, sMatchStr);
        }
        lr_debug_message(LR_MSG_CLASS_EXTENDED_LOG,"Result:%s",sSrc);
        free(findPos);
        lr_debug_message(LR_MSG_CLASS_EXTENDED_LOG ,"Notify:Function ‘ReplaceStr‘ ended (Duration: %lf)",
                         lr_end_timer(timer_ReplaceStr));
        return 0;
}

 

2.在init/Action/end 中调用:

char strView[1024];


  web_reg_save_param("viewState5",
        "LB=\"javax.faces.ViewState\" value=\"",
        "RB=\"",
        LAST);

sprintf(strView,lr_eval_string("{viewState5}"));

 ReplaceStr(strView,"+","%2B");
 ReplaceStr(strView,"/","%2F");
 ReplaceStr(strView,"=","%2D");

  lr_save_string(lr_eval_string(strView), "viewState6");

memset(strView,0,sizeof(strView));

 

LoadRunner中自定义C函数实现字符串替换

标签:

原文地址:http://www.cnblogs.com/qmfsun/p/4481133.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!