-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsnappy-cli.cc
More file actions
58 lines (51 loc) · 1.48 KB
/
snappy-cli.cc
File metadata and controls
58 lines (51 loc) · 1.48 KB
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
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include "snappy.h"
inline bool ends_with(std::string const & value, std::string const & ending)
{
if (ending.size() > value.size()) {
return false;
}
return std::equal(ending.rbegin(), ending.rend(), value.rbegin());
}
int main(int argc, char** argv)
{
if (argc < 3) {
printf("Usage: snappy-cli <input file> <output file>\n");
return 0;
}
struct stat st;
if (stat(argv[1], &st) != 0) {
perror("stat");
return 1;
}
char* in = (char*)malloc(st.st_size);
if (in == NULL) {
perror("malloc");
return 1;
}
FILE* f1 = fopen(argv[1], "r");
if (f1 == NULL) {
perror("fopen");
return 1;
}
fread(in, st.st_size, sizeof(char), f1);
fclose(f1);
std::string output;
size_t compressed_len = 0;
if (ends_with(argv[1], ".snappy")) {
if(!snappy::Uncompress(in, st.st_size, &output)) {
printf("Cannot uncompress!\n");
return 1;
}
printf("Uncompressed %s size %d to %s size %d\n", argv[1], int(st.st_size), argv[2], int(output.size()));
} else {
size_t compressed_len = snappy::Compress(in, st.st_size, &output);
printf("Compressed %s size %d to %s size %d\n", argv[1], int(st.st_size), argv[2], int(compressed_len));
}
FILE* f2 = fopen(argv[2], "w");
fwrite(output.data(), sizeof(char), output.size(), f2);
fclose(f2);
return 0;
}