Loadrunner - Replacing a character in a text file using C Functions


Test Case - Open a text file in loadrunner and replace '\\ with '\\\\' and save the contents to a variable.


C Program: 

/*Function to search and replace '\\ with '\\\\' */

char *Replacer(char *capValue, char *replace, char *replacewith)
{
 char *pos;
 int offset;
char *output;

 output = (char *)calloc(8000000,sizeof(char));

 pos = (char *)strstr(capValue, replace);
 strcpy(output, "");

 while(pos!=0)
 {
  offset = (int) (pos - capValue);
  strncat(output, capValue, offset);
  strcat(output, replacewith);
  capValue = (char *) (pos + strlen(replace));
  pos = (char *)strstr(capValue, replace);
 }
 strcat(output, capValue);
 lr_output_message("%s", output);
 return output;
}



Action()
{

char filename[500];
long file;
int flength;
int fcontent;
char *mbuffer;
char *delimiter = "\r\n";
char *rop;

rop = (char *)calloc(8000000,sizeof(char));
temp = (char *)calloc(8000000,sizeof(char));
tempall = (char *)calloc(8000000,sizeof(char));
finalfile = (char *)calloc(8000000,sizeof(char));

 strcpy((char *)filename,lr_eval_string("{P_File1}"));\

 /*file open*/
 file = fopen(filename, "rb");
 if (!file) {
    lr_error_message("Opening text file failed %s", filename);
    return;
 }


 /*find file size*/
 fseek(file, 0, SEEK_END);
 flength=ftell(file);
 fseek(file, 0, SEEK_SET);
 lr_log_message("File length is: %9d B.", flength);


 /*buffer memory allocation*/
 mbuffer=(char *)malloc(flength+1);
 if (!mbuffer) {
    lr_error_message("Unable to allocate %10d bytes", flength+1);
    fclose(file);
    return;
 }


 /*copy contents into buffer*/
 fcontent = fread(mbuffer, 1, flength, file);
 if (fcontent != flength)
 {
    lr_error_message("File length is %10d bytes but only read %10d bytes", flength, fcontent);
 }
 else
 {
    lr_log_message("Successfully read %9d bytes from file: ", fcontent);
 }
 fclose(file);


 /*Convert C variable to a loadrunner parameter*/
 lr_save_var( mbuffer, fcontent, 0, "P_fileContent");
 //lr_log_message("File contents: %s", lr_eval_string("{P_fileContent}"));
 free(mbuffer);

/*Pass arguments to search and replace function and save it in a variable*/
 rop = Replacer(lr_eval_string("{P_fileContent}"), "\\", "\\\\");
lr_save_string(rop, "ModFile");

 free(rop);

}




No comments:

Post a Comment