/**********************************************************
   srcs: source string
   Dest: destination of string
   froms: firt word to match
   Tos: second word to match
   return: 1 successufull otherwise error
***********************************************************/

int extractContent(char *srcs, char* Dest ,char* froms,char* Tos)
{
	int iret;
	char *resultS;
	char *resultE;
	int len;
	int lentoken;
	iret=0;
	lentoken=strlen(Tos);
	resultS=strstr(srcs,froms);
	Dest[0]=0;
	if(resultS!=NULL)//first string found
	{
        resultS=resultS+strlen(froms);
		resultE=strstr(resultS,Tos);
		if(resultE!=NULL)//second string found
		{
			len=resultE-resultS
			strncpy(Dest,resultS,len);
			Dest[len]=0;
			iret=1;
		}

	}

	return iret;
}

How to use extractContent function:

void TestFunction()
{
   char Ssource[100],Sdest[100];
   strcpy(Ssource,"<root><child>content of tag</child></root>");
   if(extractContent(Ssource,Sdest,"<root>","</root>"))
   {
       printf("Source String:\n%s\n\n   Extracted String:\n   %s\n\n",Ssource,Sdest);
       strcpy(Ssource,Sdest);
       if(extractContent(Ssource,Sdest,"<child>","</child>"))
       {
           printf("   Source String:\n   %s\n\n      Extracted String:\n      %s",Ssource,Sdest);
       }
       else
         printf("Error Second String not found");

   }
   else
      printf("Error String not found");

}

Result of TestFunction:
extract

Advertisements