OSDN Git Service

Merge branch 'master' of ttp@git.sourceforge.jp:/gitroot/applistation/AppliStation
[applistation/AppliStation.git] / na-get-lib / NaGet / Utils.cs
index 8bd2e09..59a61d7 100644 (file)
@@ -1,8 +1,10 @@
 using System;\r
+using System.Collections.Generic;\r
 using System.IO;\r
 using System.Text;\r
 using System.Globalization;\r
 using System.Security.Principal;\r
+using System.Security.AccessControl;\r
 using System.Reflection;\r
 using System.Diagnostics;\r
 using Microsoft.Win32;\r
@@ -22,6 +24,8 @@ namespace NaGet
                {\r
                }\r
                \r
+               #region 汎用的なオブジェクト操作メソッド\r
+               \r
                /// <summary>\r
                /// オブジェクトのフィールドをコピーしてクローン化する\r
                /// </summary>\r
@@ -36,6 +40,98 @@ namespace NaGet
                        }\r
                }\r
                \r
+               #endregion\r
+               \r
+               #region リスト関連関数\r
+               \r
+               /// <summary>\r
+               /// イテレータを結合して、返す\r
+               /// </summary>\r
+               /// <param name="enus">元となる複数のイテレータ</param>\r
+               /// <returns>結合されたイテレータ</returns>\r
+               public static IEnumerable<T> MergeEnumerable<T>(params IEnumerable<T>[] enus)\r
+               {\r
+                       foreach (IEnumerable<T> enu in enus) {\r
+                               if (enu == null) continue;\r
+                               \r
+                               foreach (T elem in enu) {\r
+                                       yield return elem;\r
+                               }\r
+                       }\r
+               }\r
+\r
+               /// <summary>\r
+               /// イテレータを結合して、返す\r
+               /// </summary>\r
+               /// <param name="enus">元となる複数のイテレータ</param>\r
+               /// <returns>結合されたイテレータ</returns>\r
+               public static IEnumerable<T> MergeEnumerable<T>(params IEnumerator<T>[] enus)\r
+               {\r
+                       foreach (IEnumerator<T> enu in enus) {\r
+                               if (enu == null) continue;\r
+                               \r
+                               try {\r
+                                       while (enu.MoveNext()) {\r
+                                               yield return enu.Current;\r
+                                       }\r
+                               } finally {\r
+                                       enu.Dispose();\r
+                               }\r
+                       }\r
+               }\r
+               \r
+               /// <summary>\r
+               /// イテレータを結合して、Listとして返す\r
+               /// </summary>\r
+               /// <param name="enus">元となる複数のイテレータ</param>\r
+               /// <returns>結合されたイテレータ</returns>\r
+               public static List<T> MeargeList<T>(params IEnumerable<T>[] enus)\r
+               {\r
+                       List<T> list = new List<T>();\r
+                       \r
+                       foreach (IEnumerable<T> enu in enus) {\r
+                               if (enu == null) continue;\r
+                       \r
+                               list.AddRange(enu);\r
+                       }\r
+                       return list;\r
+               }\r
+               \r
+               /// <summary>\r
+               /// イテレータを配列に効率的に変換する\r
+               /// </summary>\r
+               /// <remarks>与えられる型が具体的にわかっているならば、それに特化した手続きをとる方が好ましい</remarks>\r
+               /// <param name="enu">元となるイテレータ</param>\r
+               /// <returns>変換された配列</returns>\r
+               public static T[] IEnumerable2Array<T>(IEnumerable<T> enu) {\r
+                       if (enu is T[]) {\r
+                               return (T[]) enu;\r
+                       } else {\r
+                               return ((enu is List<T>)? ((List<T>)enu):new List<T>(enu)).ToArray();\r
+                       }\r
+               }\r
+               \r
+               /// <summary>\r
+               /// リストに対して指定した2つの要素の位置を入れ替える\r
+               /// </summary>\r
+               /// <param name="list">操作対象のリスト</param>\r
+               /// <param name="indexA">位置</param>\r
+               /// <param name="indexB">位置</param>\r
+               public static void ListSwap(System.Collections.IList list, int indexA, int indexB)\r
+               {\r
+                       if ((indexA < 0) || (list.Count <= indexA) || (indexB < 0) || (list.Count <= indexB)) {\r
+                               throw new IndexOutOfRangeException();\r
+                       } else if (indexA != indexB) {\r
+                               object temp = list[indexA];\r
+                               list[indexA] = list[indexB];\r
+                               list[indexB] = temp;\r
+                       }\r
+               }\r
+               \r
+               #endregion\r
+                               \r
+               #region ファイル情報関連ユーテイリティ関数\r
+               \r
                /// <summary>\r
                /// パス変数に指定のフォルダを追加する\r
                /// </summary>\r
