Loadrunner C代码字符串操作

时间:2012-02-25 23:19:52

标签: c loadrunner

我在google搜索时遇到了以下代码,效果很好。 (感谢Chaitanya Bhatt @ Performancecompetence.com)

下面的函数搜索传递的分隔符的最后一次出现,并将输入字符串的剩余部分保存到返回的输出字符串。

void strLastOccr(char inputStr[100], char* outputStr, char *delim)
    {
        char *temp, *temp2;
        int i = 0;
        temp = "";
        while (temp!=NULL)
        {
            if(i==0)
            {   
                temp2 = temp;
                temp = (char *)strtok(inputStr,delim);
                i++;
            }
            if(i>0)
            {
                temp2 = temp;
                temp = (char *)strtok(NULL,delim);
            }
            lr_save_string(temp2,outputStr);
        }
    }

基本上尝试添加两个新选项以传入。

  1. 出现次数而不是默认为最后一次出现,而是允许特定的停止位置并保存剩余的字符串。

  2. 要保存的字符串的一部分:(左,右)一旦找到分隔符,字符串正在保存右侧。附加选项旨在允许用户指定找到分隔符的左侧或右侧。

    void strOccr(char inputStr [100],char * outputStr,char * delim,int * occrNo,char * stringSide)

  3. 所以问题是我需要对上述功能进行哪些修改? 它实际上也可以吗?

    更新

    在我坚持下去后,我能够解决问题。

    由于我无法再回答6个小时的问题,因此可以为能提供改进功能的人颁发积分。具体来说,我不喜欢评论下的代码“//删除字符串末尾的delim。”

    void lr_custom_string_delim_save (char inputStr[500], char* outputStr, char *delim, int occrNo, int stringSide)
    {
        char *temp, *temp2;
        char temp3[500] = {0};
        int i = 0;
        int i2;
        int iOccrNo = 1;
        temp = "";
    
        while (temp!=NULL) {
            if(i==0) {
                temp2 = temp;
                temp = (char *)strtok(inputStr,delim);
                i++;
            }
    
            if(i>0) {
                temp2 = temp;
                temp = (char *)strtok(NULL,delim);
    
                if (stringSide==0) {
                    if (iOccrNo > occrNo) {
                        strcat(temp3, temp2);
                            // Ensure an extra delim is not added at the end of the string.
                            if (temp!=NULL) {
                                // Adds the delim back into the string that is removed by strtok.
                                strcat(temp3, delim);
                            }
                    }
                }
    
                if (stringSide==1) {
                    if (iOccrNo <= occrNo) {
                        strcat(temp3, temp2);
                        strcat(temp3, delim);
                    }
                }
                // Increase the occurrence counter.
                iOccrNo++;
            }
        }
    
        // Removes the delim at the end of the string.
        if (stringSide==1) {
            for( i2 = strlen (temp3) - 1; i2 >= 0 
            && strchr ( delim, temp3[i2] ) != NULL; i2-- )
            // replace the string terminator:
            temp3[i2] = '\0';
            }
    
        // Saves the new string to new param.
        lr_save_string(temp3,outputStr);
    }
    

1 个答案:

答案 0 :(得分:1)

你真的只需要做一些修改。当您开始使用strtok()遍历字符串时,您可以存储两个变量:char * current,* previous。

当您点击每个新令牌时,将“当前”移至“上一个”并存储新的“当前”。在字符串解析结束时,查看'previous'的值以获取最后一个元素的第二个。

其他选项,使用LoadRunner变量处理机制lr_save_string(token_value,“LR_variable_name_”)保留计数器并构建伪数组。首先,您需要首先构建变量名称字符串。当您退出解析操作时,您的count变量可能会保留从字符串中解析出来的令牌元素的总数,然后您可以使用(counter-1)索引值来构建您的字符串。

char foo[100]="";
...
sprint(foo, "{LR_variable_name_%d}",counter-1);
lr_message("My second to last element is %s",lr_eval_string(foo));

也有可能还有其他选择,但这些是我想到的两个选项。另外,我向你推荐一本书,我建议所有想要刷新他们的C(包括我的兄弟和叔叔),“C for Dummies”。您可以在LoadRunner中使用字符串处理前端有很多很棒的选项。