Files
envi-code/SourceCode/Code2026/CoreCommand/FilePath.cpp
T

139 lines
3.0 KiB
C++
Raw Normal View History

2026-10-04 00:04:20 +08:00
#include "StdAfx.h"
2026-09-28 15:28:50 +08:00
#include "sys/stat.h"
#include "FilePath.h"
#include "Initools.h"
#pragma endregion
namespace
{
bool createDir(LPCTSTR psz)
{
if (!psz) return false;
2026-10-04 00:04:20 +08:00
// 先判断目录是否存在,如不存在,则新建,如存在,则返回
2026-09-28 15:28:50 +08:00
struct _stat fstat;
if ((_tstat(psz, &fstat) == 0) && (fstat.st_mode & _S_IFDIR))
return true;
return ::CreateDirectory(psz, NULL) ? true : false;
}
}
#pragma region _FilePath
FilePath::FilePath(LPCTSTR psz): _drive(_T(""))
, _directory(_T(""))
, _directory_without_drive(_T(""))
, _file_name(_T(""))
, _file_name_without_ext(_T(""))
, _ext(_T(""))
{
if (!psz) return;
CString str(psz);
int idx = str.ReverseFind(_T('.'));
if (idx != -1)
_ext = str.Mid(idx);
else
{
2026-10-04 00:04:20 +08:00
// 如果没有 '.' 则认为是一个目录
// 判断最后的字符是不是'\\'
2026-09-28 15:28:50 +08:00
if (str[str.GetLength() - 1] != _T('\\'))
str += _T('\\');
}
idx = str.Find(_T(':'));
if (idx != -1)
_drive = str.Mid(0, idx + 1);
idx = str.ReverseFind(_T('\\'));
if (idx != -1)
{
_file_name = str.Mid(idx + 1);
_directory = str.Mid(0, idx + 1);
int idx0 = str.Find(_T('\\'));
if (idx0 < idx)
{
_directory_without_drive = str.Mid(idx0, idx - idx0 + 1);
}
}
idx = _file_name.ReverseFind(_T('.'));
if (idx != -1)
_file_name_without_ext = _file_name.Mid(0, idx);
}
CString FilePath::getSystemTemporaryFolder()
{
TCHAR szPath[MAX_PATH];
::GetTempPath(MAX_PATH, szPath);
CString folder(_T(""));
folder += szPath;
folder += _T("earthworm.tmp");
if (createDir(folder))
return folder;
return _T("");
}
CString FilePath::getProgramDataFolder()
{
CString folderName(_T("earthworm")), version(_T(""));
2026-10-04 00:04:20 +08:00
// 读product.ini文件,EName字段确定文件夹
2026-09-28 15:28:50 +08:00
std::map<CString, CString> mm;
IniTools::getSectionContent(GetSysDir() + _T("\\product.ini"), _T("Product"), mm);
std::map<CString, CString>::const_iterator it(mm.end());
if (!mm.empty())
{
it = mm.find(_T("EName"));
if (it != mm.end())
folderName = it->second;
it = mm.find(_T("Version"));
if (it != mm.end())
version = it->second;
}
TCHAR szPath[MAX_PATH];
::SHGetFolderPath(NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE, NULL, 0, szPath);
CString folder(_T(""));
folder += szPath;
folder += _T("\\ShangHuaSoft");
if (createDir(folder))
{
folder += _T("\\") + folderName;
folder += _T("-");
folder += version;
if (createDir(folder))
return folder;
}
return _T("");
}
CString FilePath::GetRootDir()
{
CString arx = GetArxDir();
CString root = arx.Left(arx.ReverseFind(_T('\\')));
return root;
}
CString FilePath::GetArxDir()
{
TCHAR szPath[MAX_PATH];
::GetModuleFileName(_hdllInstance, szPath, MAX_PATH);
CString sPath(szPath);
sPath = sPath.Left(sPath.ReverseFind(_T('\\')));
return sPath;
}
CString FilePath::GetSysDir()
{
CString arx = GetArxDir();
CString sys = arx.Left(arx.ReverseFind(_T('\\')));
return sys + _T("\\sys");
}
CString FilePath::GetEqu2dDir()
{
CString ss(GetSysDir());
ss = ss.Left(ss.ReverseFind(_T('\\'))) + _T("\\Equipment2D");
return ss;
}
#pragma endregion