ftp.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: protocol/ftp.h
  3. // Purpose: interface of wxFTP
  4. // Author: wxWidgets team
  5. // Licence: wxWindows licence
  6. /////////////////////////////////////////////////////////////////////////////
  7. /**
  8. @class wxFTP
  9. wxFTP can be used to establish a connection to an FTP server and perform all the
  10. usual operations. Please consult the RFC 959 (http://www.w3.org/Protocols/rfc959/)
  11. for more details about the FTP protocol.
  12. wxFTP can thus be used to create a (basic) FTP @b client.
  13. To use a command which doesn't involve file transfer (i.e. directory oriented
  14. commands) you just need to call a corresponding member function or use the
  15. generic wxFTP::SendCommand() method.
  16. However to actually transfer files you just get or give a stream to or from this
  17. class and the actual data are read or written using the usual stream methods.
  18. Example of using wxFTP for file downloading:
  19. @code
  20. wxFTP ftp;
  21. // if you don't use these lines anonymous login will be used
  22. ftp.SetUser("user");
  23. ftp.SetPassword("password");
  24. if ( !ftp.Connect("ftp.wxwidgets.org") )
  25. {
  26. wxLogError("Couldn't connect");
  27. return;
  28. }
  29. ftp.ChDir("/pub/2.8.9");
  30. const char *filename = "wxWidgets-2.8.9.tar.bz2";
  31. int size = ftp.GetFileSize(filename);
  32. if ( size == -1 )
  33. {
  34. wxLogError("Couldn't get the file size for \"%s\"", filename);
  35. }
  36. wxInputStream *in = ftp.GetInputStream(filename);
  37. if ( !in )
  38. {
  39. wxLogError("Couldn't get the file");
  40. }
  41. else
  42. {
  43. char *data = new char[size];
  44. if ( !in->Read(data, size) )
  45. {
  46. wxLogError("Read error: %d", ftp.GetError());
  47. }
  48. else
  49. {
  50. // file data is in the buffer
  51. ...
  52. }
  53. delete [] data;
  54. delete in;
  55. }
  56. // gracefully close the connection to the server
  57. ftp.Close();
  58. @endcode
  59. To upload a file you would do (assuming the connection to the server was opened
  60. successfully):
  61. @code
  62. wxOutputStream *out = ftp.GetOutputStream("filename");
  63. if ( out )
  64. {
  65. out->Write(...); // your data
  66. delete out;
  67. }
  68. @endcode
  69. @library{wxnet}
  70. @category{net}
  71. @see wxSocketBase
  72. */
  73. class wxFTP : public wxProtocol
  74. {
  75. public:
  76. /**
  77. Transfer modes used by wxFTP.
  78. */
  79. enum TransferMode
  80. {
  81. NONE, //!< not set by user explicitly.
  82. ASCII,
  83. BINARY
  84. };
  85. /**
  86. Default constructor.
  87. */
  88. wxFTP();
  89. /**
  90. Destructor will close the connection if connected.
  91. */
  92. virtual ~wxFTP();
  93. //@{
  94. /**
  95. Connect to the FTP server to default port (21) of the specified @a host.
  96. */
  97. virtual bool Connect(const wxString& host);
  98. /**
  99. Connect to the FTP server to any port of the specified @a host.
  100. By default (@a port = 0), connection is made to default FTP port (21)
  101. of the specified @a host.
  102. @since 2.9.1
  103. */
  104. virtual bool Connect(const wxString& host, unsigned short port);
  105. //@}
  106. /**
  107. @name Functions for managing the FTP connection
  108. */
  109. //@{
  110. /**
  111. Aborts the download currently in process, returns @true if ok, @false
  112. if an error occurred.
  113. */
  114. virtual bool Abort();
  115. /**
  116. Gracefully closes the connection with the server.
  117. */
  118. virtual bool Close();
  119. /**
  120. Send the specified @a command to the FTP server. @a ret specifies
  121. the expected result.
  122. @return @true if the command has been sent successfully, else @false.
  123. */
  124. bool CheckCommand(const wxString& command, char ret);
  125. /**
  126. Returns the last command result, i.e. the full server reply for the last command.
  127. */
  128. const wxString& GetLastResult();
  129. /**
  130. Send the specified @a command to the FTP server and return the first
  131. character of the return code.
  132. */
  133. char SendCommand(const wxString& command);
  134. /**
  135. Sets the transfer mode to ASCII. It will be used for the next transfer.
  136. */
  137. bool SetAscii();
  138. /**
  139. Sets the transfer mode to binary. It will be used for the next transfer.
  140. */
  141. bool SetBinary();
  142. /**
  143. If @a pasv is @true, passive connection to the FTP server is used.
  144. This is the default as it works with practically all firewalls.
  145. If the server doesn't support passive mode, you may call this function
  146. with @false as argument to use an active connection.
  147. */
  148. void SetPassive(bool pasv);
  149. /**
  150. Sets the password to be sent to the FTP server to be allowed to log in.
  151. */
  152. virtual void SetPassword(const wxString& passwd);
  153. /**
  154. Sets the transfer mode to the specified one. It will be used for the next
  155. transfer.
  156. If this function is never called, binary transfer mode is used by default.
  157. */
  158. bool SetTransferMode(TransferMode mode);
  159. /**
  160. Sets the user name to be sent to the FTP server to be allowed to log in.
  161. */
  162. virtual void SetUser(const wxString& user);
  163. //@}
  164. /**
  165. @name Filesystem commands
  166. */
  167. //@{
  168. /**
  169. Change the current FTP working directory.
  170. Returns @true if successful.
  171. */
  172. bool ChDir(const wxString& dir);
  173. /**
  174. Create the specified directory in the current FTP working directory.
  175. Returns @true if successful.
  176. */
  177. bool MkDir(const wxString& dir);
  178. /**
  179. Returns the current FTP working directory.
  180. */
  181. wxString Pwd();
  182. /**
  183. Rename the specified @a src element to @e dst. Returns @true if successful.
  184. */
  185. bool Rename(const wxString& src, const wxString& dst);
  186. /**
  187. Remove the specified directory from the current FTP working directory.
  188. Returns @true if successful.
  189. */
  190. bool RmDir(const wxString& dir);
  191. /**
  192. Delete the file specified by @e path. Returns @true if successful.
  193. */
  194. bool RmFile(const wxString& path);
  195. /**
  196. Returns @true if the given remote file exists, @false otherwise.
  197. */
  198. bool FileExists(const wxString& filename);
  199. /**
  200. The GetList() function is quite low-level. It returns the list of the files in
  201. the current directory. The list can be filtered using the @a wildcard string.
  202. If @a wildcard is empty (default), it will return all files in directory.
  203. The form of the list can change from one peer system to another. For example,
  204. for a UNIX peer system, it will look like this:
  205. @verbatim
  206. -r--r--r-- 1 guilhem lavaux 12738 Jan 16 20:17 cmndata.cpp
  207. -r--r--r-- 1 guilhem lavaux 10866 Jan 24 16:41 config.cpp
  208. -rw-rw-rw- 1 guilhem lavaux 29967 Dec 21 19:17 cwlex_yy.c
  209. -rw-rw-rw- 1 guilhem lavaux 14342 Jan 22 19:51 cwy_tab.c
  210. -r--r--r-- 1 guilhem lavaux 13890 Jan 29 19:18 date.cpp
  211. -r--r--r-- 1 guilhem lavaux 3989 Feb 8 19:18 datstrm.cpp
  212. @endverbatim
  213. But on Windows system, it will look like this:
  214. @verbatim
  215. winamp~1 exe 520196 02-25-1999 19:28 winamp204.exe
  216. 1 file(s) 520 196 bytes
  217. @endverbatim
  218. @return @true if the file list was successfully retrieved, @false otherwise.
  219. @see GetFilesList()
  220. */
  221. bool GetDirList(wxArrayString& files,
  222. const wxString& wildcard = wxEmptyString);
  223. /**
  224. Returns the file size in bytes or -1 if the file doesn't exist or the size
  225. couldn't be determined.
  226. Notice that this size can be approximative size only and shouldn't be used
  227. for allocating the buffer in which the remote file is copied, for example.
  228. */
  229. int GetFileSize(const wxString& filename);
  230. /**
  231. This function returns the computer-parsable list of the files in the current
  232. directory (optionally only of the files matching the @e wildcard, all files
  233. by default).
  234. This list always has the same format and contains one full (including the
  235. directory path) file name per line.
  236. @return @true if the file list was successfully retrieved, @false otherwise.
  237. @see GetDirList()
  238. */
  239. bool GetFilesList(wxArrayString& files,
  240. const wxString& wildcard = wxEmptyString);
  241. //@}
  242. /**
  243. @name Download and upload functions
  244. */
  245. //@{
  246. /**
  247. Creates a new input stream on the specified path.
  248. You can use all but the seek functionality of wxStreamBase.
  249. wxStreamBase::Seek() isn't available on all streams. For example, HTTP or FTP
  250. streams do not deal with it. Other functions like wxStreamBase::Tell() are
  251. not available for this sort of stream, at present.
  252. You will be notified when the EOF is reached by an error.
  253. @return Returns @NULL if an error occurred (it could be a network failure
  254. or the fact that the file doesn't exist).
  255. */
  256. virtual wxInputStream* GetInputStream(const wxString& path);
  257. /**
  258. Initializes an output stream to the specified @a file.
  259. The returned stream has all but the seek functionality of wxStreams.
  260. When the user finishes writing data, he has to delete the stream to close it.
  261. @return An initialized write-only stream.
  262. Returns @NULL if an error occurred (it could be a network failure
  263. or the fact that the file doesn't exist).
  264. */
  265. virtual wxOutputStream* GetOutputStream(const wxString& file);
  266. //@}
  267. };