Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » Data File StructureRSS Feeds

Program simulate the working of Pre-Processor

Posted By: Easy Tutor     Category: C++ Programming     Views: 6273

A C++ Program simulate the working of Pre-Processor.

Code for Program simulate the working of Pre-Processor in C++ Programming

 #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:
    }
  
Share: 


Didn't find what you were looking for? Find more on Program simulate the working of Pre-Processor Or get search suggestion and latest updates.

Easy Tutor
Easy Tutor author of Program simulate the working of Pre-Processor is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!