-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtag_export.cpp
67 lines (53 loc) · 1.74 KB
/
tag_export.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
#include "tag_export.h"
#include <fstream>
#include <iostream>
#include <string>
#include "tag_file.h"
#include "../libs/json.hpp"
using json = nlohmann::json;
int TagExport(const std::string& src, const std::string& dst, bool rewrite) {
std::ifstream sf(src, std::ios_base::in | std::ios_base::binary);
if (!sf) {
std::cerr << "Can't open source file '" << src << "'" << std::endl;
return 1;
}
auto tf = GetReader(sf);
if (!tf) { return 1; }
if (!rewrite) {
std::ifstream def(dst, std::ios_base::in);
if (def) {
std::cerr << "Target file '" << dst <<
"' is existed now. For overwriting use option '-f'" << std::endl;
return 1;
}
}
// Json stub
json j = { {"settings", nullptr}, {"tags", nullptr}, {"files", nullptr} };
// TagVfs settings
j["settings"]["tagrecordsize"] = tf->GetTagRecordSize();
j["settings"]["maxtags"] = tf->GetTagMaxAmount();
j["settings"]["fileblocksize"] = tf->GetFileBlockSize();
tf->EnumTags([&j](uint16_t, const std::string& name){
j["tags"].push_back({{"name", name}});
});
tf->EnumFiles([&tf, &j](const std::string& name, const std::string& target,
const uint16_t* tags, size_t tags_amount){
json tj;
for (size_t tc = 0; tc < tags_amount; ++tc) {
std::string tname;
if (!tf->GetTagInfo(tags[tc], tname)) {
std::cerr << "Can't tag with index " << tc << std::endl;
continue;
}
tj.push_back(tname);
}
j["files"].push_back({{"name", name}, {"target", target}, {"tags", tj}});
});
std::ofstream df(dst, std::ios_base::out | std::ios_base::trunc);
if (!df) {
std::cerr << "Can't create/open file '" << dst << "' for writing" << std::endl;
return 1;
}
df << std::setw(2) << j;
return 0;
}