#include #if defined(WIN32) || defined(PLATFORM_X64) #include #endif #include bool doesPathExist(const std::string& sPath) { // Inspiration by FOX #if !defined(WIN32) && !defined(PLATFORM_X64) struct stat status; return !sPath.empty() && (::stat(sPath.c_str(), &status)==0); #else return !sPath.empty() && (GetFileAttributesA(sPath.c_str()) != 0xFFFFFFFF); #endif } bool doesFileExist(const std::string& sPath) { return doesPathExist(sPath) && isFile(sPath); } bool doesDirectoryExist(const std::string& sPath) { return doesPathExist(sPath) && isDirectory(sPath); } bool isFile(const std::string& sPath) { // Inspiration by FOX #if !defined(WIN32) && !defined(PLATFORM_X64) struct stat status; return !sPath.empty() && (::stat(sPath.c_str(), &status)==0) && S_ISREG(status.st_mode); #else DWORD atts; return !sPath.empty() && ((atts=GetFileAttributesA(sPath.c_str())) != 0xFFFFFFFF) && !(atts & FILE_ATTRIBUTE_DIRECTORY); #endif } bool isDirectory(const std::string& sPath) { #if !defined(WIN32) && !defined(PLATFORM_X64) struct stat status; return !sPath.empty() && (::stat(sPath.c_str(), &status)==0) && S_ISDIR(status.st_mode); #else DWORD atts; return !sPath.empty() && ((atts=GetFileAttributesA(sPath.c_str())) != 0xFFFFFFFF) && (atts & FILE_ATTRIBUTE_DIRECTORY); #endif } bool makeDirectory(const std::string& sPath) { #if !defined(WIN32) && !defined(PLATFORM_X64) // TODO: Mode? return mkdir(sPath.c_str(), 0)==0; #else return CreateDirectory(sPath.c_str(), NULL) != 0; #endif } std::string getFilenameSuffix(const std::string& sFilename) { int i = (int) sFilename.find_last_of('.'); return ((i == -1) || (i==((int) sFilename.length()-1)) ? "" : sFilename.substr(i+1, (int) sFilename.length()-i-1)); } std::string stripFilenameSuffix(const std::string& sFilename) { size_t i = sFilename.find_last_of('.'); return ((i == -1) || (i==(sFilename.length()-1)) ? "" : sFilename.substr(0, i)); } std::string getFilenameFromPath(const std::string& sPath) { size_t i = 0; if (sPath.find(PATH_SEPARATOR) != std::string::npos) { i = sPath.find_last_of(PATH_SEPARATOR) + 1; return sPath.substr(i, (sPath.length()-1)); } else { return sPath; } } std::string getDirectoryFromPath(const std::string& sPath) { size_t i = sPath.find_last_of(PATH_SEPARATOR); return ((i == -1) || (i==(sPath.length()-1)) ? "" : sPath.substr(0, i)); } int64_t getFileSize(const std::string& sFilename) { if (sFilename.empty()) return -1; #ifdef LINUX // Linux struct stat64 statinfo; if (stat64(sFilename.c_str(), &statinfo) != 0) return -1; return (int64_t) statinfo.st_size; #endif #ifdef WIN32 // Windows struct _stat64 statinfo; if (_stat64(sFilename.c_str(), &statinfo) != 0) return -1; return (int64_t) statinfo.st_size; #endif // LINUX oder WIN32 nicht definiert return -1; } std::string combinePath(std::string p1, std::string p2, std::string p3, std::string p4) { std::string s = p1; if (!p2.empty()) { if (!s.empty()) s += PATH_SEPARATOR; s += p2; } if (!p3.empty()) { if (!s.empty()) s += PATH_SEPARATOR; s += p3; } if (!p4.empty()) { if (!s.empty()) s += PATH_SEPARATOR; s += p4; } return correctPath(s); } std::string correctPath(const std::string& sPath) { std::string s(sPath); if (PATH_SEPARATOR == '/') { for (std::string::size_type i=0; i& vsFilenames) { vsFilenames.clear(); std::string sCorectedPath = correctPath(sPath); if (!isDirectory(sCorectedPath)) return false; std::string sFilename; // Inspiration by FOX #if !defined(WIN32) && !defined(PLATFORM_X64) // Linux implementation #else // Windows implementation WIN32_FIND_DATA oData; HANDLE h; std::string sPathWildcard = sCorectedPath + std::string("\\*"); h = FindFirstFile(sPathWildcard.c_str(), &oData); if (h == INVALID_HANDLE_VALUE) return false; do { sFilename = oData.cFileName; if (!(oData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) vsFilenames.push_back(sFilename); } while (FindNextFile(h, &oData) != 0); DWORD dwError = GetLastError(); FindClose(h); #endif return true; }