old-nall/path.hpp

145 lines
3.9 KiB
C++
Raw Permalink Normal View History

2016-05-15 10:42:10 +00:00
#pragma once
2016-07-03 11:35:22 +00:00
#include <nall/string.hpp>
2016-05-15 10:42:10 +00:00
2016-07-03 11:35:22 +00:00
namespace nall { namespace Path {
inline auto active() -> string {
2016-05-15 10:42:10 +00:00
char path[PATH_MAX] = "";
auto unused = getcwd(path, PATH_MAX);
string result = path;
if(!result) result = ".";
result.transform("\\", "/");
2016-07-03 11:35:22 +00:00
if(!result.endsWith("/")) result.append("/");
2016-05-15 10:42:10 +00:00
return result;
}
2016-07-03 11:35:22 +00:00
inline auto real(string_view name) -> string {
2016-05-15 10:42:10 +00:00
string result;
char path[PATH_MAX] = "";
2016-07-09 07:51:02 +00:00
if(::realpath(name, path)) result = Location::path(string{path}.transform("\\", "/"));
2016-07-03 11:35:22 +00:00
if(!result) return active();
2016-05-15 10:42:10 +00:00
result.transform("\\", "/");
2016-07-03 11:35:22 +00:00
if(!result.endsWith("/")) result.append("/");
2016-05-15 10:42:10 +00:00
return result;
}
2016-07-03 11:35:22 +00:00
inline auto program() -> string {
2016-05-15 10:42:10 +00:00
#if defined(PLATFORM_WINDOWS)
wchar_t path[PATH_MAX] = L"";
GetModuleFileName(nullptr, path, PATH_MAX);
string result = (const char*)utf8_t(path);
result.transform("\\", "/");
2016-07-03 11:35:22 +00:00
return Path::real(result);
2016-05-15 10:42:10 +00:00
#else
Dl_info info;
2016-07-03 11:35:22 +00:00
dladdr((void*)&program, &info);
return Path::real(info.dli_fname);
2016-05-15 10:42:10 +00:00
#endif
}
// /
// c:/
2016-07-03 11:35:22 +00:00
inline auto root() -> string {
2016-05-15 10:42:10 +00:00
#if defined(PLATFORM_WINDOWS)
wchar_t path[PATH_MAX] = L"";
SHGetFolderPathW(nullptr, CSIDL_WINDOWS | CSIDL_FLAG_CREATE, nullptr, 0, path);
string result = (const char*)utf8_t(path);
result.transform("\\", "/");
return slice(result, 0, 3);
#else
return "/";
#endif
}
// /home/username/
// c:/users/username/
2016-07-03 11:35:22 +00:00
inline auto user() -> string {
2016-05-15 10:42:10 +00:00
#if defined(PLATFORM_WINDOWS)
wchar_t path[PATH_MAX] = L"";
SHGetFolderPathW(nullptr, CSIDL_PROFILE | CSIDL_FLAG_CREATE, nullptr, 0, path);
string result = (const char*)utf8_t(path);
result.transform("\\", "/");
#else
struct passwd* userinfo = getpwuid(getuid());
string result = userinfo->pw_dir;
#endif
if(!result) result = ".";
2016-07-03 11:35:22 +00:00
if(!result.endsWith("/")) result.append("/");
2016-05-15 10:42:10 +00:00
return result;
}
// /home/username/.config/
// c:/users/username/appdata/roaming/
2016-07-03 11:35:22 +00:00
inline auto config() -> string {
2016-05-15 10:42:10 +00:00
#if defined(PLATFORM_WINDOWS)
wchar_t path[PATH_MAX] = L"";
SHGetFolderPathW(nullptr, CSIDL_APPDATA | CSIDL_FLAG_CREATE, nullptr, 0, path);
string result = (const char*)utf8_t(path);
result.transform("\\", "/");
#elif defined(PLATFORM_MACOSX)
2016-07-03 11:35:22 +00:00
string result = {Path::user(), "Library/Application Support/"};
2016-05-15 10:42:10 +00:00
#else
2016-07-03 11:35:22 +00:00
string result = {Path::user(), ".config/"};
2016-05-15 10:42:10 +00:00
#endif
if(!result) result = ".";
2016-07-03 11:35:22 +00:00
if(!result.endsWith("/")) result.append("/");
2016-05-15 10:42:10 +00:00
return result;
}
// /home/username/.local/share/
// c:/users/username/appdata/local/
2016-07-03 11:35:22 +00:00
inline auto local() -> string {
2016-05-15 10:42:10 +00:00
#if defined(PLATFORM_WINDOWS)
wchar_t path[PATH_MAX] = L"";
SHGetFolderPathW(nullptr, CSIDL_LOCAL_APPDATA | CSIDL_FLAG_CREATE, nullptr, 0, path);
string result = (const char*)utf8_t(path);
result.transform("\\", "/");
#elif defined(PLATFORM_MACOSX)
2016-07-03 11:35:22 +00:00
string result = {Path::user(), "Library/Application Support/"};
2016-05-15 10:42:10 +00:00
#else
2016-07-03 11:35:22 +00:00
string result = {Path::user(), ".local/share/"};
2016-05-15 10:42:10 +00:00
#endif
if(!result) result = ".";
2016-07-03 11:35:22 +00:00
if(!result.endsWith("/")) result.append("/");
2016-05-15 10:42:10 +00:00
return result;
}
// /usr/share
// /Library/Application Support/
// c:/ProgramData/
2016-07-03 11:35:22 +00:00
inline auto shared() -> string {
2016-05-15 10:42:10 +00:00
#if defined(PLATFORM_WINDOWS)
wchar_t path[PATH_MAX] = L"";
SHGetFolderPathW(nullptr, CSIDL_COMMON_APPDATA | CSIDL_FLAG_CREATE, nullptr, 0, path);
string result = (const char*)utf8_t(path);
result.transform("\\", "/");
#elif defined(PLATFORM_MACOSX)
string result = "/Library/Application Support/";
#else
string result = "/usr/share/";
#endif
if(!result) result = ".";
2016-07-03 11:35:22 +00:00
if(!result.endsWith("/")) result.append("/");
2016-05-15 10:42:10 +00:00
return result;
}
// /tmp
// c:/users/username/AppData/Local/Temp/
2016-07-03 11:35:22 +00:00
inline auto temp() -> string {
2016-05-15 10:42:10 +00:00
#if defined(PLATFORM_WINDOWS)
wchar_t path[PATH_MAX] = L"";
GetTempPathW(PATH_MAX, path);
string result = (const char*)utf8_t(path);
result.transform("\\", "/");
#elif defined(P_tmpdir)
string result = P_tmpdir;
#else
string result = "/tmp/";
#endif
2016-07-03 11:35:22 +00:00
if(!result.endsWith("/")) result.append("/");
2016-05-15 10:42:10 +00:00
return result;
}
2016-07-03 11:35:22 +00:00
}}