//////////////////////////////////////////////////////////////////////
// SplitPath.cpp : implementation of the CSplitPath class. //
// (c) 1999, Kevin Lussier //
//////////////////////////////////////////////////////////////////////
/// \file
/// \brief CSplitPath implementation
#include "StdAfx.h"
#include "SplitPath.h"
namespace Riverblade
{
namespace Libraries
{
namespace Utils
{
////////////////////////////////////////////////////////////////////////////////
// CSplitPath
/// \brief Main constructor.
///
/// \param lpszPath A pointer to a NULL terminated string holding the pathname to split
///
CSplitPath::CSplitPath(LPCTSTR const lpszPath)
{
// Initialize our objects
::memset( szComputer, 0, sizeof( szComputer ) );
::memset( szPath, 0, sizeof( szPath ) );
::memset( szDrive, 0, sizeof( szDrive ) );
::memset( szDir, 0, sizeof( szDir ) );
::memset( szFilename, 0, sizeof( szFilename ) );
::memset( szExtension, 0, sizeof( szExtension ) );
// If we were given a path, split it
if (NULL != lpszPath)
{
ATLVERIFY(Split(lpszPath) );
}
}
/// \brief Class destructor.
///
CSplitPath::~CSplitPath(void)
{
}
/// \brief Split the given pathname.
///
/// \param lpszPath A pointer to a NULL terminated string holding the pathname to split
/// \return \em TRUE if the path was split successfully; \em FALSE otherwise.
///
bool CSplitPath::Split(LPCTSTR const lpszPath)
{
// If we weren't given a path, fail
if (NULL == lpszPath)
{
// Return failure
return false;
}
#if (_MSC_VER >= 1400)
// VS2005 or later
::_tcsncpy_s(szPath, _MAX_PATH, lpszPath, ::_tcslen(lpszPath) );
// Split the given path
::_tsplitpath_s(szPath,
szDrive,
_MAX_DRIVE,
szDir,
_MAX_DIR,
szFilename,
_MAX_FNAME,
szExtension,
_MAX_EXT);
#else
// VS2003
// Copy the path
(void)::_tcsncpy( szPath, lpszPath, sizeof(szPath) - 1);
// Split the given path
::_tsplitpath(szPath, szDrive, szDir, szFilename, szExtension);
#endif
// If UNC name
// PathIsUNC() suggested by coghlans@technologist.com on the LintProject CP forum [Anna 19.11.2004]
if (::PathIsUNC(lpszPath) )
{
#if (_MSC_VER >= 1400)
// VS2005
//
// Get the computername
(void)::_tcsncpy_s(szComputer, _MAX_FNAME, _tcsstr(szDir, _T("\\\\") ) + 2, _MAX_DIR - 1);
TCHAR* pch = _tcsstr(szComputer, _T("\\") );
if (NULL != pch)
{
*pch = '\0';
}
// Strip the computername from the directory
(void)::_tcsncpy_s(szDir, _MAX_DIR, _tcsstr(szDir, _T("\\\\") ) + 2, _MAX_DIR - 1);
(void)::_tcsncpy_s(szDir, _MAX_DIR, _tcsstr(szDir, _T("\\") ), _MAX_DIR - 1);
#else
// VS2003
//
// Get the computername
(void)::_tcsncpy(szComputer, _tcsstr(szDir, _T("\\\\") ) + 2, _MAX_FNAME - 1);
(void)::_tcsnset(_tcsstr(szComputer, _T("\\") ), 0, 1);
// Strip the computername from the directory
(void)::_tcsncpy(szDir, _tcsstr(szDir, _T("\\\\") ) + 2, _MAX_DIR - 1);
(void)::_tcsncpy(szDir, _tcsstr(szDir, _T("\\") ), _MAX_DIR -1);
#endif
}
return true;
}
} // namespace Utils
} // namespace Libraries
} // namespace Riverblade