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

📄 vmpathmaker.cpp

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

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

    Description: This class knows how to create paths on local hosts or on
                 already mapped drives.

                      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 "VMPathMaker.h"



/*****************************************************************************/
/*
     FUNCTION NAME:  VMPathMaker::VMPathMaker

       DESCRIPTION:  ctor

             INPUT:  void
            OUTPUT:  none

           RETURNS:  none
*/
VMPathMaker::VMPathMaker( void )
{
}
/* End of function "VMPathMaker::VMPathMaker"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  VMPathMaker::~VMPathMaker

       DESCRIPTION:  dtor. if this allocated a network connection, then the
                     dtor must free the network connection before this goes
                     away

             INPUT:  void
            OUTPUT:  none

           RETURNS:  none
*/
VMPathMaker::~VMPathMaker( void )
{
}
/* End of function "VMPathMaker::~VMPathMaker"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  VMPathMaker::CreatePath

       DESCRIPTION:  creates a path on a local host (or a fully qualified remote
                     host) see Create above which calls this after a network
                     drive has been allocated

             INPUT:  pchFullPath - the path to make
            OUTPUT:  none

           RETURNS:  true if successful, false otherwise
*/
bool VMPathMaker::CreatePath( const char* pchFullPath )
{
  char  achCurPath[ MAX_PATH + 1 ];
   
  strcpy( m_achFullPath, pchFullPath );
  m_bIsGood = true;

  if ( !GetCurrentDirectory( MAX_PATH, achCurPath ) )
  {
    m_bIsGood = false;
    return( m_bIsGood );
  }

  m_bIsGood = ( TRUE == CheckForOrMakeDir( pchFullPath ) );

  if ( !SetCurrentDirectory( pchFullPath ) )
  {
    SetCurrentDirectory( achCurPath );
    m_bIsGood = false;
    return( m_bIsGood );
  }
  SetCurrentDirectory( achCurPath );
  return( m_bIsGood );
}
/* End of function "VMPathMaker::CreatePath"
/*****************************************************************************/


/*****************************************************************************/
/*
     FUNCTION NAME:  VMPathMaker::CreateFilePath

       DESCRIPTION:  creates a file path on a local host (or a fully qualified 
                     remote host)

             INPUT:  pchFullPath - the file path to make
            OUTPUT:  none

           RETURNS:  true if successful, false otherwise
*/
bool VMPathMaker::CreateFilePath( const char* pchFullPath )
{
  HANDLE hFile = NULL;

  char   achPath    [ _MAX_PATH  + 1 ];
  char   achCurPath [ _MAX_PATH  + 1 ];
  char   achDrive   [ _MAX_DRIVE + 1 ];
  char   achDir     [ _MAX_DIR   + 1 ];
  char   achFname   [ _MAX_FNAME + 1 ];
  char   achExt     [ _MAX_EXT   + 1 ];
	
  strcpy( m_achFullPath, pchFullPath );
  m_bIsGood = true;

  if ( !GetCurrentDirectory( _MAX_PATH, achCurPath ) )
  {
    m_bIsGood = false;
    return( m_bIsGood );
  }

  // set up the full path to pszfilename 
  //
  _splitpath( m_achFullPath, achDrive, achDir, achFname, achExt );

  strcpy( achPath, achDrive );
  strcat( achPath, achDir );
  m_bIsGood = ( TRUE == CheckForOrMakeDir( achPath ) );

  if ( !SetCurrentDirectory( achPath ) )
  {
    SetCurrentDirectory( achCurPath );
    m_bIsGood = false;
    return( m_bIsGood );
  }

  strcat( achPath, achFname );
  strcat( achPath, achExt );

  hFile = CreateFile( achPath, 
                      GENERIC_WRITE | GENERIC_READ,
                      0,  // exclusive access
                      NULL,
                      OPEN_ALWAYS,
                      FILE_ATTRIBUTE_NORMAL,
                      NULL ); 

  if ( INVALID_HANDLE_VALUE == hFile )
  {
    m_bIsGood = false;
  }
  CloseHandle( hFile );
  SetCurrentDirectory( achCurPath );
  return( m_bIsGood );
}
/* End of function "VMPathMaker::CreateFilePath"
/*****************************************************************************/


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

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

             INPUT: pchDirectory - the target directory

           RETURNS: true if successful, false otherwise	
*/
short VMPathMaker::CheckForOrMakeDir( const char* pchDirectory )
{
  char*               pchToken;
  char                achDir[ _MAX_DIR + 1 ];
  char                achTemp[ _MAX_DIR + 50 ];
  char                achSeps[] = "\\";
  SECURITY_ATTRIBUTES xSecAttr;
	
  xSecAttr.nLength              = sizeof( xSecAttr );
  xSecAttr.lpSecurityDescriptor = NULL;
  xSecAttr.bInheritHandle       = false;

  // check and make sure they have a drive indicater
  //
  if ( pchDirectory[1] != ':' )
  {
    return( false );
  }

  // make a local copy of the dir for tokenizing
  //
  strcpy( achDir, pchDirectory );
  achTemp[ 0 ] = '\0';

  // tokenize the directory building a copy and checking each subdir
  //
  pchToken = strtok( achDir, achSeps );

  while ( pchToken )
  {
    strcat( achTemp, pchToken );
    AddBackSlash( achTemp );
    if ( ::SetCurrentDirectory( achTemp ) == false )
    {
      if ( ::CreateDirectory( achTemp, &xSecAttr ) == false )
      {
        return( false );
      }
      if ( ::SetCurrentDirectory( achTemp ) == false )
      {
        return( false );
      }
    }
    pchToken = strtok( NULL, achSeps );
  }
  return( true );
}
/*	end of function "VMPathMaker::CheckForOrMakeDir" */
/*****************************************************************************/
	

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

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

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

           RETURNS: void	
*/
void VMPathMaker::AddBackSlash( char* pchOutput )
{
  short iLength;

  iLength = strlen( pchOutput );

  if ( iLength )
  {
    // add a \ if needed
    //
    if ( pchOutput[ iLength - 1 ] != '\\' )
    {
      strcat( pchOutput, "\\" );
    }
  }
}
/*	end of function "VMPathMaker::AddBackSlash" */
/*****************************************************************************/



/*****************************************************************************/
/* Check-in history */
/*
*$Log: $
*/
/*****************************************************************************/


⌨️ 快捷键说明

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