2017-10-19 12:48:58 +08:00
|
|
|
|
#include "..\etools.h"
|
|
|
|
|
|
#include "..\Win\winbase.h"
|
|
|
|
|
|
#include <sstream>
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
#include <commdlg.h>
|
|
|
|
|
|
|
|
|
|
|
|
#define DEFINE_KNOWN_FOLDER(name, l, w1, w2, b1, b2, b3, b4, b5, b6, b7, b8) \
|
|
|
|
|
|
EXTERN_C const GUID DECLSPEC_SELECTANY name \
|
|
|
|
|
|
= { l, w1, w2,{ b1, b2, b3, b4, b5, b6, b7, b8 } }
|
|
|
|
|
|
|
|
|
|
|
|
DEFINE_KNOWN_FOLDER(FOLDERID_LocalAppData, 0xF1B32785, 0x6FBA, 0x4FCF, 0x9D, 0x55, 0x7B, 0x8E, 0x7F, 0x15, 0x70, 0x91);
|
|
|
|
|
|
|
|
|
|
|
|
typedef HRESULT(WINAPI* pFunSHGetKnownFolderPath)(
|
|
|
|
|
|
const GUID& rfid,
|
|
|
|
|
|
DWORD dwFlags,
|
|
|
|
|
|
HANDLE hToken,
|
|
|
|
|
|
PWSTR *ppszPath);
|
|
|
|
|
|
|
|
|
|
|
|
e2d::EString e2d::EFileUtils::getLocalAppDataPath()
|
|
|
|
|
|
{
|
|
|
|
|
|
// <20><>ȡ AppData\Local <20>ļ<EFBFBD><C4BC>е<EFBFBD>·<EFBFBD><C2B7>
|
|
|
|
|
|
PWSTR pszPath = NULL;
|
|
|
|
|
|
HMODULE hModule = LoadLibrary(L"shell32.dll");
|
|
|
|
|
|
pFunSHGetKnownFolderPath SHGetKnownFolderPath = (pFunSHGetKnownFolderPath)GetProcAddress(hModule, "SHGetKnownFolderPath");
|
|
|
|
|
|
HRESULT hr = SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, NULL, &pszPath);
|
|
|
|
|
|
|
|
|
|
|
|
if (SUCCEEDED(hr))
|
|
|
|
|
|
{
|
|
|
|
|
|
EString path = pszPath;
|
|
|
|
|
|
CoTaskMemFree(pszPath);
|
|
|
|
|
|
return path;
|
|
|
|
|
|
}
|
|
|
|
|
|
return L"";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-10-31 17:19:13 +08:00
|
|
|
|
e2d::EString e2d::EFileUtils::getTempPath()
|
|
|
|
|
|
{
|
|
|
|
|
|
// <20><>ȡ<EFBFBD><C8A1>ʱ<EFBFBD>ļ<EFBFBD>Ŀ¼
|
|
|
|
|
|
TCHAR path[_MAX_PATH];
|
|
|
|
|
|
::GetTempPath(_MAX_PATH, path);
|
|
|
|
|
|
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD>ļ<EFBFBD>Ŀ¼
|
|
|
|
|
|
e2d::EString tempFilePath = path + e2d::EApp::getAppName();
|
|
|
|
|
|
if (_waccess(tempFilePath.c_str(), 0) == -1)
|
|
|
|
|
|
{
|
|
|
|
|
|
_wmkdir(tempFilePath.c_str());
|
|
|
|
|
|
}
|
|
|
|
|
|
return tempFilePath;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-10-19 12:48:58 +08:00
|
|
|
|
e2d::EString e2d::EFileUtils::getDefaultSavePath()
|
|
|
|
|
|
{
|
|
|
|
|
|
EString path = EFileUtils::getLocalAppDataPath();
|
|
|
|
|
|
WARN_IF(path.empty(), "Cannot get local AppData path!");
|
|
|
|
|
|
|
|
|
|
|
|
path.append(L"\\");
|
|
|
|
|
|
path.append(EApp::getAppName());
|
|
|
|
|
|
|
|
|
|
|
|
if (_waccess(path.c_str(), 0) == -1)
|
|
|
|
|
|
{
|
|
|
|
|
|
_wmkdir(path.c_str());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
path.append(L"\\DefaultData.ini");
|
|
|
|
|
|
|
|
|
|
|
|
return path;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-11-07 23:32:44 +08:00
|
|
|
|
void e2d::EFileUtils::saveInt(const EString & key, int value)
|
2017-10-19 12:48:58 +08:00
|
|
|
|
{
|
2017-11-07 23:32:44 +08:00
|
|
|
|
::WritePrivateProfileString(L"Default", key.c_str(), std::to_wstring(value).c_str(), getDefaultSavePath().c_str());
|
2017-10-19 12:48:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-11-07 23:32:44 +08:00
|
|
|
|
void e2d::EFileUtils::saveFloat(const EString & key, float value)
|
2017-10-19 12:48:58 +08:00
|
|
|
|
{
|
2017-11-07 23:32:44 +08:00
|
|
|
|
::WritePrivateProfileString(L"Default", key.c_str(), std::to_wstring(value).c_str(), getDefaultSavePath().c_str());
|
2017-10-19 12:48:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-11-07 23:32:44 +08:00
|
|
|
|
void e2d::EFileUtils::saveString(const EString & key, const EString & value)
|
2017-10-19 12:48:58 +08:00
|
|
|
|
{
|
2017-11-07 23:32:44 +08:00
|
|
|
|
::WritePrivateProfileString(L"Default", key.c_str(), value.c_str(), getDefaultSavePath().c_str());
|
2017-10-19 12:48:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-11-07 23:32:44 +08:00
|
|
|
|
int e2d::EFileUtils::getInt(const EString & key, int default)
|
2017-10-19 12:48:58 +08:00
|
|
|
|
{
|
2017-11-07 23:32:44 +08:00
|
|
|
|
return ::GetPrivateProfileInt(L"Default", key.c_str(), default, getDefaultSavePath().c_str());
|
2017-10-19 12:48:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-11-07 23:32:44 +08:00
|
|
|
|
float e2d::EFileUtils::getFloat(const EString & key, float default)
|
2017-10-19 12:48:58 +08:00
|
|
|
|
{
|
2017-10-25 10:54:03 +08:00
|
|
|
|
TCHAR temp[32] = { 0 };
|
2017-11-07 23:32:44 +08:00
|
|
|
|
::GetPrivateProfileString(L"Default", key.c_str(), std::to_wstring(default).c_str(), temp, 31, getDefaultSavePath().c_str());
|
2017-10-25 10:54:03 +08:00
|
|
|
|
return std::stof(temp);
|
2017-10-19 12:48:58 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-11-09 21:04:39 +08:00
|
|
|
|
e2d::EString e2d::EFileUtils::getString(const EString & key, const EString & default)
|
2017-10-19 12:48:58 +08:00
|
|
|
|
{
|
2017-10-25 10:54:03 +08:00
|
|
|
|
TCHAR temp[256] = { 0 };
|
2017-11-07 23:32:44 +08:00
|
|
|
|
::GetPrivateProfileString(L"Default", key.c_str(), default.c_str(), temp, 255, getDefaultSavePath().c_str());
|
2017-10-19 12:48:58 +08:00
|
|
|
|
return EString(temp);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
e2d::EString e2d::EFileUtils::getFileExtension(const EString & filePath)
|
|
|
|
|
|
{
|
|
|
|
|
|
EString fileExtension;
|
|
|
|
|
|
// <20>ҵ<EFBFBD><D2B5>ļ<EFBFBD><C4BC><EFBFBD><EFBFBD>е<EFBFBD><D0B5><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB> '.' <20><>λ<EFBFBD><CEBB>
|
|
|
|
|
|
size_t pos = filePath.find_last_of('.');
|
|
|
|
|
|
// <20>ж<EFBFBD> pos <20>Ƿ<EFBFBD><C7B7>Ǹ<EFBFBD><C7B8><EFBFBD>Чλ<D0A7><CEBB>
|
|
|
|
|
|
if (pos != EString::npos)
|
|
|
|
|
|
{
|
|
|
|
|
|
// <20><>ȡ<EFBFBD><C8A1>չ<EFBFBD><D5B9>
|
|
|
|
|
|
fileExtension = filePath.substr(pos, filePath.length());
|
|
|
|
|
|
// ת<><D7AA>ΪСд<D0A1><D0B4>ĸ
|
|
|
|
|
|
std::transform(fileExtension.begin(), fileExtension.end(), fileExtension.begin(), ::tolower);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return fileExtension;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-11-07 23:32:44 +08:00
|
|
|
|
e2d::EString e2d::EFileUtils::getSaveFilePath(const EString & title, const EString & defExt)
|
2017-10-19 12:48:58 +08:00
|
|
|
|
{
|
|
|
|
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ի<EFBFBD><D4BB><EFBFBD>
|
|
|
|
|
|
OPENFILENAME ofn = { 0 };
|
|
|
|
|
|
TCHAR strFilename[MAX_PATH] = { 0 }; // <20><><EFBFBD>ڽ<EFBFBD><DABD><EFBFBD><EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>
|
|
|
|
|
|
ofn.lStructSize = sizeof(OPENFILENAME); // <20>ṹ<EFBFBD><E1B9B9><EFBFBD><EFBFBD>С
|
|
|
|
|
|
ofn.hwndOwner = GetHWnd(); // ӵ<><D3B5><EFBFBD>Ŵ<EFBFBD><C5B4>ھ<EFBFBD><DABE><EFBFBD><EFBFBD><EFBFBD>NULL <20><>ʾ<EFBFBD>Ի<EFBFBD><D4BB><EFBFBD><EFBFBD>Ƿ<EFBFBD>ģ̬<C4A3><CCAC>
|
|
|
|
|
|
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 = NULL; // <20><>ʼĿ¼ΪĬ<CEAA><C4AC>
|
|
|
|
|
|
ofn.Flags = OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;// Ŀ¼<C4BF><C2BC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڣ<EFBFBD><DAA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ļ<EFBFBD>ǰ<EFBFBD><C7B0><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2017-11-07 23:32:44 +08:00
|
|
|
|
ofn.lpstrTitle = title.c_str(); // ʹ<><CAB9>ϵͳĬ<CDB3>ϱ<EFBFBD><CFB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ռ<EFBFBD><D5BC><EFBFBD>
|
|
|
|
|
|
ofn.lpstrDefExt = defExt.c_str(); // Ĭ<><C4AC><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD>չ<EFBFBD><D5B9>
|
2017-10-19 12:48:58 +08:00
|
|
|
|
|
|
|
|
|
|
if (GetSaveFileName(&ofn))
|
|
|
|
|
|
{
|
|
|
|
|
|
return strFilename;
|
|
|
|
|
|
}
|
|
|
|
|
|
return L"";
|
|
|
|
|
|
}
|