libcmo21/LibCmo/CK2/MgrImpls/CKPathManager.cpp

106 lines
2.6 KiB
C++
Raw Normal View History

2023-09-04 22:58:53 +08:00
#include "CKPathManager.hpp"
namespace LibCmo::CK2::MgrImpls {
2024-08-23 11:28:49 +08:00
static constexpr char8_t g_UniqueFolder[] = u8"LibCmo";
2023-09-06 10:42:23 +08:00
CKPathManager::CKPathManager(CKContext* ctx) :
2024-08-23 11:28:49 +08:00
CKBaseManager(ctx, PATH_MANAGER_GUID, u8"Path Manager"),
m_TempFolder(), m_ExtraPathes() {
2023-09-06 10:42:23 +08:00
// preset for temp folder
// todo: add current CKContext pointer as the part of temp path.
// thus multiple CKContext can work.
m_TempFolder = std::filesystem::temp_directory_path();
2024-08-23 11:28:49 +08:00
m_TempFolder /= std::filesystem::path(g_UniqueFolder);
2023-09-06 10:42:23 +08:00
std::filesystem::create_directories(m_TempFolder);
}
CKPathManager::~CKPathManager() {}
bool CKPathManager::SetTempFolder(CKSTRING u8_temp) {
if (u8_temp == nullptr) return false;
2024-08-23 11:28:49 +08:00
std::filesystem::path cache(u8_temp);
if (std::filesystem::is_directory(cache)) {
m_TempFolder = cache;
return true;
} else {
return false;
}
2023-09-06 10:42:23 +08:00
}
2023-09-16 18:31:25 +08:00
XContainer::XString CKPathManager::GetTempFolder() {
2024-08-23 11:28:49 +08:00
return this->m_TempFolder.u8string();
2023-09-06 10:42:23 +08:00
}
2023-09-16 18:31:25 +08:00
XContainer::XString CKPathManager::GetTempFilePath(CKSTRING u8_filename) {
if (u8_filename == nullptr) return XContainer::XString();
2024-08-23 11:28:49 +08:00
std::filesystem::path stdfilename(u8_filename);
2023-09-06 10:42:23 +08:00
auto realfile = this->m_TempFolder / stdfilename;
2024-08-23 11:28:49 +08:00
return realfile.u8string();
2023-09-06 10:42:23 +08:00
}
bool CKPathManager::AddPath(CKSTRING u8path) {
if (u8path == nullptr) return false;
2024-08-23 11:28:49 +08:00
std::filesystem::path newpath(u8path);
if (std::filesystem::is_directory(newpath)) {
m_ExtraPathes.emplace_back(std::move(newpath));
return true;
} else {
return false;
}
}
void CKPathManager::ClearPath() {
m_ExtraPathes.clear();
}
2023-09-16 18:31:25 +08:00
bool CKPathManager::ResolveFileName(XContainer::XString& u8_filename) {
2024-08-23 11:28:49 +08:00
std::filesystem::path filepath(u8_filename);
// if it is absolute path, return it directly
if (filepath.is_absolute()) {
return true;
}
// test in temp folder
auto tempfile = m_TempFolder / filepath;
if (std::filesystem::is_regular_file(tempfile)) {
2024-08-23 11:28:49 +08:00
u8_filename = tempfile.u8string();
return true;
}
// otherwise check it in extra path
for (const auto& extrapath : m_ExtraPathes) {
auto combinedpath = extrapath / filepath;
if (std::filesystem::is_regular_file(combinedpath)) {
// this is correct
2024-08-23 11:28:49 +08:00
u8_filename = combinedpath.u8string();
return true;
}
}
// failed
2023-09-06 10:42:23 +08:00
return false;
}
void CKPathManager::GetFileName(XContainer::XString& u8path) {
2024-08-23 11:28:49 +08:00
std::filesystem::path filepath(u8path);
auto result = filepath.filename();
2024-08-23 11:28:49 +08:00
u8path = result.u8string();
}
2023-09-16 18:31:25 +08:00
void CKPathManager::GetExtension(XContainer::XString& u8path) {
2024-08-23 11:28:49 +08:00
std::filesystem::path filepath(u8path);
2023-09-10 21:33:43 +08:00
auto result = filepath.extension();
2024-08-23 11:28:49 +08:00
u8path = result.u8string();
2023-09-10 21:33:43 +08:00
}
2023-09-04 22:58:53 +08:00
}