From be8788bfd52404355bc13386a85626addd3f72c3 Mon Sep 17 00:00:00 2001 From: deniskovalchuk Date: Sat, 23 Sep 2023 02:23:12 +0300 Subject: [PATCH] feat: provide parsed file list --- include/ftp/file_list_reply.hpp | 12 +++++-- src/file_list_reply.cpp | 30 +++++++++++++--- test/client.cpp | 61 +++++++++------------------------ 3 files changed, 51 insertions(+), 52 deletions(-) diff --git a/include/ftp/file_list_reply.hpp b/include/ftp/file_list_reply.hpp index 515768d..f6a5bf5 100644 --- a/include/ftp/file_list_reply.hpp +++ b/include/ftp/file_list_reply.hpp @@ -27,6 +27,7 @@ #include #include +#include namespace ftp { @@ -36,12 +37,17 @@ class file_list_reply : public replies public: file_list_reply(); - file_list_reply(const replies & replies, const std::string & file_list); + file_list_reply(const replies & replies, const std::string & file_list_str); - [[nodiscard]] const std::string & get_file_list() const; + [[nodiscard]] const std::string & get_file_list_str() const; + + [[nodiscard]] const std::vector & get_file_list() const; private: - std::string file_list_; + static std::vector parse_file_list(const std::string & file_list_str); + + std::string file_list_str_; + std::vector file_list_; }; } // namespace ftp diff --git a/src/file_list_reply.cpp b/src/file_list_reply.cpp index ce4cffd..17c6735 100644 --- a/src/file_list_reply.cpp +++ b/src/file_list_reply.cpp @@ -23,6 +23,7 @@ */ #include +#include namespace ftp { @@ -31,14 +32,35 @@ file_list_reply::file_list_reply() : ftp::replies() {} -file_list_reply::file_list_reply(const replies & replies, const std::string & file_list) +file_list_reply::file_list_reply(const replies & replies, const std::string & file_list_str) : ftp::replies(replies), - file_list_(file_list) -{} + file_list_str_(file_list_str) +{ + file_list_ = parse_file_list(file_list_str); +} + +const std::string & file_list_reply::get_file_list_str() const +{ + return file_list_str_; +} -const std::string & file_list_reply::get_file_list() const +const std::vector & file_list_reply::get_file_list() const { return file_list_; } +std::vector file_list_reply::parse_file_list(const std::string & file_list_str) +{ + std::vector file_list; + + std::istringstream iss(file_list_str); + std::string line; + while (std::getline(iss, line)) + { + file_list.push_back(line); + } + + return file_list; +} + } // namespace ftp diff --git a/test/client.cpp b/test/client.cpp index f6260b3..86935e6 100644 --- a/test/client.cpp +++ b/test/client.cpp @@ -23,6 +23,7 @@ */ #include +#include #include #include #include @@ -37,6 +38,7 @@ namespace { using namespace ftp::test; +using testing::ElementsAre; class test_observer : public ftp::observer { @@ -68,37 +70,6 @@ class test_observer : public ftp::observer std::string status_strings_; }; -class test_file_list_observer : public ftp::observer -{ -public: - [[nodiscard]] const std::string & get_file_list() - { - return file_list_; - } - - [[nodiscard]] std::size_t get_file_count() - { - return file_count_; - } - -private: - void on_file_list(std::string_view file_list) override - { - file_list_ = file_list; - file_count_ = 0; - - std::istringstream iss(file_list_); - std::string file; - while (std::getline(iss, file)) - { - file_count_++; - } - } - - std::string file_list_; - std::size_t file_count_; -}; - class test_cancel_callback : public ftp::transfer_callback { public: @@ -795,36 +766,34 @@ TEST_F(client, append_file) TEST_F(client, get_file_list_only_names) { - std::shared_ptr observer = std::make_shared(); ftp::client client; - client.add_observer(observer); - check_reply(client.connect("localhost", 2121), "220 FTP server is ready."); check_reply(client.login("user", "password"), CRLF("331 Username ok, send password.", "230 Login successful.", "200 Type set to: Binary.")); - check_last_reply(client.get_file_list(".", true), "226 Transfer complete."); - ASSERT_TRUE(observer->get_file_list().empty()); + client.set_transfer_type(ftp::transfer_type::ascii); + + ftp::file_list_reply reply = client.get_file_list(".", true); + check_last_reply(reply, "226 Transfer complete."); + ASSERT_THAT(reply.get_file_list(), ElementsAre()); check_reply(client.create_directory("dir1"), R"(257 "/dir1" directory created.)"); check_reply(client.create_directory("dir2"), R"(257 "/dir2" directory created.)"); - check_last_reply(client.get_file_list(".", true), "226 Transfer complete."); - ASSERT_EQ(CRLF("dir1", "dir2", ""), observer->get_file_list()); + reply = client.get_file_list(".", true); + check_last_reply(reply, "226 Transfer complete."); + ASSERT_THAT(reply.get_file_list(), ElementsAre("dir1", "dir2")); check_reply(client.disconnect(), "221 Goodbye."); } TEST_F(client, upload_unique_file) { - std::shared_ptr observer = std::make_shared(); ftp::client client; - client.add_observer(observer); - check_reply(client.connect("localhost", 2121), "220 FTP server is ready."); check_reply(client.login("user", "password"), CRLF("331 Username ok, send password.", @@ -836,16 +805,18 @@ TEST_F(client, upload_unique_file) check_last_reply(client.upload_file(ftp::istream_adapter(iss), "file", true), "226 Transfer complete."); } - check_last_reply(client.get_file_list(".", true), "226 Transfer complete."); - ASSERT_EQ(1, observer->get_file_count()); + ftp::file_list_reply reply = client.get_file_list(".", true); + check_last_reply(reply, "226 Transfer complete."); + ASSERT_EQ(1, reply.get_file_list().size()); { std::istringstream iss("content"); check_last_reply(client.upload_file(ftp::istream_adapter(iss), "file", true), "226 Transfer complete."); } - check_last_reply(client.get_file_list(".", true), "226 Transfer complete."); - ASSERT_EQ(2, observer->get_file_count()); + reply = client.get_file_list(".", true); + check_last_reply(reply, "226 Transfer complete."); + ASSERT_EQ(2, reply.get_file_list().size()); check_reply(client.disconnect(), "221 Goodbye."); }