<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>SFML - Simple and Fast Multimedia Library</title> <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" /> <link href="doxygen.css" rel="stylesheet" type="text/css" /> <link href="tabs.css" rel="stylesheet" type="text/css" /> </head> <body> <div id="logo"> <img src="./logo.jpg" width="770" height="200" title="SFML home" alt="SFML logo" /> </div> <!-- Generated by Doxygen 1.5.8 --> <div class="navigation" id="top"> <div class="tabs"> <ul> <li><a href="index.htm"><span>Main Page</span></a></li> <li><a href="namespaces.htm"><span>Namespaces</span></a></li> <li><a href="annotated.htm"><span>Classes</span></a></li> <li class="current"><a href="files.htm"><span>Files</span></a></li> </ul> </div> <div class="tabs"> <ul> <li><a href="files.htm"><span>File List</span></a></li> </ul> </div> <h1>Ftp.cpp</h1><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <a name="l00002"></a>00002 <span class="comment">//</span> <a name="l00003"></a>00003 <span class="comment">// SFML - Simple and Fast Multimedia Library</span> <a name="l00004"></a>00004 <span class="comment">// Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)</span> <a name="l00005"></a>00005 <span class="comment">//</span> <a name="l00006"></a>00006 <span class="comment">// This software is provided 'as-is', without any express or implied warranty.</span> <a name="l00007"></a>00007 <span class="comment">// In no event will the authors be held liable for any damages arising from the use of this software.</span> <a name="l00008"></a>00008 <span class="comment">//</span> <a name="l00009"></a>00009 <span class="comment">// Permission is granted to anyone to use this software for any purpose,</span> <a name="l00010"></a>00010 <span class="comment">// including commercial applications, and to alter it and redistribute it freely,</span> <a name="l00011"></a>00011 <span class="comment">// subject to the following restrictions:</span> <a name="l00012"></a>00012 <span class="comment">//</span> <a name="l00013"></a>00013 <span class="comment">// 1. The origin of this software must not be misrepresented;</span> <a name="l00014"></a>00014 <span class="comment">// you must not claim that you wrote the original software.</span> <a name="l00015"></a>00015 <span class="comment">// If you use this software in a product, an acknowledgment</span> <a name="l00016"></a>00016 <span class="comment">// in the product documentation would be appreciated but is not required.</span> <a name="l00017"></a>00017 <span class="comment">//</span> <a name="l00018"></a>00018 <span class="comment">// 2. Altered source versions must be plainly marked as such,</span> <a name="l00019"></a>00019 <span class="comment">// and must not be misrepresented as being the original software.</span> <a name="l00020"></a>00020 <span class="comment">//</span> <a name="l00021"></a>00021 <span class="comment">// 3. This notice may not be removed or altered from any source distribution.</span> <a name="l00022"></a>00022 <span class="comment">//</span> <a name="l00024"></a>00024 <span class="comment"></span> <a name="l00026"></a>00026 <span class="comment">// Headers</span> <a name="l00028"></a>00028 <span class="comment"></span><span class="preprocessor">#include <SFML/Network/Ftp.hpp></span> <a name="l00029"></a>00029 <span class="preprocessor">#include <SFML/Network/IPAddress.hpp></span> <a name="l00030"></a>00030 <span class="preprocessor">#include <algorithm></span> <a name="l00031"></a>00031 <span class="preprocessor">#include <fstream></span> <a name="l00032"></a>00032 <span class="preprocessor">#include <iterator></span> <a name="l00033"></a>00033 <span class="preprocessor">#include <sstream></span> <a name="l00034"></a>00034 <a name="l00035"></a>00035 <a name="l00036"></a>00036 <span class="keyword">namespace </span>sf <a name="l00037"></a>00037 { <a name="l00039"></a>00039 <span class="comment">// Utility class for exchanging stuff with the server</span> <a name="l00040"></a>00040 <span class="comment">// on the data channel</span> <a name="l00042"></a>00042 <span class="comment"></span><span class="keyword">class </span>Ftp::DataChannel : NonCopyable <a name="l00043"></a>00043 { <a name="l00044"></a>00044 <span class="keyword">public</span> : <a name="l00045"></a>00045 <a name="l00047"></a>00047 <span class="comment">// Constructor</span> <a name="l00049"></a>00049 <span class="comment"></span> DataChannel(Ftp& Owner); <a name="l00050"></a>00050 <a name="l00052"></a>00052 <span class="comment">// Destructor</span> <a name="l00054"></a>00054 <span class="comment"></span> ~DataChannel(); <a name="l00055"></a>00055 <a name="l00057"></a>00057 <span class="comment">// Open the data channel using the specified mode and port</span> <a name="l00059"></a>00059 <span class="comment"></span> Ftp::Response Open(<a class="code" href="classsf_1_1Ftp.htm#1cd6b89ad23253f6d97e6d4ca4d558cb" title="Enumeration of transfer modes.">Ftp::TransferMode</a> Mode); <a name="l00060"></a>00060 <a name="l00062"></a>00062 <span class="comment">// Send data on the data channel</span> <a name="l00064"></a>00064 <span class="comment"></span> <span class="keywordtype">void</span> Send(<span class="keyword">const</span> std::vector<char>& Data); <a name="l00065"></a>00065 <a name="l00067"></a>00067 <span class="comment">// Receive data on the data channel until it is closed</span> <a name="l00069"></a>00069 <span class="comment"></span> <span class="keywordtype">void</span> Receive(std::vector<char>& Data); <a name="l00070"></a>00070 <a name="l00071"></a>00071 <span class="keyword">private</span> : <a name="l00072"></a>00072 <a name="l00074"></a>00074 <span class="comment">// Member data</span> <a name="l00076"></a>00076 <span class="comment"></span> Ftp& myFtp; <a name="l00077"></a>00077 SocketTCP myDataSocket; <a name="l00078"></a>00078 }; <a name="l00079"></a>00079 <a name="l00080"></a>00080 <a name="l00084"></a><a class="code" href="classsf_1_1Ftp_1_1Response.htm#b11ecf1c327da80f3e89e265ac741fc2">00084</a> <a class="code" href="classsf_1_1Ftp_1_1Response.htm#b11ecf1c327da80f3e89e265ac741fc2" title="Default constructor.">Ftp::Response::Response</a>(<a class="code" href="classsf_1_1Ftp_1_1Response.htm#f81738f06b6f571761696291276acb3b" title="Enumerate all the valid status codes returned in a FTP response.">Status</a> Code, <span class="keyword">const</span> std::string& Message) : <a name="l00085"></a>00085 myStatus (Code), <a name="l00086"></a>00086 myMessage(Message) <a name="l00087"></a>00087 { <a name="l00088"></a>00088 <a name="l00089"></a>00089 } <a name="l00090"></a>00090 <a name="l00091"></a>00091 <a name="l00096"></a><a class="code" href="classsf_1_1Ftp_1_1Response.htm#a04c49e1f6a19c5b879838d8d5f3c134">00096</a> <span class="keywordtype">bool</span> <a class="code" href="classsf_1_1Ftp_1_1Response.htm#a04c49e1f6a19c5b879838d8d5f3c134" title="Convenience function to check if the response status code means a success.">Ftp::Response::IsOk</a>()<span class="keyword"> const</span> <a name="l00097"></a>00097 <span class="keyword"></span>{ <a name="l00098"></a>00098 <span class="keywordflow">return</span> myStatus < 400; <a name="l00099"></a>00099 } <a name="l00100"></a>00100 <a name="l00101"></a>00101 <a name="l00105"></a><a class="code" href="classsf_1_1Ftp_1_1Response.htm#8f507094af1fd34e4786d075cce102ee">00105</a> <a class="code" href="classsf_1_1Ftp_1_1Response.htm#f81738f06b6f571761696291276acb3b" title="Enumerate all the valid status codes returned in a FTP response.">Ftp::Response::Status</a> <a class="code" href="classsf_1_1Ftp_1_1Response.htm#8f507094af1fd34e4786d075cce102ee" title="Get the response status code.">Ftp::Response::GetStatus</a>()<span class="keyword"> const</span> <a name="l00106"></a>00106 <span class="keyword"></span>{ <a name="l00107"></a>00107 <span class="keywordflow">return</span> myStatus; <a name="l00108"></a>00108 } <a name="l00109"></a>00109 <a name="l00110"></a>00110 <a name="l00114"></a><a class="code" href="classsf_1_1Ftp_1_1Response.htm#eafd573beafd242460b1a7d516d503b5">00114</a> <span class="keyword">const</span> std::string& <a class="code" href="classsf_1_1Ftp_1_1Response.htm#eafd573beafd242460b1a7d516d503b5" title="Get the full message contained in the response.">Ftp::Response::GetMessage</a>()<span class="keyword"> const</span> <a name="l00115"></a>00115 <span class="keyword"></span>{ <a name="l00116"></a>00116 <span class="keywordflow">return</span> myMessage; <a name="l00117"></a>00117 } <a name="l00118"></a>00118 <a name="l00119"></a>00119 <a name="l00123"></a><a class="code" href="classsf_1_1Ftp_1_1DirectoryResponse.htm#2f98f685d1aa56d3c341d38cfb2798bc">00123</a> <a class="code" href="classsf_1_1Ftp_1_1DirectoryResponse.htm#2f98f685d1aa56d3c341d38cfb2798bc" title="Default constructor.">Ftp::DirectoryResponse::DirectoryResponse</a>(<a class="code" href="classsf_1_1Ftp_1_1Response.htm" title="This class wraps a FTP response, which is basically :a status codea message.">Ftp::Response</a> Resp) : <a name="l00124"></a>00124 <a class="code" href="classsf_1_1Ftp.htm" title="This class provides methods for manipulating the FTP protocol (described in RFC 959)...">Ftp</a>::<a class="code" href="classsf_1_1Ftp_1_1Response.htm" title="This class wraps a FTP response, which is basically :a status codea message.">Response</a>(Resp) <a name="l00125"></a>00125 { <a name="l00126"></a>00126 <span class="keywordflow">if</span> (<a class="code" href="classsf_1_1Ftp_1_1Response.htm#a04c49e1f6a19c5b879838d8d5f3c134" title="Convenience function to check if the response status code means a success.">IsOk</a>()) <a name="l00127"></a>00127 { <a name="l00128"></a>00128 <span class="comment">// Extract the directory from the server response</span> <a name="l00129"></a>00129 std::string::size_type Begin = Resp.<a class="code" href="classsf_1_1Ftp_1_1Response.htm#eafd573beafd242460b1a7d516d503b5" title="Get the full message contained in the response.">GetMessage</a>().find(<span class="charliteral">'"'</span>, 0); <a name="l00130"></a>00130 std::string::size_type End = Resp.<a class="code" href="classsf_1_1Ftp_1_1Response.htm#eafd573beafd242460b1a7d516d503b5" title="Get the full message contained in the response.">GetMessage</a>().find(<span class="charliteral">'"'</span>, Begin + 1); <a name="l00131"></a>00131 myDirectory = Resp.<a class="code" href="classsf_1_1Ftp_1_1Response.htm#eafd573beafd242460b1a7d516d503b5" title="Get the full message contained in the response.">GetMessage</a>().substr(Begin + 1, End - Begin - 1); <a name="l00132"></a>00132 } <a name="l00133"></a>00133 } <a name="l00134"></a>00134 <a name="l00135"></a>00135 <a name="l00139"></a><a class="code" href="classsf_1_1Ftp_1_1DirectoryResponse.htm#a07297cc8acc9276ce5c0cfa58bac799">00139</a> <span class="keyword">const</span> std::string& <a class="code" href="classsf_1_1Ftp_1_1DirectoryResponse.htm#a07297cc8acc9276ce5c0cfa58bac799" title="Get the directory returned in the response.">Ftp::DirectoryResponse::GetDirectory</a>()<span class="keyword"> const</span> <a name="l00140"></a>00140 <span class="keyword"></span>{ <a name="l00141"></a>00141 <span class="keywordflow">return</span> myDirectory; <a name="l00142"></a>00142 } <a name="l00143"></a>00143 <a name="l00144"></a>00144 <a name="l00148"></a><a class="code" href="classsf_1_1Ftp_1_1ListingResponse.htm#2815303ca236427cf5bd1c09469f7db5">00148</a> <a class="code" href="classsf_1_1Ftp_1_1ListingResponse.htm#2815303ca236427cf5bd1c09469f7db5" title="Default constructor.">Ftp::ListingResponse::ListingResponse</a>(<a class="code" href="classsf_1_1Ftp_1_1Response.htm" title="This class wraps a FTP response, which is basically :a status codea message.">Ftp::Response</a> Resp, <span class="keyword">const</span> std::vector<char>& Data) : <a name="l00149"></a>00149 <a class="code" href="classsf_1_1Ftp.htm" title="This class provides methods for manipulating the FTP protocol (described in RFC 959)...">Ftp</a>::<a class="code" href="classsf_1_1Ftp_1_1Response.htm" title="This class wraps a FTP response, which is basically :a status codea message.">Response</a>(Resp) <a name="l00150"></a>00150 { <a name="l00151"></a>00151 <span class="keywordflow">if</span> (<a class="code" href="classsf_1_1Ftp_1_1Response.htm#a04c49e1f6a19c5b879838d8d5f3c134" title="Convenience function to check if the response status code means a success.">IsOk</a>()) <a name="l00152"></a>00152 { <a name="l00153"></a>00153 <span class="comment">// Fill the array of strings</span> <a name="l00154"></a>00154 std::string Paths(Data.begin(), Data.end()); <a name="l00155"></a>00155 std::string::size_type LastPos = 0; <a name="l00156"></a>00156 <span class="keywordflow">for</span> (std::string::size_type Pos = Paths.find(<span class="stringliteral">"\r\n"</span>); Pos != std::string::npos; Pos = Paths.find(<span class="stringliteral">"\r\n"</span>, LastPos)) <a name="l00157"></a>00157 { <a name="l00158"></a>00158 myFilenames.push_back(Paths.substr(LastPos, Pos - LastPos)); <a name="l00159"></a>00159 LastPos = Pos + 2; <a name="l00160"></a>00160 } <a name="l00161"></a>00161 } <a name="l00162"></a>00162 } <a name="l00163"></a>00163 <a name="l00164"></a>00164 <a name="l00168"></a><a class="code" href="classsf_1_1Ftp_1_1ListingResponse.htm#6c4b942fe9e3da2c7104de7d8a13e522">00168</a> std::size_t <a class="code" href="classsf_1_1Ftp_1_1ListingResponse.htm#6c4b942fe9e3da2c7104de7d8a13e522" title="Get the number of filenames in the listing.">Ftp::ListingResponse::GetCount</a>()<span class="keyword"> const</span> <a name="l00169"></a>00169 <span class="keyword"></span>{ <a name="l00170"></a>00170 <span class="keywordflow">return</span> myFilenames.size(); <a name="l00171"></a>00171 } <a name="l00172"></a>00172 <a name="l00173"></a>00173 <a name="l00177"></a><a class="code" href="classsf_1_1Ftp_1_1ListingResponse.htm#11b761b5fa04b5e2c3bac972cf4a8922">00177</a> <span class="keyword">const</span> std::string& <a class="code" href="classsf_1_1Ftp_1_1ListingResponse.htm#11b761b5fa04b5e2c3bac972cf4a8922" title="Get the Index-th filename in the directory.">Ftp::ListingResponse::GetFilename</a>(std::size_t Index)<span class="keyword"> const</span> <a name="l00178"></a>00178 <span class="keyword"></span>{ <a name="l00179"></a>00179 <span class="keywordflow">return</span> myFilenames[Index]; <a name="l00180"></a>00180 } <a name="l00181"></a>00181 <a name="l00182"></a>00182 <a name="l00186"></a><a class="code" href="classsf_1_1Ftp.htm#2edfa8e9009caf27bce74459ae76dc52">00186</a> <a class="code" href="classsf_1_1Ftp.htm#2edfa8e9009caf27bce74459ae76dc52" title="Destructor -- close the connection with the server.">Ftp::~Ftp</a>() <a name="l00187"></a>00187 { <a name="l00188"></a>00188 <a class="code" href="classsf_1_1Ftp.htm#5ec762168457a0b6385ad1104f5869bc" title="Close the connection with FTP server.">Disconnect</a>(); <a name="l00189"></a>00189 } <a name="l00190"></a>00190 <a name="l00191"></a>00191 <a name="l00195"></a><a class="code" href="classsf_1_1Ftp.htm#f566dc8b2595722dd08c312677c3f26c">00195</a> <a class="code" href="classsf_1_1Ftp_1_1Response.htm" title="This class wraps a FTP response, which is basically :a status codea message.">Ftp::Response</a> <a class="code" href="classsf_1_1Ftp.htm#f566dc8b2595722dd08c312677c3f26c" title="Connect to the specified FTP server.">Ftp::Connect</a>(<span class="keyword">const</span> <a class="code" href="classsf_1_1IPAddress.htm" title="IPAddress provides easy manipulation of IP v4 addresses.">IPAddress</a>& Server, <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> Port, <span class="keywordtype">float</span> Timeout) <a name="l00196"></a>00196 { <a name="l00197"></a>00197 <span class="comment">// Connect to the server</span> <a name="l00198"></a>00198 <span class="keywordflow">if</span> (myCommandSocket.<a class="code" href="classsf_1_1SocketTCP.htm#dd590d0119fa8edbe8da1574b6c8f07f" title="Connect to another computer on a specified port.">Connect</a>(Port, Server, Timeout) != Socket::Done) <a name="l00199"></a>00199 <span class="keywordflow">return</span> <a class="code" href="classsf_1_1Ftp_1_1Response.htm" title="This class wraps a FTP response, which is basically :a status codea message.">Response</a>(<a class="code" href="classsf_1_1Ftp_1_1Response.htm#f81738f06b6f571761696291276acb3b1193dea18ab5169792489796e29a154c" title="Connection with server failed.">Response::ConnectionFailed</a>); <a name="l00200"></a>00200 <a name="l00201"></a>00201 <span class="comment">// Get the response to the connection</span> <a name="l00202"></a>00202 <span class="keywordflow">return</span> GetResponse(); <a name="l00203"></a>00203 } <a name="l00204"></a>00204 <a name="l00205"></a>00205 <a name="l00209"></a><a class="code" href="classsf_1_1Ftp.htm#cee25f564e7dbab511e46e61d14bb0cf">00209</a> <a class="code" href="classsf_1_1Ftp_1_1Response.htm" title="This class wraps a FTP response, which is basically :a status codea message.">Ftp::Response</a> <a class="code" href="classsf_1_1Ftp.htm#cee25f564e7dbab511e46e61d14bb0cf" title="Log in using anonymous account.">Ftp::Login</a>() <a name="l00210"></a>00210 { <a name="l00211"></a>00211 <span class="keywordflow">return</span> <a class="code" href="classsf_1_1Ftp.htm#cee25f564e7dbab511e46e61d14bb0cf" title="Log in using anonymous account.">Login</a>(<span class="stringliteral">"anonymous"</span>, <span class="stringliteral">"user@sfml-dev.org"</span>); <a name="l00212"></a>00212 } <a name="l00213"></a>00213 <a name="l00214"></a>00214 <a name="l00218"></a><a class="code" href="classsf_1_1Ftp.htm#1ed080612214df38a5f740d1ee49f082">00218</a> <a class="code" href="classsf_1_1Ftp_1_1Response.htm" title="This class wraps a FTP response, which is basically :a status codea message.">Ftp::Response</a> <a class="code" href="classsf_1_1Ftp.htm#cee25f564e7dbab511e46e61d14bb0cf" title="Log in using anonymous account.">Ftp::Login</a>(<span class="keyword">const</span> std::string& UserName, <span class="keyword">const</span> std::string& Password) <a name="l00219"></a>00219 { <a name="l00220"></a>00220 <a class="code" href="classsf_1_1Ftp_1_1Response.htm" title="This class wraps a FTP response, which is basically :a status codea message.">Response</a> Resp = SendCommand(<span class="stringliteral">"USER"</span>, UserName); <a name="l00221"></a>00221 <span class="keywordflow">if</span> (Resp.<a class="code" href="classsf_1_1Ftp_1_1Response.htm#a04c49e1f6a19c5b879838d8d5f3c134" title="Convenience function to check if the response status code means a success.">IsOk</a>()) <a name="l00222"></a>00222 Resp = SendCommand(<span class="stringliteral">"PASS"</span>, Password); <a name="l00223"></a>00223 <a name="l00224"></a>00224 <span class="keywordflow">return</span> Resp; <a name="l00225"></a>00225 } <a name="l00226"></a>00226 <a name="l00227"></a>00227 <a name="l00231"></a><a class="code" href="classsf_1_1Ftp.htm#5ec762168457a0b6385ad1104f5869bc">00231</a> <a class="code" href="classsf_1_1Ftp_1_1Response.htm" title="This class wraps a FTP response, which is basically :a status codea message.">Ftp::Response</a> <a class="code" href="classsf_1_1Ftp.htm#5ec762168457a0b6385ad1104f5869bc" title="Close the connection with FTP server.">Ftp::Disconnect</a>() <a name="l00232"></a>00232 { <a name="l00233"></a>00233 <span class="comment">// Send the exit command</span> <a name="l00234"></a>00234 <a class="code" href="classsf_1_1Ftp_1_1Response.htm" title="This class wraps a FTP response, which is basically :a status codea message.">Response</a> Resp = SendCommand(<span class="stringliteral">"QUIT"</span>); <a name="l00235"></a>00235 <span class="keywordflow">if</span> (Resp.<a class="code" href="classsf_1_1Ftp_1_1Response.htm#a04c49e1f6a19c5b879838d8d5f3c134" title="Convenience function to check if the response status code means a success.">IsOk</a>()) <a name="l00236"></a>00236 myCommandSocket.<a class="code" href="classsf_1_1SocketTCP.htm#70a5111cbddd1edbfa6cbd6017c6d044" title="Close the socket.">Close</a>(); <a name="l00237"></a>00237 <a name="l00238"></a>00238 <span class="keywordflow">return</span> Resp; <a name="l00239"></a>00239 } <a name="l00240"></a>00240 <a name="l00241"></a>00241 <a name="l00245"></a><a class="code" href="classsf_1_1Ftp.htm#0ee896f69d6fdaa18d65df83e7347ab4">00245</a> <a class="code" href="classsf_1_1Ftp_1_1Response.htm" title="This class wraps a FTP response, which is basically :a status codea message.">Ftp::Response</a> <a class="code" href="classsf_1_1Ftp.htm#0ee896f69d6fdaa18d65df83e7347ab4" title="Send a null command just to prevent from being disconnected.">Ftp::KeepAlive</a>() <a name="l00246"></a>00246 { <a name="l00247"></a>00247 <span class="keywordflow">return</span> SendCommand(<span class="stringliteral">"NOOP"</span>); <a name="l00248"></a>00248 } <a name="l00249"></a>00249 <a name="l00250"></a>00250 <a name="l00254"></a><a class="code" href="classsf_1_1Ftp.htm#c7ae66df8800b2dffadaa293b80f3dde">00254</a> <a class="code" href="classsf_1_1Ftp_1_1DirectoryResponse.htm" title="Specialization of FTP response returning a directory.">Ftp::DirectoryResponse</a> <a class="code" href="classsf_1_1Ftp.htm#c7ae66df8800b2dffadaa293b80f3dde" title="Get the current working directory.">Ftp::GetWorkingDirectory</a>() <a name="l00255"></a>00255 { <a name="l00256"></a>00256 <span class="keywordflow">return</span> <a class="code" href="classsf_1_1Ftp_1_1DirectoryResponse.htm" title="Specialization of FTP response returning a directory.">DirectoryResponse</a>(SendCommand(<span class="stringliteral">"PWD"</span>)); <a name="l00257"></a>00257 } <a name="l00258"></a>00258 <a name="l00259"></a>00259 <a name="l00264"></a><a class="code" href="classsf_1_1Ftp.htm#2d68f88694c0b12f1b2bfdb23246c357">00264</a> <a class="code" href="classsf_1_1Ftp_1_1ListingResponse.htm" title="Specialization of FTP response returning a filename lisiting.">Ftp::ListingResponse</a> <a class="code" href="classsf_1_1Ftp.htm#2d68f88694c0b12f1b2bfdb23246c357" title="Get the contents of the given directory (subdirectories and files).">Ftp::GetDirectoryListing</a>(<span class="keyword">const</span> std::string& Directory) <a name="l00265"></a>00265 { <a name="l00266"></a>00266 <span class="comment">// Open a data channel on default port (20) using ASCII transfer mode</span> <a name="l00267"></a>00267 std::vector<char> DirData; <a name="l00268"></a>00268 DataChannel Data(*<span class="keyword">this</span>); <a name="l00269"></a>00269 <a class="code" href="classsf_1_1Ftp_1_1Response.htm" title="This class wraps a FTP response, which is basically :a status codea message.">Response</a> Resp = Data.Open(<a class="code" href="classsf_1_1Ftp.htm#1cd6b89ad23253f6d97e6d4ca4d558cbc9e544a22dce8ef3177449cb235d15c2" title="Text mode using ASCII encoding.">Ascii</a>); <a name="l00270"></a>00270 <span class="keywordflow">if</span> (Resp.<a class="code" href="classsf_1_1Ftp_1_1Response.htm#a04c49e1f6a19c5b879838d8d5f3c134" title="Convenience function to check if the response status code means a success.">IsOk</a>()) <a name="l00271"></a>00271 { <a name="l00272"></a>00272 <span class="comment">// Tell the server to send us the listing</span> <a name="l00273"></a>00273 Resp = SendCommand(<span class="stringliteral">"NLST"</span>, Directory); <a name="l00274"></a>00274 <span class="keywordflow">if</span> (Resp.<a class="code" href="classsf_1_1Ftp_1_1Response.htm#a04c49e1f6a19c5b879838d8d5f3c134" title="Convenience function to check if the response status code means a success.">IsOk</a>()) <a name="l00275"></a>00275 { <a name="l00276"></a>00276 <span class="comment">// Receive the listing</span> <a name="l00277"></a>00277 Data.Receive(DirData); <a name="l00278"></a>00278 <a name="l00279"></a>00279 <span class="comment">// Get the response from the server</span> <a name="l00280"></a>00280 Resp = GetResponse(); <a name="l00281"></a>00281 } <a name="l00282"></a>00282 } <a name="l00283"></a>00283 <a name="l00284"></a>00284 <span class="keywordflow">return</span> <a class="code" href="classsf_1_1Ftp_1_1ListingResponse.htm" title="Specialization of FTP response returning a filename lisiting.">ListingResponse</a>(Resp, DirData); <a name="l00285"></a>00285 } <a name="l00286"></a>00286 <a name="l00287"></a>00287 <a name="l00291"></a><a class="code" href="classsf_1_1Ftp.htm#6b7e5a6d2f6d3a8cac82d759502f2fbc">00291</a> <a class="code" href="classsf_1_1Ftp_1_1Response.htm" title="This class wraps a FTP response, which is basically :a status codea message.">Ftp::Response</a> <a class="code" href="classsf_1_1Ftp.htm#6b7e5a6d2f6d3a8cac82d759502f2fbc" title="Change the current working directory.">Ftp::ChangeDirectory</a>(<span class="keyword">const</span> std::string& Directory) <a name="l00292"></a>00292 { <a name="l00293"></a>00293 <span class="keywordflow">return</span> SendCommand(<span class="stringliteral">"CWD"</span>, Directory); <a name="l00294"></a>00294 } <a name="l00295"></a>00295 <a name="l00296"></a>00296 <a name="l00300"></a><a class="code" href="classsf_1_1Ftp.htm#327e052e0c352d743804d7146a7eb6f6">00300</a> <a class="code" href="classsf_1_1Ftp_1_1Response.htm" title="This class wraps a FTP response, which is basically :a status codea message.">Ftp::Response</a> <a class="code" href="classsf_1_1Ftp.htm#327e052e0c352d743804d7146a7eb6f6" title="Go to the parent directory of the current one.">Ftp::ParentDirectory</a>() <a name="l00301"></a>00301 { <a name="l00302"></a>00302 <span class="keywordflow">return</span> SendCommand(<span class="stringliteral">"CDUP"</span>); <a name="l00303"></a>00303 } <a name="l00304"></a>00304 <a name="l00305"></a>00305 <a name="l00309"></a><a class="code" href="classsf_1_1Ftp.htm#302127842055302365b0982077f11071">00309</a> <a class="code" href="classsf_1_1Ftp_1_1Response.htm" title="This class wraps a FTP response, which is basically :a status codea message.">Ftp::Response</a> <a class="code" href="classsf_1_1Ftp.htm#302127842055302365b0982077f11071" title="Create a new directory.">Ftp::MakeDirectory</a>(<span class="keyword">const</span> std::string& Name) <a name="l00310"></a>00310 { <a name="l00311"></a>00311 <span class="keywordflow">return</span> SendCommand(<span class="stringliteral">"MKD"</span>, Name); <a name="l00312"></a>00312 } <a name="l00313"></a>00313 <a name="l00314"></a>00314 <a name="l00318"></a><a class="code" href="classsf_1_1Ftp.htm#c90d9da996773cb3eed1b715fbb85d32">00318</a> <a class="code" href="classsf_1_1Ftp_1_1Response.htm" title="This class wraps a FTP response, which is basically :a status codea message.">Ftp::Response</a> <a class="code" href="classsf_1_1Ftp.htm#c90d9da996773cb3eed1b715fbb85d32" title="Remove an existing directory.">Ftp::DeleteDirectory</a>(<span class="keyword">const</span> std::string& Name) <a name="l00319"></a>00319 { <a name="l00320"></a>00320 <span class="keywordflow">return</span> SendCommand(<span class="stringliteral">"RMD"</span>, Name); <a name="l00321"></a>00321 } <a name="l00322"></a>00322 <a name="l00323"></a>00323 <a name="l00327"></a><a class="code" href="classsf_1_1Ftp.htm#85075cbe59e9cc2ac0f8ca0b6c0e982c">00327</a> <a class="code" href="classsf_1_1Ftp_1_1Response.htm" title="This class wraps a FTP response, which is basically :a status codea message.">Ftp::Response</a> <a class="code" href="classsf_1_1Ftp.htm#85075cbe59e9cc2ac0f8ca0b6c0e982c" title="Rename a file.">Ftp::RenameFile</a>(<span class="keyword">const</span> std::string& File, <span class="keyword">const</span> std::string& NewName) <a name="l00328"></a>00328 { <a name="l00329"></a>00329 <a class="code" href="classsf_1_1Ftp_1_1Response.htm" title="This class wraps a FTP response, which is basically :a status codea message.">Response</a> Resp = SendCommand(<span class="stringliteral">"RNFR"</span>, File); <a name="l00330"></a>00330 <span class="keywordflow">if</span> (Resp.<a class="code" href="classsf_1_1Ftp_1_1Response.htm#a04c49e1f6a19c5b879838d8d5f3c134" title="Convenience function to check if the response status code means a success.">IsOk</a>()) <a name="l00331"></a>00331 Resp = SendCommand(<span class="stringliteral">"RNTO"</span>, NewName); <a name="l00332"></a>00332 <a name="l00333"></a>00333 <span class="keywordflow">return</span> Resp; <a name="l00334"></a>00334 } <a name="l00335"></a>00335 <a name="l00336"></a>00336 <a name="l00340"></a><a class="code" href="classsf_1_1Ftp.htm#eb6d5688700b36ea2a34a8e0dbcf43a9">00340</a> <a class="code" href="classsf_1_1Ftp_1_1Response.htm" title="This class wraps a FTP response, which is basically :a status codea message.">Ftp::Response</a> <a class="code" href="classsf_1_1Ftp.htm#eb6d5688700b36ea2a34a8e0dbcf43a9" title="Remove an existing file.">Ftp::DeleteFile</a>(<span class="keyword">const</span> std::string& Name) <a name="l00341"></a>00341 { <a name="l00342"></a>00342 <span class="keywordflow">return</span> SendCommand(<span class="stringliteral">"DELE"</span>, Name); <a name="l00343"></a>00343 } <a name="l00344"></a>00344 <a name="l00345"></a>00345 <a name="l00349"></a><a class="code" href="classsf_1_1Ftp.htm#17e6cc8f6ce7c1904d598d5bb6a2d886">00349</a> <a class="code" href="classsf_1_1Ftp_1_1Response.htm" title="This class wraps a FTP response, which is basically :a status codea message.">Ftp::Response</a> <a class="code" href="classsf_1_1Ftp.htm#17e6cc8f6ce7c1904d598d5bb6a2d886" title="Download a file from the server.">Ftp::Download</a>(<span class="keyword">const</span> std::string& DistantFile, <span class="keyword">const</span> std::string& DestPath, <a class="code" href="classsf_1_1Ftp.htm#1cd6b89ad23253f6d97e6d4ca4d558cb" title="Enumeration of transfer modes.">TransferMode</a> Mode) <a name="l00350"></a>00350 { <a name="l00351"></a>00351 <span class="comment">// Open a data channel using the given transfer mode</span> <a name="l00352"></a>00352 DataChannel Data(*<span class="keyword">this</span>); <a name="l00353"></a>00353 <a class="code" href="classsf_1_1Ftp_1_1Response.htm" title="This class wraps a FTP response, which is basically :a status codea message.">Response</a> Resp = Data.Open(Mode); <a name="l00354"></a>00354 <span class="keywordflow">if</span> (Resp.<a class="code" href="classsf_1_1Ftp_1_1Response.htm#a04c49e1f6a19c5b879838d8d5f3c134" title="Convenience function to check if the response status code means a success.">IsOk</a>()) <a name="l00355"></a>00355 { <a name="l00356"></a>00356 <span class="comment">// Tell the server to start the transfer</span> <a name="l00357"></a>00357 Resp = SendCommand(<span class="stringliteral">"RETR"</span>, DistantFile); <a name="l00358"></a>00358 <span class="keywordflow">if</span> (Resp.<a class="code" href="classsf_1_1Ftp_1_1Response.htm#a04c49e1f6a19c5b879838d8d5f3c134" title="Convenience function to check if the response status code means a success.">IsOk</a>()) <a name="l00359"></a>00359 { <a name="l00360"></a>00360 <span class="comment">// Receive the file data</span> <a name="l00361"></a>00361 std::vector<char> FileData; <a name="l00362"></a>00362 Data.Receive(FileData); <a name="l00363"></a>00363 <a name="l00364"></a>00364 <span class="comment">// Get the response from the server</span> <a name="l00365"></a>00365 Resp = GetResponse(); <a name="l00366"></a>00366 <span class="keywordflow">if</span> (Resp.<a class="code" href="classsf_1_1Ftp_1_1Response.htm#a04c49e1f6a19c5b879838d8d5f3c134" title="Convenience function to check if the response status code means a success.">IsOk</a>()) <a name="l00367"></a>00367 { <a name="l00368"></a>00368 <span class="comment">// Extract the filename from the file path</span> <a name="l00369"></a>00369 std::string Filename = DistantFile; <a name="l00370"></a>00370 std::string::size_type Pos = Filename.find_last_of(<span class="stringliteral">"/\\"</span>); <a name="l00371"></a>00371 <span class="keywordflow">if</span> (Pos != std::string::npos) <a name="l00372"></a>00372 Filename = Filename.substr(Pos + 1); <a name="l00373"></a>00373 <a name="l00374"></a>00374 <span class="comment">// Make sure the destination path ends with a slash</span> <a name="l00375"></a>00375 std::string Path = DestPath; <a name="l00376"></a>00376 <span class="keywordflow">if</span> (!Path.empty() && (Path[Path.size() - 1] != <span class="charliteral">'\\'</span>) && (Path[Path.size() - 1] != <span class="charliteral">'/'</span>)) <a name="l00377"></a>00377 Path += <span class="stringliteral">"/"</span>; <a name="l00378"></a>00378 <a name="l00379"></a>00379 <span class="comment">// Create the file and copy the received data into it</span> <a name="l00380"></a>00380 std::ofstream File((Path + Filename).c_str(), std::ios_base::binary); <a name="l00381"></a>00381 <span class="keywordflow">if</span> (!File) <a name="l00382"></a>00382 <span class="keywordflow">return</span> <a class="code" href="classsf_1_1Ftp_1_1Response.htm" title="This class wraps a FTP response, which is basically :a status codea message.">Response</a>(<a class="code" href="classsf_1_1Ftp_1_1Response.htm#f81738f06b6f571761696291276acb3b835ed0e55eac77c5ebf33b9be7507935" title="Invalid file to upload / download.">Response::InvalidFile</a>); <a name="l00383"></a>00383 File.write(&FileData[0], static_cast<std::streamsize>(FileData.size())); <a name="l00384"></a>00384 } <a name="l00385"></a>00385 } <a name="l00386"></a>00386 } <a name="l00387"></a>00387 <a name="l00388"></a>00388 <span class="keywordflow">return</span> Resp; <a name="l00389"></a>00389 } <a name="l00390"></a>00390 <a name="l00391"></a>00391 <a name="l00395"></a><a class="code" href="classsf_1_1Ftp.htm#32240c75993a64710da8a29804bd6528">00395</a> <a class="code" href="classsf_1_1Ftp_1_1Response.htm" title="This class wraps a FTP response, which is basically :a status codea message.">Ftp::Response</a> <a class="code" href="classsf_1_1Ftp.htm#32240c75993a64710da8a29804bd6528" title="Upload a file to the server.">Ftp::Upload</a>(<span class="keyword">const</span> std::string& LocalFile, <span class="keyword">const</span> std::string& DestPath, <a class="code" href="classsf_1_1Ftp.htm#1cd6b89ad23253f6d97e6d4ca4d558cb" title="Enumeration of transfer modes.">TransferMode</a> Mode) <a name="l00396"></a>00396 { <a name="l00397"></a>00397 <span class="comment">// Get the contents of the file to send</span> <a name="l00398"></a>00398 std::ifstream File(LocalFile.c_str(), std::ios_base::binary); <a name="l00399"></a>00399 <span class="keywordflow">if</span> (!File) <a name="l00400"></a>00400 <span class="keywordflow">return</span> <a class="code" href="classsf_1_1Ftp_1_1Response.htm" title="This class wraps a FTP response, which is basically :a status codea message.">Response</a>(<a class="code" href="classsf_1_1Ftp_1_1Response.htm#f81738f06b6f571761696291276acb3b835ed0e55eac77c5ebf33b9be7507935" title="Invalid file to upload / download.">Response::InvalidFile</a>); <a name="l00401"></a>00401 File.seekg(0, std::ios::end); <a name="l00402"></a>00402 std::size_t Length = File.tellg(); <a name="l00403"></a>00403 File.seekg(0, std::ios::beg); <a name="l00404"></a>00404 std::vector<char> FileData(Length); <a name="l00405"></a>00405 File.read(&FileData[0], static_cast<std::streamsize>(Length)); <a name="l00406"></a>00406 <a name="l00407"></a>00407 <span class="comment">// Extract the filename from the file path</span> <a name="l00408"></a>00408 std::string Filename = LocalFile; <a name="l00409"></a>00409 std::string::size_type Pos = Filename.find_last_of(<span class="stringliteral">"/\\"</span>); <a name="l00410"></a>00410 <span class="keywordflow">if</span> (Pos != std::string::npos) <a name="l00411"></a>00411 Filename = Filename.substr(Pos + 1); <a name="l00412"></a>00412 <a name="l00413"></a>00413 <span class="comment">// Make sure the destination path ends with a slash</span> <a name="l00414"></a>00414 std::string Path = DestPath; <a name="l00415"></a>00415 <span class="keywordflow">if</span> (!Path.empty() && (Path[Path.size() - 1] != <span class="charliteral">'\\'</span>) && (Path[Path.size() - 1] != <span class="charliteral">'/'</span>)) <a name="l00416"></a>00416 Path += <span class="stringliteral">"/"</span>; <a name="l00417"></a>00417 <a name="l00418"></a>00418 <span class="comment">// Open a data channel using the given transfer mode</span> <a name="l00419"></a>00419 DataChannel Data(*<span class="keyword">this</span>); <a name="l00420"></a>00420 <a class="code" href="classsf_1_1Ftp_1_1Response.htm" title="This class wraps a FTP response, which is basically :a status codea message.">Response</a> Resp = Data.Open(Mode); <a name="l00421"></a>00421 <span class="keywordflow">if</span> (Resp.<a class="code" href="classsf_1_1Ftp_1_1Response.htm#a04c49e1f6a19c5b879838d8d5f3c134" title="Convenience function to check if the response status code means a success.">IsOk</a>()) <a name="l00422"></a>00422 { <a name="l00423"></a>00423 <span class="comment">// Tell the server to start the transfer</span> <a name="l00424"></a>00424 Resp = SendCommand(<span class="stringliteral">"STOR"</span>, Path + Filename); <a name="l00425"></a>00425 <span class="keywordflow">if</span> (Resp.<a class="code" href="classsf_1_1Ftp_1_1Response.htm#a04c49e1f6a19c5b879838d8d5f3c134" title="Convenience function to check if the response status code means a success.">IsOk</a>()) <a name="l00426"></a>00426 { <a name="l00427"></a>00427 <span class="comment">// Send the file data</span> <a name="l00428"></a>00428 Data.Send(FileData); <a name="l00429"></a>00429 <a name="l00430"></a>00430 <span class="comment">// Get the response from the server</span> <a name="l00431"></a>00431 Resp = GetResponse(); <a name="l00432"></a>00432 } <a name="l00433"></a>00433 } <a name="l00434"></a>00434 <a name="l00435"></a>00435 <span class="keywordflow">return</span> Resp; <a name="l00436"></a>00436 } <a name="l00437"></a>00437 <a name="l00438"></a>00438 <a name="l00442"></a>00442 <a class="code" href="classsf_1_1Ftp_1_1Response.htm" title="This class wraps a FTP response, which is basically :a status codea message.">Ftp::Response</a> Ftp::SendCommand(<span class="keyword">const</span> std::string& Command, <span class="keyword">const</span> std::string& Parameter) <a name="l00443"></a>00443 { <a name="l00444"></a>00444 <span class="comment">// Build the command string</span> <a name="l00445"></a>00445 std::string CommandStr; <a name="l00446"></a>00446 <span class="keywordflow">if</span> (Parameter != <span class="stringliteral">""</span>) <a name="l00447"></a>00447 CommandStr = Command + <span class="stringliteral">" "</span> + Parameter + <span class="stringliteral">"\r\n"</span>; <a name="l00448"></a>00448 <span class="keywordflow">else</span> <a name="l00449"></a>00449 CommandStr = Command + <span class="stringliteral">"\r\n"</span>; <a name="l00450"></a>00450 <a name="l00451"></a>00451 <span class="comment">// Send it to the server</span> <a name="l00452"></a>00452 <span class="keywordflow">if</span> (myCommandSocket.<a class="code" href="classsf_1_1SocketTCP.htm#4debed568966f75e3e63a70f08e67c21" title="Send an array of bytes to the host (must be connected first).">Send</a>(CommandStr.c_str(), CommandStr.length()) != sf::Socket::Done) <a name="l00453"></a>00453 <span class="keywordflow">return</span> Response(<a class="code" href="classsf_1_1Ftp_1_1Response.htm#f81738f06b6f571761696291276acb3b4eb0af3b6dc0d9d54834c183143950e6" title="Connection with server closed.">Response::ConnectionClosed</a>); <a name="l00454"></a>00454 <a name="l00455"></a>00455 <span class="comment">// Get the response</span> <a name="l00456"></a>00456 <span class="keywordflow">return</span> GetResponse(); <a name="l00457"></a>00457 } <a name="l00458"></a>00458 <a name="l00459"></a>00459 <a name="l00464"></a>00464 Ftp::Response Ftp::GetResponse() <a name="l00465"></a>00465 { <a name="l00466"></a>00466 <span class="comment">// We'll use a variable to keep track of the last valid code.</span> <a name="l00467"></a>00467 <span class="comment">// It is useful in case of multi-lines responses, because the end of such a response</span> <a name="l00468"></a>00468 <span class="comment">// will start by the same code</span> <a name="l00469"></a>00469 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> LastCode = 0; <a name="l00470"></a>00470 <span class="keywordtype">bool</span> IsInsideMultiline = <span class="keyword">false</span>; <a name="l00471"></a>00471 std::string Message; <a name="l00472"></a>00472 <a name="l00473"></a>00473 <span class="keywordflow">for</span> (;;) <a name="l00474"></a>00474 { <a name="l00475"></a>00475 <span class="comment">// Receive the response from the server</span> <a name="l00476"></a>00476 <span class="keywordtype">char</span> Buffer[1024]; <a name="l00477"></a>00477 std::size_t Length; <a name="l00478"></a>00478 <span class="keywordflow">if</span> (myCommandSocket.Receive(Buffer, <span class="keyword">sizeof</span>(Buffer), Length) != sf::Socket::Done) <a name="l00479"></a>00479 <span class="keywordflow">return</span> Response(<a class="code" href="classsf_1_1Ftp_1_1Response.htm#f81738f06b6f571761696291276acb3b4eb0af3b6dc0d9d54834c183143950e6" title="Connection with server closed.">Response::ConnectionClosed</a>); <a name="l00480"></a>00480 <a name="l00481"></a>00481 <span class="comment">// There can be several lines inside the received buffer, extract them all</span> <a name="l00482"></a>00482 std::istringstream In(std::string(Buffer, Length), std::ios_base::binary); <a name="l00483"></a>00483 <span class="keywordflow">while</span> (In) <a name="l00484"></a>00484 { <a name="l00485"></a>00485 <span class="comment">// Try to extract the code</span> <a name="l00486"></a>00486 <span class="keywordtype">unsigned</span> <span class="keywordtype">int</span> Code; <a name="l00487"></a>00487 <span class="keywordflow">if</span> (In >> Code) <a name="l00488"></a>00488 { <a name="l00489"></a>00489 <span class="comment">// Extract the separator</span> <a name="l00490"></a>00490 <span class="keywordtype">char</span> Sep; <a name="l00491"></a>00491 In.get(Sep); <a name="l00492"></a>00492 <a name="l00493"></a>00493 <span class="comment">// The '-' character means a multiline response</span> <a name="l00494"></a>00494 <span class="keywordflow">if</span> ((Sep == <span class="charliteral">'-'</span>) && !IsInsideMultiline) <a name="l00495"></a>00495 { <a name="l00496"></a>00496 <span class="comment">// Set the multiline flag</span> <a name="l00497"></a>00497 IsInsideMultiline = <span class="keyword">true</span>; <a name="l00498"></a>00498 <a name="l00499"></a>00499 <span class="comment">// Keep track of the code</span> <a name="l00500"></a>00500 <span class="keywordflow">if</span> (LastCode == 0) <a name="l00501"></a>00501 LastCode = Code; <a name="l00502"></a>00502 <a name="l00503"></a>00503 <span class="comment">// Extract the line</span> <a name="l00504"></a>00504 std::getline(In, Message); <a name="l00505"></a>00505 <a name="l00506"></a>00506 <span class="comment">// Remove the ending '\r' (all lines are terminated by "\r\n")</span> <a name="l00507"></a>00507 Message.erase(Message.length() - 1); <a name="l00508"></a>00508 Message = Sep + Message + <span class="stringliteral">"\n"</span>; <a name="l00509"></a>00509 } <a name="l00510"></a>00510 <span class="keywordflow">else</span> <a name="l00511"></a>00511 { <a name="l00512"></a>00512 <span class="comment">// We must make sure that the code is the same, otherwise it means</span> <a name="l00513"></a>00513 <span class="comment">// we haven't reached the end of the multiline response</span> <a name="l00514"></a>00514 <span class="keywordflow">if</span> ((Sep != <span class="charliteral">'-'</span>) && ((Code == LastCode) || (LastCode == 0))) <a name="l00515"></a>00515 { <a name="l00516"></a>00516 <span class="comment">// Clear the multiline flag</span> <a name="l00517"></a>00517 IsInsideMultiline = <span class="keyword">false</span>; <a name="l00518"></a>00518 <a name="l00519"></a>00519 <span class="comment">// Extract the line</span> <a name="l00520"></a>00520 std::string Line; <a name="l00521"></a>00521 std::getline(In, Line); <a name="l00522"></a>00522 <a name="l00523"></a>00523 <span class="comment">// Remove the ending '\r' (all lines are terminated by "\r\n")</span> <a name="l00524"></a>00524 Line.erase(Line.length() - 1); <a name="l00525"></a>00525 <a name="l00526"></a>00526 <span class="comment">// Append it to the message</span> <a name="l00527"></a>00527 <span class="keywordflow">if</span> (Code == LastCode) <a name="l00528"></a>00528 { <a name="l00529"></a>00529 std::ostringstream Out; <a name="l00530"></a>00530 Out << Code << Sep << Line; <a name="l00531"></a>00531 Message += Out.str(); <a name="l00532"></a>00532 } <a name="l00533"></a>00533 <span class="keywordflow">else</span> <a name="l00534"></a>00534 { <a name="l00535"></a>00535 Message = Sep + Line; <a name="l00536"></a>00536 } <a name="l00537"></a>00537 <a name="l00538"></a>00538 <span class="comment">// Return the response code and message</span> <a name="l00539"></a>00539 <span class="keywordflow">return</span> Response(static_cast<Response::Status>(Code), Message); <a name="l00540"></a>00540 } <a name="l00541"></a>00541 <span class="keywordflow">else</span> <a name="l00542"></a>00542 { <a name="l00543"></a>00543 <span class="comment">// The line we just read was actually not a response,</span> <a name="l00544"></a>00544 <span class="comment">// only a new part of the current multiline response</span> <a name="l00545"></a>00545 <a name="l00546"></a>00546 <span class="comment">// Extract the line</span> <a name="l00547"></a>00547 std::string Line; <a name="l00548"></a>00548 std::getline(In, Line); <a name="l00549"></a>00549 <a name="l00550"></a>00550 <span class="keywordflow">if</span> (!Line.empty()) <a name="l00551"></a>00551 { <a name="l00552"></a>00552 <span class="comment">// Remove the ending '\r' (all lines are terminated by "\r\n")</span> <a name="l00553"></a>00553 Line.erase(Line.length() - 1); <a name="l00554"></a>00554 <a name="l00555"></a>00555 <span class="comment">// Append it to the current message</span> <a name="l00556"></a>00556 std::ostringstream Out; <a name="l00557"></a>00557 Out << Code << Sep << Line << <span class="stringliteral">"\n"</span>; <a name="l00558"></a>00558 Message += Out.str(); <a name="l00559"></a>00559 } <a name="l00560"></a>00560 } <a name="l00561"></a>00561 } <a name="l00562"></a>00562 } <a name="l00563"></a>00563 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (LastCode != 0) <a name="l00564"></a>00564 { <a name="l00565"></a>00565 <span class="comment">// It seems we are in the middle of a multiline response</span> <a name="l00566"></a>00566 <a name="l00567"></a>00567 <span class="comment">// Clear the error bits of the stream</span> <a name="l00568"></a>00568 In.clear(); <a name="l00569"></a>00569 <a name="l00570"></a>00570 <span class="comment">// Extract the line</span> <a name="l00571"></a>00571 std::string Line; <a name="l00572"></a>00572 std::getline(In, Line); <a name="l00573"></a>00573 <a name="l00574"></a>00574 <span class="keywordflow">if</span> (!Line.empty()) <a name="l00575"></a>00575 { <a name="l00576"></a>00576 <span class="comment">// Remove the ending '\r' (all lines are terminated by "\r\n")</span> <a name="l00577"></a>00577 Line.erase(Line.length() - 1); <a name="l00578"></a>00578 <a name="l00579"></a>00579 <span class="comment">// Append it to the current message</span> <a name="l00580"></a>00580 Message += Line + <span class="stringliteral">"\n"</span>; <a name="l00581"></a>00581 } <a name="l00582"></a>00582 } <a name="l00583"></a>00583 <span class="keywordflow">else</span> <a name="l00584"></a>00584 { <a name="l00585"></a>00585 <span class="comment">// Error : cannot extract the code, and we are not in a multiline response</span> <a name="l00586"></a>00586 <span class="keywordflow">return</span> Response(<a class="code" href="classsf_1_1Ftp_1_1Response.htm#f81738f06b6f571761696291276acb3b68ea252a23f9da1a7caac3e8cd79b45b" title="Response is not a valid FTP one.">Response::InvalidResponse</a>); <a name="l00587"></a>00587 } <a name="l00588"></a>00588 } <a name="l00589"></a>00589 } <a name="l00590"></a>00590 <a name="l00591"></a>00591 <span class="comment">// We never reach there</span> <a name="l00592"></a>00592 } <a name="l00593"></a>00593 <a name="l00594"></a>00594 <a name="l00598"></a>00598 Ftp::DataChannel::DataChannel(Ftp& Owner) : <a name="l00599"></a>00599 myFtp(Owner) <a name="l00600"></a>00600 { <a name="l00601"></a>00601 <a name="l00602"></a>00602 } <a name="l00603"></a>00603 <a name="l00604"></a>00604 <a name="l00608"></a>00608 Ftp::DataChannel::~DataChannel() <a name="l00609"></a>00609 { <a name="l00610"></a>00610 <span class="comment">// Close the data socket</span> <a name="l00611"></a>00611 myDataSocket.Close(); <a name="l00612"></a>00612 } <a name="l00613"></a>00613 <a name="l00614"></a>00614 <a name="l00618"></a>00618 Ftp::Response Ftp::DataChannel::Open(<a class="code" href="classsf_1_1Ftp.htm#1cd6b89ad23253f6d97e6d4ca4d558cb" title="Enumeration of transfer modes.">Ftp::TransferMode</a> Mode) <a name="l00619"></a>00619 { <a name="l00620"></a>00620 <span class="comment">// Open a data connection in active mode (we connect to the server)</span> <a name="l00621"></a>00621 Ftp::Response Resp = myFtp.SendCommand(<span class="stringliteral">"PASV"</span>); <a name="l00622"></a>00622 <span class="keywordflow">if</span> (Resp.IsOk()) <a name="l00623"></a>00623 { <a name="l00624"></a>00624 <span class="comment">// Extract the connection address and port from the response</span> <a name="l00625"></a>00625 std::string::size_type begin = Resp.GetMessage().find_first_of(<span class="stringliteral">"0123456789"</span>); <a name="l00626"></a>00626 <span class="keywordflow">if</span> (begin != std::string::npos) <a name="l00627"></a>00627 { <a name="l00628"></a>00628 sf::Uint8 Data[6] = {0, 0, 0, 0, 0, 0}; <a name="l00629"></a>00629 std::string Str = Resp.GetMessage().substr(begin); <a name="l00630"></a>00630 std::size_t Index = 0; <a name="l00631"></a>00631 <span class="keywordflow">for</span> (<span class="keywordtype">int</span> i = 0; i < 6; ++i) <a name="l00632"></a>00632 { <a name="l00633"></a>00633 <span class="comment">// Extract the current number</span> <a name="l00634"></a>00634 <span class="keywordflow">while</span> (isdigit(Str[Index])) <a name="l00635"></a>00635 { <a name="l00636"></a>00636 Data[i] = Data[i] * 10 + (Str[Index] - <span class="charliteral">'0'</span>); <a name="l00637"></a>00637 Index++; <a name="l00638"></a>00638 } <a name="l00639"></a>00639 <a name="l00640"></a>00640 <span class="comment">// Skip separator</span> <a name="l00641"></a>00641 Index++; <a name="l00642"></a>00642 } <a name="l00643"></a>00643 <a name="l00644"></a>00644 <span class="comment">// Reconstruct connection port and address</span> <a name="l00645"></a>00645 <span class="keywordtype">unsigned</span> <span class="keywordtype">short</span> Port = Data[4] * 256 + Data[5]; <a name="l00646"></a>00646 <a class="code" href="classsf_1_1IPAddress.htm" title="IPAddress provides easy manipulation of IP v4 addresses.">sf::IPAddress</a> Address(static_cast<sf::Uint8>(Data[0]), <a name="l00647"></a>00647 static_cast<sf::Uint8>(Data[1]), <a name="l00648"></a>00648 static_cast<sf::Uint8>(Data[2]), <a name="l00649"></a>00649 static_cast<sf::Uint8>(Data[3])); <a name="l00650"></a>00650 <a name="l00651"></a>00651 <span class="comment">// Connect the data channel to the server</span> <a name="l00652"></a>00652 <span class="keywordflow">if</span> (myDataSocket.Connect(Port, Address) == Socket::Done) <a name="l00653"></a>00653 { <a name="l00654"></a>00654 <span class="comment">// Translate the transfer mode to the corresponding FTP parameter</span> <a name="l00655"></a>00655 std::string ModeStr; <a name="l00656"></a>00656 <span class="keywordflow">switch</span> (Mode) <a name="l00657"></a>00657 { <a name="l00658"></a>00658 <span class="keywordflow">case</span> <a class="code" href="classsf_1_1Ftp.htm#1cd6b89ad23253f6d97e6d4ca4d558cb6f253b362639fb5e059dc292762a21ee" title="Binary mode (file is transfered as a sequence of bytes).">Ftp::Binary</a> : ModeStr = <span class="stringliteral">"I"</span>; <span class="keywordflow">break</span>; <a name="l00659"></a>00659 <span class="keywordflow">case</span> <a class="code" href="classsf_1_1Ftp.htm#1cd6b89ad23253f6d97e6d4ca4d558cbc9e544a22dce8ef3177449cb235d15c2" title="Text mode using ASCII encoding.">Ftp::Ascii</a> : ModeStr = <span class="stringliteral">"A"</span>; <span class="keywordflow">break</span>; <a name="l00660"></a>00660 <span class="keywordflow">case</span> <a class="code" href="classsf_1_1Ftp.htm#1cd6b89ad23253f6d97e6d4ca4d558cbbb1e34435231e73c96534c71090be7f4" title="Text mode using EBCDIC encoding.">Ftp::Ebcdic</a> : ModeStr = <span class="stringliteral">"E"</span>; <span class="keywordflow">break</span>; <a name="l00661"></a>00661 } <a name="l00662"></a>00662 <a name="l00663"></a>00663 <span class="comment">// Set the transfer mode</span> <a name="l00664"></a>00664 Resp = myFtp.SendCommand(<span class="stringliteral">"TYPE"</span>, ModeStr); <a name="l00665"></a>00665 } <a name="l00666"></a>00666 <span class="keywordflow">else</span> <a name="l00667"></a>00667 { <a name="l00668"></a>00668 <span class="comment">// Failed to connect to the server</span> <a name="l00669"></a>00669 Resp = Ftp::Response(<a class="code" href="classsf_1_1Ftp_1_1Response.htm#f81738f06b6f571761696291276acb3b1193dea18ab5169792489796e29a154c" title="Connection with server failed.">Ftp::Response::ConnectionFailed</a>); <a name="l00670"></a>00670 } <a name="l00671"></a>00671 } <a name="l00672"></a>00672 } <a name="l00673"></a>00673 <a name="l00674"></a>00674 <span class="keywordflow">return</span> Resp; <a name="l00675"></a>00675 } <a name="l00676"></a>00676 <a name="l00677"></a>00677 <a name="l00681"></a>00681 <span class="keywordtype">void</span> Ftp::DataChannel::Receive(std::vector<char>& Data) <a name="l00682"></a>00682 { <a name="l00683"></a>00683 <span class="comment">// Receive data</span> <a name="l00684"></a>00684 Data.clear(); <a name="l00685"></a>00685 <span class="keywordtype">char</span> Buffer[1024]; <a name="l00686"></a>00686 std::size_t Received; <a name="l00687"></a>00687 <span class="keywordflow">while</span> (myDataSocket.Receive(Buffer, <span class="keyword">sizeof</span>(Buffer), Received) == sf::Socket::Done) <a name="l00688"></a>00688 { <a name="l00689"></a>00689 std::copy(Buffer, Buffer + Received, std::back_inserter(Data)); <a name="l00690"></a>00690 } <a name="l00691"></a>00691 <a name="l00692"></a>00692 <span class="comment">// Close the data socket</span> <a name="l00693"></a>00693 myDataSocket.Close(); <a name="l00694"></a>00694 } <a name="l00695"></a>00695 <a name="l00696"></a>00696 <a name="l00700"></a>00700 <span class="keywordtype">void</span> Ftp::DataChannel::Send(<span class="keyword">const</span> std::vector<char>& Data) <a name="l00701"></a>00701 { <a name="l00702"></a>00702 <span class="comment">// Send data</span> <a name="l00703"></a>00703 myDataSocket.Send(&Data[0], Data.size()); <a name="l00704"></a>00704 <a name="l00705"></a>00705 <span class="comment">// Close the data socket</span> <a name="l00706"></a>00706 myDataSocket.Close(); <a name="l00707"></a>00707 } <a name="l00708"></a>00708 <a name="l00709"></a>00709 } <span class="comment">// namespace sf</span> </pre></div></div> <p id="footer"> :: Copyright © 2007-2008 Laurent Gomila, all rights reserved :: Documentation generated by <a href="http://www.doxygen.org/" title="doxygen website">doxygen 1.5.2</a> :: </p> </body> </html>