Refactor higher level code into TorrentRead class

This also addresses the issue with paths of more than 1 item
so all torrents should now work and adds some error reporting
through exceptions
This commit is contained in:
King_DuckZ 2025-04-07 01:01:52 +01:00
commit 50f33300be
10 changed files with 350 additions and 47 deletions

63
src/torrent_read.hpp Normal file
View file

@ -0,0 +1,63 @@
/* 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>
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_file_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;
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;
};
} //namespace duck