131 lines
3.3 KiB
C++
131 lines
3.3 KiB
C++
#pragma region Includes
|
|
#define WIN32_LEAN_AND_MEAN
|
|
#define STRICT
|
|
#include <windows.h>
|
|
|
|
#include "PathName.h"
|
|
#include <strstream>
|
|
#pragma endregion
|
|
|
|
|
|
///////////////////////////////////////////////////////////
|
|
// utility
|
|
///////////////////////////////////////////////////////////
|
|
|
|
std::string& operator<<(std::string& str, int n)
|
|
{
|
|
std::strstream convert;
|
|
convert << n << '\0';
|
|
str += convert.str();
|
|
return str;
|
|
}
|
|
|
|
std::string& operator<<(std::string& str, char c)
|
|
{
|
|
const char s[2] = { c, '\0' };
|
|
str += s;
|
|
return str;
|
|
}
|
|
|
|
///////////////////////////////////////////////////////////
|
|
// CPathName
|
|
///////////////////////////////////////////////////////////
|
|
|
|
std::string CPathName::Path() const
|
|
{
|
|
char drive[_MAX_DRIVE], dir[_MAX_DIR];
|
|
_splitpath_s(c_str(), drive, sizeof(drive), dir, sizeof(dir), NULL, 0, NULL, 0);
|
|
std::string s = drive; s += dir;
|
|
return s;
|
|
}
|
|
|
|
std::string CPathName::FullName() const
|
|
{
|
|
char name[_MAX_FNAME], ext[_MAX_EXT];
|
|
_splitpath_s(c_str(), NULL, 0, NULL, 0, name, sizeof(name), ext, sizeof(ext));
|
|
std::string s = name; s += ext;
|
|
return s;
|
|
}
|
|
|
|
std::string CPathName::Name() const
|
|
{
|
|
char name[_MAX_FNAME];
|
|
_splitpath_s(c_str(), NULL, 0, NULL, 0, name, sizeof(name), NULL, 0);
|
|
return name;
|
|
}
|
|
std::string CPathName::Ext() const
|
|
{
|
|
char ext[_MAX_EXT];
|
|
_splitpath_s(c_str(), NULL, 0, NULL, 0, NULL, 0, ext, sizeof(ext));
|
|
return ext;
|
|
}
|
|
|
|
CPathName& CPathName::Set(const char* p, const char* n, const char* e)
|
|
{
|
|
char path[_MAX_PATH];
|
|
_makepath_s(path, sizeof(path), NULL, p, n, e);
|
|
return operator=(path);
|
|
}
|
|
|
|
CPathName& CPathName::SetPath(const char* p)
|
|
{
|
|
char name[_MAX_FNAME], ext[_MAX_EXT], path[_MAX_PATH];
|
|
_splitpath_s(c_str(), NULL, NULL, NULL, NULL, name, sizeof(name), ext, sizeof(ext));
|
|
_makepath_s(path, sizeof(path), NULL, p, name, ext);
|
|
return operator=(path);
|
|
}
|
|
|
|
CPathName& CPathName::SetName(const char* n)
|
|
{
|
|
char drive[_MAX_DRIVE], dir[_MAX_DIR], ext[_MAX_EXT], path[_MAX_PATH];
|
|
_splitpath_s(c_str(), drive, sizeof(drive), dir, sizeof(dir), NULL, 0, ext, sizeof(ext));
|
|
_makepath_s(path, sizeof(path), drive, dir, n, ext);
|
|
return operator=(path);
|
|
}
|
|
|
|
CPathName& CPathName::SetExt(const char* e)
|
|
{
|
|
char drive[_MAX_DRIVE], dir[_MAX_DIR], name[_MAX_FNAME];
|
|
_splitpath_s(c_str(), drive, sizeof(drive), dir, sizeof(dir), name, sizeof(name), NULL, 0);
|
|
char path[_MAX_PATH];
|
|
_makepath_s(path, sizeof(path), drive, dir, name, e);
|
|
return operator=(path);
|
|
}
|
|
|
|
bool CPathName::IsFile() const
|
|
{
|
|
if (empty())
|
|
return false;
|
|
DWORD dwAttrib = ::GetFileAttributesA(c_str());
|
|
return (dwAttrib != INVALID_FILE_ATTRIBUTES) && !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY);
|
|
}
|
|
|
|
bool CPathName::IsDirectory() const
|
|
{
|
|
if (empty())
|
|
return false;
|
|
DWORD dwAttrib = ::GetFileAttributesA(c_str());
|
|
return (dwAttrib != INVALID_FILE_ATTRIBUTES) && (dwAttrib & FILE_ATTRIBUTE_DIRECTORY);
|
|
}
|
|
|
|
size_t CPathName::List(std::vector<CPathName>& files) const
|
|
{
|
|
WIN32_FIND_DATAA ffd;
|
|
HANDLE hFind = ::FindFirstFileA(c_str(), &ffd);
|
|
while (hFind != INVALID_HANDLE_VALUE)
|
|
{
|
|
CPathName pn(ffd.cFileName);
|
|
pn.SetPath(Path().c_str());
|
|
files.push_back(pn);
|
|
|
|
if (!::FindNextFileA(hFind, &ffd))
|
|
{
|
|
::FindClose(hFind);
|
|
hFind = INVALID_HANDLE_VALUE;
|
|
}
|
|
}
|
|
return files.size();
|
|
}
|
|
|
|
|