Forum Navigation
FTPConnection
Moderators:
kris
Posted: 12/28/07 13:36:14I believe this could be useful addition to FTPConnection (especially FtpAddress?)
FtpAddress?("ftp://me:password@myftp.com:21") breaks down the address to components...
and few convenience functions for FTPConnection. Plus it could accept FtpAddress? (or just string) in the constructor and in connect.
struct FtpAddress { static FtpAddress* opCall(char[] str) { if(str.length==0) return null; try { auto ret=new FtpAddress; //remove ftp:// auto i=locatePattern(str,"ftp://"); if(i==0) str=str[6..$]; //check for username and/or password user[:pass]@ i=locatePrior(str,'@'); if(i!=str.length) { char[] up=str[0..i]; str=str[i+1..$]; i=locate(up,':'); if(i!=up.length) { ret.user=up[0..i]; ret.pass=up[i+1..$]; } else ret.user=up; } //check for port i=locatePrior(str,':'); if(i!=str.length) { ret.port=cast(uint)Int.toLong(str[i+1..$]); str=str[0..i]; } //check any directories after the adress i=locate(str,'/'); if(i!=str.length) ret.directory=str[i+1..$]; //the rest should be the address ret.address=str[0..i]; if(ret.address.length==0) return null; return ret; }catch(Object o) {return null;} } char[] address; char[] directory; char[] user; char[] pass; uint port=21; } class FTPConnection2 : FTPConnection { this(){super();} int exist(char[] file) { try { auto fi=getFileInfo(file); if(fi.type==FtpFileType.file) return 1; else if(fi.type==FtpFileType.dir || fi.type==FtpFileType.cdir || fi.type==FtpFileType.pdir) return 2; } catch(FTPException o) {if(o.response_code!="501") throw o;} return 0; } Time* modified(char[] file) { try return &getFileInfo(file).modify; catch(FTPException o) {if(o.response_code!="501") throw o;} return null; } long size(char[] file) { try return getFileInfo(file).size; catch(FTPException o) {if(o.response_code!="501") throw o;} return -1; } }