139 lines
		
	
	
		
			3.9 KiB
		
	
	
	
		
			C++
		
	
	
	
		
		
			
		
	
	
			139 lines
		
	
	
		
			3.9 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() | |||
|  | { | |||
|  | 	// <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""; | |||
|  | } | |||
|  | 
 | |||
|  | 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) | |||
|  | { | |||
|  | 	std::wstringstream ss; | |||
|  | 	ss << value; | |||
|  | 	::WritePrivateProfileString(L"Default", key, ss.str().c_str(), getDefaultSavePath().c_str()); | |||
|  | } | |||
|  | 
 | |||
|  | void e2d::EFileUtils::saveDouble(LPCTSTR key, double value) | |||
|  | { | |||
|  | 	std::wstringstream ss; | |||
|  | 	ss << value; | |||
|  | 	::WritePrivateProfileString(L"Default", key, ss.str().c_str(), getDefaultSavePath().c_str()); | |||
|  | } | |||
|  | 
 | |||
|  | void e2d::EFileUtils::saveString(LPCTSTR key, EString value) | |||
|  | { | |||
|  | 	::WritePrivateProfileString(L"Default", key, value.c_str(), getDefaultSavePath().c_str()); | |||
|  | } | |||
|  | 
 | |||
|  | int e2d::EFileUtils::getInt(LPCTSTR key, int default) | |||
|  | { | |||
|  | 	return ::GetPrivateProfileInt(L"Default", key, default, getDefaultSavePath().c_str()); | |||
|  | } | |||
|  | 
 | |||
|  | double e2d::EFileUtils::getDouble(LPCTSTR key, double default) | |||
|  | { | |||
|  | 	// <20><> default <20><><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>Ϊ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
 | |||
|  | 	std::wstringstream ss; | |||
|  | 	ss << default; | |||
|  | 	// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>
 | |||
|  | 	TCHAR temp[128] = { 0 }; | |||
|  | 	::GetPrivateProfileString(L"Default", key, ss.str().c_str(), temp, 128, getDefaultSavePath().c_str()); | |||
|  | 	// ת<><D7AA>Ϊ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>
 | |||
|  | 	ss.str(L""); | |||
|  | 	ss << temp; | |||
|  | 	// <20><><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>ת<EFBFBD><D7AA>Ϊ double
 | |||
|  | 	return _wtof(ss.str().c_str()); | |||
|  | } | |||
|  | 
 | |||
|  | e2d::EString e2d::EFileUtils::geTString(LPCTSTR key, EString default) | |||
|  | { | |||
|  | 	TCHAR temp[128] = { 0 }; | |||
|  | 	::GetPrivateProfileString(L"Default", key, default.c_str(), temp, 128, getDefaultSavePath().c_str()); | |||
|  | 	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; | |||
|  | } | |||
|  | 
 | |||
|  | e2d::EString e2d::EFileUtils::getSaveFilePath(LPCTSTR title, LPCTSTR defExt) | |||
|  | { | |||
|  | 	// <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>
 | |||
|  | 	ofn.lpstrTitle = title;								// ʹ<><CAB9>ϵͳĬ<CDB3>ϱ<EFBFBD><CFB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ռ<EFBFBD><D5BC><EFBFBD>
 | |||
|  | 	ofn.lpstrDefExt = defExt;							// Ĭ<><C4AC><EFBFBD>ӵ<EFBFBD><D3B5><EFBFBD>չ<EFBFBD><D5B9>
 | |||
|  | 
 | |||
|  | 	if (GetSaveFileName(&ofn)) | |||
|  | 	{ | |||
|  | 		return strFilename; | |||
|  | 	} | |||
|  | 	return L""; | |||
|  | } |