Friday, November 2, 2012

format a US zip code


converts a zip code of format (xxxxxxxxx) to (xxxxx-xxxx)

char* function_ZIP_FMT(char* sZipFMTtmp)
{
//char* sZipFMTtmp;
char* sZipFMTtmp1;
char* sZipFMTtmp2;
char sZipFMT[99];

sZipFMTtmp = lr_eval_string("{sZip1}");
strcat( sZipFMTtmp,     "aaaaaa" );
sZipFMTtmp1 = function_substr_index(sZipFMTtmp, 0, 5);
sZipFMTtmp2 = function_substr_index(sZipFMTtmp, 6, 10);
strcat(sZipFMTtmp1, "-");
strcat(sZipFMTtmp1,sZipFMTtmp2);
strcat(sZipFMT, sZipFMTtmp1);
lr_output_message("*****sZipFMT %s", sZipFMT);
return sZipFMT;

}

Extracts a string between the start index and end index


char* function_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;

}

Finds a string/character from the source and replaces with the specified replace string or a character


char* function_replace(const char* src,const char* search,const char* replace)
{
   
 size_t size       = strlen(src) + 1;
 size_t searchlen  = strlen(search);
 size_t replacelen = strlen(replace);

 char* value = (char*)malloc(size);

 char* ret = value;
 ret = value;
 if (value != NULL)
    {
 
  for(;;)
  {
 
   const char* match = (char*)strstr(src,search);

   if(match != NULL)
   {
   
    size_t count = match - src;
    char* temp;
    size += replacelen - searchlen;
    temp = (char*)realloc(value,size);

    if(temp == NULL)
    {
     free(value);
    return NULL;
    }
 
    ret = temp + (ret - value);
    value = temp;
    memmove(ret,src,count);
    src += count;
    ret += count;

    memmove(ret,replace,replacelen);

    src += searchlen;
    ret += replacelen;

   }

   else /* No Match found */
   {
   
    strcpy(ret,src);
    break;
   }
  }
 }

 return value;
}
const char* function_find_replace(const char* source,const char* str,const char* repl)
{
 const char* after;
 after = function_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);

 lr_save_string(after,"afterreplace");
  return after;
}

Extract a string between left boundary and right boundary


char* function_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);
 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);
   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;
       
}

Extracts a string between the start index and end of string


char* function_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;
}