#include <iostream.h>
#include <fstream.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>
#include <ctype.h>
#include <dos.h>
void find_and_replace(char*,constchar[3][100]);
int main( )
{
clrscr( );
textmode(C4350);
char FileName[15]={NULL};
cout<<"\n Enter the file name : ";
cin>>FileName;
cout<<"\n\n Please wait while processing the file ...";
fstream iFile;
fstream oFile;
iFile.open(FileName,ios::in|ios::nocreate);
if(!iFile)
{
cout<<" \n Fatal Error : Unable to open the source file.";
cout<<"\n\n press any key to exit...";
getch( );
exit(1);
}
oFile.open("~temp.cpp",ios::out|ios::trunc);
if(!oFile)
{
cout<<" \n Fatal Error : Unable to create a temp file.";
cout<<"\n\n press any key to exit...";
getch( );
exit(1);
}
char CodeLine[100]={NULL};
char Macros[10][3][100]={NULL};
int macro_counter=0;
do
{
strset(CodeLine,NULL);
iFile.getline(CodeLine,90);
if(strstr(CodeLine,"#include ")!=NULL)
{
char HeaderFile[15]={NULL};
int length=strlen(CodeLine);
for(int i=11;i<(length-1);i++)
HeaderFile[(i-11)]=CodeLine[i];
fstream File;
File.open(HeaderFile,ios::nocreate|ios::in);
if(!File)
{
char Temp[30]={NULL};
strcpy(Temp,"..\\include\\");
strcat(Temp,HeaderFile);
File.open(Temp,ios::nocreate|ios::in);
if(!File)
{
cout<<" \n Fatal Error : Unable to open the Header file.";
cout<<"\n\n press any key to exit...";
getch( );
exit(1);
}
}
do
{
strset(CodeLine,NULL);
File.getline(CodeLine,90);
oFile<<CodeLine<<endl;
}
while(!File.eof( ));
File.close( );
}
else if(strstr(CodeLine,"#define ")!=NULL)
{
char Macro[100]={NULL};
int length=strlen(CodeLine);
for(int j=9;j<length;j++)
Macro[(j-9)]=CodeLine[j];
char *Ptr=NULL;
Ptr=strtok(Macro,"");
strcpy(Macros[macro_counter][0],Ptr);
Ptr=strtok(NULL,"\n");
strcpy(Macros[macro_counter][1],Ptr);
if(strstr(Macros[macro_counter][0],"(")!=NULL)
{
Ptr=strtok(Macros[macro_counter][0],"(");
strcpy(Macros[macro_counter][0],Ptr);
Ptr=strtok(NULL,")");
strcpy(Macros[macro_counter][2],Ptr);
}
macro_counter++;
}
else
{
for(int k=0;k<macro_counter;k++)
{
if(strstr(CodeLine,Macros[k][0])!=NULL)
find_and_replace(CodeLine,Macros[k]);
}
oFile<<CodeLine<<endl;
}
}
while(!iFile.eof( ));
oFile.close( );
iFile.close( );
delay(1000);
cout<<"\n\n\n press any key to see the modified code ...";
getch( );
iFile.open("~temp.cpp",ios::in|ios::nocreate);
if(!iFile)
{
cout<<" \n Fatal Error : Unable to open the modified source file.";
cout<<"\n\n press any key to exit...";
getch( );
exit(1);
}
clrscr( );
int line_counter=0;
do
{
strset(CodeLine,NULL);
iFile.getline(CodeLine,90);
cout<<CodeLine<<endl;
line_counter++;
if(line_counter==45)
{
line_counter=0;
gotoxy(1,49);
cout<<"Press any key to continue...";
getch( );
clrscr( );
}
}
while(!iFile.eof( ));
iFile.close( );
cout<<"\n\n Press any ke to exit...";
remove("~temp.cpp");
getch( );
return 0;
}
/*************************************************************************///------------------------ find_and_replace( ) ------------------------///*************************************************************************/void find_and_replace(char* Text,constchar Macro[3][100])
{
char Temp[100]={NULL};
char Find[100]={NULL};
char Replace[100]={NULL};
char Parameter[100]={NULL};
strcpy(Find,Macro[0]);
strcpy(Replace,Macro[1]);
strcpy(Parameter,Macro[2]);
int text_length=strlen(Text);
int find_length=strlen(Find);
int replace_length=strlen(Replace);
int find_position;
char *Ptr;
strcpy(Temp,Text);
Search:
Ptr=strstr(Temp,Find);
find_position=(strlen(Text)-strlen(Ptr));
if(Ptr==NULL)
goto Skip;
if(isalpha(Ptr[-1]) || isalpha(Ptr[find_length]))
{
if(strlen(Ptr)>2)
Ptr++;
elsegoto Skip;
strcpy(Temp,Ptr);
goto Search;
}
else
{
strset(Temp,NULL);
for(int i=0;i<find_position;i++)
Temp[i]=Text[i];
int k=(find_position+find_length);
if(Text[k]=='(')
{
int index=0;
int brackets=1;
char Argument[50]={NULL};
do
{
k++;
Argument[index]=Text[k];
index++;
if(Text[(k+1)]==')')
brackets--;
elseif(Text[(k+1)]=='(')
brackets++;
}
while(brackets!=0);
k+=2;
char ReplaceWith[3][100]={NULL};
strcpy(ReplaceWith[0],Parameter);
strcpy(ReplaceWith[1],Argument);
find_and_replace(Replace,ReplaceWith);
}
for(int j=0;j<replace_length;j++,i++)
Temp[i]=Replace[j];
for(;k<text_length;k++,i++)
Temp[i]=Text[k];
strset(Text,NULL);
strcpy(Text,Temp);
goto Search;
}
Skip:
}