Resourceful Engineer

" Known is a drop, Unknown is an ocean"

LoadRunner user defined functions

  /********************************************************************/
/* Function Name: cg_random
/* Purpose : Generate random alphabets/numbers/symbols
/* Input : No.of letters, Capital or small(0 -for capital letters, 1 -for small letters,
/* 2 - mixed case, 3 -numbers, 4 -symbols)
/* Output : Random alphabets/numbers/symbols (param_name)
/* Created by : V.M.Guruprasath
/* Date created : December 11, 2008
/******************************************************************/
cg_random(char* param_name,int length,int alpha_num)
{
char buff[42] = "";
int i,r;
char c;
srand((unsigned int) time(0)); //Before invoking rand, call srand to seed the pseudo-random number generator
if(alpha_num == 0)
{ for(i=0;i {
r=rand() % 25 + 65; // A-Z = 65-90 = rand() % 25 + 65 [for values within specific range using the mod (%) operator]
c= (char) r;
buff[i]=c;
printf("%c",c);
}
}
else if(alpha_num == 1)
{
for(i=0;i {
r=rand() % 25 + 97; // a-z = 97-122 = rand() % 25 + 97 [for values within specific range using the mod (%) operator]
c= (char) r;
buff[i]=c;
printf("%c",c);
}
}
else if(alpha_num == 2)
{
for(i=0;i {
r=rand() % 57 + 65; // a-z = 65-122 = rand() % 57 + 65 [for values within specific range using the mod (%) operator]
if(r>90 && r<97)
{
r=r+10;
c= (char) r;
}
else
{
c= (char) r;
}
buff[i]=c;
printf("%c",c);
}
}
else if(alpha_num == 3)
{
for(i=0;i {
r=rand() % 9 + 48; // 0-9 = 48-57 = rand() % 9 + 48 [for values within specific range using the mod (%) operator]
c= (char) r;
buff[i]=c;
printf("%c",c);
}
}
else if(alpha_num == 4)
{
for(i=0;i {
r=rand() % 14 + 33; // !-/ = 33-47 = rand() % 14 + 33 [for values within specific range using the mod (%) operator]
c= (char) r;
buff[i]=c;
printf("%c",c);
}
}
else
{
lr_output_message("==>Enter value between 0-4 for argument 3<==");
}
lr_save_string(buff,param_name);
return 0;
}

  /********************************************************************/
/* Function Name: cg_file_write
/* Purpose : File write
/* Input : Buffer and file name
/* Output : Data written to file
/* Created by : V.M.Guruprasath
/* Date created : December 12, 2008
/******************************************************************/
int cg_file_write(char* buffer,char* filename)
{
int fp;
fp = fopen (filename,"w");
if(fp == NULL)
{
lr_error_message("ERROR: Unable to open file");
}
fprintf(fp,"%s",buffer);
fclose(fp);
}

  /********************************************************************/
/* Function Name: cg_file_read
/* Purpose : File read
/* Input : file name
/* Output : Reads content from file
/* Created by : V.M.Guruprasath
/* Date created : December 12, 2008
/******************************************************************/
int cg_file_read(char* filename)
{
int fp;
char data[1000];
char* buff;
int i=1;
int total, count =0;
//char* filename = "test1.txt";
fp = fopen(filename,"r");
if(fp == NULL)
{
lr_error_message("Cannot open %s", filename);
return -1;
}
else
{
while(! feof(fp))
{
// Read 1000 bytes while maintaining a running count
//count = fread(data, sizeof(char), 1000, fp);
count = fgets(data, 1000, fp);
if (ferror(fp)) {
lr_output_message ("Error reading file %s", filename);
break;
}
lr_output_message( "The line %d is ==> \"%s\"",i, data);
i++;
/*// Get the first line from the file
if (fgets(data, 100, fp) == NULL)
lr_output_message("fgets error");
else
lr_output_message( "The line %d is ==> \"%s\"",i, data);
i++;*/
}
}
fclose(fp);
if (ferror(fp))
{
lr_error_message("Error closing file %s", filename);
}
}

  /********************************************************************/
/* Function Name: cg_PlainToURL
/* Purpose : converts a plain text string into URL format string
/* Input : StrIn - Input String,
/* Output : URL format string (StrOut - Output buffer)
/* Created by : V.M.Guruprasath
/* Date created : December 15, 2008
/******************************************************************/
char* cg_PlainToURL(char* strIn, char* strOut)
{
int i;
char curChar;
char curStr[4] = {0};
strOut[0] = '\0';
for (i=0;curChar=strIn[i];i++)
{ if(isdigit(curChar) || isalpha(curChar)) // Verify whether the character is digit or alphabet
{ sprintf(curStr,"%c",curChar); // If yes, then print as such
}
else
{
sprintf(curStr,"%%%X",curChar); // else convert it into hex string
}
strcat(strOut,curStr); // Concatenate the output
}
return strOut;
}

  /********************************************************************/
/* Function Name: cg_PlainToURL_lr
/* Purpose : converts a plain text string into URL format string
/* Input : sIn - String which needs to be converted to URL format
/* Output : URL formatted string
/* Created by : V.M.Guruprasath
/* Date created : December 15, 2008
/******************************************************************/
cg_PlainToURL_lr(char* sIn)
{
//char sIn[] = "t es%d$ + eprst_";
lr_save_string(sIn, "InputParam");
web_convert_param("InputParam",
"SourceEncoding=PLAIN",
"TargetEncoding=URL",
LAST);
lr_output_message("%s", lr_eval_string("{InputParam}"));
}

  /********************************************************************/
/* Function Name: cg_HTMLToPlain_lr
/* Purpose : converts a URL format string into plain text string
/* Input : sIn1 - HTML string which needs to be converted to Plain format
/* Output : Plain formatted string
/* Created by : V.M.Guruprasath
/* Date created : December 15, 2008
/******************************************************************/
cg_HTMLToPlain_lr(char* sIn1)
{ //char sIn1[] = "v%20mg%25d%24%20%2B%20vmguruprasath%5F";
lr_save_string(sIn1, "InputParam");
web_convert_param("InputParam",
"SourceEncoding=HTML",
"TargetEncoding=PLAIN",
LAST);
lr_output_message("%s", lr_eval_string("{InputParam}"));
}

  /********************************************************************/
/* Function Name: cg_LTrim
/* Purpose : Trims spaces on the left of the string
/* Input : source string and the string to be trimmed
/* Output : left trimmed string
/* Created by : V.M.Guruprasath
/* Date created : December 16, 2008
/******************************************************************/
char* cg_ltrim(char *string, char junk)
{
char* original = string;
char *p = original;
int trimmed = 0;
do
{
if (*original != junk || trimmed)
{
trimmed = 1;
*p++ = *original;
}
}
while (*original++ != '\0');
return string;
}

  /********************************************************************/
/* Function Name: cg_rtrim
/* Purpose : Trims spaces on the right of the string
/* Input : source string and the string to be trimmed
/* Output : right trimmed string
/* Created by : V.M.Guruprasath
/* Date created : December 16, 2008
/******************************************************************/
char* cg_rtrim(char* string, char junk)
{
char* original = string + strlen(string);
while(*--original == junk);
*(original + 1) = '\0';
return string;
}

  /********************************************************************/
/* Function Name: cg_find_replace
/* Purpose : Finds a string/character from the source and replaces with
/* the specified replace string/character.
/* Input : source string, search string, replace string
/* Output :
/* Created by : V.M.Guruprasath
/* Date created : December 19, 2008
/******************************************************************/
char* cg_replace(const char* src,const char* search,const char* replace)
{
/* We need to find the length of the source string, seach and replace string*/
size_t size = strlen(src) + 1;
size_t searchlen = strlen(search);
size_t replacelen = strlen(replace);
/* Allocate memory to values */
char* value = (char*)malloc(size);
/* defining return value */
char* ret = value;
ret = value;
/* Verify Malloc */
if (value != NULL)
{ /* loop untill no match is found */
for(;;)
{
/* Find the search string */
const char* match = (char*)strstr(src,search);
if(match != NULL)
{
/* Found search text at location match,
* Find how many characters to copy in match */
size_t count = match - src;
/* Re-allocate memory, use a temp variable for pointer */
char* temp;
/* Calculating the total length of the string after allocation */
size += replacelen - searchlen;
/* realloc memory*/
temp = (char*)realloc(value,size);
if(temp == NULL)
{
/* Re allocation of memory failed so free the malloc'd memory*/
free(value);
return NULL;
}
/* Re-allocation successful. Now point the pointer to value */
ret = temp + (ret - value);
value = temp;
/* copy from the source to where we matched.
* Then move the source pointer to this point and move the
* destination pointer ahead by same amount */
memmove(ret,src,count);
src += count;
ret += count;
/* Now copy the replacement text 'replace' at the position of match.
* Adjust the source pointer by the text we replaced
* Adjust the destination pointer with the amount of replacement */
memmove(ret,replace,replacelen);
src += searchlen;
ret += replacelen;
}
else /* No Match found */
{
/* Copy the remaining part of the string */
strcpy(ret,src);
break;
}
}
}
return value;
}
void cg_find_replace(const char* source,const char* str,const char* repl)
{
char* after;
after = cg_replace(source,str,repl);
lr_output_message("The find string is: '%s' and replace string is '%s'",str,repl);
if(after != NULL)
{
lr_output_message("The string after replacement::> %s",after);
free(after);
}
}

  /********************************************************************/
/* Function Name: cg_substr_index
/* Purpose : Extracts a string between the start index and end index.
/* Input : Source string, Stat index and end index
/* Output : Extracted sting between the start and end index
/* Created by : V.M.Guruprasath
/* Date created : December 22, 2008
/******************************************************************/
char* cg_substr_index(char* source,int begin, int end)
{
int length = end-begin;
char* newstring;
char* tmpstring;
int i;
if(length<0)
{
return("-1");
}
newstring = (char*)malloc(length+1);
memset(newstring,'\0',length+1);
tmpstring = (char*)strdup(source);
for(i=1;i {
tmpstring++;
}
strncpy(newstring,tmpstring,length);
lr_output_message("Substring is ::>%s",newstring);
return newstring;
}

  /********************************************************************/
/* Function Name: cg_substr_lb_index
/* Purpose : Extracts a string between the start index and end of string.
/* Input : Source string and start index
/* Output : Extracted string from start index to last.
/* Created by : V.M.Guruprasath
/* Date created : December 24, 2008
/******************************************************************/
char* cg_substr_lb_index(char* src,int startIndex)
{
char* buffer;
char* temp;
int cnt;
int length = strlen(src);
if(length<0)
{
return("-1");
}
buffer = (char*)malloc(length+1);
memset(buffer,'\0',length+1);
temp = (char*)strdup(src);
for(cnt=1;cnt {
temp++;
}
strncpy(buffer,temp,length);
lr_output_message("Substring from left boundary is ::>%s",buffer);
return buffer;
}

  /********************************************************************/
/* Function Name: cg_substr
/* Purpose : Extract a string between left boundary and right boundary.
/* Input :
/* Output :
/* Created by : V.M.Guruprasath
/* Date created : December 31, 2008
/******************************************************************/
char* cg_substr(char* source,char* lbound, char* rbound)
{
char* lposition;
char* rposition;
int begin,end;
int length;
char* newstring;
char* tmpstring;
char* tmp;
int i;
int lblength = strlen(lbound);
lposition = (char *)strstr(source, lbound);
// strstr has returned the address. Now calculate * the offset from the beginning of str
begin = (int)(lposition - source + 1);
lr_output_message ("The lbound \"%s\" was found at position %d", lbound, begin);
begin = begin + lblength;
rposition = (char *)strstr(source, rbound);
// strstr has returned the address. Now calculate * the offset from the beginning of str
end = (int)(rposition - source + 1);
lr_output_message ("The rbound \"%s\" was found at position %d", rbound, end);
length= end-begin;
if(length<0)
{ tmp = (char*)malloc(length + 1);
tmp = (char*)strdup(source);
for(i=1;i {
tmp++;
}
rposition = (char *)strstr(tmp, rbound);
end = (int)(rposition - tmp + 1);
lr_output_message ("The new rbound \"%s\" was found at position %d", rbound, end);
length= end - 1;
if(length<0)
{
lr_output_message("ERROR: Right boundary value is found before left boundary value");
return("-1");
}
}
newstring = (char*)malloc(length+1);
memset(newstring,'\0',length+1);
tmpstring = (char*)strdup(source);
for(i=1;i {
tmpstring++;
}
strncpy(newstring,tmpstring,length);
lr_output_message("Substring is ::>%s",newstring);
return newstring;
}

  /********************************************************************/
/* Function Name: cg_substr_lb
/* Purpose : Extract a string between left boundary and end of string.
/* Input :
/* Output :
/* Created by : V.M.Guruprasath
/* Date created : January 02, 2009
/******************************************************************/
char* cg_substr_lb(char* src,char* lbound)
{
char* lpos;
char* newstring;
char* tmpstring;
int start,end,length,i;
int lblength = strlen(lbound);
lpos = (char *)strstr(src, lbound);
// strstr has returned the address. Now calculate * the offset from the beginning of str
start = (int)(lpos - src + 1);
lr_output_message ("The lbound \"%s\" was found at position %d", lbound, start);
start = start + lblength;
length= strlen(src);
if(length<0)
{
lr_output_message("-->Enter string with some characters<--");
return("-1");
}
newstring = (char*)malloc(length+1);
memset(newstring,'\0',length+1);
tmpstring = (char*)strdup(src);
for(i=1;i {
tmpstring++;
}
strncpy(newstring,tmpstring,length);
lr_output_message("Substring is ::>%s",newstring);
return newstring;
}

  /********************************************************************/
/* Function Name: cg_substr_lb_cnt
/* Purpose : Extract a string between left boundary and end of string
/* for the given occurence.
/* Input :
/* Output :
/* Created by : V.M.Guruprasath
/* Date created : January 05, 2009
/******************************************************************/
char* cg_substr_lb_cnt(char* src,char* lbound, int cnt)
{
char* lpos;
char* newstring;
char* tmpstring;
int start,end,length,i,j;
int lblength = strlen(lbound);
lpos = (char *)strstr(src, lbound);
// strstr has returned the address. Now calculate * the offset from the beginning of str
start = (int)(lpos - src + 1);
if(start<0)
{
lr_output_message("-->ERROR:The left boundary value'%s' - Not found in source <--",lbound);
return("-1");
}
//lr_output_message ("The lbound \"%s\" was found at position %d", lbound, start);
start = start + lblength; // calculate the start position
length= strlen(src);
if(length<0)
{
lr_output_message("-->ERROR:Enter string with some characters<--");
return("-1");
}
newstring = (char*)malloc(length+1);
memset(newstring,'\0',length+1);
tmpstring = (char*)strdup(src);
/* Msg: move pointer for the number of occcurence */
for (j=0;j {
for(i=1;i {
tmpstring++;
}
strncpy(newstring,tmpstring,length);
lpos = (char *)strstr(newstring, lbound);
start = (int)(lpos - newstring + 1);
if(start<0)
{ lr_output_message("-->Last occurence of '%s' found for occurance count %d ;the occurence value entered is %d<--",lbound,j+1,cnt);
break;
}
else
{
lr_output_message ("The lbound \"%s\" was found at position %d", lbound, start);
/* Calculate new start position */
start = start + lblength;
length= strlen(newstring);
if(length<0)
{
lr_output_message("-->ERROR:Enter string with some characters<--");
return("-1");
}
}
}
lr_output_message("Substring with occurence count %d is ::>%s",j+1,newstring);
return newstring;
}

  /***************************************************************************/
/* Function Name: cg_substr_cnt
/* Purpose : Extract a string between left boundary and right boundary.
/* for the given occurrence
/* Input :
/* Output :
/* Created by : V.M.Guruprasath
/* Date created : January 06, 2009
/************************************************************************/
char* cg_substr_cnt(char* source,char* lbound, char* rbound, int cnt)
{
char* lposition;
char* rposition;
int begin,end,i,j,length;
char* newstring;
char* tmpstring;
char* newstring1;
char* tmp;
char* temp;
int lblength = strlen(lbound);
lposition = (char *)strstr(source, lbound);
// strstr has returned the address. Now calculate * the offset from the beginning of str
begin = (int)(lposition - source + 1);
length= strlen(source);
if(length<0)
{
lr_output_message("Enter string with some characters");
}
newstring = (char*)malloc(length+1);
memset(newstring,'\0',length+1);
strncpy(newstring,source,length);
/* Verifying count values */
if(cnt == 0)
{
lr_output_message("Enter occurrence value as 1 or >1");
return("-1");
}
/* Moving begin index for the number of occurence */
if(cnt>1)
{
temp = (char*)malloc(length+1);
temp = (char*)strdup(source);
for(j=0;j {
for(i=1;i {
temp++;
}
lposition = (char *)strstr(temp, lbound);
begin = (int)(lposition - temp + 1);
begin = begin + lblength;
length= strlen(newstring);
strncpy(newstring,temp,length);
//lr_output_message("The newstring value is :%s",newstring);
}
/* Noting down the new begin position */
lposition = (char *)strstr(newstring, lbound);
begin = (int)(lposition - newstring + 1);
if(begin<0)
{
lr_output_message("The argument occurence count exceeds the left boundary occurrence count");
return("-1");
}
}
newstring1 = (char*)malloc(length+1);
memset(newstring1,'\0',length+1);
tmpstring = (char*)strdup(newstring);
begin = begin + lblength;
/* Extracting string from the new begin value */
for(i=1;i {
tmpstring++;
}
/* Finding right boundary in the new string */
rposition = (char *)strstr(tmpstring, rbound);
end = (int)(rposition - tmpstring + 1);
/* Calculating length upto the begining of right boundary*/
length = end - 1;
if(length < 0)
{
lr_output_message("last occurrence of left boundary reached");
length = strlen(newstring);
}
/* Copying the extracted string to newstring 1*/
strncpy(newstring1,tmpstring,length);
lr_output_message("Substring is ::>%s",newstring1);
return newstring1;
}

Create a Free Website