diff --git a/src/ducktorrent/c_api_torrent_read.cpp b/src/ducktorrent/c_api_torrent_read.cpp index 1eaaf2b..087c191 100644 --- a/src/ducktorrent/c_api_torrent_read.cpp +++ b/src/ducktorrent/c_api_torrent_read.cpp @@ -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(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" diff --git a/src/ducktorrent/include/ducktorrent/torrent_read.h b/src/ducktorrent/include/ducktorrent/torrent_read.h index c60d9d0..d17d867 100644 --- a/src/ducktorrent/include/ducktorrent/torrent_read.h +++ b/src/ducktorrent/include/ducktorrent/torrent_read.h @@ -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*); diff --git a/src/ducktorrent/include/ducktorrent/torrent_read.hpp b/src/ducktorrent/include/ducktorrent/torrent_read.hpp index 9e9e151..384a91a 100644 --- a/src/ducktorrent/include/ducktorrent/torrent_read.hpp +++ b/src/ducktorrent/include/ducktorrent/torrent_read.hpp @@ -48,6 +48,7 @@ public: std::size_t read_piece_length() const; std::int_fast32_t read_file_count() const; std::vector read_file_path(std::size_t index) const; + std::vector 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; diff --git a/src/ducktorrent/torrent_read.cpp b/src/ducktorrent/torrent_read.cpp index 4894795..e7d83ef 100644 --- a/src/ducktorrent/torrent_read.cpp +++ b/src/ducktorrent/torrent_read.cpp @@ -179,6 +179,14 @@ std::vector TorrentRead::read_file_path(std::size_t index) con return grab_all_strings_in_list(*path_variant); } +std::vector 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,