2018-07-08 02:41:44 +08:00
|
|
|
|
#include "..\e2dtool.h"
|
|
|
|
|
|
#include <commdlg.h>
|
|
|
|
|
|
|
|
|
|
|
|
std::list<e2d::String> e2d::File::_searchPaths;
|
|
|
|
|
|
|
|
|
|
|
|
e2d::File::File()
|
|
|
|
|
|
: _fileName()
|
|
|
|
|
|
, _attributes(0)
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
e2d::File::File(const String & fileName)
|
|
|
|
|
|
: _fileName(fileName)
|
|
|
|
|
|
, _attributes(0)
|
|
|
|
|
|
{
|
|
|
|
|
|
this->open(fileName);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
e2d::File::~File()
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool e2d::File::open(const String & fileName)
|
|
|
|
|
|
{
|
|
|
|
|
|
auto FindFile = [=](const String & path) -> bool
|
|
|
|
|
|
{
|
|
|
|
|
|
if (::_waccess((const wchar_t*)path, 0) == 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
_attributes = ::GetFileAttributes((LPCTSTR)path);
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
if (FindFile(fileName))
|
|
|
|
|
|
{
|
|
|
|
|
|
_fileName = fileName;
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
2018-08-12 14:30:28 +08:00
|
|
|
|
for (const auto& resPath : _searchPaths)
|
2018-07-08 02:41:44 +08:00
|
|
|
|
{
|
|
|
|
|
|
if (FindFile(resPath + fileName))
|
|
|
|
|
|
{
|
|
|
|
|
|
_fileName = resPath + fileName;
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool e2d::File::exists() const
|
|
|
|
|
|
{
|
|
|
|
|
|
return ::_waccess((const wchar_t*)_fileName, 0) == 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool e2d::File::isFolder() const
|
|
|
|
|
|
{
|
2018-07-16 22:43:56 +08:00
|
|
|
|
return (_attributes & FILE_ATTRIBUTE_DIRECTORY) != 0;
|
2018-07-08 02:41:44 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
e2d::String e2d::File::getFilePath() const
|
|
|
|
|
|
{
|
|
|
|
|
|
return _fileName;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
e2d::String e2d::File::getExtension() const
|
|
|
|
|
|
{
|
|
|
|
|
|
String fileExtension;
|
|
|
|
|
|
// <20>ҵ<EFBFBD><D2B5>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB> '.' <20><>λ<EFBFBD><CEBB>
|
|
|
|
|
|
size_t pos = _fileName.getWString().find_last_of(L'.');
|
|
|
|
|
|
// <20>ж<EFBFBD> pos <20>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD>Чλ<D0A7><CEBB>
|
|
|
|
|
|
if (pos != std::wstring::npos)
|
|
|
|
|
|
{
|
|
|
|
|
|
// <20><>ȡ<EFBFBD><C8A1>չ<EFBFBD><D5B9>
|
|
|
|
|
|
fileExtension = _fileName.subtract(static_cast<int>(pos));
|
|
|
|
|
|
// ת<><D7AA>ΪСд<D0A1><D0B4>ĸ
|
|
|
|
|
|
fileExtension = fileExtension.toLower();
|
|
|
|
|
|
}
|
|
|
|
|
|
return fileExtension;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool e2d::File::deleteFile()
|
|
|
|
|
|
{
|
|
|
|
|
|
if (::DeleteFile((LPCWSTR)_fileName))
|
|
|
|
|
|
return true;
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
e2d::File e2d::File::extract(int resNameId, const String & resType, const String& destFileName)
|
|
|
|
|
|
{
|
|
|
|
|
|
String destFilePath = Path::getTempPath() + destFileName;
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>
|
|
|
|
|
|
HANDLE hFile = ::CreateFile((LPCWSTR)destFilePath, GENERIC_WRITE, NULL, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_TEMPORARY, NULL);
|
|
|
|
|
|
if (hFile == INVALID_HANDLE_VALUE)
|
|
|
|
|
|
return std::move(File());
|
|
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD>ļ<EFBFBD><C4BC>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD><D4B4><EFBFBD>ڴ桢<DAB4>õ<EFBFBD><C3B5><EFBFBD>Դ<EFBFBD><D4B4>С
|
|
|
|
|
|
HRSRC hRes = ::FindResource(NULL, MAKEINTRESOURCE(resNameId), (LPCWSTR)resType);
|
|
|
|
|
|
HGLOBAL hMem = ::LoadResource(NULL, hRes);
|
|
|
|
|
|
DWORD dwSize = ::SizeofResource(NULL, hRes);
|
|
|
|
|
|
|
|
|
|
|
|
if (hRes && hMem && dwSize)
|
|
|
|
|
|
{
|
|
|
|
|
|
// д<><D0B4><EFBFBD>ļ<EFBFBD>
|
|
|
|
|
|
DWORD dwWrite = 0;
|
|
|
|
|
|
::WriteFile(hFile, hMem, dwSize, &dwWrite, NULL);
|
|
|
|
|
|
::CloseHandle(hFile);
|
|
|
|
|
|
return File(destFilePath);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
::CloseHandle(hFile);
|
|
|
|
|
|
::DeleteFile((LPCWSTR)destFilePath);
|
|
|
|
|
|
return std::move(File());
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void e2d::File::addSearchPath(const String & path)
|
|
|
|
|
|
{
|
|
|
|
|
|
String tmp = path;
|
|
|
|
|
|
tmp.replace(L"/", L"\\");
|
|
|
|
|
|
if (tmp[tmp.getLength() - 1] != L'\\')
|
|
|
|
|
|
{
|
|
|
|
|
|
tmp << L"\\";
|
|
|
|
|
|
}
|
|
|
|
|
|
auto iter = std::find(_searchPaths.cbegin(), _searchPaths.cend(), tmp);
|
|
|
|
|
|
if (iter == _searchPaths.cend())
|
|
|
|
|
|
{
|
|
|
|
|
|
_searchPaths.push_front(path);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
bool e2d::File::createFolder(const String & dirPath)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (dirPath.isEmpty() || dirPath.getLength() >= MAX_PATH)
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
wchar_t tmpDirPath[_MAX_PATH] = { 0 };
|
|
|
|
|
|
int length = dirPath.getLength();
|
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < length; ++i)
|
|
|
|
|
|
{
|
|
|
|
|
|
tmpDirPath[i] = dirPath.at(i);
|
|
|
|
|
|
if (tmpDirPath[i] == L'\\' || tmpDirPath[i] == L'/' || i == (length - 1))
|
|
|
|
|
|
{
|
|
|
|
|
|
if (::_waccess(tmpDirPath, 0) != 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (::_wmkdir(tmpDirPath) != 0)
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
e2d::String e2d::File::getSaveFilePath(const String& title, const String& defExt)
|
|
|
|
|
|
{
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ի<EFBFBD><D4BB><EFBFBD>
|
|
|
|
|
|
OPENFILENAME ofn = { 0 };
|
|
|
|
|
|
wchar_t strFilename[MAX_PATH] = { 0 }; // <20><><EFBFBD>ڽ<EFBFBD><DABD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>
|
|
|
|
|
|
ofn.lStructSize = sizeof(OPENFILENAME); // <20>ṹ<EFBFBD><E1B9B9><EFBFBD><EFBFBD>С
|
|
|
|
|
|
ofn.hwndOwner = Window::getInstance()->getHWnd(); // <20><><EFBFBD>ھ<EFBFBD><DABE><EFBFBD>
|
|
|
|
|
|
ofn.lpstrFilter = L"<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>\0*.*\0\0"; // <20><><EFBFBD>ù<EFBFBD><C3B9><EFBFBD>
|
|
|
|
|
|
ofn.nFilterIndex = 1; // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
ofn.lpstrFile = strFilename; // <20><><EFBFBD>շ<EFBFBD><D5B7>ص<EFBFBD><D8B5>ļ<EFBFBD>·<EFBFBD><C2B7><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>
|
|
|
|
|
|
ofn.nMaxFile = sizeof(strFilename); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
ofn.lpstrInitialDir = nullptr; // <20><>ʼĿ¼ΪĬ<CEAA><C4AC>
|
|
|
|
|
|
ofn.Flags = OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
|
|
|
|
|
|
ofn.lpstrTitle = (LPCWSTR)title; // <20><><EFBFBD><EFBFBD>
|
|
|
|
|
|
ofn.lpstrDefExt = (LPCWSTR)defExt; // Ĭ<><C4AC><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD>չ<EFBFBD><D5B9>
|
|
|
|
|
|
|
|
|
|
|
|
if (GetSaveFileName(&ofn))
|
|
|
|
|
|
{
|
|
|
|
|
|
return strFilename;
|
|
|
|
|
|
}
|
|
|
|
|
|
return std::move(String());
|
|
|
|
|
|
}
|