-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHuffman.cpp
194 lines (166 loc) · 5.24 KB
/
Huffman.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
/***************************************************************************
The Base Framework (Test Suite)
A framework for developing platform independent applications
See COPYRIGHT.txt for details.
This framework 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.
For the licensing terms refer to the file 'LICENSE'.
***************************************************************************/
#include <base/Application.h>
#include <base/string/FormatOutputStream.h>
#include <base/string/StringOutputStream.h>
#include <base/io/FileOutputStream.h>
#include <base/compression/Huffman.h>
#include <base/Random.h>
#include <base/Cast.h>
using namespace com::azure::dev::base;
class HuffmanApplication : public Application {
private:
static const unsigned int MAJOR_VERSION = 1;
static const unsigned int MINOR_VERSION = 0;
public:
enum Command {
COMMAND_ERROR,
COMMAND_HELP,
COMMAND_SIMPLE,
COMMAND_COMPRESS,
COMMAND_UNCOMPRESS,
COMMAND_RANDOM
};
HuffmanApplication()
: Application("Huffman")
{
}
void compress(
const String& source, const String& destination) {
File sourceFile(source, File::READ, 0);
unsigned long long size = sourceFile.getSize();
bassert(
size <= PrimitiveTraits<unsigned int>::MAXIMUM,
IOException("File is too big for me.", this)
);
Allocator<uint8> buffer(size);
sourceFile.read(buffer.getElements(), buffer.getSize());
sourceFile.close();
FileOutputStream fos(destination, File::CREATE|File::TRUNCATE);
Huffman huffman;
huffman.encode(
fos,
buffer.getElements(),
buffer.getSize()
);
}
void uncompress(
const String& source,
const String& destination) {
File sourceFile(source, File::READ, 0);
unsigned long long size = sourceFile.getSize();
bassert(
size <= PrimitiveTraits<unsigned int>::MAXIMUM,
IOException("File is too big for me.", this)
);
Allocator<uint8> buffer(size);
sourceFile.read(buffer.getElements(), buffer.getSize());
sourceFile.close();
FileOutputStream fos(destination, File::CREATE|File::TRUNCATE);
Huffman huffman;
huffman.decode(fos, buffer.getElements(), buffer.getSize());
}
void random(const String& destination)
{
unsigned int size = Random::random<unsigned int>() % (64 * 1024);
Allocator<uint8> buffer(size);
for (unsigned int i = 0; i < buffer.getSize(); ++i) {
unsigned int r = Random::random<unsigned int>();
buffer.getElements()[i] = r; // slow
}
File file(destination, File::WRITE, File::CREATE|File::TRUNCATE);
file.write(buffer.getElements(), buffer.getSize());
}
void simple(const String& destination)
{
StringOutputStream stream;
stream << getFormalName() << " version "
<< MAJOR_VERSION << '.' << MINOR_VERSION << EOL
<< "The Base Framework (Test Suite)" << EOL
<< FLUSH;
String message = stream.getString();
FileOutputStream fos(destination, File::CREATE|File::TRUNCATE);
Huffman huffman;
huffman.encode(
fos,
message.getBytes(),
message.getLength()
);
}
void main()
{
fout << getFormalName() << " version "
<< MAJOR_VERSION << '.' << MINOR_VERSION << EOL
<< "The Base Framework (Test Suite)" << EOL
<< ENDL;
Command command = COMMAND_ERROR;
String sourceFile;
String destinationFile;
const Array<String> arguments = getArguments();
switch (arguments.getSize()) {
case 1:
if (arguments[0] == "--help") {
command = COMMAND_HELP;
}
break;
case 2:
if (arguments[0] == "--simple") {
command = COMMAND_SIMPLE;
} else if (arguments[0] == "--random") {
command = COMMAND_RANDOM;
}
destinationFile = arguments[1];
break;
case 3:
if (arguments[0] == "--compress") {
command = COMMAND_COMPRESS;
} else if (arguments[0] == "--uncompress") {
command = COMMAND_UNCOMPRESS;
}
sourceFile = arguments[1];
destinationFile = arguments[2];
break;
}
try {
switch (command) {
case COMMAND_ERROR:
ferr << "Error: Invalid argument(s)." << EOL
<< "For help run: " << getFormalName() << " --help" << EOL
<< ENDL;
setExitCode(EXIT_CODE_ERROR);
break;
case COMMAND_HELP:
fout << "Options" << EOL
<< " --compress source destination" << EOL
<< " --uncompress source destination" << EOL
<< " --simple destination" << EOL
<< " --random destination" << EOL
<< " --help" << EOL
<< ENDL;
break;
case COMMAND_SIMPLE:
simple(destinationFile);
break;
case COMMAND_COMPRESS:
compress(sourceFile, destinationFile);
break;
case COMMAND_UNCOMPRESS:
uncompress(sourceFile, destinationFile);
break;
case COMMAND_RANDOM:
random(destinationFile);
break;
}
} catch (Exception& e) {
exceptionHandler(e);
}
}
};
APPLICATION_STUB(HuffmanApplication);