home *** CD-ROM | disk | FTP | other *** search
- // FtpGet.cpp : implementation file
- //
-
-
- #include "stdafx.h"
- #include "afxinet.h"
-
- #include <direct.h> // for _mkdir()
- #include <errno.h>
- #include <time.h>
-
- #include "ftp.h"
- #include "FtpView.h"
- #include "FtpGet.h"
-
-
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
-
- /////////////////////////////////////////////////////////////////////////////
- // CFtpGet
-
- CFtpGet::CFtpGet()
- {
- }
- CFtpGet::CFtpGet(CFtpView* pView, CString& Server, CString& FileTypes, CString& Path, CString& LocalRoot, CString& Gateway, int CopyType)
- {
- m_ftp= NULL;
- m_TotalFileSize= 0;
-
- m_strServer= Server;
- m_strFileTypes= FileTypes;
- m_strPath= Path;
- m_strLocalRoot= LocalRoot;
- m_strGateway= Gateway;
- m_CopyType= CopyType;
-
- m_pView= pView;
- }
-
- CFtpGet::~CFtpGet()
- {
- }
-
- /////////////////////////////////////////////////////
-
- ///////////////////////////////////////////////////////////////////////
- // DoFtpTransfer()
- // Initiate an FTP session, perform the operation, close up.
- void CFtpGet::DoFtpTransfer()
- {
- try
- {
- //
- // 1) Create an Internet session
- //
-
- //
- // 2) Establish FTP connection
- //
-
- //
- // 3) Some miscellaneous initialization
- //
-
- //
- // 4) Begin file transfer starting at root of FTP server.
- //
-
- //
- // 5) Close things up
- //
-
- }
- catch (CInternetException* pIE)
- {
- char msg[255];
- pIE->GetErrorMessage(msg, 255);
- AfxMessageBox(msg);
- pIE->Delete();
- }
- catch (CException* pIE)
- {
- char msg[255];
- pIE->GetErrorMessage(msg, 255);
- AfxMessageBox(msg);
- pIE->Delete();
- }
-
- }
-
- ///////////////////////////////////////////////////////////////////////
- // CopyDirectory()
- // Main function to do the copy of matched filenames.
- //
- BOOL CFtpGet::CopyDirectory(int PathLevel)
- {
-
- //
- // 0) Initialization
- //
-
- //
- // 1) Find desired files for current directory and copy.
- //
-
- //
- // 2) Terminate here if at end of path specification.
- //
-
- //
- // 3) Start a new search looking for sub-directory names only.
- //
-
- //
- // 4) Recursively copy matching sub-directories.
- //
-
- return TRUE;
- }
- ///////////////////////////////////////////////////////////////////////
- //
- // Utility functions
- //
- ///////////////////////////////////////////////////////////////////////
- // PathToArray()
- // Split path string into an array of strings, one per path element.
- void CFtpGet::PathToArray(CString& path, CStringArray& strArray)
- {
- char* pSep= "\\/"; // either works as a separator.
- // copy path string into a char array for strtok.
- char* pStr= new char[path.GetLength()+1];
- strcpy(pStr, path);
- // Get first path element.
- char* pPtr= strtok(pStr,pSep);
- // Extract path elements until there are no more.
- while (pPtr != NULL)
- {
- strArray.Add(pPtr);
- pPtr= strtok(NULL, pSep);
- }
- delete pStr;
- }
- ///////////////////////////////////////////////////////////////////////
- // BuildPath()
- // Given a level, build up a path from string array of path elements.
- // Note: level 0 means append zero elements
- void CFtpGet::BuildPath(int level, CString& path, CString& sep)
- {
- ASSERT(level<=m_arrDynamicPath.GetSize());
- for (int idx= 0; idx < level; idx++)
- {
- path += m_arrDynamicPath[idx];
- path += sep;
- }
- }
- ///////////////////////////////////////////////////////////////////////
- // CreateLocalDirectory()
- // Create a directory path on local filesystem.
- // The passed path string is appended to the local root directory string.
- BOOL CFtpGet::CreateLocalDirectory(CString& path, CString& FullDir)
- {
- CString msg;
- // Attempt to create destination dir if not existing
- if (_chdir(m_strLocalRoot)!=0)
- {
- msg.Format("Create %s?", m_strLocalRoot);
- if (AfxMessageBox(msg, MB_OKCANCEL) == IDOK)
- {
- if (_mkdir(m_strLocalRoot)!=0)
- {
- CString err;
- err.Format("Failed to create %s", m_strLocalRoot);
- m_pView->AddToView(err);
- return FALSE;
- }
- }
- else
- return FALSE;
- }
-
- CStringArray strArray;
- // Separate path name into path elements.
- PathToArray(path, strArray);
- // Start with the local root directory.
- FullDir= m_strLocalRoot;
- for (int idx=0; idx< strArray.GetSize(); idx++)
- {
- FullDir += strArray[idx];
- FullDir += "\\";
- if (_mkdir(FullDir)!=0)
- {
- if (errno != EEXIST)
- { // continue only if directory exists.
- CString err;
- err.Format("mkdir error on %s: %d", FullDir, errno);
- m_pView->AddToView(err);
- return FALSE;
- }
- }
- else
- {
- msg.Format("Creating new directory: %s", FullDir);
- m_pView->AddToView(msg);
- }
- }
- return TRUE;
- }
-
- ///////////////////////////////////////////////////////////////////////
- // SetFtpDirectory()
- // Set remote FTP directory based upon given level.
- BOOL CFtpGet::SetFtpDirectory(int level)
- {
- CString strPath("/");
- CString sep("/");
- BuildPath(level, strPath, sep);
- CString msg;
- msg.Format("Searching Dir: %s", strPath);
- m_pView->AddToView(msg);
- return m_ftp->SetCurrentDirectory(strPath);
- }
- ///////////////////////////////////////////////////////////////////////
- // GetLocalDirectory()
- // Given level, return string of local path.
- // Create path on filesytem if it does not exists.
- BOOL CFtpGet::GetLocalDirectory(int level, CString& FullDir)
- {
- CString strPath;
- CString sep("\\");
- BuildPath(level, strPath, sep);
- // Create it if necessary.
- if (!CreateLocalDirectory(strPath, FullDir))
- {
- return FALSE;
- }
- return TRUE;
- }
-