wxftpwxftp can be used to establish a connection to an ftp server and perform all the usual operations. please consult the rfc 959 for more details about the ftp protocol. to use a commands which doesn't involve file transfer (i.e. directory oriented commands) you just need to call a corresponding member function or use the generic sendcommand method. however to actually transfer files you just get or give a stream to or from this class and the actual data are read or written using the usual stream methods. example of using wxftp for file downloading:
wxftp ftp; // if you don't use these lines anonymous login will be used ftp.setuser("user"); ftp.setpassword("password"); if ( !ftp.connect("ftp.wxwindows.org") ) { wxlogerror("couldn't connect"); return; } ftp.chdir("/pub"); wxinputstream *in = ftp.getinputstream("wxwidgets-4.2.0.tar.gz"); if ( !in ) { wxlogerror("coudln't get file"); } else { size_t size = in->getsize(); char *data = new char[size]; if ( !in->read(data, size) ) { wxlogerror("read error"); } else { // file data is in the buffer ... } delete [] data; delete in; }to upload a file you would do (assuming the connection to the server was opened successfully):
wxoutputstream *out = ftp.getoutputstream("filename"); if ( out ) { out->write(...); // your data delete out; }constants wxftp defines constants corresponding to the two supported transfer modes:
enum transfermode { ascii, binary };derived from include files <wx/protocol/ftp.h> see also members
wxftp::wxftp
wxftp::wxftpwxftp() default constructor.
wxftp::~wxftp~wxftp() destructor will close the connection if connected.
wxftp::abortbool abort() aborts the download currently in process, returns true if ok, false if an error occurred.
wxftp::checkcommandbool checkcommand(const wxstring& command, char ret) send the specified command to the ftp server. ret specifies the expected result. return value true if the command has been sent successfully, else false.
wxftp::sendcommandchar sendcommand(const wxstring& command) send the specified command to the ftp server and return the first character of the return code.
wxftp::getlastresultconst wxstring& getlastresult() returns the last command result, i.e. the full server reply for the last command.
wxftp::chdirbool chdir(const wxstring& dir) change the current ftp working directory. returns true if successful.
wxftp::mkdirbool mkdir(const wxstring& dir) create the specified directory in the current ftp working directory. returns true if successful.
wxftp::rmdirbool rmdir(const wxstring& dir) remove the specified directory from the current ftp working directory. returns true if successful.
wxftp::pwdwxstring pwd() returns the current ftp working directory.
wxftp::renamebool rename(const wxstring& src, const wxstring& dst) rename the specified src element to dst. returns true if successful.
wxftp::rmfilebool rmfile(const wxstring& path) delete the file specified by path. returns true if successful.
wxftp::setasciibool setascii() sets the transfer mode to ascii. it will be used for the next transfer.
wxftp::setbinarybool setbinary() sets the transfer mode to binary (image). it will be used for the next transfer.
wxftp::setpassivevoid setpassive(bool pasv) if pasv is true, passive connection to the ftp server is used. this is the default as it works with practically all firewalls. if the server doesn't support passive move, you may call this function with false argument to use active connection.
wxftp::settransfermodebool settransfermode(transfermode mode) sets the transfer mode to the specified one. it will be used for the next transfer. if this function is never called, binary transfer mode is used by default.
wxftp::setuservoid setuser(const wxstring& user) sets the user name to be sent to the ftp server to be allowed to log in. default value the default value of the user name is "anonymous". remark this parameter can be included in a url if you want to use the url manager. for example, you can use: "ftp://a_user:a_password@a.host:service/a_directory/a_file" to specify a user and a password.
wxftp::setpasswordvoid setpassword(const wxstring& passwd) sets the password to be sent to the ftp server to be allowed to log in. default value the default value of the user name is your email address. for example, it could be "username@userhost.domain". this password is built by getting the current user name and the host name of the local machine from the system. remark this parameter can be included in a url if you want to use the url manager. for example, you can use: "ftp://a_user:a_password@a.host:service/a_directory/a_file" to specify a user and a password.
wxftp::fileexistsbool fileexists(const wxstring& filename) returns true if the given remote file exists, false otherwise.
wxftp::getfilesizeint getfilesize(const wxstring& filename) returns the file size in bytes or -1 if the file doesn't exist or the size couldn't be determined. notice that this size can be approximative size only and shouldn't be used for allocating the buffer in which the remote file is copied, for example.
wxftp::getdirlistbool getdirlist(wxarraystring& files, const wxstring& wildcard = "") the getlist function is quite low-level. it returns the list of the files in the current directory. the list can be filtered using the wildcard string. if wildcard is empty (default), it will return all files in directory. the form of the list can change from one peer system to another. for example, for a unix peer system, it will look like this:
-r--r--r-- 1 guilhem lavaux 12738 jan 16 20:17 cmndata.cpp -r--r--r-- 1 guilhem lavaux 10866 jan 24 16:41 config.cpp -rw-rw-rw- 1 guilhem lavaux 29967 dec 21 19:17 cwlex_yy.c -rw-rw-rw- 1 guilhem lavaux 14342 jan 22 19:51 cwy_tab.c -r--r--r-- 1 guilhem lavaux 13890 jan 29 19:18 date.cpp -r--r--r-- 1 guilhem lavaux 3989 feb 8 19:18 datstrm.cppbut on windows system, it will look like this:
winamp~1 exe 520196 02-25-1999 19:28 winamp204.exe 1 file(s) 520 196 bytesreturn value: true if the file list was successfully retrieved, false otherwise. see also
wxftp::getfileslistbool getfileslist(wxarraystring& files, const wxstring& wildcard = "") this function returns the computer-parsable list of the files in the current directory (optionally only of the files matching the wildcard, all files by default). this list always has the same format and contains one full (including the directory path) file name per line. return value: true if the file list was successfully retrieved, false otherwise.
wxftp::getoutputstreamwxoutputstream * getoutputstream(const wxstring& file) initializes an output stream to the specified file. the returned stream has all but the seek functionality of wxstreams. when the user finishes writing data, he has to delete the stream to close it. return value an initialized write-only stream. see also
wxftp::getinputstreamwxinputstream * getinputstream(const wxstring& path) creates a new input stream on the specified path. you can use all but the seek functionality of wxstream. seek isn't available on all streams. for example, http or ftp streams do not deal with it. other functions like tell are not available for this sort of stream, at present. you will be notified when the eof is reached by an error. return value returns null if an error occurred (it could be a network failure or the fact that the file doesn't exist). returns the initialized stream. you will have to delete it yourself when you don't need it anymore. the destructor closes the data stream connection but will leave the command stream connection opened. it means that you can still send new commands without reconnecting. example of a standalone connection (without wxurl)
wxftp ftp; wxinputstream *in_stream; char *data; ftp.connect("a.host.domain"); ftp.chdir("a_directory"); in_stream = ftp.getinputstream("a_file_to_get"); data = new char[in_stream->getsize()]; in_stream->read(data, in_stream->getsize()); if (in_stream->lasterror() != wxstream_noerror) { // do something. } delete in_stream; /* close the data connection */ ftp.close(); /* close the command connection */see also
|