您的位置:首页 > 编程语言 > C语言/C++

把C/C++程序代码中的注释去掉,并返回结果

2013-01-20 11:58 567 查看
#include <stdio.h>
#include <string.h>
#include <fcntl.h>

void remove_commit(char *buf, size_t size) {

char *p, *end, c;
char *sq_start, *dq_start;
char *lc_start, *bc_start;
size_t len;
p = buf;
end = p + size;
sq_start = NULL;
dq_start = NULL;
lc_start = NULL;
bc_start = NULL;
while (p < end) {
c = *p;
switch (c) {
case '\'':
if (dq_start || lc_start || bc_start) {
p++;
continue;
}
if (sq_start == NULL ) {
sq_start = p++;
} else {
len = p++ - sq_start;
if (len == 2 && *(sq_start + 1) == '\\') {
continue;
}
sq_start = NULL;
}
break;
case '\"':
if (sq_start || lc_start || bc_start) {
p++;
continue;
}
if (dq_start == NULL ) {
dq_start = p++;
} else {
if (*(p++ - 1) == '\\') {
continue;
}
dq_start = NULL;
}
break;
case '/':
if (sq_start || dq_start || lc_start || bc_start) {
p++;
continue;
}
c = *(p + 1);
if (c == '/') {
lc_start = p;
p += 2;
} else if (c == '*') {
bc_start = p;
p += 2;
} else {
p++;
}
break;
case '*':
if (sq_start || dq_start || lc_start || bc_start == NULL ) {
p++;
continue;
}
if (*(p + 1) != '/') {
p++;
continue;
}
p += 2;
memset(bc_start, ' ', p - bc_start);
bc_start = NULL;
break;
case '\n':
if (lc_start == NULL ) {
p++;
continue;
}
c = *(p - 1);
memset(lc_start, ' ', (c == '\r' ? (p++ - 1) : p++) - lc_start);
lc_start = NULL;
break;
default:
p++;
break;
}
}
if (lc_start) {
memset(lc_start, ' ', p - lc_start);
}
}

int main(int argc, char *argv[]) {
int fd, n;
char buf[102400];
fd = open("/home/lyd/Hello.c", O_RDONLY, 0);
if (fd == -1) {
return -1;
}
n = read(fd, buf, sizeof(buf));
if (n == -1 || n == 0) {
close(fd);
return -1;
}
remove_commit(buf, n);
*(buf + n) = '\0';
printf(buf);
close(fd);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: