-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
65 lines (55 loc) · 1.73 KB
/
Copy pathmain.cpp
File metadata and controls
65 lines (55 loc) · 1.73 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
59
60
61
62
63
64
65
#include "utils/CommonUtils.h"
#include "utils/NCMParser.h"
#include <fstream>
#include <iostream>
int main(int argc, char *argv[]) {
if (argc < 2) {
std::cout << "Usage: ncmdump [ncm file] [-o [output path]]" << std::endl;
return 0;
}
std::string in_path;
std::string out_path;
for (int i = 1; i < argc; ++i) {
if (strcmp(argv[i], "-o") == 0 && i + 1 < argc) {
out_path = argv[i + 1];
i++;
continue;
}
in_path = argv[i];
}
if (in_path.size() < 4 || CommonUtils::ToLower(in_path.substr(in_path.size() - 4)) != ".ncm") {
std::cout << "Input file suffix should be \"ncm\"." << std::endl;
return -1;
}
if (out_path.empty()) {
out_path = in_path.substr(0, in_path.size() - 4);
} else {
auto idx = out_path.find_last_of('.');
if (idx != std::string::npos) {
out_path = out_path.substr(0, idx);
}
}
std::fstream f;
f.open(in_path, std::ios::in | std::ios::binary);
if (!f.is_open()) {
std::cout << "Read file failed." << std::endl;
return -1;
}
NCMParser parser;
parser.data = std::string(std::istreambuf_iterator<char>(f), std::istreambuf_iterator<char>());
f.close();
if (!parser.Decode()) {
std::cout << "Decode file failed." << std::endl;
return -1;
}
out_path += "." + parser.format;
f.open(out_path, std::ios::out | std::ios::binary);
if (!f.is_open()) {
std::cout << "Write file failed." << std::endl;
return -1;
}
f.write(parser.data.data(), parser.data.size());
f.close();
std::cout << "Decode to " << parser.format << " file finished." << std::endl;
return 0;
}