forked from steven-schronk/C-Programming-Examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathex_1-23.c
More file actions
105 lines (81 loc) · 1.8 KB
/
ex_1-23.c
File metadata and controls
105 lines (81 loc) · 1.8 KB
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
100
101
102
103
104
105
/*
store state
store previous char
only check when we are not in a quoted area
check current char for * or / if prev char is a / than we are in a comment
echo out prev char
*/
#include <stdio.h>
void advbuf(char buf[], char mrk[])
{
buf[0] = buf[1];
mrk[0] = mrk[1];
//buf[1] = buf[2];
//mrk[1] = mrk[2];
/* if buffer has comment string, continue comment by default */
/* similar to carrying in math :) */
}
int main()
{
char buf[2], mrk[2] = { '\0' };
/* mrk c = comment q = quote p = program */
char strtyp = '\0'; /* store string type */
int comment = 0;
/* pre-load buffer */
for(;;)
{
advbuf(buf, mrk);
buf[1] = getchar(); // load new char at start of buffer
if(buf[1] == EOF) { return 0; } /* exit when we get to end of input */
mrk[1] = 'p';
/* found new quote */
if(buf[1] == '\'' || buf[1] == '"') /* found one of the quotes */
{
mrk[1] = 'q';
strtyp = buf[1];
}
/* if quote is in effect, keep quoting until we find corresponding quote */
if(strtyp == '\'' || strtyp == '"')
{
if(mrk[0] == 'q')
{
if(buf[1] == strtyp)
{
strtyp = '\0';
}
mrk[1] = 'q';
}
}
if(mrk[1] == 'p' || mrk[1] == 'c')
{
/* look for start of comment */
if(buf[1] == '*' || buf[1] == '/')
{
if(buf[0] == '/')
{
mrk[0] = 'c';
mrk[1] = 'c';
comment = 1;
}
}
/* look for end of comment */
if(comment == 1)
{
if(buf[1] == '/')
if(buf[0] == '*' || buf[0] == '/')
{
mrk[0] = 'c';
mrk[1] = 'c';
comment = 0;
}
mrk[1] = 'c';
}
}
//printf("%c%c", buf[0], buf[1]);
//printf("->%c%c\n", mrk[0], mrk[1]);
/* based on current state print output */
if(mrk[0] == 'p' || mrk[0] == 'q') { putchar(buf[0]); }
}
printf("This is a comment in a quote /* comment here */ just ignore it.\n");
return 0;
}