Files
cad-agent/src/cad_mcp_frame/mcp_utils.cpp
T
gjm cf87cfe93d refactor
修改了目录和部分源文件名称
2026-10-02 21:02:40 +08:00

50 lines
1.4 KiB
C++

#pragma once
#include "mcp_utils.h"
#include <Windows.h>
#include <tchar.h>
#include <ghc/filesystem.hpp>
namespace mcp_utils
{
std::wstring utf8ToWString(const std::string &utf8Str)
{
if (utf8Str.empty()) return std::wstring();
// 第一次调用获取需要的宽字符缓冲区大小
int sizeNeeded = ::MultiByteToWideChar(CP_UTF8, 0, &utf8Str[0], (int)utf8Str.size(), NULL, 0);
std::wstring wstrTo(sizeNeeded, 0);
// 第二次调用真正进行转换
::MultiByteToWideChar(CP_UTF8, 0, &utf8Str[0], (int)utf8Str.size(), &wstrTo[0], sizeNeeded);
return wstrTo;
}
bool mcp_utils::hasFileExist(const std::wstring &filePath) { return _taccess(filePath.c_str(), 0) == 0; }
bool mcp_utils::hasDirExist(const std::wstring &filePath) { return _taccess(filePath.c_str(), 0) == 0; }
std::wstring getModelDir()
{
TCHAR szPath[MAX_PATH];
::GetModuleFileName(_hdllInstance, szPath, MAX_PATH);
ghc::filesystem::path path(szPath);
return path.parent_path().wstring();
}
fs::path getModelDirPath()
{
return fs::path(getModelDir());
}
fs::path relative2absolute(const std::string &relativePath, const fs::path &rootPath)
{
return relative2absolute(fs::path(utf8ToWString(relativePath)), rootPath);
}
fs::path relative2absolute(const fs::path &relativePath, const fs::path &rootPath)
{
fs::path absolutePath = rootPath / relativePath;
return absolutePath.lexically_normal().wstring();
}
}