diff --git a/include/system.hpp b/include/system.hpp index 81df2fd0..830bc657 100644 --- a/include/system.hpp +++ b/include/system.hpp @@ -25,6 +25,10 @@ void copyFile(const std::string &srcPath, const std::string &destPath); The parent directory must exist. */ void createDirectory(const std::string &path); +/** Returns the canonicalized absolute path pointed to by `path`. +Returns "" if the symbol is not found. +*/ +std::string getAbsolutePath(const std::string &path); /** Returns the number of logical simultaneous multithreading (SMT) (e.g. Intel Hyperthreaded) threads on the CPU. */ int getLogicalCoreCount(); /** Sets a name of the current thread for debuggers and OS-specific process viewers. */ diff --git a/src/main.cpp b/src/main.cpp index 64e4e80a..a3b87759 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -95,6 +95,7 @@ int main(int argc, char *argv[]) { asset::init(); logger::init(); + DEBUG("abspath: %s", system::getAbsolutePath("今日は/a").c_str()); // We can now install a signal handler and log the output // Mac has its own decent crash handler diff --git a/src/system.cpp b/src/system.cpp index 016dd760..02eab1e0 100644 --- a/src/system.cpp +++ b/src/system.cpp @@ -42,6 +42,7 @@ std::list getEntries(const std::string &path) { return filenames; } + bool isFile(const std::string &path) { struct stat statbuf; if (stat(path.c_str(), &statbuf)) @@ -49,6 +50,7 @@ bool isFile(const std::string &path) { return S_ISREG(statbuf.st_mode); } + bool isDirectory(const std::string &path) { struct stat statbuf; if (stat(path.c_str(), &statbuf)) @@ -56,6 +58,7 @@ bool isDirectory(const std::string &path) { return S_ISDIR(statbuf.st_mode); } + void moveFile(const std::string &srcPath, const std::string &destPath) { std::remove(destPath.c_str()); // Whether this overwrites existing files is implementation-defined. @@ -64,6 +67,7 @@ void moveFile(const std::string &srcPath, const std::string &destPath) { std::rename(srcPath.c_str(), destPath.c_str()); } + void copyFile(const std::string &srcPath, const std::string &destPath) { // Open source FILE *source = fopen(srcPath.c_str(), "rb"); @@ -92,6 +96,7 @@ void copyFile(const std::string &srcPath, const std::string &destPath) { } } + void createDirectory(const std::string &path) { #if defined ARCH_WIN std::wstring pathW = string::toWstring(path); @@ -101,10 +106,23 @@ void createDirectory(const std::string &path) { #endif } + +std::string getAbsolutePath(const std::string &path) { +#if defined ARCH_LIN + char buf[PATH_MAX]; + char *pathC = realpath(path.c_str(), buf); + if (pathC) + return pathC; +#endif + return ""; +} + + int getLogicalCoreCount() { return std::thread::hardware_concurrency(); } + void setThreadName(const std::string &name) { #if defined ARCH_LIN pthread_setname_np(pthread_self(), name.c_str()); @@ -113,6 +131,7 @@ void setThreadName(const std::string &name) { #endif } + void setThreadRealTime(bool realTime) { #if defined ARCH_LIN int err; @@ -148,6 +167,7 @@ void setThreadRealTime(bool realTime) { #endif } + std::string getStackTrace() { int stackLen = 128; void *stack[stackLen]; @@ -180,6 +200,7 @@ std::string getStackTrace() { return s; } + void openBrowser(const std::string &url) { #if defined ARCH_LIN std::string command = "xdg-open \"" + url + "\""; @@ -195,6 +216,7 @@ void openBrowser(const std::string &url) { #endif } + void openFolder(const std::string &path) { #if defined ARCH_LIN std::string command = "xdg-open \"" + path + "\"";