Add read_url_list() functions
This commit is contained in:
parent
5f5518bb88
commit
86c6b1ca89
4 changed files with 37 additions and 12 deletions
|
@ -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"
|
||||
void dt_free_torrent_read (struct dt_torrent* t) {
|
||||
delete t;
|
||||
|
@ -84,18 +105,12 @@ int_fast32_t dt_read_file_count(struct dt_torrent* t) {
|
|||
|
||||
extern "C"
|
||||
dt_string_list dt_read_file_path(struct dt_torrent* t, size_t index) {
|
||||
try {
|
||||
auto vec = t->reader.read_file_path(index);
|
||||
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};
|
||||
}
|
||||
return read_string_list(t, &duck::TorrentRead::read_file_path, index);
|
||||
}
|
||||
|
||||
extern "C"
|
||||
dt_string_list dt_read_url_list(struct dt_torrent* t) {
|
||||
return read_string_list(t, &duck::TorrentRead::read_url_list);
|
||||
}
|
||||
|
||||
extern "C"
|
||||
|
|
|
@ -69,6 +69,7 @@ void dt_print_to_stdout(struct dt_torrent*);
|
|||
size_t dt_read_piece_length(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_url_list(struct dt_torrent*);
|
||||
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);
|
||||
int dt_read_is_private(struct dt_torrent*);
|
||||
|
|
|
@ -48,6 +48,7 @@ public:
|
|||
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::vector<std::string_view> read_url_list() 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;
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
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 {
|
||||
auto pieces = this->read_file_path(index);
|
||||
const std::size_t out_size = std::accumulate(pieces.cbegin(), pieces.cend(), 0u,
|
||||
|
|
Loading…
Add table
Reference in a new issue