// Copyright ? Microsoft Corporation.
// This source file is subject to the Microsoft Permissive License.
// See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx.
// All other rights reserved.
using System;
using System.Text;
using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;
using System.Diagnostics;
using System.Collections.Generic;
class Util {
///
/// Returns the path argument adjusted to be relative to the base path. Absolute path names will
/// be returned unchanged.
///
///
/// path: "xxx/aaa/target.html"
/// basePath: "xxx/bbb/source.html"
/// result: "../aaa/target.html"
///
public static string GetRelativePath(string path, string basePath) {
// ignore absolute path names and an empty basePath
if (!string.IsNullOrEmpty(path) && path[0] != '/' && !string.IsNullOrEmpty(basePath)) {
List pathParts = new List(path.Split('/'));
List basePathParts = new List(basePath.Split('/'));
// remove the base path file name
if (basePathParts.Count > 0)
basePathParts.RemoveAt(basePathParts.Count - 1);
// strip common base path bits
while (pathParts.Count > 0 && basePathParts.Count > 0 &&
string.Equals(pathParts[0], basePathParts[0], StringComparison.CurrentCultureIgnoreCase)) {
pathParts.RemoveAt(0);
basePathParts.RemoveAt(0);
}
// move up one level for each remaining base path part
foreach (string s in basePathParts)
pathParts.Insert(0, "..");
path = string.Join("/", pathParts.ToArray());
}
return path;
}
}