/********************************************************************/
/* 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, 5 - alphanumeric)
/* 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(length>40)
{
lr_output_message("ERROR:char buff[] size exceeded.Kindly decrease the length or increase the buffer size");
}
if(alpha_num == 0)
{
for(i=0;i<length;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<length;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<length;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<length;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<length;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 if(alpha_num == 5)
{
for(i=0;i<length;i++)
{
r=rand() % 42 + 48; // a-z = 48-90 = rand() % 42 + 48 [for values within specific range using the mod (%) operator]
if(r>57 && r<65)
{
r=r+10;
c= (char) r;
}
else
{
c= (char) r;
}
buff[i]=c;
printf("%c",c);
}
}
else
{
lr_output_message("==>Enter value between 0-5 for argument 3<==");
}
lr_save_string(buff,param_name);
return 0;
}
/********************************************************************/
/* Function Name: cg_file_write
/* Purpose : File write
/* Input : Buffer,file name and mode. Mode can be either "a" or "w"
/* Output : Data written to file
/* Created by : V.M.Guruprasath
/* Date created : December 12, 2008
/******************************************************************/
int cg_file_write(char* buffer,char* filename,char* mode)
{
int fp;
if(mode == "w")
{
fp = fopen (filename,mode);
if(fp == NULL)
{
lr_error_message("ERROR: Unable to open file");
}
}
else if(mode == "a")
{
fp = fopen (filename,mode);
if(fp == NULL)
{
lr_error_message("ERROR: Unable to open file");
}
}
else
{
lr_output_message("ERROR: Enter either 'w' or 'a' for mode");
return -1;
}
fprintf(fp,"%s",buffer);
fclose(fp);
}
/********************************************************************/
/* Function Name: cg_file_read
/* Purpose : File read
/* Input : file name
/* Output :
/* Created by : V.M.Guruprasath
/* Date created : December 12, 2008
/******************************************************************/
int cg_file_read(char* filename,char* strOut)
{
char data[1000];
int fp;
int i=1;
strOut[0] = '\0';
fp = fopen(filename,"r");
if(fp == NULL)
{
lr_error_message("Cannot open %s", filename);
return -1;
}
else
{
//strcpy(strOut,"");
while(! feof(fp))
{
while(fgets(data, 1000, fp)!=NULL)
{
/* keep looping until NULL pointer... */
/* print the file one line at a time */
lr_output_message( "The line %d is ==> \"%s\"",i, data);
/* Concatenate the data */
strcat(strOut,data);
i++;
}
if (ferror(fp))
{
lr_output_message ("Error reading file %s", filename);
break;
}
}
}
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* sOut)
{
sOut[0] = '\0';
lr_save_string(sIn, "InputParam");
web_convert_param("InputParam",
"SourceEncoding=PLAIN",
"TargetEncoding=URL",
LAST);
//lr_output_message("%s", lr_eval_string("{InputParam}"));
strcpy(sOut,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* sOut)
{
sOut[0] = '\0';
lr_save_string(sIn1, "InputParam");
web_convert_param("InputParam",
"SourceEncoding=HTML",
"TargetEncoding=PLAIN",
LAST);
//lr_output_message("%s", lr_eval_string("{InputParam}"));
strcpy(sOut,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;
}
const char* cg_find_replace(const char* source,const char* str,const char* repl)
{
const 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);
strcpy(source,after);
}
else
{
lr_output_message("ERROR: String replace problem");
}
//strcpy(source,after);
return source;
}
/********************************************************************/
/* 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, len;
len = strlen(source);
if(end>len)
{
lr_output_message("ERROR:Verify right bound index");
return("-1");
}
if(length<=0)
{
lr_output_message("ERROR: Enter start and end index value to extract atleast 1 character");
return("-1");
}
newstring = (char*)malloc(length+1);
memset(newstring,'\0',length+1);
tmpstring = (char*)strdup(source);
for(i=1;i<begin;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<startIndex;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);
if(begin<0)
{
lr_output_message("ERROR:Verify Left boundary");
return("-1");
}
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<begin;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: Verify right boundary value");
return("-1");
}
}
newstring = (char*)malloc(length+1);
memset(newstring,'\0',length+1);
tmpstring = (char*)strdup(source);
for(i=1;i<begin;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<start;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,op;
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<cnt;j++)
{
for(i=1;i<start;i++)
{
tmpstring++;
}
strncpy(newstring,tmpstring,length);
lpos = (char *)strstr(newstring, lbound);
start = (int)(lpos - newstring + 1);
//lr_output_message("The value of start is:%d",start);
if(start<0)
{
op=j+1;
//lr_output_message("-->Last occurence of '%s' found for occurance count %d ;the occurence value entered is %d<--",lbound,op,cnt);
if(op<cnt)
{
lr_output_message("ERROR:Left boundary not found for the given occurrence count");
return("-1");
}
//return("-1");
}
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 from left boundary is ::>%s",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<cnt;j++)
{
for(i=1;i<begin;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<begin;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;
//lr_output_message("The value of length is :%d",length); to verify length
if(length < 0)
{
lr_output_message("ERROR: Verify right boundary");
//length = strlen(newstring);
return("-1");
}
/* Copying the extracted string to newstring 1*/
strncpy(newstring1,tmpstring,length);
lr_output_message("Substring is ::>%s",newstring1);
return newstring1;
}
/********************************************************************/
/* Function Name: cg_LRTrim
/* Purpose : Trims spaces/characters on the left & right of the string
/* Input : source string and the string to be trimmed
/* Output : left& right trimmed string
/* Created by : V.M.Guruprasath
/* Date created : January 09, 2009
/******************************************************************/
char* cg_letrim(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;
}
char* cg_ritrim(char* string, char junk)
{
char* original = string + strlen(string);
while(*--original == junk);
*(original + 1) = '\0';
return string;
}
char* cg_trim(char* string,char ljunk,char rjunk)
{
cg_ritrim(string,rjunk);
cg_letrim(string,ljunk);
return string;
}
Function call in Action.c:
/*-------------- Variable Declaration section ---------------------------------------------------*/
/*---- Variable declaration for cg_file_write function-------*/
char buffer[100];
/*---- Variable declaration for cg_PlainToURL/HTMLtoPlain functions ----------*/
char strIn[] = "v mg%d$ + vmguruprasath_";
char strOut[100];
char sOut[1000];
char sOut1[100];
char sOut2[100];
char sIn[] = "<mytag>&";
/* --------- Variables for cgltrim,cg_rtrim functions ----- */
char testStr[] = " V M Guruprasath ";
char testStr1[] = " V M Guruprasath ";
char testStr2[] = " V M Guruprasath ";
char testStr3[] = "$$$$$V M Guruprasath#########";
/*-------------------------------------------------------*/
/* --------- Variables for Find_replace string function --------*/
const char source[] = "$$$$$$V M Guruprasath$$$$$$";
/* --------- Variables for substring function --------*/
char text[] = "The;quickbrown dogjumpsoverthelazy; fox;";
/*------------- Function Calls section ----------------------------------------------------------*/
/*----------- Random function ---------------- */
cg_random("test",6,0);
lr_output_message("The Param Value is::> %s",lr_eval_string("{test}"));
/* ----- File read/write functions -------- */
cg_file_write(buffer,"C:\\test.txt","w");
cg_file_read("C:\\test2.txt",sOut);
lr_output_message("The str value is:==>%s:",sOut);
/*------- Plain to URL and HTMLtoPlain functions --------- */
cg_PlainToURL(strIn, strOut);
lr_output_message("%s",strOut);
cg_PlainToURL_lr(strIn,sOut1);
lr_output_message("%s",sOut1);
cg_HTMLToPlain_lr(sIn,sOut2);
lr_output_message("%s",sOut2);
/* ------ LTrim Function ----------------*/
cg_ltrim(testStr, ' ');
lr_output_message("|%s|",testStr);
/*------- Rtrim Function ---------------*/
cg_rtrim(testStr1, ' ');
lr_output_message("|%s|",testStr1);
/*----- Using both Ltrim and rtrim -------------*/
cg_ltrim(cg_rtrim(testStr2, ' '), ' ');
lr_output_message("|%s|",testStr2);
/*----- lrtrim function -------------*/
cg_trim(testStr3,'$','#');
lr_output_message("The string value is |%s|",testStr3);
/* --- Find and replace function ----- */
lr_output_message("source = \"%s\"\n", source);
cg_find_replace(source,"$","#");
lr_output_message("The string after replacement::> %s",source);
/* --- substring function ----- */
cg_substr_index(text,4,12);
/* -------- substring (left boundary to EOS) function -------------*/
cg_substr_lb_index(text,3);
/* -------- substring with lb and rb strings function -------------*/
cg_substr(text,"The"," fox");
/* -------- substring (left boundary to EOS) function -------------*/
cg_substr_lb(text,"; ");
/* -------- substring with lb string and occurence -------------*/
cg_substr_lb_cnt(text,";", 2);
/* -------- substring with lb and rb string and occurrence -------------*/
cg_substr_cnt(text,";",";",2);