From c1f8016f6609477649e381423a641e8bd38432c8 Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Wed, 2 Apr 2025 21:23:26 +0100 Subject: [PATCH] Add helper find function Hash calculation is only correct if the relative path found in the torrent file is accessible from pwd --- src/main.cpp | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index e633060..7595e48 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -108,8 +108,8 @@ struct PrintVisitor { }; struct FindStringVisitor : boost::static_visitor { - 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> collect_hashes (std::string_view hashe } return retval; } + +std::string_view find_item(std::string_view path, const std::vector& 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'; } }