const bool OTPaths::PathExists(const OTString & strPath) { if (!strPath.Exists()) { OTLog::sError("%s: Null: %s passed in!\n", __FUNCTION__, "strPath" ); OT_ASSERT(false); } // remove trailing backslash for stat std::string l_strPath(strPath.Get()); l_strPath = (OTString::replace_chars(l_strPath,"\\",'/')); // all \ to / //std::string l_strPath_stat = l_strPath; std::string l_strPath_stat(""); // remove last / if it exists (for l_strPath_stat) if ('/' == *l_strPath.rbegin()) l_strPath_stat = l_strPath.substr(0, l_strPath.size()-1); else l_strPath_stat = l_strPath; struct stat st; memset(&st, 0, sizeof(st)); if (0 == stat(l_strPath_stat.c_str(), &st)) // good we have at-least on a node { if ('/' != *l_strPath.rbegin()) { long temp_l=0; return FileExists(strPath,temp_l); } else { return FolderExists(strPath); } } return false; }
const bool OTPaths::FileExists(const OTString & strFilePath, long & nFileLength) { if (!strFilePath.Exists()) { OTLog::sError("%s: Null: %s passed in!\n", __FUNCTION__, "strFilePath" ); OT_ASSERT(false); } // remove trailing backslash for stat std::string l_strPath(strFilePath.Get()); l_strPath = (OTString::replace_chars(l_strPath,"\\",'/')); // all \ to / //std::string l_strPath_stat = l_strPath; std::string l_strPath_stat(""); // remove last / if it exists (for l_strPath_stat) if ('/' == *l_strPath.rbegin()) l_strPath_stat = l_strPath.substr(0, l_strPath.size()-1); else l_strPath_stat = l_strPath; if ('/' != *l_strPath.rbegin()) { int status=0; #ifdef _WIN32 struct _stat st_buf; memset(&st_buf, 0, sizeof(st_buf)); char filename[4086]; // not sure about this buffer, // on windows paths cannot be longer than 4086, // so it should be fine... needs more research. strcpy_s(filename,l_strPath_stat.c_str()); status = _stat(filename, &st_buf ); #else struct stat st_buf; memset(&st_buf, 0, sizeof(st_buf)); status = stat (l_strPath.c_str(), &st_buf); #endif // check for file if (S_ISREG(st_buf.st_mode)) { // good we have a file. size_t lFileLength = st_buf.st_size; nFileLength = static_cast<long>(lFileLength); return true; } } return false; }
// static bool OTPaths::FixPath(const String& strPath, String& out_strFixedPath, const bool& bIsFolder) { if (!strPath.Exists()) { otErr << __FUNCTION__ << ": Null: " << "strPath" << " passed in!\n"; OT_FAIL; } std::string l_strPath(strPath.Get()); // first change all back-slashes to forward slashes: std::string l_strPath_noBackslash( String::replace_chars(l_strPath, "\\", '/')); // now we make sure we have the correct trailing "/". if ('/' == *l_strPath_noBackslash.rbegin()) { if (bIsFolder) { out_strFixedPath.Set(l_strPath_noBackslash.c_str()); return true; } else { out_strFixedPath.Set( l_strPath_noBackslash.substr(0, l_strPath_noBackslash.size() - 1).c_str()); return true; } } else { if (bIsFolder) { l_strPath_noBackslash += "/"; out_strFixedPath.Set(l_strPath_noBackslash.c_str()); return true; } else { out_strFixedPath.Set(l_strPath_noBackslash.c_str()); return true; } } }
const bool OTPaths::FixPath(const OTString & strPath, OTString & out_strFixedPath, const bool & bIsFolder) { if (!strPath.Exists()) { OTLog::sError("%s: Null: %s passed in!\n", __FUNCTION__, "strPath" ); OT_ASSERT(false); } std::string l_strPath(strPath.Get()); // first change all back-slashes to forward slashes: std::string l_strPath_noBackslash(OTString::replace_chars(l_strPath,"\\",'/')); // now we make sure we have the correct trailing "/". if ('/' == *l_strPath_noBackslash.rbegin()) { if (bIsFolder) { out_strFixedPath.Set(l_strPath_noBackslash.c_str()); return true; } else { out_strFixedPath.Set(l_strPath_noBackslash.substr(0, l_strPath_noBackslash.size()-1).c_str()); return true; } } else { if (bIsFolder) { l_strPath_noBackslash += "/"; out_strFixedPath.Set(l_strPath_noBackslash.c_str()); return true; } else { out_strFixedPath.Set(l_strPath_noBackslash.c_str()); return true; } } }
// this function dosn't change the "strRelativePath" so. It will only fix the strBasePath. const bool OTPaths::RelativeToCanonical(OTString & out_strCanonicalPath, const OTString & strBasePath, const OTString & strRelativePath) { if (!strBasePath.Exists()) { OTLog::sError("%s: Null: %s passed in!\n", __FUNCTION__, "strBasePath" ); OT_ASSERT(false); } if (!strRelativePath.Exists()) { OTLog::sError("%s: Null: %s passed in!\n", __FUNCTION__, "strRelativePath" ); OT_ASSERT(false); } OTString l_strBasePath_fix(""); if(!FixPath(strBasePath,l_strBasePath_fix,true)) return false; if(strRelativePath.Compare(".")) { out_strCanonicalPath = strBasePath; return true; } // if ".", return base path. std::string l_strBasePath(l_strBasePath_fix.Get()), l_strRelativePath(strRelativePath.Get()); l_strBasePath.append(l_strRelativePath); OTString l_strPath(l_strBasePath), l_strCanonicalPath(""); if(!ToReal(l_strPath,l_strCanonicalPath)) return false; out_strCanonicalPath = l_strCanonicalPath; return true; }
// static bool OTPaths::FileExists(const String& strFilePath, int64_t& nFileLength) { if (!strFilePath.Exists()) { otErr << __FUNCTION__ << ": Null: " << "strFilePath" << " passed in!\n"; OT_FAIL; } // remove trailing backslash for stat std::string l_strPath(strFilePath.Get()); l_strPath = (String::replace_chars(l_strPath, "\\", '/')); // all \ to / if ('/' != *l_strPath.rbegin()) { #ifdef _WIN32 std::string l_strPath_stat = l_strPath; struct _stat st_buf; memset(&st_buf, 0, sizeof(st_buf)); char filename[4086]; // not sure about this buffer, // on windows paths cannot be longer than 4086, // so it should be fine... needs more research. strcpy_s(filename, l_strPath_stat.c_str()); _stat(filename, &st_buf); #else struct stat st_buf; memset(&st_buf, 0, sizeof(st_buf)); stat(l_strPath.c_str(), &st_buf); #endif // check for file if (S_ISREG(st_buf.st_mode)) { // good we have a file. size_t lFileLength = st_buf.st_size; nFileLength = static_cast<int64_t>(lFileLength); return true; } } return false; }
// static bool OTPaths::PathExists(const String& strPath) { if (!strPath.Exists()) { otErr << __FUNCTION__ << ": Null: " << "strPath" << " passed in!\n"; OT_FAIL; } // remove trailing backslash for stat std::string l_strPath(strPath.Get()); l_strPath = (String::replace_chars(l_strPath, "\\", '/')); // all \ to / // std::string l_strPath_stat = l_strPath; std::string l_strPath_stat(""); // remove last / if it exists (for l_strPath_stat) if ('/' == *l_strPath.rbegin()) l_strPath_stat = l_strPath.substr(0, l_strPath.size() - 1); else l_strPath_stat = l_strPath; struct stat st; memset(&st, 0, sizeof(st)); if (0 == stat(l_strPath_stat.c_str(), &st)) // good we have at-least on a node { if ('/' != *l_strPath.rbegin()) { int64_t temp_l = 0; return FileExists(strPath, temp_l); } else { return FolderExists(strPath); } } return false; }
// this function dosn't change the "strRelativePath" so. It will only fix the // strBasePath. // static bool OTPaths::RelativeToCanonical(String& out_strCanonicalPath, const String& strBasePath, const String& strRelativePath) { if (!strBasePath.Exists()) { otErr << __FUNCTION__ << ": Null: " << "strBasePath" << " passed in!\n"; OT_FAIL; } if (!strRelativePath.Exists()) { otErr << __FUNCTION__ << ": Null: " << "strRelativePath" << " passed in!\n"; OT_FAIL; } String l_strBasePath_fix(""); if (!FixPath(strBasePath, l_strBasePath_fix, true)) return false; if (strRelativePath.Compare(".")) { out_strCanonicalPath = strBasePath; return true; } // if ".", return base path. std::string l_strBasePath(l_strBasePath_fix.Get()), l_strRelativePath(strRelativePath.Get()); l_strBasePath.append(l_strRelativePath); String l_strPath(l_strBasePath), l_strCanonicalPath(""); if (!ToReal(l_strPath, l_strCanonicalPath)) return false; out_strCanonicalPath = l_strCanonicalPath; return true; }
// static bool OTPaths::FolderExists(const String& strFolderPath) { if (!strFolderPath.Exists()) { otErr << __FUNCTION__ << ": Null: " << "strFolderPath" << " passed in!\n"; OT_FAIL; } // remove trailing backslash for stat std::string l_strPath(strFolderPath.Get()); l_strPath = (String::replace_chars(l_strPath, "\\", '/')); // all \ to / if ('/' == *l_strPath.rbegin()) { #ifdef _WIN32 std::string l_strPath_stat = l_strPath.substr(0, l_strPath.size() - 1); struct _stat st_buf; memset(&st_buf, 0, sizeof(st_buf)); char filename[4086] = ""; // not sure about this buffer, // on windows paths cannot be longer than 4086, // so it should be fine... needs more research. strcpy_s(filename, l_strPath_stat.c_str()); _stat(filename, &st_buf); #else struct stat st_buf; memset(&st_buf, 0, sizeof(st_buf)); stat(l_strPath.c_str(), &st_buf); #endif if (S_ISDIR(st_buf.st_mode)) { // good we have a directory. return true; } } return false; }