Add some more read_ functions to TorrentRead

This commit is contained in:
King_DuckZ 2025-04-07 01:09:40 +01:00
parent 50f33300be
commit 5aa42b5e43
3 changed files with 36 additions and 3 deletions

View file

@ -44,6 +44,13 @@ int main(int argc, const char* argv[]) {
duck::TorrentRead torrent(argv[1], ""); duck::TorrentRead torrent(argv[1], "");
std::cout << "Loaded file into string of size " << torrent.raw_data_size() << '\n'; std::cout << "Loaded file into string of size " << torrent.raw_data_size() << '\n';
std::cout << "Torrent name: " << torrent.read_name() << '\n';
std::cout << "Created by: " << torrent.read_created_by() << '\n';
std::cout << "Creation date: " << torrent.read_creation_date() << '\n';
std::cout << "Comment: " << torrent.read_comment() << '\n';
std::cout << "Announce: " << torrent.read_announce() << '\n';
std::cout << "Is private: " << std::boolalpha << torrent.read_is_private() << '\n';
torrent.print(std::cout); torrent.print(std::cout);
const auto& values = torrent.parsed_values(); const auto& values = torrent.parsed_values();
@ -58,7 +65,7 @@ int main(int argc, const char* argv[]) {
const std::size_t piece_length = torrent.read_piece_length(); const std::size_t piece_length = torrent.read_piece_length();
if (0 == file_count) { if (0 == file_count) {
std::string file_name{torrent.read_file_name()}; std::string file_name{torrent.read_name()};
std::cout << "Found file name \"" << file_name << "\"\n"; std::cout << "Found file name \"" << file_name << "\"\n";
std::ifstream istream(file_name, std::ios::in|std::ios::binary); std::ifstream istream(file_name, std::ios::in|std::ios::binary);

View file

@ -115,7 +115,7 @@ const std::vector<TorrentValue>& TorrentRead::parsed_values() const {
return m_parsed_values; return m_parsed_values;
} }
std::string_view TorrentRead::read_file_name() const { std::string_view TorrentRead::read_name() const {
return find_string("/info/name", m_parsed_values); return find_string("/info/name", m_parsed_values);
} }
@ -203,6 +203,27 @@ std::size_t TorrentRead::read_file_size (std::size_t index) const {
); );
} }
bool TorrentRead::read_is_private() const {
const auto val = find_int<unsigned int>("/info/private", m_parsed_values);
return (val ? true : false);
}
std::string_view TorrentRead::read_comment() const {
return find_string("/comment", m_parsed_values);
}
std::string_view TorrentRead::read_created_by() const {
return find_string("/created by", m_parsed_values);
}
std::string_view TorrentRead::read_announce() const {
return find_string("/announce", m_parsed_values);
}
std::time_t TorrentRead::read_creation_date() const {
return find_int<std::time_t>("/creation date", m_parsed_values);
}
const TorrentValue& TorrentRead::cached_info_files() const { const TorrentValue& TorrentRead::cached_info_files() const {
constexpr char info_files[] = "/info/files"; constexpr char info_files[] = "/info/files";
if (not m_cached_info_files) { if (not m_cached_info_files) {

View file

@ -43,12 +43,17 @@ public:
std::size_t raw_data_size() const; std::size_t raw_data_size() const;
const std::vector<TorrentValue>& parsed_values() const; const std::vector<TorrentValue>& parsed_values() const;
std::string_view read_file_name() const; std::string_view read_name() const;
std::size_t read_piece_length() const; std::size_t read_piece_length() const;
std::int_fast32_t read_file_count() const; std::int_fast32_t read_file_count() const;
std::vector<std::string_view> read_file_path(std::size_t index) 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::string read_joint_file_path(std::size_t index, char sep) const;
std::size_t read_file_size(std::size_t index) 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_created_by() const;
std::string_view read_announce() const;
std::time_t read_creation_date() const;
private: private:
const TorrentValue& cached_info_files() const; const TorrentValue& cached_info_files() const;