- more changes to ease file handling

This commit is contained in:
Dmytro Bogovych 2018-09-12 20:19:56 +03:00
parent 3164a7236d
commit 8c0f9510e6
2 changed files with 42 additions and 0 deletions

View File

@ -1,10 +1,18 @@
#include "HL_File.h"
#include <fstream>
#if defined(TARGET_LINUX) || defined(TARGET_OSX)
# include <unistd.h>
#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;
}

View File

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