Pchp.Library.PhpPath.dirname C# (CSharp) Method

dirname() public static method

Returns directory name component of path.
public static dirname ( string path, int levels = 1 ) : string
path string The full path.
levels int The number of parent directories to go up. Must be greater than zero.
return string
        public static string dirname(string path, int levels = 1)
        {
            if (string.IsNullOrEmpty(path))
            {
                return null;
            }

            if (levels != 1)
            {
                // added in php 7.0
                throw new NotImplementedException();
            }

            int start = 0;
            int end = path.Length - 1;

            // advance start position beyond drive specifier:
            if (path.Length >= 2 && path[1] == ':' && (path[0] >= 'a' && path[0] <= 'z' || path[0] >= 'A' && path[0] <= 'Z'))
            {
                start = 2;
                if (path.Length == 2)
                    return path;
            }

            // strip slashes from the end:
            while (end >= start && path[end].IsDirectorySeparator()) end--;
            if (end < start)
                return path.Substring(0, end + 1) + PathUtils.AltDirectorySeparator;

            // strip file name:
            while (end >= start && !path[end].IsDirectorySeparator()) end--;
            if (end < start)
                return path.Substring(0, end + 1) + '.';

            // strip slashes from the end:
            while (end >= start && path[end].IsDirectorySeparator()) end--;
            if (end < start)
                return path.Substring(0, end + 1) + PathUtils.AltDirectorySeparator;

            return path.Substring(0, end + 1);
        }

Usage Example

Ejemplo n.º 1
0
        /// <summary>
        /// Extracts part(s) from a specified path.
        /// </summary>
        /// <param name="path">The path to be parsed.</param>
        /// <param name="options">Flags determining the result.</param>
        /// <returns>
        /// If <paramref name="options"/> is <see cref="PathInfoOptions.All"/> then returns array
        /// keyed by <c>"dirname"</c>, <c>"basename"</c>, and <c>"extension"</c>. Otherwise,
        /// it returns string value containing a single part of the path.
        /// </returns>
        public static PhpValue pathinfo(string path, PathInfoOptions options = PathInfoOptions.All)
        {
            // collect strings
            string dirname = null, basename = null, extension = null, filename = null;

            if ((options & PathInfoOptions.BaseName) != 0 ||
                (options & PathInfoOptions.Extension) != 0 ||
                (options & PathInfoOptions.FileName) != 0)
            {
                basename = PhpPath.basename(path);
            }

            if ((options & PathInfoOptions.DirName) != 0)
            {
                dirname = PhpPath.dirname(path);
            }

            if ((options & PathInfoOptions.Extension) != 0)
            {
                int last_dot = basename.LastIndexOf('.');
                if (last_dot >= 0)
                {
                    extension = basename.Substring(last_dot + 1);
                }
            }

            if ((options & PathInfoOptions.FileName) != 0)
            {
                int last_dot = basename.LastIndexOf('.');
                if (last_dot >= 0)
                {
                    filename = basename.Substring(0, last_dot);
                }
                else
                {
                    filename = basename;
                }
            }

            // return requested value or all of them in an associative array
            if (options == PathInfoOptions.All)
            {
                var result = new PhpArray(4);
                result.Add("dirname", dirname);
                result.Add("basename", basename);
                result.Add("extension", extension);
                result.Add("filename", filename);
                return(PhpValue.Create(result));
            }

            if ((options & PathInfoOptions.DirName) != 0)
            {
                return(PhpValue.Create(dirname));
            }

            if ((options & PathInfoOptions.BaseName) != 0)
            {
                return(PhpValue.Create(basename));
            }

            if ((options & PathInfoOptions.Extension) != 0)
            {
                return(PhpValue.Create(extension));
            }

            if ((options & PathInfoOptions.FileName) != 0)
            {
                return(PhpValue.Create(filename));
            }

            return(PhpValue.Null);
        }