Added a new class PathHelper with a method that converts absolute paths into relative paths

git-svn-id: file:///srv/devel/repo-conversion/nusu@49 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
Markus Ewald 2007-09-24 19:18:33 +00:00
parent e281b5cbb3
commit 504d96eb0a
2 changed files with 74 additions and 0 deletions

View File

@ -179,6 +179,10 @@
<Name>SimpleRectanglePacker.Test</Name> <Name>SimpleRectanglePacker.Test</Name>
<DependentUpon>SimpleRectanglePacker.cs</DependentUpon> <DependentUpon>SimpleRectanglePacker.cs</DependentUpon>
</Compile> </Compile>
<Compile Include="Source\PathHelper.cs">
<XNAUseContentPipeline>false</XNAUseContentPipeline>
<Name>PathHelper</Name>
</Compile>
<Compile Include="Source\Scheduling\AbortedException.cs"> <Compile Include="Source\Scheduling\AbortedException.cs">
<XNAUseContentPipeline>false</XNAUseContentPipeline> <XNAUseContentPipeline>false</XNAUseContentPipeline>
<Name>AbortedException</Name> <Name>AbortedException</Name>
@ -304,6 +308,7 @@
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Folder Include="Documents\" /> <Folder Include="Documents\" />
<Folder Include="Source\Parsing\" />
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<Import Project="$(MSBuildExtensionsPath)\Microsoft\XNA\Game Studio Express\v1.0\Microsoft.Xna.ContentPipeline.targets" /> <Import Project="$(MSBuildExtensionsPath)\Microsoft\XNA\Game Studio Express\v1.0\Microsoft.Xna.ContentPipeline.targets" />

69
Source/PathHelper.cs Normal file
View File

@ -0,0 +1,69 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace Nuclex.Support {
/// <summary>Utility class for path operations</summary>
public static class PathHelper {
/// <summary>Converts an absolute path into a relative one</summary>
/// <param name="basePath">Base directory the new path should be relative to</param>
/// <param name="absolutePath">Absolute path that will be made relative</param>
/// <returns>
/// A path relative to the indicated base directory that matches the
/// absolute path given.
/// </returns>
public static string MakeRelative(string basePath, string absolutePath) {
string[] baseDirectories = basePath.Split(Path.DirectorySeparatorChar);
string[] absoluteDirectories = absolutePath.Split(Path.DirectorySeparatorChar);
// Find the common root path of both paths so we know from which point on
// the two paths will differ
int lastCommonRoot = -1;
int commonLength = Math.Min(baseDirectories.Length, absoluteDirectories.Length);
for(int index = 0; index < commonLength; ++index) {
if(absoluteDirectories[index] == baseDirectories[index])
lastCommonRoot = index;
else
break;
}
// If the paths don't share a common root, we have to use an absolute path
if(lastCommonRoot == -1)
return absolutePath;
// Calculate the required length for the StrinBuilder to be slightly more
// friendly in terms of memory usage
int requiredLength = (baseDirectories.Length - (lastCommonRoot + 1)) * 3;
for(int index = lastCommonRoot + 1; index < absoluteDirectories.Length; index++)
requiredLength += absoluteDirectories[index].Length + 1;
StringBuilder relativePath = new StringBuilder(requiredLength);
// Go to the common path by adding .. until we're where we want to be
for(int index = lastCommonRoot + 1; index < baseDirectories.Length; index++) {
if(baseDirectories[index].Length > 0) {
if(relativePath.Length > 0) // // We don't want the path to start with a slash
relativePath.Append(Path.DirectorySeparatorChar);
relativePath.Append("..");
}
}
// Now that we're in the common root folder, enter the folders that
// the absolute path has in addition to the
for(int index = lastCommonRoot + 1; index < absoluteDirectories.Length; index++) {
if(relativePath.Length > 0) // We don't want the path to start with a slash
relativePath.Append(Path.DirectorySeparatorChar);
relativePath.Append(absoluteDirectories[index]);
}
return relativePath.ToString();
}
}
} // namespace Nuclex.Support