⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vmcopytree.cpp

📁 TOOL (Tiny Object Oriented Language) is an easily-embedded, object-oriented, C++-like-language inter
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*****************************************************************************/
/*                             SOURCE FILE                                   */
/*****************************************************************************/
/*
       $Archive: $

      $Revision: $
          $Date: $
        $Author: $

   Description: The class VMCopyTree support the copy of a directory tree from 
                one location to another.

                      TOOL And XML FORMS License
                      ==========================

                      Except where otherwise noted, all of the documentation 
                      and software included in the TOOL package is 
                      copyrighted by Michael Swartzendruber.

                      Copyright (C) 2005 Michael John Swartzendruber. 
                      All rights reserved.

                      Access to this code, whether intentional or accidental,
                      does NOT IMPLY any transfer of rights.

                      This software is provided "as-is," without any express 
                      or implied warranty. In no event shall the author be held
                      liable for any damages arising from the use of this software.

                      Permission is granted to anyone to use this software for 
                      any purpose, including commercial applications, and to 
                      alter and redistribute it, provided that the following 
                      conditions are met:

                      1. All redistributions of source code files must retain 
                         all copyright notices that are currently in place, 
                         and this list of conditions without modification.

                      2. The origin of this software must not be misrepresented;
                         you must not claim that you wrote the original software.

                      3. If you use this software in another product, an acknowledgment
                         in the product documentation would be appreciated but is
                         not required.

                      4. Modified versions in source or binary form must be plainly 
                         marked as such, and must not be misrepresented as being 
                         the original software.
*/
static char OBJECT_ID[] = "$Revision: $ : $Date: $";
/*****************************************************************************/


#include "../../../stdafx.h"
#include "../Collections/VMStringList.h"
#include "VMCopyTree.h"


/*****************************************************************************/
/*
     FUNCTION NAME: CheckForOrMakeDir

       DESCRIPTION: Confirms the existance of (or creates) a directory on
                    on the destination path	

             INPUT: lpszDir - the target directory
                    psDirs - pointer to the linked list class of created
                             directories

           RETURNS: a variety of coded values based on success of various
                    failure modes	
*/
short VMCopyTree::CheckForOrMakeDir( const char* lpszDir, VMStringList* pdsDirs)
{
  char    *lpToken;
  char    szDir[MAX_PATH], szTemp[MAX_PATH+50];
  char    szSeps[] = "\\";
  short   nResult;
	SECURITY_ATTRIBUTES sSecAttr;
	
	sSecAttr.nLength              = sizeof(sSecAttr);
	sSecAttr.lpSecurityDescriptor = NULL;
	sSecAttr.bInheritHandle       = false;

  // check and make sure a drive indicater is in the string
  //
  if (lpszDir[1] != ':')
		return false;

	// make a local copy of the dir for tokenizing
  //
	strcpy(szDir, lpszDir);
	szTemp[0] = '\0';

	// tokenize the directory building a copy and checking each subdir
  //
	lpToken = strtok(szDir, szSeps);

	while ( lpToken )
	{
		strcat( szTemp, lpToken );
    AddBackSlash( szTemp );
		if ( ::SetCurrentDirectory( szTemp ) == false )
		{
			if ( ::CreateDirectory( szTemp, &sSecAttr ) == false )
				return false;
			if ( ::SetCurrentDirectory( szTemp ) == false )
				return false;

			// add this directory to list of created dirs
      //
			if ( pdsDirs )
        if ( ( nResult = pdsDirs->Add( szTemp ) ) != VMStringList::success )
					return nResult;
		}
		lpToken = strtok( NULL, szSeps );
	}
	return true;
}
/*	end of function "CheckForOrMakeDir" */
/*****************************************************************************/
	

