project64/Source/Common/path.h

185 lines
5.6 KiB
C
Raw Permalink Normal View History

#pragma once
2021-04-12 06:34:26 +00:00
#include <stdint.h>
2022-10-03 08:04:42 +00:00
#include <string>
2016-01-12 06:52:59 +00:00
class CPath
{
// Enums
public:
2022-10-03 08:04:42 +00:00
enum DIR_CURRENT_DIRECTORY
{
CURRENT_DIRECTORY = 1
};
#ifdef _WIN32
2022-10-03 08:04:42 +00:00
enum DIR_MODULE_DIRECTORY
{
MODULE_DIRECTORY = 2
};
enum DIR_MODULE_FILE
{
MODULE_FILE = 3
};
#endif
2022-10-03 08:04:42 +00:00
enum
{
2022-10-03 08:04:42 +00:00
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:
2022-10-03 08:04:42 +00:00
std::string m_strPath;
#ifdef _WIN32
2022-10-03 08:04:42 +00:00
void * m_hFindFile;
2016-01-12 06:52:59 +00:00
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();
2022-10-03 08:04:42 +00:00
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);
2022-10-03 08:04:42 +00:00
CPath(const std::string & strPath);
CPath(const std::string & strPath, const char * NameExten);
CPath(const std::string & strPath, const std::string & NameExten);
2016-01-12 06:52:59 +00:00
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
2022-10-03 08:04:42 +00:00
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;
2015-10-25 11:10:54 +00:00
operator const char *() const;
2022-10-03 08:04:42 +00:00
operator const std::string &()
{
return m_strPath;
}
2016-01-12 06:52:59 +00:00
// Get path components
#ifdef _WIN32
2022-10-03 08:04:42 +00:00
void GetDriveDirectory(std::string & rDriveDirectory) const;
2016-01-12 06:52:59 +00:00
std::string GetDriveDirectory(void) const;
#endif
2022-10-03 08:04:42 +00:00
void GetDirectory(std::string & rDirectory) const;
2016-01-12 06:52:59 +00:00
std::string GetDirectory(void) const;
2022-10-03 08:04:42 +00:00
void GetName(std::string & rName) const;
2016-01-12 06:52:59 +00:00
std::string GetName(void) const;
2022-10-03 08:04:42 +00:00
void GetNameExtension(std::string & rNameExtension) const;
2016-01-12 06:52:59 +00:00
std::string GetNameExtension(void) const;
2022-10-03 08:04:42 +00:00
void GetExtension(std::string & rExtension) const;
2016-01-12 06:52:59 +00:00
std::string GetExtension(void) const;
2022-10-03 08:04:42 +00:00
void GetLastDirectory(std::string & rDirectory) const;
2016-01-12 06:52:59 +00:00
std::string GetLastDirectory(void) const;
2022-10-03 08:04:42 +00:00
void GetFullyQualified(std::string & rFullyQualified) const;
#ifdef _WIN32
2022-10-03 08:04:42 +00:00
void GetComponents(std::string * pDrive = nullptr, std::string * pDirectory = nullptr, std::string * pName = nullptr, std::string * pExtension = nullptr) const;
#else
2022-10-03 08:04:42 +00:00
void GetComponents(std::string * pDirectory = nullptr, std::string * pName = nullptr, std::string * pExtension = nullptr) const;
#endif
// Get other state
2022-10-03 08:04:42 +00:00
bool IsEmpty() const
{
return m_strPath.empty();
}
2016-01-12 06:52:59 +00:00
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);
2022-10-03 08:04:42 +00:00
void UpDirectory(std::string * pLastDirectory = nullptr);
#ifdef _WIN32
2022-10-03 08:04:42 +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
2022-10-03 08:04:42 +00:00
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
2022-10-03 08:04:42 +00:00
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();
CPath & 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);
2022-10-03 08:04:42 +00:00
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
2022-10-03 08:04:42 +00:00
bool wildcmp(const char * wild, const char * string);
#endif
};