// Expand directory path - these cases exist: // // 1. Empty string or nil means return current directory // 2. ~ with non-existent HOME directory throws exception // 3. ~, ~/ or ~\ which expands to HOME // 4. ~foo is left unexpanded // 5. Expand to full path if path is a relative path // // No attempt is made to determine whether the path is valid or not // Returned path is always canonicalized to forward slashes private static MutableString /*!*/ ExpandPath(RubyContext /*!*/ context, MutableString /*!*/ path) { PlatformAdaptationLayer pal = context.DomainManager.Platform; int length = path.Length; try { if (path == null || length == 0) { return(Glob.CanonicalizePath(MutableString.Create(Directory.GetCurrentDirectory()))); } if (length == 1 && path.GetChar(0) == '~') { return(Glob.CanonicalizePath(MutableString.Create(Path.GetFullPath(pal.GetEnvironmentVariable("HOME"))))); } if (path.GetChar(0) == '~' && (path.GetChar(1) == Path.DirectorySeparatorChar || path.GetChar(1) == Path.AltDirectorySeparatorChar)) { string homeDirectory = pal.GetEnvironmentVariable("HOME"); return(Glob.CanonicalizePath(length < 3 ? MutableString.Create(homeDirectory) : MutableString.Create(Path.Combine(homeDirectory, path.GetSlice(2).ConvertToString())))); } else { return(Glob.CanonicalizePath(MutableString.Create(Path.GetFullPath(path.ConvertToString())))); } } catch (Exception e) { // Re-throw exception as a reasonable Ruby exception throw new Errno.InvalidError(path.ConvertToString(), e); } }
public static MutableString /*!*/ ExpandPath(RubyContext /*!*/ context, RubyClass /*!*/ self, [DefaultProtocol, NotNull] MutableString /*!*/ path, [DefaultProtocol, Optional] MutableString basePath) { // We ignore basePath parameter if first string starts with a ~ if (basePath == null || path.GetFirstChar() == '~') { return(ExpandPath(context, path)); } else { return(Glob.CanonicalizePath(MutableString.Create( Path.GetFullPath(Path.Combine(ExpandPath(context, basePath).ConvertToString(), path.ConvertToString())) ))); } }
public static MutableString /*!*/ Basename(RubyClass /*!*/ self, [DefaultProtocol, NotNull] MutableString /*!*/ path, [DefaultProtocol, NotNull, Optional] MutableString extensionFilter) { if (path.IsEmpty) { return(path); } MutableString trimmedPath = TrimTrailingSlashes(path); // Special cases of drive letters C:\\ or C:/ if (trimmedPath.Length == 2) { if (Char.IsLetter(trimmedPath.GetChar(0)) && trimmedPath.GetChar(1) == ':') { var result = (path.Length > 2 ? MutableString.Create(path.GetChar(2).ToString()) : MutableString.CreateMutable()); return(result.TaintBy(path)); } } string trimmedPathAsString = trimmedPath.ConvertToString(); if (trimmedPathAsString == "/") { return(trimmedPath); } string filename = Path.GetFileName(trimmedPath.ConvertToString()); // Handle UNC host names correctly string root = Path.GetPathRoot(trimmedPath.ConvertToString()); if (MutableString.IsNullOrEmpty(extensionFilter)) { return(MutableString.Create(trimmedPathAsString == root ? root : filename)); } string fileExtension = Path.GetExtension(filename); string basename = Path.GetFileNameWithoutExtension(filename); string strResult = WildcardExtensionMatch(fileExtension, extensionFilter.ConvertToString()) ? basename : filename; return(Glob.CanonicalizePath(MutableString.Create(strResult)).TaintBy(path)); }
public static MutableString /*!*/ DirName(RubyClass /*!*/ self, MutableString /*!*/ path) { string directoryName = path.ConvertToString(); if (IsValidPath(path.ConvertToString())) { directoryName = Path.GetDirectoryName(path.ConvertToString()); string fileName = Path.GetFileName(path.ConvertToString()); if (!String.IsNullOrEmpty(fileName)) { directoryName = StripPathCharacters(path.ConvertToString().Replace(fileName, "")); } } else { if (directoryName.Length > 1) { directoryName = "//"; } } return(Glob.CanonicalizePath(MutableString.Create(String.IsNullOrEmpty(directoryName) ? "." : directoryName))); }