-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.cpp
More file actions
221 lines (216 loc) · 7.23 KB
/
main.cpp
File metadata and controls
221 lines (216 loc) · 7.23 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
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
// Copyright (C) 2021 Antti Helminen
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//-----------------------------------------------------------------------------
#include "header.h"
#define MAJOR_VERSION 2
#define MINOR_VERSION 5
#define PATCH_VERSION 0
int main(int argc, char **argv)
{
const std::chrono::steady_clock::time_point start{std::chrono::steady_clock::now()};
std::vector<char*>input;
char* output{nullptr};
const char* mode{"a"};
bool log{false};
bool obfuscation{false};
std::string bytes;
std::filesystem::path filePath{std::filesystem::current_path()};
uintmax_t inputSize{0}, outputSize{0}, reducedSize{0};
// ------------------------------------------------------------
// handle arguments
// ------------------------------------------------------------
#ifdef __GNUC__
int opt = 0;
while ((opt = getopt(argc, argv, "hvi:o:wtf")) != -1) {
switch (opt) {
//help
case 'h':
std::cout
<< "-- Minifier --\n"
<< "You can minify all sorts of file types\n"
<< "html, css, js, xml... etc etc.\n"
<< "\n"
<< "Options:\n"
<< "-v Version info\n"
<< "-i Input filename\n"
<< "-o Output filename, or if ommitted then output will be printed to stdout.\n"
<< "-w Change output file acces mode to overwrite. Default mode is append.\n"
<< "-t Log results after minify. (time & compression)\n"
<< "-f Obfuscate javascript variables\n"
<< "\n"
<< "Feedback:\n"
<< "https://github.com/Brebl/minifier\n";
exit(0);
break;
//version
case 'v':
std::cout << "Minifier version: " << MAJOR_VERSION << "." << MINOR_VERSION << "." << PATCH_VERSION << "\n";
exit(0);
break;
//input filename
case 'i':
input.emplace_back(optarg);
filePath = optarg;
inputSize += std::filesystem::file_size(filePath);
break;
//output filename
case 'o':
output = optarg;
if(!output) {
throw std::runtime_error("output file error\n");
}
break;
//overwrite file access mode
case 'w':
mode = "w";
break;
//get timer output
case 't':
log = true;
break;
//obfuscate
case 'f':
obfuscation = true;
break;
default:
abort();
}
}
#elif
#error Need GNU compiler
#endif
// ------------------------------------------------------------
// do stuff with those arguments
// ------------------------------------------------------------
if (argc < 2) {
std::cout
<< "This is Minifier\n"
<< "You can minify all sorts of file types...\n"
<< "Eg. html, css, js, xml... etc etc.\n"
<< "\n"
<< "minifier -h for more help\n";
exit(0);
}
if (output) {
#ifdef __linux__
FILE* stream = nullptr;
stream = freopen(output, mode, stdout);
if(!stream) {
throw std::runtime_error("error on freopen");
}
#endif
#ifdef _WIN32
errno_t err;
FILE* stream = nullptr;
err = freopen_s(&stream, output, mode, stdout);
if( err != 0 ) {
std::stringstream ss;
ss << err;
throw std::runtime_error("error on freopen_s: " + ss.str());
}
#endif
}
std::ifstream in;
for(auto i: input) {
in.open(i);
if (!in) {
throw std::runtime_error("input file opening error\n");
}
BYTE b;
while (in.get(b)) {
//inside quotes, do nothing
if(b == '\'') {
do {
std::cout << b;
in.get(b);
if(b == '\\') {
std::cout << b;
in.get(b);
std::cout << b;
in.get(b);
}
} while (b != '\'');
}
if(b == '\"') {
do {
std::cout << b;
in.get(b);
if(b == '\\') {
std::cout << b;
in.get(b);
std::cout << b;
in.get(b);
}
} while (b != '\"');
}
//after punct, discard all spaces until graph
//punct means: !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~
if (ispunct(b)) {
while (isspace(in.peek())) {
in.ignore();
}
}
//after space, discard all spaces until graph
//space means: space, tab and white-space control codes
if (isspace(b) && isspace(in.peek())) {
while (isspace(in.peek())) {
in.ignore();
}
continue;
}
//discard space, if next is punct
if (isspace(b) && ispunct(in.peek())) {
continue;
}
//comments
if (b == '/') {
//discard line comments
if (in.peek() == '/') {
in.ignore(MAX, '\n');
continue;
}
//discard block comments
if (in.peek() == '*') {
while (in.ignore(MAX, '*')) {
if (in.peek() == '/') {
in.ignore();
break;
}
}
continue;
}
}
//obfuscate
if(obfuscation) {
obfuscate(in, b);
}
else {
std::cout << b;
}
}
in.close();
}
std::chrono::steady_clock::time_point finish{std::chrono::steady_clock::now()};
std::chrono::milliseconds ms = std::chrono::duration_cast<std::chrono::milliseconds>(finish - start);
if(log) {
std::clog << "Minify took: " << ms.count() << "ms\n";
if(output) {
filePath = output;
outputSize = std::filesystem::file_size(filePath);
reducedSize = inputSize - outputSize;
std::clog << "Filesize reduced by " << reducedSize << " bytes (";
reducedSize *= 100;
reducedSize /= inputSize;
std::clog << reducedSize << "%)\n";
}
}
}