summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework/Utilities/PathUtilities.cs
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <github@jplamondonw.com>2018-02-24 17:54:31 -0500
committerJesse Plamondon-Willard <github@jplamondonw.com>2018-02-24 17:54:31 -0500
commit414cf5c197b5b59776d3dda914eb15710efb0868 (patch)
tree0393a95194ad78cf4440c68657b0488b7db6d68b /src/SMAPI/Framework/Utilities/PathUtilities.cs
parent5da8b707385b9851ff3f6442de58519125f5c96f (diff)
parentf2e8450706d1971d774f870081deffdb0c6b92eb (diff)
downloadSMAPI-414cf5c197b5b59776d3dda914eb15710efb0868.tar.gz
SMAPI-414cf5c197b5b59776d3dda914eb15710efb0868.tar.bz2
SMAPI-414cf5c197b5b59776d3dda914eb15710efb0868.zip
Merge branch 'develop' into stable
Diffstat (limited to 'src/SMAPI/Framework/Utilities/PathUtilities.cs')
-rw-r--r--src/SMAPI/Framework/Utilities/PathUtilities.cs62
1 files changed, 62 insertions, 0 deletions
diff --git a/src/SMAPI/Framework/Utilities/PathUtilities.cs b/src/SMAPI/Framework/Utilities/PathUtilities.cs
new file mode 100644
index 00000000..0233d796
--- /dev/null
+++ b/src/SMAPI/Framework/Utilities/PathUtilities.cs
@@ -0,0 +1,62 @@
+using System;
+using System.Diagnostics.Contracts;
+using System.IO;
+using System.Linq;
+
+namespace StardewModdingAPI.Framework.Utilities
+{
+ /// <summary>Provides utilities for normalising file paths.</summary>
+ internal static class PathUtilities
+ {
+ /*********
+ ** Properties
+ *********/
+ /// <summary>The possible directory separator characters in a file path.</summary>
+ private static readonly char[] PossiblePathSeparators = new[] { '/', '\\', Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar }.Distinct().ToArray();
+
+ /// <summary>The preferred directory separator chaeacter in an asset key.</summary>
+ private static readonly string PreferredPathSeparator = Path.DirectorySeparatorChar.ToString();
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Get the segments from a path (e.g. <c>/usr/bin/boop</c> => <c>usr</c>, <c>bin</c>, and <c>boop</c>).</summary>
+ /// <param name="path">The path to split.</param>
+ public static string[] GetSegments(string path)
+ {
+ return path.Split(PathUtilities.PossiblePathSeparators, StringSplitOptions.RemoveEmptyEntries);
+ }
+
+ /// <summary>Normalise path separators in a file path.</summary>
+ /// <param name="path">The file path to normalise.</param>
+ [Pure]
+ public static string NormalisePathSeparators(string path)
+ {
+ string[] parts = PathUtilities.GetSegments(path);
+ string normalised = string.Join(PathUtilities.PreferredPathSeparator, parts);
+ if (path.StartsWith(PathUtilities.PreferredPathSeparator))
+ normalised = PathUtilities.PreferredPathSeparator + normalised; // keep root slash
+ return normalised;
+ }
+
+ /// <summary>Get a directory or file path relative to a given source path.</summary>
+ /// <param name="sourceDir">The source folder path.</param>
+ /// <param name="targetPath">The target folder or file path.</param>
+ [Pure]
+ public static string GetRelativePath(string sourceDir, string targetPath)
+ {
+ // convert to URIs
+ Uri from = new Uri(sourceDir.TrimEnd(PathUtilities.PossiblePathSeparators) + "/");
+ Uri to = new Uri(targetPath.TrimEnd(PathUtilities.PossiblePathSeparators) + "/");
+ if (from.Scheme != to.Scheme)
+ throw new InvalidOperationException($"Can't get path for '{targetPath}' relative to '{sourceDir}'.");
+
+ // get relative path
+ string relative = PathUtilities.NormalisePathSeparators(Uri.UnescapeDataString(from.MakeRelativeUri(to).ToString()));
+ if (relative == "")
+ relative = "./";
+ return relative;
+ }
+ }
+}