Write a function that removes the annotation from the C + + program code and returns the result. __jquery

Source: Internet
Author: User
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <io.h>


/**
* Function: Remove comments in C/C + + program code
*
* Input: A pointer to C + + program code
*/

void
Remove_comment (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 ' \ ':/* Single QUOTE * *
if (Dq_start | | lc_start | | bc_start) {
/* Ignore single quotes in strings and comments * *
p++;
Continue
}

if (Sq_start = = NULL) {
Sq_start = p++;

} else {
Len = p++-Sq_start;

if (len = = 2 && * (Sq_start + 1) = = ' \ ') {
/* Ignore single quotes in characters * * *
Continue
}

Sq_start = NULL;
}

Break

Case ' "':/* Double Quotes * *
if (Sq_start | | lc_start | | bc_start) {
/* Ignore double quotes in characters or comments * *
p++;
Continue
}

if (Dq_start = = NULL) {
Dq_start = p++;

} else {
if (* (p++-1) = = ' \ ') {
/* Ignore double quotes in String * *
Continue
}

Dq_start = NULL;
}

Break

Case '/':/* Oblique pole/*
if (Sq_start | | dq_start | | lc_start | | bc_start) {
/* Ignore diagonal bars in characters, strings, or comments
p++;
Continue
}

c = * (P + 1);

if (c = = '/') {
Lc_start = p;
p = 2;

else if (c = = ' * ') {
Bc_start = p;
p = 2;

} else {
/* Ignore Division * *
p++;
}

Break

Case ' * ': * * asterisk * *
if (Sq_start | | dq_start | | lc_start | | bc_start = NULL) {
/* ignores asterisks in characters, strings, or line comments, and ignores multiplication/
p++;
Continue
}

if (* (p + 1)!= '/') {
/* Ignore the asterisk in the block comment * *
p++;
Continue
}

p = 2;

memset (Bc_start, ', P-bc_start);

Bc_start = NULL;

Break

Case ' \ n ':/* newline character * *
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 ("C:\\Documents and settings\\administrator\\ desktop \\remove_comment.c",
_o_rdonly, 0);
if (fd = = 1) {
return-1;
}

n = Read (fd, buf, sizeof (BUF));
if (n = = 1 | | n = = 0) {
Close (FD);
return-1;
}

printf ("test\n");

Remove_comment (buf, N);

* (BUF + N) = ' ";

printf (BUF);
/***********\\\/////// */

Close (FD);

return 0;
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.