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

复制代码
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));
复制代码

 

posted @   Agoly  阅读(1810)  评论(0编辑  收藏  举报
努力加载评论中...
点击右上角即可分享
微信分享提示