header - Write a simple C preprocessor -


i trying write program imitates functions of c preprocessor. question expanding #include "header.h" statements.

i have file * pointer source file, need scan header files included in source file, , each one, enter header file , copy content new file, , copy original source file new file, result in expanded header files program.

my problem: reaching actual header files scanning source file (this headers have written, should looked in folder source file is).

any ideas appreciated. (i haven't posted miserable attempts, if somehow, post them).

  • you open output file

  • you fgets line line source file.

  • you check each line if starts #include. if no, copy line read output. if yes, open file name follows after #include , copy content output.

  • repeat until end of input.

the following preprocessor (i haven't tested it; should work):

#define false   0 #define true    1 #define max_line 1024  char linebuf[max_line]; file *fpout;  int preprocessor(file *fpin);  int main(int argc, char **argv) {     file *fpin;     if ((fpout=fopen(argv[2],"w"))==null) return(1);     if ((fpin= fopen(argv[1],"r"))==null) return(1);     preprocessor(fpin);     fclose(fpin);     fclose(fpout);     return(0); } int preprocessor(file *fpin) {     file *fpin2;     while (fgets(linebuf,max_line,fpin))     {         if (strncmp(linebuf,"#include",8)==0) {             char *cp1, *cp2;             if ((cp1= strchr(linebuf+9,'"'))==null) {fclose(fpin); return(false);}             if ((cp2= strchr(cp1+1,    '"'))==null) {fclose(fpin); return(false);}             *cp2='\0';             if ((fpin2=fopen(cp1,"r"))==null) {                 printf("file '%s' not found.\n",cp1);                 return(false);             }             if (!preprocessor(fpin2)) {fclose(fpin2); return(false);}             fclose(fpin2);         }         else fputs(linebuf, fpout);     }     return(true); } 

Comments

Popular posts from this blog

javascript - Bootstrap Popover: iOS Safari strange behaviour -

Magento/PHP - Get phones on all members in a customer group -

Website Login Issue developed in magento -