-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
642a1e4
commit 938d568
Showing
4 changed files
with
183 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |