From 8c0f9510e624efc609a4262302e02f49c7840f16 Mon Sep 17 00:00:00 2001 From: Dmytro Bogovych Date: Wed, 12 Sep 2018 20:19:56 +0300 Subject: [PATCH] - more changes to ease file handling --- src/engine/helper/HL_File.cpp | 38 +++++++++++++++++++++++++++++++++++ src/engine/helper/HL_File.h | 4 ++++ 2 files changed, 42 insertions(+) diff --git a/src/engine/helper/HL_File.cpp b/src/engine/helper/HL_File.cpp index 6cf2314b..f1147155 100644 --- a/src/engine/helper/HL_File.cpp +++ b/src/engine/helper/HL_File.cpp @@ -1,10 +1,18 @@ #include "HL_File.h" #include +#if defined(TARGET_LINUX) || defined(TARGET_OSX) +# include +#endif + bool FileHelper::exists(const std::string& s) { +#if defined(TARGET_WIN) std::ifstream ifs(s); return !ifs.bad(); +#else + return (access(s.c_str(), R_OK) != -1); +#endif } bool FileHelper::exists(const char* s) @@ -29,3 +37,33 @@ std::string FileHelper::gettempname() return buffer; } + +bool FileHelper::isAbsolute(const std::string& s) +{ + if (s.empty()) + return false; + + return s.front() == '/' || s.front() == '\\'; +} + +std::string FileHelper::addTrailingSlash(const std::string& s) +{ + if (s.empty()) + return "/"; + + if (s.back() == '/' || s.back() == '\\') + return s; + + return s + "/"; +} + +std::string FileHelper::mergePathes(const std::string& s1, const std::string& s2) +{ + std::string result(addTrailingSlash(s1)); + if (isAbsolute(s2)) + result += s2.substr(1, s2.size() - 1); + else + result += s2; + + return result; +} diff --git a/src/engine/helper/HL_File.h b/src/engine/helper/HL_File.h index e70eaa3f..857073b5 100644 --- a/src/engine/helper/HL_File.h +++ b/src/engine/helper/HL_File.h @@ -13,6 +13,10 @@ public: static void remove(const char* s); static std::string gettempname(); + static bool isAbsolute(const std::string& s); + + static std::string addTrailingSlash(const std::string& s); + static std::string mergePathes(const std::string& s1, const std::string& s2); }; #endif