/// <summary> /// Преобразует набор точек в путь /// </summary> /// <param name="points">Коллекция точек</param> /// <param name="pathKind">Тип пути - Линия(замкнутая/открытая), либо Эллипс</param> /// <returns></returns> public static string PointsToPath(PointCollection points, PathKind pathKind) { var enumerable = points.ToArray(); if (!enumerable.Any()) { return string.Empty; } switch (pathKind) { case PathKind.Ellipse: { var radiusX = (enumerable[1].X - enumerable[0].X) / 2; var radiusY = (enumerable[2].Y - enumerable[1].Y) / 2; var center = new Point(enumerable[1].X - radiusX, enumerable[2].Y - radiusY); var geometry = new EllipseGeometry(center: center, radiusX: radiusX, radiusY: radiusY); return geometry.GetFlattenedPathGeometry().ToString(CultureInfo.InvariantCulture); } default: { var start = enumerable[0]; var segments = new List<LineSegment>(); for (var i = 1; i < enumerable.Length; i++) { segments.Add(new LineSegment(new Point(enumerable[i].X, enumerable[i].Y), true)); } var figure = new PathFigure(start, segments, (pathKind == PathKind.ClosedLine)); var geometry = new PathGeometry(); geometry.Figures.Add(figure); return geometry.ToString(CultureInfo.InvariantCulture); } } }
/// <summary> /// Initializes a new instance of the <see cref="FilePath"/> class.. /// </summary> /// <param name="path">The path.</param> /// <param name="pathKind">Specifies whether the path is relative, absolute, or indeterminate.</param> public FilePath(string path, PathKind pathKind) : base(path, pathKind) { }
public TestPath(string path, PathKind pathKind = PathKind.RelativeOrAbsolute) : base(path, pathKind) { }
private static PathKind CombinePathKind(PathKind current, PathKind next) { switch (current) { case PathKind.File: switch (next) { case PathKind.File: return(PathKind.File); case PathKind.Directory: return(PathKind.FileAndDirectory); case PathKind.FileOrDirectory: return(PathKind.File); case PathKind.FileAndDirectory: return(PathKind.FileAndDirectory); default: throw new ArgumentOutOfRangeException("next"); } case PathKind.Directory: switch (next) { case PathKind.File: return(PathKind.FileAndDirectory); case PathKind.Directory: return(PathKind.Directory); case PathKind.FileOrDirectory: return(PathKind.Directory); case PathKind.FileAndDirectory: return(PathKind.FileAndDirectory); default: throw new ArgumentOutOfRangeException("next"); } case PathKind.FileOrDirectory: switch (next) { case PathKind.File: return(PathKind.File); case PathKind.Directory: return(PathKind.Directory); case PathKind.FileOrDirectory: return(PathKind.FileOrDirectory); case PathKind.FileAndDirectory: return(PathKind.FileAndDirectory); default: throw new ArgumentOutOfRangeException("next"); } case PathKind.FileAndDirectory: switch (next) { case PathKind.File: return(PathKind.FileAndDirectory); case PathKind.Directory: return(PathKind.FileAndDirectory); case PathKind.FileOrDirectory: return(PathKind.FileAndDirectory); case PathKind.FileAndDirectory: return(PathKind.FileAndDirectory); default: throw new ArgumentOutOfRangeException("next"); } default: throw new ArgumentOutOfRangeException("current"); } }
public override State OnWatcherFileRenamedEvent(object sender, RenamedEventArgs args, PathKind pathKind) { var watcher = (IFileSystemWatcher)sender; // Note: Both arg.Name and args.OldName can be null, for obscure reasons explained // in the. .NET source code. // See https://referencesource.microsoft.com/#System/services/io/system/io/FileSystemWatcher.cs,583 // See also: https://referencesource.microsoft.com/#System/services/io/system/io/FileSystemWatcher.cs,589 // // We need to defend ourselves against that so that we don't miss updates. if (!string.IsNullOrEmpty(args.OldName)) { var oldPath = PathHelpers.CombinePaths(watcher.Path.Value, args.OldName); LogPathForDebugging(oldPath, PathChangeKind.Deleted, pathKind); if (!SkipPath(oldPath)) { EnqueueChangeEvent(watcher.Path, new RelativePath(args.OldName), PathChangeKind.Deleted, pathKind); } } if (!string.IsNullOrEmpty(args.Name)) { var path = PathHelpers.CombinePaths(watcher.Path.Value, args.Name); LogPathForDebugging(path, PathChangeKind.Created, pathKind); if (!SkipPath(path)) { EnqueueChangeEvent(watcher.Path, new RelativePath(args.Name), PathChangeKind.Created, pathKind); } } StateHost.PollingThread.WakeUp(); return(this); }
/// <summary> /// Tries the parse path. /// </summary> /// <param name="path">The path.</param> /// <param name="pathKind">Kind of the path.</param> /// <param name="origin">The origin.</param> /// <param name="dataPath">The data path.</param> /// <param name="computerName">Name of the computer.</param> /// <param name="shareName">Name of the share.</param> /// <param name="driveLetter">The drive letter.</param> /// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns> private static bool TryParsePath(string path, out PathKind pathKind, out string origin, out string dataPath, out string computerName, out string shareName, out char driveLetter) { computerName = null; shareName = null; driveLetter = '\0'; origin = null; dataPath = null; Match match = PathPatterns.Match(path); if (!match.Success) { pathKind = PathKind.Invalid; return(false); } origin = match.Value; dataPath = path.Substring(origin.Length); if (match.Groups.Count != 11) { pathKind = PathKind.Invalid; return(false); } var groupSimpleDrive = match.Groups[1]; var groupExtDrive = match.Groups[2]; var groupUncDrive_Server = match.Groups[3]; var groupUncDrive_Drive = match.Groups[4]; var groupUncServerShare_Server = match.Groups[5]; var groupUncServerShare_Share = match.Groups[6]; var groupExtUncDrive_Server = match.Groups[7]; var groupExtUncDrive_Drive = match.Groups[8]; var groupExtUncShare_Server = match.Groups[9]; var groupExtUncShare_Share = match.Groups[10]; if (groupSimpleDrive.Success) { pathKind = PathKind.SimpleDrive; driveLetter = groupSimpleDrive.Value[0]; return(true); } if (groupExtDrive.Success) { pathKind = PathKind.ExtDrive; driveLetter = groupExtDrive.Value[0]; return(true); } if (groupUncDrive_Server.Success && groupUncDrive_Drive.Success) { pathKind = PathKind.UncDrive; computerName = groupUncDrive_Server.Value; driveLetter = groupUncDrive_Drive.Value[0]; return(true); } if (groupUncServerShare_Server.Success && groupUncServerShare_Share.Success) { pathKind = PathKind.UncShare; computerName = groupUncServerShare_Server.Value; shareName = groupUncServerShare_Share.Value; return(true); } if (groupExtUncDrive_Server.Success && groupExtUncDrive_Drive.Success) { pathKind = PathKind.ExtUncDrive; computerName = groupExtUncDrive_Server.Value; driveLetter = groupExtUncDrive_Drive.Value[0]; return(true); } if (groupExtUncShare_Server.Success && groupExtUncShare_Share.Success) { pathKind = PathKind.ExtUncShare; computerName = groupExtUncShare_Server.Value; shareName = groupExtUncShare_Share.Value; return(true); } pathKind = PathKind.Invalid; return(false); }
private State OnWatcherSingleFileChange(object sender, FileSystemEventArgs args, PathKind pathKind, PathChangeKind changeKind) { var watcher = (IFileSystemWatcher)sender; var path = PathHelpers.CombinePaths(watcher.Path.Value, args.Name); LogPathForDebugging(path, PathChangeKind.Changed, pathKind); if (SkipPath(path)) { return(this); } EnqueueChangeEvent(watcher.Path, new RelativePath(args.Name), changeKind, pathKind); StateHost.PollingThread.WakeUp(); return(this); }
/// <summary> /// Initializes a new instance of the <see cref="DirectoryPath" /> class /// with the specified file provider. /// </summary> /// <param name="fileProvider">The file provider.</param> /// <param name="path">The path.</param> /// <param name="pathKind">Specifies whether the path is relative, absolute, or indeterminate.</param> public DirectoryPath(Uri fileProvider, string path, PathKind pathKind) : base(fileProvider, path, pathKind) { }
private static string ResolveRelativePath(PathKind kind, string path, string basePath, string baseDirectory) { switch (kind) { case PathKind.Empty: return(null); case PathKind.Relative: baseDirectory = GetBaseDirectory(basePath, baseDirectory); if (baseDirectory == null) { return(null); } // with no search paths relative paths are relative to the base directory: return(PathUtilities.CombinePathsUnchecked(baseDirectory, path)); case PathKind.RelativeToCurrentDirectory: baseDirectory = GetBaseDirectory(basePath, baseDirectory); if (baseDirectory == null) { return(null); } if (path.Length == 1) { // "." return(baseDirectory); } else { // ".\path" return(PathUtilities.CombinePathsUnchecked(baseDirectory, path)); } case PathKind.RelativeToCurrentParent: baseDirectory = GetBaseDirectory(basePath, baseDirectory); if (baseDirectory == null) { return(null); } // ".." return(PathUtilities.CombinePathsUnchecked(baseDirectory, path)); case PathKind.RelativeToCurrentRoot: string baseRoot; if (basePath != null) { baseRoot = PathUtilities.GetPathRoot(basePath); } else if (baseDirectory != null) { baseRoot = PathUtilities.GetPathRoot(baseDirectory); } else { return(null); } if (string.IsNullOrEmpty(baseRoot)) { return(null); } Debug.Assert(PathUtilities.IsDirectorySeparator(path[0])); Debug.Assert(path.Length == 1 || !PathUtilities.IsDirectorySeparator(path[1])); return(PathUtilities.CombinePathsUnchecked(baseRoot, path.Substring(1))); case PathKind.RelativeToDriveDirectory: // drive relative paths not supported, can't resolve: return(null); case PathKind.Absolute: return(path); default: throw UnexpectedValue(kind); } }
/// <summary> /// Initializes a new instance of the <see cref="DirectoryPath"/> class. /// </summary> /// <param name="path">The path.</param> /// <param name="pathKind">Specifies whether the path is relative, absolute, or indeterminate.</param> public DirectoryPath(string path, PathKind pathKind) : base(path, pathKind) { }
private static bool GetIsAbsolute(PathKind pathKind, ReadOnlySpan <char> path) => pathKind switch {
public override State OnWatcherFileRenamedEvent(object sender, RenamedEventArgs args, PathKind pathKind) { return(this); }
public TestPath(Uri fileProvider, string path, PathKind pathKind = PathKind.RelativeOrAbsolute) : base(fileProvider, path, pathKind) { }
internal static HasProviderExtensions SetKind(this HasProviderExtensions producerSettings, PathKind kind) { producerSettings.Properties[nameof(SetKind)] = kind; return(producerSettings); }
private void EnqueueChangeEvent(FullPath rootPath, RelativePath entryPath, PathChangeKind changeKind, PathKind pathKind) { //Logger.LogInfo("Enqueue change event: {0}, {1}", path, changeKind); var entry = new PathChangeEntry(rootPath, entryPath, changeKind, pathKind); GlobalChangeRecorder.RecordChange(new PathChangeRecorder.ChangeInfo { Entry = entry, TimeStampUtc = DateTimeProvider.UtcNow, }); MergePathChange(_enqueuedChangedPaths, entry); }
private static string ResolveRelativePath(PathKind kind, string path, string basePath, string baseDirectory) { switch (kind) { case PathKind.Empty: return(null); case PathKind.Relative: baseDirectory = GetBaseDirectory(basePath, baseDirectory); if (baseDirectory == null) { return(null); } // with no search paths relative paths are relative to the base directory: return(PathUtilities.CombinePathsUnchecked(baseDirectory, path)); case PathKind.RelativeToCurrentDirectory: baseDirectory = GetBaseDirectory(basePath, baseDirectory); if (baseDirectory == null) { return(null); } if (path.Length == 1) { // "." return(baseDirectory); } else { // ".\path" return(PathUtilities.CombinePathsUnchecked(baseDirectory, path)); } case PathKind.RelativeToCurrentParent: baseDirectory = GetBaseDirectory(basePath, baseDirectory); if (baseDirectory == null) { return(null); } // ".." return(PathUtilities.CombinePathsUnchecked(baseDirectory, path)); case PathKind.RelativeToCurrentRoot: string baseRoot; if (basePath != null) { baseRoot = PathUtilities.GetPathRoot(basePath); } else if (baseDirectory != null) { baseRoot = PathUtilities.GetPathRoot(baseDirectory); } else { return(null); } if (baseRoot == null) { return(null); } Debug.Assert(PathUtilities.IsDirectorySeparator(path[0])); Debug.Assert(path.Length == 1 || !PathUtilities.IsDirectorySeparator(path[1])); Debug.Assert(baseRoot.Length >= 3); return(PathUtilities.CombinePathsUnchecked(baseRoot, path.Substring(1))); case PathKind.RelativeToDriveDirectory: // drive relative paths not supported, can't resolve: return(null); case PathKind.Absolute: return(path); default: // EDMAURER this is not using ExceptionUtilities.UnexpectedValue() because this file // is shared via linking with other code that doesn't have the ExceptionUtilities. throw new InvalidOperationException(string.Format("Unexpected PathKind {0}.", kind)); } }
public override State OnWatcherFileDeletedEvent(object sender, FileSystemEventArgs args, PathKind pathKind) { return(OnWatcherSingleFileChange(sender, args, pathKind, PathChangeKind.Deleted)); }
/// <summary> /// Initializes a new instance of the <see cref="NormalizedPath" /> class. /// </summary> /// <param name="path">The path.</param> /// <param name="pathKind">Specifies whether the path is relative, absolute, or indeterminate.</param> protected NormalizedPath(string path, PathKind pathKind) : this(GetFileProviderAndPath(null, path), false, pathKind) { }
/// <summary>Initializes a new instance of the <see cref="NormalizedPath" /> class.</summary> /// <param name="providerAndPath"> /// The provider and path as a Tuple so it can be passed from both /// of the other constructors. /// </param> /// <param name="fullySpecified"> /// If set to <c>true</c> indicates that this constructor was called /// from one where the provider and path were fully specified (as opposed to being inferred). /// </param> /// <param name="pathKind">Specifies whether the path is relative, absolute, or indeterminate.</param> private NormalizedPath(Tuple <Uri, string> providerAndPath, bool fullySpecified, PathKind pathKind) { if (providerAndPath.Item2 == null) { throw new ArgumentNullException(); } if (string.IsNullOrWhiteSpace(providerAndPath.Item2)) { throw new ArgumentException("Path cannot be empty"); } // Leave spaces since they're valid path chars FullPath = providerAndPath.Item2.Replace('\\', '/').Trim('\r', '\n', '\t'); // Remove relative part of a path, but only if it's not the only part if (FullPath.StartsWith("./", StringComparison.Ordinal) && FullPath.Length > 2) { FullPath = FullPath.Substring(2); } // Remove trailing slashes (as long as this isn't just a slash) if (FullPath.Length > 1) { FullPath = FullPath.TrimEnd('/'); } if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows) && FullPath.EndsWith(":", StringComparison.OrdinalIgnoreCase)) { FullPath = string.Concat(FullPath, "/"); } // Absolute path? switch (pathKind) { case PathKind.RelativeOrAbsolute: IsAbsolute = System.IO.Path.IsPathRooted(FullPath); break; case PathKind.Absolute: IsAbsolute = true; break; case PathKind.Relative: IsAbsolute = false; break; } // Set provider (but only if absolute) if (IsRelative && providerAndPath.Item1 != null) { throw new ArgumentException("Can not specify provider for relative paths"); } if (providerAndPath.Item1 == null && IsAbsolute && !fullySpecified) { FileProvider = DefaultFileProvider; } else if (providerAndPath.Item1?.IsAbsoluteUri == false) { throw new ArgumentException("The provider URI must always be absolute"); } else { FileProvider = providerAndPath.Item1; } // Extract path segments. Segments = FullPath.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries); }
/// <summary> /// Initializes a new instance of the <see cref="NormalizedPath" /> class /// with the given provider. /// </summary> /// <param name="fileProvider">The provider for this path.</param> /// <param name="path">The path.</param> /// <param name="pathKind">Specifies whether the path is relative, absolute, or indeterminate.</param> protected NormalizedPath(string fileProvider, string path, PathKind pathKind) : this(GetFileProviderUri(fileProvider), path, pathKind) { }
/// <summary> /// Initializes a new instance of the <see cref="NormalizedPath" /> class /// with the given provider. /// </summary> /// <param name="fileProvider">The provider for this path.</param> /// <param name="path">The path.</param> /// <param name="pathKind">Specifies whether the path is relative, absolute, or indeterminate.</param> protected NormalizedPath(Uri fileProvider, string path, PathKind pathKind) : this(Tuple.Create(fileProvider, path), true, pathKind) { }
/// <summary> /// Initializes a new instance of the <see cref="FilePath" /> class /// with the specified file provider. /// </summary> /// <param name="fileProvider">The file provider.</param> /// <param name="path">The path.</param> /// <param name="pathKind">Specifies whether the path is relative, absolute, or indeterminate.</param> public FilePath(Uri fileProvider, string path, PathKind pathKind) : base(fileProvider, path, pathKind) { }
public override State OnWatcherFileDeletedEvent(object sender, FileSystemEventArgs args, PathKind pathKind) { return(this); }
private static string ResolveRelativePath(PathKind kind, string path, string basePath, string baseDirectory) { switch (kind) { case PathKind.Empty: return null; case PathKind.Relative: baseDirectory = GetBaseDirectory(basePath, baseDirectory); if (baseDirectory == null) { return null; } // with no search paths relative paths are relative to the base directory: return PathUtilities.CombinePathsUnchecked(baseDirectory, path); case PathKind.RelativeToCurrentDirectory: baseDirectory = GetBaseDirectory(basePath, baseDirectory); if (baseDirectory == null) { return null; } if (path.Length == 1) { // "." return baseDirectory; } else { // ".\path" return PathUtilities.CombinePathsUnchecked(baseDirectory, path); } case PathKind.RelativeToCurrentParent: baseDirectory = GetBaseDirectory(basePath, baseDirectory); if (baseDirectory == null) { return null; } // ".." return PathUtilities.CombinePathsUnchecked(baseDirectory, path); case PathKind.RelativeToCurrentRoot: string baseRoot; if (basePath != null) { baseRoot = PathUtilities.GetPathRoot(basePath); } else if (baseDirectory != null) { baseRoot = PathUtilities.GetPathRoot(baseDirectory); } else { return null; } if (baseRoot == null) { return null; } Debug.Assert(PathUtilities.IsDirectorySeparator(path[0])); Debug.Assert(path.Length == 1 || !PathUtilities.IsDirectorySeparator(path[1])); Debug.Assert(baseRoot.Length >= 3); return PathUtilities.CombinePathsUnchecked(baseRoot, path.Substring(1)); case PathKind.RelativeToDriveDirectory: // drive relative paths not supported, can't resolve: return null; case PathKind.Absolute: return path; default: // EDMAURER this is not using ExceptionUtilities.UnexpectedValue() because this file // is shared via linking with other code that doesn't have the ExceptionUtilities. throw new InvalidOperationException(string.Format("Unexpected PathKind {0}.", kind)); } }
public void TestIsWellFormedPath(string path, PathKind pathState) { switch (pathState) { case PathKind.Throws: Assert.Throws <ArgumentException>(() => PathHelpers.IsWellFormedPath(path)); Assert.Throws <ArgumentException>(() => PathHelpers.IsWellFormedAbsolutePath(path)); Assert.Throws <ArgumentException>(() => PathHelpers.IsWellFormedRelativePath(path)); Assert.Throws <ArgumentException>(() => PathHelpers.IsWellFormedContainerPath(path)); Assert.Throws <ArgumentException>(() => PathHelpers.IsWellFormedSimpleName(path)); break; case PathKind.Invalid: Assert.AreEqual(PathHelpers.IsWellFormedPath(path), false); Assert.AreEqual(PathHelpers.IsWellFormedAbsolutePath(path), false); Assert.AreEqual(PathHelpers.IsWellFormedRelativePath(path), false); Assert.AreEqual(PathHelpers.IsWellFormedContainerPath(path), false); Assert.AreEqual(PathHelpers.IsWellFormedSimpleName(path), false); break; case PathKind.ValidAbsolutePath: Assert.AreEqual(PathHelpers.IsWellFormedPath(path), true); Assert.AreEqual(PathHelpers.IsWellFormedAbsolutePath(path), true); Assert.AreEqual(PathHelpers.IsWellFormedRelativePath(path), false); Assert.AreEqual(PathHelpers.IsWellFormedContainerPath(path), false); Assert.AreEqual(PathHelpers.IsWellFormedSimpleName(path), false); break; case PathKind.ValidAbsoluteContainerPath: Assert.AreEqual(PathHelpers.IsWellFormedPath(path), true); Assert.AreEqual(PathHelpers.IsWellFormedAbsolutePath(path), true); Assert.AreEqual(PathHelpers.IsWellFormedRelativePath(path), false); Assert.AreEqual(PathHelpers.IsWellFormedContainerPath(path), true); Assert.AreEqual(PathHelpers.IsWellFormedSimpleName(path), false); break; case PathKind.ValidRelativePath: Assert.AreEqual(PathHelpers.IsWellFormedPath(path), true); Assert.AreEqual(PathHelpers.IsWellFormedAbsolutePath(path), false); Assert.AreEqual(PathHelpers.IsWellFormedRelativePath(path), true); Assert.AreEqual(PathHelpers.IsWellFormedContainerPath(path), false); Assert.AreEqual(PathHelpers.IsWellFormedSimpleName(path), false); break; case PathKind.ValidRelativeContainerPath: Assert.AreEqual(PathHelpers.IsWellFormedPath(path), true); Assert.AreEqual(PathHelpers.IsWellFormedAbsolutePath(path), false); Assert.AreEqual(PathHelpers.IsWellFormedRelativePath(path), true); Assert.AreEqual(PathHelpers.IsWellFormedContainerPath(path), true); Assert.AreEqual(PathHelpers.IsWellFormedSimpleName(path), false); break; case PathKind.ValidSimpleName: Assert.AreEqual(PathHelpers.IsWellFormedPath(path), true); Assert.AreEqual(PathHelpers.IsWellFormedAbsolutePath(path), false); Assert.AreEqual(PathHelpers.IsWellFormedRelativePath(path), true); Assert.AreEqual(PathHelpers.IsWellFormedContainerPath(path), false); Assert.AreEqual(PathHelpers.IsWellFormedSimpleName(path), true); break; default: throw new ArgumentOutOfRangeException(nameof(pathState), pathState, null); } }