String AbstractFileManager::makeRelative(const String& absolutePath, const String& referencePath) { if (!::wxIsAbsolutePath(absolutePath)) return absolutePath; if (!::wxIsAbsolutePath(referencePath)) return ""; StringList absolutePathComponents = resolvePath(pathComponents(absolutePath)); StringList referencePathComponents = resolvePath(pathComponents(referencePath)); StringList relativePathComponents; if (!isDirectory(referencePath)) referencePathComponents.pop_back(); unsigned int i = 0; for (; i < (std::min)(absolutePathComponents.size(), referencePathComponents.size()); i++) { const String& absolutePathComponent = absolutePathComponents[i]; const String& referencePathComponent = referencePathComponents[i]; if (absolutePathComponent != referencePathComponent) break; } for (unsigned int j = i; j < referencePathComponents.size(); j++) relativePathComponents.push_back(".."); for (unsigned int j = i; j < absolutePathComponents.size(); j++) relativePathComponents.push_back(absolutePathComponents[j]); return joinComponents(relativePathComponents); }
StringList AbstractFileManager::directoryContents(const String& path, String extension, bool directories, bool files) { StringList result; if (!isDirectory(path) || !wxSetWorkingDirectory(path)) return result; if (!directories && !files) return result; int flags; if (directories && files) flags = 0; else if (directories) flags = wxDIR; else flags = wxFILE; String lowerExtension = Utility::toLower(extension); wxString filename = wxFindFirstFile("*", flags); while (!filename.empty()) { String stdFilename = filename.ToStdString(); bool matches = extension.empty() || Utility::toLower(pathExtension(stdFilename)) == lowerExtension; if (matches) result.push_back(pathComponents(stdFilename).back()); filename = wxFindNextFile(); } return result; }
String AbstractFileManager::resolvePath(const String& path) { StringList components = resolvePath(pathComponents(path)); String cleanPath = joinComponents(components); if (path[0] == '/') cleanPath = "/" + cleanPath; return cleanPath; }
string filenameFromPathname(const string &path) { string strippedPath(trimRight(trimLeft(path, "\t "), "\t ")); // special-case the root if (strippedPath == "/") { return strippedPath; } // directories are components too strippedPath = trimRight(strippedPath, "/"); vector<string> pathComponents(split(path, '/')); if (pathComponents.size() == 0) { THROW(ArgExc, "Expected at least one component of path '" << path << "', but saw none"); } return pathComponents[pathComponents.size() -1]; }