1
2////////////////////////////////////////////////////////////
3// Headers
4////////////////////////////////////////////////////////////
5#include <SFML/Network.hpp>
6#include <fstream>
7#include <iostream>
8
9
10////////////////////////////////////////////////////////////
11/// Print a FTP response into a standard output stream
12///
13////////////////////////////////////////////////////////////
14std::ostream& operator <<(std::ostream& stream, const sf::Ftp::Response& response)
15{
16 return stream << response.getStatus() << response.getMessage();
17}
18
19
20////////////////////////////////////////////////////////////
21/// Entry point of application
22///
23/// \return Application exit code
24///
25////////////////////////////////////////////////////////////
26int main()
27{
28 // Choose the server address
29 sf::IpAddress address;
30 do
31 {
32 std::cout << "Enter the FTP server address: ";
33 std::cin >> address;
34 }
35 while (address == sf::IpAddress::None);
36
37 // Connect to the server
38 sf::Ftp server;
39 sf::Ftp::Response connectResponse = server.connect(address);
40 std::cout << connectResponse << std::endl;
41 if (!connectResponse.isOk())
42 return EXIT_FAILURE;
43
44 // Ask for user name and password
45 std::string user, password;
46 std::cout << "User name: ";
47 std::cin >> user;
48 std::cout << "Password: ";
49 std::cin >> password;
50
51 // Login to the server
52 sf::Ftp::Response loginResponse = server.login(user, password);
53 std::cout << loginResponse << std::endl;
54 if (!loginResponse.isOk())
55 return EXIT_FAILURE;
56
57 // Main menu
58 int choice = 0;
59 do
60 {
61 // Main FTP menu
62 std::cout << std::endl;
63 std::cout << "Choose an action:" << std::endl;
64 std::cout << "1. Print working directory" << std::endl;
65 std::cout << "2. Print contents of working directory" << std::endl;
66 std::cout << "3. Change directory" << std::endl;
67 std::cout << "4. Create directory" << std::endl;
68 std::cout << "5. Delete directory" << std::endl;
69 std::cout << "6. Rename file" << std::endl;
70 std::cout << "7. Remove file" << std::endl;
71 std::cout << "8. Download file" << std::endl;
72 std::cout << "9. Upload file" << std::endl;
73 std::cout << "0. Disconnect" << std::endl;
74 std::cout << std::endl;
75
76 std::cout << "Your choice: ";
77 std::cin >> choice;
78 std::cout << std::endl;
79
80 switch (choice)
81 {
82 default:
83 {
84 // Wrong choice
85 std::cout << "Invalid choice!" << std::endl;
86 std::cin.clear();
87 std::cin.ignore(10000, '\n');
88 break;
89 }
90
91 case 1:
92 {
93 // Print the current server directory
94 sf::Ftp::DirectoryResponse response = server.getWorkingDirectory();
95 std::cout << response << std::endl;
96 std::cout << "Current directory is " << response.getDirectory() << std::endl;
97 break;
98 }
99
100 case 2:
101 {
102 // Print the contents of the current server directory
103 sf::Ftp::ListingResponse response = server.getDirectoryListing();
104 std::cout << response << std::endl;
105 const std::vector<std::string>& names = response.getListing();
106 for (std::vector<std::string>::const_iterator it = names.begin(); it != names.end(); ++it)
107 std::cout << *it << std::endl;
108 break;
109 }
110
111 case 3:
112 {
113 // Change the current directory
114 std::string directory;
115 std::cout << "Choose a directory: ";
116 std::cin >> directory;
117 std::cout << server.changeDirectory(directory) << std::endl;
118 break;
119 }
120
121 case 4:
122 {
123 // Create a new directory
124 std::string directory;
125 std::cout << "Name of the directory to create: ";
126 std::cin >> directory;
127 std::cout << server.createDirectory(directory) << std::endl;
128 break;
129 }
130
131 case 5:
132 {
133 // Remove an existing directory
134 std::string directory;
135 std::cout << "Name of the directory to remove: ";
136 std::cin >> directory;
137 std::cout << server.deleteDirectory(directory) << std::endl;
138 break;
139 }
140
141 case 6:
142 {
143 // Rename a file
144 std::string source, destination;
145 std::cout << "Name of the file to rename: ";
146 std::cin >> source;
147 std::cout << "New name: ";
148 std::cin >> destination;
149 std::cout << server.renameFile(source, destination) << std::endl;
150 break;
151 }
152
153 case 7:
154 {
155 // Remove an existing directory
156 std::string filename;
157 std::cout << "Name of the file to remove: ";
158 std::cin >> filename;
159 std::cout << server.deleteFile(filename) << std::endl;
160 break;
161 }
162
163 case 8:
164 {
165 // Download a file from server
166 std::string filename, directory;
167 std::cout << "Filename of the file to download (relative to current directory): ";
168 std::cin >> filename;
169 std::cout << "Directory to download the file to: ";
170 std::cin >> directory;
171 std::cout << server.download(filename, directory) << std::endl;
172 break;
173 }
174
175 case 9:
176 {
177 // Upload a file to server
178 std::string filename, directory;
179 std::cout << "Path of the file to upload (absolute or relative to working directory): ";
180 std::cin >> filename;
181 std::cout << "Directory to upload the file to (relative to current directory): ";
182 std::cin >> directory;
183 std::cout << server.upload(filename, directory) << std::endl;
184 break;
185 }
186
187 case 0:
188 {
189 // Disconnect
190 break;
191 }
192 }
193
194 } while (choice != 0);
195
196 // Disconnect from the server
197 std::cout << "Disconnecting from server..." << std::endl;
198 std::cout << server.disconnect() << std::endl;
199
200 // Wait until the user presses 'enter' key
201 std::cout << "Press enter to exit..." << std::endl;
202 std::cin.ignore(10000, '\n');
203 std::cin.ignore(10000, '\n');
204
205 return EXIT_SUCCESS;
206}
207