Monday, April 2, 2012

"strtok"- String Token function in Vugen

String Token function come into picture when we get server response in the given below format
Account~IRA~1234567890~Internal~Saving~Bank Of America
Account~AAA~4567891230~External~Current~Bank Of America
This is responses of requests which returns a list of accounts, and suppose i need to capture third value 1234567890 from these types of responses.
Or we need to capture values from the string like
Shashikant.Pathak.Blog.Post
Follow the given below steps to get this done
I am taking server response value as Account~IRA~1234567890~Internal~Saving~Bank Of America
I will show you how to save third value in a variable which is having different left and right boundary at each iteration
First correlate this value in a parameter Param_value
Follow the given below steps to get this done
·         Define separator value in globals.h
·         Define token string in globals.h
char separators[] = "~";
//Here in my example separator is ~ Account~IRA~1234567890~Internal~Saving~Bank Of America
char * token;
·         After capturing response in Paran_Value by giving Account~ as left boundary and ~Bank Of America as right boundary, place the given below functions in script
Captured value would be IRA~1234567890~Internal~Saving
   
 token = (char *)strtok(path, separators); // Get the first token

if (!token)
         {
              lr_output_message ("No tokens found in string!");
              return( -1 );
       }
       while (token != NULL )  // While valid tokens are returned
     {
              lr_output_message ("%s", token );          
              token = (char *)strtok(NULL, separators); // Get the next token
      }
·         Output of this function would be given below
Action.c(17): IRA
Action.c(17): 1234567890
Action.c(17): Internal
Action.c(17): Saving

·         If we want to save 123456790in any string then this can be done by modifying the while loop and defining some parameters
1.       Define given 2 parameters in action block
                                                                                          char * var ;
                                                                                           int i=1;
2.       Then use the modified while loop given below

   token = (char *)strtok(path, separators); // Get the first token
if (!token)
 {
              lr_output_message ("No tokens found in string!");
              return( -1 );
       }

       while (token != NULL )  // While valid tokens are returned
 {
         lr_output_message ("%s", token );
 if(i==2)
    var=token;
  i++;
  token = (char *)strtok(NULL, separators); // Get the next token
       }
3.       123456790 will be save in var  which can be passed further.

2 comments: