[hyena] Fix relative paths in playlists (bgo#560711)
- From: Gabriel Burt <gburt src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [hyena] Fix relative paths in playlists (bgo#560711)
- Date: Wed, 26 May 2010 02:37:07 +0000 (UTC)
commit 8973b480b91a67ae607e00f015ed0d23651a77ed
Author: Jeroen Budts <jeroen lightyear be>
Date: Sat Apr 24 12:03:13 2010 +1000
Fix relative paths in playlists (bgo#560711)
Signed-off-by: Alexander Kojevnikov <alexander kojevnikov com>
src/Hyena/Hyena/Paths.cs | 44 +++++++++++++++++++++++++++++++++++++-------
1 files changed, 37 insertions(+), 7 deletions(-)
---
diff --git a/src/Hyena/Hyena/Paths.cs b/src/Hyena/Hyena/Paths.cs
index ef4831f..1294d3a 100644
--- a/src/Hyena/Hyena/Paths.cs
+++ b/src/Hyena/Hyena/Paths.cs
@@ -27,9 +27,9 @@
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
-using System;
using System.IO;
-using Mono.Unix;
+using System;
+using System.Text;
namespace Hyena
{
@@ -113,6 +113,12 @@ namespace Hyena
return null;
}
+ if (Path.IsPathRooted (path) ^ Path.IsPathRooted (to))
+ {
+ // one path is absolute, one path is relative, impossible to compare
+ return null;
+ }
+
if (path == to) {
return String.Empty;
}
@@ -121,13 +127,37 @@ namespace Hyena
to = to + Path.DirectorySeparatorChar;
}
- if (path.Length < to.Length) {
- return null;
+ if (path.StartsWith (to))
+ {
+ return path.Substring (to.Length);
+ }
+
+ return BuildRelativePath (path, to);
+ }
+
+ private static string BuildRelativePath (string path, string to)
+ {
+ var toParts = to.Split (Path.DirectorySeparatorChar);
+ var pathParts = path.Split (Path.DirectorySeparatorChar);
+
+ var i = 0;
+ while (i < toParts.Length && i < pathParts.Length && toParts [i] == pathParts [i]) {
+ i++;
+ }
+
+ var relativePath = new StringBuilder ();
+ for (int j = 0; j < toParts.Length - i - 1; j++) {
+ relativePath.Append ("..");
+ relativePath.Append (Path.DirectorySeparatorChar);
+ }
+
+ var required = new string [pathParts.Length - i];
+ for (int j = i; j < pathParts.Length; j++) {
+ required [j - i] = pathParts [j];
}
+ relativePath.Append (String.Join (Path.DirectorySeparatorChar.ToString (), required));
- return path.StartsWith (to)
- ? path.Substring (to.Length)
- : null;
+ return relativePath.ToString ();
}
public static string ApplicationData {
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]