Browse Source

Add system::getAbsoluteDirectory().

tags/v1.1.0
Andrew Belt 4 years ago
parent
commit
029434dcb7
3 changed files with 27 additions and 0 deletions
  1. +4
    -0
      include/system.hpp
  2. +1
    -0
      src/main.cpp
  3. +22
    -0
      src/system.cpp

+ 4
- 0
include/system.hpp View File

@@ -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. */


+ 1
- 0
src/main.cpp View File

@@ -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


+ 22
- 0
src/system.cpp View File

@@ -42,6 +42,7 @@ std::list<std::string> 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 + "\"";


Loading…
Cancel
Save