-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
76 lines (70 loc) · 2.11 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
#include <iostream>
#include <fstream>
#include <string>
int main(int argc, char *argv[]) {
std::string filepath, mode;
std::string help = "Usage: ./helper -m <mode> <filename> or ./helper <filename> -m <mode> \nMode types: adler32 or sum64";
if(argc == 2){
if (strcmp(argv[1], "-h") == 0){
std::cerr << help << std::endl;
return 0;
}else{
std::cerr << help << std::endl;
return 0;
}
}else if(argc == 4){
if(strcmp(argv[1], "-m") == 0){
if(strcmp(argv[2], "adler32") == 0 or strcmp(argv[2], "sum64") == 0){
mode = argv[2];
filepath = argv[3];
}else{
std::cerr << help << std::endl;
return 0;
}
}else if(strcmp(argv[2], "-m") == 0){
if(strcmp(argv[3], "adler32") == 0 or strcmp(argv[3], "sum64") == 0){
mode = argv[3];
filepath = argv[2];
}else{
std::cerr << help << std::endl;
return 0;
}
}else{
std::cerr << help << std::endl;
return 0;
}
}else{
std::cerr << help << std::endl;
return 0;
}
std::ifstream file;
file.open(filepath);
if(!(file.is_open())){
std::cerr << "FileNotFoundError" << std::endl;
std::cerr << help << std::endl;
return 1;
}
try{
if(mode == "adler32"){
uint32_t a = 1, b = 0;
while(!file.eof()){
char s;
file.read(&s, sizeof(char));
a = (a + s) % 65521;
b = (b + a) % 65521;
}
std::cout << ((b << 16)|a) << std::endl;
}else{
uint64_t contr_sum = 0;
uint64_t block;
while(!file.eof()){
file.read((char*)&block, 8);
contr_sum += block;
}
std::cout << contr_sum << std::endl;
}
}catch(std::exception const& e){
std::cerr << e.what() << std::endl;
return 1;
}
}