Пример #1
0
        public Crosspath Append(RelativeCrosspath part)
        {
            foreach (String dir in part.directories)
            {
                Chdir(dir);
            }

            return(this);
        }
Пример #2
0
        /// <summary>
        /// Create a generic Crosspath object, which is actually an
        /// AbsoluteCrosspath or RelativeCrosspath depending on the input string.
        /// </summary>
        /// <param name="path">Any path string.</param>
        /// <returns>Crosspath object.</returns>
        public static Crosspath FromString(String path)
        {
            DetectParams(path, out CrosspathOrigin origin, out CrosspathFlavor flavor, out Char rootDrive);
            Crosspath xpath;

            switch (origin)
            {
            case CrosspathOrigin.Absolute:
                xpath = AbsoluteCrosspath.CreateInstance();
                break;

            case CrosspathOrigin.Relative:
                xpath = RelativeCrosspath.CreateInstance();
                break;

            default:
                throw new ArgumentOutOfRangeException();
            }

            xpath.Origin           = origin;
            xpath.Flavor           = flavor;
            xpath.WindowsRootDrive = rootDrive;
            xpath.SourceString     = path;
            xpath.directories      = new LinkedList <String>();

            // push directories
            String[] parts;
            if (flavor == CrosspathFlavor.Windows)
            {
                if (origin == CrosspathOrigin.Absolute)
                {
                    path = path.Substring(2);
                }

                parts = path.Split(new[] { '/', '\\' }, StringSplitOptions.RemoveEmptyEntries);
            }
            else /* if (Flavor == CrosspathFlavor.FlavorUnix) */
            {
                parts = path.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
            }

            foreach (String part in parts)
            {
                // TODO: check `part` for validity
                xpath.Chdir(part);
            }

            return(xpath);
        }
Пример #3
0
        internal Boolean EqualsToRelative(RelativeCrosspath relativeCrosspath)
        {
            if (this.WorkingDirectory == null)
            {
                if (relativeCrosspath.WorkingDirectory != null)
                {
                    return(false);
                }
            }

            if (this.WorkingDirectory != null && !this.WorkingDirectory.Equals(relativeCrosspath.WorkingDirectory))
            {
                return(false);
            }

            return(base.Equals(relativeCrosspath));
        }
Пример #4
0
 /// <summary>
 /// Creates a copy of instance.
 /// </summary>
 /// <param name="source">Source RelativeCrosspath object, which will remain untouched.</param>
 public RelativeCrosspath(RelativeCrosspath source) : base(source)
 {
     this.WorkingDirectory = source.WorkingDirectory;
 }
Пример #5
0
        public static RelativeCrosspath CreateRelativePath(AbsoluteCrosspath xpath, AbsoluteCrosspath workingDirectory, Boolean dontGoOut)
        {
            if (xpath.Flavor != workingDirectory.Flavor)
            {
                throw new PolymorphismException("can relativize only identically-flavored paths");
            }

            if (xpath.Flavor == CrosspathFlavor.Windows && xpath.WindowsRootDrive != workingDirectory.WindowsRootDrive)
            {
                throw new CrosspathLibException("cannot relativize different root drives");
            }

            /*
             * RelativeCrosspath ret = RelativeCrosspath.CreateInstance();
             * ret.Flavor = xpath.Flavor;
             * ret.Origin = CrosspathOrigin.Relative;
             * ret.WindowsRootDrive = xpath.WindowsRootDrive;
             * ret.directories = new LinkedList<String>();
             */
            RelativeCrosspath ret = RelativeCrosspath.FromString("");

            ret.Flavor           = xpath.Flavor;
            ret.WindowsRootDrive = xpath.WindowsRootDrive;
            ret.SetWorkingDirectory(workingDirectory);

            using (var myIter = xpath.directories.GetEnumerator()) {
                using (var theirsIter = workingDirectory.directories.GetEnumerator()) {
                    Boolean myMoved;
                    Boolean theirsMoved;

                    // first find a diverging point
                    while (true)
                    {
                        myMoved     = myIter.MoveNext();
                        theirsMoved = theirsIter.MoveNext();
                        if (!myMoved || !theirsMoved)
                        {
                            break;
                        }
                        if (myIter.Current != theirsIter.Current)
                        {
                            break;
                        }
                    }

                    if (theirsMoved && dontGoOut)
                    {
                        throw new CrosspathLibException("path is outide of working dir");
                    }

                    // then go back from workingDirectory and go forth on xpath

                    for (; theirsMoved; theirsMoved = theirsIter.MoveNext())
                    {
                        ret.Chdir("..");
                    }

                    for (; myMoved; myMoved = myIter.MoveNext())
                    {
                        ret.Chdir(myIter.Current);
                    }
                }
            }

            return(ret);
        }
Пример #6
0
 /// <summary>
 /// Creates a copy of AbsoluteCrosspath object, appended with another part.
 /// </summary>
 /// <param name="part">Path to append.</param>
 /// <returns>New AbsoluteCrosspath object, appended with part.</returns>
 public AbsoluteCrosspath Appended(RelativeCrosspath part)
 {
     return(new AbsoluteCrosspath(this).Append(part));
 }
Пример #7
0
 /// <summary>
 /// Appends a relative part to the path.
 /// </summary>
 /// <param name="part">Path to append.</param>
 /// <returns>Modified self object.</returns>
 public new AbsoluteCrosspath Append(RelativeCrosspath part)
 {
     return(base.Append(part) as AbsoluteCrosspath);
 }
Пример #8
0
 public RelativeCrosspath Relativized(AbsoluteCrosspath workingDir, Boolean dontGoOut = false)
 {
     return(RelativeCrosspath.CreateRelativePath(this, workingDir, dontGoOut));
 }