-
Notifications
You must be signed in to change notification settings - Fork 1
/
StripComments.c
240 lines (220 loc) · 4.69 KB
/
StripComments.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
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
* Name: Strip C Comments
* Description: A C program to strip comments from a c file and output the result to a new file.
* Date: 2022-02-11
* Author: Max Base
* Repository: https://github.com/BaseMax/Strip-C-Comments
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
const char *EXTENSIONS[] = {"c", "py"};
// Read whole of a file and return char*
char *file_read(char *filepath)
{
FILE *fp = fopen(filepath, "r");
if (fp == NULL)
{
// printf("Could not open file %s\n", filepath);
return NULL;
}
fseek(fp, 0, SEEK_END);
long fsize = ftell(fp);
fseek(fp, 0, SEEK_SET);
char *string = malloc(fsize + 1);
fread(string, fsize, 1, fp);
return string;
}
// Remove all of inline comments from a string
char *strip_inline_comments(char *input)
{
char *output = malloc(strlen(input) + 1);
char *p = input;
char *q = output;
while (*p)
{
if (*p == '/' && *(p + 1) == '/')
{
while (*p && *p != '\n')
{
p++;
}
}
else
{
*q++ = *p++;
}
}
*q = '\0';
return output;
}
// Remove all of multiline comments from a string
char *strip_multiline_comments(char *input)
{
char *output = malloc(strlen(input) + 1);
char *p = input;
char *q = output;
int comment = 0;
while (*p)
{
if (*p == '/' && *(p + 1) == '*')
{
comment = 1;
p += 2;
}
else if (*p == '*' && *(p + 1) == '/')
{
comment = 0;
p += 2;
}
else if (!comment)
{
*q++ = *p++;
}
else
{
p++;
}
}
*q = '\0';
return output;
}
char *strip_hash_sign_comments(char *input)
{
char *output = (char *)malloc(strlen(input) + 1);
char *p = input;
char *q = output;
while (*p)
{
if (*p == '#')
while (*p != '\n' && *p)
p++;
else
*q++ = *p++;
}
*q = '\0';
return output;
}
// Trim and remove space, tab, and empty line only from first and last of a string
char *trim(char *input)
{
char *output = malloc(strlen(input) + 1);
char *p = input;
char *q = output;
int skip = 1;
while (*p)
{
if (*p == ' ' || *p == '\t' || *p == '\n')
{
if (!skip)
{
*q++ = *p;
}
}
else
{
skip = 0;
*q++ = *p;
}
p++;
}
*q = '\0';
return output;
}
// remove_two_or_more_empty_lines
char *remove_two_or_more_empty_lines(char *input)
{
char *output = malloc(strlen(input) + 1);
char *p = input;
char *q = output;
int skip = 0;
while (*p)
{
if (*p == '\n')
{
if (skip)
{
skip = 0;
}
else
{
skip = 1;
*q++ = *p;
}
}
else
{
skip = 0;
*q++ = *p;
}
p++;
}
*q = '\0';
return output;
}
// Extract extension of file name
char *extract_extension(char *name)
{
char *extension = (char *)malloc(strlen(name) + 1);
if (extension == NULL)
return NULL;
char *ptr = name;
char *current = extension;
while (*ptr && *ptr != '.')
ptr++;
if (*ptr == '.')
ptr++;
while (*ptr)
*current++ = *ptr++;
return extension;
}
char *comment_remove(char *content, char *file_name)
{
char *extension = extract_extension(file_name);
if (strcmp(extension, *(EXTENSIONS + 0)) == 0)
{
content = strip_inline_comments(content);
content = strip_multiline_comments(content);
}
else if (strcmp(extension, *(EXTENSIONS + 1)) == 0)
{
content = strip_hash_sign_comments(content);
}
content = remove_two_or_more_empty_lines(content);
content = trim(content);
free(extension);
return content;
}
// Start point
int main(int argc, char **argv)
{
if (argc == 1)
{
printf("\nUsage: %s <file>\n\n", argv[0]);
return 1;
}
char *data = file_read(argv[1]);
if (data == NULL)
{
fprintf(stderr, "Could not read file %s\n", argv[1]);
return 1;
}
data = comment_remove(data, argv[1]);
if (argc == 3)
{
FILE *fp = fopen(argv[2], "w");
if (fp == NULL)
{
fprintf(stderr, "Could not open to create file %s\n", argv[2]);
return 1;
}
fprintf(fp, "%s", data);
fclose(fp);
}
else
{
printf("%s\n", data);
}
free(data);
return 0;
}