OSDN Git Service

FilePathの抜本的見直し
authorkimikage <kimikage_ceo@hotmail.com>
Fri, 26 Nov 2010 15:57:54 +0000 (00:57 +0900)
committerkimikage <kimikage_ceo@hotmail.com>
Fri, 26 Nov 2010 15:57:54 +0000 (00:57 +0900)
Karinto/FilePath.cs
Karinto/RegexSet.cs
KarintoTest/FilePathTest.cs [new file with mode: 0755]
KarintoTest/KarintoTest.csproj
KarintoTest/RegexSetTest.cs [new file with mode: 0755]

index f857753..53a0dcf 100755 (executable)
@@ -18,54 +18,177 @@ namespace Karinto
     /// </summary>\r
     public class FilePath\r
     {\r
+        #region private or protected fields\r
         private string path;\r
-        private string suffix;\r
-        private static string WorkDirectory;\r
+        private string directory;\r
+        private bool isDirectory;\r
+        private static readonly string separator = \r
+                                        Path.DirectorySeparatorChar.ToString();\r
+        private static readonly string sepPattern = Regex.Escape(separator);\r
+            \r
+        #endregion\r
 \r
-        public static void SetDirectory(string path)\r
+        #region constructors\r
+        public FilePath(string path)\r
+            : this(path, "") \r
+        { \r
+        }\r
+\r
+        public FilePath(string path, string suffix)\r
         {\r
-            WorkDirectory = (path == null) ? "" : path;\r
+            if (path == null)\r
+            {\r
+                path = "";\r
+            }\r
+            this.path = path;\r
+           \r
+            path = Regex.Replace(path, @"[/\\]", separator);\r
+\r
+            isDirectory = Regex.IsMatch(path, sepPattern + "$");\r
+            directory = RegexSet.FileName.Replace(path, "");\r
\r
+            Suffix = (suffix == null) ? "" : suffix;\r
+            FileName = path;\r
         }\r
+        #endregion\r
 \r
-        public FilePath(string path) : this(path, "") { }\r
+        #region properties\r
 \r
-        public FilePath(string path, string suffix)\r
+        public string FileName\r
+        {\r
+            get\r
+            {\r
+                return BaseName + Suffix + Extension;\r
+            }\r
+            set\r
+            {\r
+                if (RegexSet.InvalidPathChars.IsMatch(value))\r
+                { \r
+                    throw new ArgumentException("the file name includes invalid chars.");\r
+                }\r
+                string fileName = RegexSet.FileName.Matches(value)[0].Value;\r
+                if (fileName == "")\r
+                {\r
+                    BaseName = "";\r
+                    Extension = "";\r
+                    isDirectory = true;\r
+                    return;\r
+                }\r
+                Extension = RegexSet.FileExtension.Matches(fileName)[0].Value;\r
+                BaseName = Remove(Remove(fileName, Extension), Regex.Escape(Suffix));\r
+            }\r
+        }\r
+\r
+        public string BaseName\r
         {\r
-            this.path = (path == null) ? "" : path;\r
-            this.suffix = (suffix == null) ? "" : suffix;\r
+            get;\r
+            set;\r
         }\r
 \r
-        public static implicit operator string(FilePath fp)\r
+        public string Suffix\r
         {\r
-            if (fp == null) return "";\r
-            return fp.ToString();\r
+            get;\r
+            set;\r
         }\r
 \r
-        public static implicit operator FilePath(string s)\r
+        public string Extension\r
         {\r
-            return new FilePath(s);\r
+            get;\r
+            set;\r
         }\r
 \r
-        public void SetSuffix(string suffix)\r
+        public string Directory\r
         {\r
-            this.suffix = (suffix == null) ? "" : suffix;\r
+            get;\r
+            set;\r
+        }\r
+\r
+        public FilePath Parent\r
+        {\r
+            get\r
+            {\r
+                if (IsDirectory)\r
+                {\r
+                    return "";   \r
+                }\r
+                return Directory;\r
+            }\r
+        }\r
+\r
+        public static FilePath WorkDirectory\r
+        {\r
+            get;\r
+            set;\r
+        }\r
+\r
+        public bool IsDirectory\r
+        {\r
+            get\r
+            {\r
+                return false;\r
+            }\r
         }\r
 \r
+        public bool Exists\r
+        {\r
+            get\r
+            {\r
+                return File.Exists(ToString());\r
+            }\r
+        }\r
+\r
+        #endregion\r
+\r
+        #region operators\r
+\r
+        public static implicit operator string(FilePath path)\r
+        {\r
+            if (path == null) return "";\r
+            return path.ToString();\r
+        }\r
+\r
+        public static implicit operator FilePath(string path)\r
+        {\r
+            return new FilePath(path);\r
+        }\r
+        #endregion\r
+\r
+        #region public methods\r
+\r
         public override string ToString()\r
         {\r
-            string ext = Regex.Match(path, "\\.[^\\.]+$").Groups[0].Value;\r
-            string fullpath = Regex.Replace(path, ext + "$", suffix + ext);\r
+            string fullpath = directory + FileName;\r
+            /*\r
             if (File.Exists(WorkDirectory + "\\" + fullpath))\r
             {\r
-                fullpath = WorkDirectory + "\\" + fullpath;\r
+                return WorkDirectory + "\\" + fullpath;\r
             }\r
+             */\r
             return fullpath;\r
         }\r
 \r
-        public bool Exists {\r
-            get {\r
-                return File.Exists( ToString() );\r
-            }\r
+        #endregion\r
+\r
+        #region private methods\r
+        private string Remove(string input, string pattern)\r
+        {\r
+            return Regex.Replace(input, pattern, "");\r
+        }\r
+\r
+        private string Match(string input, string pattern)\r
+        {\r
+            return Regex.Match(input, pattern).Groups[0].Value;\r
+        }\r
+\r
+        private string Match(string input, string pattern, int index)\r
+        {\r
+            return Regex.Match(input, pattern).Groups[index].Value;\r
+        }\r
+\r
+        private string Match1(string input, string pattern)\r
+        {\r
+            return Regex.Match(input, pattern).Groups[1].Value;\r
         }\r
+        #endregion\r
     }\r
 }\r
index 5268e52..d539cb3 100755 (executable)
@@ -14,10 +14,35 @@ namespace Karinto
 {\r
     public static class RegexSet\r
     {\r
+        #region constructor\r
+        static RegexSet()\r
+        {\r
+            string invalidPathChars = "";\r
+            foreach (char c in System.IO.Path.GetInvalidPathChars())\r
+            {\r
+                invalidPathChars += c;\r
+            }\r
+            InvalidPathChars = new Regex(@"[" + Regex.Escape(invalidPathChars) + @"]", RegexOptions.Compiled);\r
+        }\r
+\r
+        #endregion\r
+\r
+        #region number\r
         public static readonly Regex DecimalFloat =\r
-            new Regex(@"([-+]?(?:\d+([\.,]\d*)?|([\.,]\d+))([eE][-+]?\d+)?)", RegexOptions.Compiled);\r
+            new Regex(@"([-+]?(?:\d+([\.,]\d*)?|([\.,]\d+))([eE][-+]?\d+)?)$", RegexOptions.Compiled);\r
 \r
         public static readonly Regex HexInteger =\r
-            new Regex(@"(?:((0x)?[\da-fA-F]+)|([\da-fA-F]+[hH]?)", RegexOptions.Compiled);\r
+            new Regex(@"(?:(?:0x)?(?<1>[\da-fA-F]+))|(?:(?<1>[\da-fA-F]+)[hH]?)$", RegexOptions.Compiled);\r
+        #endregion\r
+\r
+        #region i/o\r
+        public static readonly Regex InvalidPathChars;\r
+\r
+        public static readonly Regex FileName =\r
+            new Regex(@"[^\\/:;]*$", RegexOptions.Compiled);\r
+\r
+        public static readonly Regex FileExtension =\r
+            new Regex(@"\.[^\.]+$", RegexOptions.Compiled);\r
+        #endregion\r
     }\r
 }\r
diff --git a/KarintoTest/FilePathTest.cs b/KarintoTest/FilePathTest.cs
new file mode 100755 (executable)
index 0000000..7b14ce1
--- /dev/null
@@ -0,0 +1,29 @@
+/*\r
+ *     Karinto Library Project\r
+ *\r
+ *     This software is distributed under a zlib-style license.\r
+ *     See license.txt for more information.\r
+ */\r
+\r
+using System;\r
+using System.Collections.Generic;\r
+using System.Text;\r
+using System.Diagnostics;\r
+using Karinto;\r
+using NUnit.Framework;\r
+\r
+namespace KarintoTest\r
+{\r
+    [TestFixture]\r
+    public class FilePathTest\r
+    {\r
+        [Test]\r
+        public void FileWithoutDirectory()\r
+        {\r
+            FilePath path = new FilePath("karinto.test");\r
+            Assert.AreEqual("karinto", path.BaseName);\r
+            Assert.AreEqual(".test", path.Extension);\r
+            FilePath.WorkDirectory = System.IO.Path.GetTempPath();\r
+        }\r
+    }\r
+}\r
index 6cbf248..da3a029 100755 (executable)
@@ -59,6 +59,8 @@
     <Compile Include="FftTest.cs" />\r
     <Compile Include="HiokiHicorderDataReaderTest.cs" />\r
     <Compile Include="WindowFunctionTest.cs" />\r
+    <Compile Include="FilePathTest.cs" />\r
+    <Compile Include="RegexSetTest.cs" />\r
   </ItemGroup>\r
   <ItemGroup>\r
     <None Include="app.config" />\r
diff --git a/KarintoTest/RegexSetTest.cs b/KarintoTest/RegexSetTest.cs
new file mode 100755 (executable)
index 0000000..768e999
--- /dev/null
@@ -0,0 +1,31 @@
+/*\r
+ *     Karinto Library Project\r
+ *\r
+ *     This software is distributed under a zlib-style license.\r
+ *     See license.txt for more information.\r
+ */\r
+\r
+using System;\r
+using System.Collections.Generic;\r
+using System.Text;\r
+using System.Diagnostics;\r
+using Karinto;\r
+using NUnit.Framework;\r
+\r
+namespace KarintoTest\r
+{\r
+    [TestFixture]\r
+    public class RegexSetTest\r
+    {\r
+        [Test]\r
+        public void HexInteger()\r
+        {\r
+            Assert.AreEqual(true, RegexSet.HexInteger.IsMatch("0123456789"));\r
+            Assert.AreEqual(true, RegexSet.HexInteger.IsMatch("ABCDEF"));\r
+            Assert.AreEqual(true, RegexSet.HexInteger.IsMatch("abcdef"));\r
+            Assert.AreEqual(true, RegexSet.HexInteger.IsMatch("0x1Aa"));\r
+            Assert.AreEqual(true, RegexSet.HexInteger.IsMatch("2Bbh"));\r
+        }\r
+\r
+    }\r
+}\r