X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/8a2c6ef8d186df490df28a694820a5e8afc4b5e8..8493118f58b339019091cc9794d42380c762c377:/src/common/ftp.cpp diff --git a/src/common/ftp.cpp b/src/common/ftp.cpp index 28aad19f7f..a47d3a7b27 100644 --- a/src/common/ftp.cpp +++ b/src/common/ftp.cpp @@ -10,16 +10,18 @@ ///////////////////////////////////////////////////////////////////////////// #ifdef __GNUG__ -#pragma implementation "ftp.h" + #pragma implementation "ftp.h" #endif // For compilers that support precompilation, includes "wx.h". #include "wx/wxprec.h" #ifdef __BORLANDC__ -#pragma hdrstop + #pragma hdrstop #endif +#if wxUSE_SOCKETS + #ifndef __MWERKS__ #include #endif @@ -48,7 +50,7 @@ #if !USE_SHARED_LIBRARY IMPLEMENT_DYNAMIC_CLASS(wxFTP, wxProtocol) -IMPLEMENT_PROTOCOL(wxFTP, "ftp", "ftp", TRUE) +IMPLEMENT_PROTOCOL(wxFTP, wxT("ftp"), wxT("ftp"), TRUE) #endif //////////////////////////////////////////////////////////////// @@ -58,18 +60,16 @@ IMPLEMENT_PROTOCOL(wxFTP, "ftp", "ftp", TRUE) wxFTP::wxFTP() : wxProtocol() { - char tmp[256]; - m_lastError = wxPROTO_NOERR; m_streaming = FALSE; - m_user = "anonymous"; - wxGetUserName(tmp, 256); - m_passwd.sprintf("%s@",tmp); - wxGetHostName(tmp, 256); - m_passwd += tmp; + m_user = wxT("anonymous"); + m_passwd = wxGetUserId(); + m_passwd += '@'; + m_passwd += wxGetHostName(); SetNotify(0); + SetFlags(NONE); } wxFTP::~wxFTP() @@ -82,11 +82,6 @@ wxFTP::~wxFTP() //////////////////////////////////////////////////////////////// bool wxFTP::Connect(wxSockAddress& addr, bool WXUNUSED(wait)) { - if (!m_handler) { - m_lastError = wxPROTO_NOHNDLR; - return FALSE; - } - if (!wxProtocol::Connect(addr)) { m_lastError = wxPROTO_NETERR; return FALSE; @@ -104,13 +99,13 @@ bool wxFTP::Connect(wxSockAddress& addr, bool WXUNUSED(wait)) return FALSE; } - command.sprintf("USER %s", (const char *)m_user); + command.sprintf(wxT("USER %s"), (const wxChar *)m_user); if (!SendCommand(command, '3')) { Close(); return FALSE; } - command.sprintf("PASS %s", (const char *)m_passwd); + command.sprintf(wxT("PASS %s"), (const wxChar *)m_passwd); if (!SendCommand(command, '2')) { Close(); return FALSE; @@ -125,7 +120,7 @@ bool wxFTP::Connect(const wxString& host) wxString my_host = host; addr.Hostname(my_host); - addr.Service("ftp"); + addr.Service(wxT("ftp")); return Connect(addr); } @@ -137,7 +132,7 @@ bool wxFTP::Close() return FALSE; } if (m_connected) - SendCommand(wxString("QUIT"), '2'); + SendCommand(wxString(wxT("QUIT")), '2'); return wxSocketClient::Close(); } @@ -152,8 +147,9 @@ bool wxFTP::SendCommand(const wxString& command, char exp_ret) m_lastError = wxPROTO_STREAMING; return FALSE; } - tmp_str = command + "\r\n"; - if (Write((char *)tmp_str.GetData(), tmp_str.Length()).Error()) { + tmp_str = command + wxT("\r\n"); + const wxWX2MBbuf tmp_buf = tmp_str.mb_str(); + if (Write(wxMBSTRINGCAST tmp_buf, strlen(tmp_buf)).Error()) { m_lastError = wxPROTO_NETERR; return FALSE; } @@ -162,7 +158,8 @@ bool wxFTP::SendCommand(const wxString& command, char exp_ret) bool wxFTP::GetResult(char exp) { - if ((m_lastError = GetLine(this, m_lastResult))) + m_lastError = GetLine(this, m_lastResult); + if ( m_lastError ) return FALSE; if (m_lastResult.GetChar(0) != exp) { m_lastError = wxPROTO_PROTERR; @@ -172,10 +169,11 @@ bool wxFTP::GetResult(char exp) if (m_lastResult.GetChar(3) == '-') { wxString key = m_lastResult.Left((size_t)3); - key += ' '; + key += wxT(' '); while (m_lastResult.Index(key) != 0) { - if ((m_lastError = GetLine(this, m_lastResult))) + m_lastError = GetLine(this, m_lastResult); + if ( m_lastError ) return FALSE; } } @@ -189,14 +187,14 @@ bool wxFTP::ChDir(const wxString& dir) { wxString str = dir; - str.Prepend("CWD "); + str.Prepend(wxT("CWD ")); return SendCommand(str, '2'); } bool wxFTP::MkDir(const wxString& dir) { wxString str = dir; - str.Prepend("MKD "); + str.Prepend(wxT("MKD ")); return SendCommand(str, '2'); } @@ -204,7 +202,7 @@ bool wxFTP::RmDir(const wxString& dir) { wxString str = dir; - str.Prepend("PWD "); + str.Prepend(wxT("PWD ")); return SendCommand(str, '2'); } @@ -212,11 +210,11 @@ wxString wxFTP::Pwd() { int beg, end; - if (!SendCommand("PWD", '2')) + if (!SendCommand(wxT("PWD"), '2')) return wxString((char *)NULL); - beg = m_lastResult.Find('\"',FALSE); - end = m_lastResult.Find('\"',TRUE); + beg = m_lastResult.Find(wxT('\"'),FALSE); + end = m_lastResult.Find(wxT('\"'),TRUE); return wxString(beg+1, end); } @@ -225,11 +223,11 @@ bool wxFTP::Rename(const wxString& src, const wxString& dst) { wxString str; - str = "RNFR " + src; + str = wxT("RNFR ") + src; if (!SendCommand(str, '3')) return FALSE; - str = "RNTO " + dst; + str = wxT("RNTO ") + dst; return SendCommand(str, '2'); } @@ -237,7 +235,7 @@ bool wxFTP::RmFile(const wxString& path) { wxString str; - str = "DELE "; + str = wxT("DELE "); str += path; return SendCommand(str, '2'); } @@ -249,12 +247,14 @@ bool wxFTP::RmFile(const wxString& path) class wxInputFTPStream : public wxSocketInputStream { public: wxFTP *m_ftp; + size_t m_ftpsize; wxInputFTPStream(wxFTP *ftp_clt, wxSocketBase *sock) : wxSocketInputStream(*sock), m_ftp(ftp_clt) {} + size_t GetSize() const { return m_ftpsize; } virtual ~wxInputFTPStream(void) { - if (LastError() != wxStream_NOERROR) + if (LastError() == wxStream_NOERROR) m_ftp->GetResult('2'); else m_ftp->Abort(); @@ -282,32 +282,31 @@ wxSocketClient *wxFTP::GetPort() { wxIPV4address addr; wxSocketClient *client; - struct sockaddr sin; int a[6]; wxString straddr; int addr_pos; + wxUint16 port; + wxUint32 hostaddr; - if (!SendCommand("PASV", '2')) + if (!SendCommand(wxT("PASV"), '2')) return NULL; - sin.sa_family = AF_INET; - addr_pos = m_lastResult.Find('('); + addr_pos = m_lastResult.Find(wxT('(')); if (addr_pos == -1) { m_lastError = wxPROTO_PROTERR; return NULL; } straddr = m_lastResult(addr_pos+1, m_lastResult.Length()); - sscanf((const char *)straddr,"%d,%d,%d,%d,%d,%d",&a[2],&a[3],&a[4],&a[5],&a[0],&a[1]); - sin.sa_data[2] = (char)a[2]; - sin.sa_data[3] = (char)a[3]; - sin.sa_data[4] = (char)a[4]; - sin.sa_data[5] = (char)a[5]; - sin.sa_data[0] = (char)a[0]; - sin.sa_data[1] = (char)a[1]; + wxSscanf((const wxChar *)straddr,wxT("%d,%d,%d,%d,%d,%d"),&a[2],&a[3],&a[4],&a[5],&a[0],&a[1]); - addr.Disassemble(&sin, sizeof(sin)); + hostaddr = (wxUint16)a[5] << 24 | (wxUint16)a[4] << 16 | + (wxUint16)a[3] << 8 | a[2]; + addr.Hostname(hostaddr); - client = m_handler->CreateClient(); + port = (wxUint16)a[0] << 8 | a[1]; + addr.Service(port); + + client = new wxSocketClient(); if (!client->Connect(addr)) { delete client; return NULL; @@ -320,7 +319,7 @@ wxSocketClient *wxFTP::GetPort() bool wxFTP::Abort(void) { m_streaming = FALSE; - if (!SendCommand("ABOR", '4')) + if (!SendCommand(wxT("ABOR"), '4')) return FALSE; return GetResult('2'); } @@ -328,8 +327,10 @@ bool wxFTP::Abort(void) wxInputStream *wxFTP::GetInputStream(const wxString& path) { wxString tmp_str; + int pos_size; + wxInputFTPStream *in_stream; - if (!SendCommand("TYPE I", '2')) + if (!SendCommand(wxT("TYPE I"), '2')) return NULL; wxSocketClient *sock = GetPort(); @@ -339,23 +340,33 @@ wxInputStream *wxFTP::GetInputStream(const wxString& path) return NULL; } - tmp_str = "RETR " + path; + tmp_str = wxT("RETR ") + wxURL::ConvertFromURI(path); if (!SendCommand(tmp_str, '1')) return NULL; - return new wxInputFTPStream(this, sock); + in_stream = new wxInputFTPStream(this, sock); + + pos_size = m_lastResult.Index(wxT('(')); + if (pos_size != wxNOT_FOUND) { + wxString str_size = m_lastResult(pos_size+1, m_lastResult.Index(wxT(')'))-1); + + in_stream->m_ftpsize = wxAtoi(WXSTRINGCAST str_size); + } + sock->SetFlags(WAITALL); + + return in_stream; } wxOutputStream *wxFTP::GetOutputStream(const wxString& path) { wxString tmp_str; - if (!SendCommand("TYPE I", '2')) + if (!SendCommand(wxT("TYPE I"), '2')) return NULL; wxSocketClient *sock = GetPort(); - tmp_str = "STOR " + path; + tmp_str = wxT("STOR ") + path; if (!SendCommand(tmp_str, '1')) return FALSE; @@ -366,7 +377,7 @@ wxList *wxFTP::GetList(const wxString& wildcard) { wxList *file_list = new wxList; wxSocketBase *sock = GetPort(); - wxString tmp_str = "NLST"; + wxString tmp_str = wxT("NLST"); if (!wildcard.IsNull()) tmp_str += wildcard; @@ -390,3 +401,5 @@ wxList *wxFTP::GetList(const wxString& wildcard) return file_list; } +#endif + // wxUSE_SOCKETS