Click here to Skip to main content
15,893,588 members
Articles / Desktop Programming / MFC

FTP Client Class

Rate me:
Please Sign up or sign in to vote.
4.85/5 (80 votes)
8 Dec 2012CPOL4 min read 776.2K   48.2K   246  
A non-MFC class to encapsulate the FTP protocol.
#include "stdafx.h"
#include "FTPProtocolOutput.h"
#include "Definements.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

using namespace nsFTP;
using namespace nsFTP::nsView;

BEGIN_MESSAGE_MAP(CFTPProtocolOutput, CRichEditCtrl)
END_MESSAGE_MAP()

CFTPProtocolOutput::CFTPProtocolOutput()
{
}

CFTPProtocolOutput::~CFTPProtocolOutput()
{
}

DWORD CALLBACK CFTPProtocolOutput::ProtocolStreamInCallback(DWORD_PTR dwCookie, LPBYTE pbBuff, LONG cb, LONG *pcb)
{
   CMemFile* pMf = reinterpret_cast<CMemFile*>(dwCookie);
   *pcb = pMf->Read(pbBuff, cb);
   return 0;
}

void CFTPProtocolOutput::OnSendCommand(const tstring& strCommand)
{
   if( strCommand.length()==0 )
      return;

   if( strCommand.length()>4 && strCommand.substr(5)==_T("PASS ") )
      WriteLine(_T("< PASS **********\n"), RGB(0, 0, 255));
   else
      WriteLine(_T("> ") + CString(strCommand.c_str()) + _T("\n"), RGB(0, 0, 255));
}

void CFTPProtocolOutput::OnResponse(const tstring& strResponse)
{
   if( strResponse.length()==0 )
      return;

   COLORREF crText = RGB(0, 150, 0);
   switch( strResponse.at(0) )
   {
   case _T('4'):
      crText = RGB(200, 200, 0);
      break;
   case _T('5'):
      crText = RGB(255, 0, 0);
      break;
   }

   WriteLine(_T("< ") + CString(strResponse.c_str()) + _T("\n"), crText);
}

void CFTPProtocolOutput::OnInternalError(const tstring& strErrorMsg, const tstring& strFileName, DWORD dwLineNr)
{
   CString cszMsg;
   cszMsg.Format(_T("%s ==> Datei \"%s\" (%d)"), strErrorMsg.c_str(), strFileName.c_str(), dwLineNr);
   WriteLine(cszMsg, RGB(255, 0, 0));
}

void CFTPProtocolOutput::WriteLine(const CString& cszLine, COLORREF crText)
{
   if( m_hWnd )
   {
      CHARFORMAT cf;
      cf.dwMask    = CFM_BOLD|CFM_COLOR;
      cf.dwEffects = CFE_BOLD;
      cf.crTextColor = crText;

      CMemFile mf;
      mf.Write(nsHelper::CCnv::ConvertToString(tstring(cszLine)).c_str(), cszLine.GetLength());
      mf.SeekToBegin();
   
      SetSelectionCharFormat(cf);

      EDITSTREAM es;
      es.dwCookie = reinterpret_cast<DWORD_PTR>(&mf);
      es.pfnCallback = CFTPProtocolOutput::ProtocolStreamInCallback; 
      StreamIn(SF_TEXT|SFF_SELECTION, es);
      SetSel(-1, -1);
      LineScroll(1);
   }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions