banshee r5209 - in trunk/banshee: . src/Core/Banshee.Core/Banshee.Base src/Libraries/Hyena/Hyena src/Libraries/Hyena/Hyena/Tests



Author: alexk
Date: Wed Apr 15 23:12:15 2009
New Revision: 5209
URL: http://svn.gnome.org/viewvc/banshee?rev=5209&view=rev

Log:
2009-04-15  Alexander Kojevnikov  <alexander kojevnikov com>

	* src/Core/Banshee.Core/Banshee.Base/FileNamePattern.cs:
	* src/Libraries/Hyena/Hyena/StringUtil.cs:
	* src/Libraries/Hyena/Hyena/Tests/StringUtilTests.cs: Trimming dots and
	spaces in file and folder names (BGO #572999)

Modified:
   trunk/banshee/ChangeLog
   trunk/banshee/src/Core/Banshee.Core/Banshee.Base/FileNamePattern.cs
   trunk/banshee/src/Libraries/Hyena/Hyena/StringUtil.cs
   trunk/banshee/src/Libraries/Hyena/Hyena/Tests/StringUtilTests.cs

Modified: trunk/banshee/src/Core/Banshee.Core/Banshee.Base/FileNamePattern.cs
==============================================================================
--- trunk/banshee/src/Core/Banshee.Core/Banshee.Base/FileNamePattern.cs	(original)
+++ trunk/banshee/src/Core/Banshee.Core/Banshee.Base/FileNamePattern.cs	Wed Apr 15 23:12:15 2009
@@ -263,6 +263,7 @@
             }
             
             string songpath = CreateFromTrackInfo (track) + ext;
+            songpath = Hyena.StringUtil.EscapePath (songpath);
             string dir = Path.GetFullPath (Path.Combine (Paths.LibraryLocation, 
                 Path.GetDirectoryName (songpath)));
             string filename = Path.Combine (dir, Path.GetFileName (songpath));

Modified: trunk/banshee/src/Libraries/Hyena/Hyena/StringUtil.cs
==============================================================================
--- trunk/banshee/src/Libraries/Hyena/Hyena/StringUtil.cs	(original)
+++ trunk/banshee/src/Libraries/Hyena/Hyena/StringUtil.cs	Wed Apr 15 23:12:15 2009
@@ -28,6 +28,7 @@
 
 using System;
 using System.Collections.Generic;
+using System.IO;
 using System.Text;
 using System.Globalization;
 using System.Text.RegularExpressions;
@@ -262,6 +263,38 @@
             
             return invalid_path_regex.Replace (input, "_");
         }
+
+        private static readonly char[] escape_path_trim_chars = new char[] {'.', '\x20'};
+        public static string EscapePath (string input)
+        {
+            if (input == null)
+                return "";
+
+            // This method should be called before the full path is constructed.
+            if (Path.IsPathRooted (input)) {
+                return input;
+            }
+
+            StringBuilder builder = new StringBuilder ();
+            foreach (string name in input.Split (Path.DirectorySeparatorChar)) {
+                // Escape the directory or the file name.
+                string escaped = EscapeFilename (name);
+                // Remove leading and trailing dots and spaces.
+                escaped = escaped.Trim (escape_path_trim_chars);
+                // Skip empty names.
+                if (escaped.Length > 0) {
+                    builder.Append (escaped);
+                    builder.Append (Path.DirectorySeparatorChar);
+                }
+            }
+
+            // Chop off the last character.
+            if (builder.Length > 0) {
+                builder.Length--;
+            }
+
+            return builder.ToString ();
+        }
         
         public static string MaybeFallback (string input, string fallback)
         {

Modified: trunk/banshee/src/Libraries/Hyena/Hyena/Tests/StringUtilTests.cs
==============================================================================
--- trunk/banshee/src/Libraries/Hyena/Hyena/Tests/StringUtilTests.cs	(original)
+++ trunk/banshee/src/Libraries/Hyena/Hyena/Tests/StringUtilTests.cs	Wed Apr 15 23:12:15 2009
@@ -30,6 +30,7 @@
 #if ENABLE_TESTS
 
 using System;
+using System.IO;
 using NUnit.Framework;
 using Hyena;
     
@@ -321,6 +322,63 @@
             AssertSortKey ("\u0104", new byte[] {14, 2, 1, 27, 1, 1, 1, 0,});
         }
     }
+
+    [TestFixture]
+    public class EscapePathTests
+    {
+        private readonly char dir_sep = Path.DirectorySeparatorChar;
+
+        private void AssertProduces (string input, string output)
+        {
+            Assert.AreEqual (output, StringUtil.EscapePath (input));
+        }
+
+        private void AssertProducesSame (string input)
+        {
+            AssertProduces (input, input);
+        }
+
+        [Test]
+        public void TestEmpty ()
+        {
+            AssertProduces (null,   "");
+            AssertProduces ("",     "");
+            AssertProduces (" ",    "");
+            AssertProduces ("   ",  "");
+        }
+
+        [Test]
+        public void TestNotChanged ()
+        {
+            AssertProducesSame ("a");
+            AssertProducesSame ("aaa");
+            AssertProducesSame ("Foo Bar");
+            AssertProducesSame ("03-Nur getrÃumt");
+            AssertProducesSame ("ÐÑÐÐÐÐ");
+            AssertProducesSame ("nÇ hÇo");
+
+            AssertProducesSame (String.Format ("a{0}b.ogg", dir_sep));
+            AssertProducesSame (String.Format ("foo{0}bar{0}01. baz.ogg", dir_sep));
+            AssertProducesSame (String.Format ("{0}foo*?:", dir_sep)); // rooted, shouldn't change
+        }
+
+        [Test]
+        public void TestStripped ()
+        {
+            AssertProduces (
+                String.Format ("foo*bar{0}ham:spam.ogg", dir_sep),
+                String.Format ("foo_bar{0}ham_spam.ogg", dir_sep));
+            AssertProduces (
+                String.Format ("..lots..{0}o.f.{0}.dots.ogg.", dir_sep),
+                String.Format ("lots{0}o.f{0}dots.ogg", dir_sep));
+            AssertProduces (
+                String.Format ("foo{0}..{0}bar.ogg", dir_sep),
+                String.Format ("foo{0}bar.ogg", dir_sep));
+            AssertProduces (
+                String.Format (". foo{0}01. bar.ogg. ", dir_sep),
+                String.Format ("foo{0}01. bar.ogg", dir_sep));
+        }
+    }
     
     [TestFixture]
     public class SubstringCountTests



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]