00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00022 #ifndef CCXX_FTP_H_
00023 #define CCXX_FTP_H_
00024
00025 #ifndef CCXX_CONFIG_H_
00026 #include <cc++/config.h>
00027 #endif
00028
00029 #ifndef CCXX_SOCKET_H_
00030 #include <cc++/socket.h>
00031 #endif
00032
00033 #include <string>
00034 #include <set>
00035 #include <iostream>
00036 #include <utility>
00037
00038 #ifdef CCXX_NAMESPACES
00039 namespace ost {
00040 using std::string;
00041 using std::set;
00042 #endif
00043
00051 class FTPSocket
00052 {
00053 protected:
00054 TCPStream* cs;
00055
00056 friend class oftpstream;
00057 friend class iftpstream;
00058 static int debug;
00059
00060 public:
00061
00065 void sendCommand(string cmd);
00066 int getResponse(string* resp = 0);
00067
00071 FTPSocket();
00072
00076 explicit FTPSocket(InetHostAddress host, timeout_t to = 0);
00077
00081 FTPSocket(InetHostAddress host, string user, string passwd,
00082 timeout_t to = 0);
00083
00087 ~FTPSocket();
00088
00092 static void setDebug(int d = 1) { debug = d; }
00093
00097 static int getDebug() { return debug; }
00098
00102 void setAscii();
00103
00107 void setEbcdic();
00108
00112 void setBinary();
00113
00117 string command(string cmd);
00118
00122 class DirEntry
00123 {
00124 protected:
00125 string line;
00126 string access;
00127 string user;
00128 string group;
00129 string name;
00130 unsigned int size;
00131
00132 public:
00133 struct Compare
00134 {
00135 bool operator()(const DirEntry& d1, const DirEntry& d2) const
00136 { return d1.getName() < d2.getName(); }
00137 };
00138
00142 bool parse(string li);
00143
00147 string getLine() const { return line; }
00148 string getAccess() const { return access; }
00149 string getUser() const { return user; }
00150 string getGroup() const { return group; }
00151 string getName() const { return name; }
00152 unsigned int getSize() const { return size; }
00153
00154 bool isDir() const { return access[0] == 'd'; }
00155 bool isLink() const { return access[0] == 'l'; }
00156 bool canUserRead() const { return access[1] == 'r'; }
00157 bool canUserWrite() const { return access[2] == 'w'; }
00158 bool canGroupRead() const { return access[4] == 'r'; }
00159 bool canGroupWrite() const { return access[5] == 'w'; }
00160 bool canOtherRead() const { return access[7] == 'r'; }
00161 bool canOtherWrite() const { return access[8] == 'w'; }
00162 };
00163
00167 typedef set<DirEntry, DirEntry::Compare> DirType;
00168
00174 void open(InetHostAddress host, timeout_t to = 0);
00175
00179 void open(InetHostAddress host, string user, string passwd,
00180 timeout_t to = 0);
00181
00185 void login(string user, string passwd);
00186
00190 void close(void);
00191
00195 void quit(void);
00196
00200 DirType getDir(string dir = "", timeout_t to = 0);
00201
00205 string pwd(void);
00206
00210 void cwd(string dir);
00211
00215 void cdup(void);
00216
00220 void put(string file, string rfilename, timeout_t to = 0);
00221
00225 void put(string file, timeout_t to = 0)
00226 { put(file, file, to); }
00227
00231 void get(string file, string lfilename, timeout_t to = 0);
00232
00236 void get(string file, timeout_t to = 0)
00237 { get(file, file, to); }
00238
00242 void remove(string file);
00243
00247 void rename(string from, string to);
00248
00252 void mkdir(string dir);
00253
00257 void rmdir(string dir);
00258 };
00259
00263 class oftpstream : public tcpstream
00264 {
00265 protected:
00266 FTPSocket& ftpref;
00267
00268 public:
00274 oftpstream(FTPSocket& f, string rfilename, int buffer = 512,
00275 timeout_t to = 0);
00276
00280 ~oftpstream()
00281 { close(); }
00282
00286 void close();
00287 };
00288
00292 class iftpstream : public tcpstream
00293 {
00294 protected:
00295 FTPSocket& ftpref;
00296
00297 public:
00301 iftpstream(FTPSocket& f, string rfilename, int buffer = 512,
00302 timeout_t to = 0);
00303
00307 ~iftpstream()
00308 { close(); }
00309
00313 void close();
00314 };
00315
00316 #ifdef COMMON_STD_EXCEPTION
00317
00320 class FTPException : public SockException
00321 {
00322 public:
00323 FTPException(string str) : SockException(str) {};
00324 };
00325
00329 class FTPResponse : public FTPException
00330 {
00331 protected:
00332 int code;
00333 public:
00334 FTPResponse(int c, string r) : FTPException(r), code(c) {};
00335
00339 int getCode() const { return code; }
00340 };
00341 #endif
00342
00343 #ifdef CCXX_NAMESPACES
00344 };
00345 #endif
00346
00347 #endif