Skip to content

Commit

Permalink
Add an example
Browse files Browse the repository at this point in the history
  • Loading branch information
deniskovalchuk committed Sep 16, 2023
1 parent 642a1e4 commit 938d568
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 0 deletions.
13 changes: 13 additions & 0 deletions example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
cmake_minimum_required(VERSION 3.14)
project(ftp-examples LANGUAGES CXX)

function(create_example name)
add_executable(${name}
${name}.cpp
reply_handlers.cpp
reply_handlers.hpp)

target_link_libraries(${name} PRIVATE ftp::ftp)
endfunction()

create_example(download_file)
60 changes: 60 additions & 0 deletions example/download_file.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* MIT License
*
* Copyright (c) 2023 Denis Kovalchuk
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#include <iostream>
#include <fstream>
#include <ftp/client.hpp>
#include <ftp/stream/ostream_adapter.hpp>
#include "reply_handlers.hpp"

int main(int argc, char *argv[])
{
try
{
constexpr std::string_view filename = "favicon.ico";

std::ofstream ofs(filename, std::ofstream::binary | std::ofstream::trunc);

if (!ofs)
{
std::cerr << "Cannot create a file." << std::endl;
return EXIT_FAILURE;
}

ftp::client client;

handle_reply(client.connect("ftp.freebsd.org", 21, "anonymous"));

handle_reply(client.download_file(ftp::ostream_adapter(ofs), filename));

handle_reply(client.disconnect());

return EXIT_SUCCESS;
}
catch (const std::exception & ex)
{
std::cerr << ex.what() << std::endl;
return EXIT_FAILURE;
}
}
73 changes: 73 additions & 0 deletions example/reply_handlers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* MIT License
*
* Copyright (c) 2023 Denis Kovalchuk
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#include <string_view>
#include <iostream>
#include <ftp/ftp_exception.hpp>
#include "reply_handlers.hpp"

void handle_reply(const ftp::replies & replies)
{
const std::string & status_string = replies.get_status_string();

if (replies.is_positive())
{
std::cout << status_string << std::endl;
}
else
{
throw ftp::ftp_exception(status_string);
}
}

void handle_reply(const ftp::reply & reply)
{
const std::string & status_string = reply.get_status_string();

if (reply.is_positive())
{
std::cout << status_string << std::endl;
}
else
{
throw ftp::ftp_exception(status_string);
}
}

void handle_reply(const std::optional<ftp::reply> & reply)
{
if (reply)
{
const std::string & status_string = reply->get_status_string();

if (reply->is_positive())
{
std::cout << status_string << std::endl;
}
else
{
throw ftp::ftp_exception(status_string);
}
}
}
37 changes: 37 additions & 0 deletions example/reply_handlers.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* MIT License
*
* Copyright (c) 2023 Denis Kovalchuk
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

#ifndef LIBFTP_EXAMPLE_REPLY_HANDLERS_HPP
#define LIBFTP_EXAMPLE_REPLY_HANDLERS_HPP

#include <ftp/replies.hpp>
#include <ftp/reply.hpp>

void handle_reply(const ftp::replies & replies);

void handle_reply(const ftp::reply & reply);

void handle_reply(const std::optional<ftp::reply> & reply);

#endif //LIBFTP_EXAMPLE_REPLY_HANDLERS_HPP

0 comments on commit 938d568

Please sign in to comment.