-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
306 lines (245 loc) · 9.64 KB
/
main.cpp
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
// Copyright 2023 Sneller, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cerrno>
#include <string>
#include <stdexcept>
#include <iostream>
#include <variant>
#include <map>
#include <memory>
#include "iguana/error.h"
#include "iguana/file.h"
#include "iguana/output_stream.h"
#include "iguana/decoder.h"
#include "iguana/encoder.h"
#if !defined(IGUANA_COMPILER_MSVC)
// TODO: use a proper makefile
#include "iguana/common.cpp"
#include "iguana/ans_byte_statistics.cpp"
#include "iguana/ans_nibble_statistics.cpp"
#include "iguana/ans1.cpp"
#include "iguana/ans32.cpp"
#include "iguana/ans_nibble.cpp"
#include "iguana/ans_bitstream.cpp"
#include "iguana/error.cpp"
#include "iguana/entropy.cpp"
#include "iguana/output_stream.cpp"
#include "iguana/decoder.cpp"
#include "iguana/encoder.cpp"
#include "iguana/c_bindings.cpp"
#include "iguana/file.cpp"
#endif
//
namespace iguana {
class command_line final {
class parser;
using parameter_type = std::variant<std::string, double, std::monostate>;
using map_type = std::map<std::string, parameter_type>;
private:
const map_type m_options;
public:
command_line(int argc, const char* const* const argv);
~command_line() {}
command_line(const command_line&) = delete;
command_line& operator =(const command_line&) = delete;
public:
bool contains(const std::string& name) const noexcept;
template <
typename T
> T get(const std::string& name) const;
template <
typename T
> T get(const std::string& name, T default_value) const;
};
//
template <
typename T
> T command_line::get(const std::string& name) const {
const auto r = m_options.find(name);
if (r == m_options.cend()) {
throw std::invalid_argument(std::string("the option '") + name + "' has not been provided");
}
return std::get<T>(r->second);
}
template <
typename T
> T command_line::get(const std::string& name, T default_value) const {
if (!contains(name)) {
return default_value;
}
return this->template get<T>(name);
}
//
class command_line::parser final {
const int m_argc = 0;
const char* const* const m_argv = nullptr;
int m_cursor = 0;
map_type m_options;
public:
parser(int argc, const char* const* const argv)
: m_argc(argc)
, m_argv(argv) {}
~parser() {}
parser(const parser&) = delete;
parser& operator =(const parser&) = delete;
parser(parser&&) = delete;
parser& operator =(parser&&) = delete;
public:
map_type parse();
private:
std::string get_string_parameter_for(const char* name);
double get_double_parameter_for(const char* name);
template <
typename T
> void add(const char* short_name, const char* long_name, T value);
void add(const char* short_name, const char* long_name);
};
//
template <
typename T
> void command_line::parser::add(const char* short_name, const char* long_name, T value) {
if (m_options.contains(short_name)) {
throw std::invalid_argument(std::string("the option '") + short_name + "' has already been specified");
}
if (m_options.contains(long_name)) {
throw std::invalid_argument(std::string("the option '") + long_name + "' has already been specified");
}
m_options[short_name] = value;
m_options[long_name] = std::move(value);
}
}
//
iguana::command_line::command_line(int argc, const char* const* const argv)
: m_options(parser(argc, argv).parse()) {}
bool iguana::command_line::contains(const std::string& name) const noexcept {
return m_options.contains(name);
}
iguana::command_line::map_type iguana::command_line::parser::parse() {
for(m_cursor = 1; m_cursor < m_argc;) {
const char* const opt = m_argv[m_cursor++];
if ((std::strcmp(opt, "-h") == 0) || (std::strcmp(opt, "--help") == 0)) {
add("h", "help");
continue;
}
if ((std::strcmp(opt, "-d") == 0) || (std::strcmp(opt, "--decompress") == 0)) {
add("d", "decompress", get_string_parameter_for(opt));
continue;
}
if ((std::strcmp(opt, "-o") == 0) || (std::strcmp(opt, "--output") == 0)) {
add("o", "output", get_string_parameter_for(opt));
continue;
}
if ((std::strcmp(opt, "-i") == 0) || (std::strcmp(opt, "--input") == 0)) {
add("i", "input", get_string_parameter_for(opt));
continue;
}
if ((std::strcmp(opt, "-t") == 0) || (std::strcmp(opt, "--threshold") == 0)) {
const auto v = get_double_parameter_for(opt);
if ((v < 0.0) || (v > 1.0)) {
throw std::invalid_argument(std::string("the value for the option '") + opt + "' must be in the range [0.0, 1.0]");
}
add("t", "threshold", v);
continue;
}
if ((std::strcmp(opt, "-e") == 0) || (std::strcmp(opt, "--entropy") == 0)) {
const auto v = get_string_parameter_for(opt);
if ((v != "none") && (v != "ans32") && (v != "ans") && (v != "ans_nibble")) {
throw std::invalid_argument(std::string("unrecognized entropy mode '") + v + "' supplied for the option '" + opt + "'");
}
add("e", "entropy", v);
continue;
}
if ((std::strcmp(opt, "-x") == 0) || (std::strcmp(opt, "--encoding") == 0)) {
const auto v = get_string_parameter_for(opt);
if ((v != "raw") && (v != "iguana")) {
throw std::invalid_argument(std::string("unrecognized encoding '") + v + "' supplied for the option '" + opt + "'");
}
add("x", "encoding", v);
continue;
}
throw std::invalid_argument(std::string("unrecognized option '") + opt + "'");
}
return std::move(m_options);
}
std::string iguana::command_line::parser::get_string_parameter_for(const char* opt) {
if (m_cursor >= m_argc) {
throw std::invalid_argument(std::string("no argument provided for '") + opt + "'");
}
const char* const param = m_argv[m_cursor];
if (param == nullptr) {
throw std::invalid_argument(std::string("null argument provided for '") + opt + "'");
}
if (std::strlen(param) == 0) {
throw std::invalid_argument(std::string("empty argument provided for '") + opt + "'");
}
if (param[0] == '-') {
throw std::invalid_argument(std::string("no argument provided for '") + opt + "'");
}
++m_cursor;
return param;
}
double iguana::command_line::parser::get_double_parameter_for(const char* opt) {
return std::stod(get_string_parameter_for(opt));
}
void iguana::command_line::parser::add(const char* short_name, const char* long_name) {
add(short_name, long_name, std::monostate{});
}
//
int main(int argc, char *argv[]) try {
iguana::command_line options(argc, argv);
if (options.contains("help")) {
std::cout << argv[0] << " [args] [-o file]" << std::endl;
std::cout << " -h, --help" << std::endl;
std::cout << " -i, --input file" << std::endl;
std::cout << " -o, --output file" << std::endl;
std::cout << " -d, --decompress" << std::endl;
std::cout << " -t, --threshold" << std::endl;
std::cout << " -e, --entropy" << std::endl;
std::cout << " -x, --encoding" << std::endl;
return EXIT_SUCCESS;
}
{ iguana::file f_out(stdout, false);
if (options.contains("output")) {
const auto path = options.get<std::string>("output");
f_out.open(path.c_str(), "wb");
}
iguana::file f_in(options.get<std::string>("input"), "rb");
const auto f_in_size = f_in.size();
const std::unique_ptr<std::uint8_t []> data_in(new std::uint8_t[f_in_size]);
f_in.read(data_in.get(), f_in_size);
if (options.contains("decompress")) {
} else {
iguana::output_stream dst;
{ const iguana::encoder::part ep = {
.m_entropy_mode = iguana::entropy_mode_from_string(options.get<std::string>("entropy", "ans32").c_str()),
.m_encoding = iguana::encoding_from_string(options.get<std::string>("encoding", "iguana").c_str()),
.m_rejection_threshold = options.get<double>("threshold", 1.0)
};
iguana::encoder enc;
enc.encode(dst, ep);
}
printf("dst len = %zu\n", dst.size());
}
}
return EXIT_SUCCESS;
} catch(const std::exception& e) {
std::cerr << "exception: " << e.what() << std::endl;
return EXIT_FAILURE;
} catch(...) {
std::cerr << "unrecognized exception" << std::endl;
return EXIT_FAILURE;
}