]>
git.saurik.com Git - wxWidgets.git/blob - interface/wx/protocol/ftp.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: protocol/ftp.h
3 // Purpose: interface of wxFTP
4 // Author: wxWidgets team
5 // Licence: wxWindows licence
6 /////////////////////////////////////////////////////////////////////////////
11 wxFTP can be used to establish a connection to an FTP server and perform all the
12 usual operations. Please consult the RFC 959 (http://www.w3.org/Protocols/rfc959/)
13 for more details about the FTP protocol.
15 wxFTP can thus be used to create a (basic) FTP @b client.
17 To use a command which doesn't involve file transfer (i.e. directory oriented
18 commands) you just need to call a corresponding member function or use the
19 generic wxFTP::SendCommand() method.
20 However to actually transfer files you just get or give a stream to or from this
21 class and the actual data are read or written using the usual stream methods.
23 Example of using wxFTP for file downloading:
28 // if you don't use these lines anonymous login will be used
30 ftp.SetPassword("password");
32 if ( !ftp.Connect("ftp.wxwidgets.org") )
34 wxLogError("Couldn't connect");
38 ftp.ChDir("/pub/2.8.9");
39 const char *filename = "wxWidgets-2.8.9.tar.bz2";
40 int size = ftp.GetFileSize(filename);
43 wxLogError("Couldn't get the file size for \"%s\"", filename);
46 wxInputStream *in = ftp.GetInputStream(filename);
49 wxLogError("Couldn't get the file");
53 char *data = new char[size];
54 if ( !in->Read(data, size) )
56 wxLogError("Read error: %d", ftp.GetError());
60 // file data is in the buffer
68 // gracefully close the connection to the server
72 To upload a file you would do (assuming the connection to the server was opened
76 wxOutputStream *out = ftp.GetOutputStream("filename");
79 out->Write(...); // your data
89 class wxFTP
: public wxProtocol
93 Transfer modes used by wxFTP.
97 NONE
, //!< not set by user explicitly.
108 Destructor will close the connection if connected.
116 Connect to the FTP server to default port (21) of the specified @a host.
118 virtual bool Connect(const wxString
& host
);
121 Connect to the FTP server to any port of the specified @a host.
122 By default (@a port = 0), connection is made to default FTP port (21)
123 of the specified @a host.
127 virtual bool Connect(const wxString
& host
, unsigned short port
);
131 @name Functions for managing the FTP connection
136 Aborts the download currently in process, returns @true if ok, @false
137 if an error occurred.
139 virtual bool Abort();
142 Gracefully closes the connection with the server.
144 virtual bool Close();
147 Send the specified @a command to the FTP server. @a ret specifies
150 @return @true if the command has been sent successfully, else @false.
152 bool CheckCommand(const wxString
& command
, char ret
);
155 Returns the last command result, i.e. the full server reply for the last command.
157 const wxString
& GetLastResult();
160 Send the specified @a command to the FTP server and return the first
161 character of the return code.
163 char SendCommand(const wxString
& command
);
166 Sets the transfer mode to ASCII. It will be used for the next transfer.
171 Sets the transfer mode to binary. It will be used for the next transfer.
176 If @a pasv is @true, passive connection to the FTP server is used.
178 This is the default as it works with practically all firewalls.
179 If the server doesn't support passive mode, you may call this function
180 with @false as argument to use an active connection.
182 void SetPassive(bool pasv
);
185 Sets the password to be sent to the FTP server to be allowed to log in.
187 virtual void SetPassword(const wxString
& passwd
);
190 Sets the transfer mode to the specified one. It will be used for the next
193 If this function is never called, binary transfer mode is used by default.
195 bool SetTransferMode(TransferMode mode
);
198 Sets the user name to be sent to the FTP server to be allowed to log in.
200 virtual void SetUser(const wxString
& user
);
207 @name Filesystem commands
212 Change the current FTP working directory.
213 Returns @true if successful.
215 bool ChDir(const wxString
& dir
);
218 Create the specified directory in the current FTP working directory.
219 Returns @true if successful.
221 bool MkDir(const wxString
& dir
);
224 Returns the current FTP working directory.
229 Rename the specified @a src element to @e dst. Returns @true if successful.
231 bool Rename(const wxString
& src
, const wxString
& dst
);
234 Remove the specified directory from the current FTP working directory.
235 Returns @true if successful.
237 bool RmDir(const wxString
& dir
);
240 Delete the file specified by @e path. Returns @true if successful.
242 bool RmFile(const wxString
& path
);
245 Returns @true if the given remote file exists, @false otherwise.
247 bool FileExists(const wxString
& filename
);
250 The GetList() function is quite low-level. It returns the list of the files in
251 the current directory. The list can be filtered using the @a wildcard string.
253 If @a wildcard is empty (default), it will return all files in directory.
254 The form of the list can change from one peer system to another. For example,
255 for a UNIX peer system, it will look like this:
258 -r--r--r-- 1 guilhem lavaux 12738 Jan 16 20:17 cmndata.cpp
259 -r--r--r-- 1 guilhem lavaux 10866 Jan 24 16:41 config.cpp
260 -rw-rw-rw- 1 guilhem lavaux 29967 Dec 21 19:17 cwlex_yy.c
261 -rw-rw-rw- 1 guilhem lavaux 14342 Jan 22 19:51 cwy_tab.c
262 -r--r--r-- 1 guilhem lavaux 13890 Jan 29 19:18 date.cpp
263 -r--r--r-- 1 guilhem lavaux 3989 Feb 8 19:18 datstrm.cpp
266 But on Windows system, it will look like this:
269 winamp~1 exe 520196 02-25-1999 19:28 winamp204.exe
270 1 file(s) 520 196 bytes
273 @return @true if the file list was successfully retrieved, @false otherwise.
277 bool GetDirList(wxArrayString
& files
,
278 const wxString
& wildcard
= wxEmptyString
);
281 Returns the file size in bytes or -1 if the file doesn't exist or the size
282 couldn't be determined.
284 Notice that this size can be approximative size only and shouldn't be used
285 for allocating the buffer in which the remote file is copied, for example.
287 int GetFileSize(const wxString
& filename
);
290 This function returns the computer-parsable list of the files in the current
291 directory (optionally only of the files matching the @e wildcard, all files
294 This list always has the same format and contains one full (including the
295 directory path) file name per line.
297 @return @true if the file list was successfully retrieved, @false otherwise.
301 bool GetFilesList(wxArrayString
& files
,
302 const wxString
& wildcard
= wxEmptyString
);
308 @name Download and upload functions
313 Creates a new input stream on the specified path.
315 You can use all but the seek functionality of wxStreamBase.
316 wxStreamBase::Seek() isn't available on all streams. For example, HTTP or FTP
317 streams do not deal with it. Other functions like wxStreamBase::Tell() are
318 not available for this sort of stream, at present.
320 You will be notified when the EOF is reached by an error.
322 @return Returns @NULL if an error occurred (it could be a network failure
323 or the fact that the file doesn't exist).
325 virtual wxInputStream
* GetInputStream(const wxString
& path
);
328 Initializes an output stream to the specified @a file.
330 The returned stream has all but the seek functionality of wxStreams.
331 When the user finishes writing data, he has to delete the stream to close it.
333 @return An initialized write-only stream.
334 Returns @NULL if an error occurred (it could be a network failure
335 or the fact that the file doesn't exist).
337 virtual wxOutputStream
* GetOutputStream(const wxString
& file
);