@@ -73,15 +169,13 @@ namespace NaGet
                return FormatSize((double) bytes);\r
         }\r
                \r
-               #region ファイル操作関数群\r
-               \r
                /// <summary>\r
                /// URLからそのファイル名を生成する\r
                /// </summary>\r
                /// <param name="url">対象のurl</param>\r
                public static string Url2filename(string url)\r
                {\r
-                       string filename = Path.GetFileName(UrlDecode(url, Encoding.UTF8));\r
+                       string filename = Path.GetFileName(System.Web.HttpUtility.UrlDecode(url, Encoding.UTF8));\r
                        \r
                        int pos;\r
                        if ((pos = filename.IndexOfAny(Path.GetInvalidFileNameChars())) >= 0) {\r
@@ -97,82 +191,6 @@ namespace NaGet
                }\r
                \r
                /// <summary>\r
-               /// URLのデコードを行う\r
-               /// </summary>\r
-               /// <param name="s">対象のurl文字列</param>\r
-               /// <param name="e">デコードの処理に使う文字コード</param>\r
-               public static string UrlDecode(string s, Encoding e)\r
-               {\r
-                       // mono の System.Net.HttpUtility より作成\r
-                       \r
-                       if (null == s)\r
-                               return null;\r
-\r
-                       if (s.IndexOf ('%') == -1 && s.IndexOf ('+') == -1)\r
-                               return s;\r
-\r
-                       if (e == null)\r
-                               e = Encoding.GetEncoding (28591);\r
-\r
-                       StringBuilder output = new StringBuilder ();\r
-                       long len = s.Length;\r
-                       NumberStyles hexa = NumberStyles.HexNumber;\r
-                       MemoryStream bytes = new MemoryStream ();\r
-\r
-                       for (int i = 0; i < len; i++) {\r
-                               if (s [i] == '%' && i + 2 < len) {\r
-                                       if (s [i + 1] == 'u' && i + 5 < len) {\r
-                                               if (bytes.Length > 0) {\r
-                                                       //output.Append (GetChars (bytes, e));\r
-                                                       output.Append(e.GetChars(bytes.GetBuffer(), 0, (int) bytes.Length));\r
-                                                       bytes.SetLength (0);\r
-                                               }\r
-                                               \r
-                                               output.Append ((char) int.Parse(s.Substring (i + 2, 4), hexa));\r
-                                               i += 5;\r
-                                       } else {\r
-                                               bytes.WriteByte ((byte) int.Parse(s.Substring (i + 1, 2), hexa));\r
-                                               i += 2;\r
-                                       }\r
-                                       continue;\r
-                               }\r
-\r
-                               if (bytes.Length > 0) {\r
-                                       //output.Append (GetChars (bytes, e));\r
-                                       output.Append(e.GetChars(bytes.GetBuffer(), 0, (int) bytes.Length));\r
-                                       bytes.SetLength (0);\r
-                               }\r
-\r
-                               if (s [i] == '+') {\r
-                                       output.Append (' ');\r
-                               } else {\r
-                                       output.Append (s [i]);\r
-                               }\r
-                       }\r
-\r
-                       if (bytes.Length > 0) {\r
-                               //output.Append (GetChars (bytes, e));\r
-                               output.Append(e.GetChars(bytes.GetBuffer(), 0, (int) bytes.Length));\r
-                       }\r
-\r
-                       bytes = null;\r
-                       return output.ToString ();\r
-               }\r
-               \r
-               /// <summary>\r
-               /// ファイルパスから、指定のパスセパレータの意味でファイル名を取り出す。\r
-               /// </summary>\r
-               /// <param name="filepath">ファイルパス(またはURLパス)</param>\r
-               /// <param name="separator">パスセパレータ</param>\r
-               public static string Basedir(string filepath, char separator)\r
-               {\r
-                       int dirSep = filepath.LastIndexOf(separator);\r
-                       if (dirSep < 0) return "";\r
-\r
-                       return filepath.Substring(0, dirSep);\r
-               }\r
-               \r
-               /// <summary>\r
                /// 再帰的にファイルの属性を指定します。強制的にフォルダの再帰削除の前に読み込み専用属性を消すのに使います。\r
                /// </summary>\r
                /// <param name="path">設定するフォルダ</param>\r
@@ -194,6 +212,27 @@ namespace NaGet
                }\r
                \r
                /// <summary>\r
