project64/Source/Common/path.h

148 lines
4.6 KiB
C
Raw Normal View History

#pragma once
2015-10-25 11:10:54 +00:00
#include <string>
#include "stdtypes.h"
class CPathException
{
public:
2015-10-25 11:10:54 +00:00
uint32_t m_dwErrorCode;
public:
2015-10-25 11:10:54 +00:00
CPathException(uint32_t code =0): m_dwErrorCode(code) {}
};
class CPath
{
//Enums
public:
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 */
//Attributes
private:
2015-10-25 11:10:54 +00:00
std::string m_strPath;
uint32_t m_dwFindFileAttributes;
void * m_hFindFile;
static void * m_hInst;
public:
//Methods
//Construction / destruction
CPath();
CPath(const CPath& rPath);
2015-10-25 11:10:54 +00:00
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(DIR_CURRENT_DIRECTORY sdt, const char * NameExten = NULL);
CPath(DIR_MODULE_DIRECTORY sdt, const char * NameExten = NULL);
CPath(DIR_MODULE_FILE sdt);
virtual ~CPath();
//Setup & Cleanup
inline void Init();
inline void Exit();
//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;
bool operator != (const CPath& rPath) const;
operator const char *() const;
operator std::string &() { return m_strPath; }
//Get path components
2015-10-25 11:10:54 +00:00
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 GetCurrentDirectory(std::string& rDrive) const;
std::string GetCurrentDirectory(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;
//Get other state
2015-10-25 11:10:54 +00:00
bool IsEmpty() const { return m_strPath.empty(); }
bool IsRelative() const;
//Set path components
2015-10-25 11:10:54 +00:00
void SetDrive(char chDrive);
void SetDriveDirectory(const char * lpszDriveDirectory);
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);
2015-10-25 11:10:54 +00:00
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
void Empty() { m_strPath.erase(); }
void CurrentDirectory();
void Module();
2015-10-25 11:10:54 +00:00
void Module(void * hInstance);
void ModuleDirectory();
2015-10-25 11:10:54 +00:00
void ModuleDirectory(void * hInstance);
//Directory information
2015-10-25 11:10:54 +00:00
bool IsDirectory() const;
bool DirectoryExists() const;
//File Information
2015-10-25 11:10:54 +00:00
bool IsFile() const { return !IsDirectory(); }
bool Exists() const;
//Directory operations
2015-10-25 11:10:54 +00:00
bool DirectoryCreate(bool bCreateIntermediates = true);
bool ChangeDirectory();
//File operations
2015-10-25 11:10:54 +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
2015-10-25 11:10:54 +00:00
bool FindFirst(uint32_t dwAttributes = 0);
bool FindNext();
// Helpers
2015-10-25 11:10:54 +00:00
static void SethInst(void * hInst);
static void * GethInst();
private:
2015-10-25 11:10:54 +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;
};