c言語による注釈変換(cからc++)
6525 ワード
:
c c++
:
input.c
// 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 = 9527;
/***/
/* int z = 7748258; */ /*int b=94250;*/
// /*dsklfjdasl;fdsf;ldsfds*/
/*
int j==0;
in t i=0;
*/
int k=0;
output.c
// 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 = 9527;
//*
// int z = 7748258;
//int b=94250;
// /*dsklfjdasl;fdsf;ldsfds*/
//
//int j==0;
//in t i=0;
//
int k=0;
:
//convert.h
#ifndef __CONVERT_H__
#define __CONVERT_H__
#include
enum {
NULSTAT,//
CSTAT,//c
EOFSTAT,//
CPPSTAT//c++
};
#define INPUT "input.c"
#define OUTPUT "output.c"
void convert_main();
#endif
//convert.c
#include
#define _CRT_SECURE_NO_WARNINGS 1
#include "convert.h"//
static int status=NULSTAT;
void do_null_stat(FILE *ipf,FILE *opf)
{
int c=fgetc(ipf);
switch(c)
{
case '/':
{
int s=fgetc(ipf);
switch(s)
{
case '*':
fputc('/',opf);
fputc('/',opf);
status=CSTAT;
break;
case '/':
fputc(c,opf);
fputc('/',opf);
status=CPPSTAT;
break;
default :
fputc(c,opf);
// fputc('/',opf);
ungetc(s,ipf);
status=NULSTAT;
break;
}
}
break;
case EOF:
fputc(c,opf);
status= EOFSTAT;
break;
default:
break;
}
}
void do_cpp_stat(FILE *ipf,FILE *opf)
{
int c=fgetc(ipf);
switch(c)
{
case '
':
fputc(c,opf);
status=NULSTAT;
break;
case EOF:
status=NULSTAT;
break;
default :
fputc(c,opf);
status=CPPSTAT;
break;
}
}
void do_c_stat(FILE *ipf,FILE *opf)
{
int c=fgetc(ipf);
switch(c)
{
case '
':
fputc('
',opf);
fputc('/',opf);
fputc('/',opf);
status=CSTAT;
break;
case '*':
{
int s=fgetc(ipf);
switch(s)
{
case '/':
{
int n=fgetc(ipf);
if(n=='
')
fputc('
',opf);
else
fputc(n,opf);
ungetc(n,ipf);
}
status=NULSTAT;
break;
case EOF:
status=EOFSTAT;
default:
ungetc(s,ipf);
status=CSTAT;
break;
}
}
break;
case EOF:
status=EOFSTAT;
break;
default:
break;
}
}
static void convert_work(FILE *ipf,FILE *opf)
{
while(status!=EOFSTAT)
{
switch(status)
{
case NULSTAT:
do_null_stat(ipf,opf);
break;
case CPPSTAT:
do_cpp_stat(ipf,opf);
break;
case CSTAT:
do_c_stat(ipf,opf);
break;
case EOFSTAT:
do_eof_stat(ipf,opf);
break;
default :
break;
}
}
}
void convert_main()
{
FILE *ipf=fopen(INPUT,"r");
FILE *opf=fopen(OUTPUT,"w");
//pankong
if(ipf==NULL||opf==NULL)
{
fprintf(stderr,"open file error
");
exit(1);
}
convert_work(ipf,opf);
fclose(ipf);
fclose(opf);
}
// main.c
#include "convert.h"
int main()
{
convert_main();
system("pause");
return 0;
}