Ejemplo n.º 1
0
        public static int GetDepth(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                return(0);
            }

            var depth = path.Split(PathSeparator.ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Length - 1;

            return(depth);
        }
Ejemplo n.º 2
0
        private void Initialize(string rawPath, IPathParser parser)
        {
            if (rawPath == null)
            {
                return;
            }

            Drive      = parser.ParseDrive(rawPath) ?? "";
            DriveLabel = PathUtils.GetDriveLabel(rawPath) ?? "";
            Root       = parser.ParseRoot(rawPath) ?? "";

            if (Drive.Length + Root.Length >= rawPath.Length)
            {
                return;
            }

            rawPath = rawPath.Substring(Drive.Length + Root.Length);

            // Since the drive can contain invalid characters like '\\?\' or
            // ':', we want to wait until after we parse the drive and root.
            char reservedCharacter;

            if (parser.ReservedCharactersInPath(rawPath, out reservedCharacter))
            {
                throw new InvalidPathException(rawPath, String.Format(
                                                   "Path contains reserved character '{0}'.", reservedCharacter));
            }

            // Remove trailing slash
            // This is what Python's pathlib does, but I don't think it's
            // necessarily required by spec
            if (rawPath.EndsWith(PathSeparator))
            {
                rawPath = rawPath.TrimEnd(PathSeparator.ToCharArray());
            }

            Dirname = parser.ParseDirname(rawPath) ?? "";
            rawPath = rawPath.Substring(Dirname.Length);

            Basename = parser.ParseBasename(rawPath) ?? "";
            rawPath  = rawPath.Substring(Basename.Length);

            Extension = parser.ParseExtension(rawPath) ?? "";

            // If filename is just an extension, consider it a "hidden file"
            // where the leading dot is the filename, not the extension.
            if (Basename == String.Empty && Extension != String.Empty)
            {
                Basename  = Extension;
                Extension = String.Empty;
            }

            Normalize();
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Determines whether the specified path is valid.
        /// </summary>
        /// <param name="path">The path.</param>
        /// <returns>
        ///     <c>true</c> if the specified path is valid; otherwise, <c>false</c>.
        /// </returns>
        public static PathResult IsValidPath(string path)
        {
            if (path == null)
            {
                throw new ArgumentNullException("path");
            }
            if (path.Length == 0)
            {
                return(PathResult.Empty);
            }
            if (path.Length > Data.DataProvider.Current.PathMaxLength)
            {
                return(PathResult.TooLong);
            }
            if (PathContainsInvalidChar(path))
            {
                return(PathResult.InvalidPathChar);
            }

            string[] segments = path.Split(PathSeparator.ToCharArray());
            for (int i = 0; i < segments.Length; i++)
            {
                string segment = segments[i];
                if (i == 0)
                {
                    if (segment.Length != 0)
                    {
                        return(PathResult.InvalidFirstChar);
                    }
                }
                else
                {
                    if (segment.Length == 0)
                    {
                        return(PathResult.Empty);
                    }
                    if (Char.IsWhiteSpace(segment[0]))
                    {
                        return(PathResult.StartsWithSpace);
                    }
                    if (Char.IsWhiteSpace(segment[segment.Length - 1]))
                    {
                        return(PathResult.EndsWithSpace);
                    }
                    if (segment[segment.Length - 1] == '.')
                    {
                        return(PathResult.EndsWithDot);
                    }
                }
            }
            return(PathResult.Correct);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Determines whether a given path falls within the specified scope.
        /// </summary>
        /// <param name="scopeToCheck">The scope value to check.</param>
        /// <param name="scope">The scope constraint being checked.  Specify a value of
        /// string.Empty if there is no scope constraint.</param>
        /// <returns><c>true</c> if the <paramref name="scopeToCheck"/> is determined to fall within <paramref name="scope"/>;
        /// otherwise, <c>false</c>.</returns>
        /// <remarks>In general, <paramref name="scope"/> is considered to be within the scope of <paramref name="scopeToCheck"/>
        /// when both values are well formed and <paramref name="scope"/> is an empty string, or each element in
        /// <paramref name="scope"/> matches the corresponding element in <paramref name="scopeToCheck"/> (based on position).</remarks>
        /// <exception cref="ArgumentException"><paramref name="scopeToCheck"/> or <paramref name="scope"/> is <c>null</c>.</exception>
        /// <example>The following example shows how to determine if a path is within the scope of another path.
        /// <code>
        /// string scopeToCheck = @"/a/b/c";
        /// string scope = @"/a/b";
        /// BasicPathScopeInterpreter comparer = new BasicPathScopeInterpreter();
        ///
        /// if (comparer.IsInScope(scopeToCheck, scope))
        /// {
        ///   // It's in scope, do something...
        /// }
        /// </code>
        /// </example>
        public bool IsInScope(string scopeToCheck, string scope)
        {
            if (!IsScopeStringWellFormed(scopeToCheck.Trim()))
            {
                throw new ArgumentException(string.Format(
                                                "{0} is not a well-formed basic scope string.", "scopeToCheck"));
            }

            if (!IsScopeStringWellFormed(scope.Trim()))
            {
                throw new ArgumentException(string.Format(
                                                "{0} is not a well-formed basic scope string.", "scope"));
            }

            scopeToCheck = scopeToCheck.Trim();
            scope        = scope.Trim();

            if (string.IsNullOrEmpty(scope) || scope.Equals(PathSeparator, StringComparison.InvariantCultureIgnoreCase))
            {
                return(true);
            }

            if (string.IsNullOrEmpty(scopeToCheck))
            {
                return(false);
            }

            string[] scopeToCheckPath = scopeToCheck.Trim().Split(PathSeparator.ToCharArray());
            string[] scopePath        = scope.Trim().Split(PathSeparator.ToCharArray());

            if (scopePath.Length > scopeToCheckPath.Length)
            {
                return(false);
            }

            bool isInScope = true;

            for (int i = 0; i < scopePath.Length; ++i)
            {
                if (!scopeToCheckPath[i].Equals(scopePath[i], StringComparison.InvariantCultureIgnoreCase))
                {
                    isInScope = false;
                    break;
                }
            }

            return(isInScope);
        }
Ejemplo n.º 5
0
        public TreeNode DrillToNodePath(string nodePath)
        {
            TreeNode foundNode = null;

            if (Nodes != null)
            {
                if (Nodes.Count > 0 && !String.IsNullOrEmpty(nodePath))
                {
                    string[] parts = nodePath.Split(PathSeparator.ToCharArray());

                    foundNode = DrillToNode(Nodes, new List <string>(parts));
                }
            }

            return(foundNode);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// フルパスで指定されたツリービューを追加する
        /// </summary>
        /// <param name="p_strPath">フルパス</param>
        /// <param name="p_nImageIndex">イメージID</param>
        /// <param name="p_nSelectedImageIndex">選択中のイメージID</param>
        /// <returns></returns>
        public TreeNode AddNode(string p_strPath, int p_nImageIndex = -1, int p_nSelectedImageIndex = -1)
        {
            // パスを分割する
            string[]           aryTree = p_strPath.Split(PathSeparator.ToCharArray());
            TreeNodeCollection objRoot = Nodes;  // 追加先ノード
            TreeNode           objNode = null;   // 追加対象

            // パスを順にたどる
            foreach (string strNode in aryTree)
            {
                if (strNode == "")
                {
                    continue;
                }

                // すでに存在するか確認
                TreeNode[] objFind = objRoot.Find(strNode, false);

                if (objFind.Length == 0)
                {
                    // なかったら作成する : イメージIDが未設定 / 設定済みでオーバロード切り替え
                    objNode = (p_nImageIndex == -1) ? objRoot.Add(strNode, strNode)
                                                        : objRoot.Add(strNode, strNode, p_nImageIndex, p_nSelectedImageIndex);
                    // 親ノードを展開する
                    if (objNode.Parent != null)
                    {
                        objNode.Parent.ExpandAll();
                    }
                    objRoot = objNode.Nodes;                    // ノードを子パスに切り替える
                }
                else
                {
                    objNode = objFind[0];
                    objRoot = objNode.Nodes;                     // 見つけたノードの子パスをルートにする
                }
            }
            return(objNode);
        }