127 lines
3.7 KiB
C++
127 lines
3.7 KiB
C++
#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()
|
||
{
|
||
// »ñÈ¡ AppData\Local Îļþ¼ÐµÄ·¾¶
|
||
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"";
|
||
}
|
||
|
||
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;
|
||
}
|
||
|
||
void e2d::EFileUtils::saveInt(LPCTSTR key, int value)
|
||
{
|
||
::WritePrivateProfileString(L"Default", key, std::to_wstring(value).c_str(), getDefaultSavePath().c_str());
|
||
}
|
||
|
||
void e2d::EFileUtils::saveFloat(LPCTSTR key, float value)
|
||
{
|
||
::WritePrivateProfileString(L"Default", key, std::to_wstring(value).c_str(), getDefaultSavePath().c_str());
|
||
}
|
||
|
||
void e2d::EFileUtils::saveString(LPCTSTR key, LPCTSTR value)
|
||
{
|
||
::WritePrivateProfileString(L"Default", key, value, getDefaultSavePath().c_str());
|
||
}
|
||
|
||
int e2d::EFileUtils::getInt(LPCTSTR key, int default)
|
||
{
|
||
return ::GetPrivateProfileInt(L"Default", key, default, getDefaultSavePath().c_str());
|
||
}
|
||
|
||
float e2d::EFileUtils::getFloat(LPCTSTR key, float default)
|
||
{
|
||
TCHAR temp[32] = { 0 };
|
||
::GetPrivateProfileString(L"Default", key, std::to_wstring(default).c_str(), temp, 31, getDefaultSavePath().c_str());
|
||
return std::stof(temp);
|
||
}
|
||
|
||
e2d::EString e2d::EFileUtils::geTString(LPCTSTR key, LPCTSTR default)
|
||
{
|
||
TCHAR temp[256] = { 0 };
|
||
::GetPrivateProfileString(L"Default", key, default, temp, 255, getDefaultSavePath().c_str());
|
||
return EString(temp);
|
||
}
|
||
|
||
e2d::EString e2d::EFileUtils::getFileExtension(const EString & filePath)
|
||
{
|
||
EString fileExtension;
|
||
// ÕÒµ½ÎļþÃûÖеÄ×îºóÒ»¸ö '.' µÄλÖÃ
|
||
size_t pos = filePath.find_last_of('.');
|
||
// ÅÐ¶Ï pos ÊÇ·ñÊǸöÓÐЧλÖÃ
|
||
if (pos != EString::npos)
|
||
{
|
||
// ½ØÈ¡À©Õ¹Ãû
|
||
fileExtension = filePath.substr(pos, filePath.length());
|
||
// ת»»ÎªÐ¡Ð´×Öĸ
|
||
std::transform(fileExtension.begin(), fileExtension.end(), fileExtension.begin(), ::tolower);
|
||
}
|
||
|
||
return fileExtension;
|
||
}
|
||
|
||
e2d::EString e2d::EFileUtils::getSaveFilePath(LPCTSTR title, LPCTSTR defExt)
|
||
{
|
||
// µ¯³ö±£´æ¶Ô»°¿ò
|
||
OPENFILENAME ofn = { 0 };
|
||
TCHAR strFilename[MAX_PATH] = { 0 }; // ÓÃÓÚ½ÓÊÕÎļþÃû
|
||
ofn.lStructSize = sizeof(OPENFILENAME); // ½á¹¹Ìå´óС
|
||
ofn.hwndOwner = GetHWnd(); // ÓµÓÐ×Å´°¿Ú¾ä±ú£¬NULL ±íʾ¶Ô»°¿òÊÇ·Çģ̬µÄ
|
||
ofn.lpstrFilter = L"ËùÓÐÎļþ\0*.*\0\0"; // ÉèÖùýÂË
|
||
ofn.nFilterIndex = 1; // ¹ýÂËÆ÷Ë÷Òý
|
||
ofn.lpstrFile = strFilename; // ½ÓÊÕ·µ»ØµÄÎļþ·¾¶ºÍÎļþÃû
|
||
ofn.nMaxFile = sizeof(strFilename); // »º³åÇø³¤¶È
|
||
ofn.lpstrInitialDir = NULL; // ³õʼĿ¼ΪĬÈÏ
|
||
ofn.Flags = OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;// Ŀ¼±ØÐë´æÔÚ£¬¸²¸ÇÎļþǰ·¢³ö¾¯¸æ
|
||
ofn.lpstrTitle = title; // ʹÓÃϵͳĬÈϱêÌâÁô¿Õ¼´¿É
|
||
ofn.lpstrDefExt = defExt; // ĬÈÏ×·¼ÓµÄÀ©Õ¹Ãû
|
||
|
||
if (GetSaveFileName(&ofn))
|
||
{
|
||
return strFilename;
|
||
}
|
||
return L"";
|
||
}
|