-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
221 lines (188 loc) · 5.43 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
#include <iostream>
#include <fstream>
#include <cstring>
#include <filesystem>
#include "huffman.hpp"
// Messages
#define ENTER_HELP "Enter '-h' as argument for help.\n"
#define INVALID_ARG "Invalid argument received.\n" ENTER_HELP
#define INVALID_OPTION_COMBINATION "Invalid option combination,\n" ENTER_HELP
#define SAME_PATH "Source and destination cannot be the same.\n"
#define FILE_IS_EMPTY "File is empty.\n"
// Exit Code
#define EC_GOOD 0
#define EC_JUST_ERROR -1
#define EC_INVALID_ARG -2
#define EC_UNABLE_TO_OPEN_FILE -3
#define EC_INVALID_OPTION_COMBINATION -4
#define EC_SAME_PATH -5
#define EC_EMPTY_FILE -6
// Options
#define ENCODE 01
#define DECODE 02
#define HELP 04
#define PRINT_SIZE 010
#define REMOVE_SOURCE 020
using namespace std;
namespace fs = std::filesystem;
void PrintHelp();
void PrintSize(const fs::path& src, const fs::path& dst);
int FillOption(int& option, char str[]);
int main(int argc, char* argv[])
try {
int options = 0;
int i;
for (i = 1; i < argc && argv[i][0] == '-'; i++) {
int err_code = FillOption(options, argv[i] + 1);
if (err_code != EC_GOOD)
return err_code;
}
if (options & HELP) {
PrintHelp();
return EC_GOOD;
}
fs::path dst_path;
if (options & ENCODE) {
if (argc - i == 2) {
dst_path = argv[i + 1];
if (fs::is_directory(dst_path))
dst_path = dst_path / fs::path(argv[i]).filename().replace_extension("huf");
}
else if (argc - i == 1) {
dst_path = fs::path(argv[i]).replace_extension("huf");
}
else {
cerr << INVALID_ARG;
return EC_INVALID_ARG;
}
if (argv[i] == dst_path) {
cerr << SAME_PATH;
return EC_SAME_PATH;
}
ofstream os{ dst_path, ios_base::binary };
if (!os.good()) {
auto ec = make_error_code(huf_errc::invalid_fstream);
throw fs::filesystem_error{ "main", dst_path, ec };
}
Huffman::Compress(argv[i], os);
flush(os);
}
else if (options & DECODE) {
if (argc == i) {
cerr << INVALID_ARG;
return EC_INVALID_ARG;
}
ifstream is{ argv[i], ios_base::binary };
if (!is.good()) {
auto ec = make_error_code(huf_errc::invalid_fstream);
throw fs::filesystem_error{ "main", argv[i], ec };
}
else if (is.peek() == EOF) {
cerr << FILE_IS_EMPTY;
return EC_EMPTY_FILE;
}
dst_path = (argc - i == 2) ? argv[i + 1] : fs::path(argv[i]).parent_path();
if (argv[i] == dst_path) {
cerr << SAME_PATH;
return EC_SAME_PATH;
}
dst_path /= Huffman::DecompressRetFilename(is, dst_path);
}
else {
cerr << INVALID_ARG;
return EC_INVALID_ARG;
}
if (options & PRINT_SIZE)
PrintSize(argv[i], dst_path);
if (options & REMOVE_SOURCE)
fs::remove_all(argv[i]);
return EC_GOOD;
}
catch (fs::filesystem_error& e) {
cout << e.what() << endl;
return e.code().value();
}
catch (exception& e) {
cout << e.what() << endl;
return EC_JUST_ERROR;
}
catch (...) {
cout << "Unknown error";
}
void PrintHelp()
{
cout << "usage: app_name [options] source [destination]\n"
"ex) huffman -e -s -r source.txt destination.huf\n"
" options:\n"
" All options are compared by first letter only\n"
" -h (help) print help. No source and destination input required.\n"
" -e (encode) Compress the source and save it to the destination.\n"
" -d (decode) Decompress the source and save it to the destination.\n"
" -s (size) Print the size of the source file and destination file.\n"
" -r (remove) Delete source file.\n"
" source:\n"
" Path to the target file to be compressed or decompressed.\n"
" Cannot be the same as the destination\n"
" destination:\n"
" The path to the file in which to save the compressed or unpacked results.\n"
" Cannot be the same as the source\n";
}
uintmax_t GetDirectorySize(const fs::path& dir_path)
{
uintmax_t size = 0;
for (const auto& entry : fs::recursive_directory_iterator(dir_path))
size += entry.file_size();
return size;
}
void PrintSize(const fs::path& src, const fs::path& dst)
{
auto source_size = fs::is_directory(src) ?
GetDirectorySize(src) : fs::file_size(src);
auto destination_size = fs::is_directory(dst) ?
GetDirectorySize(dst) : fs::file_size(dst);
cout << "source: " << source_size << "bytes, ";
cout << "destination: " << destination_size << "bytes, ";
if (source_size >= destination_size) {
cout << "decrease: " << source_size - destination_size << "bytes, ";
cout << (1 - (long double)destination_size / source_size) * 100 << '%';
}
else {
cout << "increase: " << destination_size - source_size << "bytes, ";
cout << ((long double)destination_size / source_size - 1) * 100 << '%';
}
cout << endl;
}
int FillOption(int& option, char str[])
{
for (; *str; str++) {
switch (*str) {
case 'e':
if (option & (DECODE | HELP)) goto ERROR;
option |= ENCODE;
break;
case 'd':
if (option & (ENCODE | HELP)) goto ERROR;
option |= DECODE;
break;
case 'h':
if (option & (~HELP)) //& (ENCODE | DECODE | PRINT_SIZE | REMOVE_SOURCE))
goto ERROR;
option |= HELP;
break;
case 's':
if (option & HELP) goto ERROR;
option |= PRINT_SIZE;
break;
case 'r':
if (option & HELP) goto ERROR;
option |= REMOVE_SOURCE;
break;
default:
goto ERROR;
}
}
return EC_GOOD;
ERROR:
cerr << INVALID_OPTION_COMBINATION;
return EC_INVALID_OPTION_COMBINATION;
}