diff options
Diffstat (limited to 'src/support')
-rw-r--r-- | src/support/path.cpp | 31 | ||||
-rw-r--r-- | src/support/path.h | 8 |
2 files changed, 31 insertions, 8 deletions
diff --git a/src/support/path.cpp b/src/support/path.cpp index e9817aca9..2501c636c 100644 --- a/src/support/path.cpp +++ b/src/support/path.cpp @@ -24,16 +24,32 @@ namespace wasm { namespace Path { -std::string getPathSeparator() { +char getPathSeparator() { // TODO: use c++17's path separator // http://en.cppreference.com/w/cpp/experimental/fs/path #if defined(WIN32) || defined(_WIN32) - return "\\"; + return '\\'; #else - return "/"; + return '/'; #endif } +std::string getDirName(const std::string& path) { + auto sep = path.rfind(getPathSeparator()); + if (sep == std::string::npos) { + return ""; + } + return path.substr(0, sep); +} + +std::string getBaseName(const std::string& path) { + auto sep = path.rfind(getPathSeparator()); + if (sep == std::string::npos) { + return path; + } + return path.substr(sep + 1); +} + std::string getBinaryenRoot() { auto* envVar = getenv("BINARYEN_ROOT"); if (envVar) { @@ -52,10 +68,15 @@ std::string getBinaryenBinDir() { } } -void setBinaryenBinDir(std::string dir) { binDir = dir; } +void setBinaryenBinDir(const std::string& dir) { + binDir = dir; + if (binDir.back() != getPathSeparator()) { + binDir += getPathSeparator(); + } +} // Gets the path to a binaryen binary tool, like wasm-opt -std::string getBinaryenBinaryTool(std::string name) { +std::string getBinaryenBinaryTool(const std::string& name) { return getBinaryenBinDir() + name; } diff --git a/src/support/path.h b/src/support/path.h index 505ba0d88..e9f675142 100644 --- a/src/support/path.h +++ b/src/support/path.h @@ -28,7 +28,9 @@ namespace wasm { namespace Path { -std::string getPathSeparator(); +char getPathSeparator(); +std::string getDirName(const std::string& path); +std::string getBaseName(const std::string& path); // Get the binaryen root dor. std::string getBinaryenRoot(); @@ -37,10 +39,10 @@ std::string getBinaryenRoot(); std::string getBinaryenBinDir(); // Set the binaryen bin dir (allows tools to change it based on user input). -void setBinaryenBinDir(std::string dir); +void setBinaryenBinDir(const std::string& dir); // Gets the path to a binaryen binary tool, like wasm-opt. -std::string getBinaryenBinaryTool(std::string name); +std::string getBinaryenBinaryTool(const std::string& name); } // namespace Path |