bool createDirectories(const cv::String& path_) { cv::String path = path_; for (;;) { char last_char = path.empty() ? 0 : path[path.length() - 1]; if (isPathSeparator(last_char)) { path = path.substr(0, path.length() - 1); continue; } break; } if (path.empty() || path == "./" || path == ".\\" || path == ".") return true; if (isDirectory(path)) return true; size_t pos = path.rfind('/'); if (pos == cv::String::npos) pos = path.rfind('\\'); if (pos != cv::String::npos) { cv::String parent_directory = path.substr(0, pos); if (!parent_directory.empty()) { if (!createDirectories(parent_directory)) return false; } } return createDirectory(path); }
bool Filename::isRelativePath() const { if (this->empty()) return true; UTF8String::const_iterator it = this->begin(); if (isPathSeparator(*it)) return false; // absolute path else if (*it=='.') return true; // relative path *it++; if (it==this->end()) return true; // relative path. It's a single letter filename (e.g: "f") else if (*it==':') return false; // absolute path. It starts with a drive letter (e.g: "C:\xxx") else return true; // relative path }
static bool extractComponentName(const char **componentName, int *length, const char *filename) // Return 'true' if the specified 'filename' corresponds to a valid // filename for a BDE component, and 'false' otherwise. If 'filename' // corresponds to a valid component name, 'componentName' will be set to // point to the start of that component name within 'filename', and // 'length' will store the length of that component name. Note that this // sub-string will *not* be null-terminated. { // A component filename is a filename ending in one of the following set of // file extensions: '.h', '.cpp', '.t.cpp'. // The component name is the component filename minus the extension and any // leading pathname. // The basic algorithm searches for the end of the 'filename' and then // iterates backwards. First we verify that the filename has a valid // extension for a component: '.cpp', '.t.cpp', or '.h'. Then we keep // searching backwards for the first path separator, which will depend on // the filesystem the compiler is running on. The string between the last // path separator (if any) and the period starting the file extension mark // out the component name to be returned. if (!componentName || !length || !filename) { printf("passed at least one null pointer\n"); return false; } const char *end = filename; for (; *end; ++end) { // Find the end of the string. } if (3 > (end - filename)) { printf("filename is too short\n"); return false; } --end; if ('h' == *end) { if ('.' != *--end) { printf("filename is not a header\n"); return false; } } else if ('p' == *end) { if (4 > (end - filename)) { printf("filename is not long enough for a .cpp\n"); return false; } if ('p' != *--end) { printf("filename is not a .cpp(1)\n"); return false; } if ('c' != *--end) { printf("filename is not a .cpp(2)\n"); return false; } if ('.' != *--end) { printf("filename is not a .cpp(3)\n"); return false; } if (2 < (end - filename)) { const char *cursor = end; if ('t' == *--cursor) { if ('.' == *--cursor) { end = cursor; } } } } else { printf("filename is not recognized\n"); return false; } const char *cursor = end; while (cursor != filename) { --cursor; if (isPathSeparator(*cursor)) { ++cursor; break; } } *componentName = cursor; *length = end - cursor; return true; }
cv::String getCacheDirectory(const char* sub_directory_name, const char* configuration_name) { String cache_path; if (configuration_name) { cache_path = utils::getConfigurationParameterString(configuration_name, ""); } if (cache_path.empty()) { cv::String default_cache_path; #ifdef _WIN32 char tmp_path_buf[MAX_PATH+1] = {0}; DWORD res = GetTempPath(MAX_PATH, tmp_path_buf); if (res > 0 && res <= MAX_PATH) { default_cache_path = tmp_path_buf; } #elif defined __ANDROID__ // no defaults #elif defined __APPLE__ const char* tmpdir_env = getenv("TMPDIR"); if (tmpdir_env && utils::fs::isDirectory(tmpdir_env)) { default_cache_path = tmpdir_env; } else { default_cache_path = "/tmp/"; CV_LOG_WARNING(NULL, "Using world accessible cache directory. This may be not secure: " << default_cache_path); } #elif defined __linux__ // https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html if (default_cache_path.empty()) { const char* xdg_cache_env = getenv("XDG_CACHE_HOME"); if (xdg_cache_env && xdg_cache_env[0] && utils::fs::isDirectory(xdg_cache_env)) { default_cache_path = xdg_cache_env; } } if (default_cache_path.empty()) { const char* home_env = getenv("HOME"); if (home_env && home_env[0] && utils::fs::isDirectory(home_env)) { cv::String home_path = home_env; cv::String home_cache_path = home_path + "/.cache/"; if (utils::fs::isDirectory(home_cache_path)) { default_cache_path = home_cache_path; } } } if (default_cache_path.empty()) { const char* temp_path = "/var/tmp/"; if (utils::fs::isDirectory(temp_path)) { default_cache_path = temp_path; CV_LOG_WARNING(NULL, "Using world accessible cache directory. This may be not secure: " << default_cache_path); } } if (default_cache_path.empty()) { default_cache_path = "/tmp/"; CV_LOG_WARNING(NULL, "Using world accessible cache directory. This may be not secure: " << default_cache_path); } #else // no defaults #endif CV_LOG_VERBOSE(NULL, 0, "default_cache_path = " << default_cache_path); if (!default_cache_path.empty()) { if (utils::fs::isDirectory(default_cache_path)) { default_cache_path += "/opencv/" CV_VERSION "/"; if (sub_directory_name && sub_directory_name[0] != '\0') default_cache_path += cv::String(sub_directory_name) + "/"; if (!utils::fs::createDirectories(default_cache_path)) { CV_LOG_DEBUG(NULL, "Can't create OpenCV cache sub-directory: " << default_cache_path); } else { cache_path = default_cache_path; } } else { CV_LOG_INFO(NULL, "Can't find default cache directory (does it exist?): " << default_cache_path); } } else { CV_LOG_DEBUG(NULL, "OpenCV has no support to discover default cache directory on the current platform"); } } else { if (cache_path == "disabled") return cache_path; if (!isDirectory(cache_path)) { CV_LOG_WARNING(NULL, "Specified non-existed directory, creating OpenCV sub-directory for caching purposes: " << cache_path); if (!createDirectories(cache_path)) { CV_LOG_ERROR(NULL, "Can't create OpenCV cache sub-directory: " << cache_path); cache_path.clear(); } } } CV_Assert(cache_path.empty() || utils::fs::isDirectory(cache_path)); if (!cache_path.empty()) { if (!isPathSeparator(cache_path[cache_path.size() - 1])) { cache_path += '/'; } } return cache_path; }
cv::String getCacheDirectory(const char* sub_directory_name, const char* configuration_name) { String cache_path; if (configuration_name) { cache_path = utils::getConfigurationParameterString(configuration_name, ""); } if (cache_path.empty()) { cv::String default_cache_path; #ifdef _WIN32 char tmp_path_buf[MAX_PATH+1] = {0}; DWORD res = GetTempPath(MAX_PATH, tmp_path_buf); if (res > 0 && res <= MAX_PATH) { default_cache_path = tmp_path_buf; } #elif defined __ANDROID__ // no defaults #elif defined __APPLE__ const char* tmpdir_env = getenv("TMPDIR"); if (tmpdir_env && utils::fs::isDirectory(tmpdir_env)) { default_cache_path = tmpdir_env; } else { default_cache_path = "/tmp/"; CV_LOG_WARNING(NULL, "Using world accessible cache directory. This may be not secure: " << default_cache_path); } #elif defined __linux__ || defined __HAIKU__ || defined __FreeBSD__ // https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html if (default_cache_path.empty()) { const char* xdg_cache_env = getenv("XDG_CACHE_HOME"); if (xdg_cache_env && xdg_cache_env[0] && utils::fs::isDirectory(xdg_cache_env)) { default_cache_path = xdg_cache_env; } } if (default_cache_path.empty()) { const char* home_env = getenv("HOME"); if (home_env && home_env[0] && utils::fs::isDirectory(home_env)) { cv::String home_path = home_env; cv::String home_cache_path = utils::fs::join(home_path, ".cache/"); if (utils::fs::isDirectory(home_cache_path)) { default_cache_path = home_cache_path; } } } if (default_cache_path.empty()) { const char* temp_path = "/var/tmp/"; if (utils::fs::isDirectory(temp_path)) { default_cache_path = temp_path; CV_LOG_WARNING(NULL, "Using world accessible cache directory. This may be not secure: " << default_cache_path); } } if (default_cache_path.empty()) { default_cache_path = "/tmp/"; CV_LOG_WARNING(NULL, "Using world accessible cache directory. This may be not secure: " << default_cache_path); } #else // no defaults #endif CV_LOG_VERBOSE(NULL, 0, "default_cache_path = " << default_cache_path); if (!default_cache_path.empty()) { if (utils::fs::isDirectory(default_cache_path)) { cv::String default_cache_path_base = utils::fs::join(default_cache_path, "opencv"); default_cache_path = utils::fs::join(default_cache_path_base, "4.0" CV_VERSION_STATUS); if (utils::getConfigurationParameterBool("OPENCV_CACHE_SHOW_CLEANUP_MESSAGE", true) && !utils::fs::isDirectory(default_cache_path)) { std::vector<cv::String> existedCacheDirs; try { utils::fs::glob_relative(default_cache_path_base, "*", existedCacheDirs, false, true); } catch (...) { // ignore } if (!existedCacheDirs.empty()) { CV_LOG_WARNING(NULL, "Creating new OpenCV cache directory: " << default_cache_path); CV_LOG_WARNING(NULL, "There are several neighbour directories, probably created by old OpenCV versions."); CV_LOG_WARNING(NULL, "Feel free to cleanup these unused directories:"); for (size_t i = 0; i < existedCacheDirs.size(); i++) { CV_LOG_WARNING(NULL, " - " << existedCacheDirs[i]); } CV_LOG_WARNING(NULL, "Note: This message is showed only once."); } } if (sub_directory_name && sub_directory_name[0] != '\0') default_cache_path = utils::fs::join(default_cache_path, cv::String(sub_directory_name) + native_separator); if (!utils::fs::createDirectories(default_cache_path)) { CV_LOG_DEBUG(NULL, "Can't create OpenCV cache sub-directory: " << default_cache_path); } else { cache_path = default_cache_path; } } else { CV_LOG_INFO(NULL, "Can't find default cache directory (does it exist?): " << default_cache_path); } } else { CV_LOG_DEBUG(NULL, "OpenCV has no support to discover default cache directory on the current platform"); } } else { if (cache_path == "disabled") return cache_path; if (!isDirectory(cache_path)) { CV_LOG_WARNING(NULL, "Specified non-existed directory, creating OpenCV sub-directory for caching purposes: " << cache_path); if (!createDirectories(cache_path)) { CV_LOG_ERROR(NULL, "Can't create OpenCV cache sub-directory: " << cache_path); cache_path.clear(); } } } CV_Assert(cache_path.empty() || utils::fs::isDirectory(cache_path)); if (!cache_path.empty()) { if (!isPathSeparator(cache_path[cache_path.size() - 1])) { cache_path += native_separator; } } return cache_path; }
void djvFileInfoUtil::split( const QString & in, QString & path, QString & base, QString & number, QString & extension) { //DJV_DEBUG("djvFileInfoUtil::split"); //DJV_DEBUG_PRINT("in = " << in); //DJV_DEBUG_PRINT("length = " << in.length()); path.resize(0); base.resize(0); number.resize(0); extension.resize(0); const int length = in.length(); if (! length) return; // Extension. int i = length - 1; int tmp = i; for (; in[i] != '.' && ! isPathSeparator(in[i]) && i > 0; --i) ; if (i > 0 && '.' == in[i] && ! isPathSeparator(in[i - 1])) { extension = in.mid(i, tmp - i + 1); --i; } else { i = length - 1; } //DJV_DEBUG_PRINT("extension = " << extension); // Number. if (i >= 0 && isSequenceValid(in[i])) { tmp = i; int separator = -1; QString word; for (; i > 0; --i) { if (! isSequenceValid(in[i - 1]) || seqSeparator(in[i - 1])) { if (separator != -1 && ! matchPadding(in.mid(i, separator - i), word)) { i = separator + 1; break; } else { word = in.mid( i, -1 == separator ? (tmp - i + 1) : (separator - i)); separator = i - 1; } } if (! (isSequenceValid(in[i - 1]) || seqSeparator(in[i - 1]))) break; } number = in.mid(i, tmp - i + 1); --i; } //DJV_DEBUG_PRINT("number = " << number); // Base. if (i >= 0 && ! isPathSeparator(in[i])) { tmp = i; for (; i > 0 && ! isPathSeparator(in[i - 1]); --i) ; base = in.mid(i, tmp - i + 1); --i; } //DJV_DEBUG_PRINT("base = " << base); // Path. if (i >= 0) { path = in.mid(0, i + 1); } //DJV_DEBUG_PRINT("path = " << path); }