-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
111 lines (100 loc) · 2.29 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
#include <iostream>
#include <memory>
#include <unistd.h>
#include "SMiniz.h"
#include <vector>
#include <unordered_set>
#include <csignal>
#define BUF_SIZE 10000
volatile bool should_exit = false;
void sighandler(int)
{
std::cout << "ending process" << std::endl;
should_exit = true;
}
class CompDecompFile
{
private:
FILE* _fileHandle = nullptr;
std::unique_ptr<util::SMinizDecompress> _decompressor;
std::unique_ptr<util::SMinizCompress> _compressor;
public:
CompDecompFile(const std::string& filepath)
{
_fileHandle = fopen(filepath.c_str(), "rb");
}
void deCompress(std::function<bool(const unsigned char* data, size_t length)> callback)
{
char buf[BUF_SIZE];
_decompressor = std::make_unique<util::SMinizDecompress>(callback);
_decompressor->initialize();
while(!should_exit)
{
size_t readLen = fread(buf, 1, BUF_SIZE, _fileHandle);
if (0 == readLen)
break;
_decompressor->decompressData((unsigned char*)buf, readLen);
}
_decompressor.reset();
}
void Compress(std::function<bool(const unsigned char* data, size_t length)> callback)
{
char buf[BUF_SIZE];
_compressor = std::make_unique<util::SMinizCompress>(callback);
_compressor->initialize();
while(!should_exit)
{
size_t readLen = fread(buf, 1, BUF_SIZE, _fileHandle);
if (0 == readLen)
break;
_compressor->compressData((unsigned char*)buf, readLen);
}
_compressor.reset();
}
};
void processfunc(unsigned char* buf)
{
}
int main(int argc, char** argv)
{
signal(SIGTERM, sighandler);
signal(SIGINT, sighandler);
bool decompress;
std::string filepath;
int c;
while((c = getopt(argc, argv, "cdf:")) != -1)
{
switch(c)
{
case 'f':
if (optarg)
filepath = optarg;
break;
case 'd':
decompress = true;
break;
case 'c':
decompress = false;
break;
}
}
if (!filepath.empty())
{
CompDecompFile df(filepath);
std::vector<char> dataRead;
if (decompress)
{
df.deCompress([&](const unsigned char* data, size_t length)->bool {
// handle the uncompressed data here
return true;
});
}
else
{
df.Compress([&](const unsigned char* data, size_t length)->bool {
// handle the compressed data here
return true;
});
}
}
}