Add read_url_list() functions

This commit is contained in:
King_DuckZ 2025-04-07 22:56:05 +01:00
parent 5f5518bb88
commit 86c6b1ca89
4 changed files with 37 additions and 12 deletions

View file

@ -48,6 +48,27 @@ struct dt_torrent* dt_make_reader_from_path (dt_string path) {
} }
} }
namespace {
//Just a convenience wrapper because I found I was copy-pasting code to fetch
//arrays from TorrentRead just because each C function would call into a
//different method. So here I take the method as a template parameter and no
//more copy-paste
dt_string_list read_string_list (struct dt_torrent* t, auto method, auto&&... args) {
try {
auto vec = (t->reader.*method)(std::forward<decltype(args)>(args)...);
t->vec_buff.clear();
t->vec_buff.reserve(vec.size());
for (std::string_view s : vec) {
t->vec_buff.emplace_back(s.data(), s.size());
}
return {t->vec_buff.data(), t->vec_buff.size()};
}
catch (...) {
return {nullptr, 0};
}
}
} //unnamed namespace
extern "C" extern "C"
void dt_free_torrent_read (struct dt_torrent* t) { void dt_free_torrent_read (struct dt_torrent* t) {
delete t; delete t;
@ -84,18 +105,12 @@ int_fast32_t dt_read_file_count(struct dt_torrent* t) {
extern "C" extern "C"
dt_string_list dt_read_file_path(struct dt_torrent* t, size_t index) { dt_string_list dt_read_file_path(struct dt_torrent* t, size_t index) {
try { return read_string_list(t, &duck::TorrentRead::read_file_path, index);
auto vec = t->reader.read_file_path(index); }
t->vec_buff.clear();
t->vec_buff.reserve(vec.size()); extern "C"
for (std::string_view s : vec) { dt_string_list dt_read_url_list(struct dt_torrent* t) {
t->vec_buff.emplace_back(s.data(), s.size()); return read_string_list(t, &duck::TorrentRead::read_url_list);
}
return {t->vec_buff.data(), t->vec_buff.size()};
}
catch (...) {
return {nullptr, 0};
}
} }
extern "C" extern "C"

View file

@ -69,6 +69,7 @@ void dt_print_to_stdout(struct dt_torrent*);
size_t dt_read_piece_length(struct dt_torrent*); size_t dt_read_piece_length(struct dt_torrent*);
int_fast32_t dt_read_file_count(struct dt_torrent*); int_fast32_t dt_read_file_count(struct dt_torrent*);
dt_string_list dt_read_file_path(struct dt_torrent*, size_t index); dt_string_list dt_read_file_path(struct dt_torrent*, size_t index);
dt_string_list dt_read_url_list(struct dt_torrent*);
dt_string dt_read_joint_file_path(struct dt_torrent*, size_t index, char sep); dt_string dt_read_joint_file_path(struct dt_torrent*, size_t index, char sep);
size_t dt_read_file_size(struct dt_torrent*, size_t index); size_t dt_read_file_size(struct dt_torrent*, size_t index);
int dt_read_is_private(struct dt_torrent*); int dt_read_is_private(struct dt_torrent*);

View file

@ -48,6 +48,7 @@ public:
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::vector<std::string_view> read_url_list() 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; bool read_is_private() const;

View file

@ -179,6 +179,14 @@ std::vector<std::string_view> TorrentRead::read_file_path(std::size_t index) con
return grab_all_strings_in_list(*path_variant); return grab_all_strings_in_list(*path_variant);
} }
std::vector<std::string_view> TorrentRead::read_url_list() const {
auto found = find_variant("/url-list", m_parsed_values);
if (not found)
return {};
return grab_all_strings_in_list(*found);
}
std::string TorrentRead::read_joint_file_path(std::size_t index, char sep) const { std::string TorrentRead::read_joint_file_path(std::size_t index, char sep) const {
auto pieces = this->read_file_path(index); auto pieces = this->read_file_path(index);
const std::size_t out_size = std::accumulate(pieces.cbegin(), pieces.cend(), 0u, const std::size_t out_size = std::accumulate(pieces.cbegin(), pieces.cend(), 0u,