2012-02-25 14 views
0

グーグルグーグルでは以下のコードが出てきました。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); 
     } 
    } 

は基本的に渡すために2つの新しいオプションを追加しようとし

  1. 発生しません:。代わりに、最後の出現をデフォルトで停止し、残りを保存するためにどの出現特定することを可能にします文字列の

  2. 保存する文字列の一部:(左、右)区切り文字が見つかると、ストリングは右側を保存しています。追加オプションは、区切り文字の左側または右側にユーザ​​ーが指定できるようにするためのものです。

    無効strOccr(CHAR inputstrに[100]、CHAR * outputStr、CHAR * DELIM、int型* occrNo、CHAR * 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

質問は何ですか? –

+0

あなたは何をしようとしましたか? –

+3

私はLoadrunnerが何十年前にApple IIeでプレイしたビデオゲームを参照していたと思って興奮しました!エミュレータを起動する時間... –

答えて

1

実際には、いくつかの変更を加えるだけです。 strtok()で文字列を歩き始めると、char * current、* previousという2つの変数を格納できます。

新しいトークンを打つと、 '現在の'を '前'に移動し、新しい '現在の'を保存します。文字列解析の最後に、 'previous'の値を見て、最後の要素から2番目の値を取得します。

その他のオプションでは、カウンタを保持し、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)); 

他にもオプションがありますが、これは頭に浮かぶ2つのものです。また、自分のC(兄と私の叔父を含む)、「C for Dummies」をブラッシュアップしたいすべての人にお勧めする本をお勧めします。文字列処理のフロントには、LoadRunnerで活用できる優れたオプションがたくさんあります。