project64/Source/Common/path.h

165 lines
5.5 KiB
C
Raw Permalink Normal View History

#pragma once
2015-10-25 11:10:54 +00:00
#include <string>
2021-04-12 06:34:26 +00:00
#include <stdint.h>
2016-01-12 06:52:59 +00:00
class CPath
{
// Enums
public:
enum DIR_CURRENT_DIRECTORY { CURRENT_DIRECTORY = 1 };
#ifdef _WIN32
2016-01-12 06:52:59 +00:00
enum DIR_MODULE_DIRECTORY { MODULE_DIRECTORY = 2 };
enum DIR_MODULE_FILE { MODULE_FILE = 3 };
#endif
enum
{
FIND_ATTRIBUTE_ALLFILES = 0xFFFF, // Search include all files
FIND_ATTRIBUTE_FILES = 0x0000, // File can be read or written to without restriction
FIND_ATTRIBUTE_SUBDIR = 0x0010, // Subdirectories
};
// Attributes
2016-01-12 06:52:59 +00:00
private:
std::string m_strPath;
#ifdef _WIN32
2016-01-12 06:52:59 +00:00
void * m_hFindFile;
static void * m_hInst;
#else
void * m_OpenedDir;
std::string m_FindWildcard;
#endif
uint32_t m_dwFindFileAttributes;
public:
// Methods
// Construction / destruction
2016-01-12 06:52:59 +00:00
CPath();
CPath(const CPath& rPath);
2015-10-25 11:10:54 +00:00
CPath(const char * lpszPath);
2016-01-12 06:52:59 +00:00
CPath(const char * lpszPath, const char * NameExten);
CPath(const std::string& strPath);
CPath(const std::string& strPath, const char * NameExten);
CPath(const std::string& strPath, const std::string& NameExten);
2021-04-12 11:35:39 +00:00
CPath(DIR_CURRENT_DIRECTORY sdt, const char * NameExten = nullptr);
#ifdef _WIN32
2021-04-12 11:35:39 +00:00
CPath(DIR_MODULE_DIRECTORY sdt, const char * NameExten = nullptr);
2016-01-12 06:52:59 +00:00
CPath(DIR_MODULE_FILE sdt);
#endif
2016-01-12 06:52:59 +00:00
virtual ~CPath();
// Operators
CPath& operator = (const CPath& rPath);
2015-10-25 11:10:54 +00:00
CPath& operator = (const char * lpszPath);
CPath& operator = (const std::string & strPath);
bool operator == (const CPath& rPath) const;
2016-01-12 06:52:59 +00:00
bool operator != (const CPath& rPath) const;
2015-10-25 11:10:54 +00:00
operator const char *() const;
2016-01-12 12:19:50 +00:00
operator const std::string &() { return m_strPath; }
2016-01-12 06:52:59 +00:00
// Get path components
#ifdef _WIN32
2016-01-12 06:52:59 +00:00
void GetDriveDirectory(std::string & rDriveDirectory) const;
std::string GetDriveDirectory(void) const;
#endif
2016-01-12 06:52:59 +00:00
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;
2016-01-12 12:19:50 +00:00
void GetLastDirectory(std::string& rDirectory) const;
2016-01-12 06:52:59 +00:00
std::string GetLastDirectory(void) const;
void GetFullyQualified(std::string& rFullyQualified) const;
#ifdef _WIN32
2021-04-12 11:35:39 +00:00
void GetComponents(std::string* pDrive = nullptr, std::string* pDirectory = nullptr, std::string* pName = nullptr, std::string* pExtension = nullptr) const;
#else
2021-04-12 11:35:39 +00:00
void GetComponents(std::string* pDirectory = nullptr, std::string* pName = nullptr, std::string* pExtension = nullptr) const;
#endif
// Get other state
2016-01-12 06:52:59 +00:00
bool IsEmpty() const { return m_strPath.empty(); }
bool IsRelative() const;
// Set path components
#ifdef _WIN32
2015-10-25 11:10:54 +00:00
void SetDrive(char chDrive);
void SetDriveDirectory(const char * lpszDriveDirectory);
#endif
2015-10-25 11:10:54 +00:00
void SetDirectory(const char * lpszDirectory, bool bEnsureAbsolute = false);
void SetName(const char * lpszName);
void SetName(int iName);
2015-10-25 11:10:54 +00:00
void SetNameExtension(const char * lpszNameExtension);
void SetExtension(const char * lpszExtension);
void SetExtension(int iExtension);
2016-01-12 06:52:59 +00:00
void AppendDirectory(const char * lpszSubDirectory);
2021-04-12 11:35:39 +00:00
void UpDirectory(std::string* pLastDirectory = nullptr);
#ifdef _WIN32
2016-01-12 12:19:50 +00:00
void SetComponents(const char * lpszDrive, const char * lpszDirectory, const char * lpszName, const char * lpszExtension);
#else
void SetComponents(const char * lpszDirectory, const char * lpszName, const char * lpszExtension);
#endif
// Set whole path
void Empty() { m_strPath.erase(); }
void CurrentDirectory();
#ifdef _WIN32
void Module();
2016-01-12 06:52:59 +00:00
void Module(void * hInstance);
void ModuleDirectory();
2016-01-12 06:52:59 +00:00
void ModuleDirectory(void * hInstance);
#endif
// Directory information
2015-10-25 11:10:54 +00:00
bool IsDirectory() const;
2016-01-12 06:52:59 +00:00
bool DirectoryExists() const;
// File information
bool IsFile() const { return !IsDirectory(); }
bool Exists() const;
#ifdef _WIN32
bool SelectFile(void * hwndOwner, const char * InitialDir, const char * FileFilter, bool FileMustExist);
#endif
// Directory operations
2016-01-12 06:52:59 +00:00
bool DirectoryCreate(bool bCreateIntermediates = true);
bool ChangeDirectory();
2018-11-19 10:46:58 +00:00
void NormalizePath(CPath BaseDir);
// File operations
2016-01-12 06:52:59 +00:00
bool Delete(bool bEvenIfReadOnly = true) const;
bool CopyTo(const char * lpcszTargetFile, bool bOverwrite = true);
bool MoveTo(const char * lpcszTargetFile, bool bOverwrite = true);
// Finders
2016-01-12 06:52:59 +00:00
bool FindFirst(uint32_t dwAttributes = 0);
bool FindNext();
2016-01-12 06:52:59 +00:00
// Helpers
#ifdef _WIN32
2016-01-12 06:52:59 +00:00
static void SethInst(void * hInst);
static void * GethInst();
#endif
private:
// Setup and cleanup
2018-02-19 06:43:42 +00:00
inline void Init();
inline void Exit();
2016-01-12 06:52:59 +00:00
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;
#ifndef _WIN32
bool wildcmp(const char *wild, const char *string);
#endif
};