-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompactor.c
233 lines (213 loc) · 4.55 KB
/
compactor.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
/*
* compactor.c
*
* This file is part of "BoolX"
* Copyright (c) 2021 Andrea Calligaris
* Distributed under the MIT License, see "license.txt"
*/
#include <ctype.h> // isprint
#include <getopt.h>
#include <stdbool.h> // bool
#include <stdio.h> // printf, file stuff
#include <stdlib.h> // abort
#define LINES_LENGTH_DEFAULT 36
static bool show_usage = false;
static char *source_program_path;
static char *output_program_path;
static long int lines_length = LINES_LENGTH_DEFAULT;
static long long int n_nested_comments = 0;
static int
process_arguments(int argc, char *argv[])
{
char *lines_length_arg = NULL;
int c;
opterr = 0;
int non_option_argc;
static struct option long_options[] = {
{"lines_length", required_argument, NULL, 'l'}, {NULL, 0, NULL, 0}};
while ((c = getopt_long(argc, argv, "l:", long_options, NULL)) != -1) {
switch (c) {
case 'l': {
lines_length_arg = optarg;
break;
}
case '?': {
if (optopt == 'l') {
fprintf(
stderr,
"Option -%c requires an argument.\n",
optopt);
} else if (isprint(optopt)) {
fprintf(
stderr, "Unknown option `-%c'.\n", optopt);
} else {
fprintf(
stderr,
"Unknown option character `\\x%x'.\n",
optopt);
}
return 1;
}
default: {
abort();
}
}
}
if (lines_length_arg != NULL) {
lines_length = strtol(lines_length_arg, NULL, 10);
if (lines_length == 0) {
fprintf(
stderr,
"Option '-l' has been given a bad value.\n");
}
}
non_option_argc = argc - optind;
if (non_option_argc == 0) {
show_usage = true;
return 0;
} else if (non_option_argc == 1) {
printf("Missing output file.\n");
return 1;
} else if (non_option_argc == 2) {
source_program_path = argv[optind];
output_program_path = argv[optind + 1];
return 0;
} else {
printf("Too many arguments.\n");
return 1;
}
}
static bool
is_instruction_to_be_included(char current_instruction)
{
switch (current_instruction) {
case '>':
return true;
case '<':
return true;
case '|':
return true;
case '+':
return true;
case '-':
return true;
case '=':
return true;
case '_':
return true;
case '^':
return true;
case '*':
return true;
case '%':
return true;
case ']':
return true;
case '[':
return true;
case '#':
return true;
case '&':
return true;
case '?':
return true;
case '"':
return true;
case '!':
return true;
case ';':
return true;
case ':':
return true;
case '/':
return true;
case '\\':
return true;
case '$':
return true;
case '\'':
return true;
case '@':
return true;
case '~':
return true;
default:
return false;
}
}
static void
print_usage()
{
printf("Usage: compactorx [options] <source_file> <output_file>\n");
printf("\nA compactor for BoolX code; v1.0."
"\nRemove comments and other useless characters from "
"<source_file>\n"
"and save the result to <output_file>, with the goal of "
"creating an\n"
"artistic and esoteric source code.\n");
printf(
" -l N set the max number of characters in "
"each line to N\n"
" (default is %d)\n",
LINES_LENGTH_DEFAULT);
}
int
main(int argc, char *argv[])
{
FILE *source_program;
FILE *output_file;
char current_instruction;
bool writing_error = false;
long int char_line_counter = 0;
if (process_arguments(argc, argv) != 0) {
return 1;
}
if (show_usage) {
print_usage();
return 0;
}
source_program = fopen(source_program_path, "r");
if (source_program == NULL) {
printf("Can't open the source program file.\n");
return 1;
}
output_file = fopen(output_program_path, "w");
if (output_file == NULL) {
printf("Can't open the output file.\n");
return 1;
}
/* Read the instructions one by one. */
while (fscanf(source_program, "%1c", ¤t_instruction) == 1) {
if (current_instruction == '{') {
n_nested_comments++;
continue;
} else if (current_instruction == '}') {
n_nested_comments--;
continue;
}
if (n_nested_comments > 0) {
continue;
}
if (is_instruction_to_be_included(current_instruction)) {
if (putc(current_instruction, output_file) == EOF) {
printf("Error while writing to the output "
"file.\n");
writing_error = true;
break;
}
printf("%c", current_instruction);
char_line_counter++;
if (char_line_counter == lines_length) {
putc('\n', output_file);
printf("\n");
char_line_counter = 0;
}
}
}
if (writing_error == false) {
printf("\nDone.\n");
}
fclose(output_file);
fclose(source_program);
return 0;
}