-
Notifications
You must be signed in to change notification settings - Fork 1
/
finecomment.cpp
99 lines (85 loc) · 1.8 KB
/
finecomment.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void slash_slash(string &buff, int &i)
{
while (buff[i] != '\n') // skip upto return
{
i++;
}
//if(buff[i]=='\n') i++; // '\n' skip
}
void string_found(string &buff, string &out_buff,int &i)
{
out_buff.push_back(buff[i]);
i++;
while (buff[i] != '"') //write anything including // and */
{
out_buff.push_back(buff[i]); // write upto second "
i++;
}
out_buff.push_back(buff[i]); // write last "
}
void full_comment(string & buff, int &i)
{
i++;
while (1)
{
if (buff[i] == '*' && buff[i + 1] == '/') // full comment ends here
break;
i++;
}
i++; // skip the last /
}
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int main(int argc, char *argv[])
{
if (argc < 2)
{
cout << "please try again with an input file name--" << endl;
return -1;
}
int size;
string buff;
string out_buff;
fstream myfile(argv[1], ios::in);
ofstream outfile("nocomments.cpp");
if (myfile.fail())
{
cout << "send correct file !!" << endl;
return -1;
}
int i = 0;
char c;
while (1)
{
myfile.get(c); // read character by character
if (myfile.eof())
break;
buff.push_back(c); // append to string
}
size = buff.length();
for (i = 0; i <= size; i++)
{
if (buff[i] == '/' && buff[i + 1] == '/') // line comment starts here
{
slash_slash(buff, i);
}
if (buff[i] == '"') // string start
{
string_found(buff,out_buff,i);
}
else if (buff[i] == '/' && buff[i + 1] == '*') // full comment starts here
{
full_comment(buff, i);
}
else // normal syntax write
{
out_buff.push_back(buff[i]);
}
}
outfile << out_buff; // write to file
cout << "done!" << endl;
return 0;
}