2017-09-27 17:56:28 +08:00
|
|
|
|
#include "..\easy2d.h"
|
2017-09-10 23:56:52 +08:00
|
|
|
|
#include "..\EasyX\easyx.h"
|
2017-09-20 14:52:50 +08:00
|
|
|
|
#include <Shlobj.h>
|
|
|
|
|
|
#pragma comment(lib, "shell32.lib")
|
2017-09-27 17:56:28 +08:00
|
|
|
|
#include <sstream>
|
|
|
|
|
|
#include <algorithm>
|
2017-09-10 23:56:52 +08:00
|
|
|
|
|
2017-09-20 14:52:50 +08:00
|
|
|
|
#ifndef UNICODE
|
|
|
|
|
|
#include <io.h>
|
|
|
|
|
|
#include <direct.h>
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
2017-10-05 00:53:03 +08:00
|
|
|
|
TString FileUtils::getLocalAppDataPath()
|
2017-09-20 14:52:50 +08:00
|
|
|
|
{
|
|
|
|
|
|
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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-10-05 00:53:03 +08:00
|
|
|
|
TString FileUtils::getDefaultSavePath()
|
2017-09-20 14:52:50 +08:00
|
|
|
|
{
|
|
|
|
|
|
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);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-10-05 00:53:03 +08:00
|
|
|
|
TString path = m_lpszDefaultDir;
|
2017-09-20 14:52:50 +08:00
|
|
|
|
path.append(_T("\\"));
|
2017-10-12 02:44:44 +08:00
|
|
|
|
path.append(EApp::getAppName());
|
2017-09-20 14:52:50 +08:00
|
|
|
|
|
|
|
|
|
|
#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());
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-10-05 00:53:03 +08:00
|
|
|
|
void FileUtils::saveString(LPCTSTR key, TString value)
|
2017-09-20 14:52:50 +08:00
|
|
|
|
{
|
|
|
|
|
|
::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;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-10-05 00:53:03 +08:00
|
|
|
|
TString FileUtils::geTString(LPCTSTR key, TString default)
|
2017-09-20 14:52:50 +08:00
|
|
|
|
{
|
|
|
|
|
|
TCHAR temp[128] = { 0 };
|
|
|
|
|
|
::GetPrivateProfileString(_T("Default"), key, default.c_str(), temp, 128, getDefaultSavePath().c_str());
|
2017-10-05 00:53:03 +08:00
|
|
|
|
return TString(temp);
|
2017-09-20 14:52:50 +08:00
|
|
|
|
}
|
2017-09-10 23:56:52 +08:00
|
|
|
|
|
2017-10-05 00:53:03 +08:00
|
|
|
|
TString FileUtils::getFileExtension(const TString & filePath)
|
2017-09-10 23:56:52 +08:00
|
|
|
|
{
|
2017-10-05 00:53:03 +08:00
|
|
|
|
TString fileExtension;
|
2017-09-10 23:56:52 +08:00
|
|
|
|
// <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>
|
2017-10-05 00:53:03 +08:00
|
|
|
|
if (pos != TString::npos)
|
2017-09-10 23:56:52 +08:00
|
|
|
|
{
|
|
|
|
|
|
// <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-10-05 00:53:03 +08:00
|
|
|
|
bool FileUtils::getSaveFilePath(TString& path, LPCTSTR title, LPCTSTR defExt)
|
2017-09-10 23:56:52 +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 = _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;
|
|
|
|
|
|
}
|