]> git.saurik.com Git - wxWidgets.git/blob - include/wx/protocol/ftp.h
Fix warnings about signed/unsigned comparisons inside wxMax() and friends.
[wxWidgets.git] / include / wx / protocol / ftp.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: ftp.h
3 // Purpose: FTP protocol
4 // Author: Vadim Zeitlin
5 // Modified by: Mark Johnson, wxWindows@mj10777.de
6 // 20000917 : RmDir, GetLastResult, GetList
7 // Created: 07/07/1997
8 // RCS-ID: $Id$
9 // Copyright: (c) 1997, 1998 Guilhem Lavaux
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
12
13 #ifndef __WX_FTP_H__
14 #define __WX_FTP_H__
15
16 #include "wx/defs.h"
17
18 #if wxUSE_PROTOCOL_FTP
19
20 #include "wx/sckaddr.h"
21 #include "wx/protocol/protocol.h"
22 #include "wx/url.h"
23
24 class WXDLLIMPEXP_NET wxFTP : public wxProtocol
25 {
26 public:
27 enum TransferMode
28 {
29 NONE, // not set by user explicitly
30 ASCII,
31 BINARY
32 };
33
34 wxFTP();
35 virtual ~wxFTP();
36
37 // Connecting and disconnecting
38 virtual bool Connect(const wxSockAddress& addr, bool wait = true);
39 virtual bool Connect(const wxString& host) { return Connect(host, 0); }
40 virtual bool Connect(const wxString& host, unsigned short port);
41
42 // disconnect
43 virtual bool Close();
44
45 // Parameters set up
46
47 // set transfer mode now
48 void SetPassive(bool pasv) { m_bPassive = pasv; }
49 bool SetBinary() { return SetTransferMode(BINARY); }
50 bool SetAscii() { return SetTransferMode(ASCII); }
51 bool SetTransferMode(TransferMode mode);
52
53 // Generic FTP interface
54
55 // FTP doesn't know the MIME type of the last downloaded/uploaded file
56 virtual wxString GetContentType() const { return wxEmptyString; }
57
58 // the last FTP server reply
59 const wxString& GetLastResult() const { return m_lastResult; }
60
61 // send any FTP command (should be full FTP command line but without
62 // trailing "\r\n") and return its return code
63 char SendCommand(const wxString& command);
64
65 // check that the command returned the given code
66 bool CheckCommand(const wxString& command, char expectedReturn)
67 {
68 // SendCommand() does updates m_lastError
69 return SendCommand(command) == expectedReturn;
70 }
71
72 // Filesystem commands
73 bool ChDir(const wxString& dir);
74 bool MkDir(const wxString& dir);
75 bool RmDir(const wxString& dir);
76 wxString Pwd();
77 bool Rename(const wxString& src, const wxString& dst);
78 bool RmFile(const wxString& path);
79
80 // Get the size of a file in the current dir.
81 // this function tries its best to deliver the size in bytes using BINARY
82 // (the SIZE command reports different sizes depending on whether
83 // type is set to ASCII or BINARY)
84 // returns -1 if file is non-existant or size could not be found
85 int GetFileSize(const wxString& fileName);
86
87 // Check to see if a file exists in the current dir
88 bool FileExists(const wxString& fileName);
89
90 // Download methods
91 bool Abort();
92
93 virtual wxInputStream *GetInputStream(const wxString& path);
94 virtual wxOutputStream *GetOutputStream(const wxString& path);
95
96 // Directory listing
97
98 // get the list of full filenames, the format is fixed: one file name per
99 // line
100 bool GetFilesList(wxArrayString& files,
101 const wxString& wildcard = wxEmptyString)
102 {
103 return GetList(files, wildcard, false);
104 }
105
106 // get a directory list in server dependent format - this can be shown
107 // directly to the user
108 bool GetDirList(wxArrayString& files,
109 const wxString& wildcard = wxEmptyString)
110 {
111 return GetList(files, wildcard, true);
112 }
113
114 // equivalent to either GetFilesList() (default) or GetDirList()
115 bool GetList(wxArrayString& files,
116 const wxString& wildcard = wxEmptyString,
117 bool details = false);
118
119 protected:
120 // this executes a simple ftp command with the given argument and returns
121 // true if it its return code starts with '2'
122 bool DoSimpleCommand(const wxChar *command,
123 const wxString& arg = wxEmptyString);
124
125 // get the server reply, return the first character of the reply code,
126 // '1'..'5' for normal FTP replies, 0 (*not* '0') if an error occurred
127 char GetResult();
128
129 // check that the result is equal to expected value
130 bool CheckResult(char ch) { return GetResult() == ch; }
131
132 // return the socket to be used, Passive/Active versions are used only by
133 // GetPort()
134 wxSocketBase *GetPort();
135 wxSocketBase *GetPassivePort();
136 wxSocketBase *GetActivePort();
137
138 // helper for GetPort()
139 wxString GetPortCmdArgument(const wxIPV4address& Local, const wxIPV4address& New);
140
141 // accept connection from server in active mode, returns the same socket as
142 // passed in in passive mode
143 wxSocketBase *AcceptIfActive(wxSocketBase *sock);
144
145
146 // internal variables:
147
148 wxString m_lastResult;
149
150 // true if there is an FTP transfer going on
151 bool m_streaming;
152
153 // although this should be set to ASCII by default according to STD9,
154 // we will use BINARY transfer mode by default for backwards compatibility
155 TransferMode m_currentTransfermode;
156
157 bool m_bPassive;
158
159 // following is true when a read or write times out, we then assume
160 // the connection is dead and abort. we avoid additional delays this way
161 bool m_bEncounteredError;
162
163
164 friend class wxInputFTPStream;
165 friend class wxOutputFTPStream;
166
167 DECLARE_DYNAMIC_CLASS_NO_COPY(wxFTP)
168 DECLARE_PROTOCOL(wxFTP)
169 };
170
171 // the trace mask used by assorted wxLogTrace() in ftp code, do
172 // wxLog::AddTraceMask(FTP_TRACE_MASK) to see them in output
173 #define FTP_TRACE_MASK wxT("ftp")
174
175 #endif // wxUSE_PROTOCOL_FTP
176
177 #endif // __WX_FTP_H__