ducktorrent/src/torrent_read.hpp
King_DuckZ d04b4d8274 Add a read_announce_list() to TorrentRead
announce-list is very annoyingly a list of lists. I don't
have any useful primitive to manage that case in TorrentRead,
so for now I've done a not-so-elegant implementation where I
step around the visitor, use my knowledge of the variant and
access the outer list directly through boost::get(). I'm not
necessarily convinced that this is bad since on the other hand
the solution consistent with the rest of the implementation
would involve searching for "/[[x]]/[[y]]" which has its own
overhead. I mean, variant contains a vector, I can access it
normally. Visitor is a convenience thing, I don't see why
parser's client code should be obliged to pretend the variant
cannot be accessed directly.
2025-04-07 02:39:35 +01:00

74 lines
2.5 KiB
C++

/* Copyright 2025, Michele "King_DuckZ" Santullo
* This file is part of ducktorrent.
*
* Ducktorrent is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Ducktorrent is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ducktorrent. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
//This file contains higher level helpers that build on top of the parser and
//the visitors in parser.hpp and visitors/find_t_visitor.hpp. It is just here
//for convenience and it is not strictly required to read and parse bencoded
//data.
#include <vector>
#include <string>
#include <string_view>
#include <filesystem>
#include <ostream>
#include <cstddef>
#include <cstdint>
#include <array>
namespace duck {
struct TorrentValue;
class TorrentRead {
public:
TorrentRead (std::string_view torrent_path, std::filesystem::path workdir);
TorrentRead (const TorrentRead&) = delete;
~TorrentRead() noexcept;
void print (std::ostream& out) const;
std::size_t raw_data_size() const;
const std::vector<TorrentValue>& parsed_values() const;
std::string_view read_name() const;
std::size_t read_piece_length() const;
std::int_fast32_t read_file_count() const;
std::vector<std::string_view> read_file_path(std::size_t index) const;
std::string read_joint_file_path(std::size_t index, char sep) const;
std::size_t read_file_size(std::size_t index) const;
bool read_is_private() const;
std::string_view read_comment() const;
std::string_view read_hashes() const;
std::string_view read_created_by() const;
std::string_view read_announce() const;
std::vector<std::vector<std::string_view>> read_announce_list() const;
std::time_t read_creation_date() const;
private:
const TorrentValue& cached_info_files() const;
std::filesystem::path m_workdir;
std::filesystem::path m_torrent_path;
std::string m_raw_torrent;
std::vector<TorrentValue> m_parsed_values;
mutable const TorrentValue* m_cached_info_files;
};
std::vector<std::array<std::uint32_t, 5>> group_torrent_hashes (std::string_view hashes);
std::vector<std::array<std::uint32_t, 5>> group_torrent_hashes (const TorrentRead& torrent);
} //namespace duck