Add helper find function

Hash calculation is only correct if the relative path
found in the torrent file is accessible from pwd
This commit is contained in:
King_DuckZ 2025-04-02 21:23:26 +01:00
commit c1f8016f66

View file

@ -108,8 +108,8 @@ struct PrintVisitor {
};
struct FindStringVisitor : boost::static_visitor<std::string_view> {
FindStringVisitor (std::string path) {
auto split = dincore::split(path, '/', true, true);
FindStringVisitor (std::string_view search_path) {
auto split = dincore::split(search_path, '/', true, true);
search.reserve(split.size());
for (const auto& itm : split) {
search.emplace_back(itm.data(), itm.size());
@ -188,6 +188,16 @@ std::vector<std::array<std::uint32_t, 5>> collect_hashes (std::string_view hashe
}
return retval;
}
std::string_view find_item(std::string_view path, const std::vector<duck::TorrentValue>& values) {
FindStringVisitor visitor(path);
for (const auto& value : values) {
std::string_view found = boost::apply_visitor(visitor, value);
if (not found.empty())
return found;
}
return {};
}
} //unnamed namespace
int main(int argc, const char* argv[]) {
@ -208,9 +218,8 @@ int main(int argc, const char* argv[]) {
boost::apply_visitor(visitor, value);
}
if (values.size() == 1) {
FindStringVisitor visitor("/info/pieces");
auto source_hashes = boost::apply_visitor(visitor, values.front());
{
auto source_hashes = find_item("/info/pieces", values);
std::cout << "Got source_hashes with size " << source_hashes.size() << '\n';
auto hashes = collect_hashes(source_hashes);
std::cout << "Got " << hashes.size() << " hashes\n";
@ -222,12 +231,14 @@ int main(int argc, const char* argv[]) {
}
}
std::string_view file_name = find_item("/info/name", values);
std::cout << "Found file name \"" << file_name << "\"\n";
{
SHA1::MessageDigest hash160;
SHA1::FileSource file_source;
const char* const hash_path = argv[1];
file_source.open(hash_path);
file_source.open(std::string{file_name});
hash160 = SHA1::computeFromSource(file_source);
std::cout << hash160.toHexString() << " " << hash_path << '\n';
std::cout << hash160.toHexString() << " " << file_name << '\n';
}
}