/*****************************************************************************/
/*
     FUNCTION NAME: MyCopyFile

       DESCRIPTION: Moves a file but also looks for user input from the user
                    during the file copy.	

             INPUT: lpszSource - the full path of the file to copy from
                    lpszDest - the full path of the target for the copy

           RETURNS: true on success, false otherwise	
*/
bool VMCopyTree::MyCopyFile(LPSTR lpszSource, LPSTR lpszDest, bool bFail)
{
   if ( !CopyFile( lpszSource, lpszDest, false ) )
   {
      // attempt to get more errors out of the system
      char   achBuffer[255];
      LPVOID lpMsgBuffer;
      DWORD  dwErrCode = GetLastError();

      if ( 0 != dwErrCode )
      {        
         FormatMessage ( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
                         NULL,
                         dwErrCode,
                         MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
                         (LPTSTR) &lpMsgBuffer,
                         0,
                         NULL );
         wsprintf (achBuffer, "Error: '%d' was returned from the system", dwErrCode );
         MessageBox( NULL, achBuffer, "AutoLoad", MB_ICONHAND | MB_OK );
         if ( strlen( (const char*)lpMsgBuffer ) )
             MessageBox( NULL, (const char*)lpMsgBuffer, "AutoLoad", MB_ICONHAND | MB_OK );
         LocalFree( lpMsgBuffer );
      }
      return false;
   }
   return true;
}
/*	end of function "MyCopyFile" */
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME: VMCopyTree

       DESCRIPTION: ctor	

             INPUT: none

           RETURNS: none
*/
VMCopyTree::VMCopyTree()
{
}
/*	end of function "VMCopyTree" */
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME: UpDir

       DESCRIPTION: Sets the desination string up one directory	

             INPUT: sz - the destination directory string

           RETURNS: void	
*/
void VMCopyTree::UpDir( LPSTR sz )
{
	for ( int i = strlen(sz) - 1; i > 0; i-- )
  {
		if ( sz[i] == '\\' )
		{
			sz[i] = '\0';
			return;
		}
  }
	return;
}
/*	end of function "Updir" */
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME: AddBackSlash

       DESCRIPTION: Check end of string and add a backshash if one does not 
                    exist	

             INPUT: sz - a pointer to the string to add the '\' to

           RETURNS: void	
*/
void VMCopyTree::AddBackSlash( LPSTR sz )
{
    short nLen;

	// add a \ if needed
	nLen = strlen(sz);

	if ( nLen )
	{
		if ( sz[nLen - 1] != '\\' )
			strcat( sz, "\\" );
	}
}
/*	end of function "AddBackSlash" */
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME: Copy

       DESCRIPTION: Performs the copy	

             INPUT: lpcszSoure - the source directory
                    lpcszDest - the destination directory
                    lpcszCurrent - the current directory
                    pFrame - pointer to the parent window to this 

           RETURNS:	
*/
void VMCopyTree::Copy( const char* lpcszSource, const char* lpcszDest, 
                     const char* lpcszCurrent, const char* lpcszPrevInst, 
                     bool bDeleteSource )
{
	strcpy( m_achDest, lpcszDest );
	strcpy( m_achSource, lpcszSource );
    
  // MJS: added support for previous install dir
  //
  strcpy( m_achPrevInstDir, lpcszPrevInst );

	if ( SetCurrentDirectory( m_achSource ) == false )
	{
		m_iStatus = false;
		return;
	}

	// start status as SUCCESS, if anything fails while walking the tree
	// the status will be set and walk will return up the tree
  //
	m_iStatus = true;

	Walk(0);
  CleanUp();

  if ( bDeleteSource )
     DeleteSourceTree();

	// replace the current working dir
  //
	SetCurrentDirectory( lpcszCurrent );
}
/*	end of function "Copy" */
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME: Walk

       DESCRIPTION: finds a subdirectory in the current working directory,
                    changes the current working directory to this sub-
                    directory, and recusively calls itself until there are 
                    no more subdirectories

          COMMENTS: When a new directory is entered from above, a handle for
                    the new directory is obtained using FindFirstDirectory.
                    Once the first directory is found, the current working 
                    directory is changed to this first directory and Walk() 
                    is recursively called again.  At this point, the next 
                    available directory is searched for using FindNextFile, 
                    entered and a recursive call is made to Walk().  When 
                    each directory has been searched, until no more direc-
                    tories exist, the current working directory is changed 
                    to the parent directory (..).  This continues until
                    the current working directory is equal to the original
                    directory.	

             INPUT: wLevel - bookmark, when wLevel is greater than 0, then 
                    the current working directory is a subdirectory of the 
                    original directory.  If wLevel is equal to 0, then the
                    directory is the original directory and the recursive 
                    calls stop

           RETURNS: void
*/
void VMCopyTree::Walk( WORD wLevel )
{
	bool             bRC = true;
	DWORD            dwRC;
  short 			     nReturn;
	HANDLE           hSearch;
	WIN32_FIND_DATA  w32FindBuf;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -