169 lines
		
	
	
		
			4.1 KiB
		
	
	
	
		
			C++
		
	
	
	
		
		
			
		
	
	
			169 lines
		
	
	
		
			4.1 KiB
		
	
	
	
		
			C++
		
	
	
	
|  | #include "..\easy2d.h"
 | |||
|  | #include "..\EasyX\easyx.h"
 | |||
|  | #include <Shlobj.h>
 | |||
|  | #pragma comment(lib, "shell32.lib")
 | |||
|  | #include <sstream>
 | |||
|  | #include <algorithm>
 | |||
|  | 
 | |||
|  | #ifndef UNICODE
 | |||
|  | 	#include <io.h>
 | |||
|  | 	#include <direct.h>
 | |||
|  | #endif
 | |||
|  | 
 | |||
|  | TString FileUtils::getLocalAppDataPath() | |||
|  | { | |||
|  | 	TCHAR m_lpszDefaultDir[MAX_PATH] = { 0 }; | |||
|  | 	TCHAR szDocument[MAX_PATH] = { 0 }; | |||
|  | 
 | |||
|  | 	// <20><>ȡ AppData\Local <20>ļ<EFBFBD><C4BC>е<EFBFBD> ID
 | |||
|  | 	LPITEMIDLIST pidl = NULL; | |||
|  | 	SHGetSpecialFolderLocation(NULL, CSIDL_LOCAL_APPDATA, &pidl); | |||
|  | 	if (pidl && SHGetPathFromIDList(pidl, szDocument)) | |||
|  | 	{ | |||
|  | 		// <20><>ȡ<EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>·<EFBFBD><C2B7>
 | |||
|  | 		GetShortPathName(szDocument, m_lpszDefaultDir, _MAX_PATH); | |||
|  | 	} | |||
|  | 
 | |||
|  | 	return m_lpszDefaultDir; | |||
|  | } | |||
|  | 
 | |||
|  | TString FileUtils::getDefaultSavePath() | |||
|  | { | |||
|  | 	TCHAR m_lpszDefaultDir[MAX_PATH] = { 0 }; | |||
|  | 	TCHAR szDocument[MAX_PATH] = { 0 }; | |||
|  | 
 | |||
|  | 	// <20><>ȡ AppData\Local <20>ļ<EFBFBD><C4BC>е<EFBFBD> ID
 | |||
|  | 	LPITEMIDLIST pidl = NULL; | |||
|  | 	SHGetSpecialFolderLocation(NULL, CSIDL_LOCAL_APPDATA, &pidl); | |||
|  | 	if (pidl && SHGetPathFromIDList(pidl, szDocument)) | |||
|  | 	{ | |||
|  | 		// <20><>ȡ<EFBFBD>ļ<EFBFBD><C4BC><EFBFBD>·<EFBFBD><C2B7>
 | |||
|  | 		GetShortPathName(szDocument, m_lpszDefaultDir, _MAX_PATH); | |||
|  | 	} | |||
|  | 
 | |||
|  | 	TString path = m_lpszDefaultDir; | |||
|  | 	path.append(_T("\\")); | |||
|  | 	path.append(EApp::getAppName()); | |||
|  | 
 | |||
|  | #ifdef UNICODE
 | |||
|  | 	if (_waccess(path.c_str(), 0) == -1) | |||
|  | 	{ | |||
|  | 		_wmkdir(path.c_str()); | |||
|  | 	} | |||
|  | #else
 | |||
|  | 	if (_access(path.c_str(), 0) == -1) | |||
|  | 	{ | |||
|  | 		_mkdir(path.c_str()); | |||
|  | 	} | |||
|  | #endif
 | |||
|  | 
 | |||
|  | 	path.append(_T("\\DefaultData.ini")); | |||
|  | 
 | |||
|  | 	return path; | |||
|  | } | |||
|  | 
 | |||
|  | void FileUtils::saveInt(LPCTSTR key, int value) | |||
|  | { | |||
|  | #ifdef UNICODE
 | |||
|  | 	std::wstringstream ss; | |||
|  | #else
 | |||
|  | 	std::stringstream ss; | |||
|  | #endif
 | |||
|  | 	ss << value; | |||
|  | 	::WritePrivateProfileString(_T("Default"), key, ss.str().c_str(), getDefaultSavePath().c_str()); | |||
|  | } | |||
|  | 
 | |||
|  | void FileUtils::saveDouble(LPCTSTR key, double value) | |||
|  | { | |||
|  | #ifdef UNICODE
 | |||
|  | 	std::wstringstream ss; | |||
|  | #else
 | |||
|  | 	std::stringstream ss; | |||
|  | #endif
 | |||
|  | 	ss << value; | |||
|  | 	::WritePrivateProfileString(_T("Default"), key, ss.str().c_str(), getDefaultSavePath().c_str()); | |||
|  | } | |||
|  | 
 | |||
|  | void FileUtils::saveString(LPCTSTR key, TString value) | |||
|  | { | |||
|  | 	::WritePrivateProfileString(_T("Default"), key, value.c_str(), getDefaultSavePath().c_str()); | |||
|  | } | |||
|  | 
 | |||
|  | int FileUtils::getInt(LPCTSTR key, int default) | |||
|  | { | |||
|  | 	return ::GetPrivateProfileInt(_T("Default"), key, default, getDefaultSavePath().c_str()); | |||
|  | } | |||
|  | #include <iostream>
 | |||
|  | using namespace std; | |||
|  | double FileUtils::getDouble(LPCTSTR key, double default) | |||
|  | { | |||
|  | 	// <20><> default <20><><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>Ϊ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
 | |||
|  | #ifdef UNICODE
 | |||
|  | 	std::wstringstream ss; | |||
|  | #else
 | |||
|  | 	std::stringstream ss; | |||
|  | #endif
 | |||
|  | 	ss << default; | |||
|  | 	// <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>
 | |||
|  | 	TCHAR temp[128] = { 0 }; | |||
|  | 	::GetPrivateProfileString(_T("Default"), key, ss.str().c_str(), temp, 128, getDefaultSavePath().c_str()); | |||
|  | 	// ת<><D7AA>Ϊ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>
 | |||
|  | 	ss.str(_T("")); | |||
|  | 	ss << temp; | |||
|  | 	// <20><><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>ת<EFBFBD><D7AA>Ϊ double
 | |||
|  | #ifdef UNICODE
 | |||
|  | 	double d = _wtof(ss.str().c_str()); | |||
|  | #else
 | |||
|  | 	double d = atof(ss.str().c_str()); | |||
|  | #endif
 | |||
|  | 	return d; | |||
|  | } | |||
|  | 
 | |||
|  | TString FileUtils::geTString(LPCTSTR key, TString default) | |||
|  | { | |||
|  | 	TCHAR temp[128] = { 0 }; | |||
|  | 	::GetPrivateProfileString(_T("Default"), key, default.c_str(), temp, 128, getDefaultSavePath().c_str()); | |||
|  | 	return TString(temp); | |||
|  | } | |||
|  | 
 | |||
|  | TString FileUtils::getFileExtension(const TString & filePath) | |||
|  | { | |||
|  | 	TString 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 != TString::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; | |||
|  | } | |||
|  | 
 | |||
|  | bool FileUtils::getSaveFilePath(TString& path, 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 = _T("<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)) | |||
|  | 	{ | |||
|  | 		path = strFilename; | |||
|  | 		return true; | |||
|  | 	} | |||
|  | 	return false; | |||
|  | } |