Hello all,
I have a problem with a function I wrote.
I wrote a main() that takes from current library a *.c file (that the user gives by the argv[1]) and make a copy of itself but without the comments and saves it as *.c1, so after this operation we will have 2 files, the first one is *.c (didn't change anything in it), the other is *.c1 (*.c without comments).
The other part is a function that takes this *.c1 file pointer and makes another file (third one) *.c2 which is exactly as *.c1 file, but all the #include <file.h> statements will be replaced with the contents of the file.h's .
The main() with its functions is:
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
enum status {OUT , IN_STRING , LEFT_SLASH , IN_COMMENT , RIGHT_STAR , IN_CPP_COM};
/*functions declarations*/
void chk_correct_file_arg(char *s);
void chk_no_arg(int num);
void includes_extractor(FILE *c1_fp , char *c1_file_name ,int c1_file_str_len );
int main(int argc , char **argv)
{
FILE *c_fp , *c1_fp; /* fd -> *.c ; c1_fp -> *.c1 */
int ch;
int state = OUT;
int c1_file_string_len = strlen(argv[1])+2; /*num of chars in name.c +1 for name.c1*/
char *c1_file_name;
chk_correct_file_arg(argv[1]);
chk_no_arg(argc);
c1_file_name=(char *) malloc ((c1_file_string_len)*sizeof(char));
strcpy(c1_file_name , argv[1]);
c1_file_name[c1_file_string_len-1] = '\0';
c1_file_name[c1_file_string_len-2] = '1';
if( !(c_fp = fopen (argv[1],"r") ) )
{
fprintf(stderr,"cannot open source file !\n");
exit(0);
}
if( !(c1_fp = fopen (c1_file_name,"w+") ) )
{
fprintf(stderr,"cannot open destination file !\n");
exit(0);
}
ch=fgetc(c_fp);
while (!feof(c_fp)){
switch (state)
{
case OUT:
if (ch=='/')
{
state = LEFT_SLASH;
}
else
{
fputc(ch,c1_fp);
if (ch=='\"')
state = IN_STRING;
}
break; /*OUT*/
case LEFT_SLASH:
if(ch=='*')
{
state = IN_COMMENT;
}
else if (ch=='/')
{
state = IN_CPP_COM;
}
else
{
fputc('/',c1_fp);
fputc(ch,c1_fp);
state = OUT;
}
break; /*LEFT_SLASH*/
case IN_COMMENT:
if(ch=='*')
{
state = RIGHT_STAR;
}
break; /*IN_COMMENT*/
case IN_CPP_COM:
if(ch=='\n')
{
state = OUT;
fputc('\n',c1_fp);
}
break; /*IN_CPP_COM*/
case RIGHT_STAR:
if(ch=='/')
{
state = OUT;
}
else if (ch!= '*')
{
state = IN_COMMENT;
}
break; /*RIGHT_STAR*/
case IN_STRING:
if(ch=='\"')
{
state = OUT;
}
fputc(ch,c1_fp);
break; /*IN_STRING*/
} /*switch*/
ch=fgetc(c_fp);
} /*while*/
fclose(c_fp);
fclose(c1_fp);
return 0; /*dummy*/
includes_extractor(c1_fp , c1_file_name ,c1_file_string_len); /*extract all headers*/
} /*main()*/
/* This func validate that the data entered into argv[1] refer to a file type of *.c */
void chk_correct_file_arg(char *s)
{
int arg_len;
arg_len = strlen(s);
if( !( (s[arg_len] == '\0') && (s[arg_len-1] == 'c') && (s[arg_len-2] == '.') ))
{
printf("\n%s is an incorrect file name type !!\n\n",s);
exit(0);
}
} /*chk_correct_file_arg*/
/*This func validate that # of argv organs are exact 2*/
void chk_no_arg(int num_arg)
{
if (num_arg > 2)
{
printf("\nToo many arguments !\n\n Program will now terminate !");
exit(0);
}
if (num_arg == 1)
{
printf("\nMissing argument !\n\n Program will now terminate !");
exit(0);
}
}/*chk_no_arg*/
The function that makes another file (*.c2) is:
/*This func runs *.c1 file, and replace every include file with its content*/
void includes_extractor(FILE *c1_fp , char *c1_file_name ,int c1_file_str_len )
{
int i=0,header_file_len=0;
FILE *c2_fp , *header_fp;
char *c2_file_name, ch, inc_name[]="include",*header_name;
char inc_chk[8]; /*[8] are 7 for "include" +1 for null*/
header_name=(char *) malloc ((header_file_len)*sizeof(char));
c2_file_name=(char *) malloc ((c1_file_str_len)*sizeof(char));
c2_file_name[c1_file_str_len-1] = '\0';
c2_file_name[c1_file_str_len-2] = '2';
/*Open source & destination files + ERR check */
if( !(c1_fp = fopen (c1_file_name,"r") ) )
{
fprintf(stderr,"\ncannot open *.c1 file !\n");
exit(0);
}
if( !(c2_fp = fopen (c2_file_name,"w+") ) )
{
fprintf(stderr,"\ncannot open *.c2 file !\n");
exit(0);
}
/*next code lines are copy char by char from c1 to c2,
but if meet header file, copy its content */
ch=fgetc(c1_fp);
while (!feof(c1_fp))
{
if (ch == '#') /*potential #include occur*/
{
fgets(inc_chk, 8, c1_fp); /*8 places for "include" + null*/
if(strcmp(inc_chk,inc_name)==0) /*means we are in include case*/
{
ch=fgetc(c1_fp);
while(ch==' ') /*stop when head with a '<' or '"'*/
{
ch=fgetc(c1_fp);
} /*while(2)*/
ch=fgetc(c1_fp); /*start read header file name*/
while((ch!='"') || (ch!='>')) /*until we get end of header name*/
{
header_name[i] = ch;
i++;
ch=fgetc(c1_fp);
}/*while(3)*/
header_name[i]='\0'; /*close the header_name array*/
header_file_len=strlen(header_name);
if( !(header_fp = fopen (header_name,"r") ) ) /*open *.h for read + ERR chk*/
{
fprintf(stderr,"cannot open header file !\n");
exit(0);
}
while (!feof(header_fp)) /*copy header file content to *.c2 file*/
{
ch=fgetc(header_fp);
fputc(ch,c2_fp);
}/*while(4)*/
fclose(header_fp);
}
}/*frst if*/
else
{
fputc(ch,c2_fp);
}
ch=fgetc(c1_fp);
}/*while(1)*/
fclose(c1_fp);
fclose(c2_fp);
}/*includes_extractor*/
Everything compile perfectly, but the function does not create the *.c2 file, only the *.c1 file is created...
Many Thanks !!