PathUtil.cs :  » Network-Clients » edtFTPnet » EnterpriseDT » Util » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Network Clients » edtFTPnet 
edtFTPnet » EnterpriseDT » Util » PathUtil.cs
// 
// Copyright (C) 2006 Enterprise Distributed Technologies Ltd
// 
// www.enterprisedt.com
//

#region Change Log

// Change Log:
// 
// $Log$
// Revision 1.4  2009-10-01 01:32:00  hans
// Combine failed if pathRight was null.
//
// Revision 1.3  2009-09-30 03:34:13  hans
// Fixed numerous bugs.
//
// Revision 1.2  2009-09-25 05:56:25  bruceb
// remove Contains
//
// Revision 1.1  2009-09-25 05:21:27  hans
// Utility for dealing with FTP (i.e. UNIX) paths.
//
//
//

#endregion

using System;
using System.Collections;
using System.Collections.Specialized;
using System.Text;
using System.Text.RegularExpressions;

using EnterpriseDT.Util.Debug;

namespace EnterpriseDT.Util{
    internal class PathUtil
    {
        //private static Logger log = Logger.GetLogger("PathUtil");
        private const char SEPARATOR_CHAR = '/';
        private const string SAMEDIR_STRING = ".";
        private const string UPDIR_STRING = "..";

        public static char SeparatorChar
        {
            get { return SEPARATOR_CHAR; }
        }

        public static string Separator
        {
            get { return SEPARATOR_CHAR.ToString(); }
        }

        public static string Fix(string path)
        {
            return Implode(Fix(Explode(path)));
        }

        public static StringCollection Fix(StringCollection path)
        {
            if (path.Count == 0)
                return path;
            StringCollection fixedPath = new StringCollection();
            for (int i = 0; i < path.Count; i++)
            {
                string p = path[i];
                if (p == Separator)
                {
                    // add separator unless it's a duplicate (i.e. follows another sep - "//")
                    if (i == 0 || (fixedPath.Count>0 && fixedPath[fixedPath.Count - 1] != Separator))
                        fixedPath.Add(p);
                }
                else if (p == UPDIR_STRING)
                {
                    if (fixedPath.Count == 1)
                        throw new ArgumentException("Cannot change up a directory from the root");

                    // only remove parent directory if there is one and it's not another ".."
                    if (fixedPath.Count >= 2 && fixedPath[fixedPath.Count - 2] != UPDIR_STRING)
                    {
                        fixedPath.RemoveAt(fixedPath.Count - 1);  // remove separator
                        fixedPath.RemoveAt(fixedPath.Count - 1);  // remove parent
                    }
                    else
                        fixedPath.Add(p);   // add the ".." 
                }
                else if (p != "" && p != SAMEDIR_STRING)  // ignore empty strings and "."
                    fixedPath.Add(p);
            }

            // remove trailing slashes
            while (fixedPath.Count > 1 && fixedPath[fixedPath.Count - 1] == Separator)
                fixedPath.RemoveAt(fixedPath.Count - 1);

            // if there's nothing then just return "."
            if (fixedPath.Count == 0)
                fixedPath.Add(SAMEDIR_STRING);

            return fixedPath;
        }

        public static bool IsAbsolute(string path)
        {
            return path!=null && path.StartsWith("/");
        }

        public static string GetFileName(string path)
        {
            if (path.IndexOf(SEPARATOR_CHAR.ToString()) >= 0)
                return path.Substring(path.LastIndexOf(SEPARATOR_CHAR) + 1);
            else
                return path;
        }

        public static string GetFolderPath(string path)
        {
            if (path.IndexOf(SEPARATOR_CHAR.ToString()) >= 0)
                return path.Substring(0, path.LastIndexOf(SEPARATOR_CHAR));
            else
                return path;
        }

        public static string Combine(string pathLeft, string pathRight)
        {
            if (pathLeft == null)
                pathLeft = "/";
            if (pathRight == null || pathRight == "" || pathRight == ".")
                return pathLeft;
            if (pathRight.StartsWith(Separator))
                throw new ArgumentException("Second argument cannot be absolute", "pathRight");
            if (pathLeft.EndsWith(Separator))
                pathLeft = pathLeft.Substring(0, pathLeft.Length - 1);
            return Fix(pathLeft + Separator + pathRight);
        }

        public static string Combine(string path1, string path2, params string[] pathN)
        {
            string path = Combine(path1, path2);
            foreach (string p in pathN)
                path = Combine(path, p);
            return path;
        }

        public static StringCollection Explode(string path)
        {
            StringCollection elements = new StringCollection();
            int pos1 = 0;
            while (true)
            {
                int pos2 = path.IndexOf(SeparatorChar, pos1);
                if (pos2 < 0)
                    break;
                if (pos1 == pos2)
                {
                    elements.Add(Separator);
                    pos1 = pos2 + 1;
                }
                else
                {
                    elements.Add(path.Substring(pos1, pos2 - pos1));
                    pos1 = pos2;
                }
            }
            if (pos1<path.Length)
                elements.Add(path.Substring(pos1));
            return elements;
        }

        public static string Implode(IEnumerable pathElements, int start, int length)
        {
            StringBuilder path = new StringBuilder();
            int i = 0;
            foreach (string p in pathElements)
            {
                if (i >= start && (length < 0 || i < start + length))
                    path.Append(p);
                i++;
            }
            return path.ToString();
        }

        public static string Implode(IEnumerable pathElements, int start)
        {
            return Implode(pathElements, start, -1);
        }

        public static string Implode(IEnumerable pathElements)
        {
            return Implode(pathElements, 0, -1);
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.