+               /// 再帰的にファイルのアクセス権限(ユーザ権限など)を設定します\r
+               /// </summary>\r
+               /// <param name="path">設定するフォルダ</param>\r
+               /// <param name="filesec">変更先権限</param>\r
+               public static void SetAccessControlRecursive(string path, FileSecurity filesec)\r
+               {\r
+                       // 自分自身の権限を変更\r
+                       File.SetAccessControl(path, filesec);\r
+                       \r
+                       // 子ファイルの権限を変更\r
+                       foreach (string file in Directory.GetFiles(path)) {\r
+                               File.SetAccessControl(file, filesec);\r
+                       }\r
+                       \r
+                       // 子フォルダを再帰的に権限変更\r
+                       foreach (string file in Directory.GetDirectories(path)) {\r
+                               SetAccessControlRecursive(file, filesec);\r
+                       }\r
+               }\r
+               \r
+               /// <summary>\r
                /// ファイルまたはフォルダの容量を算出して返す\r
                /// </summary>\r
                /// <param name="path">\r
@@ -240,7 +279,7 @@ namespace NaGet
                        }\r
 \r
                        string[] pathArray = pattern.Split(Path.DirectorySeparatorChar);\r
-                       System.Collections.Generic.List<string> extended = new System.Collections.Generic.List<string>();\r
+                       List<string> extended = new List<string>();\r
                        try {\r
                                if (pathArray.Length == 1) {\r
                                        extended.AddRange(Directory.GetFiles(baseDir, pathArray[0], SearchOption.TopDirectoryOnly));\r
@@ -319,7 +358,7 @@ namespace NaGet
                public static string GetDotsRemovedPath(string aPath)\r
                {\r
                        string[] folders = splitPath(aPath);\r
-                       System.Collections.Generic.List<string> newFolders = new System.Collections.Generic.List<string>();\r
+                       List<string> newFolders = new List<string>();\r
                        \r
                        foreach (string fol in folders) {\r
                                if (fol == ".") {\r
@@ -337,8 +376,13 @@ namespace NaGet
                \r
                #endregion\r
                \r
-               #region XmlSerializer便利メソッド群\r
+               #region シリアル化関連\r
                \r
+               /// <summary>\r
+               /// XMLでシリアル化したオブジェクトのXMLファイルを読み込み、デシリアル化したオブジェクトを取得する\r
+               /// </summary>\r
+               /// <param name="path">XMLファイルのパス</param>\r
+               /// <returns>デシリアル化されたオブジェクト</returns>\r
                public static T GetDeserializedObject<T>(string path)\r
                {\r
                        T retVal = default(T);\r
@@ -349,6 +393,11 @@ namespace NaGet
                        return retVal;\r
                }\r
                \r
+               /// <summary>\r
+               /// オブジェクトをXMLでシリアル化してファイルに書き込む\r
+               /// </summary>\r
+               /// <param name="path">XMLファイルのパス</param>\r
+               /// <param name="obj">シリアル化する対象のオブジェクト</param>\r
                public static void PutSerializeObject<T>(string path, T obj)\r
                {\r
                        using (FileStream fs = new FileStream(path, FileMode.Create, FileAccess.Write)) {\r
@@ -362,12 +411,11 @@ namespace NaGet
                #region 権限関連関数群\r
                \r
                /// <summary>\r
-               /// 現在のユーザがAdministrators権限を持っているか否かを返す\r
+               /// 現在のユーザがAdministrators権限を持っているか否かを返す\r
                /// </summary>\r
+               /// <remarks>UAC有効時には権限昇格後になってtrueを返すようになります</remarks>\r
                public static bool IsAdministrators()\r
                {\r
-                       // TODO UAC はどうするんだ!!!!\r
-                       \r
                        // 現在の Windows ユーザーを現在のスレッドのプリンシパルに反映する\r
                        AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal );\r
                        IPrincipal prin = System.Threading.Thread.CurrentPrincipal;\r
@@ -454,6 +502,7 @@ namespace NaGet
                \r
                #endregion\r
 \r
+               #region イベント情報\r
                \r
                /// <summary>\r
                /// 任意データのイベント情報を表現するクラス\r
@@ -477,5 +526,7 @@ namespace NaGet
                                get { return data; }\r
                        }\r
                }\r
+\r
+               #endregion\r
        }\r
 }\r