c言語版ソース注釈の除去
13815 ワード
#include
#include
#include
#include
#include
/**
*
**/
void remove_comment(char *buf)
{
size_t off;
char *s, *p, *t, *tmp;
char *sss="adas\"d/**/\"*a\"//s/**/d*/"; //
for (s = p = buf; *p != '\0'; p++) {
switch(*p) {
case '"': // "
case '\'': /* ' */
// , /* //
for (tmp = p + 1; *tmp != *p; tmp++) {
if (*tmp == '\\') {
tmp++; /* , 2 */
}
}//
// while(1) {
// if ( NULL != (t = strchr(tmp, '\\')) ) {
// printf("%c
", *tmp);
// tmp = t + 2; //
// }
// if ( NULL != (t = strchr(tmp, *p)) ) {
// tmp = t + 1; //
// break;
// }
// }
// printf("%c
", *(tmp-2));
// exit(0);
// while ( NULL != (t = strchr(tmp, *p)) ) {
// tmp = t + 1; /* */
// if ( *(t - 1) != '\\' ) {
// break; // ,
// }
// }
off = tmp - p+1/* */;
if (off > 1) {
strncpy(s, p, off);
p += off - 1; // p++,
s += off;
}
break;
case '/':
switch ( *(p + 1) ) {
case '*': // /*
if (NULL != (t = strstr(p + 1, "*/"))) {
p = t + 1; /* /* */
}
break;
case '/': // //
for (tmp = p - 1; 0 != isspace(*tmp); tmp--) {
} // /*
s -= p - tmp - 1; // s
*s++ = '
'; /* // , */
while (*p != '
' && *p != '\r') {
p++;// p
}
break;
default: //
*s++ = *p;
break;
}
break;
default: //
*s++ = *p;
break;
}
}
*s = '\0'; //
}
/**
* M
* ,
* ,
*
**/
int main(int argc, char *argv[])
{
if (argc != 2) { /* */
printf("Usage : %s file.c
", argv[0]);
return -1;
}
const char *filename = argv[1]; //
struct stat statbuff; /* */
if(stat(filename, &statbuff) < 0){
return -1; //
}
off_t filesize = statbuff.st_size; //
char *fileBuff = (char *)malloc(sizeof(char) * filesize);
int fd = open(filename, O_RDONLY);
if (-1 == fd) {
printf("open %s : %s
", filename, strerror(errno));
return errno;
}
ssize_t n = read(fd, fileBuff, filesize);
if (n <= 0) { // ,
close(fd);
return -1;
}
*(fileBuff + n) = '\0'; /* */
remove_comment(/* 2 */fileBuff); //
printf("%s
", fileBuff); /* */
close(fd);
return 0;
}
4.以下に機能を示し、上のファイルをmainとして保存する.c,gcc mainを実行する.c && .\a.exe main.c
#include
#include
#include
#include
#include
void remove_comment(char *buf)
{
size_t off;
char *s, *p, *t, *tmp;
char *sss="adas\"d/**/\"*a\"//s/**/d*/";
for (s = p = buf; *p != '\0'; p++) {
switch(*p) {
case '"':
case '\'':
for (tmp = p + 1; *tmp != *p; tmp++) {
if (*tmp == '\\') {
tmp++;
}
}
off = tmp - p+1;
if (off > 1) {
strncpy(s, p, off);
p += off - 1;
s += off;
}
break;
case '/':
switch ( *(p + 1) ) {
case '*':
if (NULL != (t = strstr(p + 1, "*/"))) {
p = t + 1;
}
break;
case '/':
for (tmp = p - 1; 0 != isspace(*tmp); tmp--) {
}
s -= p - tmp - 1;
*s++ = '
';
while (*p != '
' && *p != '\r') {
p++;
}
break;
default:
*s++ = *p;
break;
}
break;
default:
*s++ = *p;
break;
}
}
*s = '\0';
}
int main(int argc, char *argv[])
{
if (argc != 2) {
printf("Usage : %s file.c
", argv[0]);
return -1;
}
const char *filename = argv[1];
struct stat statbuff;
if(stat(filename, &statbuff) < 0){
return -1;
}
off_t filesize = statbuff.st_size;
char *fileBuff = (char *)malloc(sizeof(char) * filesize);
int fd = open(filename, O_RDONLY);
if (-1 == fd) {
printf("open %s : %s
", filename, strerror(errno));
return errno;
}
ssize_t n = read(fd, fileBuff, filesize);
if (n <= 0) {
close(fd);
return -1;
}
*(fileBuff + n) = '\0';
remove_comment(fileBuff);
printf("%s
", fileBuff);
close(fd);
return 0;
}