I need a LoadRunner function to search a string for left and right boundaries

  • Questions
  • I need a LoadRunner function to search a string for left and right boundaries
Question ID: 104050
2
0

I am using LR and in my script, trying to determine which option is selected in a drop down list. I need a function to search a string for left and right boundaries, similar to other LR built in functions. I just need the function to search inside a string though, not inside the entire HTTP response. Has anyone created such a function?

Marked as spam
Posted by (Questions: 36, Answers: 3)
Asked on February 24, 2010 2:55 pm
29 views
Answers (3)
4
Private answer

Updated function to save to parameter instead of leaving memory issues as noted above

void findValue(const char * originalStr,char * leftBound, char * rightBound,int * offset, char * savedParamName)
{
    char search_str[50];
    char * start_position;
    char * end_position;
    char * returnStr;
    int offset1,offset2;

    // ADDED
    if(originalStr==NULL)
    {
        lr_save_string('''',savedParamName);
        // return NULL;
        return;
    }

    sprintf(search_str,leftBound);
    start_position = (char *)strstr(originalStr,search_str);
    if(start_position!=NULL)
    {
        offset1 = (int)(start_position - originalStr);
        *offset = offset1+strlen(search_str);
        start_position+=strlen(search_str);

        sprintf(search_str,rightBound);
        end_position = (char *)strstr(start_position,search_str);
        if(end_position!=NULL)
        {
            offset2 = (int)(end_position - start_position);
            *offset += offset2;
            if((returnStr = (char *)malloc((offset2+1)*sizeof(char)))==NULL)
            {
                lr_output_message(''calloc error - not enough memory to allocate'');
                lr_exit(LR_EXIT_VUSER,LR_FAIL);
            }
            //lr_output_message(''start_pos = %d - offset 2 = %d'',start_position,offset2);
            strncpy(returnStr,start_position,offset2);
            //lr_output_message(''returnStr = %s'',returnStr);
            returnStr[offset2]=''; // make sure null char is set at the end
        }
        else
        {
            lr_save_string('''',savedParamName);
            // return NULL;
            return;
        }
    }
    else
    {
        lr_save_string('''',savedParamName);
        // return NULL;
        return;
    }
    lr_save_string(returnStr,savedParamName);
    //return returnStr;
    free(returnStr);
}
Marked as spam
Posted by (Questions: 3, Answers: 16)
Answered on March 31, 2010 8:35 pm
3
Private answer

This function takes in a string and
two boundaries to search for and it
returns the string found in between
the boundaries. This function can be
used to determine which option is
selected in a drop down when
lr_xml_get_values won't work. Any
suggestions to improve the function
are greatly appreciated.

//-----------------------------------------------------------------------
// Function:     findValue
// Inputs:         originalStr = The string to be searched
//                 leftBound = text to the left of wanted text
//                rightBound = text to the right of wanted text
//                offset = position of found value
// Outputs:     returnStr = text found or NULL
// Description: Searches a string for text between the right and left boundry.
//                offset is used to pass by reference.  This allows 
//                the calling function to move the pointer past the found value.
//-----------------------------------------------------------------------
char * findValue(const char * originalStr,char * leftBound, char * rightBound,int * offset)
{
    char search_str[50];
    char * start_position;
    char * end_position;
    char * returnStr;
    int offset1,offset2;

    // ADDED
    if(originalStr==NULL)
        return NULL;

    sprintf(search_str,leftBound);
    start_position = (char *)strstr(originalStr,search_str);
    if(start_position!=NULL)
    {
        offset1 = (int)(start_position - originalStr);
        *offset = offset1+strlen(search_str);
        start_position+=strlen(search_str);

        sprintf(search_str,rightBound);
        end_position = (char *)strstr(start_position,search_str);
        if(end_position!=NULL)
        {
            offset2 = (int)(end_position - start_position);
            *offset += offset2;
            if((returnStr = (char *)malloc((offset2+1)*sizeof(char)))==NULL)
            {
                lr_output_message(''calloc error - not enough memory to allocate'');
                lr_exit(LR_EXIT_VUSER,LR_FAIL);
            }
  //lr_output_message(''start_pos = %d - offset 2 = %d'',start_position,offset2);
            strncpy(returnStr,start_position,offset2);
  //lr_output_message(''returnStr = %s'',returnStr);
            returnStr[offset2]=''; // make sure null char is set at the end
        }
        else
        {
            return NULL;
        }
    }
    else
    {
        return NULL;
    }
    return returnStr;
}
Marked as spam
Posted by (Questions: 6, Answers: 167)
Answered on February 24, 2010 3:14 pm
3
Private answer

Alternative method: save result to LR Parameter rather than create a memory leak with
malloc().

//-----------------------------------------------------------------------<br>
 // Function:     SaveBoundedValue<br>
 // Inputs:         cPName = parameter name to save results<br>
 //                 cInStr = The string to be searched<br>
 //                 cLB    = text to the left of wanted text<br>
 //                 cRB    = text to the right of wanted text<br>
 // Outputs:     pointer to saved string, string saved as parameter (NULL if not found)<br>
 // Description: Searches a string for text between the right and left boundry.<br>
 //              String between boundries is saved as a LR parameter<br>
 //-----------------------------------------------------------------------<br>

char *SaveBoundedValue(const char *cPName, char *cInStr, char *cLB, char *cRB) {

    char *cWorkPtr;        // work pointer for start of string
    char *cEndPtr;         // work pointer for end of string
    char cFound;           // temp location store end of string byte
    char cWorkName[128];   // parameter name with curly braces - 
                           // note max of 128 bytes including braces and null
    char *cRet;            // return value

    if (!cInStr || !cPName)    {    // no input or output specified
        cRet = NULL;                // set return value to null
    } else {
        cWorkPtr = strstr(cInStr,cLB);
        if (!cWorkPtr) {                             // nothing found
            lr_save_string('''',cPName);               // save empty string
            cRet = NULL;                             // set return value to null
        } else {                                     // lb found - look for rb
            cWorkPtr += strlen(cLB);                 // skip left boundry
            cEndPtr = cWorkPtr + 1;                  // start at first byte past left bound
            cEndPtr = strstr(cEndPtr,cRB);           // search for rb
            if (cEndPtr) {                           // rb found - extract and save string
                cFound = *cEndPtr;                   // save found byte
                *cEndPtr = 0;                        // null terminate the string
                lr_save_string(cWorkPtr,cPName);     // save string to LR Paramater
                *cEndPtr = cFound;                   // repair input string
                sprintf(cWorkName,''{%s}'',cPName);    // create name for lr_eval_string
                cRet = lr_eval_string(cWorkName);    // set return pointer
            } else {                                 // rb not found
                lr_save_string('''',cPName);           // save null string
                cRet = NULL;                         // set return value to null
            }
        }
    }
    return cRet;
}
Marked as spam
Posted by (Questions: 1, Answers: 5)
Answered on March 31, 2010 8:32 pm
The above code needs two adjustments before it will compile in LR; both require a casting of the strstr function to char * so that it returns a pointer instead of an int: cWorkPtr = strstr(cInStr,cLB); // should be cWorkPtr = (char *)(strstr(cInStr,cLB)); cEndPtr = strstr(cEndPtr,cRB); // should be cEndPtr = (char *)(strstr(cEndPtr,cRB)); Then it will compile and work!
( at October 30, 2018 7:57 pm)