Add abspath to fileop

This commit is contained in:
2025-10-15 23:11:12 +08:00
parent e2d528150b
commit 852fe6eade
2 changed files with 41 additions and 0 deletions

View File

@@ -24,6 +24,7 @@
#include "time_util.h"
#include <regex>
#include <list>
#include <malloc.h>
#ifdef _WIN32
#if HAVE__ACCESS_S
@@ -114,6 +115,16 @@ bool listdir_internal(wchar_t* fn, std::list<std::string>& list, bool ignore_hid
return true;
}
std::string realpath_internal(wchar_t* fn) {
DWORD len = GetFullPathNameW(fn, 0, nullptr, nullptr);
wchar_t* buf = (wchar_t*)malloc(sizeof(wchar_t) * len);
GetFullPathNameW(fn, len, buf, nullptr);
std::string re;
wchar_util::wstr_to_str(re, buf, CP_UTF8);
free(buf);
return re;
}
template <typename T, typename ... Args>
T fileop_internal(const char* fname, UINT codePage, T(*callback)(wchar_t* fn, Args... args), T failed, Args... args) {
int wlen;
@@ -708,3 +719,27 @@ std::string fileop::extname(std::string path) {
}
return path.substr(loc + 1, path.length() - loc - 1);
}
std::string fileop::abspath(std::string path) {
#if _WIN32
UINT cp[] = { CP_UTF8, CP_OEMCP, CP_ACP };
int i;
std::string re;
for (i = 0; i < 3; i++) {
re = fileop_internal<std::string>(path.c_str(), cp[i], &realpath_internal, "");
if (!re.empty()) return re;
}
DWORD len = GetFullPathNameA(path.c_str(), 0, nullptr, nullptr);
char* buf = (char*)malloc(sizeof(char) * len);
GetFullPathNameA(path.c_str(), len, buf, nullptr);
auto re2 = std::string(buf);
free(buf);
return re2;
#else
char* rp = realpath(path.c_str(), nullptr);
if (!rp) return path;
std::string re(rp);
free(rp);
return re;
#endif
}

View File

@@ -205,5 +205,11 @@ namespace fileop {
* @return File extension name. If no extension, will return empty string.
*/
std::string extname(std::string path);
/**
* @brief Convert a relative path to an absolute path
* @param path Path
* @return Absolute path
*/
std::string abspath(std::string path);
}
#endif