home *** CD-ROM | disk | FTP | other *** search
/ Mastering MFC Development / MMD.ISO / labs / c11 / lab03 / baseline / ftpget.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-20  |  5.4 KB  |  242 lines

  1. // FtpGet.cpp : implementation file
  2. //
  3.  
  4.  
  5. #include "stdafx.h"
  6. #include "afxinet.h"
  7.  
  8. #include <direct.h>        // for _mkdir()
  9. #include <errno.h>
  10. #include <time.h>
  11.  
  12. #include "ftp.h"
  13. #include "FtpView.h"
  14. #include "FtpGet.h"
  15.  
  16.  
  17. #ifdef _DEBUG
  18. #define new DEBUG_NEW
  19. #undef THIS_FILE
  20. static char THIS_FILE[] = __FILE__;
  21. #endif
  22.  
  23. /////////////////////////////////////////////////////////////////////////////
  24. // CFtpGet
  25.  
  26. CFtpGet::CFtpGet()
  27. {
  28. }
  29. CFtpGet::CFtpGet(CFtpView* pView, CString& Server, CString& FileTypes, CString& Path, CString& LocalRoot, CString& Gateway, int CopyType)
  30. {
  31.     m_ftp= NULL;
  32.     m_TotalFileSize= 0;
  33.  
  34.     m_strServer= Server;
  35.     m_strFileTypes= FileTypes;
  36.     m_strPath= Path;
  37.     m_strLocalRoot= LocalRoot;
  38.     m_strGateway= Gateway;
  39.     m_CopyType= CopyType;
  40.  
  41.     m_pView= pView;
  42. }
  43.  
  44. CFtpGet::~CFtpGet()
  45. {
  46. }
  47.  
  48. /////////////////////////////////////////////////////
  49.  
  50. ///////////////////////////////////////////////////////////////////////
  51. // DoFtpTransfer()
  52. //    Initiate an FTP session, perform the operation, close up.
  53. void CFtpGet::DoFtpTransfer()
  54. {
  55.         try 
  56.         {
  57.             //
  58.             // 1) Create an Internet session
  59.             //
  60.  
  61.             //
  62.             // 2) Establish FTP connection
  63.             //
  64.  
  65.             //
  66.             // 3) Some miscellaneous initialization
  67.             //
  68.  
  69.             //
  70.             // 4) Begin file transfer starting at root of FTP server.
  71.             //
  72.  
  73.             //
  74.             // 5) Close things up
  75.             //    
  76.  
  77.         } 
  78.         catch (CInternetException* pIE) 
  79.         {
  80.             char msg[255];
  81.             pIE->GetErrorMessage(msg, 255);
  82.             AfxMessageBox(msg);
  83.             pIE->Delete();
  84.         } 
  85.         catch (CException* pIE) 
  86.         {
  87.             char msg[255];
  88.             pIE->GetErrorMessage(msg, 255);
  89.             AfxMessageBox(msg);
  90.             pIE->Delete();
  91.         }
  92.  
  93. }
  94.  
  95. ///////////////////////////////////////////////////////////////////////
  96. // CopyDirectory()
  97. //    Main function to do the copy of matched filenames.
  98. //    
  99. BOOL CFtpGet::CopyDirectory(int PathLevel)
  100. {
  101.  
  102.     //
  103.     // 0) Initialization
  104.     //
  105.  
  106.     //
  107.     // 1) Find desired files for current directory and copy.
  108.     //
  109.  
  110.     //
  111.     // 2) Terminate here if at end of path specification.
  112.     //
  113.  
  114.     //
  115.     // 3) Start a new search looking for sub-directory names only.
  116.     //
  117.  
  118.     //
  119.     // 4) Recursively copy matching sub-directories.
  120.     //
  121.  
  122.     return TRUE;
  123. }
  124. ///////////////////////////////////////////////////////////////////////
  125. //
  126. //    Utility functions
  127. //
  128. ///////////////////////////////////////////////////////////////////////
  129. // PathToArray()
  130. // Split path string into an array of strings, one per path element.
  131. void CFtpGet::PathToArray(CString& path, CStringArray& strArray)
  132. {
  133.     char* pSep= "\\/";    // either works as a separator.
  134.     // copy path string into a char array for strtok.
  135.     char* pStr= new char[path.GetLength()+1];
  136.     strcpy(pStr, path);
  137.     // Get first path element.
  138.     char* pPtr= strtok(pStr,pSep);
  139.     // Extract path elements until there are no more.
  140.     while (pPtr != NULL) 
  141.     {
  142.         strArray.Add(pPtr);
  143.         pPtr= strtok(NULL, pSep);
  144.     }
  145.     delete pStr;
  146. }
  147. ///////////////////////////////////////////////////////////////////////
  148. // BuildPath()
  149. // Given a level, build up a path from string array of path elements.
  150. // Note: level 0 means append zero elements
  151. void CFtpGet::BuildPath(int level, CString& path, CString& sep)
  152. {
  153.     ASSERT(level<=m_arrDynamicPath.GetSize());
  154.     for (int idx= 0; idx < level; idx++) 
  155.     {
  156.         path += m_arrDynamicPath[idx];
  157.         path += sep;
  158.     }
  159. }
  160. ///////////////////////////////////////////////////////////////////////
  161. // CreateLocalDirectory()
  162. // Create a directory path on local filesystem.
  163. // The passed path string is appended to the local root directory string.
  164. BOOL CFtpGet::CreateLocalDirectory(CString& path, CString& FullDir)
  165. {
  166.     CString msg;
  167.     // Attempt to create destination dir if not existing
  168.     if (_chdir(m_strLocalRoot)!=0)
  169.     {
  170.         msg.Format("Create %s?", m_strLocalRoot); 
  171.         if (AfxMessageBox(msg, MB_OKCANCEL) == IDOK)
  172.         {
  173.             if (_mkdir(m_strLocalRoot)!=0) 
  174.             {
  175.                 CString err;
  176.                 err.Format("Failed to create %s", m_strLocalRoot);
  177.                 m_pView->AddToView(err);
  178.                 return FALSE;
  179.             }
  180.         }
  181.         else
  182.             return FALSE;
  183.     }
  184.  
  185.     CStringArray strArray;
  186.     // Separate path name into path elements.
  187.     PathToArray(path, strArray);
  188.     // Start with the local root directory.
  189.     FullDir= m_strLocalRoot;
  190.     for (int idx=0; idx< strArray.GetSize(); idx++) 
  191.     {
  192.         FullDir += strArray[idx];
  193.         FullDir += "\\";
  194.         if (_mkdir(FullDir)!=0) 
  195.         {
  196.             if (errno != EEXIST) 
  197.             {    // continue only if directory exists.
  198.                 CString err;
  199.                 err.Format("mkdir error on %s: %d", FullDir, errno);
  200.                 m_pView->AddToView(err);
  201.                 return FALSE;
  202.             }
  203.         } 
  204.         else 
  205.         {
  206.             msg.Format("Creating new directory: %s", FullDir);
  207.             m_pView->AddToView(msg);
  208.         }
  209.     }
  210.     return TRUE;
  211. }
  212.  
  213. ///////////////////////////////////////////////////////////////////////
  214. // SetFtpDirectory()
  215. //    Set remote FTP directory based upon given level.
  216. BOOL CFtpGet::SetFtpDirectory(int level)
  217. {
  218.     CString strPath("/");
  219.     CString sep("/");
  220.     BuildPath(level, strPath, sep);
  221.     CString msg;
  222.     msg.Format("Searching Dir: %s", strPath);
  223.     m_pView->AddToView(msg);
  224.     return m_ftp->SetCurrentDirectory(strPath);
  225. }
  226. ///////////////////////////////////////////////////////////////////////
  227. // GetLocalDirectory()
  228. //    Given level, return string of local path. 
  229. //    Create path on filesytem if it does not exists.
  230. BOOL CFtpGet::GetLocalDirectory(int level, CString& FullDir)
  231. {
  232.     CString strPath;
  233.     CString sep("\\");
  234.     BuildPath(level, strPath, sep);
  235.     // Create it if necessary.
  236.     if (!CreateLocalDirectory(strPath, FullDir)) 
  237.     {
  238.         return FALSE;
  239.     }
  240.     return TRUE;
  241. }
  242.