-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
executable file
·174 lines (136 loc) · 4.27 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
#include <unistd.h>
#include <stdlib.h>
#include <iostream>
#include <set>
#include <map>
#include "FUF.h"
#include "Utils.h"
#define USAGE "\
Usage: " PROGRAM_NAME " OPTION FILE\n\
Try \"" PROGRAM_NAME " --help\" for more informations.\n"
#define HELP "\
Usage: " PROGRAM_NAME " OPTION FILE\n\
Compress or decompress a file to .fuf according to the specified algorithm.\n\
OPTION specify decompression or the type of compression algorithm.\n\
\n\
Compress options:\n\
-h Huffman compression\n\
-d Differencial\n\
-dh Differencial, Huffman compression\n\
-dt Differencial, Transform compression\n\
-dth Differencial, Transform, Huffman compression\n\
-t Transform compression\n\
-td Transform, Differencial compression\n\
-tdh Transform, Differencial, Huffman compression\n\
\n\
Decompress option:\n\
--dec Decompress a .fuf file\n\
\n\
\n\
The Transform compression used is the MDCT (Modified Discrete Cossine Transform) algorithm.\n\
\n\
Fun fact: .fuf stands for 'Formato Ultra Foda'.\n\n"
#define BAD_OPTION "Bad option: "
#define NO_SUCH_FILE "No such file: "
using namespace std;
bool processInput(int argc, char** argv, string& option, string &filename);
int main(int argc, char** argv) {
string option = "";
string filename = "";
if (!processInput(argc, argv, option, filename)) {
cout << USAGE << endl;
return EXIT_SUCCESS;
}
// Remove the extension
string noExtentionFilename = filename.substr(0, filename.find_last_of("."));
cout << noExtentionFilename << endl;
// Object Instance
FUF fuf;
if (option == "--debug") {
// Debug option!
// TODO colocar código de depuração aqui!
fuf.readFromFile(filename.c_str(), EXTENSION_WAV);
printWavInfo(fuf.sample->info);
// fuf.compress(TRANSFORM);
// fuf.decompress();
// for (int c = 0; c < fuf.sample->data.channelCount; c++) {
// fuf.sample->data.data[c] = fuf.compressedData->getData(c);
// }
// fuf.writeToFile(noExtentionFilename.c_str(), EXTENSION_WAV);
return EXIT_SUCCESS;
}
if (option == "--dec") {
fuf.readFromFile(filename.c_str(), EXTENSION_FUF);
cout << "Decompressing file " << filename << endl;
fuf.decompress();
fuf.writeToFile(noExtentionFilename.c_str(), EXTENSION_WAV);
}
else {
fuf.readFromFile(filename.c_str(), EXTENSION_WAV);
compressMode algorithm[] = { NONE, NONE, NONE };
map<char, compressMode> compressMap;
compressMap['h'] = HUFFMAN;
compressMap['t'] = TRANSFORM;
compressMap['d'] = DIFFERENCE;
switch(option.size()) {
case 4:
algorithm[2] = compressMap[option[3]];
case 3:
algorithm[1] = compressMap[option[2]];
case 2:
algorithm[0] = compressMap[option[1]];
break;
}
fuf.compress(algorithm[0], algorithm[1], algorithm[2]);
cout << "File compressed successfully!" << endl;
fuf.writeToFile(noExtentionFilename.c_str(), EXTENSION_FUF);
}
return EXIT_SUCCESS;
}
bool processInput(int argc, char** argv, string& option, string &filename) {
string s1, s2 = "";
if (argc > 1) {
s1 = string(argv[1]);
}
if (argc > 2) {
s2 = string(argv[2]);
}
if (s1[0] == '-') {
option = s1;
filename = s2;
}
else if (s2[0] == '-') {
option = s2;
filename = s1;
}
if (argc < 2 || argc > 3) {
return false;
}
set<string> options;
options.insert("-h");
options.insert("-d");
options.insert("-dh");
options.insert("-dt");
options.insert("-dth");
options.insert("-t");
options.insert("-td");
options.insert("-tdh");
options.insert("--dec");
options.insert("--deb");
bool isHelp = (option == "--help");
bool badOption = (argc > 2 && !options.count(option));
bool invalidFileName = (argc > 2 && access(filename.c_str(), F_OK) == -1);
if (isHelp) {
cout << HELP << endl;
exit(EXIT_SUCCESS);
}
if (badOption) {
cout << BAD_OPTION << option << endl;
return false;
}
if (invalidFileName) {
cout << NO_SUCH_FILE << filename << endl;
return false;
}
return true;
}