Back to index

libsfml  1.6+dfsg2
Ftp.cpp
Go to the documentation of this file.
00001 
00003 // Headers
00005 #include <SFML/Network.hpp>
00006 #include <fstream>
00007 #include <iostream>
00008 
00009 
00014 std::ostream& operator <<(std::ostream& Stream, const sf::Ftp::Response& Response)
00015 {
00016     return Stream << Response.GetStatus() << Response.GetMessage();
00017 }
00018 
00019 
00026 int main()
00027 {
00028     // Choose the server address
00029     sf::IPAddress Address;
00030     do
00031     {
00032         std::cout << "Enter the FTP server address : ";
00033         std::cin  >> Address;
00034     }
00035     while (!Address.IsValid());
00036 
00037     // Connect to the server
00038     sf::Ftp Server;
00039     sf::Ftp::Response ConnectResponse = Server.Connect(Address);
00040     std::cout << ConnectResponse << std::endl;
00041     if (!ConnectResponse.IsOk())
00042         return EXIT_FAILURE;
00043 
00044     // Ask for user name and password
00045     std::string UserName, Password;
00046     std::cout << "User name : ";
00047     std::cin  >> UserName;
00048     std::cout << "Password : ";
00049     std::cin  >> Password;
00050 
00051     // Login to the server
00052     sf::Ftp::Response LoginResponse = Server.Login(UserName, Password);
00053     std::cout << LoginResponse << std::endl;
00054     if (!LoginResponse.IsOk())
00055         return EXIT_FAILURE;
00056 
00057     // Main menu
00058     int Choice = 0;
00059     do
00060     {
00061         // Main FTP menu
00062         std::cout << std::endl;
00063         std::cout << "Choose an action:"                      << std::endl;
00064         std::cout << "1. Print working directory"             << std::endl;
00065         std::cout << "2. Print contents of working directory" << std::endl;
00066         std::cout << "3. Change directory"                    << std::endl;
00067         std::cout << "4. Create directory"                    << std::endl;
00068         std::cout << "5. Delete directory"                    << std::endl;
00069         std::cout << "6. Rename file"                         << std::endl;
00070         std::cout << "7. Remove file"                         << std::endl;
00071         std::cout << "8. Download file"                       << std::endl;
00072         std::cout << "9. Upload file"                         << std::endl;
00073         std::cout << "0. Disconnect"                          << std::endl;
00074         std::cout << std::endl;
00075 
00076         std::cout << "Your choice: ";
00077         std::cin  >> Choice;
00078         std::cout << std::endl;
00079 
00080         switch (Choice)
00081         {
00082             default :
00083             {
00084                 // Wrong choice
00085                 std::cout << "Invalid choice!" << std::endl;
00086                 std::cin.clear();
00087                 std::cin.ignore(10000, '\n');
00088                 break;
00089             }
00090 
00091             case 1 :
00092             {
00093                 // Print current server directory
00094                 sf::Ftp::DirectoryResponse Response = Server.GetWorkingDirectory();
00095                 std::cout << Response << std::endl;
00096                 std::cout << "Current directory is " << Response.GetDirectory() << std::endl;
00097                 break;
00098             }
00099 
00100             case 2 :
00101             {
00102                 // Print content of current server directory
00103                 sf::Ftp::ListingResponse Response = Server.GetDirectoryListing();
00104                 std::cout << Response << std::endl;
00105                 for (std::size_t i = 0; i < Response.GetCount(); ++i)
00106                     std::cout << Response.GetFilename(i) << std::endl;
00107                 break;
00108             }
00109 
00110             case 3 :
00111             {
00112                 // Change the current directory
00113                 std::string Directory;
00114                 std::cout << "Choose a directory: ";
00115                 std::cin  >> Directory;
00116                 std::cout << Server.ChangeDirectory(Directory) << std::endl;
00117                 break;
00118             }
00119 
00120             case 4 :
00121             {
00122                 // Create a new directory
00123                 std::string Directory;
00124                 std::cout << "Name of the directory to create: ";
00125                 std::cin  >> Directory;
00126                 std::cout << Server.MakeDirectory(Directory) << std::endl;
00127                 break;
00128             }
00129 
00130             case 5 :
00131             {
00132                 // Remove an existing directory
00133                 std::string Directory;
00134                 std::cout << "Name of the directory to remove: ";
00135                 std::cin  >> Directory;
00136                 std::cout << Server.DeleteDirectory(Directory) << std::endl;
00137                 break;
00138             }
00139 
00140             case 6 :
00141             {
00142                 // Rename a file
00143                 std::string SrcFilename, DstFilename;
00144                 std::cout << "Name of the file to rename: ";
00145                 std::cin  >> SrcFilename;
00146                 std::cout << "New name: ";
00147                 std::cin  >> DstFilename;
00148                 std::cout << Server.RenameFile(SrcFilename, DstFilename) << std::endl;
00149                 break;
00150             }
00151 
00152             case 7 :
00153             {
00154                 // Remove an existing directory
00155                 std::string Filename;
00156                 std::cout << "Name of the file to remove: ";
00157                 std::cin  >> Filename;
00158                 std::cout << Server.DeleteFile(Filename) << std::endl;
00159                 break;
00160             }
00161 
00162             case 8 :
00163             {
00164                 // Download a file from server
00165                 std::string Filename, Directory;
00166                 std::cout << "Path of the file to download (relative to current directory): ";
00167                 std::cin  >> Filename;
00168                 std::cout << "Directory to download the file to: ";
00169                 std::cin  >> Directory;
00170                 std::cout << Server.Download(Filename, Directory) << std::endl;
00171                 break;
00172             }
00173 
00174             case 9 :
00175             {
00176                 // Upload a file to server
00177                 std::string Filename, Directory;
00178                 std::cout << "Path of the file to upload (absolute or relative to working directory): ";
00179                 std::cin  >> Filename;
00180                 std::cout << "Directory to upload the file to (relative to current directory): ";
00181                 std::cin  >> Directory;
00182                 std::cout << Server.Upload(Filename, Directory) << std::endl;
00183                 break;
00184             }
00185 
00186             case 0 :
00187             {
00188                 // Disconnect
00189                 break;
00190             }
00191         }
00192 
00193     } while (Choice != 0);
00194 
00195     // Disconnect from the server
00196     std::cout << "Disconnecting from server..." << std::endl;
00197     std::cout << Server.Disconnect() << std::endl;
00198 
00199     // Wait until the user presses 'enter' key
00200     std::cout << "Press enter to exit..." << std::endl;
00201     std::cin.ignore(10000, '\n');
00202     std::cin.ignore(10000, '\n');
00203 
00204     return EXIT_SUCCESS;
00205 }