Annotation conversion

//implement the annotation conversion file

#include<stdio.h>

#include"convert.h"
#pragma warning(disable:4996)
#pragma warning(disable:4013)
static int status = NULLSTAT; //define a global variable
void do_null_start(FILE *ipf, FILE *opf) //implement the processing of input files in general
{
int c = fgetc(ipf); //read a character from the input.c file
switch (c)
{
{
case '/': //if the character is '/'
{
int s = fgetc(ipf); //read the second character of the input file
switch (s)
{
case '*': //if the character is '*'
fputc('/',opf); //output '//' in output.c
fputc('/', opf);
status = CSTAT;   
break;
case '/':

fputc('/', opf); //output '//'

fputc('/', opf);
status = CPPSTAT;
break;
case EOF:
status = EOFSTAT;
break;
default:
fputc('/', opf); //When '/' is followed by other characters, such as a/b
ungetc(s, ipf); //Return to the place where the '/' in front of b in the input.c file
status = ZERO;
break;
}


}
break;
case EOF:
status = EOFSTAT;
break;
default:
fputc(c, opf); //write characters to output.c
status = ZERO;
break;
}
}
void do_cpp_start(FILE *ipf, FILE *opf) //Processing of c++ style comments
{
int c = fgetc(ipf); //read in a character
switch (c)
{
case '\n':
fputc(c, opf);
status = ZERO;
break;
case EOF:
status = EOFSTAT;
break;
default:
fputc(c, opf);
status = CPPSTAT;
break;


}
}
void do_c_start(FILE *ipf, FILE *opf) //Processing of c style comments
{
int c = fgetc(ipf);//ipf input file stream
switch (c)
{
case '\n':
fputc('\n',opf);
fputc('/', opf);
fputc('/', opf);
status = CSTAT;////
break;
case '*':
{
int m = fgetc(ipf);
switch (m)
{
case '/':
{
int n = fgetc(ipf);
if ('\n' == n)
{
fputc('\n', opf);
}
else
ungetc(n, ipf);
}
status = ZERO;
break;
case EOF:
status = EOFSTAT;
break;
default:
fputc(m, opf);
status = CSTAT;
break;
}
}
break;
case EOF:
status = EOFSTAT;
break;
default:
fputc(c, opf);
status = CSTAT;
break;


}
}
static void convert_work(FILE *ipf, FILE *opf)
{
while (status != EOFSTAT)
{
switch (status)
{
case NULLSTAT: //Generally 
do_null_start(ipf, opf);
break;
case CSTAT:
do_c_start(ipf, opf); //c style comment
break;
case CPPSTAT:
do_cpp_start(ipf, opf);
break;
case EOFSTAT:
//do_eof_start(ipf, opf);
break;
default:
break;


}



}
}
void convert_main()
{
FILE *ipf = fopen(INPUT, "r"); //read-only file input.c
FILE *opf = fopen(OUTPUT, "w"); //write only the file output.c
if (NULL == ipf || NULL == opf)
{
fprintf(stderr, "open file error\n");
exit(1);
}
convert_work(ipf, opf); //Call the file conversion function
fclose(ipf); //Close the file input.c
fclose(opf); //Close the file outout.c

}

//implement header file

#ifndef __CONVERT_H__
#define __CONVERT_H__
enum{
ZERO STATE, 
CSTAT,
CPPSTAT,
EOFSTAT


};
#define INPUT "input.c"
#define OUTPUT "output.c"
void convert_main();


#endif

// test header file

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<Windows.h>
#include"convert.h"
int main()
{

convert_main();
system("pause");
return 0;
}

//Note that you must first create an input.c file in the directory of the folder for testing

// this is cpp comment
/ * int i = 0; * /
/ * int j = 10 * / int k = 3;
int n = 20;
/ *
int i = 0;
int j = 20;
int k = 250;
* / int q = 9627;
/ ** /
/ * int z = 7748258 * // * int b = 94259 * /
// / * dsgfhgwuezjzjb * /

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325528222&siteId=291194637