Ftp.hpp
1
2//
3// SFML - Simple and Fast Multimedia Library
4// Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)
5//
6// This software is provided 'as-is', without any express or implied warranty.
7// In no event will the authors be held liable for any damages arising from the use of this software.
8//
9// Permission is granted to anyone to use this software for any purpose,
10// including commercial applications, and to alter it and redistribute it freely,
11// subject to the following restrictions:
12//
13// 1. The origin of this software must not be misrepresented;
14// you must not claim that you wrote the original software.
15// If you use this software in a product, an acknowledgment
16// in the product documentation would be appreciated but is not required.
17//
18// 2. Altered source versions must be plainly marked as such,
19// and must not be misrepresented as being the original software.
20//
21// 3. This notice may not be removed or altered from any source distribution.
22//
24
25#ifndef SFML_FTP_HPP
26#define SFML_FTP_HPP
27
29// Headers
31#include <SFML/System/NonCopyable.hpp>
32#include <SFML/Network/SocketTCP.hpp>
33#include <string>
34#include <vector>
35
36
37namespace sf
38{
39class IPAddress;
40
47class SFML_API Ftp : NonCopyable
48{
49public :
50
60
66 class SFML_API Response
67 {
68 public :
69
74 enum Status
75 {
76 // 1xx: the requested action is being initiated,
77 // expect another reply before proceeding with a new command
82
83 // 2xx: the requested action has been successfully completed
84 Ok = 200,
88 FileStatus = 213,
90 SystemType = 215,
96 LoggedIn = 230,
99
100 // 3xx: the command has been accepted, but the requested action
101 // is dormant, pending receipt of further information
105
106 // 4xx: the command was not accepted and the requested action did not take place,
107 // but the error condition is temporary and the action may be requested again
114
115 // 5xx: the command was not accepted and
116 // the requested action did not take place
128
129 // 10xx: SFML custom codes
134 };
135
143 Response(Status Code = InvalidResponse, const std::string& Message = "");
144
152 bool IsOk() const;
153
160 Status GetStatus() const;
161
168 const std::string& GetMessage() const;
169
170 private :
171
173 // Member data
175 Status myStatus;
176 std::string myMessage;
177 };
178
182 class SFML_API DirectoryResponse : public Response
183 {
184 public :
185
193
200 const std::string& GetDirectory() const;
201
202 private :
203
205 // Member data
207 std::string myDirectory;
208 };
209
210
214 class SFML_API ListingResponse : public Response
215 {
216 public :
217
225 ListingResponse(Response Resp, const std::vector<char>& Data);
226
233 std::size_t GetCount() const;
234
243 const std::string& GetFilename(std::size_t Index) const;
244
245 private :
246
248 // Member data
250 std::vector<std::string> myFilenames;
251 };
252
253
258 ~Ftp();
259
270 Response Connect(const IPAddress& Server, unsigned short Port = 21, float Timeout = 0.f);
271
278 Response Login();
279
289 Response Login(const std::string& UserName, const std::string& Password);
290
298
306
314
324 ListingResponse GetDirectoryListing(const std::string& Directory = "");
325
334 Response ChangeDirectory(const std::string& Directory);
335
343
352 Response MakeDirectory(const std::string& Name);
353
362 Response DeleteDirectory(const std::string& Name);
363
373 Response RenameFile(const std::string& File, const std::string& NewName);
374
383 Response DeleteFile(const std::string& Name);
384
395 Response Download(const std::string& DistantFile, const std::string& DestPath, TransferMode Mode = Binary);
396
407 Response Upload(const std::string& LocalFile, const std::string& DestPath, TransferMode Mode = Binary);
408
409private :
410
420 Response SendCommand(const std::string& Command, const std::string& Parameter = "");
421
429 Response GetResponse();
430
435 class DataChannel;
436
437 friend class DataChannel;
438
440 // Member data
442 SocketTCP myCommandSocket;
443};
444
445} // namespace sf
446
447
448#endif // SFML_FTP_HPP
Specialization of FTP response returning a directory.
Definition Ftp.hpp:183
DirectoryResponse(Response Resp)
Default constructor.
Definition Ftp.cpp:123
const std::string & GetDirectory() const
Get the directory returned in the response.
Definition Ftp.cpp:139
Specialization of FTP response returning a filename lisiting.
Definition Ftp.hpp:215
std::size_t GetCount() const
Get the number of filenames in the listing.
Definition Ftp.cpp:168
const std::string & GetFilename(std::size_t Index) const
Get the Index-th filename in the directory.
Definition Ftp.cpp:177
ListingResponse(Response Resp, const std::vector< char > &Data)
Default constructor.
Definition Ftp.cpp:148
This class wraps a FTP response, which is basically :
Definition Ftp.hpp:67
Response(Status Code=InvalidResponse, const std::string &Message="")
Default constructor.
Definition Ftp.cpp:84
Status
Enumerate all the valid status codes returned in a FTP response.
Definition Ftp.hpp:75
@ NeedInformation
Requested file action pending further information.
Definition Ftp.hpp:104
@ FilenameNotAllowed
Requested action not taken, file name not allowed.
Definition Ftp.hpp:127
@ DirectoryOk
PATHNAME created.
Definition Ftp.hpp:98
@ RestartMarkerReply
Restart marker reply.
Definition Ftp.hpp:78
@ NeedAccountToStore
Need account for storing files.
Definition Ftp.hpp:123
@ ServiceReadySoon
Service ready in N minutes.
Definition Ftp.hpp:79
@ CommandNotImplemented
Command not implemented.
Definition Ftp.hpp:119
@ PointlessCommand
Command not implemented.
Definition Ftp.hpp:85
@ FileUnavailable
Requested action not taken, file unavailable.
Definition Ftp.hpp:124
@ ServiceUnavailable
Service not available, closing control connection.
Definition Ftp.hpp:108
@ EnteringPassiveMode
Entering passive mode.
Definition Ftp.hpp:95
@ ConnectionFailed
Connection with server failed.
Definition Ftp.hpp:131
@ LoggedIn
User logged in, proceed. Logged out if appropriate.
Definition Ftp.hpp:96
@ InvalidResponse
Response is not a valid FTP one.
Definition Ftp.hpp:130
@ InsufficientStorageSpace
Requested action not taken; insufficient storage space in system, file unavailable.
Definition Ftp.hpp:113
@ DataConnectionUnavailable
Can't open data connection.
Definition Ftp.hpp:109
@ CommandUnknown
Syntax error, command unrecognized.
Definition Ftp.hpp:117
@ SystemType
NAME system type, where NAME is an official system name from the list in the Assigned Numbers documen...
Definition Ftp.hpp:90
@ OpeningDataConnection
File status ok, about to open data connection.
Definition Ftp.hpp:81
@ TransferAborted
Connection closed, transfer aborted.
Definition Ftp.hpp:110
@ HelpMessage
Help message.
Definition Ftp.hpp:89
@ DirectoryStatus
Directory status.
Definition Ftp.hpp:87
@ ParameterNotImplemented
Command not implemented for that parameter.
Definition Ftp.hpp:121
@ NeedPassword
User name ok, need password.
Definition Ftp.hpp:102
@ SystemStatus
System status, or system help reply.
Definition Ftp.hpp:86
@ NeedAccountToLogIn
Need account for login.
Definition Ftp.hpp:103
@ Ok
Command ok.
Definition Ftp.hpp:84
@ ClosingConnection
Service closing control connection.
Definition Ftp.hpp:92
@ ClosingDataConnection
Closing data connection, requested file action successful.
Definition Ftp.hpp:94
@ BadCommandSequence
Bad sequence of commands.
Definition Ftp.hpp:120
@ ConnectionClosed
Connection with server closed.
Definition Ftp.hpp:132
@ PageTypeUnknown
Requested action aborted, page type unknown.
Definition Ftp.hpp:125
@ DataConnectionOpened
Data connection open, no transfer in progress.
Definition Ftp.hpp:93
@ LocalError
Requested action aborted, local error in processing.
Definition Ftp.hpp:112
@ ServiceReady
Service ready for new user.
Definition Ftp.hpp:91
@ FileStatus
File status.
Definition Ftp.hpp:88
@ InvalidFile
Invalid file to upload / download.
Definition Ftp.hpp:133
@ NotEnoughMemory
Requested file action aborted, exceeded storage allocation.
Definition Ftp.hpp:126
@ ParametersUnknown
Syntax error in parameters or arguments.
Definition Ftp.hpp:118
@ FileActionAborted
Requested file action not taken.
Definition Ftp.hpp:111
@ FileActionOk
Requested file action ok.
Definition Ftp.hpp:97
@ DataConnectionAlreadyOpened
Data connection already opened, transfer starting.
Definition Ftp.hpp:80
@ NotLoggedIn
Not logged in.
Definition Ftp.hpp:122
This class provides methods for manipulating the FTP protocol (described in RFC 959).
Definition Ftp.hpp:48
Response KeepAlive()
Send a null command just to prevent from being disconnected.
Definition Ftp.cpp:245
Response Download(const std::string &DistantFile, const std::string &DestPath, TransferMode Mode=Binary)
Download a file from the server.
Definition Ftp.cpp:349
TransferMode
Enumeration of transfer modes.
Definition Ftp.hpp:55
@ Binary
Binary mode (file is transfered as a sequence of bytes).
Definition Ftp.hpp:56
@ Ebcdic
Text mode using EBCDIC encoding.
Definition Ftp.hpp:58
@ Ascii
Text mode using ASCII encoding.
Definition Ftp.hpp:57
ListingResponse GetDirectoryListing(const std::string &Directory="")
Get the contents of the given directory (subdirectories and files).
Definition Ftp.cpp:264
Response MakeDirectory(const std::string &Name)
Create a new directory.
Definition Ftp.cpp:309
Response Upload(const std::string &LocalFile, const std::string &DestPath, TransferMode Mode=Binary)
Upload a file to the server.
Definition Ftp.cpp:396
Response ParentDirectory()
Go to the parent directory of the current one.
Definition Ftp.cpp:300
Response Disconnect()
Close the connection with FTP server.
Definition Ftp.cpp:231
Response ChangeDirectory(const std::string &Directory)
Change the current working directory.
Definition Ftp.cpp:291
Response RenameFile(const std::string &File, const std::string &NewName)
Rename a file.
Definition Ftp.cpp:327
DirectoryResponse GetWorkingDirectory()
Get the current working directory.
Definition Ftp.cpp:254
Response DeleteDirectory(const std::string &Name)
Remove an existing directory.
Definition Ftp.cpp:318
Response Login()
Log in using anonymous account.
Definition Ftp.cpp:209
Response DeleteFile(const std::string &Name)
Remove an existing file.
Definition Ftp.cpp:340
Response Connect(const IPAddress &Server, unsigned short Port=21, float Timeout=0.f)
Connect to the specified FTP server.
Definition Ftp.cpp:195
IPAddress provides easy manipulation of IP v4 addresses.
Definition IPAddress.hpp:43
SocketTCP wraps a socket using TCP protocol to send data safely (but a bit slower).
Definition SocketTCP.hpp:46
NonCopyable()
The default constructor won't be generated, so provide it.