-
Notifications
You must be signed in to change notification settings - Fork 0
/
reflect-c_EXPAND_COMMENTS.c
65 lines (54 loc) · 1.59 KB
/
reflect-c_EXPAND_COMMENTS.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static const char COMMENT_OPEN[] = "/*#!";
static const char COMMENT_CLOSE[] = "*/";
enum reflectc_comment_state { REFLECTC_COMMENT_OFF = 0, REFLECTC_COMMENT_ON };
static void
expand_comments(char *buf, enum reflectc_comment_state state)
{
while (*buf != '\0') {
switch (state) {
case REFLECTC_COMMENT_OFF:
if (0 != strncmp(buf, COMMENT_OPEN, sizeof(COMMENT_OPEN) - 1))
break;
/* make sure directive is on a new line */
buf[sizeof(COMMENT_OPEN) - 1] = '\n';
buf += sizeof(COMMENT_OPEN) - 1;
state = REFLECTC_COMMENT_ON;
continue;
case REFLECTC_COMMENT_ON:
if (0 != strncmp(buf, COMMENT_CLOSE, sizeof(COMMENT_CLOSE) - 1))
break;
buf += sizeof(COMMENT_CLOSE) - 1;
state = REFLECTC_COMMENT_OFF;
continue;
}
fputc(*buf, stdout);
++buf;
}
}
int
main()
{
enum reflectc_comment_state state = REFLECTC_COMMENT_OFF;
size_t bufsize = 2048;
size_t count;
char *buf = malloc(bufsize);
if (!buf) return EXIT_FAILURE;
while ((count = fread(buf, sizeof(char), bufsize - 1, stdin))) {
if (count >= bufsize - 1) {
char *tmp = realloc(buf, bufsize * 2);
if (!tmp) {
free(buf);
return EXIT_FAILURE;
}
buf = tmp;
bufsize *= 2;
}
buf[count] = '\0';
expand_comments(buf, state);
}
free(buf);
return EXIT_SUCCESS;
}