📄 path.cpp
字号:
ZeroMemory(buff_dir, sizeof(buff_dir));
ZeroMemory(buff_name, sizeof(buff_name));
ZeroMemory(buff_ext, sizeof(buff_ext));
SPLITPATH(m_strPath.c_str(),
pDrive ? buff_drive : NULL,
pDirectory ? buff_dir : NULL,
pName ? buff_name : NULL,
pExtension ? buff_ext : NULL);
if(pDrive)
*pDrive =buff_drive;
if(pDirectory)
*pDirectory =buff_dir;
if(pName)
*pName =buff_name;
if(pExtension)
*pExtension =buff_ext;
// DOS's _splitpath returns "d:", we return "d"
if(pDrive)
StripTrailingChar(*pDrive,DRIVE_DELIMITER);
// DOS's _splitpath returns "\dir\subdir\", we return "\dir\subdir"
if(pDirectory)
StripTrailingBackslash(*pDirectory);
// DOS's _splitpath returns ".ext", we return "ext"
if(pExtension)
StripLeadingChar(*pExtension,EXTENSION_DELIMITER);
}
//-------------------------------------------------------------
// Pre :
// Post :
// Globals :
// I/O :
// Task : Get drive from path
//-------------------------------------------------------------
void CPath::GetDrive(string& rDrive) const
{
GetComponents(&rDrive);
}
//-------------------------------------------------------------
// Pre :
// Post :
// Globals :
// I/O :
// Task : Get drive and directory from path
//-------------------------------------------------------------
void CPath::GetDriveDirectory(string& rDriveDirectory) const
{
string Drive;
string Directory;
GetComponents(&Drive,&Directory);
rDriveDirectory =Drive;
if(!Drive.empty())
{
rDriveDirectory += DRIVE_DELIMITER;
rDriveDirectory += Directory;
}
}
//-------------------------------------------------------------
// Pre :
// Post :
// Globals :
// I/O :
// Task : Get directory from path
//-------------------------------------------------------------
void CPath::GetDirectory(string& rDirectory) const
{
GetComponents(NULL,&rDirectory);
}
//-------------------------------------------------------------
// Pre :
// Post :
// Globals :
// I/O :
// Task : Get filename and extension from path
//-------------------------------------------------------------
void CPath::GetNameExtension(string& rNameExtension) const
{
string Name;
string Extension;
GetComponents(NULL,NULL,&Name,&Extension);
rNameExtension =Name;
if(!Extension.empty())
{
rNameExtension += EXTENSION_DELIMITER;
rNameExtension += Extension;
}
}
//-------------------------------------------------------------
// Pre :
// Post :
// Globals :
// I/O :
// Task : Get filename from path
//-------------------------------------------------------------
void CPath::GetName(string& rName) const
{
GetComponents(NULL,NULL,&rName);
}
//-------------------------------------------------------------
// Pre :
// Post :
// Globals :
// I/O :
// Task : Get file extension from path
//-------------------------------------------------------------
void CPath::GetExtension(string& rExtension) const
{
GetComponents(NULL,NULL,NULL,&rExtension);
}
//-------------------------------------------------------------
// Pre :
// Post :
// Globals :
// I/O :
// Task : Get fully qualified path
//-------------------------------------------------------------
void CPath::GetFullyQualified(string& rFullyQualified) const
{
TCHAR buff_fullname[MAX_PATH];
FULLPATH(buff_fullname,m_strPath.c_str(),MAX_PATH-1);
rFullyQualified =buff_fullname;
}
//-------------------------------------------------------------
// Pre :
// Post :
// Globals :
// I/O :
// Task : Get fully qualified path in short (8.3 style) form
//-------------------------------------------------------------
void CPath::GetFullyQualifiedShort(string& rFullyQualifiedShort) const
{
GetFullyQualified(rFullyQualifiedShort);
#pragma message(Reminder(_T("Also implement a GetFullyQualifiedLong")))
TCHAR buff_fullname[MAX_PATH];
GetShortPathName(rFullyQualifiedShort.c_str(),buff_fullname,sizeof(buff_fullname)/sizeof(TCHAR));
rFullyQualifiedShort =buff_fullname;
}
//-------------------------------------------------------------
// Pre :
// Post : Return TRUE if path does not start from filesystem root
// Globals :
// I/O :
// Task : Check if path is a relative one (e.g. doesn't start with C:\...)
//-------------------------------------------------------------
BOOL CPath::IsRelative() const
{
return (m_strPath.find(DRIVE_DELIMITER) == string::npos);
}
//-------------------------------------------------------------
// Pre :
// Post : Return TRUE if there are wildcards in the path
// Globals :
// I/O :
// Task : Check if path contains wildcards
//-------------------------------------------------------------
BOOL CPath::IsWild() const
{
return (m_strPath.find_first_of(WILD_SET) != string::npos);
}
//-------------------------------------------------------------
// Pre :
// Post : Return TRUE if path is lexically correct
// Globals :
// I/O :
// Task : Determine whether lpszFileName is valid. A filename
// is valid if it contains only legal characters, doesn't
// have repeated contiguous subdirectory delimiters, has at
// most one drive delimiter, and all components fit within
// maximum sizes
// This routine does NOT determine if a file exists, or
// even if it could exist relative to the user's directory
// hierarchy. Its tests are for lexical correctness only
// See also: CPath::Exists
//-------------------------------------------------------------
BOOL CPath::IsValid () const
{
// Check 4 illegal characters (no wildcards allowed)
// We accept either \ or / as folder delimiter
if(IsWild() ||
(m_strPath.find_first_of(_T("|\"<>")) != string::npos) ||
(m_strPath.find(_T("//")) != string::npos))
return FALSE;
int index =m_strPath.find(_T("\\\\"));
if((index != string::npos) && (index > 0))
return FALSE;
// Make sure : can appear only in the 2nd position as a drive delimiter
if(((index =m_strPath.find(':')) != string::npos) && (index != 1))
return FALSE;
// Make sure it fits in the maximum path size
if(m_strPath.length() > MAX_PATH)
return FALSE;
// Path is valid
return TRUE;
}
//-------------------------------------------------------------
// Pre :
// Post :
// Globals :
// I/O :
// Task : Set path components
//-------------------------------------------------------------
void CPath::SetComponents(LPCTSTR lpszDrive,
LPCTSTR lpszDirectory,
LPCTSTR lpszName,
LPCTSTR lpszExtension)
{
TCHAR buff_fullname[MAX_PATH];
MAKEPATH(buff_fullname,lpszDrive,lpszDirectory,lpszName,lpszExtension);
m_strPath.erase();
m_strPath =buff_fullname;
}
//-------------------------------------------------------------
// Pre :
// Post :
// Globals :
// I/O :
// Task : Set path's drive
//-------------------------------------------------------------
void CPath::SetDrive(TCHAR chDrive)
{
string Drive(1,chDrive);
string Directory;
string Name;
string Extension;
GetComponents(NULL,&Directory,&Name,&Extension);
SetComponents(Drive.c_str(),Directory.c_str(),Name.c_str(),Extension.c_str());
}
//-------------------------------------------------------------
// Pre :
// Post :
// Globals :
// I/O :
// Task : Set path's directory
//-------------------------------------------------------------
void CPath::SetDirectory(LPCTSTR lpszDirectory, BOOL bEnsureAbsolute /*= FALSE*/)
{
string Drive;
string Directory =lpszDirectory;
string Name;
string Extension;
if(bEnsureAbsolute)
EnsureLeadingBackslash(Directory);
EnsureTrailingBackslash(Directory);
GetComponents(&Drive,NULL,&Name,&Extension);
SetComponents(Drive.c_str(),Directory.c_str(),Name.c_str(),Extension.c_str());
}
//-------------------------------------------------------------
// Pre :
// Post :
// Globals :
// I/O :
// Task : Set path's drive and directory
//-------------------------------------------------------------
void CPath::SetDriveDirectory(LPCTSTR lpszDriveDirectory)
{
string DriveDirectory =lpszDriveDirectory;
string Name;
string Extension;
EnsureTrailingBackslash(DriveDirectory);
GetComponents(NULL,NULL,&Name,&Extension);
SetComponents(NULL,DriveDirectory.c_str(),Name.c_str(),Extension.c_str());
}
//-------------------------------------------------------------
// Pre :
// Post :
// Globals :
// I/O :
// Task : Set path's filename
//-------------------------------------------------------------
void CPath::SetName(LPCTSTR lpszName)
{
string Drive;
string Directory;
string Extension;
GetComponents(&Drive,&Directory,NULL,&Extension);
SetComponents(Drive.c_str(),Directory.c_str(),lpszName,Extension.c_str());
}
//-------------------------------------------------------------
// Pre :
// Post :
// Globals :
// I/O :
// Task : Set path's file extension
//-------------------------------------------------------------
void CPath::SetExtension(LPCTSTR lpszExtension)
{
string Drive;
string Directory;
string Name;
GetComponents(&Drive,&Directory,&Name);
SetComponents(Drive.c_str(),Directory.c_str(),Name.c_str(),lpszExtension);
}
//-------------------------------------------------------------
// Pre :
// Post :
// Globals :
// I/O :
// Task : Set path's filename and extension
//-------------------------------------------------------------
void CPath::SetNameExtension(LPCTSTR lpszNameExtension)
{
string Drive;
string Directory;
GetComponents(&Drive,&Directory);
SetComponents(Drive.c_str(),Directory.c_str(),lpszNameExtension,NULL);
}
//-------------------------------------------------------------
// Pre :
// Post :
// Globals :
// I/O :
// Task : Append a subdirectory 2 path's directory
//-------------------------------------------------------------
void CPath::AppendDirectory(LPCTSTR lpszSubDirectory)
{
string Drive;
string Directory;
string SubDirectory =lpszSubDirectory;
string Name;
string Extension;
if(SubDirectory.empty())
return;
// Strip out any preceeding backslash
StripLeadingBackslash(SubDirectory);
EnsureTrailingBackslash(SubDirectory);
GetComponents(&Drive,&Directory,&Name,&Extension);
EnsureTrailingBackslash(Directory);
Directory +=SubDirectory;
SetComponents(Drive.c_str(),Directory.c_str(),Name.c_str(),Extension.c_str());
}
//-------------------------------------------------------------
// Pre : If pLastDirectory is given we will store the name of the
// deepest directory (the one we're just exiting) in it
// Post :
// Globals :
// I/O :
// Task : Remove deepest subdirectory from path
//-------------------------------------------------------------
void CPath::UpDirectory(string *pLastDirectory /*= NULL*/)
{
string Directory;
GetDirectory(Directory);
StripTrailingBackslash(Directory);
if(Directory.empty())
return;
string::size_type nDelimiter =Directory.rfind(DIRECTORY_DELIMITER);
if(pLastDirectory != NULL)
{
*pLastDirectory =Directory.substr(nDelimiter);
StripLeadingBackslash(*pLastDirectory);
}
if(nDelimiter != string::npos)
Directory =Directory.substr(0,nDelimiter);
SetDirectory(Directory.c_str());
}
//-------------------------------------------------------------
// Pre :
// Post :
// Globals :
// I/O :
// Task : Set path 2 current directory
//-------------------------------------------------------------
void CPath::CurrentDirectory()
{
TCHAR buff_path[MAX_PATH];
GetCurrentDirectory(MAX_PATH,buff_path);
Empty();
SetDriveDirectory(buff_path);
}
//-------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -