From 7b747cb5e0d8163b5529dcb0f993533400a6a23f Mon Sep 17 00:00:00 2001 From: zilmar Date: Tue, 12 Jan 2016 17:52:59 +1100 Subject: [PATCH] [Project64] Cleanup path.cpp --- Source/Common/path.cpp | 948 ++++++++++++++++++++--------------------- Source/Common/path.h | 182 ++++---- 2 files changed, 563 insertions(+), 567 deletions(-) diff --git a/Source/Common/path.cpp b/Source/Common/path.cpp index 2c2e96c03..b74b4ac5a 100644 --- a/Source/Common/path.cpp +++ b/Source/Common/path.cpp @@ -13,13 +13,13 @@ const char * const DLL_EXTENSION = "dll"; const char * const INI_EXTENSION = "ini"; const char * const EXE_EXTENSION = "exe"; const char * const WILD_NAME_EXTENSION = "*.*"; -const TCHAR WILD_ONE = '?'; -const TCHAR WILD_ANY = '*'; -const char * const WILD_SET = "?*"; -const char * const DIR_DOUBLEDELIM = "\\\\"; -const TCHAR DRIVE_DELIMITER = ':'; -const TCHAR DIRECTORY_DELIMITER = '\\'; -const TCHAR EXTENSION_DELIMITER = '.'; +const TCHAR WILD_ONE = '?'; +const TCHAR WILD_ANY = '*'; +const char * const WILD_SET = "?*"; +const char * const DIR_DOUBLEDELIM = "\\\\"; +const TCHAR DRIVE_DELIMITER = ':'; +const TCHAR DIRECTORY_DELIMITER = '\\'; +const TCHAR EXTENSION_DELIMITER = '.'; const TCHAR DIRECTORY_DELIMITER2 = '/'; void * CPath::m_hInst = NULL; @@ -27,14 +27,14 @@ void * CPath::m_hInst = NULL; // Helpers ////////////////////////////////////////////////////////////////////// -void CPath::SethInst ( void * hInst ) +void CPath::SethInst(void * hInst) { - m_hInst = hInst; + m_hInst = hInst; } void * CPath::GethInst() { - return m_hInst; + return m_hInst; } ////////////////////////////////////////////////////////////////////// @@ -42,14 +42,14 @@ void * CPath::GethInst() ////////////////////////////////////////////////////////////////////// //------------------------------------------------------------- -// Task : Helper function for the various CPath constructors. +// Task : Helper function for the various CPath constructors. // Initializes the data members and establishes various // class invariants //------------------------------------------------------------- inline void CPath::Init() { - m_dwFindFileAttributes =0; - m_hFindFile =NULL; + m_dwFindFileAttributes = 0; + m_hFindFile = NULL; } //------------------------------------------------------------- @@ -58,10 +58,10 @@ inline void CPath::Init() //------------------------------------------------------------- inline void CPath::Exit() { - if(m_hFindFile != NULL) + if (m_hFindFile != NULL) { - FindClose(m_hFindFile); - m_hFindFile =NULL; + FindClose(m_hFindFile); + m_hFindFile = NULL; } } @@ -74,7 +74,7 @@ inline void CPath::Exit() //------------------------------------------------------------- CPath::CPath() { - Init(); + Init(); Empty(); } @@ -83,8 +83,8 @@ CPath::CPath() //------------------------------------------------------------- CPath::CPath(const CPath& rPath) { - Init(); - m_strPath =rPath.m_strPath; + Init(); + m_strPath = rPath.m_strPath; } //------------------------------------------------------------- @@ -92,16 +92,16 @@ CPath::CPath(const CPath& rPath) //------------------------------------------------------------- CPath::CPath(const char * lpszPath) { - Init(); - m_strPath =lpszPath ? lpszPath : ""; - cleanPathString(m_strPath); + Init(); + m_strPath = lpszPath ? lpszPath : ""; + cleanPathString(m_strPath); } CPath::CPath(const char * lpszPath, const char * NameExten) { - Init(); - SetDriveDirectory(lpszPath); - SetNameExtension(NameExten); + Init(); + SetDriveDirectory(lpszPath); + SetNameExtension(NameExten); } //------------------------------------------------------------- @@ -110,8 +110,8 @@ CPath::CPath(const char * lpszPath, const char * NameExten) CPath::CPath(const std::string& strPath) { Init(); - m_strPath =strPath; - cleanPathString(m_strPath); + m_strPath = strPath; + cleanPathString(m_strPath); } //------------------------------------------------------------- @@ -119,9 +119,9 @@ CPath::CPath(const std::string& strPath) //------------------------------------------------------------- CPath::CPath(const std::string& strPath, const char * NameExten) { - Init(); - SetDriveDirectory(strPath.c_str()); - SetNameExtension(NameExten); + Init(); + SetDriveDirectory(strPath.c_str()); + SetNameExtension(NameExten); } //------------------------------------------------------------- @@ -129,9 +129,9 @@ CPath::CPath(const std::string& strPath, const char * NameExten) //------------------------------------------------------------- CPath::CPath(const std::string& strPath, const std::string& NameExten) { - Init(); - SetDriveDirectory(strPath.c_str()); - SetNameExtension(NameExten.c_str()); + Init(); + SetDriveDirectory(strPath.c_str()); + SetNameExtension(NameExten.c_str()); } //------------------------------------------------------------- @@ -139,10 +139,9 @@ CPath::CPath(const std::string& strPath, const std::string& NameExten) //------------------------------------------------------------- CPath::~CPath() { - Exit(); + Exit(); } - //------------------------------------------------------------- // Post : Return TRUE if paths are equal // Task : Check if the two path are the same @@ -150,14 +149,14 @@ CPath::~CPath() bool CPath::operator ==(const CPath& rPath) const { // Get fully qualified versions of the paths - std::string FullyQualified1; + std::string FullyQualified1; std::string FullyQualified2; - - GetFullyQualified(FullyQualified1); - rPath.GetFullyQualified(FullyQualified2); - + + GetFullyQualified(FullyQualified1); + rPath.GetFullyQualified(FullyQualified2); + // Compare them - return _stricmp(FullyQualified1.c_str(), FullyQualified2.c_str()) == 0; + return _stricmp(FullyQualified1.c_str(), FullyQualified2.c_str()) == 0; } //------------------------------------------------------------- @@ -173,9 +172,9 @@ bool CPath::operator !=(const CPath& rPath) const // Task : Assign a path 2 another //------------------------------------------------------------- CPath& CPath::operator =(const CPath& rPath) -{ - if(this != &rPath) - m_strPath =rPath.m_strPath; +{ + if (this != &rPath) + m_strPath = rPath.m_strPath; return *this; } @@ -185,7 +184,7 @@ CPath& CPath::operator =(const CPath& rPath) //------------------------------------------------------------- CPath& CPath::operator =(const char * lpszPath) { - m_strPath =lpszPath ? lpszPath : ""; + m_strPath = lpszPath ? lpszPath : ""; return *this; } @@ -195,7 +194,7 @@ CPath& CPath::operator =(const char * lpszPath) //------------------------------------------------------------- CPath& CPath::operator =(const std::string& strPath) { - m_strPath =strPath; + m_strPath = strPath; return *this; } @@ -203,7 +202,7 @@ CPath& CPath::operator =(const std::string& strPath) // Post : Converts path 2 string // Task : Convert path 2 string // Warning: because this pointer 2 string point in the data -// of this class, it is possible 2 cast the result of this +// of this class, it is possible 2 cast the result of this // function in any non-constant pointer and alter the data. // Very dangerous //------------------------------------------------------------- @@ -214,80 +213,80 @@ CPath::operator const char *() const CPath::CPath(DIR_CURRENT_DIRECTORY /*sdt*/, const char * NameExten) { - // Application's current directory - Init(); - CurrentDirectory(); - if (NameExten) { SetNameExtension(NameExten); } + // Application's current directory + Init(); + CurrentDirectory(); + if (NameExten) { SetNameExtension(NameExten); } } CPath::CPath(DIR_MODULE_DIRECTORY /*sdt*/, const char * NameExten) { - // The directory where the executable of this app is - Init(); - ModuleDirectory(); - if (NameExten) { SetNameExtension(NameExten); } + // The directory where the executable of this app is + Init(); + ModuleDirectory(); + if (NameExten) { SetNameExtension(NameExten); } } CPath::CPath(DIR_MODULE_FILE /*sdt*/) { - // The directory where the executable of this app is - Init(); - Module(); + // The directory where the executable of this app is + Init(); + Module(); } //------------------------------------------------------------- // Post : Returns the drive component without a colon, e.g. "c" -// Returns the directory component with a leading backslash, +// Returns the directory component with a leading backslash, // but no trailing backslash, e.g. "\dir\subdir" // Returns name compleletely without delimiters, e.g "letter" // Returns extension completely without delimiters, e.g. "doc" // Globals : // I/O : -// Task : Return the individual components of this path. -// For any given argument, you can pass NULL if you are not +// Task : Return the individual components of this path. +// For any given argument, you can pass NULL if you are not // interested in that component. -// Do not rely on pNames being <= 8 characters, extensions +// Do not rely on pNames being <= 8 characters, extensions // being <= 3 characters, or drives being 1 character //------------------------------------------------------------- -void CPath::GetComponents(std::string* pDrive, - std::string* pDirectory, - std::string* pName, - std::string* pExtension) const +void CPath::GetComponents(std::string* pDrive, + std::string* pDirectory, + std::string* pName, + std::string* pExtension) const { TCHAR buff_drive[_MAX_DRIVE + 1]; - TCHAR buff_dir [_MAX_DIR + 1]; - TCHAR buff_name [_MAX_FNAME + 1]; - TCHAR buff_ext [_MAX_EXT + 1]; + TCHAR buff_dir[_MAX_DIR + 1]; + TCHAR buff_name[_MAX_FNAME + 1]; + TCHAR buff_ext[_MAX_EXT + 1]; - ZeroMemory(buff_drive,sizeof(buff_drive)); - ZeroMemory(buff_dir, sizeof(buff_dir)); + ZeroMemory(buff_drive, sizeof(buff_drive)); + ZeroMemory(buff_dir, sizeof(buff_dir)); ZeroMemory(buff_name, sizeof(buff_name)); - ZeroMemory(buff_ext, sizeof(buff_ext)); + 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; + _splitpath(m_strPath.c_str(), + pDrive ? buff_drive : NULL, + pDirectory ? buff_dir : NULL, + pName ? buff_name : NULL, + pExtension ? buff_ext : NULL); - // 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); + 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); } //------------------------------------------------------------- @@ -298,35 +297,35 @@ void CPath::GetDriveDirectory(std::string& rDriveDirectory) const std::string Drive; std::string Directory; - GetComponents(&Drive,&Directory); - rDriveDirectory =Drive; - if(!Drive.empty()) + GetComponents(&Drive, &Directory); + rDriveDirectory = Drive; + if (!Drive.empty()) { - rDriveDirectory += DRIVE_DELIMITER; + rDriveDirectory += DRIVE_DELIMITER; rDriveDirectory += Directory; } } std::string CPath::GetDriveDirectory(void) const { - std::string rDriveDirectory; - GetDriveDirectory(rDriveDirectory); - return rDriveDirectory; + std::string rDriveDirectory; + GetDriveDirectory(rDriveDirectory); + return rDriveDirectory; } //------------------------------------------------------------- // Task : Get directory from path //------------------------------------------------------------- void CPath::GetDirectory(std::string& rDirectory) const { - GetComponents(NULL,&rDirectory); -} + GetComponents(NULL, &rDirectory); +} std::string CPath::GetDirectory(void) const { - std::string rDirectory; - GetComponents(NULL,&rDirectory); - return rDirectory; -} + std::string rDirectory; + GetComponents(NULL, &rDirectory); + return rDirectory; +} //------------------------------------------------------------- // Task : Get filename and extension from path @@ -336,20 +335,20 @@ void CPath::GetNameExtension(std::string& rNameExtension) const std::string Name; std::string Extension; - GetComponents(NULL,NULL,&Name,&Extension); - rNameExtension =Name; - if(!Extension.empty()) + GetComponents(NULL, NULL, &Name, &Extension); + rNameExtension = Name; + if (!Extension.empty()) { - rNameExtension += EXTENSION_DELIMITER; + rNameExtension += EXTENSION_DELIMITER; rNameExtension += Extension; } } std::string CPath::GetNameExtension(void) const { - std::string rNameExtension; - GetNameExtension(rNameExtension); - return rNameExtension; + std::string rNameExtension; + GetNameExtension(rNameExtension); + return rNameExtension; } //------------------------------------------------------------- @@ -357,14 +356,14 @@ std::string CPath::GetNameExtension(void) const //------------------------------------------------------------- void CPath::GetName(std::string& rName) const { - GetComponents(NULL,NULL,&rName); + GetComponents(NULL, NULL, &rName); } std::string CPath::GetName(void) const { - std::string rName; - GetComponents(NULL,NULL,&rName); - return rName; + std::string rName; + GetComponents(NULL, NULL, &rName); + return rName; } //------------------------------------------------------------- @@ -372,43 +371,43 @@ std::string CPath::GetName(void) const //------------------------------------------------------------- void CPath::GetExtension(std::string& rExtension) const { - GetComponents(NULL,NULL,NULL,&rExtension); -} + GetComponents(NULL, NULL, NULL, &rExtension); +} std::string CPath::GetExtension(void) const { - std::string rExtension; - GetComponents(NULL,NULL,NULL,&rExtension); - return rExtension; -} + std::string rExtension; + GetComponents(NULL, NULL, NULL, &rExtension); + return rExtension; +} //------------------------------------------------------------- // Task : Get current directory //------------------------------------------------------------- void CPath::GetLastDirectory(std::string& rDirectory) const { - std::string Directory; + std::string Directory; + + rDirectory = ""; + + GetDirectory(Directory); + StripTrailingBackslash(Directory); + if (Directory.empty()) + { + return; + } - rDirectory = ""; - - GetDirectory(Directory); - StripTrailingBackslash(Directory); - if(Directory.empty()) - { - return; - } - std::string::size_type nDelimiter = Directory.rfind(DIRECTORY_DELIMITER); - rDirectory = Directory.substr(nDelimiter); - StripLeadingBackslash(rDirectory); -} + rDirectory = Directory.substr(nDelimiter); + StripLeadingBackslash(rDirectory); +} std::string CPath::GetLastDirectory(void) const { - std::string rDirecotry; - GetLastDirectory(rDirecotry); - return rDirecotry; -} + std::string rDirecotry; + GetLastDirectory(rDirecotry); + return rDirecotry; +} //------------------------------------------------------------- // Task : Get fully qualified path @@ -417,45 +416,45 @@ void CPath::GetFullyQualified(std::string& rFullyQualified) const { TCHAR buff_fullname[MAX_PATH]; - memset(buff_fullname, 0, sizeof(buff_fullname)); + memset(buff_fullname, 0, sizeof(buff_fullname)); - _fullpath(buff_fullname, m_strPath.c_str(), MAX_PATH - 1); - rFullyQualified =buff_fullname; + _fullpath(buff_fullname, m_strPath.c_str(), MAX_PATH - 1); + rFullyQualified = buff_fullname; } //------------------------------------------------------------- // Post : Return TRUE if path does not start from filesystem root // Task : Check if path is a relative one (e.g. doesn't start with C:\...) -//------------------------------------------------------------- +//------------------------------------------------------------- bool CPath::IsRelative() const { - if (m_strPath.length() > 1 && m_strPath[1] == DRIVE_DELIMITER) - { - return false; - } - if (m_strPath.length() > 1 && m_strPath[0] == DIRECTORY_DELIMITER && m_strPath[1] == DIRECTORY_DELIMITER) - { - return false; - } - return true; + if (m_strPath.length() > 1 && m_strPath[1] == DRIVE_DELIMITER) + { + return false; + } + if (m_strPath.length() > 1 && m_strPath[0] == DIRECTORY_DELIMITER && m_strPath[1] == DIRECTORY_DELIMITER) + { + return false; + } + return true; } //------------------------------------------------------------- // Task : Set path components //------------------------------------------------------------- -void CPath::SetComponents(const char * lpszDrive, - const char * lpszDirectory, - const char * lpszName, - const char * lpszExtension) +void CPath::SetComponents(const char * lpszDrive, + const char * lpszDirectory, + const char * lpszName, + const char * lpszExtension) { TCHAR buff_fullname[MAX_PATH]; - memset(buff_fullname, 0, sizeof(buff_fullname)); + memset(buff_fullname, 0, sizeof(buff_fullname)); - _makepath(buff_fullname, lpszDrive, lpszDirectory, lpszName, lpszExtension); + _makepath(buff_fullname, lpszDrive, lpszDirectory, lpszName, lpszExtension); m_strPath.erase(); - m_strPath =buff_fullname; + m_strPath = buff_fullname; } //------------------------------------------------------------- @@ -463,13 +462,13 @@ void CPath::SetComponents(const char * lpszDrive, //------------------------------------------------------------- void CPath::SetDrive(TCHAR chDrive) { - stdstr_f Drive("%c",chDrive); - std::string Directory; - std::string Name; - std::string Extension; - - GetComponents(NULL,&Directory,&Name,&Extension); - SetComponents(Drive.c_str(),Directory.c_str(),Name.c_str(),Extension.c_str()); + stdstr_f Drive("%c", chDrive); + std::string Directory; + std::string Name; + std::string Extension; + + GetComponents(NULL, &Directory, &Name, &Extension); + SetComponents(Drive.c_str(), Directory.c_str(), Name.c_str(), Extension.c_str()); } //------------------------------------------------------------- @@ -477,46 +476,46 @@ void CPath::SetDrive(TCHAR chDrive) //------------------------------------------------------------- void CPath::SetDirectory(const char * lpszDirectory, bool bEnsureAbsolute /*= FALSE*/) { - std::string Drive; - std::string Directory =lpszDirectory; - std::string Name; - std::string Extension; - - if(bEnsureAbsolute) - EnsureLeadingBackslash(Directory); - EnsureTrailingBackslash(Directory); + std::string Drive; + std::string Directory = lpszDirectory; + std::string Name; + std::string Extension; - GetComponents(&Drive,NULL,&Name,&Extension); - SetComponents(Drive.c_str(),Directory.c_str(),Name.c_str(),Extension.c_str()); -} + if (bEnsureAbsolute) + EnsureLeadingBackslash(Directory); + EnsureTrailingBackslash(Directory); + + GetComponents(&Drive, NULL, &Name, &Extension); + SetComponents(Drive.c_str(), Directory.c_str(), Name.c_str(), Extension.c_str()); +} //------------------------------------------------------------- // Task : Set path's drive and directory //------------------------------------------------------------- void CPath::SetDriveDirectory(const char * lpszDriveDirectory) { - std::string DriveDirectory =lpszDriveDirectory; - std::string Name; - std::string Extension; - - EnsureTrailingBackslash(DriveDirectory); - cleanPathString(DriveDirectory); - - GetComponents(NULL,NULL,&Name,&Extension); - SetComponents(NULL,DriveDirectory.c_str(),Name.c_str(),Extension.c_str()); -} + std::string DriveDirectory = lpszDriveDirectory; + std::string Name; + std::string Extension; + + EnsureTrailingBackslash(DriveDirectory); + cleanPathString(DriveDirectory); + + GetComponents(NULL, NULL, &Name, &Extension); + SetComponents(NULL, DriveDirectory.c_str(), Name.c_str(), Extension.c_str()); +} //------------------------------------------------------------- // Task : Set path's filename //------------------------------------------------------------- void CPath::SetName(const char * lpszName) { - std::string Drive; - std::string Directory; - std::string Extension; - - GetComponents(&Drive,&Directory,NULL,&Extension); - SetComponents(Drive.c_str(),Directory.c_str(),lpszName,Extension.c_str()); + std::string Drive; + std::string Directory; + std::string Extension; + + GetComponents(&Drive, &Directory, NULL, &Extension); + SetComponents(Drive.c_str(), Directory.c_str(), lpszName, Extension.c_str()); } //------------------------------------------------------------- @@ -524,31 +523,30 @@ void CPath::SetName(const char * lpszName) //------------------------------------------------------------- void CPath::SetName(int iName) { - std::string Drive; - std::string Directory; - std::string Extension; - TCHAR sName[33]; - - memset(sName, 0, sizeof(sName)); + std::string Drive; + std::string Directory; + std::string Extension; + TCHAR sName[33]; - _itoa(iName, sName, 10); - - GetComponents(&Drive,&Directory,NULL,&Extension); - SetComponents(Drive.c_str(),Directory.c_str(),sName,Extension.c_str()); + memset(sName, 0, sizeof(sName)); + + _itoa(iName, sName, 10); + + GetComponents(&Drive, &Directory, NULL, &Extension); + SetComponents(Drive.c_str(), Directory.c_str(), sName, Extension.c_str()); } - //------------------------------------------------------------- // Task : Set path's file extension //------------------------------------------------------------- void CPath::SetExtension(const char * lpszExtension) { - std::string Drive; - std::string Directory; - std::string Name; - - GetComponents(&Drive,&Directory,&Name); - SetComponents(Drive.c_str(),Directory.c_str(),Name.c_str(),lpszExtension); + std::string Drive; + std::string Directory; + std::string Name; + + GetComponents(&Drive, &Directory, &Name); + SetComponents(Drive.c_str(), Directory.c_str(), Name.c_str(), lpszExtension); } //------------------------------------------------------------- @@ -556,17 +554,17 @@ void CPath::SetExtension(const char * lpszExtension) //------------------------------------------------------------- void CPath::SetExtension(int iExtension) { - std::string Drive; - std::string Directory; - std::string Name; - TCHAR sExtension[20]; + std::string Drive; + std::string Directory; + std::string Name; + TCHAR sExtension[20]; - memset(sExtension, 0, sizeof(sExtension)); + memset(sExtension, 0, sizeof(sExtension)); - _itoa(iExtension, sExtension, 10); + _itoa(iExtension, sExtension, 10); - GetComponents(&Drive,&Directory,&Name); - SetComponents(Drive.c_str(),Directory.c_str(),Name.c_str(),sExtension); + GetComponents(&Drive, &Directory, &Name); + SetComponents(Drive.c_str(), Directory.c_str(), Name.c_str(), sExtension); } //------------------------------------------------------------- @@ -574,36 +572,36 @@ void CPath::SetExtension(int iExtension) //------------------------------------------------------------- void CPath::SetNameExtension(const char * lpszNameExtension) { - std::string Drive; - std::string Directory; + std::string Drive; + std::string Directory; - GetComponents(&Drive,&Directory); - SetComponents(Drive.c_str(),Directory.c_str(),lpszNameExtension,NULL); -} + GetComponents(&Drive, &Directory); + SetComponents(Drive.c_str(), Directory.c_str(), lpszNameExtension, NULL); +} //------------------------------------------------------------- // Task : Append a subdirectory 2 path's directory //------------------------------------------------------------- void CPath::AppendDirectory(const char * lpszSubDirectory) -{ - std::string Drive; - std::string Directory; - std::string SubDirectory =lpszSubDirectory; - std::string Name; - std::string Extension; - - if(SubDirectory.empty()) - return; +{ + std::string Drive; + std::string Directory; + std::string SubDirectory = lpszSubDirectory; + std::string Name; + std::string Extension; - // Strip out any preceeding backslash - StripLeadingBackslash(SubDirectory); - EnsureTrailingBackslash(SubDirectory); + if (SubDirectory.empty()) + return; - GetComponents(&Drive,&Directory,&Name,&Extension); - EnsureTrailingBackslash(Directory); - Directory +=SubDirectory; + // Strip out any preceeding backslash + StripLeadingBackslash(SubDirectory); + EnsureTrailingBackslash(SubDirectory); - SetComponents(Drive.c_str(),Directory.c_str(),Name.c_str(),Extension.c_str()); + GetComponents(&Drive, &Directory, &Name, &Extension); + EnsureTrailingBackslash(Directory); + Directory += SubDirectory; + + SetComponents(Drive.c_str(), Directory.c_str(), Name.c_str(), Extension.c_str()); } //------------------------------------------------------------- @@ -613,25 +611,25 @@ void CPath::AppendDirectory(const char * lpszSubDirectory) //------------------------------------------------------------- void CPath::UpDirectory(std::string *pLastDirectory /*= NULL*/) { - std::string Directory; + std::string Directory; - GetDirectory(Directory); - StripTrailingBackslash(Directory); - if(Directory.empty()) - return; - - std::string::size_type nDelimiter =Directory.rfind(DIRECTORY_DELIMITER); - - if(pLastDirectory != NULL) - { - *pLastDirectory =Directory.substr(nDelimiter); - StripLeadingBackslash(*pLastDirectory); - } - - if(nDelimiter != std::string::npos) - Directory =Directory.substr(0,nDelimiter); - - SetDirectory(Directory.c_str()); + GetDirectory(Directory); + StripTrailingBackslash(Directory); + if (Directory.empty()) + return; + + std::string::size_type nDelimiter = Directory.rfind(DIRECTORY_DELIMITER); + + if (pLastDirectory != NULL) + { + *pLastDirectory = Directory.substr(nDelimiter); + StripLeadingBackslash(*pLastDirectory); + } + + if (nDelimiter != std::string::npos) + Directory = Directory.substr(0, nDelimiter); + + SetDirectory(Directory.c_str()); } //------------------------------------------------------------- @@ -639,14 +637,14 @@ void CPath::UpDirectory(std::string *pLastDirectory /*= NULL*/) //------------------------------------------------------------- void CPath::CurrentDirectory() { - TCHAR buff_path[MAX_PATH]; - - memset(buff_path, 0, sizeof(buff_path)); + TCHAR buff_path[MAX_PATH]; - ::GetCurrentDirectory(MAX_PATH,buff_path); - - Empty(); - SetDriveDirectory(buff_path); + memset(buff_path, 0, sizeof(buff_path)); + + ::GetCurrentDirectory(MAX_PATH, buff_path); + + Empty(); + SetDriveDirectory(buff_path); } //------------------------------------------------------------- @@ -656,10 +654,10 @@ void CPath::Module(void * hInstance) { TCHAR buff_path[MAX_PATH]; - memset(buff_path, 0, sizeof(buff_path)); + memset(buff_path, 0, sizeof(buff_path)); - GetModuleFileName((HINSTANCE)hInstance, buff_path, MAX_PATH); - m_strPath =buff_path; + GetModuleFileName((HINSTANCE)hInstance, buff_path, MAX_PATH); + m_strPath = buff_path; } //------------------------------------------------------------- @@ -668,10 +666,10 @@ void CPath::Module(void * hInstance) void CPath::Module() { TCHAR buff_path[MAX_PATH]; - memset(buff_path, 0, sizeof(buff_path)); + memset(buff_path, 0, sizeof(buff_path)); - GetModuleFileName((HMODULE)m_hInst,buff_path,MAX_PATH); - m_strPath =buff_path; + GetModuleFileName((HMODULE)m_hInst, buff_path, MAX_PATH); + m_strPath = buff_path; } //------------------------------------------------------------- @@ -679,8 +677,8 @@ void CPath::Module() //------------------------------------------------------------- void CPath::ModuleDirectory(void * hInstance) { - Module((HINSTANCE)hInstance); - SetNameExtension(""); + Module((HINSTANCE)hInstance); + SetNameExtension(""); } //------------------------------------------------------------- @@ -688,8 +686,8 @@ void CPath::ModuleDirectory(void * hInstance) //------------------------------------------------------------- void CPath::ModuleDirectory() { - Module(); - SetNameExtension(""); + Module(); + SetNameExtension(""); } //--------------------------------------------------------------------------- @@ -701,7 +699,7 @@ bool CPath::IsDirectory() const // Check if this path has a filename std::string file_name; GetNameExtension(file_name); - + return file_name.empty(); } @@ -715,22 +713,22 @@ bool CPath::IsDirectory() const //------------------------------------------------------------- bool CPath::DirectoryExists() const { - // Create test path - CPath TestPath(m_strPath.c_str()); + // Create test path + CPath TestPath(m_strPath.c_str()); - std::string DirName; - TestPath.UpDirectory(&DirName); - TestPath.SetNameExtension(DirName.c_str()); + std::string DirName; + TestPath.UpDirectory(&DirName); + TestPath.SetNameExtension(DirName.c_str()); - WIN32_FIND_DATA FindData; - HANDLE hFindFile =FindFirstFile((const char *)TestPath,&FindData); // Find anything - bool bGotFile =(hFindFile != INVALID_HANDLE_VALUE); + WIN32_FIND_DATA FindData; + HANDLE hFindFile = FindFirstFile((const char *)TestPath, &FindData); // Find anything + bool bGotFile = (hFindFile != INVALID_HANDLE_VALUE); - if(hFindFile != NULL) // Make sure we close the search - FindClose(hFindFile); + if (hFindFile != NULL) // Make sure we close the search + FindClose(hFindFile); - return bGotFile; -} + return bGotFile; +} //------------------------------------------------------------- // Post : Return TRUE if these is such a file @@ -738,14 +736,14 @@ bool CPath::DirectoryExists() const //------------------------------------------------------------- bool CPath::Exists() const { - WIN32_FIND_DATA FindData; - HANDLE hFindFile =FindFirstFile(m_strPath.c_str(),&FindData); - bool bSuccess =(hFindFile != INVALID_HANDLE_VALUE); + WIN32_FIND_DATA FindData; + HANDLE hFindFile = FindFirstFile(m_strPath.c_str(), &FindData); + bool bSuccess = (hFindFile != INVALID_HANDLE_VALUE); - if(hFindFile != NULL) // Make sure we close the search - FindClose(hFindFile); + if (hFindFile != NULL) // Make sure we close the search + FindClose(hFindFile); - return bSuccess; + return bSuccess; } //------------------------------------------------------------- @@ -754,18 +752,18 @@ bool CPath::Exists() const //------------------------------------------------------------- bool CPath::Delete(bool bEvenIfReadOnly) const { - uint32_t dwAttr =::GetFileAttributes(m_strPath.c_str()); - if(dwAttr == (uint32_t)-1) + uint32_t dwAttr = ::GetFileAttributes(m_strPath.c_str()); + if (dwAttr == (uint32_t)-1) // File does not exists return FALSE; - if(((dwAttr & FILE_ATTRIBUTE_READONLY) == FILE_ATTRIBUTE_READONLY) && !bEvenIfReadOnly) + if (((dwAttr & FILE_ATTRIBUTE_READONLY) == FILE_ATTRIBUTE_READONLY) && !bEvenIfReadOnly) // File is read-only, and we're not allowed 2 delete it return FALSE; - SetFileAttributes(m_strPath.c_str(),FILE_ATTRIBUTE_NORMAL); + SetFileAttributes(m_strPath.c_str(), FILE_ATTRIBUTE_NORMAL); return DeleteFile(m_strPath.c_str()) != 0; -} +} //------------------------------------------------------------- // Post : Return TRUE on success, FALSE if there is such a target file @@ -778,21 +776,21 @@ bool CPath::CopyTo(const char * lpcszTargetFile, bool bOverwrite) { // Check if the target file exists CPath TargetFile(lpcszTargetFile); - if(TargetFile.Exists()) + if (TargetFile.Exists()) { - // Yeah there is already such a target file + // Yeah there is already such a target file // Decide if we should overwrite - if(!bOverwrite) + if (!bOverwrite) return FALSE; // Delete any previous target - if(!TargetFile.Delete(TRUE)) + if (!TargetFile.Delete(TRUE)) return FALSE; } - // CopyFile will set the target's attributes 2 the same as + // CopyFile will set the target's attributes 2 the same as // the source after copying - return CopyFile(m_strPath.c_str(),lpcszTargetFile,!bOverwrite) != 0; + return CopyFile(m_strPath.c_str(), lpcszTargetFile, !bOverwrite) != 0; } //------------------------------------------------------------- @@ -804,19 +802,19 @@ bool CPath::MoveTo(const char * lpcszTargetFile, bool bOverwrite) { // Check if the target file exists CPath TargetFile(lpcszTargetFile); - if(TargetFile.Exists()) + if (TargetFile.Exists()) { - // Yeah there is already such a target file + // Yeah there is already such a target file // Decide if we should overwrite - if(!bOverwrite) + if (!bOverwrite) return FALSE; // Delete any previous target - if(!TargetFile.Delete(TRUE)) + if (!TargetFile.Delete(TRUE)) return FALSE; } - return MoveFile(m_strPath.c_str(),lpcszTargetFile) != 0; + return MoveFile(m_strPath.c_str(), lpcszTargetFile) != 0; } //------------------------------------------------------------- @@ -825,36 +823,36 @@ bool CPath::MoveTo(const char * lpcszTargetFile, bool bOverwrite) //------------------------------------------------------------- bool CPath::AttributesMatch(uint32_t dwTargetAttributes, uint32_t dwFileAttributes) { - if (dwTargetAttributes == _A_ALLFILES) - { - return true; - } - if(dwTargetAttributes == _A_NORMAL) - { - return ((_A_SUBDIR & dwFileAttributes) == 0); - } - return ( ((dwTargetAttributes & dwFileAttributes) != 0) && ((_A_SUBDIR & dwTargetAttributes) == (_A_SUBDIR & dwFileAttributes)) ); + if (dwTargetAttributes == _A_ALLFILES) + { + return true; + } + if (dwTargetAttributes == _A_NORMAL) + { + return ((_A_SUBDIR & dwFileAttributes) == 0); + } + return (((dwTargetAttributes & dwFileAttributes) != 0) && ((_A_SUBDIR & dwTargetAttributes) == (_A_SUBDIR & dwFileAttributes))); } //------------------------------------------------------------- // Post : Return TRUE if any match found // Task : Find the first file that meets this path and the specified attributes -// You can specify the current attributes of the file or directory -// The attributes are represented by a combination (|) of the following +// You can specify the current attributes of the file or directory +// The attributes are represented by a combination (|) of the following // constants: // -// _A_ARCH Archive. Set whenever the file is +// _A_ARCH Archive. Set whenever the file is // changed, and cleared by the BACKUP command -// _A_HIDDEN Hidden file. Not normally seen with -// the DIR command, unless the /AH option -// is used. Returns information about normal +// _A_HIDDEN Hidden file. Not normally seen with +// the DIR command, unless the /AH option +// is used. Returns information about normal // files as well as files with this attribute -// _A_NORMAL Normal. File can be read or written to +// _A_NORMAL Normal. File can be read or written to // without restriction -// _A_RDONLY Read-only. File cannot be opened for writing, +// _A_RDONLY Read-only. File cannot be opened for writing, // and a file with the same name cannot be created // _A_SUBDIR Subdirectory -// _A_SYSTEM System file. Not normally seen with the DIR +// _A_SYSTEM System file. Not normally seen with the DIR // command, unless the /AS option is used // // These attributes do not follow a simple additive logic @@ -866,90 +864,89 @@ bool CPath::AttributesMatch(uint32_t dwTargetAttributes, uint32_t dwFileAttribut //------------------------------------------------------------- bool CPath::FindFirst(uint32_t dwAttributes /*= _A_NORMAL*/) { - m_dwFindFileAttributes =dwAttributes; - BOOL bGotFile; - BOOL bWantSubdirectory =(BOOL)(_A_SUBDIR & dwAttributes); + m_dwFindFileAttributes = dwAttributes; + BOOL bGotFile; + BOOL bWantSubdirectory = (BOOL)(_A_SUBDIR & dwAttributes); // Close handle to any previous enumeration Exit(); - // i.) Finding first candidate file - WIN32_FIND_DATA FindData; - m_hFindFile =FindFirstFile(m_strPath.c_str(),&FindData); - bGotFile =(m_hFindFile != INVALID_HANDLE_VALUE); + // i.) Finding first candidate file + WIN32_FIND_DATA FindData; + m_hFindFile = FindFirstFile(m_strPath.c_str(), &FindData); + bGotFile = (m_hFindFile != INVALID_HANDLE_VALUE); - while(bGotFile) - { - // ii.) Compare candidate to attributes, and filter out the "." and ".." folders - if(!AttributesMatch(m_dwFindFileAttributes,FindData.dwFileAttributes)) - goto LABEL_GetAnother; - if(bWantSubdirectory && (FindData.cFileName[0] == '.')) - goto LABEL_GetAnother; + while (bGotFile) + { + // ii.) Compare candidate to attributes, and filter out the "." and ".." folders + if (!AttributesMatch(m_dwFindFileAttributes, FindData.dwFileAttributes)) + goto LABEL_GetAnother; + if (bWantSubdirectory && (FindData.cFileName[0] == '.')) + goto LABEL_GetAnother; - // iii.) Found match, prepare result - if((_A_SUBDIR & FindData.dwFileAttributes) != 0) + // iii.) Found match, prepare result + if ((_A_SUBDIR & FindData.dwFileAttributes) != 0) StripTrailingBackslash(m_strPath); - SetNameExtension(FindData.cFileName); + SetNameExtension(FindData.cFileName); - if((_A_SUBDIR & FindData.dwFileAttributes) != 0) + if ((_A_SUBDIR & FindData.dwFileAttributes) != 0) EnsureTrailingBackslash(m_strPath); - return TRUE; - - // iv.) Not found match, get another - LABEL_GetAnother: - bGotFile =FindNextFile(m_hFindFile,&FindData); - } - - return FALSE; + return TRUE; + + // iv.) Not found match, get another + LABEL_GetAnother: + bGotFile = FindNextFile(m_hFindFile, &FindData); + } + + return FALSE; } //------------------------------------------------------------- // Post : Return TRUE if a new match found -// Task : Find the next file that meets the conditions specified +// Task : Find the next file that meets the conditions specified // in the last FindFirst call //------------------------------------------------------------- bool CPath::FindNext() { if (m_hFindFile == NULL) - return FALSE; + return FALSE; - WIN32_FIND_DATA FindData; - while(FindNextFile(m_hFindFile,&FindData) != FALSE) + WIN32_FIND_DATA FindData; + while (FindNextFile(m_hFindFile, &FindData) != FALSE) { // while(FindNext(...)) - - if(AttributesMatch(m_dwFindFileAttributes,FindData.dwFileAttributes)) - { // if(AttributesMatch(...) - if((_A_SUBDIR & FindData.dwFileAttributes) == _A_SUBDIR) - { - if (IsDirectory()) - { - // Found a directory - UpDirectory(); - } - else - { - SetNameExtension(""); - } - AppendDirectory(FindData.cFileName); - } - else - { + if (AttributesMatch(m_dwFindFileAttributes, FindData.dwFileAttributes)) + { // if(AttributesMatch(...) + if ((_A_SUBDIR & FindData.dwFileAttributes) == _A_SUBDIR) + { + if (IsDirectory()) + { + // Found a directory + UpDirectory(); + } + else + { + SetNameExtension(""); + } + AppendDirectory(FindData.cFileName); + } + else + { // Found a file - if (IsDirectory()) - { - // Found a directory - UpDirectory(); - } - SetNameExtension(FindData.cFileName); - } - if((_A_SUBDIR & FindData.dwFileAttributes) == _A_SUBDIR) - EnsureTrailingBackslash(m_strPath); - return TRUE; - } - } + if (IsDirectory()) + { + // Found a directory + UpDirectory(); + } + SetNameExtension(FindData.cFileName); + } + if ((_A_SUBDIR & FindData.dwFileAttributes) == _A_SUBDIR) + EnsureTrailingBackslash(m_strPath); + return TRUE; + } + } - return FALSE; + return FALSE; } //------------------------------------------------------------- @@ -958,8 +955,8 @@ bool CPath::FindNext() //------------------------------------------------------------- bool CPath::ChangeDirectory() { - std::string DriveDirectory; - GetDriveDirectory(DriveDirectory); + std::string DriveDirectory; + GetDriveDirectory(DriveDirectory); return SetCurrentDirectory(DriveDirectory.c_str()) != 0; } @@ -972,36 +969,36 @@ bool CPath::ChangeDirectory() //------------------------------------------------------------- bool CPath::DirectoryCreate(bool bCreateIntermediates /*= TRUE*/) { - std::string PathText; - bool bSuccess; - - GetDriveDirectory(PathText); + std::string PathText; + bool bSuccess; + + GetDriveDirectory(PathText); StripTrailingBackslash(PathText); - bSuccess =::CreateDirectory(PathText.c_str(),NULL) != 0; - if(!bSuccess) - { - CPath CurrentDir(CPath::CURRENT_DIRECTORY); - bSuccess = ChangeDirectory() != 0; - CurrentDir.ChangeDirectory(); - } + bSuccess = ::CreateDirectory(PathText.c_str(), NULL) != 0; + if (!bSuccess) + { + CPath CurrentDir(CPath::CURRENT_DIRECTORY); + bSuccess = ChangeDirectory() != 0; + CurrentDir.ChangeDirectory(); + } - if(!bSuccess && bCreateIntermediates) - { - std::string::size_type nDelimiter =PathText.rfind(DIRECTORY_DELIMITER); - if(nDelimiter == std::string::npos) - return FALSE; + if (!bSuccess && bCreateIntermediates) + { + std::string::size_type nDelimiter = PathText.rfind(DIRECTORY_DELIMITER); + if (nDelimiter == std::string::npos) + return FALSE; - PathText.resize(nDelimiter + 1); - CPath SubPath(PathText); - - if (SubPath.DirectoryCreate()) - return DirectoryCreate(false); - else - return FALSE; - } + PathText.resize(nDelimiter + 1); + CPath SubPath(PathText); - return bSuccess; -} + if (SubPath.DirectoryCreate()) + return DirectoryCreate(false); + else + return FALSE; + } + + return bSuccess; +} //Helpers @@ -1010,52 +1007,51 @@ bool CPath::DirectoryCreate(bool bCreateIntermediates /*= TRUE*/) //------------------------------------------------------------------------ void CPath::cleanPathString(std::string& rDirectory) const { - LPCSTR const DIR_DOUBLEDELIM = "\\\\"; + LPCSTR const DIR_DOUBLEDELIM = "\\\\"; - std::string::size_type pos = rDirectory.find( DIRECTORY_DELIMITER2 ); - while ( pos != std::string::npos ) - { - rDirectory.replace( pos, 1, &DIRECTORY_DELIMITER ); - pos = rDirectory.find( DIRECTORY_DELIMITER2, pos + 1 ); - } + std::string::size_type pos = rDirectory.find(DIRECTORY_DELIMITER2); + while (pos != std::string::npos) + { + rDirectory.replace(pos, 1, &DIRECTORY_DELIMITER); + pos = rDirectory.find(DIRECTORY_DELIMITER2, pos + 1); + } - bool AppendEnd = !_strnicmp(rDirectory.c_str(), "\\\\", 2); - pos = rDirectory.find( DIR_DOUBLEDELIM ); - while ( pos != std::string::npos ) - { - rDirectory.replace( pos, 2, &DIRECTORY_DELIMITER ); - pos = rDirectory.find( DIR_DOUBLEDELIM, pos + 1 ); - } - if (AppendEnd) - { - rDirectory.insert(0, "\\"); - } + bool AppendEnd = !_strnicmp(rDirectory.c_str(), "\\\\", 2); + pos = rDirectory.find(DIR_DOUBLEDELIM); + while (pos != std::string::npos) + { + rDirectory.replace(pos, 2, &DIRECTORY_DELIMITER); + pos = rDirectory.find(DIR_DOUBLEDELIM, pos + 1); + } + if (AppendEnd) + { + rDirectory.insert(0, "\\"); + } } void CPath::StripLeadingChar(std::string& rText, TCHAR chLeading) const { - std::string::size_type nLength =rText.length(); - if(nLength == 0) - return; + std::string::size_type nLength = rText.length(); + if (nLength == 0) + return; - if(rText[0] == chLeading) - rText =rText.substr(1); + if (rText[0] == chLeading) + rText = rText.substr(1); } - //------------------------------------------------------------------------ // Task : Remove first character if \ //------------------------------------------------------------------------ void CPath::StripLeadingBackslash(std::string& Directory) const { - std::string::size_type nLength =Directory.length(); + std::string::size_type nLength = Directory.length(); // If Directory is of the form '\', don't do it - if(nLength <= 1) - return; + if (nLength <= 1) + return; - if(Directory[0] == DIRECTORY_DELIMITER) - Directory =Directory.substr(1); + if (Directory[0] == DIRECTORY_DELIMITER) + Directory = Directory.substr(1); } //------------------------------------------------------------------------ @@ -1063,12 +1059,12 @@ void CPath::StripLeadingBackslash(std::string& Directory) const //------------------------------------------------------------------------ void CPath::StripTrailingChar(std::string& rText, TCHAR chTrailing) const { - std::string::size_type nLength =rText.length(); - if(nLength == 0) - return; - - if(rText[nLength-1] == chTrailing) - rText.resize(nLength-1); + std::string::size_type nLength = rText.length(); + if (nLength == 0) + return; + + if (rText[nLength - 1] == chTrailing) + rText.resize(nLength - 1); } //------------------------------------------------------------------------ @@ -1076,21 +1072,21 @@ void CPath::StripTrailingChar(std::string& rText, TCHAR chTrailing) const //------------------------------------------------------------------------ void CPath::StripTrailingBackslash(std::string& rDirectory) const { - for (;;) - { - std::string::size_type nLength = rDirectory.length(); - if(nLength <= 1) - { - return; - } + for (;;) + { + std::string::size_type nLength = rDirectory.length(); + if (nLength <= 1) + { + return; + } - if(rDirectory[nLength-1] == DIRECTORY_DELIMITER || rDirectory[nLength-1] == DIRECTORY_DELIMITER2) - { - rDirectory.resize(nLength-1); - continue; - } - return; - } + if (rDirectory[nLength - 1] == DIRECTORY_DELIMITER || rDirectory[nLength - 1] == DIRECTORY_DELIMITER2) + { + rDirectory.resize(nLength - 1); + continue; + } + return; + } } //------------------------------------------------------------------------ @@ -1099,12 +1095,12 @@ void CPath::StripTrailingBackslash(std::string& rDirectory) const //------------------------------------------------------------------------ void CPath::EnsureTrailingBackslash(std::string& Directory) const { - std::string::size_type nLength = Directory.length(); + std::string::size_type nLength = Directory.length(); - if (Directory.empty() || (Directory[nLength - 1] != DIRECTORY_DELIMITER)) - { - Directory += DIRECTORY_DELIMITER; - } + if (Directory.empty() || (Directory[nLength - 1] != DIRECTORY_DELIMITER)) + { + Directory += DIRECTORY_DELIMITER; + } } //------------------------------------------------------------------------ @@ -1113,8 +1109,8 @@ void CPath::EnsureTrailingBackslash(std::string& Directory) const //------------------------------------------------------------------------ void CPath::EnsureLeadingBackslash(std::string & Directory) const { - if(Directory.empty() || (Directory[0] != DIRECTORY_DELIMITER)) + if (Directory.empty() || (Directory[0] != DIRECTORY_DELIMITER)) { - Directory = stdstr_f("%c%s", DIRECTORY_DELIMITER, Directory.c_str()); + Directory = stdstr_f("%c%s", DIRECTORY_DELIMITER, Directory.c_str()); } -} +} \ No newline at end of file diff --git a/Source/Common/path.h b/Source/Common/path.h index d42665559..4a469f0bc 100644 --- a/Source/Common/path.h +++ b/Source/Common/path.h @@ -8,84 +8,84 @@ public: uint32_t m_dwErrorCode; public: - CPathException(uint32_t code =0): m_dwErrorCode(code) {} + CPathException(uint32_t code = 0) : m_dwErrorCode(code) {} }; -class CPath +class CPath { -//Enums + //Enums public: - enum DIR_CURRENT_DIRECTORY { CURRENT_DIRECTORY = 1 }; - enum DIR_MODULE_DIRECTORY { MODULE_DIRECTORY = 2 }; - enum DIR_MODULE_FILE { MODULE_FILE = 3 }; + enum DIR_CURRENT_DIRECTORY { CURRENT_DIRECTORY = 1 }; + enum DIR_MODULE_DIRECTORY { MODULE_DIRECTORY = 2 }; + enum DIR_MODULE_FILE { MODULE_FILE = 3 }; - enum { _A_ALLFILES = 0xFFFF }; /* Search Include all files */ + enum { _A_ALLFILES = 0xFFFF }; /* Search Include all files */ -//Attributes -private: - - std::string m_strPath; - uint32_t m_dwFindFileAttributes; - void * m_hFindFile; - static void * m_hInst; + //Attributes +private: + + std::string m_strPath; + uint32_t m_dwFindFileAttributes; + void * m_hFindFile; + static void * m_hInst; public: -//Methods + //Methods - //Construction / destruction - CPath(); + //Construction / destruction + CPath(); CPath(const CPath& rPath); CPath(const char * lpszPath); - CPath(const char * lpszPath, const char * NameExten); - CPath(const char * lpszPath, const std::string & NameExten); - CPath(const std::string& strPath); - CPath(const std::string& strPath, const char * NameExten); - CPath(const std::string& strPath, const std::string& NameExten); + CPath(const char * lpszPath, const char * NameExten); + CPath(const char * lpszPath, const std::string & NameExten); + CPath(const std::string& strPath); + CPath(const std::string& strPath, const char * NameExten); + CPath(const std::string& strPath, const std::string& NameExten); - CPath(DIR_CURRENT_DIRECTORY sdt, const char * NameExten = NULL); - CPath(DIR_MODULE_DIRECTORY sdt, const char * NameExten = NULL); - CPath(DIR_MODULE_FILE sdt); - - virtual ~CPath(); + CPath(DIR_CURRENT_DIRECTORY sdt, const char * NameExten = NULL); + CPath(DIR_MODULE_DIRECTORY sdt, const char * NameExten = NULL); + CPath(DIR_MODULE_FILE sdt); - //Setup & Cleanup + virtual ~CPath(); + + //Setup & Cleanup inline void Init(); - inline void Exit(); + inline void Exit(); - //Operators + //Operators CPath& operator = (const CPath& rPath); CPath& operator = (const char * lpszPath); CPath& operator = (const std::string & strPath); bool operator == (const CPath& rPath) const; - bool operator != (const CPath& rPath) const; + bool operator != (const CPath& rPath) const; operator const char *() const; - operator std::string &() { return m_strPath; } - - //Get path components - void GetDriveDirectory(std::string & rDriveDirectory) const; - std::string GetDriveDirectory(void) const; - void GetDirectory(std::string& rDirectory) const; - std::string GetDirectory(void) const; - void GetName(std::string& rName) const; - std::string GetName(void) const; - void GetNameExtension(std::string& rNameExtension) const; - std::string GetNameExtension(void) const; - void GetExtension(std::string& rExtension) const; - std::string GetExtension(void) const; - void GetLastDirectory(std::string& rDrive) const; - std::string GetLastDirectory(void) const; - void GetFullyQualified(std::string& rFullyQualified) const; - void GetComponents(std::string* pDrive = NULL, - std::string* pDirectory =NULL, - std::string* pName = NULL, - std::string* pExtension = NULL) const; + operator std::string &() { return m_strPath; } - //Get other state - bool IsEmpty() const { return m_strPath.empty(); } - bool IsRelative() const; + //Get path components + void GetDriveDirectory(std::string & rDriveDirectory) const; + std::string GetDriveDirectory(void) const; + void GetDirectory(std::string& rDirectory) const; + std::string GetDirectory(void) const; + void GetName(std::string& rName) const; + std::string GetName(void) const; + void GetNameExtension(std::string& rNameExtension) const; + std::string GetNameExtension(void) const; + void GetExtension(std::string& rExtension) const; + std::string GetExtension(void) const; + void GetLastDirectory(std::string& rDrive) const; + std::string GetLastDirectory(void) const; + void GetFullyQualified(std::string& rFullyQualified) const; + void GetComponents(std::string* pDrive = NULL, + std::string* pDirectory = NULL, + std::string* pName = NULL, + std::string* pExtension = NULL) const; - //Set path components + //Get other state + bool IsEmpty() const { return m_strPath.empty(); } + bool IsRelative() const; + + //Set path components void SetDrive(char chDrive); void SetDriveDirectory(const char * lpszDriveDirectory); void SetDirectory(const char * lpszDirectory, bool bEnsureAbsolute = false); @@ -94,54 +94,54 @@ public: void SetNameExtension(const char * lpszNameExtension); void SetExtension(const char * lpszExtension); void SetExtension(int iExtension); - void AppendDirectory(const char * lpszSubDirectory); - void UpDirectory(std::string* pLastDirectory = NULL); - void SetComponents(const char * lpszDrive, - const char * lpszDirectory, - const char * lpszName, - const char * lpszExtension); + void AppendDirectory(const char * lpszSubDirectory); + void UpDirectory(std::string* pLastDirectory = NULL); + void SetComponents(const char * lpszDrive, + const char * lpszDirectory, + const char * lpszName, + const char * lpszExtension); - //Set whole path + //Set whole path void Empty() { m_strPath.erase(); } void CurrentDirectory(); void Module(); - void Module(void * hInstance); + void Module(void * hInstance); void ModuleDirectory(); - void ModuleDirectory(void * hInstance); + void ModuleDirectory(void * hInstance); - //Directory information + //Directory information bool IsDirectory() const; - bool DirectoryExists() const; + bool DirectoryExists() const; - //File Information - bool IsFile() const { return !IsDirectory(); } - bool Exists() const; + //File Information + bool IsFile() const { return !IsDirectory(); } + bool Exists() const; - //Directory operations - bool DirectoryCreate(bool bCreateIntermediates = true); - bool ChangeDirectory(); - - //File operations - bool Delete(bool bEvenIfReadOnly = true) const; - bool CopyTo(const char * lpcszTargetFile, bool bOverwrite = true); - bool MoveTo(const char * lpcszTargetFile, bool bOverwrite = true); + //Directory operations + bool DirectoryCreate(bool bCreateIntermediates = true); + bool ChangeDirectory(); - //Finders - bool FindFirst(uint32_t dwAttributes = 0); - bool FindNext(); + //File operations + bool Delete(bool bEvenIfReadOnly = true) const; + bool CopyTo(const char * lpcszTargetFile, bool bOverwrite = true); + bool MoveTo(const char * lpcszTargetFile, bool bOverwrite = true); - // Helpers - static void SethInst(void * hInst); - static void * GethInst(); + //Finders + bool FindFirst(uint32_t dwAttributes = 0); + bool FindNext(); + + // Helpers + static void SethInst(void * hInst); + static void * GethInst(); private: - bool AttributesMatch(uint32_t dwTargetAttributes, uint32_t dwFileAttributes); + bool AttributesMatch(uint32_t dwTargetAttributes, uint32_t dwFileAttributes); - void cleanPathString(std::string& rDirectory) const; - void StripLeadingChar(std::string& rText, char chLeading) const; - void StripLeadingBackslash(std::string& Directory) const; - void StripTrailingChar(std::string& rText, char chTrailing) const; - void StripTrailingBackslash(std::string& rDirectory) const; - void EnsureTrailingBackslash(std::string& Directory) const; - void EnsureLeadingBackslash(std::string& Directory) const; + void cleanPathString(std::string& rDirectory) const; + void StripLeadingChar(std::string& rText, char chLeading) const; + void StripLeadingBackslash(std::string& Directory) const; + void StripTrailingChar(std::string& rText, char chTrailing) const; + void StripTrailingBackslash(std::string& rDirectory) const; + void EnsureTrailingBackslash(std::string& Directory) const; + void EnsureLeadingBackslash(std::string& Directory) const; };