2008-09-18 03:15:49 +00:00
|
|
|
// Path.cpp: implementation of the CPath class.
|
|
|
|
//
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
#include "stdafx.h"
|
2016-04-28 07:28:22 +00:00
|
|
|
#ifdef _WIN32
|
2008-09-18 03:15:49 +00:00
|
|
|
#include <Shlobj.h>
|
2015-10-25 11:10:54 +00:00
|
|
|
#include <dos.h>
|
2017-01-20 23:31:09 +00:00
|
|
|
#pragma warning(push)
|
|
|
|
#pragma warning(disable : 4996) // warning C4091: 'typedef ': ignored on left of 'tagGPFIDL_FLAGS' when no variable is declared
|
|
|
|
#include <CommDlg.h>
|
|
|
|
#pragma warning(pop)
|
2016-04-28 07:28:22 +00:00
|
|
|
#else
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <unistd.h>
|
2016-08-03 11:30:46 +00:00
|
|
|
#include <errno.h>
|
2016-04-28 07:28:22 +00:00
|
|
|
#endif
|
|
|
|
#include "Platform.h"
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2016-06-28 05:45:54 +00:00
|
|
|
/*
|
|
|
|
* g_ModuleLogLevel may be NULL while AppInit() is still in session in path.cpp.
|
|
|
|
* The added check to compare to NULL here is at least a temporary workaround.
|
|
|
|
*/
|
|
|
|
#undef WriteTrace
|
|
|
|
#ifdef _WIN32
|
|
|
|
#define WriteTrace(m, s, format, ...) if (g_ModuleLogLevel != NULL && g_ModuleLogLevel[(m)] >= (s)) { WriteTraceFull((m), (s), __FILE__, __LINE__, __FUNCTION__, (format), ## __VA_ARGS__); }
|
|
|
|
#else
|
|
|
|
#define WriteTrace(m, s, format, ...) if (g_ModuleLogLevel != NULL && g_ModuleLogLevel[(m)] >= (s)) { WriteTraceFull((m), (s), __FILE__, __LINE__, __PRETTY_FUNCTION__, (format), ## __VA_ARGS__); }
|
|
|
|
#endif
|
|
|
|
|
2008-09-18 03:15:49 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
// Constants
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-04-28 07:28:22 +00:00
|
|
|
#ifdef _WIN32
|
2016-01-12 12:19:50 +00:00
|
|
|
const char DRIVE_DELIMITER = ':';
|
2016-01-12 06:52:59 +00:00
|
|
|
const char * const DIR_DOUBLEDELIM = "\\\\";
|
2016-01-12 12:19:50 +00:00
|
|
|
const char DIRECTORY_DELIMITER = '\\';
|
|
|
|
const char DIRECTORY_DELIMITER2 = '/';
|
2016-04-28 07:28:22 +00:00
|
|
|
#else
|
|
|
|
const char * const DIR_DOUBLEDELIM = "//";
|
|
|
|
const char DIRECTORY_DELIMITER = '/';
|
|
|
|
const char DIRECTORY_DELIMITER2 = '\\';
|
|
|
|
#endif
|
2016-01-12 12:19:50 +00:00
|
|
|
const char EXTENSION_DELIMITER = '.';
|
2016-04-28 07:28:22 +00:00
|
|
|
#ifdef _WIN32
|
2015-10-25 11:10:54 +00:00
|
|
|
void * CPath::m_hInst = NULL;
|
2016-04-28 07:28:22 +00:00
|
|
|
#endif
|
2008-09-18 03:15:49 +00:00
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
// Helpers
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
2016-04-28 07:28:22 +00:00
|
|
|
#ifdef _WIN32
|
2016-01-12 06:52:59 +00:00
|
|
|
void CPath::SethInst(void * hInst)
|
2009-12-28 22:22:50 +00:00
|
|
|
{
|
2016-01-12 06:52:59 +00:00
|
|
|
m_hInst = hInst;
|
2009-12-28 22:22:50 +00:00
|
|
|
}
|
|
|
|
|
2015-10-25 11:10:54 +00:00
|
|
|
void * CPath::GethInst()
|
2009-12-28 22:22:50 +00:00
|
|
|
{
|
2016-01-12 06:52:59 +00:00
|
|
|
return m_hInst;
|
2009-12-28 22:22:50 +00:00
|
|
|
}
|
2016-04-28 07:28:22 +00:00
|
|
|
#endif
|
2008-09-18 03:15:49 +00:00
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
// Initialisation
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
//-------------------------------------------------------------
|
2016-01-12 06:52:59 +00:00
|
|
|
// Task : Helper function for the various CPath constructors.
|
2008-09-18 03:15:49 +00:00
|
|
|
// Initializes the data members and establishes various
|
|
|
|
// class invariants
|
|
|
|
//-------------------------------------------------------------
|
|
|
|
inline void CPath::Init()
|
|
|
|
{
|
2016-01-12 06:52:59 +00:00
|
|
|
m_dwFindFileAttributes = 0;
|
2016-04-28 07:28:22 +00:00
|
|
|
#ifdef _WIN32
|
2016-01-12 06:52:59 +00:00
|
|
|
m_hFindFile = NULL;
|
2016-04-28 07:28:22 +00:00
|
|
|
#else
|
|
|
|
m_OpenedDir = NULL;
|
|
|
|
m_FindWildcard = "";
|
|
|
|
#endif
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------
|
|
|
|
// Task : Helper function for the various CPath destructors.
|
|
|
|
// Cleans up varios internals
|
|
|
|
//-------------------------------------------------------------
|
|
|
|
inline void CPath::Exit()
|
|
|
|
{
|
2016-04-28 07:28:22 +00:00
|
|
|
#ifdef _WIN32
|
2016-01-12 06:52:59 +00:00
|
|
|
if (m_hFindFile != NULL)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2016-01-12 06:52:59 +00:00
|
|
|
FindClose(m_hFindFile);
|
|
|
|
m_hFindFile = NULL;
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
2016-04-28 07:28:22 +00:00
|
|
|
#else
|
|
|
|
if (m_OpenedDir != NULL)
|
|
|
|
{
|
|
|
|
closedir((DIR*)m_OpenedDir);
|
|
|
|
m_OpenedDir = NULL;
|
|
|
|
}
|
|
|
|
#endif
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
// Construction/Destruction
|
|
|
|
//////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
//-------------------------------------------------------------
|
|
|
|
// Task : Constructs a path
|
|
|
|
//-------------------------------------------------------------
|
|
|
|
CPath::CPath()
|
|
|
|
{
|
2016-01-12 06:52:59 +00:00
|
|
|
Init();
|
2008-09-18 03:15:49 +00:00
|
|
|
Empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------
|
|
|
|
// Task : Constructs a path as copy of another
|
|
|
|
//-------------------------------------------------------------
|
|
|
|
CPath::CPath(const CPath& rPath)
|
|
|
|
{
|
2016-01-12 06:52:59 +00:00
|
|
|
Init();
|
|
|
|
m_strPath = rPath.m_strPath;
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------
|
|
|
|
// Task : Constructs a path and points it 2 lpszPath
|
|
|
|
//-------------------------------------------------------------
|
2015-10-25 11:10:54 +00:00
|
|
|
CPath::CPath(const char * lpszPath)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2016-01-12 06:52:59 +00:00
|
|
|
Init();
|
|
|
|
m_strPath = lpszPath ? lpszPath : "";
|
|
|
|
cleanPathString(m_strPath);
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2015-10-25 11:10:54 +00:00
|
|
|
CPath::CPath(const char * lpszPath, const char * NameExten)
|
2008-11-14 20:47:53 +00:00
|
|
|
{
|
2016-08-03 11:30:46 +00:00
|
|
|
WriteTrace(TracePath, TraceDebug, "Start (lpszPath: \"%s\" NameExten: \"%s\")", lpszPath ? lpszPath : "(null)", NameExten ? NameExten : "(null)");
|
2016-01-12 06:52:59 +00:00
|
|
|
Init();
|
2016-04-28 07:28:22 +00:00
|
|
|
#ifdef _WIN32
|
2016-01-12 06:52:59 +00:00
|
|
|
SetDriveDirectory(lpszPath);
|
2016-04-28 07:28:22 +00:00
|
|
|
#else
|
|
|
|
SetDirectory(lpszPath);
|
|
|
|
#endif
|
2016-01-12 06:52:59 +00:00
|
|
|
SetNameExtension(NameExten);
|
2016-08-03 11:30:46 +00:00
|
|
|
WriteTrace(TracePath, TraceDebug, "Done (m_strPath: \"%s\")", m_strPath.c_str());
|
2008-11-14 20:47:53 +00:00
|
|
|
}
|
|
|
|
|
2008-09-18 03:15:49 +00:00
|
|
|
//-------------------------------------------------------------
|
|
|
|
// Task : Constructs a path and points it 2 strPath
|
|
|
|
//-------------------------------------------------------------
|
2015-10-25 11:10:54 +00:00
|
|
|
CPath::CPath(const std::string& strPath)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
|
|
|
Init();
|
2016-01-12 06:52:59 +00:00
|
|
|
m_strPath = strPath;
|
|
|
|
cleanPathString(m_strPath);
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------
|
|
|
|
// Task : Constructs a path and points it 2 strPath
|
|
|
|
//-------------------------------------------------------------
|
2015-10-25 11:10:54 +00:00
|
|
|
CPath::CPath(const std::string& strPath, const char * NameExten)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2016-01-12 06:52:59 +00:00
|
|
|
Init();
|
2016-04-28 07:28:22 +00:00
|
|
|
#ifdef _WIN32
|
2016-01-12 06:52:59 +00:00
|
|
|
SetDriveDirectory(strPath.c_str());
|
2016-04-28 07:28:22 +00:00
|
|
|
#else
|
|
|
|
SetDirectory(strPath.c_str(),true);
|
|
|
|
#endif
|
2016-01-12 06:52:59 +00:00
|
|
|
SetNameExtension(NameExten);
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------
|
|
|
|
// Task : Constructs a path and points it 2 strPath
|
|
|
|
//-------------------------------------------------------------
|
2015-10-25 11:10:54 +00:00
|
|
|
CPath::CPath(const std::string& strPath, const std::string& NameExten)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2016-01-12 06:52:59 +00:00
|
|
|
Init();
|
2016-04-28 07:28:22 +00:00
|
|
|
#ifdef _WIN32
|
2016-01-12 06:52:59 +00:00
|
|
|
SetDriveDirectory(strPath.c_str());
|
2016-04-28 07:28:22 +00:00
|
|
|
#else
|
|
|
|
SetDirectory(strPath.c_str(),true);
|
|
|
|
#endif
|
2016-01-12 06:52:59 +00:00
|
|
|
SetNameExtension(NameExten.c_str());
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------
|
|
|
|
// Task : Cleanup and destruct a path object
|
|
|
|
//-------------------------------------------------------------
|
|
|
|
CPath::~CPath()
|
|
|
|
{
|
2016-01-12 06:52:59 +00:00
|
|
|
Exit();
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------
|
|
|
|
// Post : Return TRUE if paths are equal
|
|
|
|
// Task : Check if the two path are the same
|
|
|
|
//-------------------------------------------------------------
|
2015-10-25 11:10:54 +00:00
|
|
|
bool CPath::operator ==(const CPath& rPath) const
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
|
|
|
// Get fully qualified versions of the paths
|
2016-01-12 06:52:59 +00:00
|
|
|
std::string FullyQualified1;
|
2015-10-25 11:10:54 +00:00
|
|
|
std::string FullyQualified2;
|
2016-01-12 06:52:59 +00:00
|
|
|
|
|
|
|
GetFullyQualified(FullyQualified1);
|
|
|
|
rPath.GetFullyQualified(FullyQualified2);
|
|
|
|
|
2008-09-18 03:15:49 +00:00
|
|
|
// Compare them
|
2016-01-12 06:52:59 +00:00
|
|
|
return _stricmp(FullyQualified1.c_str(), FullyQualified2.c_str()) == 0;
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------
|
|
|
|
// Post : Return TRUE if paths are different
|
|
|
|
// Task : Check if the two path are different
|
|
|
|
//-------------------------------------------------------------
|
2015-10-25 11:10:54 +00:00
|
|
|
bool CPath::operator !=(const CPath& rPath) const
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
|
|
|
return !(*this == rPath);
|
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------
|
|
|
|
// Task : Assign a path 2 another
|
|
|
|
//-------------------------------------------------------------
|
|
|
|
CPath& CPath::operator =(const CPath& rPath)
|
2016-01-12 06:52:59 +00:00
|
|
|
{
|
|
|
|
if (this != &rPath)
|
2016-01-28 17:34:50 +00:00
|
|
|
{
|
2016-01-12 06:52:59 +00:00
|
|
|
m_strPath = rPath.m_strPath;
|
2016-01-28 17:34:50 +00:00
|
|
|
}
|
2008-09-18 03:15:49 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------
|
|
|
|
// Post : Return the path, so that assignements can be chained
|
|
|
|
// Task : Assign a string 2 a path
|
|
|
|
//-------------------------------------------------------------
|
2015-10-25 11:10:54 +00:00
|
|
|
CPath& CPath::operator =(const char * lpszPath)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2016-01-12 06:52:59 +00:00
|
|
|
m_strPath = lpszPath ? lpszPath : "";
|
2008-09-18 03:15:49 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------
|
|
|
|
// Post : Return the path, so that assignements can be chained
|
|
|
|
// Task : Assign a string 2 a path
|
|
|
|
//-------------------------------------------------------------
|
2015-10-25 11:10:54 +00:00
|
|
|
CPath& CPath::operator =(const std::string& strPath)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2016-01-12 06:52:59 +00:00
|
|
|
m_strPath = strPath;
|
2008-09-18 03:15:49 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------
|
|
|
|
// Post : Converts path 2 string
|
|
|
|
// Task : Convert path 2 string
|
|
|
|
// Warning: because this pointer 2 string point in the data
|
2016-01-12 06:52:59 +00:00
|
|
|
// of this class, it is possible 2 cast the result of this
|
2008-09-18 03:15:49 +00:00
|
|
|
// function in any non-constant pointer and alter the data.
|
|
|
|
// Very dangerous
|
|
|
|
//-------------------------------------------------------------
|
2015-10-25 11:10:54 +00:00
|
|
|
CPath::operator const char *() const
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2015-10-25 11:10:54 +00:00
|
|
|
return (const char *)m_strPath.c_str();
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2015-10-25 11:10:54 +00:00
|
|
|
CPath::CPath(DIR_CURRENT_DIRECTORY /*sdt*/, const char * NameExten)
|
2009-12-28 22:22:50 +00:00
|
|
|
{
|
2016-01-12 06:52:59 +00:00
|
|
|
// Application's current directory
|
|
|
|
Init();
|
|
|
|
CurrentDirectory();
|
|
|
|
if (NameExten) { SetNameExtension(NameExten); }
|
2009-12-28 22:22:50 +00:00
|
|
|
}
|
|
|
|
|
2016-04-28 07:28:22 +00:00
|
|
|
#ifdef _WIN32
|
2015-10-25 11:10:54 +00:00
|
|
|
CPath::CPath(DIR_MODULE_DIRECTORY /*sdt*/, const char * NameExten)
|
2009-12-28 22:22:50 +00:00
|
|
|
{
|
2016-01-12 06:52:59 +00:00
|
|
|
// The directory where the executable of this app is
|
|
|
|
Init();
|
|
|
|
ModuleDirectory();
|
|
|
|
if (NameExten) { SetNameExtension(NameExten); }
|
2009-12-28 22:22:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
CPath::CPath(DIR_MODULE_FILE /*sdt*/)
|
|
|
|
{
|
2016-01-12 06:52:59 +00:00
|
|
|
// The directory where the executable of this app is
|
|
|
|
Init();
|
|
|
|
Module();
|
2009-12-28 22:22:50 +00:00
|
|
|
}
|
2016-04-28 07:28:22 +00:00
|
|
|
#endif
|
2009-12-28 22:22:50 +00:00
|
|
|
|
2008-09-18 03:15:49 +00:00
|
|
|
//-------------------------------------------------------------
|
|
|
|
// Post : Returns the drive component without a colon, e.g. "c"
|
2016-01-12 06:52:59 +00:00
|
|
|
// Returns the directory component with a leading backslash,
|
2008-09-18 03:15:49 +00:00
|
|
|
// 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 :
|
2016-01-12 06:52:59 +00:00
|
|
|
// Task : Return the individual components of this path.
|
|
|
|
// For any given argument, you can pass NULL if you are not
|
2008-09-18 03:15:49 +00:00
|
|
|
// interested in that component.
|
2016-01-12 06:52:59 +00:00
|
|
|
// Do not rely on pNames being <= 8 characters, extensions
|
2008-09-18 03:15:49 +00:00
|
|
|
// being <= 3 characters, or drives being 1 character
|
|
|
|
//-------------------------------------------------------------
|
2016-04-28 07:28:22 +00:00
|
|
|
#ifdef _WIN32
|
2016-01-12 12:19:50 +00:00
|
|
|
void CPath::GetComponents(std::string* pDrive, std::string* pDirectory, std::string* pName, std::string* pExtension) const
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2018-11-19 10:46:58 +00:00
|
|
|
WriteTrace(TracePath, TraceDebug, "Start (m_strPath: \"%s\")", m_strPath.c_str());
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2018-11-19 10:46:58 +00:00
|
|
|
char buff_drive[_MAX_DRIVE + 1] = { 0 };
|
|
|
|
char buff_dir[_MAX_DIR + 1] = { 0 };
|
|
|
|
char buff_name[_MAX_FNAME + 1] = { 0 };
|
|
|
|
char buff_ext[_MAX_EXT + 1] = { 0 };
|
2016-01-12 06:52:59 +00:00
|
|
|
|
2018-11-19 10:46:58 +00:00
|
|
|
const char * BasePath = m_strPath.c_str();
|
|
|
|
const char * DriveDir = strrchr(BasePath, DRIVE_DELIMITER);
|
|
|
|
if (DriveDir != NULL)
|
2016-01-12 12:19:50 +00:00
|
|
|
{
|
2018-11-19 10:46:58 +00:00
|
|
|
int len = sizeof(buff_dir) < (DriveDir - BasePath) ? sizeof(buff_drive) : DriveDir - BasePath;
|
|
|
|
strncpy(buff_drive, BasePath, len);
|
|
|
|
BasePath += len + 1;
|
2016-01-12 12:19:50 +00:00
|
|
|
}
|
2018-11-19 10:46:58 +00:00
|
|
|
|
|
|
|
const char * last = strrchr(BasePath, DIRECTORY_DELIMITER);
|
|
|
|
if (last != NULL)
|
2016-01-12 12:19:50 +00:00
|
|
|
{
|
2018-11-19 10:46:58 +00:00
|
|
|
int len = sizeof(buff_dir) < (last - BasePath) ? sizeof(buff_dir) : last - BasePath;
|
|
|
|
if (len > 0)
|
|
|
|
{
|
|
|
|
strncpy(buff_dir, BasePath, len);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
buff_dir[0] = DIRECTORY_DELIMITER;
|
|
|
|
buff_dir[1] = '\0';
|
|
|
|
}
|
|
|
|
strncpy(buff_name, last + 1, sizeof(buff_name));
|
2016-01-12 12:19:50 +00:00
|
|
|
}
|
2018-11-19 10:46:58 +00:00
|
|
|
else
|
2016-01-12 12:19:50 +00:00
|
|
|
{
|
2018-11-19 10:46:58 +00:00
|
|
|
strncpy(buff_dir, BasePath, sizeof(buff_dir));
|
2016-01-12 12:19:50 +00:00
|
|
|
}
|
2018-11-19 10:46:58 +00:00
|
|
|
char * ext = strrchr(buff_name, '.');
|
|
|
|
if (ext != NULL)
|
2016-01-12 12:19:50 +00:00
|
|
|
{
|
2018-11-19 10:46:58 +00:00
|
|
|
strncpy(buff_ext, ext + 1, sizeof(buff_ext));
|
|
|
|
*ext = '\0';
|
2016-01-12 12:19:50 +00:00
|
|
|
}
|
2016-01-12 06:52:59 +00:00
|
|
|
|
|
|
|
if (pDrive)
|
2016-01-12 12:19:50 +00:00
|
|
|
{
|
2018-11-19 10:46:58 +00:00
|
|
|
*pDrive = buff_drive;
|
2016-01-12 12:19:50 +00:00
|
|
|
}
|
2016-01-12 06:52:59 +00:00
|
|
|
if (pDirectory)
|
2016-01-12 12:19:50 +00:00
|
|
|
{
|
2018-11-19 10:46:58 +00:00
|
|
|
*pDirectory = buff_dir;
|
|
|
|
}
|
|
|
|
if (pName)
|
|
|
|
{
|
|
|
|
*pName = buff_name;
|
2016-01-12 12:19:50 +00:00
|
|
|
}
|
2016-01-12 06:52:59 +00:00
|
|
|
if (pExtension)
|
2016-01-12 12:19:50 +00:00
|
|
|
{
|
2018-11-19 10:46:58 +00:00
|
|
|
*pExtension = buff_ext;
|
2016-01-12 12:19:50 +00:00
|
|
|
}
|
2018-11-19 10:46:58 +00:00
|
|
|
WriteTrace(TracePath, TraceDebug, "Done (dir: \"%s\" name: \"%s\" ext: \"%s\")", buff_dir, buff_name, buff_ext);
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
2016-04-28 07:28:22 +00:00
|
|
|
#else
|
|
|
|
void CPath::GetComponents(std::string* pDirectory, std::string* pName, std::string* pExtension) const
|
|
|
|
{
|
|
|
|
WriteTrace(TracePath, TraceDebug, "Start (m_strPath: \"%s\")",m_strPath.c_str());
|
|
|
|
|
|
|
|
char buff_dir[260];
|
|
|
|
char buff_name[260];
|
|
|
|
char buff_ext[260];
|
|
|
|
|
|
|
|
memset(buff_dir, 0, sizeof(buff_dir));
|
|
|
|
memset(buff_name, 0, sizeof(buff_name));
|
|
|
|
memset(buff_ext, 0, sizeof(buff_ext));
|
|
|
|
|
|
|
|
const char * BasePath = m_strPath.c_str();
|
|
|
|
const char * last = strrchr(BasePath,DIRECTORY_DELIMITER);
|
|
|
|
if (last != NULL)
|
|
|
|
{
|
|
|
|
int len = sizeof(buff_dir) < (last - BasePath) ? sizeof(buff_dir) : last - BasePath;
|
|
|
|
if (len > 0)
|
|
|
|
{
|
|
|
|
strncpy(buff_dir,BasePath,len);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
buff_dir[0] = DIRECTORY_DELIMITER;
|
|
|
|
buff_dir[1] = '\0';
|
|
|
|
}
|
|
|
|
strncpy(buff_name,last + 1,sizeof(buff_name));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
strncpy(buff_dir,BasePath,sizeof(buff_dir));
|
|
|
|
}
|
|
|
|
char * ext = strrchr(buff_name,'.');
|
|
|
|
if (ext != NULL)
|
|
|
|
{
|
|
|
|
strncpy(buff_ext,ext + 1,sizeof(buff_ext));
|
|
|
|
*ext = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pDirectory)
|
|
|
|
{
|
|
|
|
*pDirectory = buff_dir;
|
|
|
|
}
|
|
|
|
if (pName)
|
|
|
|
{
|
|
|
|
*pName = buff_name;
|
|
|
|
}
|
|
|
|
if (pExtension)
|
|
|
|
{
|
|
|
|
*pExtension = buff_ext;
|
2016-08-03 11:30:46 +00:00
|
|
|
}
|
2016-04-28 07:28:22 +00:00
|
|
|
WriteTrace(TracePath, TraceDebug, "Done (dir: \"%s\" name: \"%s\" ext: \"%s\")",buff_dir,buff_name,buff_ext);
|
|
|
|
}
|
|
|
|
#endif
|
2008-09-18 03:15:49 +00:00
|
|
|
|
|
|
|
//-------------------------------------------------------------
|
|
|
|
// Task : Get drive and directory from path
|
|
|
|
//-------------------------------------------------------------
|
2016-04-28 07:28:22 +00:00
|
|
|
#ifdef _WIN32
|
2015-10-25 11:10:54 +00:00
|
|
|
void CPath::GetDriveDirectory(std::string& rDriveDirectory) const
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2015-10-25 11:10:54 +00:00
|
|
|
std::string Drive;
|
|
|
|
std::string Directory;
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2016-01-12 06:52:59 +00:00
|
|
|
GetComponents(&Drive, &Directory);
|
|
|
|
rDriveDirectory = Drive;
|
|
|
|
if (!Drive.empty())
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2016-01-12 06:52:59 +00:00
|
|
|
rDriveDirectory += DRIVE_DELIMITER;
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
2018-11-19 10:46:58 +00:00
|
|
|
rDriveDirectory += Directory;
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2015-10-25 11:10:54 +00:00
|
|
|
std::string CPath::GetDriveDirectory(void) const
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2016-01-12 06:52:59 +00:00
|
|
|
std::string rDriveDirectory;
|
|
|
|
GetDriveDirectory(rDriveDirectory);
|
|
|
|
return rDriveDirectory;
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
2016-04-28 07:28:22 +00:00
|
|
|
#endif
|
|
|
|
|
2008-09-18 03:15:49 +00:00
|
|
|
//-------------------------------------------------------------
|
|
|
|
// Task : Get directory from path
|
|
|
|
//-------------------------------------------------------------
|
2015-10-25 11:10:54 +00:00
|
|
|
void CPath::GetDirectory(std::string& rDirectory) const
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2016-04-28 07:28:22 +00:00
|
|
|
#ifdef _WIN32
|
2016-01-12 06:52:59 +00:00
|
|
|
GetComponents(NULL, &rDirectory);
|
2016-04-28 07:28:22 +00:00
|
|
|
#else
|
|
|
|
GetComponents(&rDirectory);
|
|
|
|
#endif
|
2016-01-12 06:52:59 +00:00
|
|
|
}
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2015-10-25 11:10:54 +00:00
|
|
|
std::string CPath::GetDirectory(void) const
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2016-01-12 06:52:59 +00:00
|
|
|
std::string rDirectory;
|
2016-01-12 12:19:50 +00:00
|
|
|
GetDirectory(rDirectory);
|
2016-01-12 06:52:59 +00:00
|
|
|
return rDirectory;
|
|
|
|
}
|
2008-09-18 03:15:49 +00:00
|
|
|
|
|
|
|
//-------------------------------------------------------------
|
|
|
|
// Task : Get filename and extension from path
|
|
|
|
//-------------------------------------------------------------
|
2015-10-25 11:10:54 +00:00
|
|
|
void CPath::GetNameExtension(std::string& rNameExtension) const
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2015-10-25 11:10:54 +00:00
|
|
|
std::string Name;
|
|
|
|
std::string Extension;
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2016-04-28 07:28:22 +00:00
|
|
|
#ifdef _WIN32
|
2016-01-12 06:52:59 +00:00
|
|
|
GetComponents(NULL, NULL, &Name, &Extension);
|
2016-04-28 07:28:22 +00:00
|
|
|
#else
|
|
|
|
GetComponents(NULL, &Name, &Extension);
|
|
|
|
#endif
|
2016-01-12 06:52:59 +00:00
|
|
|
rNameExtension = Name;
|
|
|
|
if (!Extension.empty())
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2016-01-12 06:52:59 +00:00
|
|
|
rNameExtension += EXTENSION_DELIMITER;
|
2008-09-18 03:15:49 +00:00
|
|
|
rNameExtension += Extension;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-25 11:10:54 +00:00
|
|
|
std::string CPath::GetNameExtension(void) const
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2016-01-12 06:52:59 +00:00
|
|
|
std::string rNameExtension;
|
|
|
|
GetNameExtension(rNameExtension);
|
|
|
|
return rNameExtension;
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------
|
|
|
|
// Task : Get filename from path
|
|
|
|
//-------------------------------------------------------------
|
2015-10-25 11:10:54 +00:00
|
|
|
void CPath::GetName(std::string& rName) const
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2016-04-28 07:28:22 +00:00
|
|
|
#ifdef _WIN32
|
2016-01-12 06:52:59 +00:00
|
|
|
GetComponents(NULL, NULL, &rName);
|
2016-04-28 07:28:22 +00:00
|
|
|
#else
|
|
|
|
GetComponents(NULL, &rName);
|
|
|
|
#endif
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2015-10-25 11:10:54 +00:00
|
|
|
std::string CPath::GetName(void) const
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2016-01-12 06:52:59 +00:00
|
|
|
std::string rName;
|
2016-01-12 12:19:50 +00:00
|
|
|
GetName(rName);
|
2016-01-12 06:52:59 +00:00
|
|
|
return rName;
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------
|
|
|
|
// Task : Get file extension from path
|
|
|
|
//-------------------------------------------------------------
|
2015-10-25 11:10:54 +00:00
|
|
|
void CPath::GetExtension(std::string& rExtension) const
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2016-04-28 07:28:22 +00:00
|
|
|
#ifdef _WIN32
|
2016-01-12 06:52:59 +00:00
|
|
|
GetComponents(NULL, NULL, NULL, &rExtension);
|
2016-04-28 07:28:22 +00:00
|
|
|
#else
|
|
|
|
GetComponents(NULL, NULL, &rExtension);
|
|
|
|
#endif
|
2016-01-12 06:52:59 +00:00
|
|
|
}
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2015-10-25 11:10:54 +00:00
|
|
|
std::string CPath::GetExtension(void) const
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2016-01-12 06:52:59 +00:00
|
|
|
std::string rExtension;
|
2016-01-12 12:19:50 +00:00
|
|
|
GetExtension(rExtension);
|
2016-01-12 06:52:59 +00:00
|
|
|
return rExtension;
|
|
|
|
}
|
2008-09-18 03:15:49 +00:00
|
|
|
|
|
|
|
//-------------------------------------------------------------
|
|
|
|
// Task : Get current directory
|
|
|
|
//-------------------------------------------------------------
|
2016-01-05 10:32:18 +00:00
|
|
|
void CPath::GetLastDirectory(std::string& rDirectory) const
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2016-01-12 06:52:59 +00:00
|
|
|
std::string Directory;
|
|
|
|
|
|
|
|
rDirectory = "";
|
|
|
|
|
|
|
|
GetDirectory(Directory);
|
|
|
|
StripTrailingBackslash(Directory);
|
|
|
|
if (Directory.empty())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-01-05 10:32:18 +00:00
|
|
|
std::string::size_type nDelimiter = Directory.rfind(DIRECTORY_DELIMITER);
|
2016-01-12 06:52:59 +00:00
|
|
|
rDirectory = Directory.substr(nDelimiter);
|
|
|
|
StripLeadingBackslash(rDirectory);
|
|
|
|
}
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2016-01-05 10:32:18 +00:00
|
|
|
std::string CPath::GetLastDirectory(void) const
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2016-01-12 06:52:59 +00:00
|
|
|
std::string rDirecotry;
|
|
|
|
GetLastDirectory(rDirecotry);
|
|
|
|
return rDirecotry;
|
|
|
|
}
|
2008-09-18 03:15:49 +00:00
|
|
|
|
|
|
|
//-------------------------------------------------------------
|
|
|
|
// Task : Get fully qualified path
|
|
|
|
//-------------------------------------------------------------
|
2015-10-25 11:10:54 +00:00
|
|
|
void CPath::GetFullyQualified(std::string& rFullyQualified) const
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2016-04-28 07:28:22 +00:00
|
|
|
#ifdef _WIN32
|
2016-01-12 12:19:50 +00:00
|
|
|
char buff_fullname[MAX_PATH];
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2016-01-12 06:52:59 +00:00
|
|
|
memset(buff_fullname, 0, sizeof(buff_fullname));
|
2009-12-28 22:22:50 +00:00
|
|
|
|
2016-01-12 06:52:59 +00:00
|
|
|
_fullpath(buff_fullname, m_strPath.c_str(), MAX_PATH - 1);
|
|
|
|
rFullyQualified = buff_fullname;
|
2016-04-28 07:28:22 +00:00
|
|
|
#endif
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------
|
|
|
|
// 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:\...)
|
2016-01-12 06:52:59 +00:00
|
|
|
//-------------------------------------------------------------
|
2015-10-25 11:10:54 +00:00
|
|
|
bool CPath::IsRelative() const
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2016-04-28 07:28:22 +00:00
|
|
|
#ifdef _WIN32
|
2016-01-12 06:52:59 +00:00
|
|
|
if (m_strPath.length() > 1 && m_strPath[1] == DRIVE_DELIMITER)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2019-02-08 18:32:02 +00:00
|
|
|
if (m_strPath.length() > 2 && m_strPath[0] == DIRECTORY_DELIMITER && m_strPath[1] == DIRECTORY_DELIMITER)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
if (m_strPath.length() > 1 && m_strPath[0] == DIRECTORY_DELIMITER)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-04-28 07:28:22 +00:00
|
|
|
#endif
|
2016-01-12 06:52:59 +00:00
|
|
|
return true;
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------
|
|
|
|
// Task : Set path components
|
|
|
|
//-------------------------------------------------------------
|
2016-04-28 07:28:22 +00:00
|
|
|
#ifdef _WIN32
|
2016-01-12 12:19:50 +00:00
|
|
|
void CPath::SetComponents(const char * lpszDrive, const char * lpszDirectory, const char * lpszName, const char * lpszExtension)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2016-01-12 12:19:50 +00:00
|
|
|
char buff_fullname[MAX_PATH];
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2016-01-12 06:52:59 +00:00
|
|
|
memset(buff_fullname, 0, sizeof(buff_fullname));
|
2016-08-21 16:20:42 +00:00
|
|
|
if (lpszDirectory == NULL || strlen(lpszDirectory) == 0)
|
|
|
|
{
|
|
|
|
static char empty_dir[] = { DIRECTORY_DELIMITER, '\0' };
|
|
|
|
lpszDirectory = empty_dir;
|
|
|
|
}
|
2016-04-28 07:28:22 +00:00
|
|
|
|
2016-01-12 06:52:59 +00:00
|
|
|
_makepath(buff_fullname, lpszDrive, lpszDirectory, lpszName, lpszExtension);
|
2016-04-28 07:28:22 +00:00
|
|
|
m_strPath.erase();
|
|
|
|
m_strPath = buff_fullname;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
void CPath::SetComponents(const char * lpszDirectory, const char * lpszName, const char * lpszExtension)
|
|
|
|
{
|
|
|
|
char buff_fullname[260];
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2016-04-28 07:28:22 +00:00
|
|
|
memset(buff_fullname, 0, sizeof(buff_fullname));
|
|
|
|
if (lpszDirectory != NULL && lpszDirectory[0] != '\0')
|
|
|
|
{
|
|
|
|
if (lpszDirectory[0] != DIRECTORY_DELIMITER) { buff_fullname[0] = DIRECTORY_DELIMITER; }
|
2016-11-27 20:23:12 +00:00
|
|
|
strncat(buff_fullname,lpszDirectory,sizeof(buff_fullname) - 1);
|
2016-04-28 07:28:22 +00:00
|
|
|
std::string::size_type nLength = strlen(buff_fullname);
|
|
|
|
if (buff_fullname[nLength - 1] != DIRECTORY_DELIMITER && nLength < sizeof(buff_fullname))
|
|
|
|
{
|
|
|
|
buff_fullname[nLength] = DIRECTORY_DELIMITER;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (lpszName != NULL)
|
|
|
|
{
|
2016-11-27 20:23:12 +00:00
|
|
|
strncat(buff_fullname,lpszName,sizeof(buff_fullname) - 1);
|
2016-04-28 07:28:22 +00:00
|
|
|
}
|
2016-08-09 10:19:43 +00:00
|
|
|
if (lpszExtension != NULL && lpszExtension[0] != '\0')
|
2016-04-28 07:28:22 +00:00
|
|
|
{
|
2016-05-15 10:20:55 +00:00
|
|
|
if (lpszExtension[0] != '.')
|
|
|
|
{
|
2016-11-27 20:23:12 +00:00
|
|
|
strncat(buff_fullname,".",sizeof(buff_fullname)-1);
|
2016-05-15 10:20:55 +00:00
|
|
|
}
|
2016-11-27 20:23:12 +00:00
|
|
|
strncat(buff_fullname,lpszExtension,sizeof(buff_fullname)-1);
|
2016-04-28 07:28:22 +00:00
|
|
|
}
|
|
|
|
buff_fullname[sizeof(buff_fullname) - 1] = 0; //Make sure it is null terminated
|
2008-09-18 03:15:49 +00:00
|
|
|
m_strPath.erase();
|
2016-01-12 06:52:59 +00:00
|
|
|
m_strPath = buff_fullname;
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
2016-04-28 07:28:22 +00:00
|
|
|
#endif
|
2008-09-18 03:15:49 +00:00
|
|
|
|
|
|
|
//-------------------------------------------------------------
|
|
|
|
// Task : Set path's drive
|
|
|
|
//-------------------------------------------------------------
|
2016-04-28 07:28:22 +00:00
|
|
|
#ifdef _WIN32
|
2016-01-12 12:19:50 +00:00
|
|
|
void CPath::SetDrive(char chDrive)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2016-01-12 06:52:59 +00:00
|
|
|
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());
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
2016-04-28 07:28:22 +00:00
|
|
|
#endif
|
2008-09-18 03:15:49 +00:00
|
|
|
|
|
|
|
//-------------------------------------------------------------
|
|
|
|
// Task : Set path's directory
|
|
|
|
//-------------------------------------------------------------
|
2016-01-12 12:19:50 +00:00
|
|
|
void CPath::SetDirectory(const char * lpszDirectory, bool bEnsureAbsolute /*= false*/)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2016-04-28 07:28:22 +00:00
|
|
|
WriteTrace(TracePath, TraceDebug, "start (lpszDirectory: \"%s\" bEnsureAbsolute: %s)", lpszDirectory ? lpszDirectory : "(null)", bEnsureAbsolute ? "true" : "false");
|
2016-01-12 06:52:59 +00:00
|
|
|
std::string Directory = lpszDirectory;
|
|
|
|
std::string Name;
|
|
|
|
std::string Extension;
|
|
|
|
|
|
|
|
if (bEnsureAbsolute)
|
2016-01-28 17:34:50 +00:00
|
|
|
{
|
|
|
|
EnsureLeadingBackslash(Directory);
|
|
|
|
}
|
|
|
|
if (Directory.length() > 0)
|
|
|
|
{
|
|
|
|
EnsureTrailingBackslash(Directory);
|
|
|
|
}
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2016-04-28 07:28:22 +00:00
|
|
|
#ifdef _WIN32
|
2016-01-12 12:19:50 +00:00
|
|
|
std::string Drive;
|
2016-01-12 06:52:59 +00:00
|
|
|
GetComponents(&Drive, NULL, &Name, &Extension);
|
|
|
|
SetComponents(Drive.c_str(), Directory.c_str(), Name.c_str(), Extension.c_str());
|
2016-04-28 07:28:22 +00:00
|
|
|
#else
|
|
|
|
GetComponents(NULL, &Name, &Extension);
|
|
|
|
SetComponents(Directory.c_str(), Name.c_str(), Extension.c_str());
|
|
|
|
#endif
|
|
|
|
WriteTrace(TracePath, TraceDebug, "Done (m_strPath: \"%s\")", m_strPath.c_str());
|
2016-01-12 06:52:59 +00:00
|
|
|
}
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2016-04-28 07:28:22 +00:00
|
|
|
#ifdef _WIN32
|
2008-09-18 03:15:49 +00:00
|
|
|
//-------------------------------------------------------------
|
|
|
|
// Task : Set path's drive and directory
|
|
|
|
//-------------------------------------------------------------
|
2015-10-25 11:10:54 +00:00
|
|
|
void CPath::SetDriveDirectory(const char * lpszDriveDirectory)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2016-01-12 06:52:59 +00:00
|
|
|
std::string DriveDirectory = lpszDriveDirectory;
|
|
|
|
std::string Name;
|
|
|
|
std::string Extension;
|
|
|
|
|
2016-01-28 17:34:50 +00:00
|
|
|
if (DriveDirectory.length() > 0)
|
|
|
|
{
|
|
|
|
EnsureTrailingBackslash(DriveDirectory);
|
|
|
|
cleanPathString(DriveDirectory);
|
|
|
|
}
|
2016-01-12 06:52:59 +00:00
|
|
|
|
|
|
|
GetComponents(NULL, NULL, &Name, &Extension);
|
|
|
|
SetComponents(NULL, DriveDirectory.c_str(), Name.c_str(), Extension.c_str());
|
|
|
|
}
|
2016-04-28 07:28:22 +00:00
|
|
|
#endif
|
2008-09-18 03:15:49 +00:00
|
|
|
|
|
|
|
//-------------------------------------------------------------
|
|
|
|
// Task : Set path's filename
|
|
|
|
//-------------------------------------------------------------
|
2015-10-25 11:10:54 +00:00
|
|
|
void CPath::SetName(const char * lpszName)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2016-01-12 06:52:59 +00:00
|
|
|
std::string Directory;
|
|
|
|
std::string Extension;
|
|
|
|
|
2016-04-28 07:28:22 +00:00
|
|
|
#ifdef _WIN32
|
2016-01-12 12:19:50 +00:00
|
|
|
std::string Drive;
|
2016-01-12 06:52:59 +00:00
|
|
|
GetComponents(&Drive, &Directory, NULL, &Extension);
|
|
|
|
SetComponents(Drive.c_str(), Directory.c_str(), lpszName, Extension.c_str());
|
2016-04-28 07:28:22 +00:00
|
|
|
#else
|
|
|
|
GetComponents(&Directory, NULL, &Extension);
|
|
|
|
SetComponents(Directory.c_str(), lpszName, Extension.c_str());
|
|
|
|
#endif
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------
|
|
|
|
// Task : Set path's filename
|
|
|
|
//-------------------------------------------------------------
|
|
|
|
void CPath::SetName(int iName)
|
|
|
|
{
|
2016-01-12 06:52:59 +00:00
|
|
|
std::string Directory;
|
|
|
|
std::string Extension;
|
2016-01-12 12:19:50 +00:00
|
|
|
char sName[33];
|
2009-12-28 22:22:50 +00:00
|
|
|
|
2016-01-12 06:52:59 +00:00
|
|
|
memset(sName, 0, sizeof(sName));
|
|
|
|
|
2016-01-12 12:19:50 +00:00
|
|
|
_snprintf(sName, sizeof(sName), "%d", iName);
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2016-04-28 07:28:22 +00:00
|
|
|
#ifdef _WIN32
|
2016-01-12 12:19:50 +00:00
|
|
|
std::string Drive;
|
2016-01-12 06:52:59 +00:00
|
|
|
GetComponents(&Drive, &Directory, NULL, &Extension);
|
|
|
|
SetComponents(Drive.c_str(), Directory.c_str(), sName, Extension.c_str());
|
2016-04-28 07:28:22 +00:00
|
|
|
#else
|
|
|
|
GetComponents(&Directory, NULL, &Extension);
|
|
|
|
SetComponents(Directory.c_str(), sName, Extension.c_str());
|
|
|
|
#endif
|
2016-01-12 06:52:59 +00:00
|
|
|
}
|
2008-09-18 03:15:49 +00:00
|
|
|
|
|
|
|
//-------------------------------------------------------------
|
|
|
|
// Task : Set path's file extension
|
|
|
|
//-------------------------------------------------------------
|
2015-10-25 11:10:54 +00:00
|
|
|
void CPath::SetExtension(const char * lpszExtension)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2016-01-12 06:52:59 +00:00
|
|
|
std::string Directory;
|
|
|
|
std::string Name;
|
|
|
|
|
2016-04-28 07:28:22 +00:00
|
|
|
#ifdef _WIN32
|
2016-01-12 12:19:50 +00:00
|
|
|
std::string Drive;
|
2016-01-12 06:52:59 +00:00
|
|
|
GetComponents(&Drive, &Directory, &Name);
|
|
|
|
SetComponents(Drive.c_str(), Directory.c_str(), Name.c_str(), lpszExtension);
|
2016-04-28 07:28:22 +00:00
|
|
|
#else
|
|
|
|
GetComponents(&Directory, &Name);
|
|
|
|
SetComponents(Directory.c_str(), Name.c_str(), lpszExtension);
|
|
|
|
#endif
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------
|
|
|
|
// Task : Set path's file extension
|
|
|
|
//-------------------------------------------------------------
|
|
|
|
void CPath::SetExtension(int iExtension)
|
|
|
|
{
|
2016-01-12 06:52:59 +00:00
|
|
|
std::string Directory;
|
|
|
|
std::string Name;
|
2016-01-12 12:19:50 +00:00
|
|
|
char sExtension[20];
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2016-01-12 06:52:59 +00:00
|
|
|
memset(sExtension, 0, sizeof(sExtension));
|
2009-12-28 22:22:50 +00:00
|
|
|
|
2016-01-12 12:19:50 +00:00
|
|
|
_snprintf(sExtension, sizeof(sExtension), "%d", iExtension);
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2016-04-28 07:28:22 +00:00
|
|
|
#ifdef _WIN32
|
2016-01-12 12:19:50 +00:00
|
|
|
std::string Drive;
|
2016-01-12 06:52:59 +00:00
|
|
|
GetComponents(&Drive, &Directory, &Name);
|
|
|
|
SetComponents(Drive.c_str(), Directory.c_str(), Name.c_str(), sExtension);
|
2016-04-28 07:28:22 +00:00
|
|
|
#else
|
|
|
|
GetComponents(&Directory, &Name);
|
|
|
|
SetComponents(Directory.c_str(), Name.c_str(), sExtension);
|
|
|
|
#endif
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------
|
|
|
|
// Task : Set path's filename and extension
|
|
|
|
//-------------------------------------------------------------
|
2015-10-25 11:10:54 +00:00
|
|
|
void CPath::SetNameExtension(const char * lpszNameExtension)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2016-01-12 06:52:59 +00:00
|
|
|
std::string Directory;
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2016-04-28 07:28:22 +00:00
|
|
|
#ifdef _WIN32
|
2016-01-12 12:19:50 +00:00
|
|
|
std::string Drive;
|
2016-01-12 06:52:59 +00:00
|
|
|
GetComponents(&Drive, &Directory);
|
|
|
|
SetComponents(Drive.c_str(), Directory.c_str(), lpszNameExtension, NULL);
|
2016-04-28 07:28:22 +00:00
|
|
|
#else
|
|
|
|
GetComponents(&Directory);
|
|
|
|
SetComponents(Directory.c_str(), lpszNameExtension, NULL);
|
|
|
|
#endif
|
2016-01-12 06:52:59 +00:00
|
|
|
}
|
2008-09-18 03:15:49 +00:00
|
|
|
|
|
|
|
//-------------------------------------------------------------
|
|
|
|
// Task : Append a subdirectory 2 path's directory
|
|
|
|
//-------------------------------------------------------------
|
2015-10-25 11:10:54 +00:00
|
|
|
void CPath::AppendDirectory(const char * lpszSubDirectory)
|
2016-01-12 06:52:59 +00:00
|
|
|
{
|
|
|
|
std::string Directory;
|
|
|
|
std::string SubDirectory = lpszSubDirectory;
|
|
|
|
std::string Name;
|
|
|
|
std::string Extension;
|
|
|
|
|
|
|
|
if (SubDirectory.empty())
|
2016-01-12 12:19:50 +00:00
|
|
|
{
|
2016-01-12 06:52:59 +00:00
|
|
|
return;
|
2016-01-12 12:19:50 +00:00
|
|
|
}
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2016-01-12 06:52:59 +00:00
|
|
|
// Strip out any preceeding backslash
|
|
|
|
StripLeadingBackslash(SubDirectory);
|
|
|
|
EnsureTrailingBackslash(SubDirectory);
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2016-04-28 07:28:22 +00:00
|
|
|
#ifdef _WIN32
|
2016-01-12 12:19:50 +00:00
|
|
|
std::string Drive;
|
2016-01-12 06:52:59 +00:00
|
|
|
GetComponents(&Drive, &Directory, &Name, &Extension);
|
2016-04-28 07:28:22 +00:00
|
|
|
#else
|
|
|
|
GetComponents(&Directory, &Name, &Extension);
|
|
|
|
#endif
|
2016-01-12 06:52:59 +00:00
|
|
|
EnsureTrailingBackslash(Directory);
|
|
|
|
Directory += SubDirectory;
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2016-04-28 07:28:22 +00:00
|
|
|
#ifdef _WIN32
|
2016-01-12 06:52:59 +00:00
|
|
|
SetComponents(Drive.c_str(), Directory.c_str(), Name.c_str(), Extension.c_str());
|
2016-04-28 07:28:22 +00:00
|
|
|
#else
|
|
|
|
SetComponents(Directory.c_str(), Name.c_str(), Extension.c_str());
|
|
|
|
#endif
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------
|
|
|
|
// Pre : If pLastDirectory is given we will store the name of the
|
|
|
|
// deepest directory (the one we're just exiting) in it
|
|
|
|
// Task : Remove deepest subdirectory from path
|
|
|
|
//-------------------------------------------------------------
|
2015-10-25 11:10:54 +00:00
|
|
|
void CPath::UpDirectory(std::string *pLastDirectory /*= NULL*/)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2016-01-12 06:52:59 +00:00
|
|
|
std::string Directory;
|
|
|
|
|
|
|
|
GetDirectory(Directory);
|
|
|
|
StripTrailingBackslash(Directory);
|
|
|
|
if (Directory.empty())
|
|
|
|
return;
|
|
|
|
|
|
|
|
std::string::size_type nDelimiter = Directory.rfind(DIRECTORY_DELIMITER);
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2016-01-12 06:52:59 +00:00
|
|
|
if (pLastDirectory != NULL)
|
|
|
|
{
|
|
|
|
*pLastDirectory = Directory.substr(nDelimiter);
|
|
|
|
StripLeadingBackslash(*pLastDirectory);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nDelimiter != std::string::npos)
|
2016-01-12 12:19:50 +00:00
|
|
|
{
|
2016-01-12 06:52:59 +00:00
|
|
|
Directory = Directory.substr(0, nDelimiter);
|
2016-01-12 12:19:50 +00:00
|
|
|
}
|
2016-01-12 06:52:59 +00:00
|
|
|
|
|
|
|
SetDirectory(Directory.c_str());
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------
|
|
|
|
// Task : Set path 2 current directory
|
|
|
|
//-------------------------------------------------------------
|
|
|
|
void CPath::CurrentDirectory()
|
|
|
|
{
|
2016-04-28 07:28:22 +00:00
|
|
|
char buff_path[260];
|
2016-01-12 06:52:59 +00:00
|
|
|
memset(buff_path, 0, sizeof(buff_path));
|
|
|
|
|
|
|
|
Empty();
|
2016-04-28 07:28:22 +00:00
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
::GetCurrentDirectory(sizeof(buff_path), buff_path);
|
2016-01-12 06:52:59 +00:00
|
|
|
SetDriveDirectory(buff_path);
|
2016-04-28 07:28:22 +00:00
|
|
|
#else
|
|
|
|
getcwd(buff_path, sizeof(buff_path));
|
|
|
|
SetDirectory(buff_path);
|
|
|
|
#endif
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------
|
|
|
|
// Task : Set path 2 the name of specified module
|
|
|
|
//-------------------------------------------------------------
|
2016-04-28 07:28:22 +00:00
|
|
|
#ifdef _WIN32
|
2015-10-25 11:10:54 +00:00
|
|
|
void CPath::Module(void * hInstance)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2016-01-12 12:19:50 +00:00
|
|
|
char buff_path[MAX_PATH];
|
2009-12-28 22:22:50 +00:00
|
|
|
|
2016-01-12 06:52:59 +00:00
|
|
|
memset(buff_path, 0, sizeof(buff_path));
|
2009-12-28 22:22:50 +00:00
|
|
|
|
2016-01-12 06:52:59 +00:00
|
|
|
GetModuleFileName((HINSTANCE)hInstance, buff_path, MAX_PATH);
|
|
|
|
m_strPath = buff_path;
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------
|
|
|
|
// Task : Set path 2 the name of current module
|
|
|
|
//-------------------------------------------------------------
|
|
|
|
void CPath::Module()
|
|
|
|
{
|
2016-01-12 12:19:50 +00:00
|
|
|
Module(m_hInst);
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------
|
|
|
|
// Task : Set path 2 the directory of specified module
|
|
|
|
//-------------------------------------------------------------
|
2015-10-25 11:10:54 +00:00
|
|
|
void CPath::ModuleDirectory(void * hInstance)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2016-01-12 12:19:50 +00:00
|
|
|
Module(hInstance);
|
2016-01-12 06:52:59 +00:00
|
|
|
SetNameExtension("");
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------
|
|
|
|
// Task : Set path 2 the directory of current module
|
|
|
|
//-------------------------------------------------------------
|
|
|
|
void CPath::ModuleDirectory()
|
|
|
|
{
|
2016-01-12 06:52:59 +00:00
|
|
|
Module();
|
|
|
|
SetNameExtension("");
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
2016-04-28 07:28:22 +00:00
|
|
|
#endif
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2015-02-16 06:13:37 +00:00
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
// Post : Return TRUE if a directory
|
|
|
|
// Task : Check if this path represents a directory
|
|
|
|
//---------------------------------------------------------------------------
|
2015-10-25 11:10:54 +00:00
|
|
|
bool CPath::IsDirectory() const
|
2015-02-16 06:13:37 +00:00
|
|
|
{
|
|
|
|
// Check if this path has a filename
|
2015-10-25 11:10:54 +00:00
|
|
|
std::string file_name;
|
2015-02-16 06:13:37 +00:00
|
|
|
GetNameExtension(file_name);
|
2016-01-12 06:52:59 +00:00
|
|
|
|
2015-02-16 06:13:37 +00:00
|
|
|
return file_name.empty();
|
|
|
|
}
|
|
|
|
|
2008-09-18 03:15:49 +00:00
|
|
|
//-------------------------------------------------------------
|
2015-02-16 06:13:37 +00:00
|
|
|
// Post : Return TRUE if directory exists
|
|
|
|
// Task : To determine if the directory exists, we need to
|
|
|
|
// create a test path with a wildcard (*.*) extension
|
|
|
|
// and see if FindFirstFile returns anything. We don't
|
|
|
|
// use CPath::FindFirst() because that routine parses out
|
|
|
|
// '.' and '..', which fails for empty directories
|
2008-09-18 03:15:49 +00:00
|
|
|
//-------------------------------------------------------------
|
2015-10-25 11:10:54 +00:00
|
|
|
bool CPath::DirectoryExists() const
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2016-08-03 11:30:46 +00:00
|
|
|
WriteTrace(TracePath, TraceDebug, "m_strPath = %s",m_strPath.c_str());
|
2016-04-28 07:28:22 +00:00
|
|
|
#ifdef _WIN32
|
2016-01-12 06:52:59 +00:00
|
|
|
// Create test path
|
|
|
|
CPath TestPath(m_strPath.c_str());
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2016-01-12 06:52:59 +00:00
|
|
|
std::string DirName;
|
|
|
|
TestPath.UpDirectory(&DirName);
|
|
|
|
TestPath.SetNameExtension(DirName.c_str());
|
2009-12-28 22:22:50 +00:00
|
|
|
|
2016-01-12 06:52:59 +00:00
|
|
|
WIN32_FIND_DATA FindData;
|
2016-08-03 11:30:46 +00:00
|
|
|
HANDLE hFindFile = FindFirstFile((const char *)TestPath, &FindData); // Find anything
|
|
|
|
bool res = (hFindFile != INVALID_HANDLE_VALUE);
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2016-01-12 06:52:59 +00:00
|
|
|
if (hFindFile != NULL) // Make sure we close the search
|
2016-01-12 12:19:50 +00:00
|
|
|
{
|
2016-01-12 06:52:59 +00:00
|
|
|
FindClose(hFindFile);
|
2016-01-12 12:19:50 +00:00
|
|
|
}
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2016-04-28 07:28:22 +00:00
|
|
|
#else
|
2016-08-03 11:30:46 +00:00
|
|
|
std::string PathText;
|
|
|
|
GetDirectory(PathText);
|
|
|
|
StripTrailingBackslash(PathText);
|
|
|
|
WriteTrace(TracePath, TraceDebug, "Checking if directory \"%s\" exists",PathText.c_str());
|
|
|
|
|
2016-04-28 07:28:22 +00:00
|
|
|
struct stat fileinfo;
|
2016-08-03 11:30:46 +00:00
|
|
|
bool res = stat(PathText.c_str(), &fileinfo) == 0 && S_ISDIR(fileinfo.st_mode);
|
2016-04-28 07:28:22 +00:00
|
|
|
#endif
|
2016-08-03 11:30:46 +00:00
|
|
|
WriteTrace(TracePath, TraceDebug, "Exist = %s",res ? "True" : "False");
|
|
|
|
return res;
|
2016-01-12 06:52:59 +00:00
|
|
|
}
|
2008-09-18 03:15:49 +00:00
|
|
|
|
|
|
|
//-------------------------------------------------------------
|
2015-02-16 06:13:37 +00:00
|
|
|
// Post : Return TRUE if these is such a file
|
|
|
|
// Task : Check if file exists
|
2008-09-18 03:15:49 +00:00
|
|
|
//-------------------------------------------------------------
|
2015-10-25 11:10:54 +00:00
|
|
|
bool CPath::Exists() const
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2016-04-28 07:28:22 +00:00
|
|
|
#ifdef _WIN32
|
2016-01-12 06:52:59 +00:00
|
|
|
WIN32_FIND_DATA FindData;
|
|
|
|
HANDLE hFindFile = FindFirstFile(m_strPath.c_str(), &FindData);
|
|
|
|
bool bSuccess = (hFindFile != INVALID_HANDLE_VALUE);
|
2009-12-28 22:22:50 +00:00
|
|
|
|
2016-01-12 06:52:59 +00:00
|
|
|
if (hFindFile != NULL) // Make sure we close the search
|
2016-01-12 12:19:50 +00:00
|
|
|
{
|
2016-01-12 06:52:59 +00:00
|
|
|
FindClose(hFindFile);
|
2016-01-12 12:19:50 +00:00
|
|
|
}
|
2009-12-28 22:22:50 +00:00
|
|
|
|
2016-01-12 06:52:59 +00:00
|
|
|
return bSuccess;
|
2016-04-28 07:28:22 +00:00
|
|
|
#else
|
|
|
|
struct stat statbuf;
|
|
|
|
return stat(m_strPath.c_str(), &statbuf) == 0;
|
|
|
|
#endif
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2017-01-20 23:31:09 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
bool CPath::SelectFile(void * hwndOwner, const char * InitialDir, const char * FileFilter, bool FileMustExist)
|
|
|
|
{
|
|
|
|
CPath CurrentDir(CURRENT_DIRECTORY);
|
|
|
|
|
|
|
|
OPENFILENAME openfilename;
|
|
|
|
char FileName[MAX_PATH];
|
|
|
|
memset(&FileName, 0, sizeof(FileName));
|
|
|
|
memset(&openfilename, 0, sizeof(openfilename));
|
|
|
|
|
|
|
|
openfilename.lStructSize = sizeof(openfilename);
|
|
|
|
openfilename.hwndOwner = (HWND)hwndOwner;
|
|
|
|
openfilename.lpstrFilter = FileFilter;
|
|
|
|
openfilename.lpstrFile = FileName;
|
|
|
|
openfilename.lpstrInitialDir = InitialDir;
|
|
|
|
openfilename.nMaxFile = MAX_PATH;
|
|
|
|
openfilename.Flags = OFN_HIDEREADONLY | (FileMustExist ? OFN_FILEMUSTEXIST : 0);
|
|
|
|
|
|
|
|
bool res = GetOpenFileName(&openfilename);
|
|
|
|
if (CPath(CURRENT_DIRECTORY) != CurrentDir)
|
|
|
|
{
|
|
|
|
CurrentDir.ChangeDirectory();
|
|
|
|
}
|
|
|
|
if (!res)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
m_strPath = FileName;
|
|
|
|
cleanPathString(m_strPath);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2008-09-18 03:15:49 +00:00
|
|
|
//-------------------------------------------------------------
|
2015-02-16 06:13:37 +00:00
|
|
|
// Post : Return TRUE on success
|
|
|
|
// Task : Delete file
|
2008-09-18 03:15:49 +00:00
|
|
|
//-------------------------------------------------------------
|
2015-10-25 11:10:54 +00:00
|
|
|
bool CPath::Delete(bool bEvenIfReadOnly) const
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2016-04-28 07:28:22 +00:00
|
|
|
#ifdef _WIN32
|
2016-01-12 06:52:59 +00:00
|
|
|
uint32_t dwAttr = ::GetFileAttributes(m_strPath.c_str());
|
|
|
|
if (dwAttr == (uint32_t)-1)
|
2016-01-12 12:19:50 +00:00
|
|
|
{
|
2016-06-25 20:54:24 +00:00
|
|
|
// File does not exist.
|
2016-01-12 12:19:50 +00:00
|
|
|
return false;
|
|
|
|
}
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2016-01-12 06:52:59 +00:00
|
|
|
if (((dwAttr & FILE_ATTRIBUTE_READONLY) == FILE_ATTRIBUTE_READONLY) && !bEvenIfReadOnly)
|
2016-01-12 12:19:50 +00:00
|
|
|
{
|
2015-02-16 06:13:37 +00:00
|
|
|
// File is read-only, and we're not allowed 2 delete it
|
2016-01-12 12:19:50 +00:00
|
|
|
return false;
|
|
|
|
}
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2016-01-12 06:52:59 +00:00
|
|
|
SetFileAttributes(m_strPath.c_str(), FILE_ATTRIBUTE_NORMAL);
|
2015-10-25 11:10:54 +00:00
|
|
|
return DeleteFile(m_strPath.c_str()) != 0;
|
2016-04-28 07:28:22 +00:00
|
|
|
#else
|
2016-08-03 11:30:46 +00:00
|
|
|
return unlink(m_strPath.c_str()) == 0;
|
2016-04-28 07:28:22 +00:00
|
|
|
#endif
|
2016-01-12 06:52:59 +00:00
|
|
|
}
|
2008-09-18 03:15:49 +00:00
|
|
|
|
|
|
|
//-------------------------------------------------------------
|
2016-01-12 12:19:50 +00:00
|
|
|
// Post : Return TRUE on success, false if there is such a target file
|
2015-02-16 06:13:37 +00:00
|
|
|
// and we weren't granted permission 2 overwrite file or some error
|
|
|
|
// Task : Copy file
|
|
|
|
// Since ::CopyFile will not overwrite read only files
|
|
|
|
// we will make sure the target file is writable first
|
2008-09-18 03:15:49 +00:00
|
|
|
//-------------------------------------------------------------
|
2015-10-25 11:10:54 +00:00
|
|
|
bool CPath::CopyTo(const char * lpcszTargetFile, bool bOverwrite)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2016-08-03 11:30:46 +00:00
|
|
|
if (lpcszTargetFile == NULL)
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
WriteTrace(TracePath, TraceDebug, "copy \"%s\" to \"%s\"",m_strPath.c_str(),lpcszTargetFile);
|
2016-04-28 07:28:22 +00:00
|
|
|
#ifdef _WIN32
|
2015-02-16 06:13:37 +00:00
|
|
|
// Check if the target file exists
|
|
|
|
CPath TargetFile(lpcszTargetFile);
|
2016-01-12 06:52:59 +00:00
|
|
|
if (TargetFile.Exists())
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2016-01-12 06:52:59 +00:00
|
|
|
// Yeah there is already such a target file
|
2015-02-16 06:13:37 +00:00
|
|
|
// Decide if we should overwrite
|
2016-01-12 06:52:59 +00:00
|
|
|
if (!bOverwrite)
|
2016-01-12 12:19:50 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2015-02-16 06:13:37 +00:00
|
|
|
// Delete any previous target
|
2016-01-12 12:19:50 +00:00
|
|
|
if (!TargetFile.Delete(true))
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
2015-02-16 06:13:37 +00:00
|
|
|
|
2016-01-12 06:52:59 +00:00
|
|
|
// CopyFile will set the target's attributes 2 the same as
|
2015-02-16 06:13:37 +00:00
|
|
|
// the source after copying
|
2016-01-12 06:52:59 +00:00
|
|
|
return CopyFile(m_strPath.c_str(), lpcszTargetFile, !bOverwrite) != 0;
|
2016-04-28 07:28:22 +00:00
|
|
|
#else
|
2016-08-03 11:30:46 +00:00
|
|
|
|
|
|
|
bool res = true;
|
|
|
|
WriteTrace(TracePath, TraceDebug, "opening \"%s\" for reading",m_strPath.c_str());
|
|
|
|
FILE * infile = fopen(m_strPath.c_str(), "rb");
|
|
|
|
if(infile == NULL)
|
|
|
|
{
|
|
|
|
WriteTrace(TracePath, TraceWarning, "failed to open m_strPath = %s",m_strPath.c_str());
|
|
|
|
res = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
WriteTrace(TracePath, TraceDebug, "opened \"%s\"",m_strPath.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
FILE * outfile = NULL;
|
|
|
|
if (res)
|
|
|
|
{
|
|
|
|
WriteTrace(TracePath, TraceDebug, "opening \"%s\" for writing",lpcszTargetFile);
|
|
|
|
outfile = fopen(lpcszTargetFile, "wb");
|
|
|
|
if (outfile == NULL)
|
|
|
|
{
|
|
|
|
WriteTrace(TracePath, TraceWarning, "failed to open m_strPath = %s errno=%d",lpcszTargetFile, errno);
|
|
|
|
res = false;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
WriteTrace(TracePath, TraceDebug, "opened \"%s\"",lpcszTargetFile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (res)
|
|
|
|
{
|
|
|
|
WriteTrace(TracePath, TraceDebug, "copying data");
|
|
|
|
while (!feof(infile))
|
|
|
|
{
|
|
|
|
char buffer[1024];
|
|
|
|
size_t bytes = fread(buffer, 1, sizeof(buffer), infile);
|
|
|
|
if (ferror(infile))
|
|
|
|
{
|
|
|
|
WriteTrace(TracePath, TraceWarning, "failed to read from %s", m_strPath.c_str());
|
|
|
|
res = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!feof(infile))
|
|
|
|
{
|
|
|
|
size_t written = fwrite(buffer, 1, sizeof(buffer), outfile);
|
|
|
|
}
|
|
|
|
if (ferror(outfile))
|
|
|
|
{
|
|
|
|
WriteTrace(TracePath, TraceWarning, "failed to write to %s, ferror(outfile) = %X", lpcszTargetFile, ferror(outfile));
|
|
|
|
res = false;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct stat ts;
|
|
|
|
if (res)
|
|
|
|
{
|
|
|
|
if (fstat(fileno(infile), &ts) != 0)
|
|
|
|
{
|
|
|
|
WriteTrace(TracePath, TraceWarning, "fstat failed on %s, ferror(infile) = %X", m_strPath.c_str(), ferror(infile));
|
|
|
|
res = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (res)
|
|
|
|
{
|
|
|
|
if (fchmod(fileno(outfile),ts.st_mode) != 0)
|
|
|
|
{
|
|
|
|
WriteTrace(TracePath, TraceWarning, "fchmod failed on %s, errno = %X", lpcszTargetFile, errno);
|
|
|
|
res = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (infile != NULL)
|
|
|
|
{
|
|
|
|
fclose(infile);
|
|
|
|
}
|
|
|
|
if (outfile != NULL)
|
|
|
|
{
|
|
|
|
fclose(outfile);
|
|
|
|
}
|
|
|
|
WriteTrace(TracePath, TraceDebug, "Done, res: %s",res ? "true" : "false");
|
|
|
|
return res;
|
2016-04-28 07:28:22 +00:00
|
|
|
#endif
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------
|
2016-01-12 12:19:50 +00:00
|
|
|
// Post : Return TRUE on success, false if there is such a target file
|
2015-02-16 06:13:37 +00:00
|
|
|
// and we weren't granted permission 2 overwrite file or some error
|
|
|
|
// Task : Move file
|
2008-09-18 03:15:49 +00:00
|
|
|
//-------------------------------------------------------------
|
2015-10-25 11:10:54 +00:00
|
|
|
bool CPath::MoveTo(const char * lpcszTargetFile, bool bOverwrite)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2016-04-28 07:28:22 +00:00
|
|
|
#ifdef _WIN32
|
2015-02-16 06:13:37 +00:00
|
|
|
// Check if the target file exists
|
|
|
|
CPath TargetFile(lpcszTargetFile);
|
2016-01-12 06:52:59 +00:00
|
|
|
if (TargetFile.Exists())
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2016-01-12 06:52:59 +00:00
|
|
|
// Yeah there is already such a target file
|
2015-02-16 06:13:37 +00:00
|
|
|
// Decide if we should overwrite
|
2016-01-12 06:52:59 +00:00
|
|
|
if (!bOverwrite)
|
2016-01-12 12:19:50 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2009-12-28 22:22:50 +00:00
|
|
|
|
2015-02-16 06:13:37 +00:00
|
|
|
// Delete any previous target
|
2016-01-12 06:52:59 +00:00
|
|
|
if (!TargetFile.Delete(TRUE))
|
2016-01-12 12:19:50 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2016-01-12 06:52:59 +00:00
|
|
|
return MoveFile(m_strPath.c_str(), lpcszTargetFile) != 0;
|
2016-04-28 07:28:22 +00:00
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------
|
2015-02-16 06:13:37 +00:00
|
|
|
// Post : Return TRUE if attributes do match
|
|
|
|
// Task : Compare finder attributes
|
2008-09-18 03:15:49 +00:00
|
|
|
//-------------------------------------------------------------
|
2015-10-25 11:10:54 +00:00
|
|
|
bool CPath::AttributesMatch(uint32_t dwTargetAttributes, uint32_t dwFileAttributes)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2016-04-12 07:53:40 +00:00
|
|
|
if (dwTargetAttributes == FIND_ATTRIBUTE_ALLFILES)
|
2016-01-12 06:52:59 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2016-04-12 07:53:40 +00:00
|
|
|
if (dwTargetAttributes == FIND_ATTRIBUTE_FILES)
|
2016-01-12 06:52:59 +00:00
|
|
|
{
|
2016-04-12 07:53:40 +00:00
|
|
|
return ((FIND_ATTRIBUTE_SUBDIR & dwFileAttributes) == 0);
|
2016-01-12 06:52:59 +00:00
|
|
|
}
|
2016-04-12 07:53:40 +00:00
|
|
|
return (((dwTargetAttributes & dwFileAttributes) != 0) && ((FIND_ATTRIBUTE_SUBDIR & dwTargetAttributes) == (FIND_ATTRIBUTE_SUBDIR & dwFileAttributes)));
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------
|
|
|
|
// Post : Return TRUE if any match found
|
|
|
|
// Task : Find the first file that meets this path and the specified attributes
|
2016-01-12 06:52:59 +00:00
|
|
|
// You can specify the current attributes of the file or directory
|
|
|
|
// The attributes are represented by a combination (|) of the following
|
2008-09-18 03:15:49 +00:00
|
|
|
// constants:
|
|
|
|
//
|
2016-01-12 06:52:59 +00:00
|
|
|
// _A_ARCH Archive. Set whenever the file is
|
2008-09-18 03:15:49 +00:00
|
|
|
// changed, and cleared by the BACKUP command
|
2016-01-12 06:52:59 +00:00
|
|
|
// _A_HIDDEN Hidden file. Not normally seen with
|
|
|
|
// the DIR command, unless the /AH option
|
|
|
|
// is used. Returns information about normal
|
2008-09-18 03:15:49 +00:00
|
|
|
// files as well as files with this attribute
|
2016-04-28 07:28:22 +00:00
|
|
|
// FIND_ATTRIBUTE_FILES Normal. File can be read or written to
|
|
|
|
// without restriction
|
2016-01-12 06:52:59 +00:00
|
|
|
// _A_RDONLY Read-only. File cannot be opened for writing,
|
2008-09-18 03:15:49 +00:00
|
|
|
// and a file with the same name cannot be created
|
2016-04-28 07:28:22 +00:00
|
|
|
// FIND_ATTRIBUTE_SUBDIR Subdirectory
|
2016-01-12 06:52:59 +00:00
|
|
|
// _A_SYSTEM System file. Not normally seen with the DIR
|
2008-09-18 03:15:49 +00:00
|
|
|
// command, unless the /AS option is used
|
|
|
|
//
|
|
|
|
// These attributes do not follow a simple additive logic
|
2016-04-28 07:28:22 +00:00
|
|
|
// Note that FIND_ATTRIBUTE_FILES is 0x00, so it effectively cannot be
|
2008-09-18 03:15:49 +00:00
|
|
|
// removed from the attribute set. You will therefore always
|
|
|
|
// get normal files, and may also get Archive, Hidden, etc.
|
|
|
|
// if you specify those attributes
|
|
|
|
// See aso: FindFirstFile, FindNextFile
|
|
|
|
//-------------------------------------------------------------
|
2016-04-28 07:28:22 +00:00
|
|
|
bool CPath::FindFirst(uint32_t dwAttributes /*= FIND_ATTRIBUTE_FILES*/)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
|
|
|
// Close handle to any previous enumeration
|
|
|
|
Exit();
|
|
|
|
|
2016-04-28 07:28:22 +00:00
|
|
|
m_dwFindFileAttributes = dwAttributes;
|
|
|
|
#ifdef _WIN32
|
|
|
|
BOOL bGotFile;
|
|
|
|
BOOL bWantSubdirectory = (BOOL)(FIND_ATTRIBUTE_SUBDIR & dwAttributes);
|
|
|
|
|
2016-01-12 06:52:59 +00:00
|
|
|
// 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;
|
|
|
|
|
|
|
|
// iii.) Found match, prepare result
|
2016-04-28 07:28:22 +00:00
|
|
|
if ((FIND_ATTRIBUTE_SUBDIR & FindData.dwFileAttributes) != 0)
|
2008-09-18 03:15:49 +00:00
|
|
|
StripTrailingBackslash(m_strPath);
|
|
|
|
|
2016-01-12 06:52:59 +00:00
|
|
|
SetNameExtension(FindData.cFileName);
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2016-04-28 07:28:22 +00:00
|
|
|
if ((FIND_ATTRIBUTE_SUBDIR & FindData.dwFileAttributes) != 0)
|
2008-09-18 03:15:49 +00:00
|
|
|
EnsureTrailingBackslash(m_strPath);
|
2016-01-12 06:52:59 +00:00
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
// iv.) Not found match, get another
|
|
|
|
LABEL_GetAnother:
|
|
|
|
bGotFile = FindNextFile(m_hFindFile, &FindData);
|
|
|
|
}
|
2016-04-28 07:28:22 +00:00
|
|
|
#else
|
|
|
|
std::string Directory, Name, Extension;
|
|
|
|
GetComponents(&Directory, &Name, &Extension);
|
|
|
|
|
|
|
|
m_FindWildcard = Name;
|
|
|
|
if (!Extension.empty())
|
|
|
|
{
|
|
|
|
m_FindWildcard += EXTENSION_DELIMITER;
|
|
|
|
m_FindWildcard += Extension;
|
|
|
|
}
|
2016-01-12 06:52:59 +00:00
|
|
|
|
2016-04-28 07:28:22 +00:00
|
|
|
m_OpenedDir = opendir(Directory.c_str());
|
|
|
|
if (m_OpenedDir == NULL) return false;
|
|
|
|
return FindNext();
|
|
|
|
#endif
|
2016-01-12 12:19:50 +00:00
|
|
|
return false;
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------
|
|
|
|
// Post : Return TRUE if a new match found
|
2016-01-12 06:52:59 +00:00
|
|
|
// Task : Find the next file that meets the conditions specified
|
2008-09-18 03:15:49 +00:00
|
|
|
// in the last FindFirst call
|
|
|
|
//-------------------------------------------------------------
|
2015-10-25 11:10:54 +00:00
|
|
|
bool CPath::FindNext()
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2016-04-28 07:28:22 +00:00
|
|
|
#ifdef _WIN32
|
2008-09-18 03:15:49 +00:00
|
|
|
if (m_hFindFile == NULL)
|
2016-01-12 12:19:50 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2016-01-12 06:52:59 +00:00
|
|
|
WIN32_FIND_DATA FindData;
|
2016-01-12 12:19:50 +00:00
|
|
|
while (FindNextFile(m_hFindFile, &FindData) != false)
|
2008-09-18 03:15:49 +00:00
|
|
|
{ // while(FindNext(...))
|
2016-01-12 06:52:59 +00:00
|
|
|
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
|
|
|
|
{
|
2008-09-18 03:15:49 +00:00
|
|
|
// Found a file
|
2016-01-12 06:52:59 +00:00
|
|
|
if (IsDirectory())
|
|
|
|
{
|
|
|
|
// Found a directory
|
|
|
|
UpDirectory();
|
|
|
|
}
|
|
|
|
SetNameExtension(FindData.cFileName);
|
|
|
|
}
|
|
|
|
if ((_A_SUBDIR & FindData.dwFileAttributes) == _A_SUBDIR)
|
2016-04-28 07:28:22 +00:00
|
|
|
{
|
2016-01-12 06:52:59 +00:00
|
|
|
EnsureTrailingBackslash(m_strPath);
|
2016-04-28 07:28:22 +00:00
|
|
|
}
|
2016-01-12 06:52:59 +00:00
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
2016-04-28 07:28:22 +00:00
|
|
|
#else
|
|
|
|
dirent* pEntry;
|
2016-11-27 20:23:12 +00:00
|
|
|
while ((pEntry = readdir((DIR*)m_OpenedDir)))
|
2016-04-28 07:28:22 +00:00
|
|
|
{
|
|
|
|
uint32_t dwFileAttributes = pEntry->d_type == DT_DIR ? FIND_ATTRIBUTE_SUBDIR : FIND_ATTRIBUTE_FILES;
|
|
|
|
|
|
|
|
WriteTrace(TracePath, TraceVerbose, "m_dwFindFileAttributes = %X dwFileAttributes = %X AttributesMatch: %s",m_dwFindFileAttributes, dwFileAttributes, AttributesMatch(m_dwFindFileAttributes, dwFileAttributes) ? "true" : "false");
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2016-04-28 07:28:22 +00:00
|
|
|
// ii.) Compare candidate to attributes, and filter out the "." and ".." folders
|
|
|
|
if (!AttributesMatch(m_dwFindFileAttributes, dwFileAttributes) ||
|
2016-08-03 11:30:46 +00:00
|
|
|
strcmp(pEntry->d_name,".") == 0 ||
|
2016-04-28 07:28:22 +00:00
|
|
|
strcmp(pEntry->d_name,"..") == 0 ||
|
|
|
|
!wildcmp(m_FindWildcard.c_str(),pEntry->d_name))
|
|
|
|
{
|
|
|
|
WriteTrace(TracePath, TraceVerbose, "continue, d_name = %s",pEntry->d_name);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ((FIND_ATTRIBUTE_SUBDIR & dwFileAttributes) == FIND_ATTRIBUTE_SUBDIR)
|
|
|
|
{
|
|
|
|
WriteTrace(TracePath, TraceVerbose, "is dir");
|
|
|
|
if (IsDirectory())
|
|
|
|
{
|
|
|
|
// Found a directory
|
|
|
|
UpDirectory();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SetNameExtension("");
|
|
|
|
}
|
|
|
|
AppendDirectory(pEntry->d_name);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
WriteTrace(TracePath, TraceVerbose, "is file");
|
|
|
|
// Found a file
|
|
|
|
if (IsDirectory())
|
|
|
|
{
|
|
|
|
// Found a directory
|
|
|
|
UpDirectory();
|
|
|
|
}
|
|
|
|
SetNameExtension(pEntry->d_name);
|
2016-08-03 11:30:46 +00:00
|
|
|
WriteTrace(TracePath, TraceVerbose, "m_strPath: %s pEntry->d_name: %s", m_strPath.c_str(), pEntry->d_name);
|
2016-04-28 07:28:22 +00:00
|
|
|
}
|
|
|
|
if ((FIND_ATTRIBUTE_SUBDIR & dwFileAttributes) == FIND_ATTRIBUTE_SUBDIR)
|
|
|
|
{
|
|
|
|
EnsureTrailingBackslash(m_strPath);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
2016-01-12 12:19:50 +00:00
|
|
|
return false;
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//-------------------------------------------------------------
|
|
|
|
// Post : Return TRUE on success
|
|
|
|
// Task : Change current working directory of application 2 path
|
|
|
|
//-------------------------------------------------------------
|
2015-10-25 11:10:54 +00:00
|
|
|
bool CPath::ChangeDirectory()
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2016-04-28 07:28:22 +00:00
|
|
|
#ifdef _WIN32
|
2016-01-12 06:52:59 +00:00
|
|
|
std::string DriveDirectory;
|
|
|
|
GetDriveDirectory(DriveDirectory);
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2015-10-25 11:10:54 +00:00
|
|
|
return SetCurrentDirectory(DriveDirectory.c_str()) != 0;
|
2016-04-28 07:28:22 +00:00
|
|
|
#else
|
|
|
|
std::string Dir;
|
|
|
|
GetDirectory(Dir);
|
|
|
|
|
|
|
|
return chdir(Dir.c_str()) == 0;
|
|
|
|
#endif
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2018-11-19 10:46:58 +00:00
|
|
|
void CPath::NormalizePath(CPath BaseDir)
|
|
|
|
{
|
|
|
|
#ifdef _WIN32
|
|
|
|
stdstr Directory = BaseDir.GetDriveDirectory();
|
|
|
|
#else
|
|
|
|
stdstr Directory = BaseDir.GetDirectory();
|
|
|
|
#endif
|
|
|
|
bool Changed = false;
|
|
|
|
if (IsRelative())
|
|
|
|
{
|
|
|
|
EnsureTrailingBackslash(Directory);
|
|
|
|
Directory += GetDirectory();
|
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
strvector Parts = Directory.Tokenize(DIRECTORY_DELIMITER);
|
|
|
|
strvector NormalizesParts;
|
|
|
|
for (strvector::const_iterator itr = Parts.begin(); itr != Parts.end(); itr++)
|
|
|
|
{
|
|
|
|
if (*itr == ".")
|
|
|
|
{
|
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
else if (*itr == "..")
|
|
|
|
{
|
|
|
|
NormalizesParts.pop_back();
|
|
|
|
Changed = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
NormalizesParts.push_back(*itr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (Changed)
|
|
|
|
{
|
|
|
|
Directory.clear();
|
|
|
|
for (strvector::const_iterator itr = NormalizesParts.begin(); itr != NormalizesParts.end(); itr++)
|
|
|
|
{
|
|
|
|
Directory += *itr + DIRECTORY_DELIMITER;
|
|
|
|
}
|
|
|
|
#ifdef _WIN32
|
|
|
|
SetDriveDirectory(Directory.c_str());
|
|
|
|
#else
|
|
|
|
SetDirectory(Directory.c_str());
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-18 03:15:49 +00:00
|
|
|
//-------------------------------------------------------------
|
|
|
|
// Pre : If bCreateIntermediates is TRUE, create all eventually
|
|
|
|
// missing parent directories too
|
|
|
|
// Post : Return TRUE on success
|
|
|
|
// Task : Create new directory
|
|
|
|
//-------------------------------------------------------------
|
2015-10-25 11:10:54 +00:00
|
|
|
bool CPath::DirectoryCreate(bool bCreateIntermediates /*= TRUE*/)
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2016-08-03 11:30:46 +00:00
|
|
|
WriteTrace(TracePath, TraceDebug, "m_strPath = %s bCreateIntermediates = %s",m_strPath.c_str(),bCreateIntermediates ? "true" : "false");
|
2016-01-12 06:52:59 +00:00
|
|
|
std::string PathText;
|
|
|
|
bool bSuccess;
|
|
|
|
|
2016-08-03 11:30:46 +00:00
|
|
|
if (DirectoryExists())
|
|
|
|
{
|
|
|
|
WriteTrace(TracePath, TraceDebug, "Directory already exists, res = true");
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-04-28 07:28:22 +00:00
|
|
|
#ifdef _WIN32
|
2016-01-12 06:52:59 +00:00
|
|
|
GetDriveDirectory(PathText);
|
2008-09-18 03:15:49 +00:00
|
|
|
StripTrailingBackslash(PathText);
|
2016-08-03 11:30:46 +00:00
|
|
|
WriteTrace(TracePath, TraceDebug, "Create %s",PathText.c_str());
|
2016-01-12 06:52:59 +00:00
|
|
|
bSuccess = ::CreateDirectory(PathText.c_str(), NULL) != 0;
|
2016-04-28 07:28:22 +00:00
|
|
|
#else
|
|
|
|
GetDirectory(PathText);
|
|
|
|
StripTrailingBackslash(PathText);
|
2016-08-03 11:30:46 +00:00
|
|
|
WriteTrace(TracePath, TraceDebug, "Create %s",PathText.c_str());
|
|
|
|
bSuccess = mkdir(PathText.c_str(), S_IRWXU) == 0;
|
|
|
|
if (!bSuccess)
|
2016-01-12 06:52:59 +00:00
|
|
|
{
|
2016-08-03 11:30:46 +00:00
|
|
|
WriteTrace(TracePath, TraceWarning, "failed to create \"%s\" errno: %d",PathText.c_str(), errno);
|
2016-01-12 06:52:59 +00:00
|
|
|
}
|
2016-04-28 07:28:22 +00:00
|
|
|
#endif
|
2016-01-12 06:52:59 +00:00
|
|
|
if (!bSuccess && bCreateIntermediates)
|
|
|
|
{
|
2016-08-03 11:30:46 +00:00
|
|
|
WriteTrace(TracePath, TraceDebug, "failed creating intermediates");
|
2016-01-12 06:52:59 +00:00
|
|
|
std::string::size_type nDelimiter = PathText.rfind(DIRECTORY_DELIMITER);
|
|
|
|
if (nDelimiter == std::string::npos)
|
2016-01-12 12:19:50 +00:00
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
2016-01-12 06:52:59 +00:00
|
|
|
|
|
|
|
PathText.resize(nDelimiter + 1);
|
|
|
|
CPath SubPath(PathText);
|
|
|
|
|
2016-01-12 12:19:50 +00:00
|
|
|
return SubPath.DirectoryCreate() ? DirectoryCreate(false) : false;
|
2016-01-12 06:52:59 +00:00
|
|
|
}
|
2016-08-03 11:30:46 +00:00
|
|
|
WriteTrace(TracePath, TraceDebug, "res = %s",bSuccess ? "true" : "false");
|
2016-01-12 06:52:59 +00:00
|
|
|
return bSuccess;
|
|
|
|
}
|
2008-09-18 03:15:49 +00:00
|
|
|
|
|
|
|
//Helpers
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------
|
|
|
|
// Task : Remove first character (if any) if it's chLeading
|
|
|
|
//------------------------------------------------------------------------
|
2015-10-25 11:10:54 +00:00
|
|
|
void CPath::cleanPathString(std::string& rDirectory) const
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2016-01-12 06:52:59 +00:00
|
|
|
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);
|
|
|
|
}
|
2009-12-28 22:22:50 +00:00
|
|
|
|
2016-01-12 12:19:50 +00:00
|
|
|
bool AppendEnd = !_strnicmp(rDirectory.c_str(), DIR_DOUBLEDELIM, 2);
|
2016-01-12 06:52:59 +00:00
|
|
|
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)
|
|
|
|
{
|
2016-01-28 17:34:50 +00:00
|
|
|
rDirectory.insert(0, stdstr_f("%c", DIRECTORY_DELIMITER).c_str());
|
2016-01-12 06:52:59 +00:00
|
|
|
}
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
2016-01-12 12:19:50 +00:00
|
|
|
void CPath::StripLeadingChar(std::string& rText, char chLeading) const
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2016-01-12 06:52:59 +00:00
|
|
|
std::string::size_type nLength = rText.length();
|
|
|
|
if (nLength == 0)
|
|
|
|
return;
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2016-01-12 06:52:59 +00:00
|
|
|
if (rText[0] == chLeading)
|
|
|
|
rText = rText.substr(1);
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------
|
|
|
|
// Task : Remove first character if \
|
|
|
|
//------------------------------------------------------------------------
|
2015-10-25 11:10:54 +00:00
|
|
|
void CPath::StripLeadingBackslash(std::string& Directory) const
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2016-01-12 06:52:59 +00:00
|
|
|
std::string::size_type nLength = Directory.length();
|
2008-09-18 03:15:49 +00:00
|
|
|
|
|
|
|
// If Directory is of the form '\', don't do it
|
2016-01-12 06:52:59 +00:00
|
|
|
if (nLength <= 1)
|
|
|
|
return;
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2016-01-12 06:52:59 +00:00
|
|
|
if (Directory[0] == DIRECTORY_DELIMITER)
|
|
|
|
Directory = Directory.substr(1);
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------
|
|
|
|
// Task : Remove last character (if any) if it's chTrailing
|
|
|
|
//------------------------------------------------------------------------
|
2016-01-12 12:19:50 +00:00
|
|
|
void CPath::StripTrailingChar(std::string& rText, char chTrailing) const
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2016-01-12 06:52:59 +00:00
|
|
|
std::string::size_type nLength = rText.length();
|
|
|
|
if (nLength == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (rText[nLength - 1] == chTrailing)
|
|
|
|
rText.resize(nLength - 1);
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------
|
|
|
|
// Task : Remove last character if \
|
|
|
|
//------------------------------------------------------------------------
|
2015-10-25 11:10:54 +00:00
|
|
|
void CPath::StripTrailingBackslash(std::string& rDirectory) const
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2016-01-12 06:52:59 +00:00
|
|
|
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;
|
|
|
|
}
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------
|
|
|
|
// Task : Add a backslash to the end of Directory if there is
|
|
|
|
// not already one there
|
|
|
|
//------------------------------------------------------------------------
|
2015-10-25 11:10:54 +00:00
|
|
|
void CPath::EnsureTrailingBackslash(std::string& Directory) const
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2016-01-12 06:52:59 +00:00
|
|
|
std::string::size_type nLength = Directory.length();
|
2008-09-18 03:15:49 +00:00
|
|
|
|
2016-01-12 06:52:59 +00:00
|
|
|
if (Directory.empty() || (Directory[nLength - 1] != DIRECTORY_DELIMITER))
|
|
|
|
{
|
|
|
|
Directory += DIRECTORY_DELIMITER;
|
|
|
|
}
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------
|
|
|
|
// Task : Add a backslash to the beginning of Directory if there
|
|
|
|
// is not already one there
|
|
|
|
//------------------------------------------------------------------------
|
2015-10-25 11:10:54 +00:00
|
|
|
void CPath::EnsureLeadingBackslash(std::string & Directory) const
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2016-01-12 06:52:59 +00:00
|
|
|
if (Directory.empty() || (Directory[0] != DIRECTORY_DELIMITER))
|
2008-09-18 03:15:49 +00:00
|
|
|
{
|
2016-01-12 06:52:59 +00:00
|
|
|
Directory = stdstr_f("%c%s", DIRECTORY_DELIMITER, Directory.c_str());
|
2008-09-18 03:15:49 +00:00
|
|
|
}
|
2016-04-28 07:28:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef _WIN32
|
|
|
|
bool CPath::wildcmp(const char *wild, const char *string)
|
|
|
|
{
|
|
|
|
const char *cp = NULL, *mp = NULL;
|
|
|
|
|
2016-08-03 11:30:46 +00:00
|
|
|
while ((*string) && (*wild != '*'))
|
2016-04-28 07:28:22 +00:00
|
|
|
{
|
2016-08-03 11:30:46 +00:00
|
|
|
if ((*wild != *string) && (*wild != '?'))
|
2016-04-28 07:28:22 +00:00
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
wild++;
|
|
|
|
string++;
|
|
|
|
}
|
|
|
|
|
2016-08-03 11:30:46 +00:00
|
|
|
while (*string)
|
2016-04-28 07:28:22 +00:00
|
|
|
{
|
2016-08-03 11:30:46 +00:00
|
|
|
if (*wild == '*')
|
2016-04-28 07:28:22 +00:00
|
|
|
{
|
2016-08-03 11:30:46 +00:00
|
|
|
if (!*++wild)
|
2016-04-28 07:28:22 +00:00
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
mp = wild;
|
|
|
|
cp = string+1;
|
2016-08-03 11:30:46 +00:00
|
|
|
}
|
|
|
|
else if ((*wild == *string) || (*wild == '?'))
|
2016-04-28 07:28:22 +00:00
|
|
|
{
|
|
|
|
wild++;
|
|
|
|
string++;
|
2016-08-03 11:30:46 +00:00
|
|
|
}
|
|
|
|
else
|
2016-04-28 07:28:22 +00:00
|
|
|
{
|
|
|
|
wild = mp;
|
|
|
|
string = cp++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-03 11:30:46 +00:00
|
|
|
while (*wild == '*')
|
2016-04-28 07:28:22 +00:00
|
|
|
{
|
|
|
|
wild++;
|
|
|
|
}
|
|
|
|
return !*wild;
|
|
|
|
}
|
|
|
|
#endif
|