2012-02-25 80 views
0

我遇到了下面的代碼,而谷歌搜索這很好。 (發信到Chaitanya Bhatt @ Performancecompetence.com)Loadrunner C代碼字符串操作

下面的函數搜索最後一次出現的分隔符,並將輸入字符串的剩餘部分保存到返回的輸出字符串中。

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. 要保存的字符串的一部分:(左,右)此時字符串一旦找到分隔符就保存右側。附加選項旨在允許用戶指定發現分隔符的左側或右側。

    無效strOccr(焦inputStr [100],字符* outputStr,字符* DELIM,INT * occrNo,字符* stringSide)

所以,問題是什麼是我需要將上面的函數修改? 也是實際上可以做到的嗎?

UPDATE

我一直在這之後我就能夠鍛鍊的解決方案。

由於我無法回答自己的問題6個小時,所以會給予誰可以提供改進功能的積分。具體而言,我不喜歡評論下的代碼「//刪除字符串末尾的分隔符。」

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); 
} 
+0

問題是什麼? – 2012-02-25 23:21:58

+0

你嘗試失敗了什麼? – 2012-02-25 23:29:28

+3

我很興奮,因爲Loadrunner參考了我幾十年前在Apple IIe上玩過的視頻遊戲!是時候啓動模擬器了... – 2012-02-25 23:34:09

回答

1

你真的只需要做一些修改。當你開始用strtok()來移動字符串時,你可以存儲兩個變量,char * current,* previous。

當你擊中每個新的令牌時,將'current'移動到'previous'並存儲新的'current'。在字符串解析結束時,查看'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中使用這些選項。