-
Notifications
You must be signed in to change notification settings - Fork 4
/
Utils.h
68 lines (52 loc) · 1.49 KB
/
Utils.h
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
#define ZLIB_WINAPI
#include <string>
#include "zlib.h"
// gzip constants
#define MOD_GZIP_ZLIB_WINDOWSIZE 15
#define MOD_GZIP_ZLIB_CFACTOR 9
#define MOD_GZIP_ZLIB_BSIZE 8096
class Utils {
public:
// Returns true if the stringToSearch contains the stringToFind
static bool contains(std::string stringToSearch, std::string stringToFind) {
if (stringToSearch.compare("") > 0) {
return ((int)stringToSearch.find(stringToFind) > (int)std::string::npos);
}else{
return false;
}
}
// gzips an input string with the supplied compression level
static std::string gzipString(const std::string& str, int compressionlevel = Z_BEST_COMPRESSION) {
z_stream zs;
memset(&zs, 0, sizeof(zs));
if (deflateInit2(&zs,
compressionlevel,
Z_DEFLATED,
MOD_GZIP_ZLIB_WINDOWSIZE + 16,
MOD_GZIP_ZLIB_CFACTOR,
Z_DEFAULT_STRATEGY) != Z_OK
) {
throw(std::runtime_error("deflateInit2 failed while compressing."));
}
zs.next_in = (Bytef*)str.data();
zs.avail_in = str.size();
int ret;
char* outbuffer = new char[32768];
std::string outstring;
do {
zs.next_out = reinterpret_cast<Bytef*>(outbuffer);
zs.avail_out = sizeof(outbuffer);
ret = deflate(&zs, Z_FINISH);
if (outstring.size() < zs.total_out) {
outstring.append(outbuffer,
zs.total_out - outstring.size());
}
} while (ret == Z_OK);
deflateEnd(&zs);
if (ret != Z_STREAM_END) {
printf("Error during gzip compression: %s", zs.msg);
}
delete[] outbuffer;
return outstring;
}
};